MCDE dtpaint: zoom 1x/2x/4x/8x (D3)

The magnifier cycles the factor (left up, right down); View > Normal Size
(Ctrl+PgUp) and Large Size (Ctrl+PgDn) set 1x and 4x.  The canvas grows to
image*zoom inside the scrolled window; Expose samples only the exposed
rectangle into an XImage (nearest pixel), and pointer events divide by the
factor, so drawing and the status line stay in image coordinates.
Measured under Xvfb: 1x 640x480 -> 2x 1280x960 -> 4x 2560x1920, a line
drawn at 4x appears at the same image coordinates at 1x.
This commit is contained in:
lotherk committed 2026-11-22 06:51:21 +00:00
1 parent fd66cdb200
commit 6fb09e6d9d
3 files changed
+106 -10

No files matched your search

+102 -10
View File
@@ -117,6 +117,7 @@ static Widget statusHelp, statusPos, fgbg;
static Pixmap pix, snap;
static GC gc;
static int imgW = 640, imgH = 480;
static int zoom = 1; /* 1, 2, 4 or 8 */
static int tool = T_PENCIL;
static unsigned long fgPix, bgPix;
static XColor palette[PAL_N];
@@ -439,7 +440,8 @@ OpenBMP(const char *path)
pix = np;
snap = ns;
if (canvas != NULL)
XtVaSetValues(canvas, XmNwidth, imgW, XmNheight, imgH, NULL);
XtVaSetValues(canvas, XmNwidth, imgW * zoom,
XmNheight, imgH * zoom, NULL);
}
XSetForeground(dpy, gc, bgPix);
XFillRectangle(dpy, pix, gc, 0, 0, imgW, imgH);
@@ -768,11 +770,32 @@ FlipHorizontal(void)
XFreePixmap(dpy, tmp);
}
static void
SetCanvasSize(void)
{
if (canvas != NULL)
XtVaSetValues(canvas, XmNwidth, imgW * zoom,
XmNheight, imgH * zoom, NULL);
}
static void
Redraw(void)
{
if (canvas != NULL && XtWindow(canvas) != None)
XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True);
XClearArea(dpy, XtWindow(canvas), 0, 0,
imgW * zoom, imgH * zoom, True);
}
static void
SetZoom(int z)
{
if (z < 1)
z = 1;
if (z > 8)
z = 8;
zoom = z;
SetCanvasSize();
Redraw();
}
static void
@@ -847,15 +870,69 @@ ShowColors(void)
XClearArea(dpy, XtWindow(fgbg), 0, 0, 34, 34, True);
}
/* draw the exposed part of the image at the current zoom (nearest pixel) */
static void
ShowZoomed(int rx, int ry, int rw, int rh)
{
XImage *src, *dst;
int x, y;
if (rw <= 0 || rh <= 0)
return;
if (zoom == 1) {
XCopyArea(dpy, pix, XtWindow(canvas), gc, rx, ry, (unsigned) rw,
(unsigned) rh, rx, ry);
return;
}
if ((src = XGetImage(dpy, pix, 0, 0, imgW, imgH, AllPlanes, ZPixmap)) == NULL)
return;
dst = XCreateImage(dpy, vis, depth, ZPixmap, 0, NULL,
(unsigned) rw, (unsigned) rh, 32, 0);
if (dst == NULL) {
XDestroyImage(src);
return;
}
dst->data = calloc(1, (size_t) dst->bytes_per_line * rh);
if (dst->data == NULL) {
XDestroyImage(dst);
XDestroyImage(src);
return;
}
for (y = 0; y < rh; y++)
for (x = 0; x < rw; x++) {
int sx = (rx + x) / zoom;
int sy = (ry + y) / zoom;
if (sx >= imgW) sx = imgW - 1;
if (sy >= imgH) sy = imgH - 1;
XPutPixel(dst, x, y, XGetPixel(src, sx, sy));
}
XPutImage(dpy, XtWindow(canvas), gc, dst, 0, 0, rx, ry,
(unsigned) rw, (unsigned) rh);
XDestroyImage(src);
XDestroyImage(dst);
}
static void
CanvasExpose(Widget w, XtPointer cd, XtPointer call)
{
XmDrawingAreaCallbackStruct *c = (XmDrawingAreaCallbackStruct *) call;
XRectangle r;
(void) cd;
(void) w; (void) cd;
if (c->reason != XmCR_EXPOSE)
return;
XCopyArea(dpy, pix, XtWindow(w), gc, 0, 0, imgW, imgH, 0, 0);
r.x = c->event->xexpose.x;
r.y = c->event->xexpose.y;
r.width = (unsigned short) c->event->xexpose.width;
r.height = (unsigned short) c->event->xexpose.height;
if (r.x >= imgW * zoom || r.y >= imgH * zoom)
return;
if (r.x + r.width > imgW * zoom)
r.width = (unsigned short) (imgW * zoom - r.x);
if (r.y + r.height > imgH * zoom)
r.height = (unsigned short) (imgH * zoom - r.y);
ShowZoomed(r.x, r.y, r.width, r.height);
}
static void
@@ -869,8 +946,8 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep)
case ButtonPress:
if (ev->xbutton.button != Button1 && ev->xbutton.button != Button3)
return;
x = ev->xbutton.x;
y = ev->xbutton.y;
x = ev->xbutton.x / zoom;
y = ev->xbutton.y / zoom;
/* 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)
@@ -921,14 +998,20 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep)
XDrawPoint(dpy, pix, gc, x, y);
dirty = 1;
break;
case T_ZOOM:
SetZoom(ev->xbutton.button == Button3 ?
(zoom > 1 ? zoom / 2 : 8) :
(zoom < 8 ? zoom * 2 : 1));
drawing = 0;
break;
default:
Snapshot();
break;
}
break;
case MotionNotify:
x = ev->xmotion.x;
y = ev->xmotion.y;
x = ev->xmotion.x / zoom;
y = ev->xmotion.y / zoom;
StatusPos(x, y);
if (!drawing)
return;
@@ -1006,7 +1089,7 @@ 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_HELP, C_ABOUT
C_TOOLS, C_COLORS, C_STATUS, C_HELP, C_ABOUT, C_ZNORMAL, C_ZLARGE
};
enum { A_NEW, A_OPEN, A_EXIT };
@@ -1199,6 +1282,12 @@ Command(Widget w, XtPointer data, XtPointer call)
XtUnmanageChild(statusPos);
}
break;
case C_ZNORMAL:
SetZoom(1);
break;
case C_ZLARGE:
SetZoom(4);
break;
case C_HELP:
ShowInfo(34,
"Pick a tool on the left and a colour below it, then draw on "
@@ -1502,6 +1591,9 @@ main(int argc, char **argv)
AddItem(m, PaintMsg(14, "Tool Box"), "Ctrl<Key>T", "Ctrl+T", C_TOOLS, 0, True);
AddItem(m, PaintMsg(15, "Color Box"), "Ctrl<Key>L", "Ctrl+L", C_COLORS, 0, True);
AddItem(m, PaintMsg(16, "Status Bar"), NULL, NULL, C_STATUS, 0, True);
AddItem(m, NULL, NULL, NULL, 0, 1, True);
AddItem(m, PaintMsg(35, "Normal Size"), "Ctrl<Key>Prior", "Ctrl+PgUp", C_ZNORMAL, 0, True);
AddItem(m, PaintMsg(36, "Large Size"), "Ctrl<Key>Next", "Ctrl+PgDn", C_ZLARGE, 0, True);
m = AddMenu(menubarW, PaintMsg(4, "Image"));
AddItem(m, PaintMsg(17, "Flip Horizontal"), NULL, NULL, C_FLIP, 0, True);
AddItem(m, PaintMsg(18, "Invert Colors"), "Ctrl<Key>I", "Ctrl+I", C_INVERT, 0, True);
@@ -1626,7 +1718,7 @@ main(int argc, char **argv)
XmNscrollingPolicy, XmAUTOMATIC,
XmNscrollBarDisplayPolicy, XmAS_NEEDED, NULL);
canvas = XtVaCreateManagedWidget("canvas", xmDrawingAreaWidgetClass,
scroll, XmNwidth, imgW, XmNheight, imgH, NULL);
scroll, XmNwidth, imgW * zoom, XmNheight, imgH * zoom, NULL);
XtAddCallback(canvas, XmNexposeCallback, CanvasExpose, NULL);
XtAddEventHandler(canvas, ButtonPressMask | ButtonReleaseMask |
PointerMotionMask | ButtonMotionMask, False, CanvasInput, NULL);
+2
View File
@@ -36,3 +36,5 @@ $set 1
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.
35 Normal Size
36 Large Size
@@ -37,3 +37,5 @@ $set 1
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.
35 Normale Groesse
36 Grosse Darstellung