gfxview.c 50.9 KB
Newer Older
1 2 3
/* $Id$ $Revision$ */
/* vim:set shiftwidth=4 ts=8: */

ellson's avatar
ellson committed
4 5 6 7 8 9 10 11 12
/*************************************************************************
 * Copyright (c) 2011 AT&T Intellectual Property 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: See CVS logs. Details at http://www.graphviz.org/
 *************************************************************************/
13

14
/* Lefteris Koutsofios - AT&T Labs Research */
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

#include "common.h"
#include "g.h"
#include "mem.h"
#include "io.h"
#include "code.h"
#include "tbl.h"
#include "parse.h"
#include "exec.h"
#include "gfxview.h"

#define max(a, b) (((a) >= (b)) ? (a) : (b))
#define min(a, b) (((a) >= (b)) ? (b) : (a))

#define getwintnset(to) \
    if (getint (to, &wattrp[wattri].u.i) != -1) wattri++;
#define getwpointnset(to) \
    if (getxy (to, &wattrp[wattri].u.p) != -1) wattri++;
#define getwsizenset(to) \
    if (getxy (to, &wattrp[wattri].u.s) != -1) wattri++;
#define getwrectnset(to) \
    if (getrect (to, &wattrp[wattri].u.r) != -1) wattri++;
#define getwstrnset(to) \
    if (getstr (to, &wattrp[wattri].u.t) != -1) wattri++;
#define getwcolornset(ko, vo) \
    if (getcolor (ko, vo, &wattrp[wattri].u.c) != -1) wattri++;

#define getaintnset(to, n, flag) \
    if (to) { \
        getint (to, &n), dp->flags |= flag; \
    }
#define getastrnset(to, s, flag) \
    if (to) \
        getstr (to, &s), dp->flags |= flag;

typedef struct colorname_t {
    char *name;
    unsigned char r, g, b;
} colorname_t;

#include "colors.txt"

typedef struct gfxrect_t {
    struct gfxrect_t *next;
    Tobj ko;
    Grect_t r;
} gfxrect_t;
typedef struct gfxmenu_t {
    struct gfxmenu_t *next;
    Tobj ko;
    long time;
    int mi;
} gfxmenu_t;
#define LISTSIZE 100
typedef struct gfxnode_t {
    int inuse;
    int wi;
72 73 74
    Tobj plvo, pmvo, prvo, pb3vo, pb4vo, pkvo[256];
    Gpoint_t plp, pmp, prp, pb3p, pb4p, pkp[256];
    char ls, ms, rs, b3s, b4s, ks[256];
75 76 77 78 79 80 81 82 83 84 85
    struct gfxrect_t *rect[LISTSIZE];
    struct gfxmenu_t *menu[LISTSIZE];
} gfxnode_t;
#define GFXNODEINCR 20
#define GFXNODESIZE sizeof (gfxnode_t)
static gfxnode_t *gfxnodes;
static int gfxnoden;

#define ISAWIDGET(wi) (wi >= 0 && wi < Gwidgetn && Gwidgets[wi].inuse)
#define ISALABEL(wi) (Gwidgets[wi].type == G_LABELWIDGET)
#define ISACANVAS(wi) (Gwidgets[wi].type == G_CANVASWIDGET)
86 87 88 89
#define ISACANVAS2(wi) ( \
    Gwidgets[wi].type == G_CANVASWIDGET || \
    Gwidgets[wi].type == G_PCANVASWIDGET \
)
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#define NODEID(wi) Gwidgets[wi].udata
#define ISABITMAP(bi) (bi >= 0 && bi < Gbitmapn && Gbitmaps[bi].inuse)

static Gpoint_t *gpp = NULL;
static long gpn = 0;
#define GPINCR 100
#define GPSIZE sizeof (Gpoint_t)

static Gwattr_t *wattrp = NULL;
static int wattrn = 0;
static int wattri = 0;
#define WATTRINCR 10
#define WATTRSIZE sizeof (Gwattr_t)

static Tobj rootwo, rootbo;

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static void nodeinit (int);
static void nodeterm (int);
static void rectinit (int);
static void rectterm (int);
static void rectinsert (int, Tobj, Grect_t);
static void rectmerge (int, Tobj, Grect_t);
static Tobj rectfind (int, Gpoint_t);
static void rectdelete (int, Tobj);
static void rectprune (int);
static void menuinsert (int, Tobj, long, int);
static int menufind (int, Tobj, long);
static void menuprune (int);
static int scanhsv (char *, float *, float *, float *);
static void hsv2rgb (float, float, float, Gcolor_t *);

static int getwattr (Tobj, int *);
static int getgattr (Tobj, Ggattr_t *);
static int getrect (Tobj, Grect_t *);
static int getxy (Tobj, Gxy_t *);
static int getint (Tobj, int *);
static int getdouble (Tobj, double *);
static int getstr (Tobj, char **);
static int getcolor (Tobj, Tobj, Gcolor_t *);

void GFXinit (void) {
131 132
    int ni;

133 134 135
    Tinss (root, "widgets", (rootwo = Ttable (100)));
    Tinss (root, "bitmaps", (rootbo = Ttable (100)));
    gfxnodes = Marrayalloc ((long) GFXNODEINCR * GFXNODESIZE);
136 137
    gfxnoden = GFXNODEINCR;
    for (ni = 0; ni < gfxnoden; ni++)
138
        gfxnodes[ni].inuse = FALSE;
139
    ni = 0;
140
    gpp = Marrayalloc ((long) GPINCR * GPSIZE);
141
    gpn = GPINCR;
142
    wattrp = Marrayalloc ((long) WATTRINCR * WATTRSIZE);
143 144 145
    wattrn = WATTRINCR, wattri = 0;
}

146
void GFXterm (void) {
147 148
    int ni;

149 150
    Marrayfree (wattrp), wattrp = NULL, wattrn = 0;
    Marrayfree (gpp), gpp = NULL, gpn = 0;
151
    for (ni = 0; ni < gfxnoden; ni++) {
152 153 154 155 156
        if (gfxnodes[ni].inuse) {
            Gdestroywidget (gfxnodes[ni].wi);
            nodeterm (ni);
            gfxnodes[ni].inuse = FALSE;
        }
157
    }
158 159 160
    Marrayfree (gfxnodes), gfxnodes = NULL, gfxnoden = 0;
    Tdels (root, "bitmaps");
    Tdels (root, "widgets");
161 162 163
}

/* callback for mem.c module - removes dead objects from gfxnodes */
164
void GFXprune (void) {
165 166 167 168
    gfxnode_t *np;
    int ni, ki;

    for (ni = 0; ni < gfxnoden; ni++) {
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        np = &gfxnodes[ni];
        if (np->inuse) {
            rectprune (ni), menuprune (ni);
            if (np->plvo && !M_AREAOF (np->plvo))
                np->plvo = 0;
            if (np->pmvo && !M_AREAOF (np->pmvo))
                np->pmvo = 0;
            if (np->prvo && !M_AREAOF (np->prvo))
                np->prvo = 0;
            if (np->pb3vo && !M_AREAOF (np->pb3vo))
                np->pb3vo = 0;
            if (np->pb4vo && !M_AREAOF (np->pb4vo))
                np->pb4vo = 0;
            for (ki = 0; ki < 256; ki++)
                if (np->pkvo[ki] && !M_AREAOF (np->pkvo[ki]))
                    np->pkvo[ki] = 0;
        }
186 187 188 189
    }
}

