~ubuntu-branches/ubuntu/lucid/graphviz/lucid-security

« back to all changes in this revision

Viewing changes to lefty/ws/x11/garray.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2002-02-05 18:52:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020205185212-8i04c70te00rc40y
Tags: upstream-1.7.16
ImportĀ upstreamĀ versionĀ 1.7.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#pragma prototyped
 
2
/* Lefteris Koutsofios - AT&T Bell Laboratories */
 
3
 
 
4
#include "common.h"
 
5
#include "g.h"
 
6
#include "gcommon.h"
 
7
#include "mem.h"
 
8
 
 
9
#define WAU widget->u.a
 
10
 
 
11
extern WidgetClass arrayWidgetClass;
 
12
 
 
13
static void awcallback (Widget, XtPointer, XtPointer);
 
14
 
 
15
int GAcreatewidget (Gwidget_t *parent, Gwidget_t *widget,
 
16
        int attrn, Gwattr_t *attrp) {
 
17
    PIXsize_t ps;
 
18
    int ai;
 
19
    XColor c;
 
20
    int color;
 
21
 
 
22
    if (!parent) {
 
23
        Gerr (POS, G_ERRNOPARENTWIDGET);
 
24
        return -1;
 
25
    }
 
26
    WAU->func = NULL;
 
27
    ps.x = ps.y = MINAWSIZE;
 
28
    RESETARGS;
 
29
    for (ai = 0; ai < attrn; ai++) {
 
30
        switch (attrp[ai].id) {
 
31
        case G_ATTRSIZE:
 
32
            GETSIZE (attrp[ai].u.s, ps, MINAWSIZE);
 
33
            break;
 
34
        case G_ATTRBORDERWIDTH:
 
35
            ADD2ARGS (XtNborderWidth, attrp[ai].u.i);
 
36
            break;
 
37
        case G_ATTRMODE:
 
38
            if (Strcmp ("horizontal", attrp[ai].u.t) == 0) {
 
39
                ADD2ARGS (XtNorientation, XtorientHorizontal);
 
40
                WAU->mode = G_AWHARRAY;
 
41
            } else if (Strcmp ("vertical", attrp[ai].u.t) == 0) {
 
42
                WAU->mode = G_AWVARRAY;
 
43
            } else {
 
44
                Gerr (POS, G_ERRBADATTRVALUE, attrp[ai].u.t);
 
45
                return -1;
 
46
            }
 
47
            break;
 
48
        case G_ATTRLAYOUT:
 
49
            if (Strcmp ("on", attrp[ai].u.t) == 0)
 
50
                Gawsetmode (widget, FALSE);
 
51
            else if (Strcmp ("off", attrp[ai].u.t) == 0)
 
52
                Gawsetmode (widget, TRUE);
 
53
            else {
 
54
                Gerr (POS, G_ERRBADATTRVALUE, attrp[ai].u.t);
 
55
                return -1;
 
56
            }
 
57
            break;
 
58
        case G_ATTRCOLOR:
 
59
            color = attrp[ai].u.c.index;
 
60
            if (color != 0 && color != 1) {
 
61
                Gerr (POS, G_ERRBADCOLORINDEX, color);
 
62
                return -1;
 
63
            }
 
64
            c.red = attrp[ai].u.c.r * 257;
 
65
            c.green = attrp[ai].u.c.g * 257;
 
66
            c.blue = attrp[ai].u.c.b * 257;
 
67
            if (XAllocColor (
 
68
                Gdisplay, DefaultColormap (Gdisplay, Gscreenn), &c
 
69
            ))
 
70
                if (color == 0)
 
71
                    ADD2ARGS (XtNbackground, c.pixel);
 
72
                else
 
73
                    ADD2ARGS (XtNforeground, c.pixel);
 
74
            break;
 
75
        case G_ATTRWINDOWID:
 
76
            Gerr (POS, G_ERRCANNOTSETATTR1, "windowid");
 
77
            return -1;
 
78
        case G_ATTRRESIZECB:
 
79
            WAU->func = (Gawcoordscb) attrp[ai].u.func;
 
80
            break;
 
81
        case G_ATTRUSERDATA:
 
82
            widget->udata = attrp[ai].u.u;
 
83
            break;
 
84
        default:
 
85
            Gerr (POS, G_ERRBADATTRID, attrp[ai].id);
 
86
            return -1;
 
87
        }
 
88
    }
 
89
    ADD2ARGS (XtNwidth, ps.x);
 
90
    ADD2ARGS (XtNheight, ps.y);
 
91
    if (!(widget->w = XtCreateWidget ("array", arrayWidgetClass,
 
92
            parent->w, argp, argn))) {
 
93
        Gerr (POS, G_ERRCANNOTCREATEWIDGET);
 
94
        return -1;
 
95
    }
 
96
    XtAddCallback (widget->w, XtNcallback, awcallback, (XtPointer) NULL);
 
97
    Glazymanage (widget->w);
 
98
    return 0;
 
99
}
 
