/* * 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; }