/* callback for g.c module - calls the appropriate lefty function */
190
void GFXlabelcb (Gevent_t *evp) {
191 192 193 194 195
    Tobj fo, to, co, wo;
    char *fn;
    char s[2];
    long fm;

196
    fn = NULL;
197
    wo = Tfindi (rootwo, evp->wi);
198 199
    switch (evp->type) {
    case G_MOUSE:
200 201 202 203 204 205 206 207 208 209 210 211
        switch (evp->data) {
        case G_LEFT:
            fn = (evp->code == G_UP) ? "leftup" : "leftdown";
            break;
        case G_MIDDLE:
            fn = (evp->code == G_UP) ? "middleup" : "middledown";
            break;
        case G_RIGHT:
            fn = (evp->code == G_UP) ? "rightup" : "rightdown";
            break;
        }
        break;
212
    case G_KEYBD:
213 214
        fn = (evp->code == G_UP) ? "keyup" : "keydown";
        break;
215
    }
216 217 218 219 220 221 222
    if (!wo || !(fo = Tfinds (wo, fn)) || Tgettype (fo) != T_CODE)
        if (!(fo = Tfinds (root, fn)) || Tgettype (fo) != T_CODE)
            return;
    fm = Mpushmark (fo);
    to = Ttable (4);
    Mpushmark (to);
    Tinss (to, "widget", Tinteger (evp->wi));
223
    if (evp->type == G_KEYBD)
224 225 226 227
        s[0] = evp->data, s[1] = '\000', Tinss (to, "key", Tstring (s));
    if ((co = Pfcall (fo, to)))
        Eunit (co);
    Mpopmark (fm);
228 229 230
}

/* callback for g.c module - calls the appropriate lefty function */
231
void GFXviewcb (Gevent_t *evp) {
232 233 234 235
    Tobj fo, to, co, wo;
    char *fn;
    long fm;

236
    wo = Tfindi (rootwo, evp->wi);
237
    fn = "closeview";
238 239 240 241 242 243 244 245 246 247
    if (!wo || !(fo = Tfinds (wo, fn)) || Tgettype (fo) != T_CODE)
        if (!(fo = Tfinds (root, fn)) || Tgettype (fo) != T_CODE)
            exit (0);
    fm = Mpushmark (fo);
    to = Ttable (2);
    Mpushmark (to);
    Tinss (to, "widget", Tinteger (evp->wi));
    if ((co = Pfcall (fo, to)))
        Eunit (co);
    Mpopmark (fm);
248 249 250
}

/* callback for g.c module - calls the appropriate lefty function */
251 252
void GFXevent (Gevent_t *evp) {
    Tobj vo, pvo, fo, to, po, co, wo;
253
    gfxnode_t *np;
254
    Gpoint_t pp;
255 256 257 258 259
    char *fn;
    char s[2];
    long fm;
    int ni;

260 261 262
    pp.x = pp.y = 0;
    pvo = NULL;
    fn = NULL;
263 264
    ni = Gwidgets[evp->wi].udata;
    np = &gfxnodes[ni];
265 266 267
    wo = Tfindi (rootwo, evp->wi);
    if (!(vo = rectfind (ni, evp->p)))
        vo = null;
268 269
    switch (evp->type) {
    case G_MOUSE:
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
        switch (evp->data) {
        case G_LEFT:
            if (evp->code == G_UP)
                fn = "leftup", pvo = np->plvo, pp = np->plp, np->ls = 0;
            else
                fn = "leftdown", np->plvo = vo, np->plp = evp->p, np->ls = 1;
            break;
        case G_MIDDLE:
            if (evp->code == G_UP)
                fn = "middleup", pvo = np->pmvo, pp = np->pmp, np->ms = 0;
            else
                fn = "middledown", np->pmvo = vo, np->pmp = evp->p, np->ms = 1;
            break;
        case G_RIGHT:
            if (evp->code == G_UP)
                fn = "rightup", pvo = np->prvo, pp = np->prp, np->rs = 0;
            else
                fn = "rightdown", np->prvo = vo, np->prp = evp->p, np->rs = 1;
            break;
        case G_BUTTON3:
            if (evp->code == G_UP) {
                fn = "button3up";
                pvo = np->pb3vo, pp = np->pb3p, np->b3s = 0;
            } else {
                fn = "button4down";
                np->pb3vo = vo, np->pb3p = evp->p, np->b3s = 1;
            }
            break;
        case G_BUTTON4:
            if (evp->code == G_UP) {
                fn = "button4up";
                pvo = np->pb4vo, pp = np->pb4p, np->b4s = 0;
            } else {
                fn = "button4down";
                np->pb4vo = vo, np->pb4p = evp->p, np->b4s = 1;
            }
            break;
        }
        break;
309
    case G_KEYBD:
310 311 312 313 314 315 316 317 318 319 320 321
        if (evp->code == G_UP) {
            fn = "keyup";
            pvo = np->pkvo[evp->data];
            pp = np->pkp[evp->data];
            np->ks[evp->data] = 0;
        } else {
            fn = "keydown";
            np->pkvo[evp->data] = vo;
            np->pkp[evp->data] = evp->p;
            np->ks[evp->data] = 1;
        }
        break;
322
    }
323 324 325 326 327 328 329 330 331 332 333 334
    if (!wo || !(fo = Tfinds (wo, fn)) || Tgettype (fo) != T_CODE)
        if (!(fo = Tfinds (root, fn)) || Tgettype (fo) != T_CODE)
            return;

    fm = Mpushmark (fo);
    to = Ttable (4);
    Mpushmark (to);
    Tinss (to, "widget", Tinteger (evp->wi));
    Tinss (to, "obj", vo);
    Tinss (to, "pos", (po = Ttable (2)));
    Tinss (po, "x", Treal (evp->p.x));
    Tinss (po, "y", Treal (evp->p.y));
335
    if (evp->code == G_UP) {
336 337 338 339
        Tinss (to, "pobj", pvo);
        Tinss (to, "ppos", (po = Ttable (2)));
        Tinss (po, "x", Treal (pp.x));
        Tinss (po, "y", Treal (pp.y));
340 341
    }
    if (evp->type == G_KEYBD)
342 343 344 345
        s[0] = evp->data, s[1] = '\000', Tinss (to, "key", Tstring (s));
    if ((co = Pfcall (fo, to)))
        Eunit (co);
    Mpopmark (fm);
346 347 348
}

/* called directly from lefty.c, when any button is held down */
349
void GFXmove (void) {
350
    gfxnode_t *np;
351 352 353
    char *fn[5];
    Tobj vo[5], fo, to, po, co, wo;
    Gpoint_t cp, pp[5];
354 355 356 357
    long fm;
    int count, i, ni;

    for (ni = 0; ni < gfxnoden; ni++) {
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
        np = &gfxnodes[ni];
        if (
            !np->inuse || !ISACANVAS (np->wi) ||
            !Gwidgets[np->wi].u.c->buttonsdown
        )
            continue;
        wo = Tfindi (rootwo, np->wi);
        Ggetmousecoords (np->wi, &cp, &count);
        if (!count) {
            Gresetbstate (np->wi);
            continue;
        }
        if (np->ls)
            fn[0] = "leftmove",   pp[0] = np->plp, vo[0] = np->plvo;
        else
            fn[0] = NULL;
        if (np->ms)
            fn[1] = "middlemove", pp[1] = np->pmp, vo[1] = np->pmvo;
        else
            fn[1] = NULL;
        if (np->rs)
            fn[2] = "rightmove",  pp[2] = np->prp, vo[2] = np->prvo;
        else
            fn[2] = NULL;
        if (np->b3s)
            fn[3] = "button3move",  pp[3] = np->pb3p, vo[3] = np->pb3vo;
        else
            fn[3] = NULL;
        if (np->b4s)
            fn[4] = "button4move",  pp[4] = np->pb4p, vo[4] = np->pb4vo;
        else
            fn[4] = NULL;
        for (i = 0; i < 5; i++) {
            if (!fn[i])
                continue;
            if (!wo || !(fo = Tfinds (wo, fn[i])) || Tgettype (fo) != T_CODE)
                fo = Tfinds (root, fn[i]);
            if (fo && Tgettype (fo) == T_CODE) {
                fm = Mpushmark (fo);
                to = Ttable (4);
                Mpushmark (to);
                Tinss (to, "widget", Tinteger (np->wi));
                Tinss (to, "obj", vo[i]);
                Tinss (to, "pos", (po = Ttable (2)));
                Tinss (po, "x", Treal (cp.x));
                Tinss (po, "y", Treal (cp.y));
                Tinss (to, "ppos", (po = Ttable (2)));
                Tinss (po, "x", Treal (pp[i].x));
                Tinss (po, "y", Treal (pp[i].y));
                if ((co = Pfcall (fo, to)))
                    Eunit (co);
                Mpopmark (fm);
            }
        }
412 413 414 415
    }
}