100
 
 
101
int GAsetwidgetattr (Gwidget_t *widget, int attrn, Gwattr_t *attrp) {
 
102
    PIXsize_t ps;
 
103
    int ai;
 
104
 
 
105
    RESETARGS;
 
106
    for (ai = 0; ai < attrn; ai++) {
 
107
        switch (attrp[ai].id) {
 
108
        case G_ATTRSIZE:
 
109
            GETSIZE (attrp[ai].u.s, ps, MINAWSIZE);
 
110
            ADD2ARGS (XtNwidth, ps.x);
 
111
            ADD2ARGS (XtNheight, ps.y);
 
112
            break;
 
113
        case G_ATTRBORDERWIDTH:
 
114
            ADD2ARGS (XtNborderWidth, attrp[ai].u.i);
 
115
            break;
 
116
        case G_ATTRMODE:
 
117
            Gerr (POS, G_ERRCANNOTSETATTR2, "mode");
 
118
            return -1;
 
119
        case G_ATTRLAYOUT:
 
120
            if (Strcmp ("on", attrp[ai].u.t) == 0)
 
121
                Gawsetmode (widget, FALSE);
 
122
            else if (Strcmp ("off", attrp[ai].u.t) == 0)
 
123
                Gawsetmode (widget, TRUE);
 
124
            else {
 
125
                Gerr (POS, G_ERRBADATTRVALUE, attrp[ai].u.t);
 
126
                return -1;
 
127
            }
 
128
            break;
 
129
        case G_ATTRWINDOWID:
 
130
            Gerr (POS, G_ERRCANNOTSETATTR2, "windowid");
 
131
            return -1;
 
132
        case G_ATTRRESIZECB:
 
133
            WAU->func = (Gawcoordscb) attrp[ai].u.func;
 
134
            break;
 
135
        case G_ATTRUSERDATA:
 
136
            widget->udata = attrp[ai].u.u;
 
137
            break;
 
138
        default:
 
139
            Gerr (POS, G_ERRBADATTRID, attrp[ai].id);
 
140
            return -1;
 
141
        }
 
142
    }
 
143
    XtSetValues (widget->w, argp, argn);
 
144
    return 0;
 
145
}
 
