McdeSys gains mcde_sys_print_file(path, printer): it looks for lp(1) (/usr/bin, /usr/local/bin, /usr/sbin, pkg prefix; MCDE_PRINT_COMMAND overrides it for tests), passes -d PRINTER when one is named, and runs it as the existing job (fd into the event loop, finish/cancel). dtpaint links libMcdeSys, adds File > Print... (Ctrl+P): it picks the default queue from mcde_sys_printers (or the first), writes the image to /tmp/dtpaint-PID.bmp, starts the job and removes the file when the job ends; errors and 'no printer is set up' go to the information dialog. Measured under Xvfb with MCDE_PRINT_COMMAND=fake lp: argv '-d HP_LaserJet_M110w_CC1016 /tmp/dtpaint-3543226.bmp', the fake copied a valid 640x480 BMP and the temporary file was gone; no physical page was printed (the real queue is the user's step).
3109 lines
77 KiB
C
3109 lines
77 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, 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 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.
|
|
*/
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <locale.h>
|
|
#include <nl_types.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include <X11/Xlib.h>
|
|
#include <X11/Xutil.h>
|
|
|
|
#include <Mcde/McdeSys.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/Scale.h>
|
|
#include <Xm/ScrolledW.h>
|
|
#include <Xm/SelectioB.h>
|
|
#include <Xm/Separator.h>
|
|
#include <Xm/Text.h>
|
|
#include <Xm/TextF.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). The core font has
|
|
* no UTF-8 rendition, so the German copy transliterates umlauts. */
|
|
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);
|
|
}
|
|
|
|
/* 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)
|
|
|
|
/* 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 XtAppContext appctx;
|
|
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 zoom = 1; /* 1, 2, 4 or 8 */
|
|
static int tool = T_PENCIL;
|
|
static unsigned long fgPix, bgPix;
|
|
static XColor palette[PAL_N];
|
|
static int drawing, sx, sy, lx, ly;
|
|
static int curveState; /* 0 idle, 1 drag ends, 2 bend */
|
|
static int curveX0, curveY0, curveX1, curveY1;
|
|
static unsigned long curveColor;
|
|
#define POLY_MAX 128
|
|
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 Widget attrW, attrH, colRS, colGS, colBS;
|
|
static int toolsShown = 1, colorsShown = 1, statusShown = 1;
|
|
static char curPath[512];
|
|
static int dirty;
|
|
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 */
|
|
|
|
static int
|
|
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)
|
|
{
|
|
XColor c;
|
|
|
|
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)
|
|
{
|
|
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;
|
|
unsigned long *keys = NULL;
|
|
unsigned char *krgb = NULL;
|
|
int nkeys = 0;
|
|
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;
|
|
}
|
|
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 r, g, b;
|
|
unsigned char *p = data + (size_t) (imgH - 1 - y) * row + x * 3;
|
|
unsigned long px = XGetPixel(im, x, y);
|
|
|
|
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);
|
|
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);
|
|
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;
|
|
}
|
|
free(data);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
OpenBMP24(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) | ((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;
|
|
}
|
|
row = ((w * 3 + 3) / 4) * 4;
|
|
if ((data = malloc(row * h)) == NULL) {
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
if (fseek(f, (long) off, SEEK_SET) != 0 ||
|
|
fread(data, 1, row * h, f) != row * h) {
|
|
free(data);
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
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);
|
|
if (snap != None)
|
|
XFreePixmap(dpy, snap);
|
|
pix = np;
|
|
snap = ns;
|
|
if (canvas != NULL)
|
|
XtVaSetValues(canvas, XmNwidth, imgW * zoom,
|
|
XmNheight, imgH * zoom, NULL);
|
|
}
|
|
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, vis, depth, 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;
|
|
}
|
|
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;
|
|
|
|
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);
|
|
free(data);
|
|
return 0;
|
|
}
|
|
|
|
/* ---------------------------------------------------------- more formats */
|
|
|
|
static unsigned
|
|
LE16(const unsigned char *p)
|
|
{
|
|
return (unsigned) (p[0] | (p[1] << 8));
|
|
}
|
|
|
|
static unsigned long
|
|
LE32(const unsigned char *p)
|
|
{
|
|
return (unsigned long) p[0] | ((unsigned long) p[1] << 8) |
|
|
((unsigned long) p[2] << 16) | ((unsigned long) p[3] << 24);
|
|
}
|
|
|
|
/* install a top-down RGB buffer as the document (owner keeps the buffer) */
|
|
static int
|
|
SetImageRGB(const unsigned char *rgb, int w, int h)
|
|
{
|
|
Pixmap np, ns;
|
|
XImage *im;
|
|
int x, y;
|
|
unsigned long *keys = NULL, *kpix = NULL;
|
|
int nkeys = 0;
|
|
|
|
if (w < 1 || h < 1 || w > 4096 || h > 4096)
|
|
return -1;
|
|
np = XCreatePixmap(dpy, RootWindowOfScreen(scr), (unsigned) w,
|
|
(unsigned) h, depth);
|
|
ns = XCreatePixmap(dpy, RootWindowOfScreen(scr), (unsigned) w,
|
|
(unsigned) h, depth);
|
|
if (np == None || ns == None) {
|
|
if (np != None)
|
|
XFreePixmap(dpy, np);
|
|
if (ns != None)
|
|
XFreePixmap(dpy, ns);
|
|
return -1;
|
|
}
|
|
if (pix != None)
|
|
XFreePixmap(dpy, pix);
|
|
if (snap != None)
|
|
XFreePixmap(dpy, snap);
|
|
pix = np;
|
|
snap = ns;
|
|
imgW = w;
|
|
imgH = h;
|
|
if (canvas != NULL)
|
|
XtVaSetValues(canvas, XmNwidth, imgW * zoom,
|
|
XmNheight, imgH * zoom, NULL);
|
|
XSetForeground(dpy, gc, bgPix);
|
|
XFillRectangle(dpy, pix, gc, 0, 0, (unsigned) w, (unsigned) h);
|
|
im = XCreateImage(dpy, vis, depth, ZPixmap, 0, NULL,
|
|
(unsigned) w, (unsigned) h, 32, 0);
|
|
if (im == NULL)
|
|
return -1;
|
|
im->data = calloc(1, (size_t) im->bytes_per_line * h);
|
|
if (im->data == NULL) {
|
|
XDestroyImage(im);
|
|
return -1;
|
|
}
|
|
if (truecolor) {
|
|
for (y = 0; y < h; y++)
|
|
for (x = 0; x < w; x++) {
|
|
const unsigned char *p =
|
|
rgb + ((size_t) y * w + x) * 3;
|
|
|
|
XPutPixel(im, x, y, RGBToPixel(p[0], p[1], p[2]));
|
|
}
|
|
} else {
|
|
keys = malloc(sizeof(unsigned long) * (size_t) w * h);
|
|
if (keys != NULL) {
|
|
int i;
|
|
|
|
for (i = 0; i < w * h; i++)
|
|
keys[i] = ((unsigned long) rgb[i * 3] << 16) |
|
|
((unsigned long) rgb[i * 3 + 1] << 8) |
|
|
rgb[i * 3 + 2];
|
|
qsort(keys, (size_t) (w * h), sizeof(unsigned long),
|
|
CmpUL);
|
|
nkeys = 0;
|
|
for (i = 0; i < w * h; i++)
|
|
if (i == 0 || keys[i] != keys[i - 1])
|
|
keys[nkeys++] = keys[i];
|
|
kpix = malloc(sizeof(unsigned long) * (size_t) nkeys);
|
|
if (kpix != NULL) {
|
|
unsigned char *krgb =
|
|
malloc((size_t) nkeys * 3);
|
|
|
|
if (krgb != NULL) {
|
|
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 < h; y++)
|
|
for (x = 0; x < w; x++) {
|
|
const unsigned char *p =
|
|
rgb + ((size_t) y * w + x) * 3;
|
|
unsigned long key =
|
|
((unsigned long) p[0] << 16) |
|
|
((unsigned long) p[1] << 8) | p[2];
|
|
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[0], p[1], p[2]);
|
|
}
|
|
XPutPixel(im, x, y, px);
|
|
}
|
|
free(keys);
|
|
free(kpix);
|
|
}
|
|
XPutImage(dpy, pix, gc, im, 0, 0, 0, 0, (unsigned) w, (unsigned) h);
|
|
XDestroyImage(im);
|
|
return 0;
|
|
}
|
|
|
|
/* 8-bit palette BMP, uncompressed or RLE8 (the 24-bit path is OpenBMP24) */
|
|
static int
|
|
LoadBMP8(FILE *f)
|
|
{
|
|
unsigned char fh[14], ih[40], pal[256][3], *raw, *rgb, *idx;
|
|
unsigned long off, row, size;
|
|
long w, h, hs;
|
|
int bpp, comp, ncolors, i, y, x, topdown = 0;
|
|
|
|
if (fread(fh, 1, 14, f) != 14 || fh[0] != 'B' || fh[1] != 'M')
|
|
return -1;
|
|
if (fread(ih, 1, 40, f) != 40)
|
|
return -1;
|
|
if (LE32(ih) != 40 || LE16(ih + 12) != 1)
|
|
return -1;
|
|
bpp = (int) LE16(ih + 14);
|
|
comp = (int) LE32(ih + 16);
|
|
if (bpp != 8 || (comp != 0 && comp != 1))
|
|
return -1;
|
|
w = (long) LE32(ih + 4);
|
|
hs = (long) (int) LE32(ih + 8);
|
|
if (hs < 0) {
|
|
topdown = 1;
|
|
h = -hs;
|
|
} else {
|
|
h = hs;
|
|
}
|
|
if (w < 1 || h < 1 || w > 4096 || h > 4096)
|
|
return -1;
|
|
off = LE32(fh + 10);
|
|
ncolors = (int) LE32(ih + 32);
|
|
if (ncolors == 0)
|
|
ncolors = 256;
|
|
if (ncolors > 256)
|
|
ncolors = 256;
|
|
memset(pal, 0, sizeof pal);
|
|
if (fseek(f, 14 + 40, SEEK_SET) != 0)
|
|
return -1;
|
|
for (i = 0; i < ncolors; i++) {
|
|
unsigned char e[4];
|
|
|
|
if (fread(e, 1, 4, f) != 4)
|
|
return -1;
|
|
pal[i][0] = e[2];
|
|
pal[i][1] = e[1];
|
|
pal[i][2] = e[0];
|
|
}
|
|
idx = calloc((size_t) w * h, 1);
|
|
if (idx == NULL)
|
|
return -1;
|
|
|
|
if (comp == 0) {
|
|
row = (((unsigned long) w * 8 + 31) / 32) * 4;
|
|
raw = malloc((size_t) row * h);
|
|
if (raw == NULL) {
|
|
free(idx);
|
|
return -1;
|
|
}
|
|
if (fseek(f, (long) off, SEEK_SET) != 0 ||
|
|
fread(raw, 1, (size_t) row * h, f) != (size_t) row * h) {
|
|
free(raw);
|
|
free(idx);
|
|
return -1;
|
|
}
|
|
for (y = 0; y < h; y++) {
|
|
unsigned char *src =
|
|
raw + (topdown ? y : (h - 1 - y)) * row;
|
|
|
|
memcpy(idx + (size_t) y * w, src, (size_t) w);
|
|
}
|
|
free(raw);
|
|
} else {
|
|
/* RLE8: count/value pairs, 00 00 end of line, 00 01 end of
|
|
* bitmap, 00 02 dx dy delta, 00 n literals (padded to word) */
|
|
FILE *g = f;
|
|
long beg, end;
|
|
unsigned char *rl;
|
|
size_t p = 0;
|
|
|
|
if (fseek(g, 0, SEEK_END) != 0 ||
|
|
(end = ftell(g)) < 0 ||
|
|
fseek(g, (long) off, SEEK_SET) != 0) {
|
|
free(idx);
|
|
return -1;
|
|
}
|
|
beg = (long) off;
|
|
size = (unsigned long) (end - beg);
|
|
if (size > 64UL * 1024 * 1024) {
|
|
free(idx);
|
|
return -1;
|
|
}
|
|
rl = malloc((size_t) size);
|
|
if (rl == NULL || fread(rl, 1, (size_t) size, g) != (size_t) size) {
|
|
free(rl);
|
|
free(idx);
|
|
return -1;
|
|
}
|
|
x = 0;
|
|
y = topdown ? 0 : (int) h - 1;
|
|
while (p + 1 < size) {
|
|
int cnt = rl[p], val = rl[p + 1];
|
|
|
|
p += 2;
|
|
if (cnt > 0) {
|
|
int k;
|
|
|
|
for (k = 0; k < cnt; k++) {
|
|
if (x < w && y >= 0 && y < h)
|
|
idx[(size_t) y * w + x] =
|
|
(unsigned char) val;
|
|
x++;
|
|
}
|
|
} else if (val == 0) {
|
|
x = 0;
|
|
y += topdown ? 1 : -1;
|
|
} else if (val == 1) {
|
|
break;
|
|
} else if (val == 2) {
|
|
int dx, dy;
|
|
|
|
if (p + 1 >= size)
|
|
break;
|
|
dx = rl[p++];
|
|
dy = rl[p++];
|
|
x += dx;
|
|
y += topdown ? dy : -dy;
|
|
} else {
|
|
int k;
|
|
|
|
for (k = 0; k < val && p < size; k++) {
|
|
if (x < w && y >= 0 && y < h)
|
|
idx[(size_t) y * w + x] = rl[p];
|
|
x++;
|
|
p++;
|
|
}
|
|
if (val & 1)
|
|
p++;
|
|
}
|
|
}
|
|
free(rl);
|
|
}
|
|
|
|
rgb = malloc((size_t) w * h * 3);
|
|
if (rgb == NULL) {
|
|
free(idx);
|
|
return -1;
|
|
}
|
|
for (i = 0; i < w * h; i++) {
|
|
unsigned char c = idx[i];
|
|
|
|
if (c >= ncolors) {
|
|
rgb[i * 3] = rgb[i * 3 + 1] = rgb[i * 3 + 2] = 0;
|
|
} else {
|
|
rgb[i * 3] = pal[c][0];
|
|
rgb[i * 3 + 1] = pal[c][1];
|
|
rgb[i * 3 + 2] = pal[c][2];
|
|
}
|
|
}
|
|
free(idx);
|
|
i = SetImageRGB(rgb, (int) w, (int) h);
|
|
free(rgb);
|
|
return i;
|
|
}
|
|
|
|
/* the first image of a Windows .ico (1, 4, 8, 24 or 32 bpp; PNG icons are
|
|
* refused because the program has no PNG decoder) */
|
|
static int
|
|
LoadICO(FILE *f)
|
|
{
|
|
unsigned char dir[6], ent[16], ih[40], pal[256][3], *raw, *rgb;
|
|
unsigned long off, row;
|
|
long w, h, hs;
|
|
int bpp, ncolors, i, y, x;
|
|
unsigned br, bg, bb;
|
|
|
|
if (fread(dir, 1, 6, f) != 6 || LE16(dir) != 0 || LE16(dir + 2) != 1)
|
|
return -1;
|
|
if (LE16(dir + 4) < 1)
|
|
return -1;
|
|
if (fread(ent, 1, 16, f) != 16)
|
|
return -1;
|
|
off = LE32(ent + 12);
|
|
if (off < 22)
|
|
return -1;
|
|
if (fseek(f, (long) off, SEEK_SET) != 0)
|
|
return -1;
|
|
if (fread(ih, 1, 40, f) != 40)
|
|
return -1;
|
|
if (ih[0] == 0x89 && ih[1] == 'P' && ih[2] == 'N' && ih[3] == 'G')
|
|
return -1;
|
|
if (LE32(ih) != 40 || LE16(ih + 12) != 1 || LE32(ih + 16) != 0)
|
|
return -1;
|
|
w = (long) LE32(ih + 4);
|
|
hs = (long) (int) LE32(ih + 8);
|
|
if (hs <= 0 || (hs % 2) != 0)
|
|
return -1;
|
|
h = hs / 2;
|
|
if (w < 1 || h < 1 || w > 4096 || h > 4096)
|
|
return -1;
|
|
bpp = (int) LE16(ih + 14);
|
|
if (bpp != 1 && bpp != 4 && bpp != 8 && bpp != 24 && bpp != 32)
|
|
return -1;
|
|
ncolors = 0;
|
|
if (bpp <= 8) {
|
|
ncolors = (int) LE32(ih + 32);
|
|
if (ncolors == 0)
|
|
ncolors = 1 << bpp;
|
|
if (ncolors > 256)
|
|
ncolors = 256;
|
|
}
|
|
memset(pal, 0, sizeof pal);
|
|
for (i = 0; i < ncolors; i++) {
|
|
unsigned char e[4];
|
|
|
|
if (fread(e, 1, 4, f) != 4)
|
|
return -1;
|
|
pal[i][0] = e[2];
|
|
pal[i][1] = e[1];
|
|
pal[i][2] = e[0];
|
|
}
|
|
row = (((unsigned long) w * bpp + 31) / 32) * 4;
|
|
raw = malloc((size_t) row * h);
|
|
if (raw == NULL)
|
|
return -1;
|
|
if (fread(raw, 1, (size_t) row * h, f) != (size_t) row * h) {
|
|
free(raw);
|
|
return -1;
|
|
}
|
|
rgb = malloc((size_t) w * h * 3);
|
|
if (rgb == NULL) {
|
|
free(raw);
|
|
return -1;
|
|
}
|
|
PixelToRGB(bgPix, &br, &bg, &bb);
|
|
for (y = 0; y < h; y++) {
|
|
unsigned char *src = raw + (h - 1 - y) * row;
|
|
|
|
for (x = 0; x < w; x++) {
|
|
unsigned char *d = rgb + ((size_t) y * w + x) * 3;
|
|
|
|
if (bpp == 32) {
|
|
unsigned a = src[x * 4 + 3];
|
|
|
|
d[0] = src[x * 4 + 2];
|
|
d[1] = src[x * 4 + 1];
|
|
d[2] = src[x * 4];
|
|
if (a == 0) {
|
|
d[0] = (unsigned char) br;
|
|
d[1] = (unsigned char) bg;
|
|
d[2] = (unsigned char) bb;
|
|
}
|
|
} else if (bpp == 24) {
|
|
d[0] = src[x * 3 + 2];
|
|
d[1] = src[x * 3 + 1];
|
|
d[2] = src[x * 3];
|
|
} else {
|
|
int idx;
|
|
|
|
if (bpp == 8)
|
|
idx = src[x];
|
|
else if (bpp == 4)
|
|
idx = (x & 1) ? (src[x / 2] & 15) :
|
|
(src[x / 2] >> 4);
|
|
else
|
|
idx = (src[x / 8] >> (7 - (x & 7))) & 1;
|
|
if (idx >= ncolors) {
|
|
d[0] = d[1] = d[2] = 0;
|
|
} else {
|
|
d[0] = pal[idx][0];
|
|
d[1] = pal[idx][1];
|
|
d[2] = pal[idx][2];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
free(raw);
|
|
i = SetImageRGB(rgb, (int) w, (int) h);
|
|
free(rgb);
|
|
return i;
|
|
}
|
|
|
|
/* Open a BMP (24 or 8 bit) or the first image of an ICO. */
|
|
static int
|
|
OpenBMP(const char *path)
|
|
{
|
|
FILE *f;
|
|
unsigned char m[6], hdr[54];
|
|
int bpp;
|
|
|
|
if ((f = fopen(path, "rb")) == NULL)
|
|
return -1;
|
|
if (fread(m, 1, 6, f) != 6) {
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
if (m[0] == 'B' && m[1] == 'M') {
|
|
fseek(f, 0, SEEK_SET);
|
|
if (fread(hdr, 1, 54, f) != 54 || LE32(hdr + 14) != 40) {
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
bpp = (int) LE16(hdr + 28);
|
|
if (bpp == 24) {
|
|
fclose(f);
|
|
return OpenBMP24(path);
|
|
}
|
|
if (bpp == 8) {
|
|
fseek(f, 0, SEEK_SET);
|
|
bpp = LoadBMP8(f);
|
|
fclose(f);
|
|
return bpp;
|
|
}
|
|
} else if (m[0] == 0 && m[1] == 0 && m[2] == 1 && m[3] == 0) {
|
|
fseek(f, 0, SEEK_SET);
|
|
bpp = LoadICO(f);
|
|
fclose(f);
|
|
return bpp;
|
|
}
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
|
|
/* --------------------------------------------------------------- drawing */
|
|
|
|
static void
|
|
SetFg(unsigned long color)
|
|
{
|
|
XSetForeground(dpy, gc, color);
|
|
}
|
|
|
|
static void
|
|
Snapshot(void)
|
|
{
|
|
XCopyArea(dpy, pix, snap, gc, 0, 0, imgW, imgH, 0, 0);
|
|
}
|
|
|
|
static void
|
|
RestoreSnap(void)
|
|
{
|
|
XCopyArea(dpy, snap, pix, gc, 0, 0, imgW, imgH, 0, 0);
|
|
}
|
|
|
|
static void
|
|
DrawDab(int x, int y, int radius, unsigned long 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, unsigned long color)
|
|
{
|
|
SetFg(color);
|
|
XDrawLine(dpy, pix, gc, x0, y0, x1, y1);
|
|
}
|
|
|
|
static void
|
|
DrawShape(int x0, int y0, int x1, int y1, unsigned long 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;
|
|
}
|
|
}
|
|
|
|
/* quadratic Bezier P0 -> control -> P1, sampled into short line segments */
|
|
static void
|
|
DrawCurve(int x0, int y0, int cx, int cy, int x1, int y1, unsigned long color)
|
|
{
|
|
double px = x0, py = y0;
|
|
int i;
|
|
|
|
SetFg(color);
|
|
for (i = 1; i <= 64; i++) {
|
|
double t = i / 64.0, u = 1.0 - t;
|
|
double x = u * u * x0 + 2.0 * u * t * cx + t * t * x1;
|
|
double y = u * u * y0 + 2.0 * u * t * cy + t * t * y1;
|
|
|
|
XDrawLine(dpy, pix, gc, (int) (px + 0.5), (int) (py + 0.5),
|
|
(int) (x + 0.5), (int) (y + 0.5));
|
|
px = x;
|
|
py = y;
|
|
}
|
|
}
|
|
|
|
/* the polygon so far plus a rubber line to the pointer, over the snapshot */
|
|
static void
|
|
DrawPolyPreview(int curx, int cury)
|
|
{
|
|
int i;
|
|
|
|
RestoreSnap();
|
|
SetFg(polyColor);
|
|
for (i = 1; i < polyN; i++)
|
|
XDrawLine(dpy, pix, gc, polyX[i - 1], polyY[i - 1],
|
|
polyX[i], polyY[i]);
|
|
if (polyN > 0)
|
|
XDrawLine(dpy, pix, gc, polyX[polyN - 1], polyY[polyN - 1],
|
|
curx, cury);
|
|
}
|
|
|
|
static void
|
|
ClosePolygon(void)
|
|
{
|
|
int i;
|
|
|
|
RestoreSnap();
|
|
SetFg(polyColor);
|
|
if (polyN >= 2) {
|
|
for (i = 1; i < polyN; i++)
|
|
XDrawLine(dpy, pix, gc, polyX[i - 1], polyY[i - 1],
|
|
polyX[i], polyY[i]);
|
|
XDrawLine(dpy, pix, gc, polyX[polyN - 1], polyY[polyN - 1],
|
|
polyX[0], polyY[0]);
|
|
dirty = 1;
|
|
}
|
|
polyActive = 0;
|
|
drawing = 0;
|
|
Redraw();
|
|
}
|
|
|
|
/* 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, unsigned long color)
|
|
{
|
|
XImage *im;
|
|
unsigned long old;
|
|
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 == color) {
|
|
XDestroyImage(im);
|
|
return;
|
|
}
|
|
stack = malloc(sizeof(*stack) * (size_t) cap);
|
|
if (stack == NULL) {
|
|
XDestroyImage(im);
|
|
return;
|
|
}
|
|
stack[sp].x = x;
|
|
stack[sp].y = y;
|
|
sp++;
|
|
while (sp > 0) {
|
|
int cx, cy, x1, x2, i, dy;
|
|
|
|
sp--;
|
|
cx = stack[sp].x;
|
|
cy = stack[sp].y;
|
|
if (XGetPixel(im, cx, cy) != old)
|
|
continue;
|
|
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);
|
|
XDestroyImage(im);
|
|
}
|
|
|
|
static void
|
|
InvertImage(void)
|
|
{
|
|
XImage *im;
|
|
int x, y;
|
|
|
|
if ((im = XGetImage(dpy, pix, 0, 0, imgW, imgH, AllPlanes, ZPixmap)) == NULL)
|
|
return;
|
|
if (truecolor) {
|
|
for (y = 0; y < imgH; y++)
|
|
for (x = 0; x < imgW; x++) {
|
|
unsigned r, g, b;
|
|
|
|
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);
|
|
}
|
|
|
|
static void
|
|
FlipHorizontal(void)
|
|
{
|
|
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);
|
|
for (x = 0; x < imgW; x++)
|
|
XCopyArea(dpy, tmp, pix, gc, x, 0, 1, imgH, imgW - 1 - x, 0);
|
|
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 * 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
|
|
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)
|
|
{
|
|
Deselect();
|
|
XSetForeground(dpy, gc, bgPix);
|
|
XFillRectangle(dpy, pix, gc, 0, 0, imgW, imgH);
|
|
Snapshot();
|
|
dirty = 0;
|
|
curPath[0] = '\0';
|
|
SetTitle();
|
|
Redraw();
|
|
}
|
|
|
|
static void
|
|
ClearImage(void)
|
|
{
|
|
Deselect();
|
|
Snapshot();
|
|
XSetForeground(dpy, gc, bgPix);
|
|
XFillRectangle(dpy, pix, gc, 0, 0, imgW, imgH);
|
|
dirty = 1;
|
|
Redraw();
|
|
}
|
|
|
|
/* ----------------------------------------------------------------- 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)
|
|
{
|
|
char buf[96];
|
|
XmString s;
|
|
|
|
snprintf(buf, sizeof buf, "%d,%d", x, y);
|
|
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);
|
|
}
|
|
|
|
/* ------------------------------------------------------------- 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)
|
|
{
|
|
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) w; (void) cd;
|
|
if (c->reason != XmCR_EXPOSE)
|
|
return;
|
|
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);
|
|
DrawMarquee();
|
|
}
|
|
|
|
static void
|
|
CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep)
|
|
{
|
|
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;
|
|
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)
|
|
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;
|
|
StatusPos(x, y);
|
|
drawing = 1;
|
|
switch (tool) {
|
|
case T_FILL:
|
|
Snapshot();
|
|
FloodFill(x, y, color);
|
|
dirty = 1;
|
|
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);
|
|
|
|
if (ev->xbutton.button == Button3)
|
|
bgPix = p;
|
|
else
|
|
fgPix = p;
|
|
ShowColors();
|
|
XDestroyImage(im);
|
|
}
|
|
}
|
|
drawing = 0;
|
|
break;
|
|
case T_ERASER:
|
|
Snapshot();
|
|
DrawDab(x, y, 4, bgPix);
|
|
dirty = 1;
|
|
break;
|
|
case T_BRUSH:
|
|
Snapshot();
|
|
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);
|
|
XDrawPoint(dpy, pix, gc, x, y);
|
|
dirty = 1;
|
|
break;
|
|
case T_CURVE:
|
|
if (curveState == 2) {
|
|
/* the preview is the result */
|
|
curveState = 0;
|
|
drawing = 0;
|
|
dirty = 1;
|
|
Redraw();
|
|
} else if (curveState == 0) {
|
|
Snapshot();
|
|
curveX0 = curveX1 = x;
|
|
curveY0 = curveY1 = y;
|
|
curveColor = color;
|
|
curveState = 1;
|
|
drawing = 1;
|
|
} else {
|
|
drawing = 0;
|
|
}
|
|
break;
|
|
case T_POLYGON:
|
|
if (polyActive && ev->xbutton.button == Button3) {
|
|
ClosePolygon();
|
|
break;
|
|
}
|
|
if (ev->xbutton.button != Button1) {
|
|
drawing = 0;
|
|
break;
|
|
}
|
|
if (!polyActive) {
|
|
Snapshot();
|
|
polyActive = 1;
|
|
polyN = 0;
|
|
polyColor = color;
|
|
} else if (ev->xbutton.time - polyLastTime < 300 &&
|
|
abs(x - polyLastX) < 4 && abs(y - polyLastY) < 4) {
|
|
ClosePolygon();
|
|
break;
|
|
}
|
|
if (polyN < POLY_MAX) {
|
|
polyX[polyN] = x;
|
|
polyY[polyN] = y;
|
|
polyN++;
|
|
}
|
|
polyLastX = x;
|
|
polyLastY = y;
|
|
polyLastTime = ev->xbutton.time;
|
|
DrawPolyPreview(x, y);
|
|
drawing = 0;
|
|
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 / zoom;
|
|
y = ev->xmotion.y / zoom;
|
|
StatusPos(x, y);
|
|
if (!drawing && !(tool == T_CURVE && curveState == 2) &&
|
|
!(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:
|
|
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, bgPix);
|
|
dirty = 1;
|
|
break;
|
|
case T_AIRBRUSH:
|
|
Spray(x, y, color);
|
|
dirty = 1;
|
|
break;
|
|
case T_CURVE:
|
|
RestoreSnap();
|
|
if (curveState == 1)
|
|
DrawLineTo(curveX0, curveY0, x, y, curveColor);
|
|
else if (curveState == 2)
|
|
DrawCurve(curveX0, curveY0, x, y, curveX1,
|
|
curveY1, curveColor);
|
|
break;
|
|
case T_POLYGON:
|
|
if (polyActive)
|
|
DrawPolyPreview(x, y);
|
|
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;
|
|
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;
|
|
if (x < 0) x = 0;
|
|
if (y < 0) y = 0;
|
|
if (x >= imgW) x = imgW - 1;
|
|
if (y >= imgH) y = imgH - 1;
|
|
curveX1 = x;
|
|
curveY1 = y;
|
|
curveState = 2;
|
|
}
|
|
drawing = 0;
|
|
Redraw();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void
|
|
ToolChanged(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
(void) call;
|
|
if (!XmToggleButtonGetState(w))
|
|
return;
|
|
if (curveState != 0) {
|
|
RestoreSnap();
|
|
curveState = 0;
|
|
}
|
|
if (polyActive) {
|
|
RestoreSnap();
|
|
polyActive = 0;
|
|
}
|
|
tool = (int) (long) data;
|
|
Redraw();
|
|
}
|
|
|
|
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)
|
|
fgPix = palette[idx].pixel;
|
|
else if (ev->xbutton.button == Button3)
|
|
bgPix = palette[idx].pixel;
|
|
ShowColors();
|
|
}
|
|
|
|
static void
|
|
SwatchActivate(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
(void) w; (void) call;
|
|
fgPix = palette[(int) (long) data].pixel;
|
|
ShowColors();
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ menus */
|
|
|
|
enum {
|
|
C_NEW, C_OPEN, C_SAVE, C_SAVEAS, C_PRINT, C_EXIT,
|
|
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,
|
|
C_ATTR, C_EDITCOLORS
|
|
};
|
|
|
|
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
|
|
ShowText(const char *body, int titleId, const char *titleFb)
|
|
{
|
|
Widget d = XmCreateInformationDialog(top, "info", NULL, 0);
|
|
XmString s = XmStringCreateLocalized((String) body);
|
|
|
|
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);
|
|
}
|
|
|
|
static void
|
|
ShowInfo(int bodyId, const char *bodyFb, int titleId, const char *titleFb)
|
|
{
|
|
ShowText(PaintMsg(bodyId, bodyFb), titleId, titleFb);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- printing */
|
|
|
|
static McdeSysJob *printJob;
|
|
static char printPath[256];
|
|
|
|
static void
|
|
PrintDone(XtPointer cd, int *fd, XtInputId *id)
|
|
{
|
|
McdeSysJob *job = (McdeSysJob *) cd;
|
|
int rc = mcde_sys_job_finish(job);
|
|
|
|
(void) fd;
|
|
XtRemoveInput(*id);
|
|
if (rc != MCDE_SYS_OK)
|
|
ShowText(mcde_sys_error(), 51, "Print");
|
|
unlink(printPath);
|
|
printJob = NULL;
|
|
}
|
|
|
|
static void
|
|
PrintImage(void)
|
|
{
|
|
McdeSysPrinter list[32];
|
|
const char *printer = NULL;
|
|
int n, i;
|
|
|
|
if (printJob != NULL)
|
|
return;
|
|
n = mcde_sys_printers(list, 32);
|
|
if (n <= 0) {
|
|
ShowInfo(50, "No printer is set up.", 51, "Print");
|
|
return;
|
|
}
|
|
for (i = 0; i < n; i++)
|
|
if (list[i].is_default) {
|
|
printer = list[i].name;
|
|
break;
|
|
}
|
|
if (printer == NULL)
|
|
printer = list[0].name;
|
|
snprintf(printPath, sizeof printPath, "/tmp/dtpaint-%d.bmp",
|
|
(int) getpid());
|
|
if (SaveBMP(printPath) != 0) {
|
|
ShowError(27, printPath);
|
|
return;
|
|
}
|
|
printJob = mcde_sys_print_file(printPath, printer);
|
|
if (printJob == NULL) {
|
|
unlink(printPath);
|
|
ShowText(mcde_sys_error(), 51, "Print");
|
|
return;
|
|
}
|
|
XtAppAddInput(appctx, mcde_sys_job_fd(printJob),
|
|
(XtPointer) XtInputReadMask, PrintDone, printJob);
|
|
}
|
|
|
|
/* ------------------------------------------------- image size and colours */
|
|
|
|
static void
|
|
ResizeImage(int nw, int nh)
|
|
{
|
|
Pixmap np, ns;
|
|
|
|
if (nw < 1 || nh < 1 || nw > 4096 || nh > 4096)
|
|
return;
|
|
np = XCreatePixmap(dpy, RootWindowOfScreen(scr), (unsigned) nw,
|
|
(unsigned) nh, depth);
|
|
ns = XCreatePixmap(dpy, RootWindowOfScreen(scr), (unsigned) nw,
|
|
(unsigned) nh, depth);
|
|
if (np == None || ns == None) {
|
|
if (np != None)
|
|
XFreePixmap(dpy, np);
|
|
if (ns != None)
|
|
XFreePixmap(dpy, ns);
|
|
return;
|
|
}
|
|
Deselect();
|
|
XSetForeground(dpy, gc, bgPix);
|
|
XFillRectangle(dpy, np, gc, 0, 0, (unsigned) nw, (unsigned) nh);
|
|
XCopyArea(dpy, pix, np, gc, 0, 0,
|
|
(unsigned) (imgW < nw ? imgW : nw),
|
|
(unsigned) (imgH < nh ? imgH : nh), 0, 0);
|
|
XFreePixmap(dpy, pix);
|
|
XFreePixmap(dpy, snap);
|
|
pix = np;
|
|
snap = ns;
|
|
imgW = nw;
|
|
imgH = nh;
|
|
XCopyArea(dpy, pix, snap, gc, 0, 0, imgW, imgH, 0, 0);
|
|
dirty = 1;
|
|
SetCanvasSize();
|
|
Redraw();
|
|
}
|
|
|
|
static void
|
|
AttrOk(Widget w, XtPointer cd, XtPointer call)
|
|
{
|
|
char *sw = XmTextFieldGetString(attrW);
|
|
char *sh = XmTextFieldGetString(attrH);
|
|
long nw = (sw != NULL) ? strtol(sw, NULL, 10) : 0;
|
|
long nh = (sh != NULL) ? strtol(sh, NULL, 10) : 0;
|
|
|
|
(void) cd; (void) call;
|
|
if (sw != NULL)
|
|
XtFree(sw);
|
|
if (sh != NULL)
|
|
XtFree(sh);
|
|
if (nw < 1 || nw > 4096 || nh < 1 || nh > 4096) {
|
|
ShowInfo(42, "Width and height must be between 1 and 4096.",
|
|
43, "Attributes");
|
|
return;
|
|
}
|
|
ResizeImage((int) nw, (int) nh);
|
|
DestroyDialog(w, NULL, NULL);
|
|
}
|
|
|
|
static void
|
|
AttrDialog(void)
|
|
{
|
|
Widget form = XmCreateFormDialog(top, "attributes", NULL, 0);
|
|
Widget shell, lw, lh, buttons, ok, cancel;
|
|
XmString s;
|
|
char buf[32];
|
|
|
|
if (form == NULL)
|
|
return;
|
|
shell = XtParent(form);
|
|
XtVaSetValues(shell,
|
|
XmNtitle, (String) PaintMsg(43, "Attributes"),
|
|
XmNwidth, 280, XmNheight, 190,
|
|
XmNdeleteResponse, XmDESTROY,
|
|
XmNdialogStyle, XmDIALOG_APPLICATION_MODAL, NULL);
|
|
|
|
lw = XmCreateLabel(form, "widthLabel", NULL, 0);
|
|
s = Msg(40, "Width:");
|
|
XtVaSetValues(lw, XmNlabelString, s,
|
|
XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM,
|
|
XmNtopOffset, 12, XmNleftOffset, 10, NULL);
|
|
XmStringFree(s);
|
|
XtManageChild(lw);
|
|
|
|
attrW = XmCreateTextField(form, "width", NULL, 0);
|
|
snprintf(buf, sizeof buf, "%d", imgW);
|
|
XmTextFieldSetString(attrW, buf);
|
|
XtVaSetValues(attrW,
|
|
XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_WIDGET,
|
|
XmNleftWidget, lw, XmNrightAttachment, XmATTACH_FORM,
|
|
XmNtopOffset, 8, XmNleftOffset, 8, XmNrightOffset, 10, NULL);
|
|
XtManageChild(attrW);
|
|
|
|
lh = XmCreateLabel(form, "heightLabel", NULL, 0);
|
|
s = Msg(41, "Height:");
|
|
XtVaSetValues(lh, XmNlabelString, s,
|
|
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, attrW,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNtopOffset, 10, XmNleftOffset, 10, NULL);
|
|
XmStringFree(s);
|
|
XtManageChild(lh);
|
|
|
|
attrH = XmCreateTextField(form, "height", NULL, 0);
|
|
snprintf(buf, sizeof buf, "%d", imgH);
|
|
XmTextFieldSetString(attrH, buf);
|
|
XtVaSetValues(attrH,
|
|
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, attrW,
|
|
XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, lh,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNtopOffset, 6, XmNleftOffset, 8, XmNrightOffset, 10, NULL);
|
|
XtManageChild(attrH);
|
|
|
|
buttons = XmCreateRowColumn(form, "buttons", NULL, 0);
|
|
XtVaSetValues(buttons, XmNorientation, XmHORIZONTAL,
|
|
XmNpacking, XmPACK_TIGHT, XmNspacing, 8,
|
|
XmNbottomAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNbottomOffset, 10, XmNrightOffset, 10, NULL);
|
|
ok = XmCreatePushButton(buttons, "ok", NULL, 0);
|
|
XtAddCallback(ok, XmNactivateCallback, AttrOk, NULL);
|
|
s = Msg(48, "OK");
|
|
XtVaSetValues(ok, XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
XtManageChild(ok);
|
|
cancel = XmCreatePushButton(buttons, "cancel", NULL, 0);
|
|
XtAddCallback(cancel, XmNactivateCallback, DestroyDialog, NULL);
|
|
s = Msg(49, "Cancel");
|
|
XtVaSetValues(cancel, XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
XtManageChild(cancel);
|
|
XtManageChild(buttons);
|
|
XtVaSetValues(form, XmNwidth, 280, XmNheight, 190,
|
|
XmNresizePolicy, XmRESIZE_NONE, NULL);
|
|
XtManageChild(form);
|
|
XtPopup(shell, XtGrabNone);
|
|
XtVaSetValues(shell, XmNwidth, 280, XmNheight, 190, NULL);
|
|
}
|
|
|
|
static void
|
|
ColorOk(Widget w, XtPointer cd, XtPointer call)
|
|
{
|
|
int r, g, b;
|
|
|
|
(void) cd; (void) call;
|
|
XmScaleGetValue(colRS, &r);
|
|
XmScaleGetValue(colGS, &g);
|
|
XmScaleGetValue(colBS, &b);
|
|
fgPix = RGBToPixel((unsigned) r, (unsigned) g, (unsigned) b);
|
|
ShowColors();
|
|
DestroyDialog(w, NULL, NULL);
|
|
}
|
|
|
|
static Widget
|
|
ColorRow(Widget parent, const char *name, const char *label, int value)
|
|
{
|
|
Widget row = XmCreateRowColumn(parent, (char *) name, NULL, 0);
|
|
Widget lab, sc;
|
|
XmString s = XmStringCreateLocalized((String) label);
|
|
|
|
XtVaSetValues(row, XmNorientation, XmHORIZONTAL, XmNspacing, 6,
|
|
XmNpacking, XmPACK_TIGHT, NULL);
|
|
lab = XmCreateLabel(row, "label", NULL, 0);
|
|
XtVaSetValues(lab, XmNlabelString, s, XmNwidth, 55,
|
|
XmNrecomputeSize, False, NULL);
|
|
XmStringFree(s);
|
|
XtManageChild(lab);
|
|
sc = XmCreateScale(row, "scale", NULL, 0);
|
|
XtVaSetValues(sc, XmNminimum, 0, XmNmaximum, 255, XmNvalue, value,
|
|
XmNorientation, XmHORIZONTAL, XmNshowValue, True,
|
|
XmNwidth, 220, NULL);
|
|
XtManageChild(sc);
|
|
XtManageChild(row);
|
|
return sc;
|
|
}
|
|
|
|
static void
|
|
ColorDialog(void)
|
|
{
|
|
Widget form = XmCreateFormDialog(top, "editcolors", NULL, 0);
|
|
Widget shell, rc, buttons, ok, cancel;
|
|
XmString s;
|
|
unsigned r, g, b;
|
|
|
|
if (form == NULL)
|
|
return;
|
|
shell = XtParent(form);
|
|
XtVaSetValues(shell,
|
|
XmNtitle, (String) PaintMsg(44, "Edit Colors"),
|
|
XmNwidth, 330, XmNheight, 230,
|
|
XmNdeleteResponse, XmDESTROY,
|
|
XmNdialogStyle, XmDIALOG_APPLICATION_MODAL, NULL);
|
|
PixelToRGB(fgPix, &r, &g, &b);
|
|
|
|
rc = XmCreateRowColumn(form, "rc", NULL, 0);
|
|
XtVaSetValues(rc, XmNorientation, XmVERTICAL, XmNspacing, 8,
|
|
XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNtopOffset, 12, XmNleftOffset, 12, XmNrightOffset, 12, NULL);
|
|
colRS = ColorRow(rc, "red", PaintMsg(45, "Red:"), (int) r);
|
|
colGS = ColorRow(rc, "green", PaintMsg(46, "Green:"), (int) g);
|
|
colBS = ColorRow(rc, "blue", PaintMsg(47, "Blue:"), (int) b);
|
|
|
|
buttons = XmCreateRowColumn(rc, "buttons", NULL, 0);
|
|
XtVaSetValues(buttons, XmNorientation, XmHORIZONTAL,
|
|
XmNpacking, XmPACK_TIGHT, XmNspacing, 8, NULL);
|
|
ok = XmCreatePushButton(buttons, "ok", NULL, 0);
|
|
XtAddCallback(ok, XmNactivateCallback, ColorOk, NULL);
|
|
s = Msg(48, "OK");
|
|
XtVaSetValues(ok, XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
XtManageChild(ok);
|
|
cancel = XmCreatePushButton(buttons, "cancel", NULL, 0);
|
|
XtAddCallback(cancel, XmNactivateCallback, DestroyDialog, NULL);
|
|
s = Msg(49, "Cancel");
|
|
XtVaSetValues(cancel, XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
XtManageChild(cancel);
|
|
XtManageChild(buttons);
|
|
XtManageChild(rc);
|
|
XtVaSetValues(form, XmNwidth, 330, XmNheight, 230,
|
|
XmNresizePolicy, XmRESIZE_NONE, NULL);
|
|
XtManageChild(form);
|
|
XtPopup(shell, XtGrabNone);
|
|
XtVaSetValues(shell, XmNwidth, 330, XmNheight, 230, NULL);
|
|
}
|
|
|
|
/* 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) {
|
|
/* C_SAVE passes curPath itself; snprintf onto the same buffer
|
|
* is undefined and emptied it (the title fell back to untitled) */
|
|
if (path != curPath)
|
|
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)
|
|
{
|
|
(void) w; (void) call;
|
|
switch ((int) (long) data) {
|
|
case C_NEW:
|
|
MaybeDiscard(A_NEW);
|
|
break;
|
|
case C_OPEN:
|
|
MaybeDiscard(A_OPEN);
|
|
break;
|
|
case C_SAVE:
|
|
if (curPath[0])
|
|
DoSave(curPath);
|
|
else
|
|
FileDialog(1);
|
|
break;
|
|
case C_SAVEAS:
|
|
FileDialog(1);
|
|
break;
|
|
case C_PRINT:
|
|
PrintImage();
|
|
break;
|
|
case C_EXIT:
|
|
MaybeDiscard(A_EXIT);
|
|
break;
|
|
case C_UNDO:
|
|
Deselect();
|
|
RestoreSnap();
|
|
dirty = 1;
|
|
Redraw();
|
|
break;
|
|
case C_CLEAR:
|
|
ClearImage();
|
|
break;
|
|
case C_CLEARSEL:
|
|
ClearSelection();
|
|
break;
|
|
case C_SELALL:
|
|
SelectAll();
|
|
break;
|
|
case C_INVERT:
|
|
Snapshot();
|
|
InvertImage();
|
|
dirty = 1;
|
|
Redraw();
|
|
break;
|
|
case C_FLIP:
|
|
Snapshot();
|
|
FlipHorizontal();
|
|
dirty = 1;
|
|
Redraw();
|
|
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_ZNORMAL:
|
|
SetZoom(1);
|
|
break;
|
|
case C_ZLARGE:
|
|
SetZoom(4);
|
|
break;
|
|
case C_ATTR:
|
|
AttrDialog();
|
|
break;
|
|
case C_EDITCOLORS:
|
|
ColorDialog();
|
|
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:
|
|
ShowInfo(25, "dtpaint - MCDE", 22, "About dtpaint");
|
|
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) {
|
|
DoSave(path);
|
|
} else {
|
|
if (OpenBMP(path) == 0) {
|
|
snprintf(curPath, sizeof curPath, "%s", path);
|
|
dirty = 0;
|
|
Deselect();
|
|
Snapshot();
|
|
SetTitle();
|
|
Redraw();
|
|
} else {
|
|
ShowError(26, path);
|
|
}
|
|
}
|
|
XtFree(path);
|
|
}
|
|
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);
|
|
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((char *) name, xmCascadeButtonWidgetClass, bar,
|
|
XmNsubMenuId, shell, NULL);
|
|
return shell;
|
|
}
|
|
|
|
static Widget
|
|
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 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);
|
|
|
|
XtVaSetValues(b, XmNacceleratorText, s, NULL);
|
|
XmStringFree(s);
|
|
}
|
|
return b;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ icons */
|
|
|
|
static void
|
|
Glyph(Display *d, Pixmap p, int t, unsigned long bg)
|
|
{
|
|
XGCValues gv;
|
|
GC k;
|
|
|
|
gv.foreground = bg;
|
|
k = XCreateGC(d, p, GCForeground, &gv);
|
|
XFillRectangle(d, p, k, 0, 0, 16, 16);
|
|
XSetForeground(d, k, BlackPixelOfScreen(scr));
|
|
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, "");
|
|
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)
|
|
selftest = 1;
|
|
|
|
top = XtVaAppInitialize(&app, "Dtpaint", NULL, 0, &argc, argv, NULL, NULL);
|
|
appctx = app;
|
|
dpy = XtDisplay(top);
|
|
(void) srand((unsigned) getpid());
|
|
InitColor();
|
|
|
|
for (i = 0; i < PAL_N; i++) {
|
|
XColor c;
|
|
|
|
snprintf(hex, sizeof hex, "#%s", palHex[i]);
|
|
if (!XParseColor(dpy, cmap, hex, &c) ||
|
|
!XAllocColor(dpy, cmap, &c)) {
|
|
c.pixel = (i == 0) ? BlackPixelOfScreen(scr) : WhitePixelOfScreen(scr);
|
|
}
|
|
palette[i] = c;
|
|
}
|
|
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) {
|
|
char tmp[128];
|
|
XImage *a, *b;
|
|
int x, y, ok = 1;
|
|
|
|
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);
|
|
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", "Ctrl+N", C_NEW, 0, True);
|
|
AddItem(m, PaintMsg(8, "Open..."), "Ctrl<Key>O", "Ctrl+O", C_OPEN, 0, True);
|
|
AddItem(m, PaintMsg(9, "Save"), "Ctrl<Key>S", "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(53, "Print..."), "Ctrl<Key>P", "Ctrl+P",
|
|
C_PRINT, 0, True);
|
|
AddItem(m, PaintMsg(11, "Exit"), "Alt<Key>F4", "Alt+F4", C_EXIT, 0, True);
|
|
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);
|
|
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);
|
|
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);
|
|
AddItem(m, PaintMsg(19, "Clear Image"), "Ctrl Shift<Key>N", "Ctrl+Shift+N", C_CLEAR, 0, True);
|
|
AddItem(m, NULL, NULL, NULL, 0, 1, True);
|
|
AddItem(m, PaintMsg(39, "Attributes..."), "Ctrl<Key>E", "Ctrl+E", C_ATTR, 0, True);
|
|
m = AddMenu(menubarW, PaintMsg(5, "Colors"));
|
|
AddItem(m, PaintMsg(20, "Edit Colors..."), NULL, NULL, C_EDITCOLORS, 0, True);
|
|
m = AddMenu(menubarW, PaintMsg(6, "Help"));
|
|
AddItem(m, PaintMsg(33, "Help Topics"), "Ctrl<Key>F1", "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, 920, XmNheight, 560, 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, RootWindowOfScreen(scr), 16, 16, depth);
|
|
|
|
Glyph(dpy, p, i, toolbg);
|
|
b = XtVaCreateManagedWidget(toolNames[i], xmToggleButtonWidgetClass,
|
|
toolbox, XmNlabelType, XmPIXMAP, XmNlabelPixmap, p,
|
|
XmNindicatorOn, False, XmNshadowThickness, 1,
|
|
XmNmarginWidth, 1, XmNmarginHeight, 1,
|
|
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: 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,
|
|
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, 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, empty, NULL);
|
|
XmStringFree(empty);
|
|
XtAddCallback(b, XmNactivateCallback,
|
|
SwatchActivate, (XtPointer) (long) idx);
|
|
XtAddEventHandler(b, ButtonPressMask, False,
|
|
SwatchPress, (XtPointer) (long) idx);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* 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);
|
|
{
|
|
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, XmNrecomputeSize, False,
|
|
XmNwidth, 90, NULL);
|
|
{
|
|
XmString s = XmStringCreateLocalized("0,0");
|
|
|
|
XtVaSetValues(statusPos, XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
}
|
|
|
|
scroll = XtVaCreateManagedWidget("scroll", xmScrolledWindowWidgetClass,
|
|
form, XmNtopAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, leftcol,
|
|
XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, statusHelp,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNscrollingPolicy, XmAUTOMATIC,
|
|
XmNscrollBarDisplayPolicy, XmAS_NEEDED, NULL);
|
|
canvas = XtVaCreateManagedWidget("canvas", xmDrawingAreaWidgetClass,
|
|
scroll, XmNwidth, imgW * zoom, XmNheight, imgH * zoom, NULL);
|
|
XtAddCallback(canvas, XmNexposeCallback, CanvasExpose, NULL);
|
|
XtAddEventHandler(canvas, ButtonPressMask | ButtonReleaseMask |
|
|
PointerMotionMask | ButtonMotionMask, False, CanvasInput, NULL);
|
|
|
|
SetTitle();
|
|
XtRealizeWidget(top);
|
|
NewImage();
|
|
XtAppMainLoop(app);
|
|
return 0;
|
|
}
|