From eee45018740e12713887d898030ef1cea89766e4 Mon Sep 17 00:00:00 2001 From: lotherk Date: Sat, 14 Nov 2026 15:05:02 +0000 Subject: [PATCH] MCDE dtimage: the picture viewer, Previous/Next, Zoom, Slideshow (t0160) --- AGENTS.md | 9 + configure.ac | 1 + programs/Makefile.am | 2 +- programs/dtimage/Makefile.am | 14 ++ programs/dtimage/dtimage.c | 417 +++++++++++++++++++++++++++++++++++ 5 files changed, 442 insertions(+), 1 deletion(-) create mode 100644 programs/dtimage/Makefile.am create mode 100644 programs/dtimage/dtimage.c diff --git a/AGENTS.md b/AGENTS.md index e18c585a..7e0a39f0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -155,6 +155,15 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt toggle and the text field exist unmanaged (`G_rename_toggle`, `G_rename_text`). The multi-collision and the folder-merge dialog are unchanged (follow-up). +- **dtimage** (`programs/dtimage`): the picture viewer of the reference -- + toolbar Previous/Next, Zoom -/+, Slideshow, the picture in a scrolled + canvas, the folder's pictures on the right, the name, size and zoom in the + status line. Pictures come from McdeThumb (PNG/JPEG/GIF/BMP/XPM); the + module only shrinks, so dtimage scales itself (nearest neighbour) to make + Zoom + enlarge too (measured: 320x240 at 100 %, 640x480 at 200 %). The + slideshow's timer runs only while it is on. **PDF is not read**: the tree + has no PDF renderer and the choice between mupdf (C) and poppler (C++) is + Konrad's (docs/MCDE-PROGRAMS.md, section 6); a PDF says so. - **dtnetutility** (`programs/dtnetutility`): tabs Info (name and the interfaces with their addresses, McdeSys), Lookup (getaddrinfo(3) in the program -- a name to its IPv4 and IPv6 addresses, no program run, no diff --git a/configure.ac b/configure.ac index 61126a93..1a256af8 100644 --- a/configure.ac +++ b/configure.ac @@ -900,6 +900,7 @@ programs/dtbackup/Makefile programs/dtphoto/Makefile programs/dtconsole/Makefile programs/dtnetutility/Makefile +programs/dtimage/Makefile programs/dtpaint/Makefile programs/dttoolbox/Makefile programs/mcdesoundd/Makefile diff --git a/programs/Makefile.am b/programs/Makefile.am index 7bce3aa8..613a605b 100644 --- a/programs/Makefile.am +++ b/programs/Makefile.am @@ -6,7 +6,7 @@ SUBDIRS = backdrops icons dsdm dtmail dtpad dtfile dterror dtwm dtlogin \ palettes dtappintegrate dtprintegrate dtconfig \ dtcreate dtprintinfo fontaliases dtdspmsg dtimsstart \ dtpdm dtsr dtpdmd dtinfo types tttypes util dtopen localized \ - dthelp dtdocbook dtlaunch dtbar dtsystemsettings dtdiskutility dtbackup dtappscan dtkeys dtthemesync dtphoto dtpaint dttoolbox mcdesoundd dtconsole dtnetutility + dthelp dtdocbook dtlaunch dtbar dtsystemsettings dtdiskutility dtbackup dtappscan dtkeys dtthemesync dtphoto dtpaint dttoolbox mcdesoundd dtconsole dtnetutility dtimage # The application builder and ttsnoop, its demonstration, need dtcodegen, a # program the build runs: not in a cross build (--disable-appbuilder; the native diff --git a/programs/dtimage/Makefile.am b/programs/dtimage/Makefile.am new file mode 100644 index 00000000..bff9d4aa --- /dev/null +++ b/programs/dtimage/Makefile.am @@ -0,0 +1,14 @@ +MAINTAINERCLEANFILES = Makefile.in + +bin_PROGRAMS = dtimage + +dtimage_CPPFLAGS = -I$(top_srcdir)/include \ + -DCDE_INSTALLATION_TOP=\"$(CDE_INSTALLATION_TOP)\" + +dtimage_LDADD = $(DTCLIENTLIBS) $(XTOOLLIB) + +dtimage_SOURCES = dtimage.c + +if SOLARIS +dtimage_LDADD += -lintl +endif diff --git a/programs/dtimage/dtimage.c b/programs/dtimage/dtimage.c new file mode 100644 index 00000000..86ec57d0 --- /dev/null +++ b/programs/dtimage/dtimage.c @@ -0,0 +1,417 @@ +/* + * dtimage.c -- MCDE: the picture viewer (t0160) + * + * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. + * + * The reference's viewer: a toolbar with Previous / Next, Zoom - / +, Fit + * and Slideshow, the picture in a scrolled canvas, and the other pictures of + * the folder on the right. Pictures are read with McdeThumb (PNG, JPEG, + * GIF, BMP, XPM; libpng, libjpeg and C99), scaled for the chosen zoom and + * drawn on Expose -- no animation, and the only timer is the slideshow's, + * and only while it runs. + * + * PDF is not read here: the tree has no PDF renderer, and the choice + * between mupdf (C, pkgsrc print/mupdf) and poppler (C++) is a decision to + * put to Konrad (docs/MCDE-PROGRAMS.md, section 6). A PDF named on the + * command line says so instead of showing nothing. + * + * C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md). + */ + +#define _POSIX_C_SOURCE 200809L + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static XtAppContext app; +static Widget shell, canvas, list, status, slideToggle; +static char **files; +static int nfiles, at = -1; +static Pixmap pm = None; +static int pmw, pmh; +static McdeThumbImage full; /* the picture as read */ +static int haveFull; +static int zoom = 100; +static int naturalW, naturalH; +static XtIntervalId slideId; + +static void +SetText(Widget w, const char *text) +{ + XmString s = XmStringCreateLocalized((char *) text); + + XtVaSetValues(w, XmNlabelString, s, NULL); + XmStringFree(s); +} + +static Widget +Text(Widget parent, const char *name, const char *text) +{ + XmString s = XmStringCreateLocalized((char *) text); + Widget w = XtVaCreateManagedWidget((char *) name, + xmLabelWidgetClass, parent, XmNlabelString, s, + XmNalignment, XmALIGNMENT_BEGINNING, NULL); + + XmStringFree(s); + return w; +} + +static Widget +Button(Widget parent, const char *label, XtCallbackProc cb, XtPointer d) +{ + XmString s = XmStringCreateLocalized((char *) label); + Widget b = XtVaCreateManagedWidget("button", xmPushButtonWidgetClass, + parent, XmNlabelString, s, NULL); + + XmStringFree(s); + if (cb != NULL) + XtAddCallback(b, XmNactivateCallback, cb, d); + return b; +} + +static int +IsPicture(const char *name) +{ + const char *dot = strrchr(name, '.'); + + if (dot == NULL) + return 0; + return strcasecmp(dot, ".png") == 0 || strcasecmp(dot, ".jpg") == 0 || + strcasecmp(dot, ".jpeg") == 0 || strcasecmp(dot, ".gif") == 0 || + strcasecmp(dot, ".bmp") == 0 || strcasecmp(dot, ".xpm") == 0; +} + +/* the pictures of DIR into files[] */ +static void +Collect(const char *dir) +{ + DIR *d = opendir(dir); + struct dirent *de; + char path[1400]; + int cap = 0; + + if (d == NULL) + return; + while ((de = readdir(d)) != NULL) { + struct stat st; + + if (de->d_name[0] == '.' || !IsPicture(de->d_name)) + continue; + (void)snprintf(path, sizeof path, "%s/%s", dir, de->d_name); + if (stat(path, &st) != 0 || !S_ISREG(st.st_mode)) + continue; + if (nfiles == cap) { + cap = cap ? cap * 2 : 32; + files = realloc(files, (size_t) cap * sizeof *files); + } + files[nfiles++] = strdup(path); + } + closedir(d); +} + +/* SRC (SW x SH RGB) to TW x TH, nearest neighbour: McdeThumbScale only + * makes a picture smaller, and Zoom + has to make it larger */ +static int +ScaleTo(const unsigned char *src, int sw, int sh, int tw, int th, + unsigned char **dst) +{ + unsigned char *out; + int x, y, at; + + if (tw < 1 || th < 1 || sw < 1 || sh < 1) + return -1; + out = malloc((size_t) tw * th * 3); + if (out == NULL) + return -1; + for (y = 0; y < th; y++) + for (x = 0; x < tw; x++) { + int sx = x * sw / tw, sy = y * sh / th; + + at = (sy * sw + sx) * 3; + out[(y * tw + x) * 3] = src[at]; + out[(y * tw + x) * 3 + 1] = src[at + 1]; + out[(y * tw + x) * 3 + 2] = src[at + 2]; + } + *dst = out; + return 0; +} + +/* the picture again, at the chosen zoom */ +static void +Load(void) +{ + Screen *scr = XtScreen(canvas); + Pixel fg, bg; + + if (pm != None) { + McdeThumbFreePixmap(scr, pm); + pm = None; + } + if (at < 0 || at >= nfiles) { + SetText(status, nfiles == 0 ? "No picture in this folder." : + ""); + return; + } + if (!haveFull || full.width == 0) { + if (haveFull) + McdeThumbFree(&full); + if (McdeThumbRead(files[at], 8192, 8192, &full) != 0) { + haveFull = 0; + SetText(status, "This file is not a picture I can read."); + return; + } + haveFull = 1; + naturalW = full.width; + naturalH = full.height; + } + XtVaGetValues(canvas, XmNforeground, &fg, XmNbackground, &bg, NULL); + { + McdeThumbImage scaled; + unsigned char *rgb = NULL; + int tw = naturalW * zoom / 100, th = naturalH * zoom / 100; + + if (tw < 8) + tw = 8; + if (th < 8) + th = 8; + if (ScaleTo(full.rgb, full.width, full.height, tw, th, &rgb) != 0) { + SetText(status, "This picture is too large."); + return; + } + scaled.width = tw; + scaled.height = th; + scaled.rgb = rgb; + if (McdeThumbPixmap(scr, &scaled, &pm) != 0) { + free(rgb); + pm = None; + SetText(status, "This picture cannot be drawn here."); + return; + } + free(rgb); + pmw = tw; + pmh = th; + } + XtVaSetValues(canvas, XmNwidth, (Dimension) pmw, + XmNheight, (Dimension) pmh, NULL); + { + char line[1400], name[512]; + const char *base = strrchr(files[at], '/'); + + (void)snprintf(name, sizeof name, "%s", base != NULL ? + base + 1 : files[at]); + (void)snprintf(line, sizeof line, "%s -- %d x %d, %d%% (%d of " + "%d)", name, pmw, pmh, zoom, at + 1, nfiles); + SetText(status, line); + } + XmListSelectPos(list, at + 1, False); + if (XtIsRealized(canvas)) + XClearArea(XtDisplay(canvas), XtWindow(canvas), 0, 0, 0, 0, + True); +} + +static void +Draw(Widget w, XtPointer data, XEvent *ev, Boolean *cont) +{ + (void) w; + (void) data; + (void) ev; + (void) cont; + if (pm == None) + return; + XCopyArea(XtDisplay(canvas), pm, XtWindow(canvas), + DefaultGC(XtDisplay(canvas), DefaultScreen(XtDisplay(canvas))), + 0, 0, (unsigned) pmw, (unsigned) pmh, 0, 0); +} + +static void +Show(int which) +{ + if (nfiles == 0) + return; + if (which < 0) + which = nfiles - 1; + if (which >= nfiles) + which = 0; + if (which != at) { + zoom = 100; + naturalW = naturalH = 0; + if (haveFull) { + McdeThumbFree(&full); + haveFull = 0; + } + } + at = which; + Load(); +} + +static void +Chosen(Widget w, XtPointer data, XtPointer call) +{ + (void) w; + (void) data; + if (((XmListCallbackStruct *) call)->item_position > 0) + Show(((XmListCallbackStruct *) call)->item_position - 1); +} + +static void +Prev(Widget w, XtPointer data, XtPointer call) +{ + (void) w; (void) data; (void) call; + Show(at - 1); +} + +static void +Next(Widget w, XtPointer data, XtPointer call) +{ + (void) w; (void) data; (void) call; + Show(at + 1); +} + +static void +ZoomIn(Widget w, XtPointer data, XtPointer call) +{ + (void) w; (void) data; (void) call; + if (zoom < 800) { + zoom *= 2; + Load(); + } +} + +static void +ZoomOut(Widget w, XtPointer data, XtPointer call) +{ + (void) w; (void) data; (void) call; + if (zoom > 10) { + zoom /= 2; + Load(); + } +} + +static void +Slideshow(XtPointer data, XtIntervalId *id) +{ + (void) data; (void) id; + Show(at + 1); + slideId = XtAppAddTimeOut(app, 3000, Slideshow, NULL); +} + +static void +SlideToggle(Widget w, XtPointer data, XtPointer call) +{ + (void) w; (void) data; + if (((XmToggleButtonCallbackStruct *) call)->set) + slideId = XtAppAddTimeOut(app, 3000, Slideshow, NULL); + else if (slideId != 0) { + XtRemoveTimeOut(slideId); + slideId = 0; + } +} + +int +main(int argc, char **argv) +{ + Widget form, bar, paned, sw, right; + static String fallback[] = { "*renderTable: mcde", NULL }; + int i; + + XtSetLanguageProc(NULL, NULL, NULL); + shell = XtVaAppInitialize(&app, "Dtimage", NULL, 0, &argc, argv, + (String *) fallback, XmNtitle, "Preview", NULL); + + /* the pictures: the named one and its folder, or every argument */ + for (i = 1; i < argc; i++) { + struct stat st; + + if (stat(argv[i], &st) != 0) + continue; + if (S_ISDIR(st.st_mode)) + Collect(argv[i]); + else if (IsPicture(argv[i])) { + files = realloc(files, (size_t) (nfiles + 1) * + sizeof *files); + files[nfiles++] = strdup(argv[i]); + } else + fprintf(stderr, "dtimage: not a picture: %s\n", + argv[i]); + } + if (nfiles == 0) + fprintf(stderr, "dtimage: no picture named\n"); + + form = XtVaCreateManagedWidget("form", xmFormWidgetClass, shell, + XmNwidth, 760, XmNheight, 560, NULL); + bar = XtVaCreateManagedWidget("toolbar", xmRowColumnWidgetClass, form, + XmNorientation, XmHORIZONTAL, XmNspacing, 8, XmNmarginHeight, 4, + XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, NULL); + (void) Button(bar, "Previous", Prev, NULL); + (void) Button(bar, "Next", Next, NULL); + (void) Button(bar, "Zoom -", ZoomOut, NULL); + (void) Button(bar, "Zoom +", ZoomIn, NULL); + slideToggle = XtVaCreateManagedWidget("Slideshow", + xmToggleButtonWidgetClass, bar, NULL); + XtAddCallback(slideToggle, XmNvalueChangedCallback, SlideToggle, NULL); + + paned = XtVaCreateManagedWidget("paned", xmRowColumnWidgetClass, form, + XmNorientation, XmHORIZONTAL, XmNspacing, 8, XmNmarginWidth, 6, + XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, bar, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, + status = Text(form, "status", ""), XmNbottomOffset, 6, NULL); + sw = XtVaCreateManagedWidget("pictureBox", xmScrolledWindowWidgetClass, + paned, XmNscrollingPolicy, XmAUTOMATIC, XmNwidth, 520, + XmNheight, 460, NULL); + canvas = XtVaCreateManagedWidget("canvas", xmDrawingAreaWidgetClass, + sw, XmNwidth, 480, XmNheight, 400, NULL); + XtAddEventHandler(canvas, ExposureMask, False, Draw, NULL); + + right = XtVaCreateManagedWidget("side", xmRowColumnWidgetClass, paned, + XmNorientation, XmVERTICAL, XmNspacing, 4, XmNmarginWidth, 0, + NULL); + (void) Text(right, "sideLabel", "Pictures:"); + list = XmCreateScrolledList(right, "pictures", NULL, 0); + XtVaSetValues(list, XmNvisibleItemCount, 18, NULL); + XtVaSetValues(XtParent(list), XmNwidth, 190, XmNheight, 430, NULL); + XtManageChild(list); + XtAddCallback(list, XmNbrowseSelectionCallback, Chosen, NULL); + { + int k; + + for (k = 0; k < nfiles; k++) { + const char *base = strrchr(files[k], '/'); + XmString s = XmStringCreateLocalized((char *) + (base != NULL ? base + 1 : files[k])); + + XmListAddItemUnselected(list, s, 0); + XmStringFree(s); + } + } + XtVaSetValues(status, XmNbottomAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, + XmATTACH_FORM, NULL); + XtManageChild(form); + XtRealizeWidget(shell); + if (nfiles > 0) + Show(0); + XtAppMainLoop(app); + return 0; +}