146
 
 
147
int GAgetwidgetattr (Gwidget_t *widget, int attrn, Gwattr_t *attrp) {
 
148
    Dimension width, height;
 
149
    int ai;
 
150
 
 
151
    for (ai = 0; ai < attrn; ai++) {
 
152
        RESETARGS;
 
153
        switch (attrp[ai].id) {
 
154
        case G_ATTRSIZE:
 
155
            ADD2ARGS (XtNwidth, &width);
 
156
            ADD2ARGS (XtNheight, &height);
 
157
            XtGetValues (widget->w, argp, argn);
 
158
            attrp[ai].u.s.x = width, attrp[ai].u.s.y = height;
 
159
            break;
 
160
        case G_ATTRBORDERWIDTH:
 
161
            ADD2ARGS (XtNborderWidth, &width);
 
162
            XtGetValues (widget->w, argp, argn);
 
163
            attrp[ai].u.i = width;
 
164
            break;
 
165
        case G_ATTRMODE:
 
166
            attrp[ai].u.t = (WAU->mode == G_AWHARRAY) ?
 
167
                    "horizontal" : "vertical";
 
168
            break;
 
169
        case G_ATTRLAYOUT:
 
170
            attrp[ai].u.t = (Gawgetmode (widget)) ? "off" : "on";
 
171
            break;
 
172
        case G_ATTRWINDOWID:
 
173
            sprintf (&Gbufp[0], "0x%lx", XtWindow (widget->w));
 
174
            attrp[ai].u.t = &Gbufp[0];
 
175
            break;
 
176
        case G_ATTRRESIZECB:
 
177
            attrp[ai].u.func = WAU->func;
 
178
            break;
 
179
        case G_ATTRUSERDATA:
 
180
            attrp[ai].u.u = widget->udata;
 
181
            break;
 
182
        default:
 
183
            Gerr (POS, G_ERRBADATTRID, attrp[ai].id);
 
184
            return -1;
 
185
        }
 
186
    }
 
187
    return 0;
 
188
}
 
189
 
 
190
int GAdestroywidget (Gwidget_t *widget) {
 
191
    XtDestroyWidget (widget->w);
 
192
    return 0;
 
193
}
 
194
 
 
195
static void awcallback (Widget w, XtPointer clientdata, XtPointer calldata) {
 
196
    Gwidget_t *widget;
 
197
 
 
198
    if (!(widget = findwidget ((unsigned long) w, G_ARRAYWIDGET)))
 
199
        return;
 
200
    if (WAU->func)
 
201
        (*WAU->func) (widget - &Gwidgets[0], (Gawdata_t *) calldata);
 
202
    else
 
203
        Gawdefcoordscb (widget - &Gwidgets[0], (Gawdata_t *) calldata);
 
204
}
 
205
 
 
206
/* the rest of this file contains the implementation of the array widget */
 
207
 
 
208
#include <X11/IntrinsicP.h>
 
209
 
 
210
typedef struct _ArrayClassRec *ArrayWidgetClass;
 
211
typedef struct _ArrayRec *ArrayWidget;
 
212
 
 
213
typedef struct _ArrayClassPart {
 
214
    int dummy; /* not used */
 
215
} ArrayClassPart;
 
216
 
 
217
typedef struct _ArrayClassRec {
 
218
    CoreClassPart core_class;
 
219
    CompositeClassPart composite_class;
 
220
    ArrayClassPart array_class;
 
221
} ArrayClassRec;
 
222
 
 
223
typedef struct _ArrayPart {
 
224
    XtCallbackList callbacks;
 
225
    XtOrientation orientation;
 
226
    Gawdata_t data;
 
227
    int batchmode;
 
228
} ArrayPart;
 
229
 
 
230
typedef struct _ArrayRec {
 
231
    CorePart core;
 
232
    CompositePart composite;
 
233
    ArrayPart array;
 
234
} ArrayRec;
 
235
 
 
236
#define CHILDINCR 10
 
237
#define CHILDSIZE sizeof (Gawcarray_t)
 
238
 
 
239
static XtResource resources[] = {
 
240
    {
 
241
        XtNcallback,
 
242
        XtCCallback,
 
243
        XtRCallback,
 
244
        sizeof (XtPointer),
 
245
        XtOffsetOf (ArrayRec, array.callbacks),
 
246
        XtRCallback, (XtPointer) NULL
 
247
    },
 
248
    {
 
249
        XtNorientation,
 
250
        XtCOrientation,
 
251
        XtROrientation,
 
252
        sizeof (XtOrientation),
 
253
        XtOffsetOf (ArrayRec, array.orientation),
 
254
        XtRImmediate, (XtPointer) XtorientVertical
 
255
    }
 
256
};
 
257
 
 
258
static void ClassInitialize (void);
 
259
static void Initialize (Widget, Widget, ArgList, Cardinal *);
 
260
static void Destroy (Widget);
 
261
static void Resize (Widget);
 
262
static Boolean SetValues (Widget, Widget, Widget, ArgList, Cardinal *);
 
