File: mui.c

package info (click to toggle)
glut 3.7-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 12,360 kB
  • ctags: 39,914
  • sloc: ansic: 148,715; makefile: 28,471; ada: 2,062; yacc: 473; fortran: 290; lex: 131; csh: 51; sed: 49; sh: 33
file content (638 lines) | stat: -rw-r--r-- 15,671 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
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
628
629
630
631
632
633
634
635
636
637
638
/*
 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
 * ALL RIGHTS RESERVED 
 * Permission to use, copy, modify, and distribute this software for 
 * any purpose and without fee is hereby granted, provided that the above
 * copyright notice appear in all copies and that both the copyright notice
 * and this permission notice appear in supporting documentation, and that 
 * the name of Silicon Graphics, Inc. not be used in advertising
 * or publicity pertaining to distribution of the software without specific,
 * written prior permission. 
 *
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 * US Government Users Restricted Rights 
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
 * clause at DFARS 252.227-7013 and/or in similar or successor
 * clauses in the FAR or the DOD or NASA FAR Supplement.
 * Unpublished-- rights reserved under the copyright laws of the
 * United States.  Contractor/manufacturer is Silicon Graphics,
 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
 *
 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
 */

#include <mui/gizmo.h>
#include <stdio.h>
#include <GL/glut.h>
#include <stdlib.h>

extern int activemenu;
extern int menuinuse;

static muiObject *newmuiobj(void)
{
    muiObject *newobj = (muiObject *)malloc(sizeof(muiObject));
    newobj->active = 0;
    newobj->enable = 1;
    newobj->select = 0;
    newobj->locate = 0;
    newobj->visible = 1;
    newobj->callback = 0;
    muiSetUIList(newobj, muiGetActiveUIList());
    muiAddToUIList(muiGetActiveUIList(), newobj);
    newobj->id = 0;
    return newobj;
}

muiObject *muiNewPulldown(void)
{
    muiObject *newPD = newmuiobj();
    newPD->type = MUI_PULLDOWN;
    newPD->xmin = 0;
    newPD->ymin = glutGet(GLUT_WINDOW_HEIGHT) - PULLDOWN_HEIGHT;
    newPD->xmax = glutGet(GLUT_WINDOW_WIDTH);
    newPD->ymax = glutGet(GLUT_WINDOW_HEIGHT);
    newPD->object = (void *)newpd();
    newPD->handler = pdhandler;
    return newPD;
}

muiObject *muiNewButton(int xmin, int xmax, int ymin, int ymax)
{
    muiObject *newb = newmuiobj();
    newb->type = MUI_BUTTON;
    newb->xmin = xmin;
    newb->ymin = ymin;
    newb->xmax = xmax;
    newb->ymax = ymax;
    newb->object = (void *)newbut();
    ((Button *)(newb->object))->object = newb;
    newb->handler = buttonhandler;
    return newb;
}

muiObject *muiNewRadioButton(int xmin, int ymin)
{
    muiObject *newb = newmuiobj();
    newb->type = MUI_RADIOBUTTON;
    newb->xmin = xmin;
    newb->ymin = ymin;
    newb->xmax = xmin+ RADIOWIDTH-1;
    newb->ymax = ymin+RADIOHEIGHT-1;
    newb->object = (void *)newradiobut();
    ((Button *)(newb->object))->object = newb;
    newb->handler = buttonhandler;
    return newb;
}

muiObject *muiNewTinyRadioButton(int xmin, int ymin)
{
    muiObject *newb = newmuiobj();
    newb->type = MUI_TINYRADIOBUTTON;
    newb->xmin = xmin;
    newb->ymin = ymin;
    newb->xmax = xmin+ TINYRADIOWIDTH-1;
    newb->ymax = ymin+ TINYRADIOHEIGHT-1;
    newb->object = (void *)newradiobut();
    ((Button *)(newb->object))->object = newb;
    newb->handler = buttonhandler;
    return newb;
}

