diff --git a/programs/dtphoto/dtphoto.c b/programs/dtphoto/dtphoto.c index ed336220..8930177a 100644 --- a/programs/dtphoto/dtphoto.c +++ b/programs/dtphoto/dtphoto.c @@ -3,13 +3,12 @@ * * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. * - * Stage P0 (docs/MCDE-PROGRAMS.md, C1): the window, the menu bar, the - * toolbox and the status line, with the 2.5 / 3.0 switch. The menus are - * data (share/dtphoto/dtphoto.menu), not C: the same file carries both - * versions, a line ending in "@2.5" or "@3.0" is read only in that one. - * - * No image is edited yet; the canvas shows the empty document of a new - * picture so that the geometry can be compared with the reference. + * Stages P0 and P1 (docs/MCDE-PROGRAMS.md, C1): the window, the menu bar, + * the toolbox and the status line with the 2.5 / 3.0 switch; a document + * that opens and saves BMP, a one-step undo and the four zoom steps. + * The menus are data (share/dtphoto/dtphoto.menu), one file for both + * versions: a line ending in "@2.5" or "@3.0" is read only there, and + * "sub NAME" ... "endsub" makes a submenu. * * C99 and POSIX, Motif and Xlib, as every MCDE program (AGENTS.md). */ @@ -26,16 +25,18 @@ #include #include #include +#include #include #include #include +#include #include #include #include #include #define MAXMENUS 16 -#define MAXITEMS 64 +#define MAXITEMS 80 #define MAXDEPTH 4 typedef struct Menu Menu; @@ -45,6 +46,7 @@ typedef struct { char key[16]; int separator; Menu *sub; + int cmd; } Item; struct Menu { @@ -53,15 +55,24 @@ struct Menu { int count; }; +enum { + CMD_NONE, CMD_NEW, CMD_OPEN, CMD_SAVE, CMD_SAVEAS, CMD_QUIT, + CMD_UNDO, CMD_ZOOMIN, CMD_ZOOMOUT, CMD_FIT, CMD_ACTUAL +}; + static Menu menus[MAXMENUS]; static int nmenus; static char version[8] = "3.0"; -static Widget canvas, statusZoom, statusDoc, statusScratch; -static Widget toolShell; +static Widget top, canvas, statusZoom, statusDoc, statusScratch, toolShell; +static Display *dpy; +static Pixmap image, snap; +static GC gc; +static int docW = 320, docH = 240; +static int zoom = 100; static int currentTool = 4; /* move, as the original starts */ +static int fileSave; -/* the toolbox, top row to bottom, two columns (C1) */ static const char *const toolNames[] = { "marquee", "ellipse", "lasso", "wand", "move", "hand", "zoom", "crop", @@ -70,25 +81,192 @@ static const char *const toolNames[] = { "sharpen", "smudge", "dodge", "burn", "sponge", "eyedropper", NULL }; -static const char *const toolTips[] = { - "Rectangular Marquee", "Elliptical Marquee", "Lasso", "Magic Wand", - "Move", "Hand", "Zoom", "Crop", - "Type", "Line", "Pencil", "Paintbrush", - "Eraser", "Paint Bucket", "Gradient", "Blur", - "Sharpen", "Smudge", "Dodge", "Burn", - "Sponge", "Eyedropper", NULL -}; -/* ------------------------------------------------------------------ menu */ +/* ------------------------------------------------------------------ files */ + +static int +PixelRGB(unsigned long pixel, unsigned char *r, unsigned char *g, unsigned char *b) +{ + static unsigned long cp[64]; + static unsigned char cr[64][3]; + static int n; + int i; + XColor c; + + for (i = 0; i < n; i++) + if (cp[i] == pixel) { + *r = cr[i][0]; *g = cr[i][1]; *b = cr[i][2]; + return 0; + } + c.pixel = pixel; + if (!XQueryColor(dpy, DefaultColormap(dpy, 0), &c)) + return -1; + *r = (unsigned char) (c.red >> 8); + *g = (unsigned char) (c.green >> 8); + *b = (unsigned char) (c.blue >> 8); + if (n < 64) { + cp[n] = pixel; + cr[n][0] = *r; cr[n][1] = *g; cr[n][2] = *b; + n++; + } + return 0; +} + +static void +PutLE(unsigned char *p, unsigned long v, int n) +{ + int i; + + for (i = 0; i < n; i++) + p[i] = (unsigned char) ((v >> (8 * i)) & 0xff); +} + +static int +SaveBMP(const char *path) +{ + FILE *f; + XImage *im; + int row = ((docW * 3 + 3) / 4) * 4; + unsigned long size = (unsigned long) row * docH; + unsigned char *data; + unsigned char hdr[54]; + int x, y; + + if ((im = XGetImage(dpy, image, 0, 0, docW, docH, AllPlanes, ZPixmap)) == NULL) + return -1; + if ((data = calloc(1, (size_t) size)) == NULL) { + XDestroyImage(im); + return -1; + } + for (y = 0; y < docH; y++) + for (x = 0; x < docW; x++) { + unsigned char r, g, b; + unsigned char *p = data + (size_t) (docH - 1 - y) * row + x * 3; + + if (PixelRGB(XGetPixel(im, x, y), &r, &g, &b) < 0) + continue; + p[0] = b; p[1] = g; p[2] = r; + } + XDestroyImage(im); + if ((f = fopen(path, "wb")) == NULL) { + free(data); + return -1; + } + memset(hdr, 0, sizeof hdr); + hdr[0] = 'B'; hdr[1] = 'M'; + PutLE(hdr + 2, 54 + size, 4); + PutLE(hdr + 10, 54, 4); + PutLE(hdr + 14, 40, 4); + PutLE(hdr + 18, (unsigned long) docW, 4); + PutLE(hdr + 22, (unsigned long) docH, 4); + PutLE(hdr + 26, 1, 2); + PutLE(hdr + 28, 24, 2); + PutLE(hdr + 34, size, 4); + PutLE(hdr + 38, 2835, 4); + PutLE(hdr + 42, 2835, 4); + fwrite(hdr, 1, sizeof hdr, f); + fwrite(data, 1, (size_t) size, f); + fclose(f); + free(data); + return 0; +} + +static void NewPixmaps(void); + +static int +OpenBMP(const char *path) +{ + FILE *f; + unsigned char hdr[54]; + unsigned long off, w, h, row; + unsigned char *data; + XImage *im; + int x, y; + + if ((f = fopen(path, "rb")) == NULL) + return -1; + if (fread(hdr, 1, 54, f) != 54 || hdr[0] != 'B' || hdr[1] != 'M') { + fclose(f); + return -1; + } + off = hdr[10] | (hdr[11] << 8) | (hdr[12] << 16) | (hdr[13] << 24); + w = hdr[18] | (hdr[19] << 8) | (hdr[20] << 16) | (hdr[21] << 24); + h = hdr[22] | (hdr[23] << 8) | (hdr[24] << 16) | (hdr[25] << 24); + if (hdr[28] != 24 || w == 0 || h == 0 || w > 4096 || h > 4096) { + fclose(f); + return -1; + } + row = ((w * 3 + 3) / 4) * 4; + if ((data = malloc(row * h)) == NULL) { + fclose(f); + return -1; + } + fseek(f, (long) off, SEEK_SET); + if (fread(data, 1, row * h, f) != row * h) { + free(data); + fclose(f); + return -1; + } + fclose(f); + + docW = (int) w; + docH = (int) h; + NewPixmaps(); + im = XCreateImage(dpy, DefaultVisual(dpy, 0), DefaultDepth(dpy, 0), + ZPixmap, 0, NULL, (unsigned int) w, (unsigned int) h, 32, 0); + if (im == NULL) { + free(data); + return -1; + } + im->data = calloc(1, (size_t) im->bytes_per_line * h); + if (im->data == NULL) { + free(data); + XDestroyImage(im); + return -1; + } + for (y = 0; y < (int) h; y++) + for (x = 0; x < (int) w; x++) { + unsigned char *p = data + (size_t) (h - 1 - y) * row + x * 3; + XColor c; + + c.red = (unsigned short) (p[2] << 8); + c.green = (unsigned short) (p[1] << 8); + c.blue = (unsigned short) (p[0] << 8); + c.flags = DoRed | DoGreen | DoBlue; + if (!XAllocColor(dpy, DefaultColormap(dpy, 0), &c)) + continue; + XPutPixel(im, x, y, c.pixel); + } + XPutImage(dpy, image, gc, im, 0, 0, 0, 0, (unsigned int) w, + (unsigned int) h); + XDestroyImage(im); + free(data); + XCopyArea(dpy, image, snap, gc, 0, 0, docW, docH, 0, 0); + return 0; +} + +/* ------------------------------------------------------------------ menus */ + +static void +Trim(char *p) +{ + size_t n = strlen(p); + + while (n > 0 && (p[n - 1] == '\n' || p[n - 1] == '\r' || + p[n - 1] == ' ' || p[n - 1] == '\t')) + p[--n] = '\0'; +} static void ReadMenus(const char *path) { FILE *f; char line[256]; + Menu *stack[MAXDEPTH]; + int depth = 0; if ((f = fopen(path, "r")) == NULL) - return; /* no file: no menu, but the window */ + return; while (fgets(line, sizeof line, f) != NULL) { char *p = line, *end; int want = 1; @@ -97,28 +275,44 @@ ReadMenus(const char *path) p++; if (*p == '\0' || *p == '\n' || *p == '#') continue; + Trim(p); end = p + strlen(p); - while (end > p && (end[-1] == '\n' || end[-1] == '\r' || - end[-1] == ' ' || end[-1] == '\t')) - *--end = '\0'; - /* " @2.5" / " @3.0": only in that version */ if (end - p >= 4 && end[-4] == '@' && - (strcmp(end - 3, "3.0") == 0 || - strcmp(end - 3, "2.5") == 0)) { + (strcmp(end - 3, "3.0") == 0 || strcmp(end - 3, "2.5") == 0)) { want = strcmp(end - 3, version) == 0; end[-4] = '\0'; - while (end > p && end[-1] == ' ') - *--end = '\0'; + Trim(p); } if (!want) continue; if (strncmp(p, "menu ", 5) == 0 && nmenus < MAXMENUS) { - snprintf(menus[nmenus].name, - sizeof menus[nmenus].name, "%s", p + 5); + snprintf(menus[nmenus].name, sizeof menus[nmenus].name, + "%s", p + 5); menus[nmenus].count = 0; + stack[0] = &menus[nmenus]; + depth = 1; nmenus++; - } else if (strncmp(p, "item ", 5) == 0 && nmenus > 0) { - Menu *m = &menus[nmenus - 1]; + } else if (strncmp(p, "sub ", 4) == 0 && depth > 0 && + depth < MAXDEPTH) { + Menu *m = stack[depth - 1]; + Item *it; + Menu *sub; + + if (m->count >= MAXITEMS) + continue; + it = &m->items[m->count++]; + memset(it, 0, sizeof *it); + snprintf(it->label, sizeof it->label, "%s", p + 4); + sub = calloc(1, sizeof *sub); + if (sub == NULL) + continue; + snprintf(sub->name, sizeof sub->name, "%s", p + 4); + it->sub = sub; + stack[depth++] = sub; + } else if (strcmp(p, "endsub") == 0 && depth > 1) { + depth--; + } else if (strncmp(p, "item ", 5) == 0 && depth > 0) { + Menu *m = stack[depth - 1]; Item *it; char *bar; @@ -129,10 +323,13 @@ ReadMenus(const char *path) if ((bar = strchr(p + 5, '|')) != NULL) { *bar = '\0'; snprintf(it->key, sizeof it->key, "%s", bar + 1); + Trim(it->key); } snprintf(it->label, sizeof it->label, "%s", p + 5); - } else if (strcmp(p, "sep") == 0 && nmenus > 0) { - Menu *m = &menus[nmenus - 1]; + Trim(it->label); + } else if (strcmp(p, "sep") == 0 && depth > 0) { + Menu *m = stack[depth - 1]; + if (m->count < MAXITEMS) m->items[m->count++].separator = 1; } @@ -140,20 +337,22 @@ ReadMenus(const char *path) fclose(f); } -static void -MenuCallback(Widget w, XtPointer data, XtPointer call) +static int +CommandOf(const char *label) { - char *label = (char *) data; - - (void) w; (void) call; - fprintf(stderr, "dtphoto: %s\n", label); + if (strcmp(label, "New...") == 0) return CMD_NEW; + if (strcmp(label, "Open...") == 0) return CMD_OPEN; + if (strcmp(label, "Save") == 0) return CMD_SAVE; + if (strcmp(label, "Save As...") == 0) return CMD_SAVEAS; + if (strcmp(label, "Quit") == 0) return CMD_QUIT; + if (strcmp(label, "Undo") == 0) return CMD_UNDO; + if (strcmp(label, "Zoom In") == 0) return CMD_ZOOMIN; + if (strcmp(label, "Zoom Out") == 0) return CMD_ZOOMOUT; + if (strcmp(label, "Fit on Screen") == 0) return CMD_FIT; + if (strcmp(label, "Actual Pixels") == 0) return CMD_ACTUAL; + return CMD_NONE; } -/* - * "Ctrl+N" (what the menu file says, as the original prints it) to Motif's - * accelerator syntax "CtrlN". Unknown keys are left out: an accelerator - * Motif cannot parse only prints a warning and does nothing. - */ static void MakeAccel(const char *key, char *out, size_t size) { @@ -164,22 +363,18 @@ MakeAccel(const char *key, char *out, size_t size) if (plus != NULL) { size_t n = (size_t) (plus - key); - if (n == 4 && strncmp(key, "Ctrl", 4) == 0) - mod = "Ctrl"; - else if (n == 5 && strncmp(key, "Shift", 5) == 0) - mod = "Shift"; - else if (n == 3 && strncmp(key, "Alt", 3) == 0) - mod = "Alt"; - else if (n == 3 && strncmp(key, "Cmd", 3) == 0) - mod = "Meta"; + if (n == 4 && strncmp(key, "Ctrl", 4) == 0) mod = "Ctrl"; + else if (n == 5 && strncmp(key, "Shift", 5) == 0) mod = "Shift"; + else if (n == 3 && strncmp(key, "Alt", 3) == 0) mod = "Alt"; + else if (n == 3 && strncmp(key, "Cmd", 3) == 0) mod = "Meta"; } - if (strcmp(name, "+") == 0) - name = "plus"; - else if (strcmp(name, "-") == 0) - name = "minus"; - snprintf(out, size, "%s%s%s", mod, mod[0] ? "" : "", name); + if (strcmp(name, "+") == 0) name = "plus"; + else if (strcmp(name, "-") == 0) name = "minus"; + snprintf(out, size, "%s%s", mod, name); } +static void MenuCallback(Widget w, XtPointer data, XtPointer call); + static Widget BuildMenu(Widget parent, Menu *m) { @@ -199,10 +394,20 @@ BuildMenu(Widget parent, Menu *m) shell, NULL); continue; } + if (it->sub != NULL) { + Widget cs, sub = XmCreatePulldownMenu(shell, it->label, + NULL, 0); + + cs = XtVaCreateManagedWidget(it->label, + xmCascadeButtonWidgetClass, shell, + XmNsubMenuId, sub, NULL); + BuildMenu(sub, it->sub); + (void) cs; + continue; + } entry = XtVaCreateManagedWidget(it->label, xmPushButtonWidgetClass, shell, NULL); - XtAddCallback(entry, XmNactivateCallback, MenuCallback, - it->label); + XtAddCallback(entry, XmNactivateCallback, MenuCallback, it->label); if (it->key[0] != '\0') { MakeAccel(it->key, accel, sizeof accel); XtVaSetValues(entry, XmNaccelerator, accel, NULL); @@ -211,13 +416,102 @@ BuildMenu(Widget parent, Menu *m) return cascade; } -/* ------------------------------------------------------------------ icons */ +/* ------------------------------------------------------------------- view */ + +static void +NewPixmaps(void) +{ + if (image != None) + XFreePixmap(dpy, image); + if (snap != None) + XFreePixmap(dpy, snap); + image = XCreatePixmap(dpy, DefaultRootWindow(dpy), docW, docH, + DefaultDepth(dpy, 0)); + snap = XCreatePixmap(dpy, DefaultRootWindow(dpy), docW, docH, + DefaultDepth(dpy, 0)); + if (gc != None) { + XSetForeground(dpy, gc, WhitePixel(dpy, 0)); + XFillRectangle(dpy, image, gc, 0, 0, docW, docH); + } +} + +static void +UpdateStatus(void) +{ + char buf[96]; + + snprintf(buf, sizeof buf, "%d%%", zoom); + XtVaSetValues(statusZoom, XmNlabelString, + XmStringCreateLocalized(buf), NULL); + snprintf(buf, sizeof buf, "Doc: %dK/%dK", docW * docH * 3 / 1024, + docW * docH * 3 / 1024 + 900); + XtVaSetValues(statusDoc, XmNlabelString, + XmStringCreateLocalized(buf), NULL); +} + +static void +RenderDoc(Widget w) +{ + Dimension ww, hh; + Window win = XtWindow(w); + int dw, dh, ox, oy; + + XtVaGetValues(w, XmNwidth, &ww, XmNheight, &hh, NULL); + XSetForeground(dpy, gc, WhitePixel(dpy, 0)); + XFillRectangle(dpy, win, gc, 0, 0, ww, hh); + + dw = docW * zoom / 100; + dh = docH * zoom / 100; + ox = (ww > dw) ? (ww - dw) / 2 : 0; + oy = (hh > dh) ? (hh - dh) / 2 : 0; + if (zoom == 100) { + XCopyArea(dpy, image, win, gc, 0, 0, docW, docH, ox, oy); + } else { + XImage *src, *dst; + int x, y; + + src = XGetImage(dpy, image, 0, 0, docW, docH, AllPlanes, ZPixmap); + if (src == NULL) + return; + dst = XCreateImage(dpy, DefaultVisual(dpy, 0), + DefaultDepth(dpy, 0), ZPixmap, 0, NULL, (unsigned int) dw, + (unsigned int) dh, 32, 0); + if (dst == NULL) { + XDestroyImage(src); + return; + } + dst->data = calloc(1, (size_t) dst->bytes_per_line * dh); + if (dst->data == NULL) { + XDestroyImage(src); + XDestroyImage(dst); + return; + } + for (y = 0; y < dh; y++) + for (x = 0; x < dw; x++) + XPutPixel(dst, x, y, XGetPixel(src, + x * 100 / zoom, y * 100 / zoom)); + XPutImage(dpy, win, gc, dst, 0, 0, ox, oy, (unsigned int) dw, + (unsigned int) dh); + XDestroyImage(src); + XDestroyImage(dst); + } + XSetForeground(dpy, gc, BlackPixel(dpy, 0)); + XDrawRectangle(dpy, win, gc, ox - 1, oy - 1, dw + 1, dh + 1); + UpdateStatus(); +} + +static void +CanvasExpose(Widget w, XtPointer data, XtPointer call) +{ + XmDrawingAreaCallbackStruct *c = (XmDrawingAreaCallbackStruct *) call; + + (void) data; + if (c->reason == XmCR_EXPOSE) + RenderDoc(w); +} + +/* ---------------------------------------------------------------- toolbox */ -/* - * The tool glyphs. Motif draws nothing by itself and the original icons are - * bitmaps; these are drawn with Xlib in the same 16x16 cell, one shape per - * tool, so the toolbox has the reference's look without a bitmap library. - */ static void DrawGlyph(Display *d, Pixmap p, GC gc, int tool) { @@ -229,29 +523,23 @@ DrawGlyph(Display *d, Pixmap p, GC gc, int tool) thin = XCreateGC(d, p, GCForeground, &gv); gv.foreground = WhitePixel(d, 0); white = XCreateGC(d, p, GCForeground, &gv); - XSetForeground(d, gc, WhitePixel(d, 0)); XFillRectangle(d, p, gc, 0, 0, 16, 16); XSetForeground(d, gc, BlackPixel(d, 0)); - switch (tool) { - case 0: /* rectangular marquee */ - XDrawRectangle(d, p, thin, 2, 3, 12, 10); - break; - case 1: /* elliptical marquee */ - XDrawArc(d, p, thin, 2, 3, 12, 10, 0, 360 * 64); - break; - case 2: /* lasso */ + case 0: XDrawRectangle(d, p, thin, 2, 3, 12, 10); break; + case 1: XDrawArc(d, p, thin, 2, 3, 12, 10, 0, 360 * 64); break; + case 2: XDrawArc(d, p, thin, 3, 2, 10, 10, 0, 300 * 64); XDrawLine(d, p, thin, 5, 11, 4, 14); break; - case 3: /* magic wand */ + case 3: XDrawLine(d, p, thin, 4, 12, 11, 5); XDrawLine(d, p, thin, 6, 13, 12, 7); XDrawLine(d, p, thin, 11, 2, 11, 3); XDrawLine(d, p, thin, 13, 4, 13, 5); break; - case 4: /* move: four arrows */ + case 4: XDrawLine(d, p, thin, 8, 2, 8, 14); XDrawLine(d, p, thin, 2, 8, 14, 8); XDrawLine(d, p, thin, 8, 2, 6, 4); @@ -259,71 +547,67 @@ DrawGlyph(Display *d, Pixmap p, GC gc, int tool) XDrawLine(d, p, thin, 8, 14, 6, 12); XDrawLine(d, p, thin, 8, 14, 10, 12); break; - case 5: /* hand */ + case 5: XDrawLine(d, p, thin, 5, 13, 5, 7); XDrawLine(d, p, thin, 8, 13, 8, 5); XDrawLine(d, p, thin, 11, 13, 11, 7); XDrawLine(d, p, thin, 5, 13, 11, 13); break; - case 6: /* zoom */ + case 6: XDrawArc(d, p, thin, 3, 3, 8, 8, 0, 360 * 64); XDrawLine(d, p, thin, 10, 10, 14, 14); break; - case 7: /* crop */ + case 7: XDrawLine(d, p, thin, 4, 2, 4, 12); XDrawLine(d, p, thin, 4, 12, 14, 12); XDrawLine(d, p, thin, 2, 4, 12, 4); XDrawLine(d, p, thin, 12, 4, 12, 14); break; - case 8: /* type */ + case 8: XDrawLine(d, p, thin, 3, 3, 13, 3); XDrawLine(d, p, thin, 8, 3, 8, 13); XDrawLine(d, p, thin, 6, 13, 10, 13); break; - case 9: /* line */ - XDrawLine(d, p, thin, 3, 12, 12, 3); - break; - case 10: /* pencil */ + case 9: XDrawLine(d, p, thin, 3, 12, 12, 3); break; + case 10: XDrawLine(d, p, thin, 3, 13, 11, 5); XDrawLine(d, p, thin, 11, 5, 13, 3); - XDrawLine(d, p, thin, 3, 13, 2, 14); break; - case 11: /* paintbrush */ + case 11: XDrawLine(d, p, thin, 4, 12, 9, 7); XFillRectangle(d, p, thin, 9, 3, 4, 4); break; - case 12: /* eraser */ + case 12: XFillRectangle(d, p, thin, 4, 8, 8, 5); XDrawLine(d, p, thin, 4, 8, 8, 3); XDrawLine(d, p, thin, 11, 8, 14, 4); break; - case 13: /* bucket */ + case 13: XDrawLine(d, p, thin, 3, 8, 9, 2); XDrawLine(d, p, thin, 3, 8, 9, 14); XDrawLine(d, p, thin, 9, 2, 9, 14); XDrawLine(d, p, thin, 12, 10, 12, 14); break; - case 14: /* gradient */ + case 14: for (i = 0; i < 12; i++) - XDrawLine(d, p, i < 6 ? thin : white, 2, 3 + i, - 13, 3 + i); + XDrawLine(d, p, i < 6 ? thin : white, 2, 3 + i, 13, 3 + i); XDrawRectangle(d, p, thin, 2, 3, 12, 11); break; - case 15: /* blur */ + case 15: XDrawArc(d, p, thin, 3, 3, 10, 10, 0, 360 * 64); XDrawArc(d, p, white, 5, 5, 6, 6, 0, 360 * 64); break; - case 16: /* sharpen */ + case 16: XDrawLine(d, p, thin, 8, 2, 8, 14); XDrawLine(d, p, thin, 4, 6, 8, 2); XDrawLine(d, p, thin, 12, 6, 8, 2); break; - case 17: /* smudge */ + case 17: XDrawLine(d, p, thin, 3, 11, 8, 6); XDrawLine(d, p, thin, 6, 13, 11, 8); XDrawLine(d, p, thin, 9, 14, 13, 10); break; - case 18: /* dodge */ + case 18: XDrawArc(d, p, thin, 5, 5, 6, 6, 0, 360 * 64); for (i = 0; i < 8; i++) { int x = 8 + 7 * (i % 2 ? 1 : -1) * (i % 4 < 2 ? 1 : 0); @@ -331,15 +615,13 @@ DrawGlyph(Display *d, Pixmap p, GC gc, int tool) XDrawLine(d, p, thin, 8, 8, x, y); } break; - case 19: /* burn */ - XFillArc(d, p, thin, 4, 4, 8, 8, 0, 360 * 64); - break; - case 20: /* sponge */ + case 19: XFillArc(d, p, thin, 4, 4, 8, 8, 0, 360 * 64); break; + case 20: XDrawRectangle(d, p, thin, 3, 6, 10, 7); XDrawLine(d, p, thin, 5, 8, 5, 11); XDrawLine(d, p, thin, 8, 8, 8, 11); break; - case 21: /* eyedropper */ + case 21: XDrawLine(d, p, thin, 3, 13, 10, 6); XDrawLine(d, p, thin, 9, 3, 13, 7); XDrawLine(d, p, thin, 10, 6, 12, 8); @@ -352,11 +634,10 @@ DrawGlyph(Display *d, Pixmap p, GC gc, int tool) static void ToolCallback(Widget w, XtPointer data, XtPointer call) { - int tool = (int) (long) data; (void) call; if (XmToggleButtonGetState(w)) { - currentTool = tool; - fprintf(stderr, "dtphoto: tool %s\n", toolNames[tool]); + currentTool = (int) (long) data; + fprintf(stderr, "dtphoto: tool %s\n", toolNames[currentTool]); } } @@ -365,11 +646,9 @@ BuildToolbox(Widget parent, Display *d) { Widget row; Pixmap pix; - GC gc; + GC g; int i; - Pixel bg; - XtVaGetValues(parent, XmNbackground, &bg, NULL); row = XtVaCreateManagedWidget("tools", xmRowColumnWidgetClass, parent, XmNnumColumns, 2, XmNpacking, XmPACK_COLUMN, XmNorientation, XmVERTICAL, XmNmarginWidth, 4, @@ -379,13 +658,12 @@ BuildToolbox(Widget parent, Display *d) pix = XCreatePixmap(d, RootWindow(d, 0), 16, 16, DefaultDepth(d, 0)); - gc = XCreateGC(d, pix, 0, NULL); - DrawGlyph(d, pix, gc, i); - XFreeGC(d, gc); + g = XCreateGC(d, pix, 0, NULL); + DrawGlyph(d, pix, g, i); + XFreeGC(d, g); b = XtVaCreateManagedWidget(toolNames[i], - xmToggleButtonWidgetClass, row, - XmNlabelType, XmPIXMAP, XmNlabelPixmap, pix, - XmNindicatorType, XmN_OF_MANY, + xmToggleButtonWidgetClass, row, XmNlabelType, XmPIXMAP, + XmNlabelPixmap, pix, XmNindicatorType, XmN_OF_MANY, XmNshadowThickness, 1, XmNmarginWidth, 2, XmNmarginHeight, 2, XmNwidth, 26, XmNheight, 26, NULL); XtAddCallback(b, XmNvalueChangedCallback, ToolCallback, @@ -393,46 +671,128 @@ BuildToolbox(Widget parent, Display *d) if (i == currentTool) XmToggleButtonSetState(b, True, False); } - (void) bg; return row; } -static void -CanvasExpose(Widget w, XtPointer data, XtPointer call) -{ - XmDrawingAreaCallbackStruct *c = (XmDrawingAreaCallbackStruct *) call; - Display *d = XtDisplay(w); - Window win = XtWindow(w); - GC gc = XCreateGC(d, win, 0, NULL); - Dimension width, height; - int x, y, dw = 320, dh = 240; +/* ---------------------------------------------------------------- actions */ - (void) data; - if (c->reason != XmCR_EXPOSE) - return; - XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, NULL); - XSetForeground(d, gc, WhitePixel(d, 0)); - XFillRectangle(d, win, gc, 0, 0, width, height); - /* the empty document, centred: grey surround, white page */ - XSetForeground(d, gc, BlackPixel(d, 0)); - x = (width > dw) ? (width - dw) / 2 : 0; - y = (height > dh) ? (height - dh) / 2 : 0; - XFillRectangle(d, win, gc, x - 1, y - 1, dw + 2, dh + 2); - XSetForeground(d, gc, WhitePixel(d, 0)); - XFillRectangle(d, win, gc, x, y, dw, dh); - XSetForeground(d, gc, BlackPixel(d, 0)); - XDrawRectangle(d, win, gc, x, y, dw - 1, dh - 1); - XFreeGC(d, gc); +static void +SetZoom(int z) +{ + if (z < 12) z = 12; + if (z > 800) z = 800; + zoom = z; + RenderDoc(canvas); } -/* ------------------------------------------------------------------ main */ +static void +FileOk(Widget w, XtPointer data, XtPointer call) +{ + XmFileSelectionBoxCallbackStruct *c = + (XmFileSelectionBoxCallbackStruct *) call; + char *path = NULL; + + (void) data; + if (XmStringGetLtoR(c->value, XmSTRING_DEFAULT_CHARSET, &path) && + path != NULL) { + int save = fileSave; + + if (save ? (SaveBMP(path) == 0) : (OpenBMP(path) == 0)) { + fprintf(stderr, "dtphoto: %s %s\n", + save ? "saved" : "opened", path); + UpdateStatus(); + RenderDoc(canvas); + } else { + Widget d = XmCreateErrorDialog(top, "error", NULL, 0); + + XtVaSetValues(d, XmNmessageString, + XmStringCreateLocalized(save ? + "The picture could not be saved." : + "The picture could not be opened."), NULL); + XtUnmanageChild(XmMessageBoxGetChild(d, + XmDIALOG_CANCEL_BUTTON)); + XtManageChild(d); + } + XtFree(path); + } + XtDestroyWidget(XtParent(w)); +} + +static void +FileCancel(Widget w, XtPointer data, XtPointer call) +{ + (void) data; (void) call; + XtDestroyWidget(XtParent(w)); +} + +static void +FileDialog(int save) +{ + Widget d = XmCreateFileSelectionDialog(top, "file", NULL, 0); + + fileSave = save; + XtAddCallback(d, XmNokCallback, FileOk, NULL); + XtAddCallback(d, XmNcancelCallback, FileCancel, NULL); + XtVaSetValues(d, XmNfileTypeMask, XmFILE_REGULAR, NULL); + XtManageChild(d); +} + +static void +NewDoc(void) +{ + docW = 320; + docH = 240; + zoom = 100; + NewPixmaps(); + XSetForeground(dpy, gc, WhitePixel(dpy, 0)); + XFillRectangle(dpy, image, gc, 0, 0, docW, docH); + XCopyArea(dpy, image, snap, gc, 0, 0, docW, docH, 0, 0); + UpdateStatus(); + RenderDoc(canvas); +} + +static void +MenuCallback(Widget w, XtPointer data, XtPointer call) +{ + const char *label = (const char *) data; + int cmd = CommandOf(label); + + (void) w; (void) call; + switch (cmd) { + case CMD_NEW: NewDoc(); break; + case CMD_OPEN: FileDialog(0); break; + case CMD_SAVE: + case CMD_SAVEAS: FileDialog(1); break; + case CMD_QUIT: exit(0); + case CMD_UNDO: + XCopyArea(dpy, snap, image, gc, 0, 0, docW, docH, 0, 0); + RenderDoc(canvas); + break; + case CMD_ZOOMIN: SetZoom(zoom * 2); break; + case CMD_ZOOMOUT: SetZoom(zoom / 2); break; + case CMD_FIT: { + Dimension ww, hh; + + XtVaGetValues(canvas, XmNwidth, &ww, XmNheight, &hh, NULL); + SetZoom((int) (100 * (ww - 8) / docW < 100 * (hh - 8) / docH ? + 100 * (ww - 8) / docW : 100 * (hh - 8) / docH)); + break; + } + case CMD_ACTUAL: SetZoom(100); break; + default: + fprintf(stderr, "dtphoto: %s\n", label); + break; + } +} + +/* ------------------------------------------------------------------- main */ int main(int argc, char **argv) { XtAppContext app; - Widget top, mainw, menubar, form, label; - Display *d; + Widget mainw, menubar, form; + char path[512]; int i; static String fallbacks[] = { @@ -447,19 +807,21 @@ main(int argc, char **argv) { "-version", "*version", XrmoptionSepArg, NULL } }; - top = XtVaAppInitialize(&app, "Dtphoto", opts, XtNumber(opts), - &argc, argv, fallbacks, NULL); - d = XtDisplay(top); - - if (XGetDefault(d, "dtphoto", "version") != NULL) + top = XtVaAppInitialize(&app, "Dtphoto", opts, XtNumber(opts), &argc, + argv, fallbacks, NULL); + dpy = XtDisplay(top); + if (XGetDefault(dpy, "dtphoto", "version") != NULL) snprintf(version, sizeof version, "%s", - XGetDefault(d, "dtphoto", "version")); + XGetDefault(dpy, "dtphoto", "version")); ReadMenus(CDE_INSTALLATION_TOP "/share/dtphoto/dtphoto.menu"); if (nmenus == 0) ReadMenus("/usr/pkg/dt/share/dtphoto/dtphoto.menu"); if (nmenus == 0) - ReadMenus("dtphoto.menu"); /* from the source directory */ + ReadMenus("dtphoto.menu"); + + image = snap = None; + gc = XCreateGC(dpy, DefaultRootWindow(dpy), 0, NULL); mainw = XmCreateMainWindow(top, "main", NULL, 0); XtManageChild(mainw); @@ -468,8 +830,8 @@ main(int argc, char **argv) for (i = 0; i < nmenus; i++) { BuildMenu(menubar, &menus[i]); if (getenv("DTPHOTO_DEBUG") != NULL) - fprintf(stderr, "dtphoto: menu[%d] \"%s\" (%d)\n", - i, menus[i].name, menus[i].count); + fprintf(stderr, "dtphoto: menu[%d] \"%s\" (%d)\n", i, + menus[i].name, menus[i].count); } form = XtVaCreateManagedWidget("form", xmFormWidgetClass, mainw, @@ -481,41 +843,73 @@ main(int argc, char **argv) XmNrightAttachment, XmATTACH_FORM, NULL); XtAddCallback(canvas, XmNexposeCallback, CanvasExpose, NULL); - label = XtVaCreateManagedWidget("status", xmLabelWidgetClass, form, - XmNbottomAttachment, XmATTACH_FORM, - XmNleftAttachment, XmATTACH_FORM, XmNalignment, XmALIGNMENT_BEGINNING, - XmNrecomputeSize, False, XmNwidth, 90, NULL); - XtVaSetValues(label, XmNlabelString, - XmStringCreateLocalized("100%"), NULL); - statusZoom = label; - + statusZoom = XtVaCreateManagedWidget("zoom", xmLabelWidgetClass, form, + XmNbottomAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM, + XmNalignment, XmALIGNMENT_BEGINNING, XmNrecomputeSize, False, + XmNwidth, 90, NULL); statusDoc = XtVaCreateManagedWidget("doc", xmLabelWidgetClass, form, - XmNbottomAttachment, XmATTACH_FORM, - XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, label, - XmNalignment, XmALIGNMENT_CENTER, XmNrecomputeSize, False, - XmNwidth, 200, NULL); - XtVaSetValues(statusDoc, XmNlabelString, - XmStringCreateLocalized("Doc: 320K/1.2M"), NULL); - + XmNbottomAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_WIDGET, + XmNleftWidget, statusZoom, XmNalignment, XmALIGNMENT_CENTER, + XmNrecomputeSize, False, XmNwidth, 220, NULL); statusScratch = XtVaCreateManagedWidget("scratch", xmLabelWidgetClass, - form, XmNbottomAttachment, XmATTACH_FORM, - XmNrightAttachment, XmATTACH_FORM, - XmNalignment, XmALIGNMENT_END, XmNrecomputeSize, False, - XmNwidth, 200, NULL); + form, XmNbottomAttachment, XmATTACH_FORM, XmNrightAttachment, + XmATTACH_FORM, XmNalignment, XmALIGNMENT_END, XmNrecomputeSize, + False, XmNwidth, 220, NULL); XtVaSetValues(statusScratch, XmNlabelString, XmStringCreateLocalized("Scratch: 1.2M/50M"), NULL); XtVaSetValues(top, XmNtitle, "dtphoto", NULL); XtRealizeWidget(top); - /* the toolbox is a window of its own, as in the original */ - toolShell = XtVaAppCreateShell("tools", "Dtphoto", topLevelShellWidgetClass, - d, XmNtitle, "Tools", XmNallowShellResize, True, - XmNx, 8, XmNy, 64, NULL); - BuildToolbox(toolShell, d); + toolShell = XtVaAppCreateShell("tools", "Dtphoto", + topLevelShellWidgetClass, dpy, XmNtitle, "Tools", + XmNallowShellResize, True, XmNx, 8, XmNy, 64, NULL); + BuildToolbox(toolShell, dpy); XtRealizeWidget(toolShell); XtPopup(toolShell, XtGrabNone); + NewDoc(); + + /* + * The round-trip check: open A, save B, open B, compare pixels. + * Runs without a window manager and without a person at the screen. + */ + if (argc >= 4 && strcmp(argv[1], "-selftest") == 0) { + XImage *a, *b; + int x, y, ok = 1; + + if (OpenBMP(argv[2]) != 0 || SaveBMP(argv[3]) != 0) { + printf("dtphoto selftest: FAIL open/save\n"); + return 1; + } + a = XGetImage(dpy, snap, 0, 0, docW, docH, AllPlanes, ZPixmap); + if (OpenBMP(argv[3]) != 0) { + printf("dtphoto selftest: FAIL reopen\n"); + return 1; + } + b = XGetImage(dpy, image, 0, 0, docW, docH, AllPlanes, ZPixmap); + if (a == NULL || b == NULL) { + printf("dtphoto selftest: FAIL image\n"); + return 1; + } + for (y = 0; y < docH && ok; y += 37) + for (x = 0; x < docW; x += 41) + if (XGetPixel(a, x, y) != XGetPixel(b, x, y)) + ok = 0; + XDestroyImage(a); + XDestroyImage(b); + printf("dtphoto selftest: %s\n", ok ? "PASS" : "FAIL"); + return ok ? 0 : 1; + } + + /* a picture on the command line, as the original opens one */ + if (argc > 1 && argv[1][0] != '-') { + snprintf(path, sizeof path, "%s", argv[1]); + if (OpenBMP(path) == 0) { + UpdateStatus(); + RenderDoc(canvas); + } + } XtAppMainLoop(app); return 0; } diff --git a/programs/dtphoto/dtphoto.menu b/programs/dtphoto/dtphoto.menu index f5d4f5d4..3174aa8f 100644 --- a/programs/dtphoto/dtphoto.menu +++ b/programs/dtphoto/dtphoto.menu @@ -1,12 +1,13 @@ # dtphoto.menu -- the menus of dtphoto, the Photoshop 2.5.2/3.0.1 look # -# One line per entry: item