Files
mcde/programs/dtfile/Navigation.c
T
lotherkandClaude Sonnet 5.5 edb0e6949c MCDE: Mac OS X keyboard shortcuts through the desktop (Cmd+space dtlaunch, f.mcde_app, bar commands, dtterm/dtfile keys); fuse-afpfs-ng dropped from the build
The makronix set list is regenerated (46569 entries,
build/logs/x-userland-2026-11-13d.log); mcde-host exit=0
(build/logs/mcde-host-2026-11-13c.log).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-13 12:05:06 +00:00

649 lines
18 KiB
C

/*
* Navigation.c -- MCDE: the navigation of the file manager window: a toolbar
* (back, forward, the view), a sidebar with the places, a history per window,
* and the free space in the status line.
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* Written in C99 against POSIX and Motif (ext/src/mcde/AGENTS.md); nothing
* here knows the operating system. The rest of dtfile is CDE's: this file
* only adds widgets around its work area and calls its own functions to
* change the directory (ChangeDirectoryToParent, which checks the path the
* way the "Go To" dialog does) and the view (as PrefsP.c applies a new view).
*
* Since 2026-11-09: toolbar, places, history, free space, the search field;
* since 2026-11-11 the column view (Columns.c). Not yet: icons on the
* toolbar buttons, devices in the sidebar, localized strings (catgets).
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/RowColumn.h>
#include <Xm/PushB.h>
#include <Xm/TextF.h>
#include <Xm/ToggleB.h>
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/Frame.h>
#include <Xm/Separator.h>
#include <Dt/Connect.h>
#include <Dt/SharedProcs.h>
#include "Encaps.h"
#include "SharedProcs.h"
#include "FileMgr.h"
#include "Desktop.h"
#include "Main.h"
#include "Prefs.h"
#include "Navigation.h"
#define SIDEBAR_WIDTH 150
/* the places of the sidebar: a label and a path below $HOME, or absolute */
static const struct {
const char *label;
const char *path; /* "~" is $HOME, "~/x" below it */
} places[] = {
{ "Computer", "/" },
{ "Home", "~" },
/* the applications by category, made by dtappscan at login */
{ "Applications", "~/.dt/Applications" },
{ "Desktop", "~/Desktop" },
{ "Documents", "~/Documents" },
{ "Downloads", "~/Downloads" },
{ "Network", NULL }, /* the network view (NetView.c) */
};
#define NPLACES (sizeof places / sizeof places[0])
/* ~/Network is the network: shares are mounted below it (NetView.c,
* MountPoint), and a window that goes to the folder itself shows the
* computers around -- the front panel's Network opens it so (dtfile -dir,
* action McdeNetwork in dt.dt) */
static Boolean
IsNetworkDir(const char *dir)
{
const char *home = getenv("HOME");
size_t n;
if (dir == NULL || home == NULL || home[0] == '\0')
return False;
n = strlen(home);
return strncmp(dir, home, n) == 0 &&
(strcmp(dir + n, "/Network") == 0 ||
strcmp(dir + n, "/Network/") == 0);
}
/* the three views of the toolbar, as dtfile names them (Prefs.h) */
static const struct {
const char *label;
int view;
} views[MCDE_NVIEWS] = {
{ "Icons", BY_NAME_AND_ICON },
{ "Small", BY_NAME_AND_SMALL_ICON },
{ "List", BY_ATTRIBUTES },
{ "Columns", -1 }, /* MCDE's own (Columns.c) */
};
/* the search text of each window, by its FileMgrData: kept here, not in
* FileMgrData, whose fields are saved and restored as resources */
#define MAX_SEARCHES 32
static struct {
FileMgrData *data;
char *text;
} searches[MAX_SEARCHES];
static Boolean clearing; /* the field is emptied by a new directory */
static char **
SearchOf(FileMgrData *data, Boolean make)
{
int i, free_slot = -1;
for (i = 0; i < MAX_SEARCHES; i++) {
if (searches[i].data == data)
return &searches[i].text;
if (searches[i].data == NULL && free_slot < 0)
free_slot = i;
}
if (!make || free_slot < 0)
return NULL;
searches[free_slot].data = data;
searches[free_slot].text = NULL;
return &searches[free_slot].text;
}
/* a name is hidden when the window has a search text that it does not
* contain (in any case); "." and ".." stay, the way back up */
Boolean
_McdeNavHidden(FileMgrData *data, const char *name)
{
char **text = SearchOf(data, False);
const char *t, *n;
if (text == NULL || *text == NULL || (*text)[0] == '\0' || name == NULL)
return False;
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
return False;
for (n = name; *n != '\0'; n++) {
for (t = *text; *t != '\0' && n[t - *text] != '\0' &&
tolower((unsigned char)n[t - *text]) ==
tolower((unsigned char)*t); t++)
;
if (*t == '\0')
return False;
}
return True;
}
static void SyncViews(FileMgrRec *rec);
static FileMgrData *
DataOf(FileMgrRec *rec)
{
DialogData *dd = _DtGetInstanceData((XtPointer)rec);
return dd != NULL ? (FileMgrData *)dd->data : NULL;
}
/* "~/x" -> "$HOME/x"; the result is XtMalloc'd */
static char *
ExpandPlace(const char *path)
{
const char *home = getenv("HOME");
size_t len;
char *s;
if (path[0] != '~')
return XtNewString(path);
if (home == NULL || home[0] == '\0')
home = "/";
len = strlen(home) + strlen(path);
s = XtMalloc(len + 1);
(void)snprintf(s, len + 1, "%s%s", home, path + 1);
return s;
}
static void
GoTo(FileMgrRec *rec, const char *path, Boolean from_history)
{
char *p = XtNewString(path);
rec->mcde_navigating = from_history;
ChangeDirectoryToParent(rec, p); /* checks the path, shows errors */
XtFree(p);
rec->mcde_navigating = False;
}
void
_McdeNavGoTo(FileMgrRec *rec, const char *path)
{
GoTo(rec, path, False);
}
static void
UpdateButtons(FileMgrRec *rec)
{
if (rec->mcde_back == NULL)
return;
XtSetSensitive(rec->mcde_back, rec->mcde_history_pos > 0);
XtSetSensitive(rec->mcde_forward,
rec->mcde_history_pos + 1 < rec->mcde_history_len);
}
static void
BackCB(Widget w, XtPointer client, XtPointer call)
{
FileMgrRec *rec = (FileMgrRec *)client;
(void)w; (void)call;
if (rec->mcde_history_pos > 0) {
rec->mcde_history_pos--;
GoTo(rec, rec->mcde_history[rec->mcde_history_pos], True);
UpdateButtons(rec);
}
}
static void
ForwardCB(Widget w, XtPointer client, XtPointer call)
{
FileMgrRec *rec = (FileMgrRec *)client;
(void)w; (void)call;
if (rec->mcde_history_pos + 1 < rec->mcde_history_len) {
rec->mcde_history_pos++;
GoTo(rec, rec->mcde_history[rec->mcde_history_pos], True);
UpdateButtons(rec);
}
}
static void
ViewTo(FileMgrRec *rec, int i)
{
FileMgrData *data;
if (i < 0 || i >= MCDE_NVIEWS || (data = DataOf(rec)) == NULL)
return;
_McdeNetShow(rec, False);
/* the column view: the columns in place of the work area */
if (views[i].view == -1) {
if (!rec->mcde_columns_on && rec->mcde_work != NULL) {
rec->mcde_columns_on = True;
_McdeColumnsShow(rec, rec->mcde_work,
data->current_directory, True);
}
return;
}
if (rec->mcde_columns_on) {
rec->mcde_columns_on = False;
_McdeColumnsShow(rec, rec->mcde_work, NULL, False);
}
if (data->view == views[i].view)
return;
/* as PrefsP.c applies a view chosen in the Preferences dialog */
data->view = views[i].view;
if (data->show_type == SINGLE_DIRECTORY)
data->view_single = views[i].view;
else
data->view_tree = views[i].view;
data->newSize = True;
FileMgrRedisplayFiles(rec, data, False);
}
static void
ViewCB(Widget w, XtPointer client, XtPointer call)
{
FileMgrRec *rec = (FileMgrRec *)client;
XmToggleButtonCallbackStruct *cbs = (XmToggleButtonCallbackStruct *)call;
int i;
if (!cbs->set)
return;
for (i = 0; i < MCDE_NVIEWS; i++)
if (rec->mcde_view[i] == w)
break;
ViewTo(rec, i);
}
static void
SearchCB(Widget w, XtPointer client, XtPointer call)
{
FileMgrRec *rec = (FileMgrRec *)client;
FileMgrData *data = DataOf(rec);
char **text;
(void)call;
if (clearing || data == NULL || (text = SearchOf(data, True)) == NULL)
return;
XtFree(*text);
*text = XmTextFieldGetString(w);
/* as the filter dialog applies a new filter (Filter.c) */
FileMgrRedisplayFiles(rec, data, False);
}
static void
PlaceTo(FileMgrRec *rec, int i)
{
char *path;
if (i < 0 || (size_t)i >= NPLACES)
return;
if (places[i].path == NULL) {
/* the network is the folder ~/Network (IsNetworkDir) */
path = ExpandPlace("~/Network");
(void) mkdir(path, 0700);
GoTo(rec, path, False);
XtFree(path);
_McdeNetShow(rec, True);
return;
}
_McdeNetShow(rec, False);
path = ExpandPlace(places[i].path);
GoTo(rec, path, False);
XtFree(path);
}
static void
PlaceCB(Widget w, XtPointer client, XtPointer call)
{
FileMgrRec *rec = (FileMgrRec *)client;
XmListCallbackStruct *cbs = (XmListCallbackStruct *)call;
(void)w;
PlaceTo(rec, cbs->item_position - 1);
}
/* the index of a place or a view by its label (the tables above) */
static int
PlaceNamed(const char *name)
{
size_t i;
for (i = 0; i < NPLACES; i++)
if (strcmp(places[i].label, name) == 0)
return (int)i;
return -1;
}
static int
ViewNamed(const char *name)
{
int i;
for (i = 0; i < MCDE_NVIEWS; i++)
if (strcmp(views[i].label, name) == 0)
return i;
return -1;
}
/*
* The window's Command keys that have no menu item to accelerate
* (docs/reference/shortcuts.txt): Command+[ and Command+] walk the history,
* Command+1..4 choose a view (Command+4 the small icons, as Finder has
* them), Shift+Command+... goes to a place of the sidebar. The key table is
* FileMgr.c's (translations_da, the McdeKey action); this is what the
* buttons of the toolbar and the sidebar do, called by name.
*/
Boolean
_McdeNavKey(FileMgrRec *rec, const char *what)
{
int i;
if (rec == NULL || what == NULL)
return False;
if (strcmp(what, "back") == 0)
BackCB(rec->mcde_back, rec, NULL);
else if (strcmp(what, "forward") == 0)
ForwardCB(rec->mcde_forward, rec, NULL);
else if (strcmp(what, "icons") == 0)
ViewTo(rec, ViewNamed("Icons"));
else if (strcmp(what, "list") == 0)
ViewTo(rec, ViewNamed("List"));
else if (strcmp(what, "columns") == 0)
ViewTo(rec, ViewNamed("Columns"));
else if (strcmp(what, "small") == 0)
ViewTo(rec, ViewNamed("Small"));
else {
if (strcmp(what, "computer") == 0)
i = PlaceNamed("Computer");
else if (strcmp(what, "desktop") == 0)
i = PlaceNamed("Desktop");
else if (strcmp(what, "applications") == 0)
i = PlaceNamed("Applications");
else if (strcmp(what, "network") == 0)
i = PlaceNamed("Network");
else
return False;
PlaceTo(rec, i);
}
/* the buttons of the toolbar follow a view chosen by a key */
SyncViews(rec);
return True;
}
static Widget
Button(Widget parent, const char *name, const char *label,
XtCallbackProc cb, XtPointer client)
{
XmString s = XmStringCreateLocalized((char *)label);
Arg args[2];
Widget b;
XtSetArg(args[0], XmNlabelString, s);
XtSetArg(args[1], XmNtraversalOn, False);
b = XmCreatePushButton(parent, (char *)name, args, 2);
XmStringFree(s);
XtAddCallback(b, XmNactivateCallback, cb, client);
XtManageChild(b);
return b;
}
Widget
_McdeNavCreate(FileMgrRec *rec, Widget parent)
{
Widget form, bar, views_rc, frame;
XmString items[NPLACES];
Arg args[12];
size_t k;
int i, n;
rec->mcde_history = NULL;
rec->mcde_history_len = 0;
rec->mcde_history_pos = -1;
rec->mcde_navigating = False;
form = XmCreateForm(parent, "mcdeNavigation", NULL, 0);
/* the toolbar: back, forward, then the views */
n = 0;
XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNspacing, 4); n++;
XtSetArg(args[n], XmNmarginHeight, 4); n++;
bar = XmCreateRowColumn(form, "mcdeToolbar", args, n);
rec->mcde_toolbar = bar;
rec->mcde_back = Button(bar, "mcdeBack", "< Back", BackCB, rec);
rec->mcde_forward = Button(bar, "mcdeForward", "Forward >", ForwardCB,
rec);
n = 0;
XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
XtSetArg(args[n], XmNradioBehavior, True); n++;
XtSetArg(args[n], XmNradioAlwaysOne, True); n++;
XtSetArg(args[n], XmNspacing, 0); n++;
views_rc = XmCreateRowColumn(bar, "mcdeViews", args, n);
for (i = 0; i < MCDE_NVIEWS; i++) {
XmString s = XmStringCreateLocalized((char *)views[i].label);
n = 0;
XtSetArg(args[n], XmNlabelString, s); n++;
XtSetArg(args[n], XmNindicatorOn, False); n++;
XtSetArg(args[n], XmNshadowThickness, 2); n++;
XtSetArg(args[n], XmNtraversalOn, False); n++;
rec->mcde_view[i] = XmCreateToggleButton(views_rc,
"mcdeView", args, n);
XmStringFree(s);
XtAddCallback(rec->mcde_view[i], XmNvalueChangedCallback,
ViewCB, rec);
XtManageChild(rec->mcde_view[i]);
}
XtManageChild(views_rc);
/* the search: what the window shows, narrowed to names with the text */
{
XmString s = XmStringCreateLocalized("Search:");
n = 0;
XtSetArg(args[n], XmNlabelString, s); n++;
XtManageChild(XmCreateLabel(bar, "mcdeSearchLabel", args, n));
XmStringFree(s);
n = 0;
XtSetArg(args[n], XmNcolumns, 16); n++;
rec->mcde_search = XmCreateTextField(bar, "mcdeSearch", args, n);
XtAddCallback(rec->mcde_search, XmNvalueChangedCallback, SearchCB,
rec);
XtManageChild(rec->mcde_search);
}
XtManageChild(bar);
/* the form gives children without a size of their own what is left, and
* that is nothing once dtfile sets the window to its saved size: the
* toolbar keeps the height it asks for */
{
XtWidgetGeometry pref;
if (XtQueryGeometry(bar, NULL, &pref) != XtGeometryNo &&
(pref.request_mode & CWHeight))
XtVaSetValues(bar, XmNheight, pref.height, NULL);
}
UpdateButtons(rec);
/* the sidebar: the places */
for (k = 0; k < NPLACES; k++)
items[k] = XmStringCreateLocalized((char *)places[k].label);
n = 0;
XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
XtSetArg(args[n], XmNtopWidget, bar); n++;
XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNshadowType, XmSHADOW_IN); n++;
XtSetArg(args[n], XmNwidth, SIDEBAR_WIDTH + 4); n++;
frame = XmCreateFrame(form, "mcdeSidebarFrame", args, n);
n = 0;
XtSetArg(args[n], XmNitems, items); n++;
XtSetArg(args[n], XmNitemCount, (int)NPLACES); n++;
XtSetArg(args[n], XmNvisibleItemCount, (int)NPLACES); n++;
XtSetArg(args[n], XmNselectionPolicy, XmBROWSE_SELECT); n++;
XtSetArg(args[n], XmNwidth, SIDEBAR_WIDTH); n++;
XtSetArg(args[n], XmNlistMarginWidth, 6); n++;
XtSetArg(args[n], XmNlistMarginHeight, 6); n++;
XtSetArg(args[n], XmNshadowThickness, 0); n++;
rec->mcde_sidebar = XmCreateList(frame, "mcdeSidebar", args, n);
for (k = 0; k < NPLACES; k++)
XmStringFree(items[k]);
XtAddCallback(rec->mcde_sidebar, XmNbrowseSelectionCallback, PlaceCB,
rec);
XtManageChild(rec->mcde_sidebar);
XtManageChild(frame);
rec->mcde_sidebar_frame = frame;
XtManageChild(form);
SyncViews(rec);
return form;
}
void
_McdeNavAttachWork(FileMgrRec *rec, Widget work)
{
Arg args[6];
int n = 0;
XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
XtSetArg(args[n], XmNtopWidget, rec->mcde_toolbar); n++;
XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
XtSetArg(args[n], XmNleftWidget, rec->mcde_sidebar_frame); n++;
XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
XtSetValues(work, args, n);
/* the column view, ready beside it (Columns.c) */
rec->mcde_work = work;
_McdeColumnsCreate(rec, XtParent(work), work);
_McdeNetCreate(rec, XtParent(work), work);
}
/* the view buttons follow the window's view. The first window gets its
* directory without ShowNewDirectory (FileMgr.c), and its data is attached
* after the toolbar is made: it is tried again until the data is there. */
static int syncTries;
static void
SyncViewsLater(XtPointer client, XtIntervalId *id)
{
FileMgrRec *rec = (FileMgrRec *)client;
FileMgrData *data = DataOf(rec);
int i;
(void)id;
if (data == NULL) {
if (++syncTries < 20)
XtAppAddTimeOut(XtWidgetToApplicationContext(
rec->mcde_toolbar), 200, SyncViewsLater, client);
return;
}
syncTries = 0;
/* the first directory came without ShowNewDirectory: it starts the
* history, so that Back leads to it */
if (rec->mcde_history_len == 0 && data->current_directory != NULL) {
rec->mcde_history = (char **)XtMalloc(sizeof(char *));
rec->mcde_history[0] = XtNewString(data->current_directory);
rec->mcde_history_len = 1;
rec->mcde_history_pos = 0;
UpdateButtons(rec);
/* a window opened on ~/Network shows the network */
if (IsNetworkDir(data->current_directory))
_McdeNetShow(rec, True);
}
for (i = 0; i < MCDE_NVIEWS; i++)
XmToggleButtonSetState(rec->mcde_view[i],
views[i].view == -1 ? rec->mcde_columns_on :
!rec->mcde_columns_on && data->view == views[i].view, False);
}
static void
SyncViews(FileMgrRec *rec)
{
SyncViewsLater((XtPointer)rec, NULL);
}
void
_McdeNavVisit(FileMgrRec *rec, const char *dir)
{
FileMgrData *data;
int i;
if (rec == NULL || rec->mcde_toolbar == NULL || dir == NULL)
return;
if (!rec->mcde_navigating) {
if (rec->mcde_history_pos >= 0 &&
strcmp(rec->mcde_history[rec->mcde_history_pos], dir) == 0)
return;
/* a new way: what was ahead of us is forgotten */
for (i = rec->mcde_history_pos + 1; i < rec->mcde_history_len;
i++)
XtFree(rec->mcde_history[i]);
rec->mcde_history_len = rec->mcde_history_pos + 1;
rec->mcde_history = (char **)XtRealloc(
(char *)rec->mcde_history,
(Cardinal)((rec->mcde_history_len + 1) * sizeof(char *)));
rec->mcde_history[rec->mcde_history_len++] = XtNewString(dir);
rec->mcde_history_pos = rec->mcde_history_len - 1;
}
UpdateButtons(rec);
/* a new directory: the search starts empty */
if ((data = DataOf(rec)) != NULL) {
char **text = SearchOf(data, False);
if (text != NULL && *text != NULL && (*text)[0] != '\0') {
XtFree(*text);
*text = NULL;
}
}
if (rec->mcde_search != NULL) {
clearing = True;
XmTextFieldSetString(rec->mcde_search, "");
clearing = False;
}
_McdeColumnsFollow(rec, dir);
/* a folder was gone to (Back, a path): the network view leaves,
* unless the folder is the network's */
_McdeNetShow(rec, IsNetworkDir(dir));
SyncViews(rec);
}
void
_McdeNavFreeSpace(const char *dir, char *buf, size_t len)
{
struct statvfs sv;
double bytes;
size_t used = strlen(buf);
if (dir == NULL || statvfs(dir, &sv) != 0 || used + 1 >= len)
return;
bytes = (double)sv.f_bavail * (double)sv.f_frsize;
if (bytes >= 1e9)
(void)snprintf(buf + used, len - used, ", %.1f GB available",
bytes / 1e9);
else
(void)snprintf(buf + used, len - used, ", %.0f MB available",
bytes / 1e6);
}