muiObject *muiNewVSlider(int xmin, int ymin, int ymax, int scenter, int shalf)
{
    muiObject *newvsl = newmuiobj();
    newvsl->type = MUI_VSLIDER;
    newvsl->xmin = xmin;
    newvsl->ymin = ymin;
    newvsl->xmax = xmin+SLIDERWIDTH-1;
    newvsl->ymax = ymax;
    newvsl->object = (void *)newvs(newvsl, ymin, ymax, scenter, shalf);
    newvsl->handler = vshandler;
    return newvsl;
}

muiObject *muiNewHSlider(int xmin, int ymin, int xmax, int scenter, int shalf)
{
    muiObject *newhsl = newmuiobj();
    newhsl->type = MUI_HSLIDER;
    newhsl->xmin = xmin;
    newhsl->ymin = ymin;
    newhsl->xmax = xmax;
    newhsl->ymax = ymin+SLIDERWIDTH-1;
    newhsl->object = (void *)newhs(newhsl, xmin, xmax, scenter, shalf);
    newhsl->handler = hshandler;
    return newhsl;
}

muiObject *muiNewTextList(int xmin, int ymin, int xmax, int listheight)
{
    muiObject *newtlo = newmuiobj();
    newtlo->type = MUI_TEXTLIST;
    newtlo->xmin = xmin;
    newtlo->ymin = ymin;
    newtlo->xmax = xmax;
    newtlo->object = (void *)newtl(newtlo, listheight);
    newtlo->handler = tlhandler;
    return newtlo;
}

muiObject *muiNewLabel(int xmin, int ymin, char *label)
{
    muiObject *newlbl = newmuiobj();
    newlbl->type = MUI_LABEL;
    newlbl->xmin = xmin;
    newlbl->ymin = ymin;
    /* XXX maximums */
    newlbl->object = (void *)newlabel(label);
    newlbl->handler = nullhandler;
    return newlbl;
}

muiObject *muiNewBoldLabel(int xmin, int ymin, char *label)
{
    muiObject *newlbl = newmuiobj();
    newlbl->type = MUI_BOLDLABEL;
    newlbl->xmin = xmin;
    newlbl->ymin = ymin;
    /* XXX maximums */
    newlbl->object = (void *)newlabel(label);
    newlbl->handler = nullhandler;
    return newlbl;
}

muiObject *muiNewTextbox(int xmin, int xmax, int ymin)
{
    muiObject *newtextbox = newmuiobj();
    newtextbox->type = MUI_TEXTBOX;
    newtextbox->xmin = xmin;
    newtextbox->ymin = ymin;
    newtextbox->xmax = xmax;
    newtextbox->ymax = ymin + TEXTBOXHEIGHT - 1;
    newtextbox->object = (void *)newtb(xmin, xmax);
    newtextbox->handler = textboxhandler;
    return newtextbox;
}

void muiGetObjectSize(muiObject *obj, int *xmin, int *ymin, int *xmax, int *ymax)
{
    *xmin = obj->xmin;
    *xmax = obj->xmax;
    *ymin = obj->ymin;
    *ymax = obj->ymax;
}

void muiFreeObject(muiObject *obj)
{
    switch (obj->type) {
	case MUI_BUTTON:
	case MUI_RADIOBUTTON:
	case MUI_TINYRADIOBUTTON:
	case MUI_VSLIDER:
	case MUI_HSLIDER:
	case MUI_TEXTBOX:
	case MUI_TEXTLIST:
	case MUI_PULLDOWN:
	    free(obj->object);
	    break;
	case MUI_LABEL:
	case MUI_BOLDLABEL:
	    break;
    }
    free(obj);
}

void	    muiSetID(muiObject *obj, int id)
{
    obj->id = id;
}

int muiGetID(muiObject *obj)
{
    return obj->id;
}

void muiSetCallback(muiObject *obj, void (*callback)(muiObject *, enum muiReturnValue))
{
    obj->callback = callback;
}