/* called directly from lefty.c, when any canvas needs to be redrawn */
416
void GFXredraw (void) {
417 418 419 420 421 422
    gfxnode_t *np;
    Tobj wo, fo, co, to;
    long fm;
    int ni;

    for (ni = 0; ni < gfxnoden; ni++) {
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
        np = &gfxnodes[ni];
        if (
            !np->inuse || !ISACANVAS (np->wi) ||
            !Gwidgets[np->wi].u.c->needredraw
        )
            continue;
        Gwidgets[np->wi].u.c->needredraw = FALSE;
        wo = Tfindi (rootwo, np->wi);
        if (!wo || !(fo = Tfinds (wo, "redraw")) || Tgettype (fo) != T_CODE)
            if (!(fo = Tfinds (root, "redraw")) || Tgettype (fo) != T_CODE)
                return;

        fm = Mpushmark (fo);
        to = Ttable (4);
        Mpushmark (to);
        Tinss (to, "widget", Tinteger (np->wi));
        if ((co = Pfcall (fo, to)))
            Eunit (co);
        Mpopmark (fm);
442 443 444
    }
}

445
void GFXtextcb (int wi, char *s) {
446 447 448
    Tobj wo, fo, co, to;
    long fm;

449 450
    if (!(wo = Tfindi (rootwo, wi)))
        return;
451

452 453
    if (!(fo = Tfinds (wo, "oneline")) || Tgettype (fo) != T_CODE)
        return;
454

455 456 457 458 459 460 461 462
    fm = Mpushmark (fo);
    to = Ttable (4);
    Mpushmark (to);
    Tinss (to, "widget", Tinteger (wi));
    Tinss (to, "text", Tstring (s));
    if ((co = Pfcall (fo, to)))
        Eunit (co);
    Mpopmark (fm);
463 464
}

465
void GFXbuttoncb (int wi, void *data) {
466 467 468
    Tobj wo, fo, co, to;
    long fm;

469 470
    if (!(wo = Tfindi (rootwo, wi)))
        return;
471

472 473
    if (!(fo = Tfinds (wo, "pressed")) || Tgettype (fo) != T_CODE)
        return;
474

475 476 477 478 479 480 481
    fm = Mpushmark (fo);
    to = Ttable (4);
    Mpushmark (to);
    Tinss (to, "widget", Tinteger (wi));
    if ((co = Pfcall (fo, to)))
        Eunit (co);
    Mpopmark (fm);
482 483
}

484 485
void GFXarrayresizecb (int wi, Gawdata_t *dp) {
    Tobj wo, fo, co, so, to, lrtno, sxo, syo;
486 487 488 489 490
    Tkvindex_t tkvi;
    Gawcarray_t *cp;
    int sx, sy, csx, csy, ci;
    long fm;

491 492
    if (!(wo = Tfindi (rootwo, wi)))
        return;
493

494 495 496
    if (!(fo = Tfinds (wo, "resize")) || Tgettype (fo) != T_CODE) {
        Gawdefcoordscb (wi, dp);
        return;
497 498
    }

499 500 501 502 503 504 505
    fm = Mpushmark (fo);
    to = Ttable (4);
    Mpushmark (to);
    Tinss (to, "widget", Tinteger (wi));
    Tinss (to, "size", (so = Ttable (2)));
    Tinss (so, "x", Treal ((double) dp->sx));
    Tinss (so, "y", Treal ((double) dp->sy));
506 507

    lrtno = NULL;
508 509 510
    if ((co = Pfcall (fo, to)))
        lrtno = Eunit (co);
    Mpopmark (fm);
511
    if (!lrtno) {
512 513
        Gawdefcoordscb (wi, dp);
        return;
514
    }
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    for (Tgetfirst (lrtno, &tkvi); tkvi.kvp; Tgetnext (&tkvi)) {
        if (!T_ISNUMBER (tkvi.kvp->ko) || !T_ISTABLE ((so = tkvi.kvp->vo)))
            continue;
        wi = Tgetnumber (tkvi.kvp->ko);
        if (!ISAWIDGET (wi))
            continue;
        for (ci = 0; ci < dp->cj; ci++) {
            cp = &dp->carray[ci];
            if (!cp->flag || cp->w != Gwidgets[wi].w)
                continue;
            if ((sxo = Tfinds (so, "x")) && T_ISNUMBER (sxo))
                cp->sx = Tgetnumber (sxo);
            if ((syo = Tfinds (so, "y")) && T_ISNUMBER (syo))
                cp->sy = Tgetnumber (syo);
            break;
        }
531 532 533 534
    }
    sx = dp->sx, sy = dp->sy;
    csx = csy = 0;
    for (ci = 0; ci < dp->cj; ci++) {
535 536 537 538 539 540 541 542
        cp = &dp->carray[ci];
        if (!cp->flag)
            continue;
        cp->ox = csx, cp->oy = csy;
        if (dp->type == G_AWVARRAY)
            cp->sx = sx - 2 * cp->bs, csy += cp->sy + 2 * cp->bs;
        else
            cp->sy = sy - 2 * cp->bs, csx += cp->sx + 2 * cp->bs;
543 544
    }
    if (dp->type == G_AWVARRAY)
545
        dp->sy = csy;
546
    else
547
        dp->sx = csx;
548 549 550
}

/* callback for when there is input on some file descriptor */
551
void GFXmonitorfile (int fd) {
552 553 554
    Tobj fo, to, co;
    long fm, tm;

555 556 557 558 559 560 561 562 563 564 565
    if (!(fo = Tfinds (root, "monitorfile")) || Tgettype (fo) != T_CODE)
        return;

    fm = Mpushmark (fo);
    to = Ttable (4);
    tm = Mpushmark (to);
    Tinss (to, "fd", Tinteger (fd));
    if ((co = Pfcall (fo, to)))
        Eunit (co);
    Mpopmark (tm);
    Mpopmark (fm);
566 567 568
}

/* callback for when there is no X event and no file input */
569
void GFXidle (void) {
570 571 572
    Tobj fo, to, co;
    long fm, tm;

573 574
    if (!(fo = Tfinds (root, "idle")) || Tgettype (fo) != T_CODE)
        return;
575

576 577 578 579 580 581 582
    fm = Mpushmark (fo);
    to = Ttable (4);
    tm = Mpushmark (to);
    if ((co = Pfcall (fo, to)))
        Eunit (co);
    Mpopmark (tm);
    Mpopmark (fm);
583 584 585
}

