diff --git a/AGENTS.md b/AGENTS.md index 1d89be92..efb1fffe 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -69,9 +69,18 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt status lines and the open/save errors) keeps its menus as the reference's (`dtphoto.menu`, docs/reference/photoshop-ui.csv) -- they are the look of the program, not its sentences. dtpaint builds its menus in code and its - labels are catalogued (`dtpaint.msg`, 23 messages); the program has no - render table, so its German strings avoid umlauts (a UTF-8 `ü` came out - as two latin-1 characters under the core font, measured 2026-11-16). + labels are catalogued (`dtpaint.msg`, 34 messages: menus, the file/error/ + discard dialogs, Help and About); the program has no render table, so its + German strings avoid umlauts (a UTF-8 `ü` came out as two latin-1 + characters under the core font, measured 2026-11-16; the German copy + transliterates ae/oe/ue throughout, re-measured 2026-11-21). Its canvas + is a drawing area of exactly the image size inside a scrolled window (a + click outside the image must never reach XGetImage -- the picker crashed + there), undo is one snapshot taken before every image-changing operation + and refreshed after a load, and Open/Save/Invert convert colours through + the visual's masks on TrueColor (or in batches otherwise), never one X + round trip per pixel (measured 2026-11-21: open 18.3 s -> 0.02 s, invert + 14.9 s -> 0.05 s, save 6.3 s -> 0.23 s on a 1024x768 BMP). McdeSys has a catalog too (`lib/McdeSys/mcdesys.msg`, `lib/McdeSys/ text.c`, `mcde_sys_msg`): common.c's tool errors ask it for their sentence (messages 1-9: not there, cannot start, failed with status, no pipe, no diff --git a/programs/dtpaint/dtpaint.c b/programs/dtpaint/dtpaint.c index 780c855e..ec319d18 100644 --- a/programs/dtpaint/dtpaint.c +++ b/programs/dtpaint/dtpaint.c @@ -5,12 +5,21 @@ * * The standard, simple drawing program of MCDE: toolbox, colour box, canvas * and status line, the sixteen tools and the six menus of the reference - * (docs/MCDE-PROGRAMS.md, C4; provenance in section 8 there). + * (docs/MCDE-PROGRAMS.md, C3; provenance in section 8 there). * * Working in this first stage: pencil, brush, eraser, fill, pick colour, * line, rectangle, ellipse, rounded rectangle, undo, invert, flip, new, * open and save as BMP. Select, text, curve, polygon, airbrush and zoom - * are in the toolbox and in the menus but do nothing yet. + * are in the toolbox but do nothing yet; their menu entries that need them + * (Clear Selection, Edit Colors) are insensitive until they exist. + * + * The canvas is a drawing area of exactly the image size inside a scrolled + * window, so a large image scrolls instead of being clipped, and a click + * outside the image can never reach XGetImage (the picker used to crash + * there). Undo is one step: a snapshot is taken before every operation + * that changes the image, and refreshed after a load. Open/Save/Invert + * convert colours in batches (or directly through the visual's masks on a + * TrueColor display), never one X round trip per pixel. * * C99 and POSIX, Motif and Xlib. */ @@ -22,6 +31,7 @@ #include #include #include +#include #include #include @@ -36,11 +46,13 @@ #include #include #include +#include #include #include /* dtpaint's message catalog (programs/dtpaint/dtpaint.msg, the locale's copy - * in programs/localized//msg, under lib/nls/msg) */ + * in programs/localized//msg, under lib/nls/msg). The core font has + * no UTF-8 rendition, so the German copy transliterates umlauts. */ static nl_catd cat = (nl_catd) -1; static const char * @@ -58,6 +70,13 @@ PaintMsg(int n, const char *text) return catgets(cat, 1, n, text); } +/* a catalog sentence as a compound string (XmStringCreateLocalized copies) */ +static XmString +Msg(int n, const char *text) +{ + return XmStringCreateLocalized((String) PaintMsg(n, text)); +} + #define PAL_COLS 14 #define PAL_ROWS 2 #define PAL_N (PAL_COLS * PAL_ROWS) @@ -85,50 +104,163 @@ static const char *const toolNames[] = { }; static Display *dpy; -static Widget top, mainw, menubar, form, canvas, colorbox, toolbox; -static Widget statusHelp, statusPos; +static Screen *scr; +static Visual *vis; +static Colormap cmap; +static int depth; +static int truecolor; +static unsigned long rmask, gmask, bmask; +static int rshift, gshift, bshift; +static unsigned long rmax, gmax, bmax; +static Widget top, mainw, form, scroll, canvas, colorbox, toolbox, leftcol; +static Widget statusHelp, statusPos, fgbg; static Pixmap pix, snap; static GC gc; static int imgW = 640, imgH = 480; static int tool = T_PENCIL; -static int fg = 0, bg = 14; +static unsigned long fgPix, bgPix; static XColor palette[PAL_N]; static int drawing, sx, sy, lx, ly; -static int curx, cury; static int toolsShown = 1, colorsShown = 1, statusShown = 1; +static char curPath[512]; +static int dirty; +static int pendingAction; -/* ------------------------------------------------------------------ files */ +/* ------------------------------------------------------------------ colour */ static int -PixelRGB(unsigned long pixel, unsigned char *r, unsigned char *g, unsigned char *b) +ShiftOf(unsigned long mask) +{ + int s = 0; + + if (mask == 0) + return 0; + while ((mask & 1) == 0) { + mask >>= 1; + s++; + } + return s; +} + +static void +InitColor(void) +{ + scr = XtScreen(top); + vis = DefaultVisualOfScreen(scr); + cmap = DefaultColormapOfScreen(scr); + depth = DefaultDepthOfScreen(scr); + rmask = vis->red_mask; + gmask = vis->green_mask; + bmask = vis->blue_mask; + rshift = ShiftOf(rmask); + gshift = ShiftOf(gmask); + bshift = ShiftOf(bmask); + rmax = rmask >> rshift; + gmax = gmask >> gshift; + bmax = bmask >> bshift; + truecolor = (vis->class == TrueColor || vis->class == DirectColor); +} + +/* RGB (0..255) to a pixel. On TrueColor/DirectColor this is arithmetic on + * the visual's masks -- no server round trip. Otherwise one XAllocColor. */ +static unsigned long +RGBToPixel(unsigned r, unsigned g, unsigned b) { - static unsigned long cachePixel[PAL_N + 8]; - static unsigned char cacheRGB[PAL_N + 8][3]; - static int ncache; - int i; XColor c; - for (i = 0; i < ncache; i++) - if (cachePixel[i] == pixel) { - *r = cacheRGB[i][0]; *g = cacheRGB[i][1]; *b = cacheRGB[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 (ncache < PAL_N + 8) { - cachePixel[ncache] = pixel; - cacheRGB[ncache][0] = *r; - cacheRGB[ncache][1] = *g; - cacheRGB[ncache][2] = *b; - ncache++; - } - return 0; + if (truecolor) + return ((unsigned long) (r * rmax / 255) << rshift) | + ((unsigned long) (g * gmax / 255) << gshift) | + ((unsigned long) (b * bmax / 255) << bshift); + c.red = (unsigned short) (r * 257); + c.green = (unsigned short) (g * 257); + c.blue = (unsigned short) (b * 257); + c.flags = DoRed | DoGreen | DoBlue; + if (!XAllocColor(dpy, cmap, &c)) + return 0; + return c.pixel; } +static void +PixelToRGB(unsigned long p, unsigned *r, unsigned *g, unsigned *b) +{ + XColor c; + + if (truecolor) { + *r = (unsigned) (((p & rmask) >> rshift) * 255 / rmax); + *g = (unsigned) (((p & gmask) >> gshift) * 255 / gmax); + *b = (unsigned) (((p & bmask) >> bshift) * 255 / bmax); + return; + } + c.pixel = p; + if (!XQueryColor(dpy, cmap, &c)) { + *r = *g = *b = 0; + return; + } + *r = c.red >> 8; + *g = c.green >> 8; + *b = c.blue >> 8; +} + +static int +CmpUL(const void *a, const void *b) +{ + unsigned long x = *(const unsigned long *) a; + unsigned long y = *(const unsigned long *) b; + + return (x < y) ? -1 : (x > y) ? 1 : 0; +} + +/* Batch pixel->RGB for a non-TrueColor visual: one XQueryColors for the + * distinct pixels instead of one XQueryColor per image pixel. */ +static void +BatchQuery(unsigned long *pix, int n, unsigned char *rgb) +{ + XColor *cols; + int i; + + cols = malloc(sizeof(XColor) * (size_t) n); + if (cols == NULL) + return; + for (i = 0; i < n; i++) + cols[i].pixel = pix[i]; + if (!XQueryColors(dpy, cmap, cols, n)) { + free(cols); + return; + } + for (i = 0; i < n; i++) { + rgb[i * 3] = (unsigned char) (cols[i].red >> 8); + rgb[i * 3 + 1] = (unsigned char) (cols[i].green >> 8); + rgb[i * 3 + 2] = (unsigned char) (cols[i].blue >> 8); + } + free(cols); +} + +/* Batch RGB->pixel for a non-TrueColor visual. Xlib has no plural alloc, + * but a PseudoColor colormap holds at most a few hundred cells, so one + * XAllocColor per *distinct* colour is bounded and cheap -- unlike one per + * image pixel. */ +static void +BatchAlloc(const unsigned char *rgb, int n, unsigned long *pix) +{ + XColor c; + int i; + + for (i = 0; i < n; i++) { + c.red = (unsigned short) (rgb[i * 3] * 257); + c.green = (unsigned short) (rgb[i * 3 + 1] * 257); + c.blue = (unsigned short) (rgb[i * 3 + 2] * 257); + c.flags = DoRed | DoGreen | DoBlue; + c.pixel = 0; + if (XAllocColor(dpy, cmap, &c)) + pix[i] = c.pixel; + else + pix[i] = 0; + } +} + +/* ------------------------------------------------------------------ files */ + static void PutLE(unsigned char *p, unsigned long v, int n) { @@ -145,6 +277,9 @@ SaveBMP(const char *path) FILE *f; int row = ((imgW * 3 + 3) / 4) * 4; unsigned char *data; + unsigned long *keys = NULL; + unsigned char *krgb = NULL; + int nkeys = 0; int x, y; unsigned long size = (unsigned long) row * imgH; @@ -154,15 +289,57 @@ SaveBMP(const char *path) XDestroyImage(im); return -1; } + if (!truecolor) { + /* gather the distinct pixels, query them in one round trip */ + keys = malloc(sizeof(unsigned long) * (size_t) imgW * imgH); + if (keys != NULL) { + for (y = 0; y < imgH; y++) + for (x = 0; x < imgW; x++) + keys[nkeys++] = XGetPixel(im, x, y); + qsort(keys, (size_t) nkeys, sizeof(unsigned long), CmpUL); + { + int i, j = 0; + + for (i = 0; i < nkeys; i++) + if (i == 0 || keys[i] != keys[i - 1]) + keys[j++] = keys[i]; + nkeys = j; + } + krgb = malloc((size_t) nkeys * 3); + if (krgb != NULL) + BatchQuery(keys, nkeys, krgb); + } + } for (y = 0; y < imgH; y++) for (x = 0; x < imgW; x++) { - unsigned char r, g, b; + unsigned r, g, b; unsigned char *p = data + (size_t) (imgH - 1 - y) * row + x * 3; + unsigned long px = XGetPixel(im, x, y); - if (PixelRGB(XGetPixel(im, x, y), &r, &g, &b) < 0) - continue; - p[0] = b; p[1] = g; p[2] = r; + if (truecolor) { + PixelToRGB(px, &r, &g, &b); + } else if (keys != NULL && krgb != NULL) { + unsigned long *hit = bsearch(&px, keys, (size_t) nkeys, + sizeof(unsigned long), CmpUL); + + if (hit == NULL) + continue; + { + size_t idx = (size_t) (hit - keys); + + r = krgb[idx * 3]; + g = krgb[idx * 3 + 1]; + b = krgb[idx * 3 + 2]; + } + } else { + PixelToRGB(px, &r, &g, &b); + } + p[0] = (unsigned char) b; + p[1] = (unsigned char) g; + p[2] = (unsigned char) r; } + free(keys); + free(krgb); XDestroyImage(im); if ((f = fopen(path, "wb")) == NULL) { free(data); @@ -183,10 +360,21 @@ SaveBMP(const char *path) PutLE(hdr + 34, size, 4); PutLE(hdr + 38, 2835, 4); PutLE(hdr + 42, 2835, 4); - fwrite(hdr, 1, sizeof hdr, f); + if (fwrite(hdr, 1, sizeof hdr, f) != sizeof hdr) { + fclose(f); + free(data); + return -1; + } + } + if (fwrite(data, 1, (size_t) size, f) != (size_t) size) { + fclose(f); + free(data); + return -1; + } + if (fclose(f) != 0) { + free(data); + return -1; } - fwrite(data, 1, (size_t) size, f); - fclose(f); free(data); return 0; } @@ -207,10 +395,12 @@ OpenBMP(const char *path) 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) { + off = hdr[10] | (hdr[11] << 8) | (hdr[12] << 16) | ((unsigned long) hdr[13] << 24); + w = hdr[18] | (hdr[19] << 8) | (hdr[20] << 16) | ((unsigned long) hdr[21] << 24); + h = hdr[22] | (hdr[23] << 8) | (hdr[24] << 16) | ((unsigned long) hdr[25] << 24); + /* a 40-byte BITMAPINFOHEADER, 24-bit, uncompressed, bottom-up */ + if ((hdr[14] | (hdr[15] << 8)) != 40 || hdr[28] != 24 || + hdr[30] != 0 || w == 0 || h == 0 || w > 4096 || h > 4096) { fclose(f); return -1; } @@ -219,8 +409,8 @@ OpenBMP(const char *path) fclose(f); return -1; } - fseek(f, (long) off, SEEK_SET); - if (fread(data, 1, row * h, f) != row * h) { + if (fseek(f, (long) off, SEEK_SET) != 0 || + fread(data, 1, row * h, f) != row * h) { free(data); fclose(f); return -1; @@ -228,23 +418,34 @@ OpenBMP(const char *path) fclose(f); if ((int) w != imgW || (int) h != imgH) { + Pixmap np, ns; + imgW = (int) w; imgH = (int) h; + np = XCreatePixmap(dpy, RootWindowOfScreen(scr), imgW, imgH, depth); + ns = XCreatePixmap(dpy, RootWindowOfScreen(scr), imgW, imgH, depth); + if (np == None || ns == None) { + if (np != None) + XFreePixmap(dpy, np); + if (ns != None) + XFreePixmap(dpy, ns); + free(data); + return -1; + } if (pix != None) XFreePixmap(dpy, pix); - pix = XCreatePixmap(dpy, DefaultRootWindow(dpy), imgW, imgH, - DefaultDepth(dpy, 0)); if (snap != None) XFreePixmap(dpy, snap); - snap = XCreatePixmap(dpy, DefaultRootWindow(dpy), imgW, imgH, - DefaultDepth(dpy, 0)); - XtVaSetValues(canvas, XmNwidth, imgW, XmNheight, imgH, NULL); + pix = np; + snap = ns; + if (canvas != NULL) + XtVaSetValues(canvas, XmNwidth, imgW, XmNheight, imgH, NULL); } - XSetForeground(dpy, gc, palette[bg].pixel); + XSetForeground(dpy, gc, bgPix); XFillRectangle(dpy, pix, gc, 0, 0, imgW, imgH); /* draw through an XImage so that the colours stay exact */ - im = XCreateImage(dpy, DefaultVisual(dpy, 0), DefaultDepth(dpy, 0), - ZPixmap, 0, NULL, (unsigned int) w, (unsigned int) h, 32, 0); + im = XCreateImage(dpy, vis, depth, ZPixmap, 0, NULL, + (unsigned int) w, (unsigned int) h, 32, 0); if (im == NULL) { free(data); return -1; @@ -255,19 +456,74 @@ OpenBMP(const char *path) 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; + if (truecolor) { + 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; - 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); + XPutPixel(im, x, y, RGBToPixel(p[2], p[1], p[0])); + } + } else { + /* distinct colours, one XAllocColors for all of them */ + unsigned long *keys = malloc(sizeof(unsigned long) * (size_t) w * h); + unsigned long *kpix = NULL; + int nkeys = 0; + + if (keys != NULL) { + 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; + + keys[nkeys++] = ((unsigned long) p[2] << 16) | + ((unsigned long) p[1] << 8) | p[0]; + } + qsort(keys, (size_t) nkeys, sizeof(unsigned long), CmpUL); + { + int i, j = 0; + + for (i = 0; i < nkeys; i++) + if (i == 0 || keys[i] != keys[i - 1]) + keys[j++] = keys[i]; + nkeys = j; + } + kpix = malloc(sizeof(unsigned long) * (size_t) nkeys); + if (kpix != NULL) { + unsigned char *krgb = malloc((size_t) nkeys * 3); + + if (krgb != NULL) { + int i; + + for (i = 0; i < nkeys; i++) { + krgb[i * 3] = (unsigned char) (keys[i] >> 16); + krgb[i * 3 + 1] = (unsigned char) (keys[i] >> 8); + krgb[i * 3 + 2] = (unsigned char) keys[i]; + } + BatchAlloc(krgb, nkeys, kpix); + free(krgb); + } + } } + 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; + unsigned long key = ((unsigned long) p[2] << 16) | + ((unsigned long) p[1] << 8) | p[0]; + unsigned long px = 0; + + if (keys != NULL && kpix != NULL) { + unsigned long *hit = bsearch(&key, keys, (size_t) nkeys, + sizeof(unsigned long), CmpUL); + + if (hit != NULL) + px = kpix[hit - keys]; + } else { + px = RGBToPixel(p[2], p[1], p[0]); + } + XPutPixel(im, x, y, px); + } + free(keys); + free(kpix); + } XPutImage(dpy, pix, gc, im, 0, 0, 0, 0, (unsigned int) w, (unsigned int) h); XDestroyImage(im); @@ -278,9 +534,15 @@ OpenBMP(const char *path) /* --------------------------------------------------------------- drawing */ static void -SetFg(int color) +SetFg(unsigned long color) { - XSetForeground(dpy, gc, palette[color].pixel); + XSetForeground(dpy, gc, color); +} + +static void +Snapshot(void) +{ + XCopyArea(dpy, pix, snap, gc, 0, 0, imgW, imgH, 0, 0); } static void @@ -290,7 +552,7 @@ RestoreSnap(void) } static void -DrawDab(int x, int y, int radius, int color) +DrawDab(int x, int y, int radius, unsigned long color) { SetFg(color); XFillArc(dpy, pix, gc, x - radius, y - radius, radius * 2 + 1, @@ -298,14 +560,14 @@ DrawDab(int x, int y, int radius, int color) } static void -DrawLineTo(int x0, int y0, int x1, int y1, int color) +DrawLineTo(int x0, int y0, int x1, int y1, unsigned long color) { SetFg(color); XDrawLine(dpy, pix, gc, x0, y0, x1, y1); } static void -DrawShape(int x0, int y0, int x1, int y1, int color) +DrawShape(int x0, int y0, int x1, int y1, unsigned long color) { int x = x0 < x1 ? x0 : x1; int y = y0 < y1 ? y0 : y1; @@ -339,37 +601,82 @@ DrawShape(int x0, int y0, int x1, int y1, int color) } } +/* Scanline flood fill: the stack holds one seed per run, not four neighbours + * per pixel, so its size follows the shape's perimeter, not its area. */ +struct Seed { int x, y; }; + static void -FloodFill(int x, int y, int color) +FloodFill(int x, int y, unsigned long color) { XImage *im; unsigned long old; - int *stack, sp = 0, max = imgW * imgH; + struct Seed *stack; + int sp = 0, cap = 256; if (x < 0 || y < 0 || x >= imgW || y >= imgH) return; if ((im = XGetImage(dpy, pix, 0, 0, imgW, imgH, AllPlanes, ZPixmap)) == NULL) return; old = XGetPixel(im, x, y); - if (old == palette[color].pixel) { + if (old == color) { XDestroyImage(im); return; } - if ((stack = malloc(sizeof(int) * (size_t) max * 4)) == NULL) { + stack = malloc(sizeof(*stack) * (size_t) cap); + if (stack == NULL) { XDestroyImage(im); return; } - stack[sp++] = y * imgW + x; + stack[sp].x = x; + stack[sp].y = y; + sp++; while (sp > 0) { - int p = stack[--sp], px = p % imgW, py = p / imgW; + int cx, cy, x1, x2, i, dy; - if (XGetPixel(im, px, py) != old) + sp--; + cx = stack[sp].x; + cy = stack[sp].y; + if (XGetPixel(im, cx, cy) != old) continue; - XPutPixel(im, px, py, palette[color].pixel); - if (px > 0) stack[sp++] = p - 1; - if (px < imgW - 1) stack[sp++] = p + 1; - if (py > 0) stack[sp++] = p - imgW; - if (py < imgH - 1) stack[sp++] = p + imgW; + x1 = cx; + while (x1 > 0 && XGetPixel(im, x1 - 1, cy) == old) + x1--; + x2 = cx; + while (x2 < imgW - 1 && XGetPixel(im, x2 + 1, cy) == old) + x2++; + for (i = x1; i <= x2; i++) + XPutPixel(im, i, cy, color); + for (dy = -1; dy <= 1; dy += 2) { + int ny = cy + dy; + int inrun = 0; + + if (ny < 0 || ny >= imgH) + continue; + for (i = x1; i <= x2; i++) { + if (XGetPixel(im, i, ny) == old) { + if (!inrun) { + if (sp == cap) { + struct Seed *ns; + + cap *= 2; + ns = realloc(stack, sizeof(*stack) * (size_t) cap); + if (ns == NULL) { + free(stack); + XDestroyImage(im); + return; + } + stack = ns; + } + stack[sp].x = i; + stack[sp].y = ny; + sp++; + inrun = 1; + } + } else { + inrun = 0; + } + } + } } free(stack); XPutImage(dpy, pix, gc, im, 0, 0, 0, 0, imgW, imgH); @@ -384,20 +691,65 @@ InvertImage(void) if ((im = XGetImage(dpy, pix, 0, 0, imgW, imgH, AllPlanes, ZPixmap)) == NULL) return; - for (y = 0; y < imgH; y++) - for (x = 0; x < imgW; x++) { - unsigned char r, g, b; - XColor c; + if (truecolor) { + for (y = 0; y < imgH; y++) + for (x = 0; x < imgW; x++) { + unsigned r, g, b; - if (PixelRGB(XGetPixel(im, x, y), &r, &g, &b) < 0) - continue; - c.red = (unsigned short) ((255 - r) << 8); - c.green = (unsigned short) ((255 - g) << 8); - c.blue = (unsigned short) ((255 - b) << 8); - c.flags = DoRed | DoGreen | DoBlue; - if (XAllocColor(dpy, DefaultColormap(dpy, 0), &c)) - XPutPixel(im, x, y, c.pixel); + PixelToRGB(XGetPixel(im, x, y), &r, &g, &b); + XPutPixel(im, x, y, RGBToPixel(255 - r, 255 - g, 255 - b)); + } + } else { + unsigned long *keys = malloc(sizeof(unsigned long) * (size_t) imgW * imgH); + unsigned char *krgb = NULL; + unsigned long *knew = NULL; + int nkeys = 0; + + if (keys != NULL) { + for (y = 0; y < imgH; y++) + for (x = 0; x < imgW; x++) + keys[nkeys++] = XGetPixel(im, x, y); + qsort(keys, (size_t) nkeys, sizeof(unsigned long), CmpUL); + { + int i, j = 0; + + for (i = 0; i < nkeys; i++) + if (i == 0 || keys[i] != keys[i - 1]) + keys[j++] = keys[i]; + nkeys = j; + } + krgb = malloc((size_t) nkeys * 3); + knew = malloc(sizeof(unsigned long) * (size_t) nkeys); + if (krgb != NULL && knew != NULL) { + int i; + + BatchQuery(keys, nkeys, krgb); + for (i = 0; i < nkeys * 3; i++) + krgb[i] = (unsigned char) (255 - krgb[i]); + BatchAlloc(krgb, nkeys, knew); + } } + for (y = 0; y < imgH; y++) + for (x = 0; x < imgW; x++) { + unsigned long px = XGetPixel(im, x, y); + + if (keys != NULL && knew != NULL) { + unsigned long *hit = bsearch(&px, keys, (size_t) nkeys, + sizeof(unsigned long), CmpUL); + + if (hit != NULL) + XPutPixel(im, x, y, knew[hit - keys]); + } else { + unsigned r, g, b; + + PixelToRGB(px, &r, &g, &b); + XPutPixel(im, x, y, RGBToPixel(255 - r, 255 - g, 255 - b)); + } + } + free(keys); + free(krgb); + free(knew); + } XPutImage(dpy, pix, gc, im, 0, 0, 0, 0, imgW, imgH); XDestroyImage(im); } @@ -405,26 +757,55 @@ InvertImage(void) static void FlipHorizontal(void) { - Pixmap tmp = XCreatePixmap(dpy, DefaultRootWindow(dpy), imgW, imgH, - DefaultDepth(dpy, 0)); + Pixmap tmp = XCreatePixmap(dpy, RootWindowOfScreen(scr), imgW, imgH, depth); + int x; + if (tmp == None) + return; XCopyArea(dpy, pix, tmp, gc, 0, 0, imgW, imgH, 0, 0); - { - int x; - - for (x = 0; x < imgW; x++) - XCopyArea(dpy, tmp, pix, gc, x, 0, 1, imgH, imgW - 1 - x, 0); - } + for (x = 0; x < imgW; x++) + XCopyArea(dpy, tmp, pix, gc, x, 0, 1, imgH, imgW - 1 - x, 0); XFreePixmap(dpy, tmp); } +static void +Redraw(void) +{ + if (canvas != NULL && XtWindow(canvas) != None) + XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True); +} + +static void +SetTitle(void) +{ + char title[600]; + const char *name = curPath[0] ? strrchr(curPath, '/') : NULL; + + name = (name != NULL) ? name + 1 : (curPath[0] ? curPath : PaintMsg(24, "untitled")); + snprintf(title, sizeof title, "%s - dtpaint", name); + XtVaSetValues(top, XmNtitle, title, NULL); +} + static void NewImage(void) { - XSetForeground(dpy, gc, palette[bg].pixel); + XSetForeground(dpy, gc, bgPix); XFillRectangle(dpy, pix, gc, 0, 0, imgW, imgH); - XCopyArea(dpy, pix, snap, gc, 0, 0, imgW, imgH, 0, 0); - XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True); + Snapshot(); + dirty = 0; + curPath[0] = '\0'; + SetTitle(); + Redraw(); +} + +static void +ClearImage(void) +{ + Snapshot(); + XSetForeground(dpy, gc, bgPix); + XFillRectangle(dpy, pix, gc, 0, 0, imgW, imgH); + dirty = 1; + Redraw(); } /* ----------------------------------------------------------------- events */ @@ -433,10 +814,37 @@ static void StatusPos(int x, int y) { char buf[96]; + XmString s; snprintf(buf, sizeof buf, "%d,%d", x, y); - XtVaSetValues(statusPos, XmNlabelString, - XmStringCreateLocalized(buf), NULL); + s = XmStringCreateLocalized(buf); + XtVaSetValues(statusPos, XmNlabelString, s, NULL); + XmStringFree(s); +} + +/* the foreground/background indicator: fg square in front, bg behind */ +static void +FgBgExpose(Widget w, XtPointer cd, XtPointer call) +{ + XmDrawingAreaCallbackStruct *c = (XmDrawingAreaCallbackStruct *) call; + + (void) cd; + if (c->reason != XmCR_EXPOSE) + return; + XSetForeground(dpy, gc, bgPix); + XFillRectangle(dpy, XtWindow(w), gc, 14, 14, 18, 18); + XSetForeground(dpy, gc, fgPix); + XFillRectangle(dpy, XtWindow(w), gc, 2, 2, 18, 18); + XSetForeground(dpy, gc, BlackPixelOfScreen(scr)); + XDrawRectangle(dpy, XtWindow(w), gc, 2, 2, 17, 17); + XDrawRectangle(dpy, XtWindow(w), gc, 14, 14, 17, 17); +} + +static void +ShowColors(void) +{ + if (fgbg != NULL && XtWindow(fgbg) != None) + XClearArea(dpy, XtWindow(fgbg), 0, 0, 34, 34, True); } static void @@ -453,24 +861,30 @@ CanvasExpose(Widget w, XtPointer cd, XtPointer call) static void CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) { - int color, x, y; + unsigned long color; + int x, y; (void) w; (void) cd; (void) keep; switch (ev->type) { case ButtonPress: if (ev->xbutton.button != Button1 && ev->xbutton.button != Button3) return; - color = (ev->xbutton.button == Button3) ? bg : fg; x = ev->xbutton.x; y = ev->xbutton.y; + /* the drawing area is exactly the image; a click outside it + * must never reach XGetImage/XDraw* on the pixmap */ + if (x < 0 || y < 0 || x >= imgW || y >= imgH) + return; + color = (ev->xbutton.button == Button3) ? bgPix : fgPix; sx = lx = x; sy = ly = y; - curx = x; cury = y; StatusPos(x, y); drawing = 1; switch (tool) { case T_FILL: + Snapshot(); FloodFill(x, y, color); + dirty = 1; drawing = 0; break; case T_PICK: @@ -480,62 +894,76 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) if (im != NULL) { unsigned long p = XGetPixel(im, 0, 0); - int i; - for (i = 0; i < PAL_N; i++) - if (palette[i].pixel == p) - fg = i; + if (ev->xbutton.button == Button3) + bgPix = p; + else + fgPix = p; + ShowColors(); XDestroyImage(im); } } drawing = 0; break; case T_ERASER: - DrawDab(x, y, 4, bg); + Snapshot(); + DrawDab(x, y, 4, bgPix); + dirty = 1; break; case T_BRUSH: + Snapshot(); DrawDab(x, y, 3, color); + dirty = 1; break; case T_PENCIL: + Snapshot(); SetFg(color); XDrawPoint(dpy, pix, gc, x, y); + dirty = 1; break; default: - XCopyArea(dpy, pix, snap, gc, 0, 0, imgW, imgH, 0, 0); + Snapshot(); break; } break; case MotionNotify: x = ev->xmotion.x; y = ev->xmotion.y; - curx = x; cury = y; StatusPos(x, y); if (!drawing) return; - color = (ev->xmotion.state & Button3Mask) ? bg : fg; + if (x < 0) x = 0; + if (y < 0) y = 0; + if (x >= imgW) x = imgW - 1; + if (y >= imgH) y = imgH - 1; + color = (ev->xmotion.state & Button3Mask) ? bgPix : fgPix; switch (tool) { case T_PENCIL: DrawLineTo(lx, ly, x, y, color); + dirty = 1; break; case T_BRUSH: DrawDab(x, y, 3, color); + dirty = 1; break; case T_ERASER: - DrawDab(x, y, 4, bg); + DrawDab(x, y, 4, bgPix); + dirty = 1; break; case T_LINE: case T_RECT: case T_ELLIPSE: case T_ROUNDRECT: RestoreSnap(); DrawShape(sx, sy, x, y, color); + dirty = 1; break; default: break; } lx = x; ly = y; - XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True); + Redraw(); break; case ButtonRelease: drawing = 0; - XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True); + Redraw(); break; default: break; @@ -546,10 +974,8 @@ static void ToolChanged(Widget w, XtPointer data, XtPointer call) { (void) call; - if (XmToggleButtonGetState(w)) { + if (XmToggleButtonGetState(w)) tool = (int) (long) data; - fprintf(stderr, "dtpaint: tool %s\n", toolNames[tool]); - } } static void @@ -561,16 +987,18 @@ SwatchPress(Widget w, XtPointer data, XEvent *ev, Boolean *keep) if (ev->type != ButtonPress) return; if (ev->xbutton.button == Button1) - fg = idx; + fgPix = palette[idx].pixel; else if (ev->xbutton.button == Button3) - bg = idx; + bgPix = palette[idx].pixel; + ShowColors(); } static void SwatchActivate(Widget w, XtPointer data, XtPointer call) { (void) w; (void) call; - fg = (int) (long) data; + fgPix = palette[(int) (long) data].pixel; + ShowColors(); } /* ------------------------------------------------------------------ menus */ @@ -578,51 +1006,175 @@ SwatchActivate(Widget w, XtPointer data, XtPointer call) enum { C_NEW, C_OPEN, C_SAVE, C_SAVEAS, C_EXIT, C_UNDO, C_CLEAR, C_INVERT, C_FLIP, - C_TOOLS, C_COLORS, C_STATUS, C_ABOUT + C_TOOLS, C_COLORS, C_STATUS, C_HELP, C_ABOUT }; +enum { A_NEW, A_OPEN, A_EXIT }; + static void FileDialog(int save); +static void PerformAction(int action); + +/* OK on a message dialog: destroy the whole dialog shell. */ +static void +DestroyDialog(Widget w, XtPointer data, XtPointer call) +{ + Widget sh = w; + + (void) data; (void) call; + while (sh != NULL && !XtIsShell(sh)) + sh = XtParent(sh); + if (sh != NULL) + XtDestroyWidget(sh); +} + +static void +ShowError(int msgid, const char *path) +{ + char buf[700]; + Widget d; + XmString s; + + snprintf(buf, sizeof buf, PaintMsg(msgid, "%s"), path); + d = XmCreateErrorDialog(top, "error", NULL, 0); + s = XmStringCreateLocalized(buf); + XtVaSetValues(d, XmNmessageString, s, NULL); + XmStringFree(s); + XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_CANCEL_BUTTON)); + XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_HELP_BUTTON)); + XtAddCallback(d, XmNokCallback, DestroyDialog, NULL); + XtManageChild(d); +} + +static void +ShowInfo(int bodyId, const char *bodyFb, int titleId, const char *titleFb) +{ + Widget d = XmCreateInformationDialog(top, "info", NULL, 0); + XmString s = Msg(bodyId, bodyFb); + + XtVaSetValues(d, XmNmessageString, s, NULL); + XmStringFree(s); + s = Msg(titleId, titleFb); + XtVaSetValues(d, XmNdialogTitle, s, NULL); + XmStringFree(s); + XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_CANCEL_BUTTON)); + XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_HELP_BUTTON)); + XtAddCallback(d, XmNokCallback, DestroyDialog, NULL); + XtManageChild(d); +} + +/* Ask before throwing away unsaved work (New / Open / Exit). */ +static void +DiscardAnswer(Widget w, XtPointer data, XtPointer call) +{ + XmAnyCallbackStruct *c = (XmAnyCallbackStruct *) call; + + (void) data; + if (c->reason == XmCR_OK) + PerformAction(pendingAction); + DestroyDialog(w, NULL, NULL); +} + +static void +MaybeDiscard(int action) +{ + Widget d; + XmString s; + char buf[700]; + const char *name = curPath[0] ? strrchr(curPath, '/') : NULL; + + if (!dirty) { + PerformAction(action); + return; + } + pendingAction = action; + name = (name != NULL) ? name + 1 : (curPath[0] ? curPath : PaintMsg(24, "untitled")); + snprintf(buf, sizeof buf, PaintMsg(28, "Save changes to \"%s\"?"), name); + d = XmCreateQuestionDialog(top, "discard", NULL, 0); + s = XmStringCreateLocalized(buf); + XtVaSetValues(d, XmNmessageString, s, NULL); + XmStringFree(s); + s = Msg(29, "Discard"); + XtVaSetValues(d, XmNokLabelString, s, NULL); + XmStringFree(s); + s = Msg(30, "Keep"); + XtVaSetValues(d, XmNcancelLabelString, s, NULL); + XmStringFree(s); + XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_HELP_BUTTON)); + XtAddCallback(d, XmNokCallback, DiscardAnswer, NULL); + XtAddCallback(d, XmNcancelCallback, DiscardAnswer, NULL); + XtManageChild(d); +} + +static void +DoSave(const char *path) +{ + if (SaveBMP(path) == 0) { + snprintf(curPath, sizeof curPath, "%s", path); + dirty = 0; + SetTitle(); + } else { + ShowError(27, path); + } +} + +static void +PerformAction(int action) +{ + switch (action) { + case A_NEW: + NewImage(); + break; + case A_OPEN: + FileDialog(0); + break; + case A_EXIT: + exit(0); + break; + } +} static void Command(Widget w, XtPointer data, XtPointer call) { - char path[512]; - (void) w; (void) call; switch ((int) (long) data) { case C_NEW: - NewImage(); + MaybeDiscard(A_NEW); break; case C_OPEN: - FileDialog(0); + MaybeDiscard(A_OPEN); break; case C_SAVE: - snprintf(path, sizeof path, "%s/dtpaint.bmp", - getenv("HOME") ? getenv("HOME") : "."); - if (SaveBMP(path) == 0) - fprintf(stderr, "dtpaint: saved %s\n", path); + if (curPath[0]) + DoSave(curPath); else - fprintf(stderr, "dtpaint: cannot save %s\n", path); + FileDialog(1); break; case C_SAVEAS: FileDialog(1); break; case C_EXIT: - exit(0); + MaybeDiscard(A_EXIT); + break; case C_UNDO: RestoreSnap(); - XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True); + dirty = 1; + Redraw(); break; case C_CLEAR: - NewImage(); + ClearImage(); break; case C_INVERT: + Snapshot(); InvertImage(); - XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True); + dirty = 1; + Redraw(); break; case C_FLIP: + Snapshot(); FlipHorizontal(); - XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True); + dirty = 1; + Redraw(); break; case C_TOOLS: toolsShown = !toolsShown; @@ -644,18 +1196,16 @@ Command(Widget w, XtPointer data, XtPointer call) XtUnmanageChild(statusPos); } break; + case C_HELP: + ShowInfo(34, + "Pick a tool on the left and a colour below it, then draw on " + "the canvas. The left button draws with the foreground " + "colour, the right button with the background colour. " + "Ctrl+Z undoes the last step.", + 33, "Help Topics"); + break; case C_ABOUT: - { - Widget d = XmCreateInformationDialog(top, "about", NULL, 0); - - XtVaSetValues(d, XmNmessageString, - XmStringCreateLocalized("dtpaint\nMCDE"), - XmNdialogTitle, - XmStringCreateLocalized(PaintMsg(22, "About dtpaint")), NULL); - XtUnmanageChild(XmMessageBoxGetChild(d, - XmDIALOG_CANCEL_BUTTON)); - XtManageChild(d); - } + ShowInfo(25, "dtpaint - MCDE", 22, "About dtpaint"); break; } } @@ -668,15 +1218,20 @@ FileOk(Widget w, XtPointer data, XtPointer call) if (XmStringGetLtoR(((XmFileSelectionBoxCallbackStruct *) call)->value, XmSTRING_DEFAULT_CHARSET, &path) && path != NULL) { - if (save ? (SaveBMP(path) == 0) : (OpenBMP(path) == 0)) - fprintf(stderr, "dtpaint: %s %s\n", - save ? "saved" : "opened", path); - else - fprintf(stderr, "dtpaint: cannot %s %s\n", - save ? "save" : "open", path); + if (save) { + DoSave(path); + } else { + if (OpenBMP(path) == 0) { + snprintf(curPath, sizeof curPath, "%s", path); + dirty = 0; + Snapshot(); + SetTitle(); + Redraw(); + } else { + ShowError(26, path); + } + } XtFree(path); - if (!save) - XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True); } XtDestroyWidget(XtParent(w)); } @@ -692,55 +1247,63 @@ static void FileDialog(int save) { Widget d = XmCreateFileSelectionDialog(top, "file", NULL, 0); + XmString s; XtAddCallback(d, XmNokCallback, FileOk, (XtPointer) (long) save); XtAddCallback(d, XmNcancelCallback, FileCancel, NULL); XtVaSetValues(d, XmNfileTypeMask, XmFILE_REGULAR, NULL); + s = Msg(save ? 32 : 31, save ? "Save As" : "Open"); + XtVaSetValues(d, XmNdialogTitle, s, NULL); + XmStringFree(s); + XtUnmanageChild(XmFileSelectionBoxGetChild(d, XmDIALOG_HELP_BUTTON)); XtManageChild(d); } - static Widget AddMenu(Widget bar, const char *name) { Widget shell = XmCreatePulldownMenu(bar, (char *) name, NULL, 0); - XtVaCreateManagedWidget(name, xmCascadeButtonWidgetClass, bar, + XtVaCreateManagedWidget((char *) name, xmCascadeButtonWidgetClass, bar, XmNsubMenuId, shell, NULL); return shell; } static void -AddItem(Widget shell, const char *label, const char *accel, int cmd, int sep) +AddItem(Widget shell, const char *label, const char *accel, + const char *accelText, int cmd, int sep, int sensitive) { + Widget b; + if (sep) { XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, shell, NULL); return; } - { - Widget b = XtVaCreateManagedWidget(label, xmPushButtonWidgetClass, - shell, NULL); + b = XtVaCreateManagedWidget((char *) label, xmPushButtonWidgetClass, shell, + XmNsensitive, sensitive, NULL); + XtAddCallback(b, XmNactivateCallback, Command, (XtPointer) (long) cmd); + if (accel != NULL) + XtVaSetValues(b, XmNaccelerator, accel, NULL); + if (accelText != NULL) { + XmString s = XmStringCreateLocalized((String) accelText); - XtAddCallback(b, XmNactivateCallback, Command, - (XtPointer) (long) cmd); - if (accel != NULL) - XtVaSetValues(b, XmNaccelerator, accel, NULL); + XtVaSetValues(b, XmNacceleratorText, s, NULL); + XmStringFree(s); } } /* ------------------------------------------------------------------ icons */ static void -Glyph(Display *d, Pixmap p, int t) +Glyph(Display *d, Pixmap p, int t, unsigned long bg) { XGCValues gv; GC k; - gv.foreground = BlackPixel(d, 0); + gv.foreground = bg; k = XCreateGC(d, p, GCForeground, &gv); - XSetForeground(d, k, WhitePixel(d, 0)); XFillRectangle(d, p, k, 0, 0, 16, 16); - XSetForeground(d, k, BlackPixel(d, 0)); + XSetForeground(d, k, BlackPixelOfScreen(scr)); switch (t) { case T_FREESEL: XDrawLine(d, p, k, 3, 4, 8, 2); @@ -821,13 +1384,13 @@ main(int argc, char **argv) { /* before the first PaintMsg(): the catalog is opened for the locale */ (void) setlocale(LC_ALL, ""); - /* the compound strings of the menus carry UTF-8 (ü in "Menü") */ XtSetLanguageProc(NULL, NULL, NULL); XtAppContext app; Widget menubarW; XGCValues gv; char hex[8]; int i, selftest = 0; + unsigned long toolbg; for (i = 1; i < argc; i++) if (strcmp(argv[i], "-selftest") == 0) @@ -835,45 +1398,82 @@ main(int argc, char **argv) top = XtVaAppInitialize(&app, "Dtpaint", NULL, 0, &argc, argv, NULL, NULL); dpy = XtDisplay(top); + InitColor(); for (i = 0; i < PAL_N; i++) { XColor c; snprintf(hex, sizeof hex, "#%s", palHex[i]); - if (!XParseColor(dpy, DefaultColormap(dpy, 0), hex, &c) || - !XAllocColor(dpy, DefaultColormap(dpy, 0), &c)) { - c.pixel = (i == 0) ? BlackPixel(dpy, 0) : WhitePixel(dpy, 0); + if (!XParseColor(dpy, cmap, hex, &c) || + !XAllocColor(dpy, cmap, &c)) { + c.pixel = (i == 0) ? BlackPixelOfScreen(scr) : WhitePixelOfScreen(scr); } palette[i] = c; } - pix = XCreatePixmap(dpy, DefaultRootWindow(dpy), imgW, imgH, - DefaultDepth(dpy, 0)); - snap = XCreatePixmap(dpy, DefaultRootWindow(dpy), imgW, imgH, - DefaultDepth(dpy, 0)); - gv.foreground = palette[0].pixel; - gv.background = palette[14].pixel; + fgPix = palette[0].pixel; + bgPix = palette[14].pixel; + pix = XCreatePixmap(dpy, RootWindowOfScreen(scr), imgW, imgH, depth); + snap = XCreatePixmap(dpy, RootWindowOfScreen(scr), imgW, imgH, depth); + if (pix == None || snap == None) { + fprintf(stderr, "dtpaint: cannot create the image pixmap\n"); + return 1; + } + gv.foreground = fgPix; + gv.background = bgPix; gc = XCreateGC(dpy, pix, GCForeground | GCBackground, &gv); if (selftest) { - XSetForeground(dpy, gc, palette[14].pixel); - XFillRectangle(dpy, pix, gc, 0, 0, imgW, imgH); - DrawLineTo(10, 10, 40, 10, 0); - FloodFill(20, 40, 16); - { - XImage *im = XGetImage(dpy, pix, 0, 0, imgW, imgH, - AllPlanes, ZPixmap); - unsigned long line = XGetPixel(im, 20, 10); - unsigned long area = XGetPixel(im, 20, 40); + char tmp[128]; + XImage *a, *b; + int x, y, ok = 1; - XDestroyImage(im); - if (line == palette[0].pixel && - area == palette[16].pixel) { - printf("dtpaint selftest: PASS\n"); - return 0; - } - printf("dtpaint selftest: FAIL\n"); + XSetForeground(dpy, gc, bgPix); + XFillRectangle(dpy, pix, gc, 0, 0, imgW, imgH); + DrawLineTo(10, 10, 40, 10, fgPix); + FloodFill(20, 40, palette[16].pixel); + a = XGetImage(dpy, pix, 0, 0, imgW, imgH, AllPlanes, ZPixmap); + if (a == NULL) { + printf("dtpaint selftest: FAIL image\n"); return 1; } + if (XGetPixel(a, 20, 10) != fgPix || + XGetPixel(a, 20, 40) != palette[16].pixel) + ok = 0; + /* the D2 criterion: a BMP round trip without loss */ + snprintf(tmp, sizeof tmp, "/tmp/dtpaint-selftest-%d.bmp", (int) getpid()); + if (SaveBMP(tmp) != 0 || OpenBMP(tmp) != 0) { + ok = 0; + } else { + b = XGetImage(dpy, pix, 0, 0, imgW, imgH, AllPlanes, ZPixmap); + if (b == NULL) { + ok = 0; + } else { + for (y = 0; y < imgH && ok; y += 37) + for (x = 0; x < imgW; x += 41) + if (XGetPixel(a, x, y) != XGetPixel(b, x, y)) + ok = 0; + XDestroyImage(b); + } + } + unlink(tmp); + /* invert and flip twice return to the original */ + InvertImage(); + InvertImage(); + FlipHorizontal(); + FlipHorizontal(); + b = XGetImage(dpy, pix, 0, 0, imgW, imgH, AllPlanes, ZPixmap); + if (b == NULL) { + ok = 0; + } else { + for (y = 0; y < imgH && ok; y += 37) + for (x = 0; x < imgW; x += 41) + if (XGetPixel(a, x, y) != XGetPixel(b, x, y)) + ok = 0; + XDestroyImage(b); + } + XDestroyImage(a); + printf("dtpaint selftest: %s\n", ok ? "PASS" : "FAIL"); + return ok ? 0 : 1; } mainw = XmCreateMainWindow(top, "main", NULL, 0); @@ -884,82 +1484,107 @@ main(int argc, char **argv) { Widget m = AddMenu(menubarW, PaintMsg(1, "File")); - AddItem(m, PaintMsg(7, "New"), "CtrlN", C_NEW, 0); - AddItem(m, PaintMsg(8, "Open..."), "CtrlO", C_OPEN, 0); - AddItem(m, PaintMsg(9, "Save"), "CtrlS", C_SAVE, 0); - AddItem(m, PaintMsg(10, "Save As..."), NULL, C_SAVEAS, 0); - AddItem(m, NULL, NULL, 0, 1); - AddItem(m, PaintMsg(11, "Exit"), "AltF4", C_EXIT, 0); + AddItem(m, PaintMsg(7, "New"), "CtrlN", "Ctrl+N", C_NEW, 0, True); + AddItem(m, PaintMsg(8, "Open..."), "CtrlO", "Ctrl+O", C_OPEN, 0, True); + AddItem(m, PaintMsg(9, "Save"), "CtrlS", "Ctrl+S", C_SAVE, 0, True); + AddItem(m, PaintMsg(10, "Save As..."), NULL, NULL, C_SAVEAS, 0, True); + AddItem(m, NULL, NULL, NULL, 0, 1, True); + AddItem(m, PaintMsg(11, "Exit"), "AltF4", "Alt+F4", C_EXIT, 0, True); m = AddMenu(menubarW, PaintMsg(2, "Edit")); - AddItem(m, PaintMsg(12, "Undo"), "CtrlZ", C_UNDO, 0); - AddItem(m, NULL, NULL, 0, 1); - AddItem(m, PaintMsg(13, "Clear Selection"), NULL, C_CLEAR, 0); + AddItem(m, PaintMsg(12, "Undo"), "CtrlZ", "Ctrl+Z", C_UNDO, 0, True); + AddItem(m, NULL, NULL, NULL, 0, 1, True); + /* needs the selection tool (D3); insensitive until it exists */ + AddItem(m, PaintMsg(13, "Clear Selection"), NULL, NULL, C_CLEAR, 0, False); m = AddMenu(menubarW, PaintMsg(3, "View")); - AddItem(m, PaintMsg(14, "Tool Box"), "CtrlT", C_TOOLS, 0); - AddItem(m, PaintMsg(15, "Color Box"), "CtrlL", C_COLORS, 0); - AddItem(m, PaintMsg(16, "Status Bar"), NULL, C_STATUS, 0); + AddItem(m, PaintMsg(14, "Tool Box"), "CtrlT", "Ctrl+T", C_TOOLS, 0, True); + AddItem(m, PaintMsg(15, "Color Box"), "CtrlL", "Ctrl+L", C_COLORS, 0, True); + AddItem(m, PaintMsg(16, "Status Bar"), NULL, NULL, C_STATUS, 0, True); m = AddMenu(menubarW, PaintMsg(4, "Image")); - AddItem(m, PaintMsg(17, "Flip Horizontal"), NULL, C_FLIP, 0); - AddItem(m, PaintMsg(18, "Invert Colors"), "CtrlI", C_INVERT, 0); - AddItem(m, PaintMsg(19, "Clear Image"), "CtrlShiftN", C_CLEAR, 0); + AddItem(m, PaintMsg(17, "Flip Horizontal"), NULL, NULL, C_FLIP, 0, True); + AddItem(m, PaintMsg(18, "Invert Colors"), "CtrlI", "Ctrl+I", C_INVERT, 0, True); + AddItem(m, PaintMsg(19, "Clear Image"), "Ctrl ShiftN", "Ctrl+Shift+N", C_CLEAR, 0, True); m = AddMenu(menubarW, PaintMsg(5, "Colors")); - AddItem(m, PaintMsg(20, "Edit Colors..."), NULL, C_ABOUT, 0); + /* needs the colour editor (D4); insensitive until it exists */ + AddItem(m, PaintMsg(20, "Edit Colors..."), NULL, NULL, C_ABOUT, 0, False); m = AddMenu(menubarW, PaintMsg(6, "Help")); - AddItem(m, PaintMsg(21, "About dtpaint..."), NULL, C_ABOUT, 0); + AddItem(m, PaintMsg(33, "Help Topics"), "CtrlF1", "Ctrl+F1", C_HELP, 0, True); + AddItem(m, NULL, NULL, NULL, 0, 1, True); + AddItem(m, PaintMsg(21, "About dtpaint..."), NULL, NULL, C_ABOUT, 0, True); } form = XtVaCreateManagedWidget("form", xmFormWidgetClass, mainw, - XmNfractionBase, 100, XmNwidth, 730, XmNheight, 540, NULL); + XmNfractionBase, 100, XmNwidth, 920, XmNheight, 560, NULL); - /* toolbox on the left, two columns of eight */ - toolbox = XtVaCreateManagedWidget("tools", xmRowColumnWidgetClass, form, - XmNleftAttachment, XmATTACH_FORM, XmNtopAttachment, XmATTACH_FORM, - XmNorientation, XmVERTICAL, XmNnumColumns, 2, - XmNpacking, XmPACK_COLUMN, XmNspacing, 1, - XmNmarginWidth, 2, XmNmarginHeight, 2, NULL); + /* a left column holds the toolbox and, below it, the colour box; the + * canvas starts to the right of the wider of the two so they never + * overlap the image */ + { + Widget left = XtVaCreateManagedWidget("left", xmFormWidgetClass, + form, XmNleftAttachment, XmATTACH_FORM, + XmNtopAttachment, XmATTACH_FORM, + XmNmarginWidth, 0, XmNmarginHeight, 0, NULL); + + toolbox = XtVaCreateManagedWidget("tools", xmRowColumnWidgetClass, left, + XmNleftAttachment, XmATTACH_FORM, XmNtopAttachment, XmATTACH_FORM, + XmNorientation, XmVERTICAL, XmNnumColumns, 2, + XmNpacking, XmPACK_COLUMN, XmNspacing, 1, + XmNmarginWidth, 2, XmNmarginHeight, 2, + XmNradioBehavior, True, XmNradioAlwaysOne, True, NULL); + leftcol = left; + } + XtVaGetValues(toolbox, XmNbackground, &toolbg, NULL); for (i = 0; i < T_NONE; i++) { Widget b; - Pixmap p = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, - DefaultDepth(dpy, 0)); + Pixmap p = XCreatePixmap(dpy, RootWindowOfScreen(scr), 16, 16, depth); - Glyph(dpy, p, i); + Glyph(dpy, p, i, toolbg); b = XtVaCreateManagedWidget(toolNames[i], xmToggleButtonWidgetClass, toolbox, XmNlabelType, XmPIXMAP, XmNlabelPixmap, p, - XmNindicatorType, XmN_OF_MANY, XmNshadowThickness, 1, + XmNindicatorOn, False, XmNshadowThickness, 1, XmNmarginWidth, 1, XmNmarginHeight, 1, - XmNwidth, 24, XmNheight, 24, NULL); + XmNwidth, 24, XmNheight, 24, XmNrecomputeSize, False, NULL); XtAddCallback(b, XmNvalueChangedCallback, ToolChanged, (XtPointer) (long) i); if (i == T_PENCIL) XmToggleButtonSetState(b, True, False); } - /* colour box under the toolbox: two rows of fourteen */ - colorbox = XtVaCreateManagedWidget("colors", xmRowColumnWidgetClass, form, + /* colour box under the toolbox: the fg/bg indicator at the left, then + * two rows of fourteen 16x16 swatches */ + colorbox = XtVaCreateManagedWidget("colors", xmRowColumnWidgetClass, leftcol, XmNleftAttachment, XmATTACH_FORM, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, toolbox, - XmNorientation, XmVERTICAL, XmNspacing, 1, + XmNtopOffset, 4, + XmNorientation, XmHORIZONTAL, XmNspacing, 4, XmNmarginWidth, 2, XmNmarginHeight, 2, NULL); + fgbg = XtVaCreateManagedWidget("fgbg", xmDrawingAreaWidgetClass, + colorbox, XmNwidth, 34, XmNheight, 34, XmNrecomputeSize, False, + XmNmarginWidth, 0, XmNmarginHeight, 0, NULL); + XtAddCallback(fgbg, XmNexposeCallback, FgBgExpose, NULL); { + Widget rows = XtVaCreateManagedWidget("rows", xmRowColumnWidgetClass, + colorbox, XmNorientation, XmVERTICAL, XmNspacing, 1, NULL); int r, c; for (r = 0; r < PAL_ROWS; r++) { Widget row = XtVaCreateManagedWidget("row", - xmRowColumnWidgetClass, colorbox, XmNorientation, + xmRowColumnWidgetClass, rows, XmNorientation, XmHORIZONTAL, XmNspacing, 1, NULL); for (c = 0; c < PAL_COLS; c++) { Widget b; int idx = r * PAL_COLS + c; + XmString empty = XmStringCreateLocalized(""); b = XtVaCreateManagedWidget("swatch", xmPushButtonWidgetClass, row, XmNbackground, palette[idx].pixel, XmNwidth, 16, XmNheight, 16, + XmNrecomputeSize, False, XmNshadowThickness, 1, XmNmarginWidth, 0, XmNmarginHeight, 0, - XmNlabelString, - XmStringCreateLocalized(""), NULL); + XmNlabelString, empty, NULL); + XmStringFree(empty); XtAddCallback(b, XmNactivateCallback, SwatchActivate, (XtPointer) (long) idx); XtAddEventHandler(b, ButtonPressMask, False, @@ -968,28 +1593,42 @@ main(int argc, char **argv) } } - /* status line at the bottom, canvas between toolbox and right edge */ + /* status line at the bottom; the canvas scrolls between toolbox and the + * right edge so a large image is reachable, not clipped */ statusHelp = XtVaCreateManagedWidget("help", xmLabelWidgetClass, form, XmNbottomAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM, XmNalignment, XmALIGNMENT_BEGINNING, NULL); - XtVaSetValues(statusHelp, XmNlabelString, XmStringCreateLocalized( - PaintMsg(23, "For Help, click Help Topics on the Help Menu.")), NULL); + { + XmString s = Msg(23, "For Help, click Help Topics on the Help Menu."); + + XtVaSetValues(statusHelp, XmNlabelString, s, NULL); + XmStringFree(s); + } statusPos = XtVaCreateManagedWidget("pos", xmLabelWidgetClass, form, XmNbottomAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, - XmNalignment, XmALIGNMENT_END, NULL); - XtVaSetValues(statusPos, XmNlabelString, - XmStringCreateLocalized("0,0"), NULL); + XmNalignment, XmALIGNMENT_END, XmNrecomputeSize, False, + XmNwidth, 90, NULL); + { + XmString s = XmStringCreateLocalized("0,0"); - canvas = XtVaCreateManagedWidget("canvas", xmDrawingAreaWidgetClass, + XtVaSetValues(statusPos, XmNlabelString, s, NULL); + XmStringFree(s); + } + + scroll = XtVaCreateManagedWidget("scroll", xmScrolledWindowWidgetClass, form, XmNtopAttachment, XmATTACH_FORM, - XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, toolbox, + XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, leftcol, XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, statusHelp, - XmNwidth, imgW, XmNheight, imgH, NULL); + XmNrightAttachment, XmATTACH_FORM, + XmNscrollingPolicy, XmAUTOMATIC, + XmNscrollBarDisplayPolicy, XmAS_NEEDED, NULL); + canvas = XtVaCreateManagedWidget("canvas", xmDrawingAreaWidgetClass, + scroll, XmNwidth, imgW, XmNheight, imgH, NULL); XtAddCallback(canvas, XmNexposeCallback, CanvasExpose, NULL); XtAddEventHandler(canvas, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ButtonMotionMask, False, CanvasInput, NULL); - XtVaSetValues(top, XmNtitle, "untitled - dtpaint", NULL); + SetTitle(); XtRealizeWidget(top); NewImage(); XtAppMainLoop(app); diff --git a/programs/dtpaint/dtpaint.msg b/programs/dtpaint/dtpaint.msg index d79dacbc..8f1f233f 100644 --- a/programs/dtpaint/dtpaint.msg +++ b/programs/dtpaint/dtpaint.msg @@ -25,3 +25,14 @@ $set 1 21 About dtpaint... 22 About dtpaint 23 For Help, click Help Topics on the Help Menu. +24 untitled +25 dtpaint - MCDE +26 Cannot open %s. +27 Cannot save %s. +28 Save changes to "%s"? +29 Discard +30 Keep +31 Open +32 Save As +33 Help Topics +34 Pick a tool on the left and a colour below it, then draw on the canvas. The left button draws with the foreground colour, the right button with the background colour. Ctrl+Z undoes the last step. diff --git a/programs/localized/de_DE.UTF-8/msg/dtpaint.msg b/programs/localized/de_DE.UTF-8/msg/dtpaint.msg index 9d35c5fc..87f104f5 100644 --- a/programs/localized/de_DE.UTF-8/msg/dtpaint.msg +++ b/programs/localized/de_DE.UTF-8/msg/dtpaint.msg @@ -1,4 +1,5 @@ -$ MCDE dtpaint message catalog, German. +$ MCDE dtpaint message catalog, German. The core font has no UTF-8 rendition, +$ so umlauts are transliterated (ae/oe/ue), as in the other MCDE catalogs. $ $set 1 @@ -9,11 +10,11 @@ $set 1 5 Farben 6 Hilfe 7 Neu -8 Öffnen... +8 Oeffnen... 9 Speichern 10 Speichern unter... 11 Beenden -12 Rückgängig +12 Rueckgaengig 13 Auswahl aufheben 14 Werkzeuge 15 Farbpalette @@ -22,6 +23,17 @@ $set 1 18 Farben umkehren 19 Bild leeren 20 Farben bearbeiten... -21 Über dtpaint... -22 Über dtpaint +21 Ueber dtpaint... +22 Ueber dtpaint 23 Hilfe: Hilfe-Themen im Hilfe-Menue. +24 unbenannt +25 dtpaint - MCDE +26 %s kann nicht geoeffnet werden. +27 %s kann nicht gespeichert werden. +28 Aenderungen an "%s" speichern? +29 Verwerfen +30 Behalten +31 Oeffnen +32 Speichern unter +33 Hilfe-Themen +34 Waehlen Sie links ein Werkzeug und darunter eine Farbe, dann zeichnen Sie auf der Flaeche. Die linke Taste zeichnet mit der Vordergrundfarbe, die rechte mit der Hintergrundfarbe. Strg+Z macht den letzten Schritt rueckgaengig.