int muiInObject(muiObject *obj, int x, int y)
{
    if (obj->xmin <= x && x <= obj->xmax && obj->ymin <= y && y <= obj->ymax)
	return 1;
    return 0;
}

int muiGetLocate(muiObject *obj)
{
    return obj->locate;
}

void muiSetLocate(muiObject *obj, int state)
{
    obj->locate = (short) state;
}

int muiGetSelect(muiObject *obj)
{
    return obj->select;
}

void muiSetUIList(muiObject *obj, int list)
{
    obj->uilist = list;
}

int muiGetUIList(muiObject *obj)
{
    return obj->uilist;
}

void muiSetSelect(muiObject *obj, int state)
{
    obj->select = (short) state;
}

int muiGetVisible(muiObject *obj)
{
    return obj->visible;
}

void muiSetVisible(muiObject *obj, int state)
{
    obj->visible = (short) state;
}

int muiGetActive(muiObject *obj)
{
    return obj->active;
}

void muiSetActive(muiObject *obj, int state)
{
    obj->active = (short) state;
}

int muiGetEnable(muiObject *obj)
{
    return obj->enable;
}

void muiSetEnable(muiObject *obj, int state)
{
    obj->enable = (short) state;
}

void muiDrawObject(muiObject *obj)
{
    switch (obj->type) {
	case MUI_BUTTON:
	    drawbut(obj);
	    break;
	case MUI_RADIOBUTTON:
	    drawradiobutton(obj);
	    break;
	case MUI_TINYRADIOBUTTON:
	    drawtinyradio(obj);
	    break;
	case MUI_LABEL:
	    drawlabel(obj);
	    break;
	case MUI_BOLDLABEL:
	    drawboldlabel(obj);
	    break;
	case MUI_TEXTBOX:
	    drawtb(obj);
	    break;
	case MUI_VSLIDER:
	    drawvs(obj);
	    break;
	case MUI_HSLIDER:
	    drawhs(obj);
	    break;
	case MUI_TEXTLIST:
	    drawtl(obj);
	    break;
	case MUI_PULLDOWN:
	    drawpulldown(obj);
	    break;
    }
}

void muiError(char *s)
{
    fprintf(stderr, "%s\n", s);
    exit(0);
}

#define MAX_UI_LISTS 50
static muiCons *muilist[MAX_UI_LISTS];
static int muilistindex[MAX_UI_LISTS];

void muiNewUIList(int listid)
{
    static int inited = 0;
    int i;

    if (inited == 0) {
	inited = 1;
	for (i = 1; i < MAX_UI_LISTS; i++)
	    muilistindex[i] = -1;
	muilistindex[0] = listid;
	muiSetActiveUIList(listid);
	return;
    }
    for (i = 0; i < MAX_UI_LISTS; i++)
	if (muilistindex[i] == -1) {
	    muilistindex[i] = listid;
	    muiSetActiveUIList(listid);
	    return;
	}
    muiError("muiNewUIList: No more UI lists available");
}

int muiGetListId(int uilist)
{
    int i;
    
    for (i = 0; i < MAX_UI_LISTS; i++) {
	if (muilistindex[i] == uilist) return i;	
    }
    muiError("muiAddToUIList: illegal UI list identifier");
    return -1;
}

muiCons *muiGetListCons(int uilist)
{
    int i;
    
    for (i = 0; i < MAX_UI_LISTS; i++) {
	if (muilistindex[i] == uilist) return muilist[i];	
    }
    muiError("muiGetListCons: illegal UI list identifier");
    return (muiCons *)0;
}

void muiAddToUIList(int uilist, muiObject *obj)
{
    int i;
    muiCons *mcons;

    if (uilist == 0) {
	muiError("muiAddToUIList: no active UI list");
    }
    if ((i = muiGetListId(uilist)) == -1) return;
    mcons = (muiCons *)malloc(sizeof(muiCons));
    mcons->next = muilist[i];
    muilist[i] = mcons;
    mcons->object = obj;
}