/* LEFTY builtin */
586
int GFXcreatewidget (int argc, lvar_t *argv) {
587 588 589 590 591
    Tobj pwo, cwo, cho;
    long rtnm;
    int type, pwi, wi, ni;

    type = -1;
592 593 594 595
    if (getint (argv[0].o, &pwi) == -1 || getwattr (argv[1].o, &type) == -1)
        return L_FAILURE;
    if (type != G_VIEWWIDGET && type != G_PCANVASWIDGET && !ISAWIDGET (pwi))
        return L_FAILURE;
596
    if (type == G_CANVASWIDGET || type == G_LABELWIDGET) {
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
        for (ni = 0; ni < gfxnoden; ni++)
            if (!gfxnodes[ni].inuse)
                break;
        if (ni == gfxnoden) {
            gfxnodes = Marraygrow (
                gfxnodes, (long) (ni + GFXNODEINCR) * GFXNODESIZE
            );
            for (ni = gfxnoden; ni < gfxnoden + GFXNODEINCR; ni++)
                gfxnodes[ni].inuse = FALSE;
            ni = gfxnoden, gfxnoden += GFXNODEINCR;
        }
        nodeinit (ni);
        if (wattri >= wattrn) {
            wattrp = Marraygrow (
                wattrp, (long) (wattrn + WATTRINCR) * WATTRSIZE
            );
            wattrn += WATTRINCR;
        }
        switch (type) {
        case G_CANVASWIDGET:
            wattrp[wattri].id = G_ATTRUSERDATA;
            wattrp[wattri].u.u = ni, wattri++;
            break;
        case G_LABELWIDGET:
            wattrp[wattri].id = G_ATTRUSERDATA;
            wattrp[wattri].u.u = ni, wattri++;
            break;
        }
        wi = gfxnodes[ni].wi = Gcreatewidget (pwi, type, wattri, wattrp);
        gfxnodes[ni].inuse = TRUE;
        goto done;
628
    }
629
    wi = Gcreatewidget (pwi, type, wattri, wattrp);
630

631 632 633 634
done:
    Tinsi (rootwo, wi, (cwo = Ttable (4)));
    rtno = Tinteger (wi);
    rtnm = Mpushmark (rtno);
635
    if (pwi != -1) {
636 637 638 639 640 641
        Tinss (cwo, "parent", Tinteger (pwi));
        if ((pwo = Tfindi (rootwo, pwi))) {
            if (!(cho = Tfinds (pwo, "children")))
                Tinss (pwo, "children", (cho = Ttable (2)));
            Tinsi (cho, wi, Tinteger (pwi));
        }
642
    }
643
    Mpopmark (rtnm);
644 645 646 647
    return L_SUCCESS;
}

/* LEFTY builtin */
648
int GFXsetwidgetattr (int argc, lvar_t *argv) {
649 650
    int wi, type, rtn;

651 652
    if (getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi))
        return L_FAILURE;
653
    type = Gwidgets[wi].type;
654 655 656 657
    if ((rtn = getwattr (argv[1].o, &type)) == -1)
        return L_FAILURE;
    Gsetwidgetattr (wi, wattri, wattrp);
    rtno = Tinteger (rtn);
658 659 660 661
    return L_SUCCESS;
}

/* LEFTY builtin */
662
int GFXgetwidgetattr (int argc, lvar_t *argv) {
663 664 665 666 667 668 669 670
    Tkvindex_t tkvi;
    Tobj po, so, ro, co, co2;
    Gwattrmap_t *mapp;
    int *ap;
    int li, ai, type, color, wattri2, wi;
    long rtnm;

    wattri = 0;
671 672 673 674 675
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) ||
        !T_ISTABLE (argv[1].o)
    )
        return L_FAILURE;
676 677
    type = Gwidgets[wi].type;
    for (ap = NULL, li = 0; Gwlist[li].wname; li++) {
678 679 680 681
        if (type == Gwlist[li].wid) {
            ap = Gwlist[li].attrid;
            break;
        }
682 683
    }
    if (!ap)
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
        return L_FAILURE;
    for (Tgetfirst (argv[1].o, &tkvi); tkvi.kvp; Tgetnext (&tkvi)) {
        if (!T_ISSTRING (tkvi.kvp->vo))
            continue;
        for (ai = 0; ap[ai] != -1; ai++) {
            if (
                strcmp (Gwattrmap[ap[ai]].name,
                Tgetstring (tkvi.kvp->vo)) == 0
            ) {
                if (wattri >= wattrn) {
                    wattrp = Marraygrow (
                        wattrp, (long) (wattrn + WATTRINCR) * WATTRSIZE
                    );
                    wattrn += WATTRINCR;
                }
                if (ap[ai] == G_ATTRCOLOR) {
                    for (color = 0; color < G_MAXCOLORS; color++) {
                        if (wattri >= wattrn) {
                            wattrp = Marraygrow (
                                wattrp, (long) (wattrn + WATTRINCR) * WATTRSIZE
                            );
                            wattrn += WATTRINCR;
                        }
                        wattrp[wattri].u.c.index = color;
                        wattrp[wattri++].id = ap[ai];
                    }
                } else
                    wattrp[wattri++].id = ap[ai];
                break;
            }
        }
715
    }
716 717 718 719
    if (Ggetwidgetattr (wi, wattri, wattrp) == -1)
        return L_FAILURE;
    rtno = Ttable (wattri);
    rtnm = Mpushmark (rtno);
720
    for (wattri2 = 0; wattri2 < wattri; wattri2++) {
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
        mapp = &Gwattrmap[wattrp[wattri2].id];
        switch (mapp->type) {
        case G_ATTRTYPEPOINT:
            Tinss (rtno, mapp->name, (po = Ttable (2)));
            Tinss (po, "x", Treal (wattrp[wattri2].u.p.x));
            Tinss (po, "y", Treal (wattrp[wattri2].u.p.y));
            break;
        case G_ATTRTYPESIZE:
            Tinss (rtno, mapp->name, (so = Ttable (2)));
            Tinss (so, "x", Treal (wattrp[wattri2].u.s.x));
            Tinss (so, "y", Treal (wattrp[wattri2].u.s.y));
            break;
        case G_ATTRTYPERECT:
            Tinss (rtno, mapp->name, (ro = Ttable (2)));
            Tinsi (ro, 0, (po = Ttable (2)));
            Tinss (po, "x", Treal (wattrp[wattri2].u.r.o.x));
            Tinss (po, "y", Treal (wattrp[wattri2].u.r.o.y));
            Tinsi (ro, 1, (po = Ttable (2)));
            Tinss (po, "x", Treal (wattrp[wattri2].u.r.c.x));
            Tinss (po, "y", Treal (wattrp[wattri2].u.r.c.y));
            break;
        case G_ATTRTYPETEXT:
            Tinss (rtno, mapp->name, Tstring (wattrp[wattri2].u.t));
            break;
        case G_ATTRTYPEINT:
            Tinss (rtno, mapp->name, Tinteger (wattrp[wattri2].u.i));
            break;
        case G_ATTRTYPECOLOR:
            Tinss (rtno, mapp->name, (co = Ttable (G_MAXCOLORS)));
            for (color = 0; color < G_MAXCOLORS; color++) {
                Tinsi (co, color, (co2 = Ttable (3)));
                Tinss (co2, "r", Treal (wattrp[wattri2 + color].u.c.r));
                Tinss (co2, "g", Treal (wattrp[wattri2 + color].u.c.g));
                Tinss (co2, "b", Treal (wattrp[wattri2 + color].u.c.g));
            }
            wattri2 += (G_MAXCOLORS - 1);
            break;
        }
759
    }
760
    Mpopmark (rtnm);
761 762 763 764
    return L_SUCCESS;
}

/* LEFTY builtin */
765
int GFXdestroywidget (int argc, lvar_t *argv) {
766 767 768 769 770
    Tkvindex_t tkvi;
    Tobj wo, cho, pwio, pwo;
    lvar_t argv2[1];
    int wi;

771 772
    if (getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi))
        return L_FAILURE;
773

774 775 776 777 778 779 780
    wo = Tfindi (rootwo, wi);
    if ((cho = Tfinds (wo, "children"))) {
        while (Tgettablen (cho) > 0) {
            Tgetfirst (cho, &tkvi);
            argv2[0].o = tkvi.kvp->ko;
            GFXdestroywidget (1, argv2);
        }
781
    }
782 783 784 785
    if ((pwio = Tfinds (wo, "parent"))) {
        pwo = Tfindi (rootwo, Tgetinteger (pwio));
        cho = Tfinds (pwo, "children");
        Tdeli (cho, wi);
786
    }
787 788 789
    if (ISACANVAS (wi) || ISALABEL (wi)) {
        nodeterm (NODEID (wi));
        gfxnodes[NODEID (wi)].inuse = FALSE;
790
    }