263
static XtGeometryResult GeometryManager(Widget,
 
264
        XtWidgetGeometry *, XtWidgetGeometry *);
 
265
static void ChangeManaged (Widget);
 
266
static void InsertChild (Widget);
 
267
static void DeleteChild (Widget);
 
268
static void dolayout (ArrayWidget, int);
 
269
 
 
270
ArrayClassRec arrayClassRec = {
 
271
    { /* core_class fields */
 
272
        /* superclass          */ (WidgetClass) &compositeClassRec,
 
273
        /* class_name          */ "Array",
 
274
        /* widget_size         */ sizeof(ArrayRec),
 
275
        /* class_initialize    */ ClassInitialize,
 
276
        /* class_part_init     */ NULL,
 
277
        /* class_inited        */ FALSE,
 
278
        /* initialize          */ Initialize,
 
279
        /* initialize_hook     */ NULL,
 
280
        /* realize             */ XtInheritRealize,
 
281
        /* actions             */ NULL,
 
282
        /* num_actions         */ 0,
 
283
        /* resources           */ resources,
 
284
        /* num_resources       */ XtNumber (resources),
 
285
        /* xrm_class           */ NULLQUARK,
 
286
        /* compress_motion     */ TRUE,
 
287
        /* compress_exposure   */ TRUE,
 
288
        /* compress_enterleave */ TRUE,
 
289
        /* visible_interest    */ FALSE,
 
290
        /* destroy             */ Destroy,
 
291
        /* resize              */ Resize,
 
292
        /* expose              */ XtInheritExpose,
 
293
        /* set_values          */ SetValues,
 
294
        /* set_values_hook     */ NULL,
 
295
        /* set_values_almost   */ XtInheritSetValuesAlmost,
 
296
        /* get_values_hook     */ NULL,
 
297
        /* accept_focus        */ NULL,
 
298
        /* version             */ XtVersion,
 
299
        /* callback_private    */ NULL,
 
300
        /* tm_table            */ NULL,
 
301
        /* query_geometry      */ XtInheritQueryGeometry,
 
302
        /* display_accelerator */ XtInheritDisplayAccelerator,
 
303
        /* extension           */ NULL
 
304
    },
 
305
    { /* composite_class fields */
 
306
        /* geometry_manager */ GeometryManager,
 
307
        /* change_managed   */ ChangeManaged,
 
308
        /* insert_child     */ InsertChild,
 
309
        /* delete_child     */ DeleteChild,
 
310
        /* extension        */ NULL
 
311
    },
 
312
    { /* array_class fields */
 
313
        /* dummy */ 0
 
314
    }
 
315
};
 
316
 
 
317
WidgetClass arrayWidgetClass = (WidgetClass)&arrayClassRec;
 
318
 
 
319
int Gaworder (Gwidget_t *widget, void *data, Gawordercb func) {
 
320
    ArrayWidget aw;
 
321
 
 
322
    aw = (ArrayWidget) widget->w;
 
323
    (*func) (data, &aw->array.data);
 
324
    dolayout (aw, TRUE);
 
325
    return 0;
 
326
}
 
327
 
 
328
int Gawsetmode (Gwidget_t *widget, int mode) {
 
329
    ArrayWidget aw;
 
330
 
 
331
    aw = (ArrayWidget) widget->w;
 
332
    aw->array.batchmode = mode;
 
333
    dolayout (aw, TRUE);
 
334
    return 0;
 
335
}
 
336
 
 
337
int Gawgetmode (Gwidget_t *widget) {
 
338
    ArrayWidget aw;
 
339
 
 
340
    aw = (ArrayWidget) widget->w;
 
341
    return aw->array.batchmode;
 
342
}
 
