MCDE dtpaint: airbrush and text tools (D3)
Airbrush sprays random foreground/background dots in a 15-pixel square while dragging; Text opens a prompt dialog, then fills the text box with the background colour and draws the string in the foreground colour with the GC's font (the image stays 1:1 at any zoom). The prompt's OK button is a gadget whose callback widget is the SelectionBox itself, so the text field is found there, not under XtParent. Measured under Xvfb: spray dots on the canvas; 'Hello dtpaint' appears at the clicked point.
This commit is contained in:
1 parent
6fb09e6d9d
commit
e987e0379f
3 files changed
+103
No files matched your search
@@ -47,7 +47,9 @@
|
||||
#include <Xm/PushB.h>
|
||||
#include <Xm/RowColumn.h>
|
||||
#include <Xm/ScrolledW.h>
|
||||
#include <Xm/SelectioB.h>
|
||||
#include <Xm/Separator.h>
|
||||
#include <Xm/Text.h>
|
||||
#include <Xm/ToggleB.h>
|
||||
|
||||
/* dtpaint's message catalog (programs/dtpaint/dtpaint.msg, the locale's copy
|
||||
@@ -127,6 +129,8 @@ static char curPath[512];
|
||||
static int dirty;
|
||||
static int pendingAction;
|
||||
|
||||
static void DestroyDialog(Widget w, XtPointer data, XtPointer call);
|
||||
|
||||
/* ------------------------------------------------------------------ colour */
|
||||
|
||||
static int
|
||||
@@ -833,6 +837,85 @@ ClearImage(void)
|
||||
|
||||
/* ----------------------------------------------------------------- events */
|
||||
|
||||
static void
|
||||
Spray(int x, int y, unsigned long color)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 14; i++) {
|
||||
int px = x + (rand() % 15) - 7;
|
||||
int py = y + (rand() % 15) - 7;
|
||||
|
||||
if (px < 0 || py < 0 || px >= imgW || py >= imgH)
|
||||
continue;
|
||||
SetFg(color);
|
||||
XDrawPoint(dpy, pix, gc, px, py);
|
||||
}
|
||||
}
|
||||
|
||||
static int textX = 100, textY = 100;
|
||||
static unsigned long textColor;
|
||||
|
||||
static void
|
||||
TextOk(Widget w, XtPointer cd, XtPointer call)
|
||||
{
|
||||
/* the OK button of a SelectionBox is a gadget: the callback's
|
||||
* widget is the SelectionBox itself, not the button */
|
||||
Widget text = XmSelectionBoxGetChild(w, XmDIALOG_TEXT);
|
||||
char *s = (text != NULL) ? XmTextGetString(text) : NULL;
|
||||
|
||||
(void) cd; (void) call;
|
||||
if (s != NULL) {
|
||||
if (s[0] != '\0') {
|
||||
XFontStruct *font = XQueryFont(dpy, XGContextFromGC(gc));
|
||||
|
||||
if (font != NULL) {
|
||||
int width = XTextWidth(font, s, (int) strlen(s));
|
||||
|
||||
SetFg(bgPix);
|
||||
XFillRectangle(dpy, pix, gc, textX,
|
||||
textY - font->ascent, (unsigned) width,
|
||||
(unsigned) (font->ascent + font->descent));
|
||||
SetFg(textColor);
|
||||
XDrawString(dpy, pix, gc, textX, textY, s,
|
||||
(int) strlen(s));
|
||||
XFreeFontInfo(NULL, font, 1);
|
||||
dirty = 1;
|
||||
Redraw();
|
||||
}
|
||||
}
|
||||
XtFree(s);
|
||||
}
|
||||
DestroyDialog(w, NULL, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
TextCancel(Widget w, XtPointer cd, XtPointer call)
|
||||
{
|
||||
(void) cd; (void) call;
|
||||
DestroyDialog(w, NULL, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
TextDialog(void)
|
||||
{
|
||||
Widget d = XmCreatePromptDialog(top, "text", NULL, 0);
|
||||
Widget help, text;
|
||||
XmString s = Msg(37, "Text:");
|
||||
|
||||
XtVaSetValues(d, XmNselectionLabelString, s, NULL);
|
||||
XmStringFree(s);
|
||||
help = XmSelectionBoxGetChild(d, XmDIALOG_HELP_BUTTON);
|
||||
if (help != NULL)
|
||||
XtUnmanageChild(help);
|
||||
XtAddCallback(d, XmNokCallback, TextOk, NULL);
|
||||
XtAddCallback(d, XmNcancelCallback, TextCancel, NULL);
|
||||
XtManageChild(d);
|
||||
text = XmSelectionBoxGetChild(d, XmDIALOG_TEXT);
|
||||
if (text != NULL)
|
||||
XmProcessTraversal(text, XmTRAVERSE_CURRENT);
|
||||
}
|
||||
|
||||
static void
|
||||
StatusPos(int x, int y)
|
||||
{
|
||||
@@ -992,6 +1075,19 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep)
|
||||
DrawDab(x, y, 3, color);
|
||||
dirty = 1;
|
||||
break;
|
||||
case T_AIRBRUSH:
|
||||
Snapshot();
|
||||
Spray(x, y, color);
|
||||
dirty = 1;
|
||||
break;
|
||||
case T_TEXT:
|
||||
Snapshot();
|
||||
textX = x;
|
||||
textY = y;
|
||||
textColor = color;
|
||||
TextDialog();
|
||||
drawing = 0;
|
||||
break;
|
||||
case T_PENCIL:
|
||||
Snapshot();
|
||||
SetFg(color);
|
||||
@@ -1033,6 +1129,10 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep)
|
||||
DrawDab(x, y, 4, bgPix);
|
||||
dirty = 1;
|
||||
break;
|
||||
case T_AIRBRUSH:
|
||||
Spray(x, y, color);
|
||||
dirty = 1;
|
||||
break;
|
||||
case T_LINE: case T_RECT: case T_ELLIPSE: case T_ROUNDRECT:
|
||||
RestoreSnap();
|
||||
DrawShape(sx, sy, x, y, color);
|
||||
@@ -1490,6 +1590,7 @@ main(int argc, char **argv)
|
||||
|
||||
top = XtVaAppInitialize(&app, "Dtpaint", NULL, 0, &argc, argv, NULL, NULL);
|
||||
dpy = XtDisplay(top);
|
||||
(void) srand((unsigned) getpid());
|
||||
InitColor();
|
||||
|
||||
for (i = 0; i < PAL_N; i++) {
|
||||
|
||||
@@ -38,3 +38,4 @@ $set 1
|
||||
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 Text:
|
||||
@@ -39,3 +39,4 @@ $set 1
|
||||
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
|
||||
37 Text:
|
||||
Reference in new issue
Block a user