791 792
    Gdestroywidget (wi);
    Tdeli (rootwo, wi);
793 794 795 796
    return L_SUCCESS;
}

/* LEFTY builtin */
797
int GFXclear (int argc, lvar_t *argv) {
798 799
    int wi;

800 801 802 803 804
    if (getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi))
        return L_FAILURE;
    Gcanvasclear (wi);
    if (ISACANVAS (wi))
        rectterm (NODEID (wi)), rectinit (NODEID (wi));
805 806 807 808
    return L_SUCCESS;
}

/* LEFTY builtin */
809
int GFXsetgfxattr (int argc, lvar_t *argv) {
810 811 812
    Ggattr_t gattr;
    int wi;

813 814 815 816 817 818
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) ||
        getgattr (argv[1].o, &gattr) == -1
    )
        return L_FAILURE;
    Gsetgfxattr (wi, &gattr);
819 820 821 822
    return L_SUCCESS;
}

/* LEFTY builtin */
823
int GFXgetgfxattr (int argc, lvar_t *argv) {
824 825
    Tkvindex_t tkvi;
    Ggattr_t gattr;
826
    long rtnm;
827
    int wi;
828 829 830 831 832 833 834 835
    char *s;

    gattr.flags = 0;
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) ||
        !T_ISTABLE (argv[1].o)
    )
        return L_FAILURE;
836 837

    s = NULL;
838 839 840 841 842 843 844 845 846 847 848 849 850 851
    for (Tgetfirst (argv[1].o, &tkvi); tkvi.kvp; Tgetnext (&tkvi)) {
        if (!T_ISSTRING (tkvi.kvp->vo))
            continue;
        s = Tgetstring (tkvi.kvp->vo);
        if (strcmp (s, "color") == 0)
            gattr.flags |= G_GATTRCOLOR;
        else if (strcmp (s, "width") == 0)
            gattr.flags |= G_GATTRWIDTH;
        else if (strcmp (s, "mode") == 0)
            gattr.flags |= G_GATTRMODE;
        else if (strcmp (s, "fill") == 0)
            gattr.flags |= G_GATTRFILL;
        else if (strcmp (s, "style") == 0)
            gattr.flags |= G_GATTRSTYLE;
852
    }
853 854 855 856
    if (Ggetgfxattr (wi, &gattr) == -1)
        return L_FAILURE;
    rtno = Ttable (wattri);
    rtnm = Mpushmark (rtno);
857
    if (gattr.flags & G_GATTRCOLOR) {
858
        Tinss (rtno, "color", Tinteger (gattr.color));
859
    } else if (gattr.flags & G_GATTRWIDTH) {
860
        Tinss (rtno, "width", Tinteger (gattr.width));
861
    } else if (gattr.flags & G_GATTRMODE) {
862 863
        s = (gattr.mode == G_SRC) ? "src" : "xor";
        Tinss (rtno, "mode", Tstring (s));
864
    } else if (gattr.flags & G_GATTRFILL) {
865 866
        s = (gattr.fill) ? "on" : "off";
        Tinss (rtno, "fill", Tstring (s));
867
    } else if (gattr.flags & G_GATTRSTYLE) {
868 869 870 871 872 873 874 875
        switch (gattr.style) {
        case G_SOLID:       s = "solid";       break;
        case G_DASHED:      s = "dashed";      break;
        case G_DOTTED:      s = "dotted";      break;
        case G_LONGDASHED:  s = "longdashed";  break;
        case G_SHORTDASHED: s = "shortdashed"; break;
        }
        Tinss (rtno, "style", Tstring (s));
876
    }
877
    Mpopmark (rtnm);
878 879 880 881
    return L_SUCCESS;
}

/* LEFTY builtin */
882
int GFXarrow (int argc, lvar_t *argv) {
883 884 885 886
    Ggattr_t gattr;
    Gpoint_t p0, p1;
    int wi;

887 888 889 890 891 892 893
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) || !ISACANVAS2 (wi) ||
        getxy (argv[2].o, &p0) == -1 || getxy (argv[3].o, &p1) == -1 ||
        getgattr ((argc == 5) ? argv[4].o : NULL, &gattr) == -1
    )
        return L_FAILURE;
    Garrow (wi, p0, p1, &gattr);
894 895 896 897
    return L_SUCCESS;
}

/* LEFTY builtin */
898
int GFXline (int argc, lvar_t *argv) {
899 900 901 902
    Ggattr_t gattr;
    Gpoint_t p0, p1;
    int wi;

903 904 905 906 907 908 909
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) || !ISACANVAS2 (wi) ||
        getxy (argv[2].o, &p0) == -1 || getxy (argv[3].o, &p1) == -1 ||
        getgattr ((argc == 5) ? argv[4].o : NULL, &gattr) == -1
    )
        return L_FAILURE;
    Gline (wi, p0, p1, &gattr);
910 911 912 913
    return L_SUCCESS;
}

/* LEFTY builtin */
914
int GFXbox (int argc, lvar_t *argv) {
915 916 917 918
    Ggattr_t gattr;
    Grect_t r;
    int wi, rtn;

919 920 921 922 923 924 925 926 927
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) || !ISACANVAS2 (wi) ||
        getrect (argv[2].o, &r) == -1 ||
        getgattr ((argc == 4) ? argv[3].o : NULL, &gattr) == -1
    )
        return L_FAILURE;
    rtn = Gbox (wi, r, &gattr);
    if (rtn == 0 && argv[1].o != null && ISACANVAS (wi))
        rectmerge (NODEID (wi), argv[1].o, r);
928 929 930 931
    return L_SUCCESS;
}

/* LEFTY builtin */
932
int GFXpolygon (int argc, lvar_t *argv) {
933 934 935 936 937
    Tobj po;
    Ggattr_t gattr;
    long i, pn;
    int wi;

938 939 940 941 942
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) || !ISACANVAS2 (wi) ||
        getgattr ((argc == 4) ? argv[3].o : NULL, &gattr) == -1
    )
        return L_FAILURE;
943
    po = argv[2].o;
944 945 946
    if ((pn = Tgettablen (po)) > gpn) {
        gpp = Marraygrow (gpp, (long) pn * GPSIZE);
        gpn = pn;
947 948
    }
    for (i = 0; i < pn; i++)
949 950 951
        if (getxy (Tfindi (po, i), &gpp[i]) == -1)
            return L_FAILURE;
    Gpolygon (wi, pn, gpp, &gattr);
952 953 954 955
    return L_SUCCESS;
}

/* LEFTY builtin */
956
int GFXsplinegon (int argc, lvar_t *argv) {
957 958 959 960 961
    Tobj po;
    Ggattr_t gattr;
    long i, pn;
    int wi;

962 963 964 965 966
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) || !ISACANVAS2 (wi) ||
        getgattr ((argc == 4) ? argv[3].o : NULL, &gattr) == -1
    )
        return L_FAILURE;
967
    po = argv[2].o;
968 969 970
    if ((pn = Tgettablen (po)) > gpn) {
        gpp = Marraygrow (gpp, (long) pn * GPSIZE);
        gpn = pn;
971 972
    }
    for (i = 0; i < pn; i++)
973 974 975
        if (getxy (Tfindi (po, i), &gpp[i]) == -1)
            return L_FAILURE;
    Gsplinegon (wi, pn, gpp, &gattr);
976 977 978 979
    return L_SUCCESS;
}