343
 
 
344
void Gawdefcoordscb (int wi, Gawdata_t *dp) {
 
345
    Gawcarray_t *cp;
 
346
    int sx, sy, csx, csy, ci;
 
347
 
 
348
    sx = dp->sx, sy = dp->sy;
 
349
    csx = csy = 0;
 
350
    for (ci = 0; ci < dp->cj; ci++) {
 
351
        cp = &dp->carray[ci];
 
352
        if (!cp->flag)
 
353
            continue;
 
354
        cp->ox = csx, cp->oy = csy;
 
355
        if (dp->type == G_AWVARRAY)
 
356
            cp->sx = sx - 2 * cp->bs, csy += cp->sy + 2 * cp->bs;
 
357
        else
 
358
            cp->sy = sy - 2 * cp->bs, csx += cp->sx + 2 * cp->bs;
 
359
    }
 
360
    if (dp->type == G_AWVARRAY)
 
361
        dp->sy = csy;
 
362
    else
 
363
        dp->sx = csx;
 
364
}
 
365
 
 
366
static void ClassInitialize (void) {
 
367
    XtAddConverter( XtRString, XtROrientation, XmuCvtStringToOrientation,
 
368
            NULL, (Cardinal)0 );
 
369
}
 
370
 
 
371
static void Initialize (Widget reqw, Widget neww,
 
372
        ArgList args, Cardinal *num_args) {
 
373
    ArrayWidget aw;
 
374
 
 
375
    aw = (ArrayWidget) neww;
 
376
    if (aw->array.orientation == XtorientVertical)
 
377
        aw->array.data.type = G_AWVARRAY;
 
378
    else
 
379
        aw->array.data.type = G_AWHARRAY;
 
380
    aw->array.data.carray = Marrayalloc ((long) CHILDINCR * CHILDSIZE);
 
381
    aw->array.data.cn = CHILDINCR;
 
382
    aw->array.data.cj = 0;
 
383
    aw->array.batchmode = FALSE;
 
384
    if (aw->core.width == 0)
 
385
        aw->core.width = 100;
 
386
    if (aw->core.height == 0)
 
387
        aw->core.height = 100;
 
388
}
 
389
 
 
390
static void Destroy (Widget w) {
 
391
    ArrayWidget aw;
 
392
 
 
393
    aw = (ArrayWidget) w;
 
394
    Marrayfree (aw->array.data.carray);
 
395
    aw->array.data.cn = aw->array.data.cj = 0;
 
396
}
 
397
 
 
398
static void Resize (Widget w) {
 
399
    dolayout ((ArrayWidget) w, FALSE);
 
400
}
 
401
 
 
402
static Boolean SetValues (Widget curw, Widget reqw, Widget neww,
 
403
        ArgList args, Cardinal *num_args) {
 
404
    ArrayWidget curaw;
 
405
    ArrayWidget newaw;
 
406
 
 
407
    curaw = (ArrayWidget) curw;
 
408
    newaw = (ArrayWidget) neww;
 
409
    if (curaw->array.orientation != newaw->array.orientation) {
 
410
        if (newaw->array.orientation == XtorientVertical)
 
411
            newaw->array.data.type = G_AWVARRAY;
 
412
        else
 
413
            newaw->array.data.type = G_AWHARRAY;
 
414
        dolayout (newaw, TRUE);
 
415
        return TRUE;
 
416
    }
 
417
    return FALSE;
 
418
}
 
419
 
 
420
static XtGeometryResult GeometryManager(Widget w,
 
421
        XtWidgetGeometry *req, XtWidgetGeometry *rep) {
 
422
    Dimension width, height;
 
423
 
 
424
    if (req->request_mode & ~(CWX | CWY | CWWidth | CWHeight))
 
425
        return XtGeometryNo;
 
426
 
 
427
    if (req->request_mode & (CWX | CWY | CWWidth | CWHeight)) {
 
428
        width = (req->request_mode & CWWidth) ? req->width : w->core.width;
 
429
        height = (req->request_mode & CWHeight) ? req->height : w->core.height;
 
430
        w->core.width = width, w->core.height = height;
 
431
        dolayout ((ArrayWidget) XtParent (w), TRUE);
 
432
        return XtGeometryYes;
 
433
    }
 
434
    return XtGeometryYes;
 
435
}
 
436
 
 
437
static void ChangeManaged (Widget w) {
 
438
    ArrayWidget aw;
 
439
 
 
440
    aw = (ArrayWidget) w;
 
441
    if (!aw->array.batchmode)
 
442
        dolayout (aw, TRUE);
 
443
}
 
