MCDE dtpaint: rectangular and free-form selection (D3)

Select drags a box, Free-Form Select traces a lasso; both draw an XOR
marquee (scaled with the zoom), keep the pixels, and can be dragged to move
them: the snapshot is the undo base, the selected pixels float in a pixmap,
and a 1-bit polygon mask clips clear/paste for the lasso.  Edit > Clear
Selection (Del) clears the selected pixels and deselects; Edit > Select All
(Ctrl+A) selects the whole image; the Clear Selection item is sensitive only
while a selection exists.  A non-selection tool's click deselects, as in the
reference.  Measured under Xvfb: box marquee, move of a scribble (outside
pixels stay), Delete clears it, lasso marquee around a dot and Delete clears
just that dot, Ctrl+A marks the image and enables the menu item.
This commit is contained in:
lotherk committed 2026-11-22 07:05:29 +00:00
1 parent ffbf8c7f2d
commit d224248651
3 files changed
+375 -6

No files matched your search

+373 -6
View File
@@ -132,6 +132,17 @@ static int polyActive, polyN, polyX[POLY_MAX], polyY[POLY_MAX];
static unsigned long polyColor;
static int polyLastX, polyLastY;
static Time polyLastTime;
#define SELPT_MAX 1024
static int selActive, selFree, selDefining, selMoving;
static int selBX, selBY, selBW, selBH; /* selection box, image coords */
static int selStartX, selStartY; /* press / last pointer */
static int selLastX = -1, selLastY = -1;
static int selDX, selDY; /* current move offset */
static Pixmap selMaskPix, selFloat;
static int selPts;
static int selPX[SELPT_MAX], selPY[SELPT_MAX];
static Widget clearSelItem;
static int toolsShown = 1, colorsShown = 1, statusShown = 1;
static char curPath[512];
static int dirty;
@@ -139,6 +150,9 @@ static int pendingAction;
static void DestroyDialog(Widget w, XtPointer data, XtPointer call);
static void Redraw(void);
static void UpdateSelUI(void);
static void DrawMarquee(void);
static void Deselect(void);
/* ------------------------------------------------------------------ colour */
@@ -881,6 +895,7 @@ SetTitle(void)
static void
NewImage(void)
{
Deselect();
XSetForeground(dpy, gc, bgPix);
XFillRectangle(dpy, pix, gc, 0, 0, imgW, imgH);
Snapshot();
@@ -893,6 +908,7 @@ NewImage(void)
static void
ClearImage(void)
{
Deselect();
Snapshot();
XSetForeground(dpy, gc, bgPix);
XFillRectangle(dpy, pix, gc, 0, 0, imgW, imgH);
@@ -1018,6 +1034,245 @@ ShowColors(void)
XClearArea(dpy, XtWindow(fgbg), 0, 0, 34, 34, True);
}
/* ------------------------------------------------------------- selection */
static void
UpdateSelUI(void)
{
if (clearSelItem != NULL)
XtSetSensitive(clearSelItem, selActive);
}
static void
FreeSelMask(void)
{
if (selMaskPix != None) {
XFreePixmap(dpy, selMaskPix);
selMaskPix = None;
}
if (selFloat != None) {
XFreePixmap(dpy, selFloat);
selFloat = None;
}
}
static void
Deselect(void)
{
selActive = 0;
selFree = 0;
selDefining = 0;
selMoving = 0;
FreeSelMask();
UpdateSelUI();
}
/* clear the selected pixels of the box at x,y (the mask is box-relative) */
static void
SelClearArea(int x, int y, int w, int h)
{
int cx = x, cy = y, cw = w, ch = h;
if (cx < 0) { cw += cx; cx = 0; }
if (cy < 0) { ch += cy; cy = 0; }
if (cx + cw > imgW) cw = imgW - cx;
if (cy + ch > imgH) ch = imgH - cy;
if (cw <= 0 || ch <= 0)
return;
XSetForeground(dpy, gc, bgPix);
if (selMaskPix != None) {
XSetClipMask(dpy, gc, selMaskPix);
XSetClipOrigin(dpy, gc, x, y);
}
XFillRectangle(dpy, pix, gc, cx, cy, (unsigned) cw, (unsigned) ch);
if (selMaskPix != None) {
XSetClipMask(dpy, gc, None);
XSetClipOrigin(dpy, gc, 0, 0);
}
}
/* paste the floating box at x,y, clipped by the mask when there is one */
static void
SelPaste(int x, int y)
{
if (selFloat == None)
return;
if (selMaskPix != None) {
XSetClipMask(dpy, gc, selMaskPix);
XSetClipOrigin(dpy, gc, x, y);
}
XCopyArea(dpy, selFloat, pix, gc, 0, 0, (unsigned) selBW,
(unsigned) selBH, x, y);
if (selMaskPix != None) {
XSetClipMask(dpy, gc, None);
XSetClipOrigin(dpy, gc, 0, 0);
}
}
static void
StartMove(int x, int y)
{
Snapshot();
selFloat = XCreatePixmap(dpy, RootWindowOfScreen(scr), (unsigned) selBW,
(unsigned) selBH, depth);
if (selFloat == None)
return;
XSetForeground(dpy, gc, bgPix);
XFillRectangle(dpy, selFloat, gc, 0, 0, (unsigned) selBW,
(unsigned) selBH);
if (selMaskPix != None) {
XSetClipMask(dpy, gc, selMaskPix);
XSetClipOrigin(dpy, gc, 0, 0);
}
XCopyArea(dpy, pix, selFloat, gc, selBX, selBY, (unsigned) selBW,
(unsigned) selBH, 0, 0);
if (selMaskPix != None) {
XSetClipMask(dpy, gc, None);
XSetClipOrigin(dpy, gc, 0, 0);
}
SelClearArea(selBX, selBY, selBW, selBH);
selStartX = x;
selStartY = y;
selDX = selDY = 0;
selMoving = 1;
}
static void
FinishMove(void)
{
selBX += selDX;
selBY += selDY;
selDX = selDY = 0;
selMoving = 0;
if (selFloat != None) {
XFreePixmap(dpy, selFloat);
selFloat = None;
}
}
static void
SelectAll(void)
{
Deselect();
selFree = 0;
selActive = 1;
selBX = 0;
selBY = 0;
selBW = imgW;
selBH = imgH;
UpdateSelUI();
Redraw();
}
static void
ClearSelection(void)
{
if (!selActive)
return;
Snapshot();
SelClearArea(selBX, selBY, selBW, selBH);
dirty = 1;
Deselect();
Redraw();
}
/* a 1-bit mask from the free-form path; points become box-relative */
static void
BuildSelMask(void)
{
XGCValues gv;
GC g1;
XPoint *pts;
int i, x0 = selPX[0], y0 = selPY[0], x1 = x0, y1 = y0;
for (i = 1; i < selPts; i++) {
if (selPX[i] < x0) x0 = selPX[i];
if (selPY[i] < y0) y0 = selPY[i];
if (selPX[i] > x1) x1 = selPX[i];
if (selPY[i] > y1) y1 = selPY[i];
}
selBX = x0;
selBY = y0;
selBW = x1 - x0 + 1;
selBH = y1 - y0 + 1;
if (selBW < 2 || selBH < 2)
return;
selMaskPix = XCreatePixmap(dpy, RootWindowOfScreen(scr),
(unsigned) selBW, (unsigned) selBH, 1);
if (selMaskPix == None)
return;
gv.foreground = 0;
gv.background = 0;
g1 = XCreateGC(dpy, selMaskPix, GCForeground | GCBackground, &gv);
XFillRectangle(dpy, selMaskPix, g1, 0, 0, (unsigned) selBW,
(unsigned) selBH);
XSetForeground(dpy, g1, 1);
pts = malloc(sizeof(XPoint) * (size_t) selPts);
if (pts != NULL) {
for (i = 0; i < selPts; i++) {
pts[i].x = (short) (selPX[i] - selBX);
pts[i].y = (short) (selPY[i] - selBY);
}
XFillPolygon(dpy, selMaskPix, g1, pts, selPts, Complex,
CoordModeOrigin);
free(pts);
}
XFreeGC(dpy, g1);
for (i = 0; i < selPts; i++) {
selPX[i] -= selBX;
selPY[i] -= selBY;
}
}
static void
DrawMarquee(void)
{
int x, y, w, h, i;
XPoint pts[SELPT_MAX + 1];
if (!selActive && !selDefining)
return;
XSetFunction(dpy, gc, GXxor);
XSetForeground(dpy, gc, WhitePixelOfScreen(scr));
XSetLineAttributes(dpy, gc, 1, LineOnOffDash, CapButt, JoinMiter);
if (selDefining && !selFree) {
x = selStartX < selLastX ? selStartX : selLastX;
y = selStartY < selLastY ? selStartY : selLastY;
w = abs(selLastX - selStartX);
h = abs(selLastY - selStartY);
XDrawRectangle(dpy, XtWindow(canvas), gc, x * zoom, y * zoom,
(unsigned) (w * zoom), (unsigned) (h * zoom));
} else if (selDefining) {
for (i = 0; i < selPts; i++) {
pts[i].x = (short) (selPX[i] * zoom);
pts[i].y = (short) (selPY[i] * zoom);
}
if (selPts > 0) {
pts[selPts].x = (short) (selLastX * zoom);
pts[selPts].y = (short) (selLastY * zoom);
XDrawLines(dpy, XtWindow(canvas), gc, pts, selPts + 1,
CoordModeOrigin);
}
} else if (selFree) {
for (i = 0; i < selPts; i++) {
pts[i].x = (short) ((selBX + selDX + selPX[i]) * zoom);
pts[i].y = (short) ((selBY + selDY + selPY[i]) * zoom);
}
if (selPts > 0) {
pts[selPts] = pts[0];
XDrawLines(dpy, XtWindow(canvas), gc, pts, selPts + 1,
CoordModeOrigin);
}
} else {
XDrawRectangle(dpy, XtWindow(canvas), gc,
(selBX + selDX) * zoom, (selBY + selDY) * zoom,
(unsigned) (selBW * zoom), (unsigned) (selBH * zoom));
}
XSetLineAttributes(dpy, gc, 0, LineSolid, CapButt, JoinMiter);
XSetFunction(dpy, gc, GXcopy);
XSetForeground(dpy, gc, fgPix);
}
/* draw the exposed part of the image at the current zoom (nearest pixel) */
static void
ShowZoomed(int rx, int ry, int rw, int rh)
@@ -1081,6 +1336,7 @@ CanvasExpose(Widget w, XtPointer cd, XtPointer call)
if (r.y + r.height > imgH * zoom)
r.height = (unsigned short) (imgH * zoom - r.y);
ShowZoomed(r.x, r.y, r.width, r.height);
DrawMarquee();
}
static void
@@ -1100,6 +1356,30 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep)
* must never reach XGetImage/XDraw* on the pixmap */
if (x < 0 || y < 0 || x >= imgW || y >= imgH)
return;
if (tool == T_SEL || tool == T_FREESEL) {
if (selActive && x >= selBX && y >= selBY &&
x < selBX + selBW && y < selBY + selBH) {
StartMove(x, y);
} else {
Deselect();
selDefining = 1;
selFree = (tool == T_FREESEL);
selStartX = selLastX = x;
selStartY = selLastY = y;
if (selFree) {
selPts = 1;
selPX[0] = x;
selPY[0] = y;
}
}
drawing = 0;
Redraw();
return;
}
if (selActive) {
Deselect();
Redraw();
}
color = (ev->xbutton.button == Button3) ? bgPix : fgPix;
sx = lx = x;
sy = ly = y;
@@ -1223,12 +1503,35 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep)
y = ev->xmotion.y / zoom;
StatusPos(x, y);
if (!drawing && !(tool == T_CURVE && curveState == 2) &&
!(tool == T_POLYGON && polyActive))
!(tool == T_POLYGON && polyActive) &&
!selDefining && !selMoving)
return;
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x >= imgW) x = imgW - 1;
if (y >= imgH) y = imgH - 1;
if (selDefining) {
selLastX = x;
selLastY = y;
if (selFree && selPts < SELPT_MAX &&
(abs(x - selPX[selPts - 1]) +
abs(y - selPY[selPts - 1]) >= 2)) {
selPX[selPts] = x;
selPY[selPts] = y;
selPts++;
}
Redraw();
return;
}
if (selMoving) {
selDX = x - selStartX;
selDY = y - selStartY;
RestoreSnap();
SelClearArea(selBX, selBY, selBW, selBH);
SelPaste(selBX + selDX, selBY + selDY);
Redraw();
return;
}
color = (ev->xmotion.state & Button3Mask) ? bgPix : fgPix;
switch (tool) {
case T_PENCIL:
@@ -1271,6 +1574,59 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep)
Redraw();
break;
case ButtonRelease:
if (selDefining) {
x = ev->xbutton.x / zoom;
y = ev->xbutton.y / zoom;
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x >= imgW) x = imgW - 1;
if (y >= imgH) y = imgH - 1;
if (selFree) {
if (selPts >= 3 &&
(abs(x - selPX[selPts - 1]) +
abs(y - selPY[selPts - 1]) >= 2)) {
if (selPts < SELPT_MAX) {
selPX[selPts] = x;
selPY[selPts] = y;
selPts++;
}
}
selMaskPix = None;
if (selPts >= 3)
BuildSelMask();
selActive = (selMaskPix != None);
selFree = selActive;
} else {
int x0 = selStartX < x ? selStartX : x;
int y0 = selStartY < y ? selStartY : y;
int w = abs(x - selStartX);
int h = abs(y - selStartY);
if (w >= 2 && h >= 2) {
selBX = x0;
selBY = y0;
selBW = w;
selBH = h;
selActive = 1;
selFree = 0;
} else {
Deselect();
}
}
selDefining = 0;
if (!selActive)
Deselect();
UpdateSelUI();
drawing = 0;
Redraw();
break;
}
if (selMoving) {
FinishMove();
drawing = 0;
Redraw();
break;
}
if (tool == T_CURVE && curveState == 1) {
x = ev->xbutton.x / zoom;
y = ev->xbutton.y / zoom;
@@ -1335,7 +1691,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_UNDO, C_CLEAR, C_CLEARSEL, C_SELALL, C_INVERT, C_FLIP,
C_TOOLS, C_COLORS, C_STATUS, C_HELP, C_ABOUT, C_ZNORMAL, C_ZLARGE
};
@@ -1490,6 +1846,7 @@ Command(Widget w, XtPointer data, XtPointer call)
MaybeDiscard(A_EXIT);
break;
case C_UNDO:
Deselect();
RestoreSnap();
dirty = 1;
Redraw();
@@ -1497,6 +1854,12 @@ Command(Widget w, XtPointer data, XtPointer call)
case C_CLEAR:
ClearImage();
break;
case C_CLEARSEL:
ClearSelection();
break;
case C_SELALL:
SelectAll();
break;
case C_INVERT:
Snapshot();
InvertImage();
@@ -1563,6 +1926,7 @@ FileOk(Widget w, XtPointer data, XtPointer call)
if (OpenBMP(path) == 0) {
snprintf(curPath, sizeof curPath, "%s", path);
dirty = 0;
Deselect();
Snapshot();
SetTitle();
Redraw();
@@ -1608,7 +1972,7 @@ AddMenu(Widget bar, const char *name)
return shell;
}
static void
static Widget
AddItem(Widget shell, const char *label, const char *accel,
const char *accelText, int cmd, int sep, int sensitive)
{
@@ -1616,7 +1980,7 @@ AddItem(Widget shell, const char *label, const char *accel,
if (sep) {
XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, shell, NULL);
return;
return NULL;
}
b = XtVaCreateManagedWidget((char *) label, xmPushButtonWidgetClass, shell,
XmNsensitive, sensitive, NULL);
@@ -1629,6 +1993,7 @@ AddItem(Widget shell, const char *label, const char *accel,
XtVaSetValues(b, XmNacceleratorText, s, NULL);
XmStringFree(s);
}
return b;
}
/* ------------------------------------------------------------------ icons */
@@ -1833,8 +2198,10 @@ main(int argc, char **argv)
m = AddMenu(menubarW, PaintMsg(2, "Edit"));
AddItem(m, PaintMsg(12, "Undo"), "Ctrl<Key>Z", "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);
clearSelItem = AddItem(m, PaintMsg(13, "Clear Selection"),
"<Key>Delete", "Del", C_CLEARSEL, 0, False);
AddItem(m, PaintMsg(38, "Select All"), "Ctrl<Key>A", "Ctrl+A",
C_SELALL, 0, True);
m = AddMenu(menubarW, PaintMsg(3, "View"));
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);
+1
View File
@@ -39,3 +39,4 @@ $set 1
35 Normal Size
36 Large Size
37 Text:
38 Select All
@@ -40,3 +40,4 @@ $set 1
35 Normale Groesse
36 Grosse Darstellung
37 Text:
38 Alles auswaehlen