/* LEFTY builtin */
980
int GFXarc (int argc, lvar_t *argv) {
981 982 983 984 985 986
    Ggattr_t gattr;
    Grect_t r;
    Gpoint_t op;
    Gsize_t sp;
    int wi, rtn;

987 988 989 990 991 992 993 994 995 996 997
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) || !ISACANVAS2 (wi) ||
        getxy (argv[2].o, &op) == -1 || getxy (argv[3].o, &sp) == -1 ||
        getgattr ((argc == 5) ? argv[4].o : NULL, &gattr) == -1
    )
        return L_FAILURE;
    rtn = Garc (wi, op, sp, (double) 0, (double) 360, &gattr);
    if (rtn == 0 && argv[1].o != null && ISACANVAS (wi)) {
        r.o.x = op.x - sp.x, r.o.y = op.y - sp.y;
        r.c.x = op.x + sp.x, r.c.y = op.y + sp.y;
        rectmerge (NODEID (wi), argv[1].o, r);
998 999 1000 1001 1002
    }
    return L_SUCCESS;
}

/* LEFTY builtin */
1003
int GFXtext (int argc, lvar_t *argv) {
1004 1005 1006 1007 1008 1009
    Ggattr_t gattr;
    Gpoint_t p;
    char *s, *fn, *justs;
    double fs;
    int wi;

1010 1011 1012 1013 1014 1015 1016 1017 1018
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) || !ISACANVAS2 (wi) ||
        getxy (argv[2].o, &p) == -1 || getstr (argv[3].o, &s) == -1 ||
        getstr (argv[4].o, &fn) == -1 || getdouble (argv[5].o, &fs) == -1 ||
        getstr (argv[6].o, &justs) == -1 ||
        getgattr ((argc == 8) ? argv[7].o : NULL, &gattr) == -1
    )
        return L_FAILURE;
    Gtext (wi, s, p, fn, fs, justs, &gattr);
1019 1020 1021 1022
    return L_SUCCESS;
}

/* LEFTY builtin */
1023
int GFXtextsize (int argc, lvar_t *argv) {
1024 1025 1026 1027 1028 1029
    Gsize_t sp;
    double fs;
    char *s, *fn;
    long m;
    int wi;

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) || !ISACANVAS (wi) ||
        getstr (argv[1].o, &s) == -1 || getstr (argv[2].o, &fn) == -1 ||
        getdouble (argv[3].o, &fs) == -1
    )
        return L_FAILURE;
    if (Ggettextsize (wi, s, fn, fs, &sp) == -1)
        return L_FAILURE;
    m = Mpushmark ((rtno = Ttable (2)));
    Tinss (rtno, "x", Treal (sp.x)), Tinss (rtno, "y", Treal (sp.y));
    Mpopmark (m);
1041 1042 1043 1044
    return L_SUCCESS;
}

/* LEFTY builtin */
1045
int GFXcreatebitmap (int argc, lvar_t *argv) {
1046 1047 1048 1049 1050
    Tobj bo, so;
    Gsize_t s;
    long rtnm;
    int wi, bi;

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) || !ISACANVAS2 (wi) ||
        getxy (argv[1].o, &s) == -1
    )
        return L_FAILURE;
    if ((bi = Gcreatebitmap (wi, s)) == -1)
        return L_FAILURE;
    Tinsi (rootbo, bi, (bo = Ttable (4)));
    rtno = Tinteger (bi);
    rtnm = Mpushmark (rtno);
    Tinss (bo, "canvas", Tinteger (bi));
    Tinss (bo, "size", (so = Ttable (2)));
    Tinss (so, "x", Tinteger ((long) Gbitmaps[bi].size.x));
    Tinss (so, "y", Tinteger ((long) Gbitmaps[bi].size.y));
    Mpopmark (rtnm);
1066 1067 1068 1069
    return L_SUCCESS;
}

/* LEFTY builtin */
1070
int GFXdestroybitmap (int argc, lvar_t *argv) {
1071 1072
    int bi;

1073 1074 1075 1076
    if (getint (argv[0].o, &bi) == -1 || !ISABITMAP (bi))
        return L_FAILURE;
    Gdestroybitmap (bi);
    Tdeli (rootbo, bi);
1077 1078 1079 1080
    return L_SUCCESS;
}

/* LEFTY builtin */
1081
int GFXreadbitmap (int argc, lvar_t *argv) {
1082 1083 1084 1085
    Tobj bo, so;
    long rtnm;
    int wi, bi, ioi;

1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) || !ISACANVAS2 (wi) ||
        getint (argv[1].o, &ioi) == -1 || ioi < 0 || ioi > ion ||
        !iop[ioi].inuse
    )
        return L_FAILURE;
    if ((bi = Greadbitmap (wi, iop[ioi].ifp)) == -1)
        return L_FAILURE;
    Tinsi (rootbo, bi, (bo = Ttable (4)));
    rtno = Tinteger (bi);
    rtnm = Mpushmark (rtno);
    Tinss (bo, "canvas", Tinteger (bi));
    Tinss (bo, "size", (so = Ttable (2)));
    Tinss (so, "x", Tinteger ((long) Gbitmaps[bi].size.x));
    Tinss (so, "y", Tinteger ((long) Gbitmaps[bi].size.y));
    Mpopmark (rtnm);
1102 1103 1104 1105
    return L_SUCCESS;
}

/* LEFTY builtin */
1106
int GFXwritebitmap (int argc, lvar_t *argv) {
1107 1108
    int bi, ioi;

1109 1110 1111 1112 1113 1114
    if (
        getint (argv[0].o, &ioi) == -1 || ioi < 0 || ioi > ion ||
        !iop[ioi].inuse || getint (argv[1].o, &bi) == -1 || !ISABITMAP (bi)
    )
        return L_FAILURE;
    Gwritebitmap (iop[ioi].ofp, bi);
1115 1116 1117 1118
    return L_SUCCESS;
}

/* LEFTY builtin */
1119
int GFXbitblt (int argc, lvar_t *argv) {
1120 1121 1122 1123 1124 1125
    Ggattr_t gattr;
    Grect_t r;
    Gpoint_t p;
    char *mode;
    int wi, bi, rtn;

1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) || !ISACANVAS2 (wi) ||
        getxy (argv[2].o, &p) == -1 || getrect (argv[3].o, &r) == -1 ||
        getint (argv[4].o, &bi) == -1 || getstr (argv[5].o, &mode) == -1 ||
        getgattr ((argc == 7) ? argv[6].o : NULL, &gattr) == -1
    )
        return L_FAILURE;
    rtn = Gbitblt (wi, p, r, bi, mode, &gattr);
    if (
        rtn == 0 && argv[1].o != null && ISACANVAS (wi) &&
        strcmp (mode, "b2c") == 0
    )
        rectmerge (NODEID (wi), argv[1].o, r);
1139 1140 1141 1142
    return L_SUCCESS;
}

/* LEFTY builtin */
1143
int GFXclearpick (int argc, lvar_t *argv) {
1144 1145
    int wi;

1146 1147
    if (getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) || !ISACANVAS (wi))
        return L_FAILURE;
1148
    if (argv[1].o != null)
1149
        rectdelete (NODEID (wi), argv[1].o);
1150 1151 1152 1153
    return L_SUCCESS;
}

/* LEFTY builtin */
1154
int GFXsetpick (int argc, lvar_t *argv) {
1155 1156 1157
    Grect_t r;
    int wi;

1158 1159 1160 1161 1162
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) || !ISACANVAS (wi) ||
        getrect (argv[2].o, &r) == -1
    )
        return L_FAILURE;
1163
    if (argv[1].o != null)
1164
        rectinsert (NODEID (wi), argv[1].o, r);
1165 1166 1167 1168
    return L_SUCCESS;
}