static muiObject *muiFastHitInList(muiCons *mcons, int x, int y)
{
    while (mcons) {
	if (muiInObject(mcons->object, x, y))
	    switch (mcons->object->type) {
		case MUI_BUTTON:
		case MUI_TEXTBOX:
		case MUI_VSLIDER:
		case MUI_HSLIDER:
		case MUI_TEXTLIST:
		case MUI_RADIOBUTTON:
		case MUI_TINYRADIOBUTTON:
		case MUI_PULLDOWN:
		    return mcons->object;
		case MUI_LABEL:
		case MUI_BOLDLABEL:
		    return 0;
	    }
	mcons = mcons->next;
    }
    return (muiObject *)0;	/* not found */
}

muiObject *muiHitInList(int uilist, int x, int y)
{
    muiCons *mcons;
    
    if ((mcons = muiGetListCons(uilist)) == (muiCons *)0) return (muiObject *)0;
    return muiFastHitInList(mcons, x, y);
}

void muiDrawUIList(int uilist)
{
    muiCons *mcons;

    if ((mcons = muiGetListCons(uilist)) == (muiCons *)0) return;
    muiBackgroundClear();
    while (mcons) {
	muiDrawObject(mcons->object);
	mcons = mcons->next;
    }
}

static muiObject *SelectedObj, *LocatedObj;
static muiCons *ActiveCons;
static int ActiveUIList = 0;
static muiObject *ActiveSlider = 0;

void muiSetActiveUIList(int i)
{
    ActiveUIList = i;
}

int muiGetActiveUIList(void)
{
    return ActiveUIList;
}

static void muiInitInteraction(int uilist)
{
    muiCons *mcons;
    muiObject *obj;

    if ((mcons = muiGetListCons(uilist)) == (muiCons *)0) return;
    SelectedObj = LocatedObj = (muiObject *)0;
    ActiveCons = mcons;
    ActiveUIList = uilist;
    while (mcons) {
	obj = mcons->object;
	muiSetSelect(obj, 0);
	muiSetLocate(obj, 0);
	mcons = mcons->next;
    }
}

static void (*noncallback)(int, int) = 0;

static void nonmuicallback(int x, int y)
{
    if (noncallback == 0) return;
    noncallback(x, y);
}

void muiSetNonMUIcallback(void (*nc)(int, int))
{
    noncallback = nc;
}

void muiHandleEvent(int event, int value, int x, int y)
{
    muiObject *obj;
    static int lastactive = 0;
    enum muiReturnValue retval;

    if (ActiveUIList == 0) {
	muiError("muiHandleEvent: no active UI list");
    }
    if (lastactive != ActiveUIList) {
	muiInitInteraction(lastactive = ActiveUIList);
    }
    if ((event == MUI_KEYSTROKE)) {
	if (obj = muiGetActiveTB()) {
	    retval = (obj->handler)(obj, event, value, x, y);
	    if (retval && obj->callback)
		(obj->callback)(obj, retval);
	    return;
	}
	/* may have to add text editors, et cetera */
	return;
    }
    if (event == MUI_DEVICE_RELEASE && ActiveSlider) {
	retval = (ActiveSlider->handler)(ActiveSlider, event, value, x, y);
	if (retval && ActiveSlider->callback)
	    (ActiveSlider->callback)(ActiveSlider, retval);
	ActiveSlider = 0;
	return;
    }
    ActiveCons = muiGetListCons(ActiveUIList);
    obj = muiFastHitInList(ActiveCons, x, y);
    if (obj == 0 && event == MUI_DEVICE_PRESS) {
	nonmuicallback(x, y);
	return;
    }
    if (event == MUI_DEVICE_UP && (!menuinuse) && (activemenu != -1) && (obj == 0 || obj->type != MUI_PULLDOWN)) {
	activemenu = -1;
	glutDetachMenu(GLUT_LEFT_BUTTON);
    }
    if (obj && (obj->type == MUI_VSLIDER || obj->type == MUI_HSLIDER)
					    && event == MUI_DEVICE_PRESS)
							    ActiveSlider = obj;
    if (obj == 0) {
	if (ActiveSlider) {
	    retval = (ActiveSlider->handler)(ActiveSlider, event, value, x, y);
	    if (retval && ActiveSlider->callback)
		(ActiveSlider->callback)(ActiveSlider, retval);
	    return;
	}
        if (LocatedObj) {
	    muiSetLocate(LocatedObj, 0);
	    muiDrawObject(LocatedObj);
	    LocatedObj = 0;
	}
	if ((event == MUI_DEVICE_RELEASE) && SelectedObj) {
	    muiSetSelect(SelectedObj, 0);
	    muiSetLocate(SelectedObj, 0);
	    muiDrawObject(SelectedObj);
	    LocatedObj = SelectedObj = 0;
	}
	return;
    }
    retval = (obj->handler)(obj, event, value, x, y);
    if (retval && obj->callback)
	(obj->callback)(obj, retval);
    return;
}

