Files
mcde/programs/dtfile/Navigation.c
T
lotherk 96909c0813 MCDE dtfile: the strip view, the fourth Finder view without 3D (t0090)
A new view beside icons, small icons and the columns (Command+4): the chosen
picture large in the middle, three neighbours each side, flat, in Motif's
frame colours -- no perspective, no reflection, no glide; a redraw happens on
Expose and when the choice changes.  Under the strip the name is bold and
the kind follows (the type's label), then a scroll bar for the strip, then
the folder's list; the chosen line is selected and moves with the strip.

Pictures come from McdeThumb: only the visible entries are read, and the
second visit of a folder reads ~/.dt/thumbs (MCDE_THUMB_DEBUG=1 makes the
cache line appear; tools/mcde-shot.sh passes the variable into the session).
A file that is no picture is named where its neighbours are.

The arrows walk the strip (the list follows), Space is the file window's
preview, and the toolbar has the fifth toggle "Strip" -- its icon is the
folder's.  Command+4 was the small-icon view; that moved to Command+5
(docs/reference/shortcuts.txt), as the strip takes the fourth place of the
reference's four views.

Measured under Xvfb with xdotool, a folder of 13 PNGs:

  - Command+4 shows Bild-01 large with its neighbours
    (docs/screenshots/leopard/finder-cover-flow-mcde.png shows Bild-03
    chosen: two left, three right, "Bild-03.png" bold over "PNG", the list
    with Bild-03 selected);
  - Right twice walks to Bild-03 and the list's third line is selected;
  - switching to Icons and back to Strip serves three of the visible
    pictures from the cache: "thumb: .../Bild-02.png from the cache" and its
    neighbours in the log (MCDE_THUMB_DEBUG=1);
  - the strip's own drawing area takes the arrows and Space.

Open: the strip has its own list, not CDE's work-area list below it (the
status line and the other views' selection are not shared), and the split is
fixed -- no sash to drag.  Idle traffic was not measured separately; the
strip draws only on Expose and on a change, and McdeThumb reads no timer.
2026-11-14 11:37:22 +00:00

951 lines
26 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 <dirent.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/PushBG.h>
#include <Xm/ScrolledW.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 <Mcde/McdeSys.h>
#include "Navigation.h"
#include "Search.h"
#include "Strip.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" },
{ "Utilities", "~/.dt/Applications/Utilities" },
{ "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) */
{ "Strip", -2 }, /* MCDE's own (Strip.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 strip view: pictures and the list in place of the work area */
if (views[i].view == -2) {
if (!rec->mcde_strip_on && rec->mcde_work != NULL) {
if (rec->mcde_columns_on) {
rec->mcde_columns_on = False;
_McdeColumnsShow(rec, rec->mcde_work, NULL,
False);
}
_McdeStripShow(rec, rec->mcde_work,
data->current_directory, True);
}
return;
}
if (rec->mcde_strip_on) {
rec->mcde_strip_on = False;
_McdeStripShow(rec, rec->mcde_work, NULL, 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, "strip") == 0)
ViewTo(rec, ViewNamed("Strip"));
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;
}
/* ---- the sidebar: groups of places, devices, shares and searches -------- */
typedef struct {
FileMgrRec *rec;
int place; /* >= 0: places[]; -1: path; -2: network;
* -3: a search */
char *path;
int when; /* the search's time */
char *name; /* the search's name criterion */
} SideEntry;
typedef struct {
FileMgrRec *rec;
Widget col;
Widget placeholder;
} SharedFill;
static void
SideGroupToggled(Widget w, XtPointer client, XtPointer call)
{
XmToggleButtonCallbackStruct *c = (XmToggleButtonCallbackStruct *) call;
Widget col = (Widget) client;
XmString ls = NULL;
char *text = NULL;
XtVaGetValues(w, XmNlabelString, &ls, NULL);
if (ls == NULL || !XmStringGetLtoR(ls, XmFONTLIST_DEFAULT_TAG, &text))
return;
text[0] = c->set ? 'v' : '>';
{
XmString s = XmStringCreateLocalized(text);
XtVaSetValues(w, XmNlabelString, s, NULL);
XmStringFree(s);
}
XtFree(text);
if (c->set)
XtManageChild(col);
else
XtUnmanageChild(col);
}
/* a group of the sidebar: the folding head, and the column of its entries */
static Widget
SideGroup(Widget parent, const char *title)
{
Widget head, col;
XmString s;
char label[64];
(void)snprintf(label, sizeof label, "v %s", title);
s = XmStringCreateLocalized(label);
head = XtVaCreateManagedWidget("sideGroup", xmToggleButtonWidgetClass,
parent, XmNlabelString, s, XmNindicatorOn, False, XmNset, True,
XmNalignment, XmALIGNMENT_BEGINNING, XmNmarginTop, 8,
XmNmarginBottom, 2, NULL);
XmStringFree(s);
col = XtVaCreateManagedWidget("sideEntries", xmRowColumnWidgetClass,
parent, XmNspacing, 1, XmNmarginWidth, 10, XmNmarginHeight, 0,
NULL);
XtAddCallback(head, XmNvalueChangedCallback, SideGroupToggled, col);
return col;
}
static Widget
SideItem(Widget col, const char *label, XtCallbackProc cb, SideEntry *data,
Boolean sensitive)
{
XmString s = XmStringCreateLocalized((char *) label);
Widget b = XtVaCreateManagedWidget("sideItem", xmPushButtonGadgetClass,
col, XmNlabelString, s, XmNalignment, XmALIGNMENT_BEGINNING,
XmNmarginHeight, 1, XmNtraversalOn, False, NULL);
XmStringFree(s);
if (cb != NULL)
XtAddCallback(b, XmNactivateCallback, cb, data);
if (!sensitive)
XtSetSensitive(b, False);
return b;
}
static void
SideEntryCB(Widget w, XtPointer client, XtPointer call)
{
SideEntry *e = client;
(void) w;
(void) call;
if (e == NULL || e->rec == NULL)
return;
if (e->place >= 0)
PlaceTo(e->rec, e->place);
else if (e->place == -1 && e->path != NULL) {
_McdeNetShow(e->rec, False);
GoTo(e->rec, e->path, False);
} else if (e->place == -2)
_McdeNetShow(e->rec, True);
else if (e->place == -3)
_McdeSearchShow(e->rec, e->name, e->when, NULL);
}
static SideEntry *
SideData(FileMgrRec *rec, int place, const char *path)
{
SideEntry *e = XtNew(SideEntry);
e->rec = rec;
e->place = place;
e->path = path != NULL ? XtNewString(path) : NULL;
e->when = 0;
e->name = NULL;
return e;
}
/* a search of the SEARCH FOR group: the time-based ones, and the saved
* (~/.dt/searches/NAME.search: name=, when=, scope=) */
static SideEntry *
SideSearch(FileMgrRec *rec, const char *name, int when)
{
SideEntry *e = SideData(rec, -3, NULL);
e->when = when;
e->name = name != NULL ? XtNewString(name) : NULL;
return e;
}
static int
WhenOf(const char *w)
{
if (w == NULL)
return MCDE_SEARCH_ANY;
if (strcmp(w, "today") == 0)
return MCDE_SEARCH_TODAY;
if (strcmp(w, "yesterday") == 0)
return MCDE_SEARCH_YESTERDAY;
if (strcmp(w, "week") == 0)
return MCDE_SEARCH_WEEK;
return MCDE_SEARCH_ANY;
}
/* the saved searches, by their file: name, when, scope */
static void
FillSavedSearches(FileMgrRec *rec, Widget col)
{
const char *home = getenv("HOME");
char dir[1024];
DIR *d;
struct dirent *de;
int shown = 0;
if (home == NULL)
return;
(void)snprintf(dir, sizeof dir, "%s/.dt/searches", home);
if ((d = opendir(dir)) == NULL)
return;
while ((de = readdir(d)) != NULL && shown < 12) {
char path[1600], line[512], label[128], *dot;
FILE *fp;
int when = MCDE_SEARCH_ANY;
char *query = NULL;
if (de->d_name[0] == '.')
continue;
dot = strrchr(de->d_name, '.');
if (dot == NULL || strcmp(dot, ".search") != 0)
continue;
(void)snprintf(path, sizeof path, "%s/%s", dir, de->d_name);
if ((fp = fopen(path, "r")) == NULL)
continue;
while (fgets(line, sizeof line, fp) != NULL) {
line[strcspn(line, "\r\n")] = '\0';
if (strncmp(line, "name=", 5) == 0) {
XtFree(query);
query = XtNewString(line + 5);
} else if (strncmp(line, "when=", 5) == 0)
when = WhenOf(line + 5);
}
fclose(fp);
(void)snprintf(label, sizeof label, "%.*s", (int) (dot -
de->d_name), de->d_name);
SideItem(col, label, SideEntryCB, SideSearch(rec, query, when),
True);
XtFree(query);
shown++;
}
closedir(d);
}
/* the drives and partitions that are mounted, the system's own left out */
static void
FillDevices(FileMgrRec *rec, Widget col)
{
McdeSysDisk disks[32];
int n = mcde_sys_disks(disks, 32), i, shown = 0;
for (i = 0; i < n && shown < 8; i++) {
char label[200];
if (disks[i].mountpoint[0] == '\0' || disks[i].system)
continue;
(void)snprintf(label, sizeof label, "%s%s",
disks[i].label[0] != '\0' ? disks[i].label : disks[i].name,
disks[i].removable ? " (removable)" : "");
SideItem(col, label, SideEntryCB, SideData(rec, -1,
disks[i].mountpoint), True);
shown++;
}
if (shown == 0)
(void) SideItem(col, "No device mounted", NULL, NULL, False);
}
/* the computers found on the local network, once, after the window is up */
static void
FillSharedLater(XtPointer client, XtIntervalId *id)
{
SharedFill *sf = client;
McdeSysNetHost hosts[16];
int n, i, shown = 0;
(void) id;
if (sf == NULL)
return;
n = mcde_sys_net_browse(hosts, 16, 1500);
if (sf->placeholder != NULL && XtIsManaged(sf->placeholder)) {
XtUnmanageChild(sf->placeholder);
XtDestroyWidget(sf->placeholder);
}
for (i = 0; i < n && shown < 8; i++) {
SideItem(sf->col, hosts[i].name[0] != '\0' ? hosts[i].name :
hosts[i].host, SideEntryCB, SideData(sf->rec, -2, NULL),
True);
shown++;
}
if (shown == 0)
(void) SideItem(sf->col, "No computer found", NULL, NULL, False);
XtFree((char *) sf);
}
static void
MakeSidebar(FileMgrRec *rec, Widget form, Widget bar)
{
Arg args[8];
int n = 0, i;
Widget frame, sw, col, group;
SharedFill *sf;
static const char *const searches[] = { "Today", "Yesterday",
"Past Week", "All Images", "All Movies", "All Documents" };
(void) searches;
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);
sw = XtVaCreateManagedWidget("mcdeSidebarScroll",
xmScrolledWindowWidgetClass, frame,
XmNscrollingPolicy, XmAUTOMATIC,
XmNwidth, SIDEBAR_WIDTH, NULL);
col = XtVaCreateManagedWidget("mcdeSidebar", xmRowColumnWidgetClass, sw,
XmNorientation, XmVERTICAL, XmNpacking, XmPACK_TIGHT, NULL);
rec->mcde_sidebar = col;
group = SideGroup(col, "DEVICES");
FillDevices(rec, group);
group = SideGroup(col, "SHARED");
sf = XtNew(SharedFill);
sf->rec = rec;
sf->col = group;
sf->placeholder = SideItem(group, "Looking for computers...", NULL,
NULL, False);
XtAppAddTimeOut(XtWidgetToApplicationContext(frame), 200, FillSharedLater,
sf);
group = SideGroup(col, "PLACES");
for (i = 0; i < (int) NPLACES; i++)
(void) SideItem(group, places[i].label, SideEntryCB,
SideData(rec, i, NULL), True);
group = SideGroup(col, "SEARCH FOR");
(void) SideItem(group, "Today", SideEntryCB,
SideSearch(rec, NULL, MCDE_SEARCH_TODAY), True);
(void) SideItem(group, "Yesterday", SideEntryCB,
SideSearch(rec, NULL, MCDE_SEARCH_YESTERDAY), True);
(void) SideItem(group, "Past Week", SideEntryCB,
SideSearch(rec, NULL, MCDE_SEARCH_WEEK), True);
/* the type-based ones wait for a type criterion */
for (i = 3; i < (int) (sizeof searches / sizeof searches[0]); i++)
(void) SideItem(group, searches[i], NULL, NULL, False);
FillSavedSearches(rec, group);
XtManageChild(frame);
rec->mcde_sidebar_frame = frame;
}
Widget
_McdeNavCreate(FileMgrRec *rec, Widget parent)
{
Widget form, bar, views_rc, frame;
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: devices, shares, places and searches (Navigation.c) */
MakeSidebar(rec, form, bar);
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);
_McdeStripCreate(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 :
views[i].view == -2 ? rec->mcde_strip_on :
!rec->mcde_columns_on && !rec->mcde_strip_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);
_McdeStripFollow(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);
}