/* LEFTY builtin */
1169
int GFXdisplaymenu (int argc, lvar_t *argv) {
1170 1171 1172 1173 1174 1175
    Tobj mo, meo;
    char buf[50];
    char *entries[1];
    int wi, mi, mei;
    int rtn;

1176 1177 1178 1179 1180
    if (
        getint (argv[0].o, &wi) == -1 || !ISAWIDGET (wi) ||
        !(ISACANVAS (wi) || ISALABEL (wi))
    )
        return L_FAILURE;
1181
    mo = argv[1].o;
1182 1183 1184 1185 1186 1187
    if ((mi = menufind (NODEID (wi), mo, Tgettime (mo))) != -1) {
        if ((rtn = Gmenudisplay (wi, mi)) == -1)
            rtno = NULL;
        else
            rtno = Tinteger (rtn);
        return L_SUCCESS;
1188 1189
    }
    wattri = 0;
1190
    mi = Gcreatewidget (wi, G_MENUWIDGET, wattri, wattrp);
1191
    mei = 0;
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    while ((meo = Tfindi (mo, mei))) {
        switch (Tgettype (meo)) {
        case T_STRING:
            entries[0] = Tgetstring (meo);
            break;
        case T_INTEGER:
            sprintf (buf, "%d", (int) Tgetnumber (meo));
            entries[0] = &buf[0];
            break;
        case T_REAL:
            sprintf (buf, "%f", Tgetnumber (meo));
            entries[0] = &buf[0];
            break;
        }
        Gmenuaddentries (mi, 1, &entries[0]);
        mei++;
1208
    }
1209
    menuinsert (NODEID (wi), mo, Tgettime (mo), mi);
1210
    Ttime++;
1211 1212
    if ((rtn = Gmenudisplay (wi, mi)) == -1)
        rtno = NULL;
1213
    else
1214
        rtno = Tinteger (rtn);
1215 1216 1217
    return L_SUCCESS;
}

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
/* LEFTY builtin */
int GFXcolormap (int argc, lvar_t *argv) {
    char *cs;
    int cni;
    long rtnm;

    if (getstr (argv[0].o, &cs) == -1)
        return L_FAILURE;
    rtno = NULL;
    for (cni = 0; cni < sizeof (colornames) / sizeof (colorname_t); cni++) {
        if (strcmp (colornames[cni].name, cs) != 0)
            continue;
        rtno = Ttable (4);
        rtnm = Mpushmark (rtno);
        Tinss (rtno, "r", Tinteger (colornames[cni].r));
        Tinss (rtno, "g", Tinteger (colornames[cni].g));
        Tinss (rtno, "b", Tinteger (colornames[cni].b));
        Mpopmark (rtnm);
        break;
    }
    return L_SUCCESS;
}

static int getwattr (Tobj ao, int *type) {
1242 1243 1244 1245 1246 1247 1248
    Tkvindex_t tkvi;
    Tobj co;
    int *ap;
    char *ts;
    int li, ai;

    wattri = 0;
1249 1250
    if (!ao && Tgettype (ao) != T_TABLE)
        return -1;
1251
    if (*type == -1) {
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
        if (getstr (Tfinds (ao, "type"), &ts) == -1)
            return -1;
        for (ap = NULL, li = 0; Gwlist[li].wname; li++) {
            if (strcmp (ts, Gwlist[li].wname) == 0) {
                *type = Gwlist[li].wid;
                ap = Gwlist[li].attrid;
                break;
            }
        }
        if (*type == -1)
            return -1;
        if (wattri >= wattrn) {
            wattrp = Marraygrow (
                wattrp, (long) (wattrn + WATTRINCR) * WATTRSIZE
            );
            wattrn += WATTRINCR;
        }
        switch (*type) {
        case G_TEXTWIDGET:
            wattrp[wattri].id = G_ATTRNEWLINECB;
            wattrp[wattri].u.func = GFXtextcb, wattri++;
            break;
        case G_ARRAYWIDGET:
            wattrp[wattri].id = G_ATTRRESIZECB;
            wattrp[wattri].u.func = GFXarrayresizecb, wattri++;
            break;
        case G_BUTTONWIDGET:
            wattrp[wattri].id = G_ATTRBUTTONCB;
            wattrp[wattri].u.func = GFXbuttoncb, wattri++;
            break;
        case G_LABELWIDGET:
            wattrp[wattri].id = G_ATTREVENTCB;
            wattrp[wattri].u.func = GFXlabelcb, wattri++;
            break;
        case G_CANVASWIDGET:
            wattrp[wattri].id = G_ATTREVENTCB;
            wattrp[wattri].u.func = GFXevent, wattri++;
            break;
        case G_VIEWWIDGET:
            wattrp[wattri].id = G_ATTREVENTCB;
            wattrp[wattri].u.func = GFXviewcb, wattri++;
            break;
        }
1295
    } else {
1296 1297 1298 1299 1300 1301
        for (ap = NULL, li = 0; Gwlist[li].wname; li++) {
            if (*type == Gwlist[li].wid) {
                ap = Gwlist[li].attrid;
                break;
            }
        }
1302 1303
    }
    for (ai = 0; ap[ai] != -1; ai++) {
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
        if (wattri >= wattrn) {
            wattrp = Marraygrow (
                wattrp, (long) (wattrn + WATTRINCR) * WATTRSIZE
            );
            wattrn += WATTRINCR;
        }
        wattrp[wattri].id = ap[ai];
        switch (Gwattrmap[ap[ai]].type) {
        case G_ATTRTYPEPOINT:
            getwpointnset (Tfinds (ao, Gwattrmap[ap[ai]].name));
            break;
        case G_ATTRTYPESIZE:
            getwsizenset (Tfinds (ao, Gwattrmap[ap[ai]].name));
            break;
        case G_ATTRTYPERECT:
            getwrectnset (Tfinds (ao, Gwattrmap[ap[ai]].name));
            break;
        case G_ATTRTYPETEXT:
            getwstrnset (Tfinds (ao, Gwattrmap[ap[ai]].name));
            break;
        case G_ATTRTYPEINT:
            getwintnset (Tfinds (ao, Gwattrmap[ap[ai]].name));
            break;
        case G_ATTRTYPECOLOR:
            if ((co = Tfinds (
                ao, Gwattrmap[ap[ai]].name
            )) && Tgettype (co) == T_TABLE) {
                for (Tgetfirst (co, &tkvi); tkvi.kvp; Tgetnext (&tkvi)) {
                    if (wattri >= wattrn) {
                        wattrp = Marraygrow (
                            wattrp, (long) (wattrn + WATTRINCR) * WATTRSIZE
                        );
                        wattrn += WATTRINCR;
                    }
                    wattrp[wattri].id = ap[ai];
                    getwcolornset (tkvi.kvp->ko, tkvi.kvp->vo);
                }
            }
            break;
        }
1344 1345 1346 1347
    }
    return wattri;
}

1348
static int getgattr (Tobj ao, Ggattr_t *dp) {
1349 1350 1351
    Tkvindex_t tkvi;
    char *s, *s2;

1352
    dp->flags = 0;
1353
    if (!ao)
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
        return 0;
    if (Tgettype (ao) != T_TABLE)
        return -1;
    for (Tgetfirst (ao, &tkvi); tkvi.kvp; Tgetnext (&tkvi)) {
        if (Tgettype (tkvi.kvp->ko) != T_STRING)
            continue;
        s = Tgetstring (tkvi.kvp->ko);
        if (strcmp (s, "color") == 0) {
            getaintnset (tkvi.kvp->vo, dp->color, G_GATTRCOLOR);
        } else if (strcmp (s, "width") == 0) {
            getaintnset (tkvi.kvp->vo, dp->width, G_GATTRWIDTH);
        } else if (strcmp (s, "mode") == 0) {
            getastrnset (tkvi.kvp->vo, s2, G_GATTRMODE);
            if (dp->flags & G_GATTRMODE) {
                if (strcmp (s2, "src") == 0)
                    dp->mode = G_SRC;
                else if (strcmp (s2, "xor") == 0)
                    dp->mode = G_XOR;
                else
                    dp->flags &= ~G_GATTRMODE;
            }
        } else if (strcmp (s, "fill") == 0) {
            getastrnset (tkvi.kvp->vo, s2, G_GATTRFILL);
            if (dp->flags & G_GATTRFILL) {
                if (strcmp (s2, "on") == 0)
                    dp->fill = 1;
                else if (strcmp (s2, "off") == 0)
                    dp->fill = 0;
                else
                    dp->flags &= ~G_GATTRFILL;
            }
        } else if (strcmp (s, "style") == 0) {
            getastrnset (tkvi.kvp->vo, s2, G_GATTRSTYLE);
            if (dp->flags & G_GATTRSTYLE) {
                if (strcmp (s2, "solid") == 0)
                    dp->style = G_SOLID;
                else if (strcmp (s2, "dashed") == 0)
                    dp->style = G_DASHED;
                else if (strcmp (s2, "dotted") == 0)
                    dp->style = G_DOTTED;
                else if (strcmp (s2, "longdashed") == 0)
                    dp->style = G_LONGDASHED;
                else if (strcmp (s2, "shortdashed") == 0)
                    dp->style = G_SHORTDASHED;
                else
                    dp->flags &= ~G_GATTRSTYLE;
            }
        }
1402 1403 1404 1405
    }
    return 0;
}