444
 
 
445
static void InsertChild (Widget w) {
 
446
    ArrayWidget aw;
 
447
    CompositeWidgetClass sclass;
 
448
 
 
449
    sclass = (CompositeWidgetClass) compositeWidgetClass;
 
450
    (*sclass->composite_class.insert_child) (w);
 
451
    aw = (ArrayWidget) XtParent (w);
 
452
    if (aw->array.data.cj == aw->array.data.cn) {
 
453
        aw->array.data.carray = Marraygrow (aw->array.data.carray,
 
454
                (long) (aw->array.data.cn + CHILDINCR) * CHILDSIZE);
 
455
        aw->array.data.cn += CHILDINCR;
 
456
    }
 
457
    aw->array.data.carray[aw->array.data.cj++].w = w;
 
458
}
 
459
 
 
460
static void DeleteChild (Widget w) {
 
461
    ArrayWidget aw;
 
462
    CompositeWidgetClass sclass;
 
463
    int ci;
 
464
 
 
465
    sclass = (CompositeWidgetClass) compositeWidgetClass;
 
466
    (*sclass->composite_class.delete_child) (w);
 
467
    aw = (ArrayWidget) XtParent (w);
 
468
    for (ci = 0; ci < aw->array.data.cj; ci++)
 
469
        if (aw->array.data.carray[ci].w == w)
 
470
            break;
 
471
    if (ci < aw->array.data.cj) {
 
472
        for (; ci + 1 < aw->array.data.cj; ci++)
 
473
            aw->array.data.carray[ci].w = aw->array.data.carray[ci + 1].w;
 
474
        aw->array.data.cj--;
 
475
    }
 
476
}
 
477
 
 
478
static void dolayout (ArrayWidget aw, int flag) {
 
479
    XtWidgetGeometry req, ret_req;
 
480
    Gawdata_t *dp;
 
481
    Gawcarray_t *cp;
 
482
    int sx, sy, ci;
 
483
 
 
484
    if (aw->array.batchmode)
 
485
        return;
 
486
    dp = &aw->array.data;
 
487
    for (ci = 0; ci < dp->cj; ci++) {
 
488
        if (!XtIsManaged (dp->carray[ci].w)) {
 
489
            dp->carray[ci].flag = 0;
 
490
            continue;
 
491
        }
 
492
        cp = &dp->carray[ci];
 
493
        cp->flag = 1;
 
494
        cp->ox = cp->w->core.x;
 
495
        cp->oy = cp->w->core.y;
 
496
        cp->sx = cp->w->core.width;
 
497
        cp->sy = cp->w->core.height;
 
498
        cp->bs = cp->w->core.border_width;
 
499
    }
 
500
    dp->sx = aw->core.width, dp->sy = aw->core.height;
 
501
    XtCallCallbackList ((Widget) aw, aw->array.callbacks, dp);
 
502
    if ((sx = dp->sx) < MINAWSIZE)
 
503
        sx = MINAWSIZE;
 
504
    if ((sy = dp->sy) < MINAWSIZE)
 
505
        sy = MINAWSIZE;
 
506
    if (flag && (aw->core.width != sx || aw->core.height != sy)) {
 
507
        req.width = sx, req.height = sy;
 
508
        req.request_mode = CWWidth | CWHeight;
 
509
        if (XtMakeGeometryRequest ((Widget) aw, &req, &ret_req) ==
 
510
                XtGeometryAlmost) {
 
511
            req = ret_req;
 
512
            XtMakeGeometryRequest ((Widget) aw, &req, &ret_req);
 
513
            dp->sx = req.width, dp->sy = req.height;
 
514
            XtCallCallbackList ((Widget) aw, aw->array.callbacks, dp);
 
515
        }
 
516
    }
 
517
    for (ci = 0; ci < dp->cj; ci++) {
 
518
        cp = &dp->carray[ci];
 
519
        if (!cp->flag)
 
520
            continue;
 
521
        XtConfigureWidget (cp->w, cp->ox, cp->oy, cp->sx, cp->sy, cp->bs);
 
522
    }
 
523
}