/* ARGSUSED2 */
enum muiReturnValue buttonhandler(muiObject *obj, int event, int value, int x, int y)
{
    if (!muiGetEnable(obj) || !muiGetVisible(obj)) return MUI_NO_ACTION;

    switch (event) {
	case MUI_DEVICE_DOWN:
	    return MUI_NO_ACTION;
	case MUI_DEVICE_UP:
	    if (LocatedObj != obj) {
	        if (LocatedObj) {
		    muiSetLocate(LocatedObj, 0);
		    muiDrawObject(LocatedObj);
		}
		muiSetLocate(obj, 1);
		muiDrawObject(obj);
		LocatedObj = obj;
	    }
	    return MUI_NO_ACTION;
	case MUI_DEVICE_PRESS:
	    muiSetSelect(obj, 1);
	    muiSetLocate(obj, 1);
	    SelectedObj = LocatedObj = obj;
	    muiDrawObject(obj);
	    return MUI_NO_ACTION;
	case MUI_DEVICE_RELEASE:
	    if (SelectedObj != obj) {
		muiSetSelect(SelectedObj, 0);
		muiSetLocate(SelectedObj, 0);
		muiDrawObject(SelectedObj);
		muiSetLocate(obj, 1);
		LocatedObj = obj;
		muiDrawObject(obj);
		return MUI_NO_ACTION;
	    }
	    if (obj->type == MUI_RADIOBUTTON || obj->type == MUI_TINYRADIOBUTTON) {
		Button *b = (Button *)obj->object, *b1;
		if (b->link) {
		    muiSetActive(obj, 1);
		    b1 = b->link;
		    while (b1 != b) {
			muiSetActive(b1->object, 0);
			b1 = b1->link;
		    }
		} else {
 		    muiSetActive(obj, ( muiGetActive(obj) ? 0 : 1 ) );
  		}
	    }
	    muiSetSelect(obj, 0);
	    muiDrawObject(obj);
	    return MUI_BUTTON_PRESS;
	case MUI_DEVICE_CLICK:
	    muiSetSelect(obj, 0);
	    muiSetLocate(obj, 1);
	    LocatedObj = obj;
	    muiDrawObject(obj);
	    return MUI_BUTTON_PRESS;
	case MUI_DEVICE_DOUBLE_CLICK:
	    muiSetSelect(obj, 0);
	    muiSetLocate(obj, 1);
	    LocatedObj = obj;
	    muiDrawObject(obj);
	    return MUI_BUTTON_PRESS; /* XXX this may not be right; */
	case MUI_KEYSTROKE:
	    return MUI_NO_ACTION;
	default:
	    muiError("buttonhandler: wacko event");
	    return MUI_NO_ACTION;
    }
}

/* ARGSUSED */
enum muiReturnValue nullhandler(muiObject *obj, int event, int value, int x, int y)
{
    return MUI_NO_ACTION;
}