/* * GoToFolder.c -- MCDE: the small "Go to Folder" dialog (GoToFolder.h) * * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. * * The reference's way to a path by keyboard: a title, "Go to the folder:", * one field, Cancel and Go. "~" and "~user" are understood (getpwnam, no * shell), Tab completes from the folder named so far -- a unique name is * added, otherwise the candidates appear in a list under the field and * choosing one takes it. A path that is not a folder leaves the dialog * open and says so under the field; Go remembers the path in * ~/.dt/last-folder for the next time. * * C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md). */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Encaps.h" #include "SharedProcs.h" #include "FileMgr.h" #include "Navigation.h" #include "GoToFolder.h" #define MAX_CANDIDATES 256 #define LIST_ROWS 6 #define BASE_HEIGHT 132 typedef struct { FileMgrRec *rec; Widget shell; Widget form; Widget label; Widget field; Widget list; /* the candidates, hidden until needed */ Widget error; Widget go; Widget cancel; char *typed_dir; /* the part of the field before the name */ } GoTo; static GoTo state; static void GoCB(Widget w, XtPointer client, XtPointer call); static void CancelCB(Widget w, XtPointer client, XtPointer call); static void ListChosen(Widget w, XtPointer client, XtPointer call); static void KeyHandler(Widget w, XtPointer client, XEvent *event, Boolean *cont); /* ---- the path in the field --------------------------------------------- */ /* ~ and ~user as the reference understands them; no shell. XtFree the * result; NULL when the user does not exist. */ static char * ExpandPath(const char *in) { const char *home; char *out; const char *rest; char user[128]; struct passwd *pw; size_t n; if (in == NULL || in[0] != '~') return XtNewString(in != NULL ? in : ""); rest = strchr(in, '/'); if (in[1] == '\0' || in[1] == '/') { home = getenv("HOME"); if (home == NULL) { pw = getpwuid(getuid()); home = pw != NULL ? pw->pw_dir : NULL; } if (rest == NULL) rest = "/"; } else { size_t len = rest != NULL ? (size_t) (rest - in - 1) : strlen(in + 1); if (len >= sizeof user) return NULL; memcpy(user, in + 1, len); user[len] = '\0'; pw = getpwnam(user); if (pw == NULL) return NULL; home = pw->pw_dir; if (rest == NULL) rest = "/"; } if (home == NULL || home[0] == '\0') return NULL; n = strlen(home) + strlen(rest) + 1; if ((out = XtMalloc((Cardinal) n)) == NULL) return NULL; (void)snprintf(out, n, "%s%s", home, rest); return out; } static void LastFolderFile(char *buf, size_t len) { const char *home = getenv("HOME"); (void)snprintf(buf, len, "%s/.dt/last-folder", home != NULL ? home : "."); } static void SaveLast(const char *path) { char file[PATH_MAX], dir[PATH_MAX]; FILE *fp; char *slash; LastFolderFile(file, sizeof file); (void)snprintf(dir, sizeof dir, "%s", file); if ((slash = strrchr(dir, '/')) != NULL) { *slash = '\0'; (void)mkdir(dir, 0700); } if ((fp = fopen(file, "w")) != NULL) { (void)fprintf(fp, "%s\n", path); (void)fclose(fp); } } /* the last folder, or "~/" */ static void LoadLast(char *buf, size_t len) { char file[PATH_MAX]; FILE *fp; LastFolderFile(file, sizeof file); buf[0] = '\0'; if ((fp = fopen(file, "r")) != NULL) { if (fgets(buf, (int) len, fp) == NULL) buf[0] = '\0'; (void)fclose(fp); buf[strcspn(buf, "\r\n")] = '\0'; } if (buf[0] == '\0') (void)snprintf(buf, len, "~/"); } /* ---- the window --------------------------------------------------------- */ static void SetError(GoTo *g, const char *text) { XmString s = XmStringCreateLocalized((char *) text); XtVaSetValues(g->error, XmNlabelString, s, NULL); XmStringFree(s); } static void HideList(GoTo *g) { if (XtIsManaged(XtParent(g->list))) XtUnmanageChild(XtParent(g->list)); } static void ShowList(GoTo *g, char **names, int n) { int i; if (!XtIsManaged(g->list)) XtManageChild(g->list); XmListDeleteAllItems(g->list); for (i = 0; i < n; i++) { XmString s = XmStringCreateLocalized(names[i]); XmListAddItemUnselected(g->list, s, 0); XmStringFree(s); } XtManageChild(XtParent(g->list)); } static void Build(GoTo *g) { Arg args[4]; Widget box, buttons; XmString s; g->shell = XtVaCreatePopupShell("mcdeGoToFolder", topLevelShellWidgetClass, g->rec->shell, XmNtitle, "Go to Folder", XmNallowShellResize, True, NULL); /* a column, not a form: every part keeps its own height and the * shell grows with the list when it appears (measured 2026-11-14: a * form whose hidden list sat between two attachments collapsed) */ box = XtVaCreateManagedWidget("mcdeGoToBox", xmRowColumnWidgetClass, g->shell, XmNorientation, XmVERTICAL, XmNpacking, XmPACK_TIGHT, XmNmarginWidth, 12, XmNmarginHeight, 12, XmNspacing, 8, NULL); g->label = XtVaCreateManagedWidget("mcdeGoToLabel", xmLabelWidgetClass, box, XmNalignment, XmALIGNMENT_BEGINNING, NULL); s = XmStringCreateLocalized((char *) "Go to the folder:"); XtVaSetValues(g->label, XmNlabelString, s, NULL); XmStringFree(s); XtSetArg(args[0], XmNcolumns, 40); g->field = XmCreateTextField(box, "mcdeGoToField", args, 1); XtManageChild(g->field); g->error = XtVaCreateManagedWidget("mcdeGoToError", xmLabelWidgetClass, box, XmNalignment, XmALIGNMENT_BEGINNING, NULL); XtSetArg(args[0], XmNvisibleItemCount, LIST_ROWS); XtSetArg(args[1], XmNselectionPolicy, XmBROWSE_SELECT); g->list = XmCreateScrolledList(box, "mcdeGoToList", args, 2); XtVaSetValues(XtParent(g->list), XmNwidth, 380, XmNheight, 12 * LIST_ROWS + 8, NULL); /* the list is managed inside its scrolled window, the window joins * the column only while there are candidates (XtParent) */ XtManageChild(g->list); /* the buttons, right-aligned in a form that spans the column */ buttons = XtVaCreateManagedWidget("mcdeGoToButtons", xmFormWidgetClass, box, XmNwidth, 380, XmNheight, 34, NULL); g->go = XtVaCreateManagedWidget("mcdeGoToGo", xmPushButtonWidgetClass, buttons, XmNtopAttachment, XmATTACH_FORM, XmNbottomAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, NULL); g->cancel = XtVaCreateManagedWidget("mcdeGoToCancel", xmPushButtonWidgetClass, buttons, XmNtopAttachment, XmATTACH_FORM, XmNbottomAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_WIDGET, XmNrightWidget, g->go, XmNrightOffset, 8, NULL); s = XmStringCreateLocalized((char *) "Cancel"); XtVaSetValues(g->cancel, XmNlabelString, s, NULL); XmStringFree(s); s = XmStringCreateLocalized((char *) "Go"); XtVaSetValues(g->go, XmNlabelString, s, NULL); XmStringFree(s); XtAddCallback(g->go, XmNactivateCallback, GoCB, g); XtAddCallback(g->cancel, XmNactivateCallback, CancelCB, g); XtAddCallback(g->list, XmNbrowseSelectionCallback, ListChosen, g); XtAddEventHandler(g->shell, KeyPressMask, False, KeyHandler, g); XtVaSetValues(g->field, XmNuserData, g, NULL); } /* ---- completion --------------------------------------------------------- */ /* the field's text completed from the folder it names: a unique name is * added; when the candidates only agree in part, that part is added and the * next Tab shows the list (the reference's Tab) */ static void Complete(GoTo *g) { char *text, *expanded, *slash, *dir, *typed_dir, *prefix; DIR *d; struct dirent *de; char *names[MAX_CANDIDATES]; int n = 0, i, keep = 0; char common[NAME_MAX + 1]; char *new = NULL; size_t len; HideList(g); text = XmTextFieldGetString(g->field); expanded = ExpandPath(text); if (expanded == NULL) { SetError(g, "There is no such user."); XBell(XtDisplay(g->field), 0); XtFree(text); return; } slash = strrchr(expanded, '/'); if (slash != NULL) { char *typed_slash = strrchr(text, '/'); prefix = slash + 1; /* the part the user typed before the name, in the ~ form */ if (typed_slash != NULL) { size_t at = (size_t) (typed_slash - text) + 1; typed_dir = XtMalloc((Cardinal) (at + 1)); memcpy(typed_dir, text, at); typed_dir[at] = '\0'; } else typed_dir = XtNewString(""); *slash = '\0'; dir = expanded[0] != '\0' ? expanded : (char *) "/"; } else { prefix = expanded; typed_dir = XtNewString(""); dir = (char *) "."; } if (g->typed_dir != NULL) XtFree(g->typed_dir); g->typed_dir = typed_dir; if ((d = opendir(dir)) != NULL) { while ((de = readdir(d)) != NULL) { if (de->d_name[0] == '.') continue; /* hidden folders: as the reference */ if (strncmp(de->d_name, prefix, strlen(prefix)) != 0) continue; if (n < MAX_CANDIDATES) names[n] = XtNewString(de->d_name); n++; } closedir(d); } if (n == 0) { SetError(g, "No folder of that name."); XBell(XtDisplay(g->field), 0); XtFree(expanded); XtFree(text); return; } len = strlen(names[0]); (void)snprintf(common, sizeof common, "%s", names[0]); for (i = 1; i < n && i < MAX_CANDIDATES; i++) { size_t j = 0; while (j < len && names[i][j] == common[j]) j++; common[j] = '\0'; len = j; } if (n == 1 || len > strlen(prefix)) { /* one name, or the part all candidates agree on */ char path[PATH_MAX]; struct stat st; const char *add = n == 1 ? names[0] : common; size_t l = strlen(typed_dir) + strlen(add) + 2; (void)snprintf(path, sizeof path, "%s%s%s", strcmp(dir, "/") == 0 ? "" : dir, "/", add); new = XtMalloc((Cardinal) l); (void)snprintf(new, l, "%s%s%s", typed_dir, add, n == 1 && stat(path, &st) == 0 && S_ISDIR(st.st_mode) ? "/" : ""); if (n == 1) SetError(g, ""); else XBell(XtDisplay(g->field), 0); } else { for (i = 0; i < n && i < MAX_CANDIDATES; i++) { size_t l = strlen(typed_dir) + strlen(names[i]) + 2; char *shown = XtMalloc((Cardinal) l); (void)snprintf(shown, l, "%s%s", typed_dir, names[i]); XtFree(names[i]); names[i] = shown; } ShowList(g, names, n < MAX_CANDIDATES ? n : MAX_CANDIDATES); SetError(g, ""); keep = 1; } for (i = 0; i < n && i < MAX_CANDIDATES; i++) if (!keep) XtFree(names[i]); if (new != NULL) { XmTextFieldSetString(g->field, new); XmTextFieldSetInsertionPosition(g->field, XmTextFieldGetLastPosition(g->field)); XtFree(new); } XtFree(expanded); XtFree(text); } /* ---- buttons and keys --------------------------------------------------- */ static void Accept(GoTo *g) { char *text = XmTextFieldGetString(g->field); char *path = ExpandPath(text); struct stat st; if (path == NULL || stat(path, &st) != 0 || !S_ISDIR(st.st_mode)) { SetError(g, "There is no folder of that name."); XBell(XtDisplay(g->field), 0); } else { SaveLast(path); _McdeNavGoTo(g->rec, path); XtPopdown(g->shell); } XtFree(text); if (path != NULL) XtFree(path); } static void Cancel(GoTo *g) { XtPopdown(g->shell); } static GoTo * StateOf(Widget w) { GoTo *g = NULL; XtVaGetValues(w, XmNuserData, &g, NULL); return g; } static void GoCB(Widget w, XtPointer client, XtPointer call) { (void)w; (void)call; Accept((GoTo *) client); } static void CancelCB(Widget w, XtPointer client, XtPointer call) { (void)w; (void)call; Cancel((GoTo *) client); } static void ListChosen(Widget w, XtPointer client, XtPointer call) { GoTo *g = client; XmString *items = NULL; int count = 0; char *text = NULL; char path[PATH_MAX], *expanded; struct stat st; (void)w; (void)call; XtVaGetValues(g->list, XmNselectedItems, &items, XmNselectedItemCount, &count, NULL); if (count < 1 || items == NULL || items[0] == NULL) return; if (!XmStringGetLtoR(items[0], XmFONTLIST_DEFAULT_TAG, &text)) return; expanded = ExpandPath(text); (void)snprintf(path, sizeof path, "%s", expanded != NULL ? expanded : ""); if (expanded != NULL) XtFree(expanded); if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) { size_t l = strlen(text) + 2; char *with_slash = XtMalloc((Cardinal) l); (void)snprintf(with_slash, l, "%s/", text); XmTextFieldSetString(g->field, with_slash); XtFree(with_slash); } else XmTextFieldSetString(g->field, text); XtFree(text); HideList(g); XmProcessTraversal(g->field, XmTRAVERSE_CURRENT); XmTextFieldSetSelection(g->field, 0, XmTextFieldGetLastPosition(g->field), CurrentTime); } static void KeyHandler(Widget w, XtPointer client, XEvent *event, Boolean *cont) { GoTo *g = client; KeySym key = XLookupKeysym(&event->xkey, 0); (void)w; if (key == XK_Escape) { Cancel(g); *cont = False; } } static void AcceptAction(Widget w, XEvent *event, String *params, Cardinal *n) { GoTo *g = StateOf(w); (void)event; (void)params; (void)n; if (g != NULL) Accept(g); } static void CancelAction(Widget w, XEvent *event, String *params, Cardinal *n) { GoTo *g = StateOf(w); (void)event; (void)params; (void)n; if (g != NULL) Cancel(g); } static void CompleteAction(Widget w, XEvent *event, String *params, Cardinal *n) { GoTo *g = StateOf(w); (void)event; (void)params; (void)n; if (g != NULL) Complete(g); } /* ---- what the menu calls ------------------------------------------------ */ void _GoToFolderShow(FileMgrRec *rec) { GoTo *g = &state; char last[PATH_MAX]; static XtActionsRec table[] = { { "McdeGoToAccept", AcceptAction }, { "McdeGoToCancel", CancelAction }, { "McdeGoToComplete", CompleteAction } }; static Boolean actions; if (rec == NULL) return; g->rec = rec; if (g->shell == NULL) Build(g); if (!actions) { XtAppAddActions(XtWidgetToApplicationContext(g->shell), table, XtNumber(table)); XtOverrideTranslations(g->field, XtParseTranslationTable( "Return:McdeGoToAccept()\n" "osfCancel:McdeGoToCancel()\n" "Tab:McdeGoToComplete()")); actions = True; } LoadLast(last, sizeof last); XmTextFieldSetString(g->field, last); SetError(g, ""); HideList(g); XtVaSetValues(g->shell, XmNtransientFor, rec->shell, NULL); XtPopup(g->shell, XtGrabNone); XmProcessTraversal(g->field, XmTRAVERSE_CURRENT); XmTextFieldSetSelection(g->field, 0, XmTextFieldGetLastPosition(g->field), CurrentTime); }