dtpaint builds its menus in code, so its labels are catalogued:
programs/dtpaint/dtpaint.msg (set 1, 23 messages: the six menu titles, the
items of File/Edit/View/Image/Colors/Help, the About title and the status
line), the German copy in programs/localized/de_DE.UTF-8/msg/dtpaint.msg,
built by msg.C.am and listed in the PLIST (C plain, ${PLIST.nls} for
German). catopen/catgets itself, setlocale in main.
dtpaint has no render table (core font): a UTF-8 "ü" in the status line came
out as two latin-1 characters ("Menü"), so the German string says
"Hilfe-Menue" instead (recorded in AGENTS.md).
Measured on Xvfb (1280x800, fresh HOME, ttsession + dtwm), de_DE.UTF-8 via
localedef/LOCPATH: menus "Datei Bearbeiten Ansicht Bild Farben Hilfe",
status "Hilfe: Hilfe-Themen im Hilfe-Menue." (/tmp/opencode/dtpa-de.png);
LANG=C: "File Edit View Image Colors Help" and the English status
(/tmp/opencode/dtpa-c.png). make mcde-linux MCDE_DIRS="install
programs/dtpaint" and "install programs/localized/C/msg" exit=0;
MCDE_DIRS=plist: "3486 installed names under dt/, 8680 in the PLISTs", ok.
998 lines
26 KiB
C
998 lines
26 KiB
C
/*
|
|
* dtpaint.c -- MCDE: the simple paint program, the Microsoft Paint look
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* 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).
|
|
*
|
|
* 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.
|
|
*
|
|
* C99 and POSIX, Motif and Xlib.
|
|
*/
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <locale.h>
|
|
#include <nl_types.h>
|
|
#include <string.h>
|
|
|
|
#include <X11/Xlib.h>
|
|
#include <X11/Xutil.h>
|
|
|
|
#include <Xm/Xm.h>
|
|
#include <Xm/CascadeB.h>
|
|
#include <Xm/DrawingA.h>
|
|
#include <Xm/FileSB.h>
|
|
#include <Xm/Form.h>
|
|
#include <Xm/Label.h>
|
|
#include <Xm/MainW.h>
|
|
#include <Xm/MessageB.h>
|
|
#include <Xm/PushB.h>
|
|
#include <Xm/RowColumn.h>
|
|
#include <Xm/Separator.h>
|
|
#include <Xm/ToggleB.h>
|
|
|
|
/* dtpaint's message catalog (programs/dtpaint/dtpaint.msg, the locale's copy
|
|
* in programs/localized/<locale>/msg, under lib/nls/msg) */
|
|
static nl_catd cat = (nl_catd) -1;
|
|
|
|
static const char *
|
|
PaintMsg(int n, const char *text)
|
|
{
|
|
if (cat == (nl_catd) -1) {
|
|
if (getenv("NLSPATH") == NULL)
|
|
(void) setenv("NLSPATH", CDE_INSTALLATION_TOP
|
|
"/lib/nls/msg/%L/%N.cat:" CDE_INSTALLATION_TOP
|
|
"/lib/nls/msg/C/%N.cat", 0);
|
|
cat = catopen("dtpaint", NL_CAT_LOCALE);
|
|
}
|
|
if (cat == (nl_catd) -1)
|
|
return text;
|
|
return catgets(cat, 1, n, text);
|
|
}
|
|
|
|
#define PAL_COLS 14
|
|
#define PAL_ROWS 2
|
|
#define PAL_N (PAL_COLS * PAL_ROWS)
|
|
|
|
/* the Windows Paint palette: two rows of fourteen, dark row then bright row */
|
|
static const char *const palHex[PAL_N] = {
|
|
"000000", "808080", "800000", "808000", "008000", "008080",
|
|
"000080", "800080", "808040", "004040", "0080FF", "004080",
|
|
"8000FF", "804000",
|
|
"FFFFFF", "C0C0C0", "FF0000", "FFFF00", "00FF00", "00FFFF",
|
|
"0000FF", "FF00FF", "FFFF80", "00FF80", "80FFFF", "8080FF",
|
|
"FF0080", "FF8040"
|
|
};
|
|
|
|
enum {
|
|
T_FREESEL, T_SEL, T_ERASER, T_FILL, T_PICK, T_ZOOM, T_PENCIL,
|
|
T_BRUSH, T_AIRBRUSH, T_TEXT, T_LINE, T_CURVE, T_RECT, T_POLYGON,
|
|
T_ELLIPSE, T_ROUNDRECT, T_NONE
|
|
};
|
|
|
|
static const char *const toolNames[] = {
|
|
"freesel", "select", "eraser", "fill", "pick", "zoom", "pencil",
|
|
"brush", "airbrush", "text", "line", "curve", "rect", "polygon",
|
|
"ellipse", "roundrect", NULL
|
|
};
|
|
|
|
static Display *dpy;
|
|
static Widget top, mainw, menubar, form, canvas, colorbox, toolbox;
|
|
static Widget statusHelp, statusPos;
|
|
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 XColor palette[PAL_N];
|
|
static int drawing, sx, sy, lx, ly;
|
|
static int curx, cury;
|
|
static int toolsShown = 1, colorsShown = 1, statusShown = 1;
|
|
|
|
/* ------------------------------------------------------------------ files */
|
|
|
|
static int
|
|
PixelRGB(unsigned long pixel, unsigned char *r, unsigned char *g, unsigned char *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;
|
|
}
|
|
|
|
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)
|
|
{
|
|
XImage *im;
|
|
FILE *f;
|
|
int row = ((imgW * 3 + 3) / 4) * 4;
|
|
unsigned char *data;
|
|
int x, y;
|
|
unsigned long size = (unsigned long) row * imgH;
|
|
|
|
if ((im = XGetImage(dpy, pix, 0, 0, imgW, imgH, AllPlanes, ZPixmap)) == NULL)
|
|
return -1;
|
|
if ((data = calloc(1, (size_t) size)) == NULL) {
|
|
XDestroyImage(im);
|
|
return -1;
|
|
}
|
|
for (y = 0; y < imgH; y++)
|
|
for (x = 0; x < imgW; x++) {
|
|
unsigned char r, g, b;
|
|
unsigned char *p = data + (size_t) (imgH - 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;
|
|
}
|
|
{
|
|
unsigned char hdr[54];
|
|
|
|
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) imgW, 4);
|
|
PutLE(hdr + 22, (unsigned long) imgH, 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 int
|
|
OpenBMP(const char *path)
|
|
{
|
|
FILE *f;
|
|
unsigned char hdr[54];
|
|
unsigned long off, w, h, row;
|
|
unsigned char *data;
|
|
XImage *im;
|
|
int y, x;
|
|
|
|
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);
|
|
|
|
if ((int) w != imgW || (int) h != imgH) {
|
|
imgW = (int) w;
|
|
imgH = (int) h;
|
|
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);
|
|
}
|
|
XSetForeground(dpy, gc, palette[bg].pixel);
|
|
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);
|
|
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, pix, gc, im, 0, 0, 0, 0, (unsigned int) w,
|
|
(unsigned int) h);
|
|
XDestroyImage(im);
|
|
free(data);
|
|
return 0;
|
|
}
|
|
|
|
/* --------------------------------------------------------------- drawing */
|
|
|
|
static void
|
|
SetFg(int color)
|
|
{
|
|
XSetForeground(dpy, gc, palette[color].pixel);
|
|
}
|
|
|
|
static void
|
|
RestoreSnap(void)
|
|
{
|
|
XCopyArea(dpy, snap, pix, gc, 0, 0, imgW, imgH, 0, 0);
|
|
}
|
|
|
|
static void
|
|
DrawDab(int x, int y, int radius, int color)
|
|
{
|
|
SetFg(color);
|
|
XFillArc(dpy, pix, gc, x - radius, y - radius, radius * 2 + 1,
|
|
radius * 2 + 1, 0, 360 * 64);
|
|
}
|
|
|
|
static void
|
|
DrawLineTo(int x0, int y0, int x1, int y1, int color)
|
|
{
|
|
SetFg(color);
|
|
XDrawLine(dpy, pix, gc, x0, y0, x1, y1);
|
|
}
|
|
|
|
static void
|
|
DrawShape(int x0, int y0, int x1, int y1, int color)
|
|
{
|
|
int x = x0 < x1 ? x0 : x1;
|
|
int y = y0 < y1 ? y0 : y1;
|
|
unsigned int w = (unsigned int) (x0 < x1 ? x1 - x0 : x0 - x1);
|
|
unsigned int h = (unsigned int) (y0 < y1 ? y1 - y0 : y0 - y1);
|
|
|
|
SetFg(color);
|
|
switch (tool) {
|
|
case T_LINE:
|
|
XDrawLine(dpy, pix, gc, x0, y0, x1, y1);
|
|
break;
|
|
case T_RECT:
|
|
XDrawRectangle(dpy, pix, gc, x, y, w, h);
|
|
break;
|
|
case T_ELLIPSE:
|
|
XDrawArc(dpy, pix, gc, x, y, w, h, 0, 360 * 64);
|
|
break;
|
|
case T_ROUNDRECT:
|
|
XDrawArc(dpy, pix, gc, x, y, 12, 12, 90 * 64, 90 * 64);
|
|
XDrawArc(dpy, pix, gc, (int) (x + w - 12), y, 12, 12, 0, 90 * 64);
|
|
XDrawArc(dpy, pix, gc, (int) (x + w - 12), (int) (y + h - 12), 12, 12,
|
|
270 * 64, 90 * 64);
|
|
XDrawArc(dpy, pix, gc, x, (int) (y + h - 12), 12, 12, 180 * 64, 90 * 64);
|
|
XDrawLine(dpy, pix, gc, x + 6, y, x + w - 6, y);
|
|
XDrawLine(dpy, pix, gc, x + 6, y + h, x + w - 6, y + h);
|
|
XDrawLine(dpy, pix, gc, x, y + 6, x, y + h - 6);
|
|
XDrawLine(dpy, pix, gc, x + w, y + 6, x + w, y + h - 6);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void
|
|
FloodFill(int x, int y, int color)
|
|
{
|
|
XImage *im;
|
|
unsigned long old;
|
|
int *stack, sp = 0, max = imgW * imgH;
|
|
|
|
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) {
|
|
XDestroyImage(im);
|
|
return;
|
|
}
|
|
if ((stack = malloc(sizeof(int) * (size_t) max * 4)) == NULL) {
|
|
XDestroyImage(im);
|
|
return;
|
|
}
|
|
stack[sp++] = y * imgW + x;
|
|
while (sp > 0) {
|
|
int p = stack[--sp], px = p % imgW, py = p / imgW;
|
|
|
|
if (XGetPixel(im, px, py) != 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;
|
|
}
|
|
free(stack);
|
|
XPutImage(dpy, pix, gc, im, 0, 0, 0, 0, imgW, imgH);
|
|
XDestroyImage(im);
|
|
}
|
|
|
|
static void
|
|
InvertImage(void)
|
|
{
|
|
XImage *im;
|
|
int x, y;
|
|
|
|
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 (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);
|
|
}
|
|
XPutImage(dpy, pix, gc, im, 0, 0, 0, 0, imgW, imgH);
|
|
XDestroyImage(im);
|
|
}
|
|
|
|
static void
|
|
FlipHorizontal(void)
|
|
{
|
|
Pixmap tmp = XCreatePixmap(dpy, DefaultRootWindow(dpy), imgW, imgH,
|
|
DefaultDepth(dpy, 0));
|
|
|
|
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);
|
|
}
|
|
XFreePixmap(dpy, tmp);
|
|
}
|
|
|
|
static void
|
|
NewImage(void)
|
|
{
|
|
XSetForeground(dpy, gc, palette[bg].pixel);
|
|
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);
|
|
}
|
|
|
|
/* ----------------------------------------------------------------- events */
|
|
|
|
static void
|
|
StatusPos(int x, int y)
|
|
{
|
|
char buf[96];
|
|
|
|
snprintf(buf, sizeof buf, "%d,%d", x, y);
|
|
XtVaSetValues(statusPos, XmNlabelString,
|
|
XmStringCreateLocalized(buf), NULL);
|
|
}
|
|
|
|
static void
|
|
CanvasExpose(Widget w, XtPointer cd, XtPointer call)
|
|
{
|
|
XmDrawingAreaCallbackStruct *c = (XmDrawingAreaCallbackStruct *) call;
|
|
|
|
(void) cd;
|
|
if (c->reason != XmCR_EXPOSE)
|
|
return;
|
|
XCopyArea(dpy, pix, XtWindow(w), gc, 0, 0, imgW, imgH, 0, 0);
|
|
}
|
|
|
|
static void
|
|
CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep)
|
|
{
|
|
int color, 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;
|
|
sx = lx = x;
|
|
sy = ly = y;
|
|
curx = x; cury = y;
|
|
StatusPos(x, y);
|
|
drawing = 1;
|
|
switch (tool) {
|
|
case T_FILL:
|
|
FloodFill(x, y, color);
|
|
drawing = 0;
|
|
break;
|
|
case T_PICK:
|
|
{
|
|
XImage *im = XGetImage(dpy, pix, x, y, 1, 1,
|
|
AllPlanes, ZPixmap);
|
|
|
|
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;
|
|
XDestroyImage(im);
|
|
}
|
|
}
|
|
drawing = 0;
|
|
break;
|
|
case T_ERASER:
|
|
DrawDab(x, y, 4, bg);
|
|
break;
|
|
case T_BRUSH:
|
|
DrawDab(x, y, 3, color);
|
|
break;
|
|
case T_PENCIL:
|
|
SetFg(color);
|
|
XDrawPoint(dpy, pix, gc, x, y);
|
|
break;
|
|
default:
|
|
XCopyArea(dpy, pix, snap, gc, 0, 0, imgW, imgH, 0, 0);
|
|
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;
|
|
switch (tool) {
|
|
case T_PENCIL:
|
|
DrawLineTo(lx, ly, x, y, color);
|
|
break;
|
|
case T_BRUSH:
|
|
DrawDab(x, y, 3, color);
|
|
break;
|
|
case T_ERASER:
|
|
DrawDab(x, y, 4, bg);
|
|
break;
|
|
case T_LINE: case T_RECT: case T_ELLIPSE: case T_ROUNDRECT:
|
|
RestoreSnap();
|
|
DrawShape(sx, sy, x, y, color);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
lx = x; ly = y;
|
|
XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True);
|
|
break;
|
|
case ButtonRelease:
|
|
drawing = 0;
|
|
XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void
|
|
ToolChanged(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
(void) call;
|
|
if (XmToggleButtonGetState(w)) {
|
|
tool = (int) (long) data;
|
|
fprintf(stderr, "dtpaint: tool %s\n", toolNames[tool]);
|
|
}
|
|
}
|
|
|
|
static void
|
|
SwatchPress(Widget w, XtPointer data, XEvent *ev, Boolean *keep)
|
|
{
|
|
int idx = (int) (long) data;
|
|
|
|
(void) w; (void) keep;
|
|
if (ev->type != ButtonPress)
|
|
return;
|
|
if (ev->xbutton.button == Button1)
|
|
fg = idx;
|
|
else if (ev->xbutton.button == Button3)
|
|
bg = idx;
|
|
}
|
|
|
|
static void
|
|
SwatchActivate(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
(void) w; (void) call;
|
|
fg = (int) (long) data;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ menus */
|
|
|
|
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
|
|
};
|
|
|
|
static void FileDialog(int save);
|
|
|
|
static void
|
|
Command(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
char path[512];
|
|
|
|
(void) w; (void) call;
|
|
switch ((int) (long) data) {
|
|
case C_NEW:
|
|
NewImage();
|
|
break;
|
|
case C_OPEN:
|
|
FileDialog(0);
|
|
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);
|
|
else
|
|
fprintf(stderr, "dtpaint: cannot save %s\n", path);
|
|
break;
|
|
case C_SAVEAS:
|
|
FileDialog(1);
|
|
break;
|
|
case C_EXIT:
|
|
exit(0);
|
|
case C_UNDO:
|
|
RestoreSnap();
|
|
XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True);
|
|
break;
|
|
case C_CLEAR:
|
|
NewImage();
|
|
break;
|
|
case C_INVERT:
|
|
InvertImage();
|
|
XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True);
|
|
break;
|
|
case C_FLIP:
|
|
FlipHorizontal();
|
|
XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True);
|
|
break;
|
|
case C_TOOLS:
|
|
toolsShown = !toolsShown;
|
|
if (toolsShown) XtManageChild(toolbox);
|
|
else XtUnmanageChild(toolbox);
|
|
break;
|
|
case C_COLORS:
|
|
colorsShown = !colorsShown;
|
|
if (colorsShown) XtManageChild(colorbox);
|
|
else XtUnmanageChild(colorbox);
|
|
break;
|
|
case C_STATUS:
|
|
statusShown = !statusShown;
|
|
if (statusShown) {
|
|
XtManageChild(statusHelp);
|
|
XtManageChild(statusPos);
|
|
} else {
|
|
XtUnmanageChild(statusHelp);
|
|
XtUnmanageChild(statusPos);
|
|
}
|
|
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);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void
|
|
FileOk(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
char *path = NULL;
|
|
int save = (int) (long) data;
|
|
|
|
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);
|
|
XtFree(path);
|
|
if (!save)
|
|
XClearArea(dpy, XtWindow(canvas), 0, 0, imgW, imgH, True);
|
|
}
|
|
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);
|
|
|
|
XtAddCallback(d, XmNokCallback, FileOk, (XtPointer) (long) save);
|
|
XtAddCallback(d, XmNcancelCallback, FileCancel, NULL);
|
|
XtVaSetValues(d, XmNfileTypeMask, XmFILE_REGULAR, NULL);
|
|
XtManageChild(d);
|
|
}
|
|
|
|
|
|
static Widget
|
|
AddMenu(Widget bar, const char *name)
|
|
{
|
|
Widget shell = XmCreatePulldownMenu(bar, (char *) name, NULL, 0);
|
|
|
|
XtVaCreateManagedWidget(name, xmCascadeButtonWidgetClass, bar,
|
|
XmNsubMenuId, shell, NULL);
|
|
return shell;
|
|
}
|
|
|
|
static void
|
|
AddItem(Widget shell, const char *label, const char *accel, int cmd, int sep)
|
|
{
|
|
if (sep) {
|
|
XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, shell, NULL);
|
|
return;
|
|
}
|
|
{
|
|
Widget b = XtVaCreateManagedWidget(label, xmPushButtonWidgetClass,
|
|
shell, NULL);
|
|
|
|
XtAddCallback(b, XmNactivateCallback, Command,
|
|
(XtPointer) (long) cmd);
|
|
if (accel != NULL)
|
|
XtVaSetValues(b, XmNaccelerator, accel, NULL);
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ icons */
|
|
|
|
static void
|
|
Glyph(Display *d, Pixmap p, int t)
|
|
{
|
|
XGCValues gv;
|
|
GC k;
|
|
|
|
gv.foreground = BlackPixel(d, 0);
|
|
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));
|
|
switch (t) {
|
|
case T_FREESEL:
|
|
XDrawLine(d, p, k, 3, 4, 8, 2);
|
|
XDrawLine(d, p, k, 8, 2, 12, 6);
|
|
XDrawLine(d, p, k, 12, 6, 8, 13);
|
|
XDrawLine(d, p, k, 8, 13, 3, 11);
|
|
XDrawLine(d, p, k, 3, 11, 3, 4);
|
|
break;
|
|
case T_SEL:
|
|
XDrawRectangle(d, p, k, 3, 3, 10, 10);
|
|
break;
|
|
case T_ERASER:
|
|
XFillRectangle(d, p, k, 3, 8, 9, 5);
|
|
XDrawLine(d, p, k, 3, 8, 7, 3);
|
|
XDrawLine(d, p, k, 11, 8, 13, 5);
|
|
break;
|
|
case T_FILL:
|
|
XDrawLine(d, p, k, 3, 8, 9, 2);
|
|
XDrawLine(d, p, k, 9, 2, 9, 14);
|
|
XDrawLine(d, p, k, 9, 14, 3, 8);
|
|
XDrawLine(d, p, k, 12, 10, 12, 14);
|
|
break;
|
|
case T_PICK:
|
|
XDrawLine(d, p, k, 3, 13, 10, 6);
|
|
XDrawLine(d, p, k, 9, 3, 13, 7);
|
|
break;
|
|
case T_ZOOM:
|
|
XDrawArc(d, p, k, 3, 3, 8, 8, 0, 360 * 64);
|
|
XDrawLine(d, p, k, 10, 10, 14, 14);
|
|
break;
|
|
case T_PENCIL:
|
|
XDrawLine(d, p, k, 3, 13, 11, 5);
|
|
XDrawLine(d, p, k, 11, 5, 13, 3);
|
|
break;
|
|
case T_BRUSH:
|
|
XDrawLine(d, p, k, 4, 12, 9, 7);
|
|
XFillRectangle(d, p, k, 9, 3, 4, 4);
|
|
break;
|
|
case T_AIRBRUSH:
|
|
XDrawLine(d, p, k, 4, 12, 8, 8);
|
|
XDrawPoint(d, p, k, 10, 4);
|
|
XDrawPoint(d, p, k, 12, 6);
|
|
XDrawPoint(d, p, k, 9, 7);
|
|
break;
|
|
case T_TEXT:
|
|
XDrawLine(d, p, k, 3, 3, 13, 3);
|
|
XDrawLine(d, p, k, 8, 3, 8, 13);
|
|
break;
|
|
case T_LINE:
|
|
XDrawLine(d, p, k, 3, 12, 12, 3);
|
|
break;
|
|
case T_CURVE:
|
|
XDrawArc(d, p, k, 3, 5, 12, 8, 0, 180 * 64);
|
|
break;
|
|
case T_RECT:
|
|
XDrawRectangle(d, p, k, 2, 3, 12, 10);
|
|
break;
|
|
case T_POLYGON:
|
|
XDrawLine(d, p, k, 8, 2, 13, 8);
|
|
XDrawLine(d, p, k, 13, 8, 9, 14);
|
|
XDrawLine(d, p, k, 9, 14, 3, 10);
|
|
XDrawLine(d, p, k, 3, 10, 8, 2);
|
|
break;
|
|
case T_ELLIPSE:
|
|
XDrawArc(d, p, k, 2, 3, 12, 10, 0, 360 * 64);
|
|
break;
|
|
case T_ROUNDRECT:
|
|
XDrawRectangle(d, p, k, 2, 3, 12, 10);
|
|
break;
|
|
}
|
|
XFreeGC(d, k);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------- main */
|
|
|
|
int
|
|
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;
|
|
|
|
for (i = 1; i < argc; i++)
|
|
if (strcmp(argv[i], "-selftest") == 0)
|
|
selftest = 1;
|
|
|
|
top = XtVaAppInitialize(&app, "Dtpaint", NULL, 0, &argc, argv, NULL, NULL);
|
|
dpy = XtDisplay(top);
|
|
|
|
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);
|
|
}
|
|
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;
|
|
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);
|
|
|
|
XDestroyImage(im);
|
|
if (line == palette[0].pixel &&
|
|
area == palette[16].pixel) {
|
|
printf("dtpaint selftest: PASS\n");
|
|
return 0;
|
|
}
|
|
printf("dtpaint selftest: FAIL\n");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
mainw = XmCreateMainWindow(top, "main", NULL, 0);
|
|
XtManageChild(mainw);
|
|
menubarW = XmCreateMenuBar(mainw, "menuBar", NULL, 0);
|
|
XtManageChild(menubarW);
|
|
|
|
{
|
|
Widget m = AddMenu(menubarW, PaintMsg(1, "File"));
|
|
|
|
AddItem(m, PaintMsg(7, "New"), "Ctrl<Key>N", C_NEW, 0);
|
|
AddItem(m, PaintMsg(8, "Open..."), "Ctrl<Key>O", C_OPEN, 0);
|
|
AddItem(m, PaintMsg(9, "Save"), "Ctrl<Key>S", C_SAVE, 0);
|
|
AddItem(m, PaintMsg(10, "Save As..."), NULL, C_SAVEAS, 0);
|
|
AddItem(m, NULL, NULL, 0, 1);
|
|
AddItem(m, PaintMsg(11, "Exit"), "Alt<Key>F4", C_EXIT, 0);
|
|
m = AddMenu(menubarW, PaintMsg(2, "Edit"));
|
|
AddItem(m, PaintMsg(12, "Undo"), "Ctrl<Key>Z", C_UNDO, 0);
|
|
AddItem(m, NULL, NULL, 0, 1);
|
|
AddItem(m, PaintMsg(13, "Clear Selection"), NULL, C_CLEAR, 0);
|
|
m = AddMenu(menubarW, PaintMsg(3, "View"));
|
|
AddItem(m, PaintMsg(14, "Tool Box"), "Ctrl<Key>T", C_TOOLS, 0);
|
|
AddItem(m, PaintMsg(15, "Color Box"), "Ctrl<Key>L", C_COLORS, 0);
|
|
AddItem(m, PaintMsg(16, "Status Bar"), NULL, C_STATUS, 0);
|
|
m = AddMenu(menubarW, PaintMsg(4, "Image"));
|
|
AddItem(m, PaintMsg(17, "Flip Horizontal"), NULL, C_FLIP, 0);
|
|
AddItem(m, PaintMsg(18, "Invert Colors"), "Ctrl<Key>I", C_INVERT, 0);
|
|
AddItem(m, PaintMsg(19, "Clear Image"), "Ctrl<Key>Shift<Key>N", C_CLEAR, 0);
|
|
m = AddMenu(menubarW, PaintMsg(5, "Colors"));
|
|
AddItem(m, PaintMsg(20, "Edit Colors..."), NULL, C_ABOUT, 0);
|
|
m = AddMenu(menubarW, PaintMsg(6, "Help"));
|
|
AddItem(m, PaintMsg(21, "About dtpaint..."), NULL, C_ABOUT, 0);
|
|
}
|
|
|
|
form = XtVaCreateManagedWidget("form", xmFormWidgetClass, mainw,
|
|
XmNfractionBase, 100, XmNwidth, 730, XmNheight, 540, 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);
|
|
for (i = 0; i < T_NONE; i++) {
|
|
Widget b;
|
|
Pixmap p = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16,
|
|
DefaultDepth(dpy, 0));
|
|
|
|
Glyph(dpy, p, i);
|
|
b = XtVaCreateManagedWidget(toolNames[i], xmToggleButtonWidgetClass,
|
|
toolbox, XmNlabelType, XmPIXMAP, XmNlabelPixmap, p,
|
|
XmNindicatorType, XmN_OF_MANY, XmNshadowThickness, 1,
|
|
XmNmarginWidth, 1, XmNmarginHeight, 1,
|
|
XmNwidth, 24, XmNheight, 24, 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,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, toolbox,
|
|
XmNorientation, XmVERTICAL, XmNspacing, 1,
|
|
XmNmarginWidth, 2, XmNmarginHeight, 2, NULL);
|
|
{
|
|
int r, c;
|
|
|
|
for (r = 0; r < PAL_ROWS; r++) {
|
|
Widget row = XtVaCreateManagedWidget("row",
|
|
xmRowColumnWidgetClass, colorbox, XmNorientation,
|
|
XmHORIZONTAL, XmNspacing, 1, NULL);
|
|
|
|
for (c = 0; c < PAL_COLS; c++) {
|
|
Widget b;
|
|
int idx = r * PAL_COLS + c;
|
|
|
|
b = XtVaCreateManagedWidget("swatch",
|
|
xmPushButtonWidgetClass, row,
|
|
XmNbackground, palette[idx].pixel,
|
|
XmNwidth, 16, XmNheight, 16,
|
|
XmNshadowThickness, 1,
|
|
XmNmarginWidth, 0, XmNmarginHeight, 0,
|
|
XmNlabelString,
|
|
XmStringCreateLocalized(""), NULL);
|
|
XtAddCallback(b, XmNactivateCallback,
|
|
SwatchActivate, (XtPointer) (long) idx);
|
|
XtAddEventHandler(b, ButtonPressMask, False,
|
|
SwatchPress, (XtPointer) (long) idx);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* status line at the bottom, canvas between toolbox and right edge */
|
|
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);
|
|
statusPos = XtVaCreateManagedWidget("pos", xmLabelWidgetClass, form,
|
|
XmNbottomAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM,
|
|
XmNalignment, XmALIGNMENT_END, NULL);
|
|
XtVaSetValues(statusPos, XmNlabelString,
|
|
XmStringCreateLocalized("0,0"), NULL);
|
|
|
|
canvas = XtVaCreateManagedWidget("canvas", xmDrawingAreaWidgetClass,
|
|
form, XmNtopAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, toolbox,
|
|
XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, statusHelp,
|
|
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);
|
|
XtRealizeWidget(top);
|
|
NewImage();
|
|
XtAppMainLoop(app);
|
|
return 0;
|
|
}
|