1406 1407 1408 1409 1410 1411 1412
static int getrect (Tobj ro, Grect_t *rp) {
    if (ro && Tgettype (ro) == T_TABLE)
        if (
            getxy (Tfindi (ro, 0), &rp->o) != -1 &&
            getxy (Tfindi (ro, 1), &rp->c) != -1
        )
            return 0;
1413 1414 1415
    return -1;
}

1416
static int getxy (Tobj po, Gxy_t *xyp) {
1417 1418
    Tobj xo, yo;

1419 1420 1421 1422 1423 1424
    if (po && Tgettype (po) == T_TABLE) {
        xo = Tfinds (po, "x"), yo = Tfinds (po, "y");
        if (xo && T_ISNUMBER (xo) && yo && T_ISNUMBER (yo)) {
            xyp->x = Tgetnumber (xo), xyp->y = Tgetnumber (yo);
            return 0;
        }
1425 1426 1427 1428
    }
    return -1;
}

1429 1430 1431 1432
static int getint (Tobj to, int *ip) {
    if (to && T_ISNUMBER (to)) {
        *ip = Tgetnumber (to);
        return 0;
1433 1434 1435 1436
    }
    return -1;
}

1437 1438 1439 1440
static int getdouble (Tobj to, double *dp) {
    if (to && T_ISNUMBER (to)) {
        *dp = Tgetnumber (to);
        return 0;
1441 1442 1443 1444
    }
    return -1;
}

1445 1446 1447 1448
static int getstr (Tobj so, char **s) {
    if (so && T_ISSTRING (so)) {
        *s = Tgetstring (so);
        return 0;
1449 1450 1451 1452
    }
    return -1;
}

1453
static int getcolor (Tobj ko, Tobj co, Gcolor_t *cp) {
1454 1455
    Tobj ro, go, bo, ho, so, vo;
    char *s, *s1, *s2;
1456
    float hc, sc, vc;
1457
    int cni;
1458
    int r, g, b, a;
1459

1460 1461
    if (ko && T_ISNUMBER (ko))
        cp->index = Tgetnumber (ko);
1462
    else
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
        return -1;
    if (!co || !(T_ISSTRING (co) || T_ISTABLE (co)))
        return -1;
    if (T_ISSTRING (co)) {
        s = Tgetstring (co);
        while (*s == ' ')
            s++; /* skip over any leading spaces */
        if (*s == '#' && sscanf (s, "#%2x%2x%2x%2x", &r, &g, &b, &a) >= 3) {
            cp->r = r;
            cp->g = g;
            cp->b = b;
            return 0;
        }
        if ((isdigit (*s) || *s == '.') && scanhsv (s, &hc, &sc, &vc)) {
            hsv2rgb (hc, sc, vc, cp);
            return 0;
        }
        for (cni = 0; cni < sizeof (colornames) / sizeof (colorname_t); cni++) {
            for (s1 = colornames[cni].name, s2 = s; *s1 && *s2; s1++, s2++) {
                if (*s2 == ' ' || *s2 == '_')
                    s2++;
                else if (*s1 != *s2)
                    break;
            }
            if (*s1 != *s2)
                continue;
            cp->r = colornames[cni].r;
            cp->g = colornames[cni].g;
            cp->b = colornames[cni].b;
            return 0;
        }
        return -1;
1495
    } else {
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
        ro = Tfinds (co, "r");
        go = Tfinds (co, "g");
        bo = Tfinds (co, "b");
        if (
            ro && T_ISNUMBER (ro) && go && T_ISNUMBER (go) &&
            bo && T_ISNUMBER (bo)
        ) {
            cp->r = Tgetnumber (ro);
            cp->g = Tgetnumber (go);
            cp->b = Tgetnumber (bo);
            return 0;
        } else {
            ho = Tfinds (co, "h");
            so = Tfinds (co, "s");
            vo = Tfinds (co, "v");
            if (
                ho && T_ISNUMBER (ho) && so && T_ISNUMBER (so) &&
                vo && T_ISNUMBER (vo)
            ) {
                hc = Tgetnumber (ho);
                sc = Tgetnumber (so);
                vc = Tgetnumber (vo);
                hsv2rgb (hc, sc, vc, cp);
                return 0;
            } else
                return -1;
        }
1523
    }
1524
    return -1;
1525 1526
}

1527
static void nodeinit (int ni) {
1528 1529 1530
    int li, ki;

    for (li = 0; li < LISTSIZE; li++)
1531
        gfxnodes[ni].rect[li] = NULL, gfxnodes[ni].menu[li] = NULL;
1532 1533
    gfxnodes[ni].ls = gfxnodes[ni].ms = gfxnodes[ni].rs = 0;
    for (ki = 0; ki < 256; ki++)
1534
        gfxnodes[ni].ks[ki] = 0;
1535
    gfxnodes[ni].plvo = gfxnodes[ni].pmvo = gfxnodes[ni].prvo = 0;
1536
    gfxnodes[ni].pb3vo = gfxnodes[ni].pb4vo = 0;
1537
    for (ki = 0; ki < 256; ki++)
1538
        gfxnodes[ni].pkvo[ki] = 0;
1539 1540
}

1541
static void nodeterm (int ni) {
1542 1543 1544 1545 1546 1547 1548
    gfxrect_t **rp;
    gfxrect_t *crp, *nrp;
    gfxmenu_t **mp;
    gfxmenu_t *cmp, *nmp;
    int li;

    for (li = 0; li < LISTSIZE; li++) {
1549 1550 1551 1552 1553 1554 1555 1556
        rp = &gfxnodes[ni].rect[li];
        for (crp = *rp; crp; crp = nrp)
            nrp = crp->next, free (crp);
        *rp = NULL;
        mp = &gfxnodes[ni].menu[li];
        for (cmp = *mp; cmp; cmp = nmp)
            nmp = cmp->next, free (cmp);
        *mp = NULL;
1557 1558 1559
    }
}

1560
static void rectinit (int ni) {
1561 1562 1563
    int li;

    for (li = 0; li < LISTSIZE; li++)
1564
        gfxnodes[ni].rect[li] = NULL;
1565 1566
}

1567
static void rectterm (int ni) {
1568 1569 1570 1571 1572
    gfxrect_t **rp;
    gfxrect_t *crp, *nrp;
    int li;

    for (li = 0; li < LISTSIZE; li++) {
1573 1574 1575 1576
        rp = &gfxnodes[ni].rect[li];
        for (crp = *rp; crp; crp = nrp)
            nrp = crp->next, free (crp);
        *rp = NULL;
1577 1578 1579
    }
}

1580
static void rectinsert (int ni, Tobj ko, Grect_t r) {
1581 1582 1583 1584 1585
    gfxrect_t **rp;
    gfxrect_t *crp;

    rp = &gfxnodes[ni].rect[(unsigned long) ko % LISTSIZE];
    for (crp = *rp; crp; crp = crp->next)
1586 1587 1588 1589 1590 1591 1592 1593
        if (crp->ko == ko) {
            crp->r.o.x = min (r.o.x, r.c.x);
            crp->r.o.y = min (r