diff --git a/docs/screenshots/.gitignore b/docs/screenshots/.gitignore new file mode 100644 index 0000000000..7745c06577 --- /dev/null +++ b/docs/screenshots/.gitignore @@ -0,0 +1,2 @@ +# the log mcde-shot.sh writes beside the picture is the measurement, not the picture +*.log diff --git a/docs/screenshots/AGENTS.md b/docs/screenshots/AGENTS.md new file mode 100644 index 0000000000..1b2d63ae8b --- /dev/null +++ b/docs/screenshots/AGENTS.md @@ -0,0 +1,33 @@ +# docs/screenshots/ -- our own pictures of MCDE, beside the reference + +## What it is for + +A state of an MCDE program as it looks in this tree, so that a change can be +compared with what it was meant to look like. One directory per reference: +`leopard/` holds the states taken from the Mac OS X 10.5 screenshots that +drive the MCDE work (the tasks in `tasksd`, scope `makronix/mcde`). + +## The rule + +- **Only our own pictures.** A reference from another system stays a URL in + its task; its artwork is not copied into this repository. A file here is a + screenshot of MCDE made with `tools/mcde-shot.sh`. +- The name is the reference's name with `-mcde` and `.png`: + `finder-quick-look-mcde.png` for + `10-5-Leopard-Finder-Quick-Look.png`. +- The picture shows the program, not a desk: the Xvfb screen + (`tools/mcde-shot.sh`), no private home, no window of the developer. +- One picture per state a task calls for (the overview and the search, the + window and its dialog), replaced when the state changes -- not a gallery of + every intermediate attempt. + +## How one is made + + MCDE_SHOT_SETUP='mkdir -p Pictures && cp /pfad/*.png Pictures/' \ + MCDE_SHOT_ACTIONS='xdotool mousemove 355 210 click 1; xdotool key space' \ + tools/mcde-shot.sh docs/screenshots/leopard/finder-quick-look-mcde.png \ + 8 dtfile -dir $PWD/build/mcde-shots/home/Pictures + +`MCDE_SHOT_SETUP` fills the fresh test home before the program starts, +`MCDE_SHOT_ACTIONS` drives it (keys, clicks) before the picture is taken. +Both are for the measurement; the file that is committed is the `.png`. diff --git a/docs/screenshots/leopard/finder-quick-look-mcde.png b/docs/screenshots/leopard/finder-quick-look-mcde.png new file mode 100644 index 0000000000..94cc9e3efc Binary files /dev/null and b/docs/screenshots/leopard/finder-quick-look-mcde.png differ diff --git a/docs/screenshots/leopard/finder-quick-look-text-mcde.png b/docs/screenshots/leopard/finder-quick-look-text-mcde.png new file mode 100644 index 0000000000..8e8d48e6c5 Binary files /dev/null and b/docs/screenshots/leopard/finder-quick-look-text-mcde.png differ diff --git a/ext/src/mcde/programs/dtfile/Columns.c b/ext/src/mcde/programs/dtfile/Columns.c index 8e3f449301..78d17a51b0 100644 --- a/ext/src/mcde/programs/dtfile/Columns.c +++ b/ext/src/mcde/programs/dtfile/Columns.c @@ -44,6 +44,7 @@ #include "SharedProcs.h" #include "FileMgr.h" #include "Navigation.h" +#include "Peek.h" #define COLUMN_WIDTH 180 #define PREVIEW_WIDTH 210 @@ -332,6 +333,25 @@ AddColumn(int k, const char *dir) wins[k].ncol++; } +static int Chosen(int k, Widget list, int *entry); + +/* MCDE: the entry the column view has chosen, the deepest one; the space + * key of the window previews it (Peek.c). The caller frees the string. */ +char * +_McdeColumnsChosen(FileMgrRec *rec) +{ + int k = WinOf(rec, False), ci, entry; + + if (k < 0 || !rec->mcde_columns_on) + return NULL; + for (ci = wins[k].ncol - 1; ci >= 0; ci--) { + if (Chosen(k, wins[k].col[ci].list, &entry) == ci) + return Join(wins[k].col[ci].dir, + wins[k].col[ci].names[entry]); + } + return NULL; +} + /* the column of LIST, and the entry chosen in it; -1 if none */ static int Chosen(int k, Widget list, int *entry) @@ -365,6 +385,8 @@ ColumnChosen(Widget w, XtPointer client, XtPointer call) while (wins[k].ncol > ci + 1) FreeColumn(&wins[k].col[--wins[k].ncol]); Preview(k, path); + /* MCDE: the preview window, if it is up, follows the column */ + _McdePeekPath(rec, path); if (IsDir(path)) { AddColumn(k, path); Layout(k); diff --git a/ext/src/mcde/programs/dtfile/File.c b/ext/src/mcde/programs/dtfile/File.c index df29577021..0998d9c3c1 100644 --- a/ext/src/mcde/programs/dtfile/File.c +++ b/ext/src/mcde/programs/dtfile/File.c @@ -209,6 +209,7 @@ #include "Encaps.h" #include "SharedProcs.h" #include "FileMgr.h" +#include "Peek.h" #include "Navigation.h" #include "Desktop.h" #include "Main.h" @@ -2725,6 +2726,10 @@ SelectFile( /* mark selected */ SetFileSelected(file_mgr_data, file_view_data); + + /* MCDE: the preview follows the selection (Peek.c) */ + if (file_mgr_data != trashFileMgrData) + _McdePeekFollow((FileMgrRec *) file_mgr_data->file_mgr_rec); } @@ -2767,6 +2772,10 @@ DeselectFile( SetFileUnselected(file_mgr_data, file_view_data); else file_view_data->selected = False; + + /* MCDE: the preview follows the selection (Peek.c) */ + if (file_mgr_data != trashFileMgrData) + _McdePeekFollow((FileMgrRec *) file_mgr_data->file_mgr_rec); } @@ -2870,6 +2879,9 @@ DeselectAllFiles( file_mgr_data->selection_list[0] = NULL; file_mgr_data->selected_file_count = 0; + /* MCDE: the preview follows the selection (Peek.c) */ + if (file_mgr_data != trashFileMgrData) + _McdePeekFollow((FileMgrRec *) file_mgr_data->file_mgr_rec); } diff --git a/ext/src/mcde/programs/dtfile/FileMgr.c b/ext/src/mcde/programs/dtfile/FileMgr.c index 0e09990691..cb0211ad57 100644 --- a/ext/src/mcde/programs/dtfile/FileMgr.c +++ b/ext/src/mcde/programs/dtfile/FileMgr.c @@ -161,6 +161,7 @@ #include "Desktop.h" #include "Main.h" #include "Navigation.h" +#include "Peek.h" #include "Common.h" #include "Filter.h" #include "Find.h" @@ -480,7 +481,7 @@ char translations_escape[] = "osfCancel:EscapeFM()"; #define DOUBLE_CLICK_DRAG #ifndef DOUBLE_CLICK_DRAG char translations_da[] = "\ -osfCancel:DrawingAreaInput() ManagerGadgetSelect()\n\ +osfCancel:McdeKey(@,peekesc)\n\ plus:DrawingAreaInput() ManagerGadgetSelect()\n\ minus:DrawingAreaInput() ManagerGadgetSelect()\n\ :DrawingAreaInput() ManagerGadgetArm()\n\ @@ -520,7 +521,7 @@ Mod4k:McdeKey(@,connect)\n\ :DrawingAreaInput()\n\ :DrawingAreaInput()\n\ :osfActivate:DrawingAreaInput() ManagerParentActivate()\n\ -:osfCancel:DrawingAreaInput() ManagerGadgetSelect()\n\ +:osfCancel:McdeKey(@,peekesc)\n\ :osfHelp:DrawingAreaInput() ManagerGadgetHelp()\n\ :osfSelect:DrawingAreaInput() ManagerGadgetSelect()\n\ :osfDelete:DrawingAreaInput() ManagerGadgetSelect()\n\ @@ -529,10 +530,11 @@ Mod4k:McdeKey(@,connect)\n\ osfBeginLine:FMInput(@)\n\ :osfPageDown:FMInput(@)\n\ :osfPageUp:FMInput(@)\n\ -:F1:DrawingAreaInput() ManagerGadgetHelp()\n\ +Mod1space:McdeKey(@,peekfull)\n\ +space:McdeKey(@,peek)\n\ +F1:DrawingAreaInput() ManagerGadgetHelp()\n\ s c a c: ActivateClist(@)\n\ ~s ~m ~a Return:DrawingAreaInput() ManagerParentActivate() ManagerGadgetSelect()\n\ -space:DrawingAreaInput() ManagerGadgetSelect()\n\ :plus:DrawingAreaInput() ManagerGadgetSelect()\n\ :minus:DrawingAreaInput() ManagerGadgetSelect()\n\ :DrawingAreaInput() ManagerGadgetKeyInput()\n\ @@ -2407,9 +2409,24 @@ McdeKey( if( num_params == NULL || *num_params < 2 || *params[0] == '@' ) return ; + sscanf( params[0], "%p", (void **) &fileMgrRec ); if( strcmp( params[1], "connect" ) == 0 ) _McdeNetConnect( fileMgrRec ); + else if( strcmp( params[1], "peek" ) == 0 ) + _McdePeekToggle( fileMgrRec ); + else if( strcmp( params[1], "peekfull" ) == 0 ) + _McdePeekFullScreen( fileMgrRec ); + else if( strcmp( params[1], "peekesc" ) == 0 ) + { + /* Escape closes the preview; without one it does what it did + * (the two actions of the rule this replaced) */ + if( _McdePeekClose( fileMgrRec ) == False ) + { + XtCallActionProc( wid, "DrawingAreaInput", event, NULL, 0 ); + XtCallActionProc( wid, "ManagerGadgetSelect", event, NULL, 0 ); + } + } else (void) _McdeNavKey( fileMgrRec, params[1] ); diff --git a/ext/src/mcde/programs/dtfile/Makefile.am b/ext/src/mcde/programs/dtfile/Makefile.am index 9847b337d8..bc085eb0bc 100644 --- a/ext/src/mcde/programs/dtfile/Makefile.am +++ b/ext/src/mcde/programs/dtfile/Makefile.am @@ -41,7 +41,7 @@ dtfile_SOURCES = ChangeDir.c ChangeDirP.c Command.c Common.c \ ModAttr.c ModAttrP.c MultiView.c OverWrite.c \ Prefs.c PrefsP.c SharedMsgs.c SharedProcs.c \ ToolTalk.c Trash.c Utils.c fsDialog.c \ - Navigation.c Columns.c NetView.c + Navigation.c Columns.c NetView.c Peek.c # Mind the quoting here... SCRIPTFLAGS = -DSHAPE -D_ILS_MACROS -DSUN_PERF \ diff --git a/ext/src/mcde/programs/dtfile/Navigation.h b/ext/src/mcde/programs/dtfile/Navigation.h index 16a8375c97..68a6b0201d 100644 --- a/ext/src/mcde/programs/dtfile/Navigation.h +++ b/ext/src/mcde/programs/dtfile/Navigation.h @@ -25,6 +25,8 @@ extern void _McdeColumnsCreate(FileMgrRec *rec, Widget parent, Widget work); extern void _McdeColumnsShow(FileMgrRec *rec, Widget work, const char *dir, Boolean on); extern void _McdeColumnsFollow(FileMgrRec *rec, const char *dir); +/* the entry the column view has chosen, or NULL; the caller frees it */ +extern char *_McdeColumnsChosen(FileMgrRec *rec); /* the network view (NetView.c) */ extern void _McdeNetCreate(FileMgrRec *rec, Widget parent, Widget work); extern void _McdeNetShow(FileMgrRec *rec, Boolean on); diff --git a/ext/src/mcde/programs/dtfile/Peek.c b/ext/src/mcde/programs/dtfile/Peek.c new file mode 100644 index 0000000000..12888b3bf0 --- /dev/null +++ b/ext/src/mcde/programs/dtfile/Peek.c @@ -0,0 +1,772 @@ +/* + * Peek.c -- MCDE: the preview of the chosen file (Peek.h) + * + * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. + * + * Space shows what is chosen without opening it: a picture at the size it + * fits, a text's first 64 KB, a folder's icon and how much is in it, the + * type's icon for everything else. Space again, Escape or the close button + * hides it; while it is up, the selection in the window behind it keeps + * changing it, as the arrow keys of the reference's list do. + * + * The window is a plain Motif top level shell in the palette: no + * transparency, no glass, no fade. A picture is read in the idle time + * (McdeThumbRequest), so a folder of large photographs does not stop the + * window, and only the picture that is asked for is read. + * + * Option+Space puts the preview over the whole screen + * (_NET_WM_STATE_FULLSCREEN, dtwm's WmEwmh.c), Option+Space or Escape + * brings it back. + * + * C99, POSIX and Motif (ext/src/mcde/AGENTS.md). + */ + +#include +#include +#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 "Peek.h" + +#define MAX_WINDOWS 32 +#define MAX_TEXT (64 * 1024) +#define MAX_W 620 /* the picture's room on a 1024 x 768 screen */ +#define MAX_H 440 +#define SMALL_W 420 +#define SMALL_H 220 + +typedef struct { + FileMgrRec *rec; + Widget shell; + Widget form; + Widget buttons; + Widget icon; /* the type's picture, for a folder */ + Widget info; /* one line under it */ + Widget image; /* the picture itself */ + Widget text; /* the scrolled text */ + Widget open; + Widget full; + Pixmap pixmap; /* of image */ + Pixmap iconpm; /* of icon */ + char *path; /* what is shown, NULL for none */ + void *request; /* the picture still being read */ + int iw, ih; + Boolean shown; /* XtPopup has been called */ + Boolean fullscreen; +} Peek; + +static Peek peeks[MAX_WINDOWS]; + +static void PictureReady(void *client, Pixmap pixmap, int w, int h); +static void RefocusLater(XtPointer client, XtIntervalId *id); +static void Room(Peek *p, int *maxw, int *maxh); +static void KeyHandler(Widget w, XtPointer client, XEvent *event, + Boolean *cont); +static void OpenCB(Widget w, XtPointer client, XtPointer call); +static void FullCB(Widget w, XtPointer client, XtPointer call); + +static FileMgrData * +DataOf(FileMgrRec *rec) +{ + DialogData *dd = _DtGetInstanceData((XtPointer) rec); + + return dd != NULL ? (FileMgrData *) dd->data : NULL; +} + +static Peek * +PeekOf(FileMgrRec *rec, Boolean make) +{ + int i; + + for (i = 0; i < MAX_WINDOWS; i++) + if (peeks[i].rec == rec) + return &peeks[i]; + if (!make) + return NULL; + for (i = 0; i < MAX_WINDOWS; i++) + if (peeks[i].rec == NULL) { + memset(&peeks[i], 0, sizeof peeks[i]); + peeks[i].rec = rec; + peeks[i].pixmap = XmUNSPECIFIED_PIXMAP; + peeks[i].iconpm = XmUNSPECIFIED_PIXMAP; + return &peeks[i]; + } + return NULL; +} + +static char * +Join(const char *dir, const char *name) +{ + size_t n = strlen(dir) + strlen(name) + 2; + char *p = XtMalloc((Cardinal) n); + + (void)snprintf(p, n, "%s%s%s", dir, + strcmp(dir, "/") == 0 ? "" : "/", name); + return p; +} + +/* the path of what is chosen in the window, or NULL */ +static char * +ChosenPath(FileMgrData *data) +{ + FileViewData *fvd; + DirectorySet *ds; + + if (data == NULL || data->selected_file_count != 1 || + data->selection_list == NULL) + return NULL; + fvd = data->selection_list[0]; + if (fvd == NULL || fvd->file_data == NULL || + strcmp(fvd->file_data->file_name, "..") == 0) + return NULL; + ds = (DirectorySet *) fvd->directory_set; + if (ds == NULL || ds->name == NULL) + return NULL; + return Join(ds->name, fvd->file_data->file_name); +} + +/* ---- the window --------------------------------------------------------- */ + +static Widget +Content(Peek *p, const char *name, WidgetClass class) +{ + return XtVaCreateWidget(name, class, p->form, + XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNbottomAttachment, XmATTACH_WIDGET, + XmNbottomWidget, p->buttons, NULL); +} + +static void +Build(Peek *p) +{ + Widget text; + Arg args[6]; + XmString s; + + p->shell = XtVaCreatePopupShell("mcdePeek", topLevelShellWidgetClass, + p->rec->shell, XmNtitle, "Preview", XmNallowShellResize, True, + NULL); + p->form = XtVaCreateManagedWidget("mcdePeekForm", xmFormWidgetClass, + p->shell, XmNmarginWidth, 8, XmNmarginHeight, 8, NULL); + + p->buttons = XtVaCreateManagedWidget("mcdePeekButtons", + xmFormWidgetClass, p->form, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNbottomAttachment, XmATTACH_FORM, NULL); + p->open = XtVaCreateManagedWidget("mcdePeekOpen", + xmPushButtonWidgetClass, p->buttons, + XmNtopAttachment, XmATTACH_FORM, + XmNbottomAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, NULL); + p->full = XtVaCreateManagedWidget("mcdePeekFull", + xmToggleButtonWidgetClass, p->buttons, + XmNtopAttachment, XmATTACH_FORM, + XmNbottomAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, NULL); + s = XmStringCreateLocalized((char *) "Open"); + XtVaSetValues(p->open, XmNlabelString, s, NULL); + XmStringFree(s); + s = XmStringCreateLocalized((char *) "Full Screen"); + XtVaSetValues(p->full, XmNlabelString, s, NULL); + XmStringFree(s); + + p->icon = Content(p, "mcdePeekIcon", xmLabelWidgetClass); + p->info = Content(p, "mcdePeekInfo", xmLabelWidgetClass); + p->image = Content(p, "mcdePeekImage", xmLabelWidgetClass); + + XtSetArg(args[0], XmNeditable, False); + XtSetArg(args[1], XmNeditMode, XmMULTI_LINE_EDIT); + XtSetArg(args[2], XmNwordWrap, True); + XtSetArg(args[3], XmNscrollVertical, True); + XtSetArg(args[4], XmNscrollHorizontal, False); + text = XmCreateScrolledText(p->form, "mcdePeekText", args, 5); + p->text = text; + /* the text is managed inside its scrolled window, the window itself + * joins the form only while a text is shown (XtParent is the window) */ + XtManageChild(text); + XtVaSetValues(XtParent(text), + XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNbottomAttachment, XmATTACH_WIDGET, + XmNbottomWidget, p->buttons, NULL); + + XtAddCallback(p->open, XmNactivateCallback, OpenCB, p); + XtAddCallback(p->full, XmNvalueChangedCallback, FullCB, p); + XtAddEventHandler(p->shell, KeyPressMask, False, KeyHandler, p); +} + +/* ---- what it shows ------------------------------------------------------ */ + +static void +HideAll(Peek *p) +{ + XtUnmanageChild(p->icon); + XtUnmanageChild(p->info); + XtUnmanageChild(p->image); + XtUnmanageChild(XtParent(p->text)); +} + +/* the icon of a file from CDE's data types, as Columns.c finds it */ +static Pixmap +TypeIcon(Widget w, const char *path) +{ + char *type, *icon; + Pixmap pm = XmUNSPECIFIED_PIXMAP; + Pixel fg, bg; + size_t i; + static const char *const sizes[] = { ".l.pm", ".m.pm", ".l.bm" }; + + if (path == NULL || (type = DtDtsFileToDataType((char *) path)) == NULL) + return pm; + icon = DtDtsDataTypeToAttributeValue(type, DtDTS_DA_ICON, NULL); + if (icon != NULL) { + XtVaGetValues(w, XmNforeground, &fg, XmNbackground, &bg, NULL); + for (i = 0; i < sizeof sizes / sizeof sizes[0] && + pm == XmUNSPECIFIED_PIXMAP; i++) { + char buf[256]; + + (void)snprintf(buf, sizeof buf, "%s%s", icon, sizes[i]); + pm = XmGetPixmap(XtScreen(w), buf, fg, bg); + if (pm == XmUNSPECIFIED_PIXMAP) { + (void)snprintf(buf, sizeof buf, + "%s/appconfig/icons/C/%s%s", + CDE_INSTALLATION_TOP, icon, sizes[i]); + pm = XmGetPixmap(XtScreen(w), buf, fg, bg); + } + } + DtDtsFreeAttributeValue(icon); + } + DtDtsFreeDataType(type); + return pm; +} + +static void +ShowInfo(Peek *p, const char *text, Pixmap icon) +{ + XmString s; + + HideAll(p); + if (p->iconpm != XmUNSPECIFIED_PIXMAP) + McdeThumbFreePixmap(XtScreen(p->shell), p->iconpm); + p->iconpm = icon; + if (icon != XmUNSPECIFIED_PIXMAP) { + XtVaSetValues(p->icon, XmNlabelType, XmPIXMAP, + XmNlabelPixmap, icon, NULL); + XtManageChild(p->icon); + XtVaSetValues(p->info, XmNtopAttachment, XmATTACH_WIDGET, + XmNtopWidget, p->icon, NULL); + } else { + XtVaSetValues(p->info, XmNtopAttachment, XmATTACH_FORM, NULL); + } + s = XmStringCreateLocalized((char *) text); + XtVaSetValues(p->info, XmNlabelString, s, NULL); + XmStringFree(s); + XtManageChild(p->info); + p->iw = SMALL_W; + p->ih = SMALL_H; +} + +static void +ShowImage(Peek *p, Pixmap pixmap, int w, int h) +{ + if (p->pixmap != XmUNSPECIFIED_PIXMAP) + McdeThumbFreePixmap(XtScreen(p->shell), p->pixmap); + p->pixmap = pixmap; + HideAll(p); + XtVaSetValues(p->image, XmNlabelType, XmPIXMAP, + XmNlabelPixmap, pixmap, NULL); + XtManageChild(p->image); + p->iw = w; + p->ih = h; +} + +static void +ShowText(Peek *p, const char *text) +{ + /* the text first, at its beginning: a widget that is shown with the + * string already in it does not scroll to the cursor it never had */ + XmTextSetString(p->text, (char *) text); + XmTextSetInsertionPosition(p->text, 0); + XmTextSetTopCharacter(p->text, 0); + HideAll(p); + XtManageChild(XtParent(p->text)); +} + +/* a picture from its first bytes, before anything is decoded */ +static int +IsPicture(const char *path) +{ + unsigned char b[8]; + const char *dot; + FILE *fp; + size_t n; + + if ((dot = strrchr(path, '.')) != NULL && + (strcasecmp(dot, ".png") == 0 || strcasecmp(dot, ".jpg") == 0 || + strcasecmp(dot, ".jpeg") == 0 || strcasecmp(dot, ".gif") == 0 || + strcasecmp(dot, ".bmp") == 0 || strcasecmp(dot, ".xpm") == 0)) + return 1; + if ((fp = fopen(path, "rb")) == NULL) + return 0; + n = fread(b, 1, sizeof b, fp); + fclose(fp); + if (n >= 8 && memcmp(b, "\211PNG\r\n\032\n", 8) == 0) + return 1; + if (n >= 3 && b[0] == 0xff && b[1] == 0xd8 && b[2] == 0xff) + return 1; + if (n >= 6 && (memcmp(b, "GIF87a", 6) == 0 || + memcmp(b, "GIF89a", 6) == 0)) + return 1; + if (n >= 2 && b[0] == 'B' && b[1] == 'M') + return 1; + if (n >= 3 && b[0] == '/' && b[1] == '*' && b[2] == ' ') + return 1; + return 0; +} + +/* a text file: no NUL and few control characters in the first kilobyte */ +static int +IsText(const char *path) +{ + FILE *fp; + unsigned char buf[1024]; + size_t n, i, bad = 0; + + if ((fp = fopen(path, "rb")) == NULL) + return 0; + n = fread(buf, 1, sizeof buf, fp); + fclose(fp); + if (n == 0) + return 0; + for (i = 0; i < n; i++) { + if (buf[i] == '\0') + return 0; + if (buf[i] < 32 && buf[i] != '\t' && buf[i] != '\n' && + buf[i] != '\r' && buf[i] != '\f' && buf[i] != '\033') + bad++; + } + return bad * 10 < n; +} + +static char * +ReadHead(const char *path) +{ + FILE *fp; + char *buf; + size_t n; + + if ((fp = fopen(path, "rb")) == NULL) + return NULL; + if ((buf = XtMalloc(MAX_TEXT + 1)) == NULL) { + fclose(fp); + return NULL; + } + n = fread(buf, 1, MAX_TEXT, fp); + fclose(fp); + buf[n] = '\0'; + return buf; +} + +static void +Update(Peek *p, const char *path) +{ + struct stat st; + const char *name; + char title[256]; + char text[256]; + int w, h; + DIR *d; + struct dirent *de; + int n; + char *head; + + if (p->request != NULL) { + McdeThumbCancel(p->request); + p->request = NULL; + } + if (p->path != NULL) + XtFree(p->path); + p->path = path != NULL ? XtNewString(path) : NULL; + if (path == NULL) { + XtVaSetValues(p->shell, XmNtitle, "Preview", NULL); + ShowInfo(p, "Nothing is chosen.", XmUNSPECIFIED_PIXMAP); + return; + } + name = strrchr(path, '/'); + name = name != NULL && name[1] != '\0' ? name + 1 : path; + (void)snprintf(title, sizeof title, "%s", name); + XtVaSetValues(p->shell, XmNtitle, title, NULL); + + if (stat(path, &st) != 0) { + ShowInfo(p, "This file cannot be read.", + XmUNSPECIFIED_PIXMAP); + return; + } + if (S_ISDIR(st.st_mode)) { + n = 0; + if ((d = opendir(path)) != NULL) { + while ((de = readdir(d)) != NULL) + if (de->d_name[0] != '.') + n++; + closedir(d); + } + (void)snprintf(text, sizeof text, "%d item%s", n, + n == 1 ? "" : "s"); + ShowInfo(p, text, TypeIcon(p->info, path)); + return; + } + if (IsPicture(path)) { + HideAll(p); + Room(p, &w, &h); + p->request = McdeThumbRequest(p->shell, path, w, h, + PictureReady, p); + return; + } + if (IsText(path) && (head = ReadHead(path)) != NULL) { + Room(p, &w, &h); + ShowText(p, head); + p->iw = w; + p->ih = h; + XtFree(head); + return; + } + (void)snprintf(text, sizeof text, "%s\n%lld bytes", name, + (long long) st.st_size); + ShowInfo(p, text, TypeIcon(p->info, path)); +} + +/* the room beside the window it belongs to: a picture is read no larger + * than it has to be, and the window never has to cover the list */ +static void +Room(Peek *p, int *maxw, int *maxh) +{ + Dimension pw = 0, ph = 0; + int px = 0, py = 0; + Display *dpy = XtDisplay(p->shell); + Screen *screen = DefaultScreenOfDisplay(dpy); + int sw = WidthOfScreen(screen), sh = HeightOfScreen(screen); + Window child; + + XtVaGetValues(p->rec->shell, XmNwidth, &pw, XmNheight, &ph, NULL); + if (XtWindow(p->rec->shell) != None) + XTranslateCoordinates(dpy, XtWindow(p->rec->shell), + DefaultRootWindow(dpy), 0, 0, &px, &py, &child); + *maxw = sw - (px + (int) pw + 16) - 40; + if (*maxw < 220) + *maxw = sw - 88; + *maxh = sh - (py + 24) - 80; + if (*maxh < 160) + *maxh = sh - 120; + if (*maxw > MAX_W) + *maxw = MAX_W; + if (*maxh > MAX_H) + *maxh = MAX_H; +} + +static void +Fit(Peek *p) +{ + int w = p->iw > 0 ? p->iw : SMALL_W; + int h = p->ih > 0 ? p->ih : SMALL_H; + Dimension pw = 0, ph = 0; + int px = 0, py = 0, x, y; + Display *dpy = XtDisplay(p->shell); + Screen *screen = DefaultScreenOfDisplay(dpy); + int sw = WidthOfScreen(screen), sh = HeightOfScreen(screen); + Window child; + + w += 32; + h += 88; + /* beside the window it belongs to, as the reference floats beside + * the list; left of it or at the edge when there is no room right. + * dtwm places a new window itself, so the place is given to it as a + * move of the window that is already up (XmNx alone is its hint). */ + XtVaGetValues(p->rec->shell, XmNwidth, &pw, XmNheight, &ph, NULL); + if (XtWindow(p->rec->shell) != None) + XTranslateCoordinates(dpy, XtWindow(p->rec->shell), + DefaultRootWindow(dpy), 0, 0, &px, &py, &child); + x = px + (int) pw + 12; + if (x + w > sw) + x = px - w - 12; + if (x < 0) + x = sw - w - 4; + if (x < 0) + x = 0; + y = py + 24; + if (y + h > sh) + y = sh - h; + if (y < 0) + y = 0; + { + char geometry[32]; + + (void)snprintf(geometry, sizeof geometry, "+%d+%d", x, y); + XtVaSetValues(p->shell, XmNgeometry, geometry, XmNwidth, w, + XmNheight, h, XmNx, x, XmNy, y, NULL); + } + if (XtWindow(p->shell) != None && p->shown) { + XMoveWindow(dpy, XtWindow(p->shell), x, y); + /* a viewer lies over what it shows; dtwm does not raise on + * focus (ext/src/mcde/AGENTS.md, "Windows") */ + XRaiseWindow(dpy, XtWindow(p->shell)); + } +} + +/* ---- buttons, keys, the screen ----------------------------------------- */ + +static void +OpenCB(Widget w, XtPointer client, XtPointer call) +{ + Peek *p = client; + char *type, *actions, *first, *end; + DtActionArg arg; + + (void)w; + (void)call; + if (p->path == NULL) + return; + if ((type = DtDtsFileToDataType(p->path)) == NULL) + return; + actions = DtDtsDataTypeToAttributeValue(type, DtDTS_DA_ACTION_LIST, + NULL); + if (actions == NULL) { + DtDtsFreeDataType(type); + return; + } + first = XtNewString(actions); + if ((end = strchr(first, ',')) != NULL) + *end = '\0'; + memset(&arg, 0, sizeof arg); + arg.argClass = DtACTION_FILE; + arg.u.file.name = p->path; + (void)DtActionInvoke(p->shell, first, &arg, 1, NULL, NULL, NULL, True, + NULL, NULL); + XtFree(first); + DtDtsFreeAttributeValue(actions); + DtDtsFreeDataType(type); +} + +/* _NET_WM_STATE_FULLSCREEN, the way dtwm reads it (WmEwmh.c) */ +static void +FullScreen(Peek *p) +{ + Display *dpy = XtDisplay(p->shell); + Window root = DefaultRootWindow(dpy); + Atom state = XInternAtom(dpy, "_NET_WM_STATE", False); + Atom full = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False); + XEvent e; + + memset(&e, 0, sizeof e); + e.xclient.type = ClientMessage; + e.xclient.window = XtWindow(p->shell); + e.xclient.message_type = state; + e.xclient.format = 32; + e.xclient.data.l[0] = p->fullscreen ? 0 : 1; /* remove, add */ + e.xclient.data.l[1] = (long) full; + e.xclient.data.l[2] = 0; + e.xclient.data.l[3] = 1; /* a normal application */ + XSendEvent(dpy, root, False, SubstructureRedirectMask | + SubstructureNotifyMask, &e); + XFlush(dpy); + p->fullscreen = !p->fullscreen; + XtVaSetValues(p->full, XmNset, p->fullscreen ? XmSET : XmUNSET, NULL); +} + +static void +FullCB(Widget w, XtPointer client, XtPointer call) +{ + Peek *p = client; + XmToggleButtonCallbackStruct *c = (XmToggleButtonCallbackStruct *) call; + + (void)w; + if (c->set != p->fullscreen) + FullScreen(p); +} + +static void +KeyHandler(Widget w, XtPointer client, XEvent *event, Boolean *cont) +{ + Peek *p = client; + KeySym key = XLookupKeysym(&event->xkey, 0); + + (void)w; + if (key == XK_Escape || key == XK_space) { + XtPopdown(p->shell); + p->shown = False; + *cont = False; + } +} + +static void +PictureReady(void *client, Pixmap pixmap, int w, int h) +{ + Peek *p = client; + + p->request = NULL; + if (pixmap == XmUNSPECIFIED_PIXMAP) { + ShowInfo(p, "This picture cannot be read.", + TypeIcon(p->info, p->path != NULL ? p->path : "/")); + } else { + ShowImage(p, pixmap, w, h); + } + Fit(p); +} + +/* ---- what the file manager calls ---------------------------------------- */ + +/* the window whose keys these are: the file window, not the preview */ +static void +Refocus(FileMgrRec *rec) +{ + if (rec == NULL || rec->shell == NULL || XtWindow(rec->shell) == None) + return; + if (rec->file_window != NULL) + XtSetKeyboardFocus(rec->shell, (Widget) rec->file_window); + XSetInputFocus(XtDisplay(rec->shell), XtWindow(rec->shell), + RevertToPointerRoot, CurrentTime); +} + +static void +RefocusLater(XtPointer client, XtIntervalId *id) +{ + (void)id; + Refocus((FileMgrRec *) client); +} + +void +_McdePeekToggle(FileMgrRec *rec) +{ + Peek *p = PeekOf(rec, True); + FileMgrData *data = DataOf(rec); + char *path; + + if (p == NULL) + return; + if (p->shell == NULL) + Build(p); + if (p->shown) { + XtPopdown(p->shell); + p->shown = False; + return; + } + if (rec->mcde_columns_on && + (path = _McdeColumnsChosen(rec)) != NULL) { + /* the columns keep their own selection (Columns.c) */ + Update(p, path); + XtFree(path); + } else if (data == NULL || data->selected_file_count == 0) { + XBell(XtDisplay(rec->shell), 0); + return; + } else if (data->selected_file_count > 1) { + char text[64]; + + (void)snprintf(text, sizeof text, "%d items selected", + data->selected_file_count); + ShowInfo(p, text, XmUNSPECIFIED_PIXMAP); + } else if ((path = ChosenPath(data)) != NULL) { + Update(p, path); + XtFree(path); + } else { + XBell(XtDisplay(rec->shell), 0); + return; + } + Fit(p); + XtPopup(p->shell, XtGrabNone); + p->shown = True; + /* a viewer takes no focus: the keys stay with the list behind it. + * The window manager hands the focus to the new window after it is + * mapped, so the window's own focus is taken back once more, a + * moment later (one shot, no polling). */ + Refocus(rec); + XtAppAddTimeOut(XtWidgetToApplicationContext(rec->shell), 150, + RefocusLater, rec); +} + +void +_McdePeekFollow(FileMgrRec *rec) +{ + Peek *p = PeekOf(rec, False); + FileMgrData *data = DataOf(rec); + char *path; + + if (p == NULL || p->shell == NULL || !p->shown || data == NULL) + return; + if (data->selected_file_count == 0) { + Update(p, NULL); + Fit(p); + return; + } + if (data->selected_file_count > 1) { + char text[64]; + + (void)snprintf(text, sizeof text, "%d items selected", + data->selected_file_count); + ShowInfo(p, text, XmUNSPECIFIED_PIXMAP); + Fit(p); + return; + } + if ((path = ChosenPath(data)) != NULL) { + Update(p, path); + XtFree(path); + Fit(p); + } +} + +void +_McdePeekPath(FileMgrRec *rec, const char *path) +{ + Peek *p = PeekOf(rec, False); + + if (p == NULL || p->shell == NULL || !p->shown) + return; + Update(p, path); + Fit(p); +} + +Boolean +_McdePeekClose(FileMgrRec *rec) +{ + Peek *p = PeekOf(rec, False); + + if (p == NULL || p->shell == NULL || !p->shown) + return False; + XtPopdown(p->shell); + p->shown = False; + return True; +} + +void +_McdePeekFullScreen(FileMgrRec *rec) +{ + Peek *p = PeekOf(rec, False); + + if (p == NULL || p->shell == NULL || !p->shown) + return; + FullScreen(p); +} diff --git a/ext/src/mcde/programs/dtfile/Peek.h b/ext/src/mcde/programs/dtfile/Peek.h new file mode 100644 index 0000000000..0c68c379c8 --- /dev/null +++ b/ext/src/mcde/programs/dtfile/Peek.h @@ -0,0 +1,24 @@ +/* + * Peek.h -- MCDE: the preview of the chosen file (Peek.c) + * + * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. + */ + +#ifndef _Peek_h +#define _Peek_h + +#include "FileMgr.h" + +/* Space: show the preview of the chosen file, or hide it */ +extern void _McdePeekToggle(FileMgrRec *rec); +/* the selection changed: the shown preview follows it */ +extern void _McdePeekFollow(FileMgrRec *rec); +/* the column view chose PATH (Columns.c): show it in the preview, if the + * preview is on */ +extern void _McdePeekPath(FileMgrRec *rec, const char *path); +/* Option+Space: the preview over the whole screen, and back */ +extern void _McdePeekFullScreen(FileMgrRec *rec); +/* Escape: True when a preview was up and is now hidden */ +extern Boolean _McdePeekClose(FileMgrRec *rec); + +#endif /* _Peek_h */ diff --git a/tools/mcde-shot.sh b/tools/mcde-shot.sh index 1077f61194..62f547d35c 100755 --- a/tools/mcde-shot.sh +++ b/tools/mcde-shot.sh @@ -47,6 +47,14 @@ LD_LIBRARY_PATH=$R/dt/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} XFILESEARCHPATH=$R/dt/app-defaults/%L/%N:$R/dt/app-defaults/C/%N export DISPLAY HOME PATH LD_LIBRARY_PATH XFILESEARCHPATH +# MCDE_SHOT_SETUP: a command run in the fresh home before the program starts +# (make a folder of pictures to look at); MCDE_SHOT_ACTIONS: commands run +# before the picture, with DISPLAY and HOME of the session +# ("xdotool mousemove 205 212 click 1; xdotool key space") +if [ -n "${MCDE_SHOT_SETUP:-}" ]; then + (cd "$H" && eval "$MCDE_SHOT_SETUP") || exit 1 +fi + # an empty environment: the session this runs in may be a CDE of its own, # whose DT*, XMICON* and SESSION_* variables would point the program at that # installation (2026-11-09: dtfile read /usr/mdt/appconfig/types) @@ -64,6 +72,12 @@ $E "$prog" "$@" >> "$OUT.log" 2>&1 & ppid=$! sleep "$WAIT" +if [ -n "${MCDE_SHOT_ACTIONS:-}" ]; then + sleep 1 + (cd "$H" && eval "$MCDE_SHOT_ACTIONS") + sleep 2 +fi + import -window root "$OUT" rc=$?