File: MenuCvt.c

package info (click to toggle)
gridengine 6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 51,532 kB
  • ctags: 51,172
  • sloc: ansic: 418,155; java: 37,080; sh: 22,593; jsp: 7,699; makefile: 5,292; csh: 4,244; xml: 2,901; cpp: 2,086; perl: 1,895; tcl: 1,188; lisp: 669; ruby: 642; yacc: 393; lex: 266
file content (643 lines) | stat: -rw-r--r-- 17,093 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
639
640
641
642
643
/* 
 * Motif Tools Library, Version 3.1
 * $Id$
 * 
 * Written by David Flanagan.
 * Copyright (c) 1992-2001 by David Flanagan.
 * All Rights Reserved.  See the file COPYRIGHT for details.
 * This is open source software.  See the file LICENSE for details.
 * There is no warranty for this software.  See NO_WARRANTY for details.
 *
 * $Log$
 * Revision 1.1.1.1  2001/07/18 11:06:02  root
 * Initial checkin.
 *
 * Revision 1.2  2001/06/12 16:25:28  andre
 * *** empty log message ***
 *
 *
 */

#include <ctype.h>
#include <stdio.h>
#include <Xmt/Xmt.h>
#include <Xmt/MenuP.h>
#include <Xmt/ConvertersP.h>
#include <Xmt/Lexer.h>

/*
 * These are keywords and symbolic names for keywords, used by the lexer.
 */
static String keywords[] = {
    "Alt",
    "Button",
    "Ctrl",
    "DoubleLine",
    "Help",
    "Line",
    "Lock",
    "Meta",
    "Off",
    "On",
    "Pixmap",
    "Shift",
    "Submenu",
    "Tearoff",
    "Title",
    "Toggle"
};
    
#define ALT		0
#define BUTTON		1
#define CTRL		2
#define DOUBLELINE	3
#define HELP		4
#define LINE		5
#define LOCK		6
#define META		7
#define OFF		8
#define ON		9
#define PIXMAP		10
#define SHIFT		11
#define SUBMENU		12
#define TEAROFF		13
#define TITLE		14
#define TOGGLE		15


#if NeedFunctionPrototypes
static int ParseAccelerator(XmtLexer l, XmtMenuItem *item)
#else
static int ParseAccelerator(l, item)
XmtLexer l;
XmtMenuItem *item;
#endif
{
    int tok;
    char accel[200];
    char accel_label[200];
    
    accel[0] = '\0';
    accel_label[0] = '\0';

    while((XmtLexerGetToken(l) == XmtLexerKeyword) &&
	  ((XmtLexerKeyValue(l) == CTRL) ||
	   (XmtLexerKeyValue(l) == SHIFT) ||
	   (XmtLexerKeyValue(l) == META) ||
	   (XmtLexerKeyValue(l) == ALT) ||
	   (XmtLexerKeyValue(l) == LOCK))) {
	strcat(accel, XmtLexerStrValue(l));
	strcat(accel_label, XmtLexerStrValue(l));
	XmtLexerConsumeToken(l);
	tok = XmtLexerGetToken(l);
	if (tok == XmtLexerPlus) {
	    strcat(accel, " ");
	    strcat(accel_label, "+");
	    XmtLexerConsumeToken(l);
	}
	else if (tok == XmtLexerMinus) {
	    strcat(accel, " ");
	    strcat(accel_label, "-");
	    XmtLexerConsumeToken(l);
	}
	else {
	    XmtWarningMsg("XmtMenu", "missingPlus",
			  "item \"%s\": modifier name in accelerator must be followed by `+' or `-'",
			  item->label);
	    goto error;
	}
    }

    strcat(accel, "<Key>");
    tok = XmtLexerGetToken(l);

    if ((tok == XmtLexerIdent) || (tok == XmtLexerString)) {
	char *sym = XmtLexerStrValue(l);

	if (isalnum(sym[0]))
	    strcat(accel, sym);
	else {
	    char buf[10];
	    sprintf(buf, "%d", sym[0]);
	    strcat(accel, buf);
	}

	strcat(accel_label, sym);
	XtFree(sym);
	XmtLexerConsumeToken(l);
    }
    else if (tok == XmtLexerInteger) {
	char buf[10];
	sprintf(buf, "%d", XmtLexerIntValue(l));
	strcat(accel, buf);
	strcat(accel_label, buf);
	XmtLexerConsumeToken(l);
    }
    else {
	XmtWarningMsg("XmtMenu", "missingKeysym",
		      "item \"%s\": accelerator is missing keysym",
		      item->label);
	goto error;
    }
    
    item->accelerator = XtNewString(accel);
    item->accelerator_label = XtNewString(accel_label);
    return 0;

 error:
    /* read forward until matching ']' */
    while(1) {
	tok = XmtLexerGetToken(l);
	if ((tok == XmtLexerRBracket) || (tok == XmtLexerEndOfString)) break;
	XmtLexerConsumeToken(l);
    }
    item->accelerator = item->accelerator_label = NULL;
    return 1;
}

#if NeedFunctionPrototypes
static void ParseLabelAndMnemonic(XmtLexer l, String *label,
				  int look_for_mnemonic, char *mnemonic)
#else
static void ParseLabelAndMnemonic(l, label, look_for_mnemonic, mnemonic)
XmtLexer l;
String *label;
int look_for_mnemonic;
char *mnemonic;
#endif
{
    register int i;
    register char *c;
    
    if (XmtLexerGetToken(l) != XmtLexerString) return;

    if (look_for_mnemonic) {
	*label = XtMalloc(XmtLexerStrLength(l) + 1);
	*mnemonic = '\0';
	
	for(i=0, c = XmtLexerStrValue(l); *c != '\0'; c++) {
	    if ((*c == '_') && (*mnemonic == '\0'))
		*mnemonic = *(c+1);
	    else
		(*label)[i++] = *c;
	}
	(*label)[i] = '\0';
	
	XtFree(XmtLexerStrValue(l));
    }
    else {
	*label = XmtLexerStrValue(l);
	*mnemonic = '\0';
    }

    XmtLexerConsumeToken(l);
}    


/*
 *
 * item:: [name] '-' '-'* ';'      // a separator
 *      | [name] '=' '='* ';'      // a double separator
 *      | [name] [type] flags* [label] [accelerator]
 *             [submenu] [symbol] callbacks
 * 
 * name::         ident ':'
 * type::         'Title' | 'Button' | 'Toggle' |
 *                    'Line' | 'DoubleLine' | 'Submenu'
 * flags::        'On' | 'Off' | 'Help' | 'Tearoff' | 'Pixmap'
 * label::        string-with-embedded-mnemonic [ '|' string ]
 * accelerator::  '[' (modifier ('+'|'-'))* keysym ']'
 * modifier::     'Ctrl' | 'Shift' | 'Meta' | 'Alt' | 'Lock'
 * keysym::       ident
 * submenu::      '-' '>' ident
 * symbol::       '$' ident
 * callbacks::    callback | '{' callback+ '}' | ';'
 * callback::     ident '(' [args] ')' ';'
 * args::         list-of-comma-separated-strings
 */

#if NeedFunctionPrototypes
static int ParseItem(Widget w, XmtLexer l, XmtMenuItem *item)
#else
static int ParseItem(w, l, item)
Widget w;
XmtLexer l;
XmtMenuItem *item;
#endif
{
    int tok;
    static XmtMenuItem null_item;  /* static memory; all fields NULL */

    /* initialize item structure */
    null_item.type = XmtMenuItemEnd;  /* undefined type, no flags */
    *item = null_item;

    /* parse item name */
    if (XmtLexerGetToken(l) == XmtLexerIdent) {
	item->name = XmtLexerStrValue(l);
	XmtLexerConsumeToken(l);
	if (XmtLexerGetToken(l) == XmtLexerColon)
	   XmtLexerConsumeToken(l);
	else
	    XmtWarningMsg("XmtMenu", "colonExpected",
			  "colon expected after item name %s.",
			  item->name);
    }

    /* parse separators as a special case */
    if (((tok = XmtLexerGetToken(l)) == XmtLexerMinus) ||
	(tok == XmtLexerEqual))   { /* a separator */
	if (tok == XmtLexerMinus) item->type = XmtMenuItemSeparator;
	else item->type = XmtMenuItemDoubleSeparator;
	    
	while (((tok = XmtLexerGetToken(l)) == XmtLexerMinus) ||
	       (tok == XmtLexerEqual))
	    XmtLexerConsumeToken(l);

	if (tok == XmtLexerSemicolon) {
	    XmtLexerConsumeToken(l);
	    return 0;
	}
	else {
	    XmtWarningMsg("XmtMenu", "semicolonAfterSeparator",
			  "semicolon expeced after separator.");
	    goto error;
	}
    }

    /*
     * parse an optional type keyword.
     * We can set the type directly, because we know that no flags are set yet.
     */
    if (XmtLexerGetToken(l) == XmtLexerKeyword) {
	switch(XmtLexerKeyValue(l)) {
	case TITLE:      item->type = XmtMenuItemLabel; break;
	case BUTTON:     item->type = XmtMenuItemPushButton; break;
	case TOGGLE:     item->type = XmtMenuItemToggleButton; break;
	case LINE:       item->type = XmtMenuItemSeparator; break;
	case DOUBLELINE: item->type = XmtMenuItemDoubleSeparator; break;
	case SUBMENU:    item->type = XmtMenuItemCascadeButton; break;
	}
	/* if we set a type eat the token */
	if (item->type != XmtMenuItemEnd) XmtLexerConsumeToken(l);
    }

    /*
     * If the type is not set, assume it is a push button until
     * we find a flag or something else that indicates otherwise.
     */
    if (item->type == XmtMenuItemEnd)
	item->type = XmtMenuItemPushButton;

    /*
     * now that we're going to set flags, we've got to be
     * careful when setting the type.  Use this macro:
     */
#define SetType(item, value) ((item)->type = ((item)->type & ~0x7) | (value))

    /*
     * parse any flags that modify the type.
     */
    while (XmtLexerGetToken(l) == XmtLexerKeyword) {
	switch(XmtLexerKeyValue(l)) {
	case ON:
	    SetType(item, XmtMenuItemToggleButton);
	    item->type |= XmtMenuItemOn;
	    break;
	case OFF: 
	    SetType(item, XmtMenuItemToggleButton);
	    item->type &= ~XmtMenuItemOn;
	    break;
	case HELP:
	    item->type |= XmtMenuItemHelp;
	    break;
	case TEAROFF:
	    item->type |= XmtMenuItemTearoff;
	    break;
	case PIXMAP:
	    item->type |= XmtMenuItemPixmap;
	    break;
	default:
	    XmtWarningMsg("XmtMenu", "unexpectedKeyword",
			  "item \"%s\": unexpected keyword \"%s\"",
			  (item->label)?item->label:"",
			  XmtLexerStrValue(l));
	    break;
	}
	XmtLexerConsumeToken(l);
    }
    
    
    /* parse item label */
    if (XmtLexerGetToken(l) == XmtLexerString) {
	/* the item label string; figure out label and mnemonic  */
	ParseLabelAndMnemonic(l, &item->label,
			      !(item->type & XmtMenuItemPixmap),
			      &item->mnemonic);
	if (XmtLexerGetToken(l) == XmtLexerBar) {  /* alternate label */
	    SetType(item, XmtMenuItemToggleButton);
	    if (XmtLexerNextToken(l) != XmtLexerString) {
		XmtWarningMsg("XmtMenu", "missingLabel",
			      "item '%s': alternate label expected after '|'",
			      item->label);
	    }
	    else
		ParseLabelAndMnemonic(l, &item->alt_label,
				      !(item->type & XmtMenuItemPixmap),
				      &item->alt_mnemonic);
	}
    }

    /* parse item accelerator */
    if (XmtLexerGetToken(l) == XmtLexerLBracket) { /* an accelerator */
	XmtLexerConsumeToken(l);
	(void) ParseAccelerator(l, item);
	if (XmtLexerGetToken(l) == XmtLexerRBracket)
	    XmtLexerConsumeToken(l);
	else
	    XmtWarningMsg("XmtMenu", "missingRBracket",
			  "`]' expected at end of accelerator for item \"%s\"",
			  item->label);
    }

    /* parse optional arrow and submenu name */
    if (XmtLexerGetToken(l) == XmtLexerMinus) {
	SetType(item, XmtMenuItemCascadeButton);
	if (XmtLexerNextToken(l) == XmtLexerGreater)
	    XmtLexerConsumeToken(l);
	if (XmtLexerGetToken(l) == XmtLexerIdent) {
	    item->submenu_name = XmtLexerStrValue(l);
	    XmtLexerConsumeToken(l);
	}
	else {
	    XmtWarningMsg("XmtMenu", "missingSubmenuName",
			  "item \"%s\": submenu name expected after `->'.",
			  item->label);
	    SetType(item, XmtMenuItemPushButton);
	    goto error;
	}
    }

    /*
     * Parse an optional symbol name, to be used by toggle buttons
     */
    if (XmtLexerGetToken(l) == XmtLexerDollar) {
	if (XmtLexerNextToken(l) != XmtLexerIdent) {
	    XmtWarningMsg("XmtMenu", "missingSymbol",
			  "item '%s': symbol name expected after '$'.",
			  item->label);
	    goto error;
	}
	item->symbol_name = XmtLexerStrValue(l);
	XmtLexerConsumeToken(l);
    }

    /*
     * parse callbacks.
     * Expect an identifier, a '{' or a ';'.
     * To parse the callbacks, we extract the callback string from the
     * private insides of the lexer and invoke whatever string-to-callback
     * converter is registered.
     */
    tok = XmtLexerGetToken(l);
    if ((tok == XmtLexerIdent) || (tok == XmtLexerLBrace)) { /* a callback */
	char *callback_string;
	int len = 0;
	XrmValue from, to;
	Boolean stat = False;
	
	if (tok == XmtLexerIdent) { /* a single callback */
	    /* back up by the length of the current identifier */
	    l->c -= XmtLexerStrLength(l);
	    XtFree(XmtLexerStrValue(l));
	
	    /* scan forward just past an unquoted semicolon */
	    tok = XmtLexerScan(l, ";", True);
	    if (tok == XmtLexerString) {
		callback_string = XmtLexerStrValue(l);
		len = XmtLexerStrLength(l);
		XmtLexerConsumeToken(l);
	    }
	    else callback_string = NULL;
	}
	else {  /* a compound callback */
	    XmtLexerConsumeToken(l);  /* eat the open brace */
	    XmtLexerSkipWhite(l);     /* skip whitespace */
	    /* scan forward to just before an unquoted close brace */
	    tok = XmtLexerScan(l, "}", False);
	    if (tok == XmtLexerString) {
		callback_string = XmtLexerStrValue(l);
		len = XmtLexerStrLength(l);
		XmtLexerConsumeToken(l);
	    }
	    else callback_string = NULL;

	    /* Now get and eat the '}' token */
	    XmtLexerNextToken(l);
	    XmtLexerConsumeToken(l);
	}

	/* convert the string to an XtCallbackList */
	if (!callback_string) {
	    XmtWarningMsg("XmtMenu", "badCallback",
			  "bad callback string for item '%s'",
			  item->label);
	}
	else {
#if 0
	    /*
	     * The ref counting on the callback converter somehow causes
	     * a core dump when the Menu widget is destroyed.  So we don't
	     * do it this way.
	     */
	    from.addr = callback_string;
	    from.size = len + 1;
	    to.addr = (XPointer) &item->callback;
	    to.size = sizeof(XtCallbackList);
	    stat = XtConvertAndStore(w, XtRString, &from, XtRCallback, &to);
#endif
	    if (_XmtCallbackConverter == NULL) {
		XmtWarningMsg("XmtMenu", "noConverter",
			      "no String to XtCallbackList converter registered.\n\tCall XmtRegisterCallbackConverter().");
		stat = False;
	    }
	    else {
		XtCacheRef ref_return;
		/*
		 * Here we just call the converter directly.
		 * We ignore the returned cache reference.  This will only
		 * cause a memory leak if the menu is destroyed, and in
		 * that case, a memory leak is better than the core dump
		 * we had above.
		 */
		from.addr = (XPointer) callback_string;
		from.size = len + 1;
		to.addr = (XPointer) &item->callback;
		to.size = sizeof(XtCallbackList);
		stat = XtCallConverter(XtDisplay(w), _XmtCallbackConverter,
				       NULL, 0,
				       &from, &to, &ref_return);
	    }

	}
	
	/* if conversion failed, a warning will already be issued */
	if (!callback_string || !stat) item->callback = NULL;
	
	/* set flag that says we have a XtCallbackList, not XtCallbackProc */
	item->type |= XmtMenuItemCallbackList;
	
	/* and free the callback string */
	XtFree(callback_string);
    }
    else if (tok == XmtLexerSemicolon) {  /* no callback at all */
	XmtLexerConsumeToken(l);
    }
    else {
	XmtWarningMsg("XmtMenu", "semicolonExpected",
		      "item \"%s\": semicolon expected at end of item description.",
		      item->label);
	goto error;
    }
    
    return 0;

 error:
    /* error recovery: free strings and read 'till `;' or end of string */
    XtFree(item->label);
    XtFree(item->accelerator);
    XtFree(item->accelerator_label);
    XtFree(item->name);
    XtFree(item->submenu_name);
    
    while(1) {
	tok = XmtLexerGetToken(l);
	if ((tok == XmtLexerSemicolon) || (tok == XmtLexerEndOfString)) break;
	XmtLexerConsumeToken(l);
    }
    XmtLexerConsumeToken(l);
    return 1;
}    

#if NeedFunctionPrototypes
Boolean XmtParseMenuString(Widget w, String str, XmtMenuItem **items_return)
#else
Boolean XmtParseMenuString(w, str, items_return)
Widget w;
String str;
XmtMenuItem **items_return;
#endif
{
    static XmtLexer l = NULL;
    XmtMenuItem *items;
    int num_items, max_items;

    if (l == NULL)
	l = XmtLexerCreate(keywords, XtNumber(keywords));

    XmtLexerInit(l, str);

    max_items = 8;
    num_items = 0;
    items = (XmtMenuItem *) XtMalloc(sizeof(XmtMenuItem) * max_items);

    while (XmtLexerGetToken(l) != XmtLexerEndOfString) {
	if (num_items == max_items) {
	    max_items *= 2;
	    items = (XmtMenuItem *) XtRealloc((char *) items,
					      sizeof(XmtMenuItem) * max_items);
	}
	if (ParseItem(w, l, &items[num_items]) == 0) /* if no error */
	    num_items++;
    }

    if (num_items == max_items) {
	max_items++;
	items = (XmtMenuItem *) XtRealloc((char *) items,
					  sizeof(XmtMenuItem) * max_items);
    }
    items[num_items].type = XmtMenuItemEnd;  /* NULL-termination */

    *items_return = items;
    return True;
}


/*ARGSUSED*/
#if NeedFunctionPrototypes
Boolean XmtConvertStringToXmtMenuItems(Display *dpy,
				       XrmValuePtr args, Cardinal *num_args,
				       XrmValuePtr from, XrmValuePtr to,
				       XtPointer *closure_return)
#else
Boolean XmtConvertStringToXmtMenuItems(dpy, args, num_args,
				       from, to, closure_return)
Display *dpy;
XrmValuePtr args;
Cardinal *num_args;
XrmValuePtr from;
XrmValuePtr to;
XtPointer *closure_return;
#endif
{
    String str = (String)from->addr;
    XmtMenuItem *value;
    Boolean status;
    Widget w = *((Widget *)args[0].addr);

    /* convert string to NULL-terminated array of XmtMenuItems */
    status = XmtParseMenuString(w, str, &value);

    if (status == False) return False;

    done(XmtMenuItem *, value);  /* a macro in ConvertersP.h */
}

/* ARGSUSED */
#if NeedFunctionPrototypes
static void MenuItemsDestructor(XtAppContext app, XrmValue *to,
				XtPointer converter_data,
				XrmValue *args, Cardinal *num_args)
#else
static void MenuItemsDestructor(app, to, converter_data, args, num_args)
XtAppContext app;
XrmValue *to;
XtPointer converter_data;
XrmValue *args;
Cardinal *num_args;
#endif
{
    XmtMenuItem *items = *(XmtMenuItem **) to->addr;
    int i;

    for(i=0; items[i].type != XmtMenuItemEnd; i++) {
	XtFree(items[i].label);
	XtFree(items[i].accelerator);
	XtFree(items[i].accelerator_label);
	XtFree(items[i].name);
	XtFree(items[i].submenu_name);
    }
    XtFree((char *)items);
}

static XtConvertArgRec convert_args[] = {
  {XtBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.self), sizeof(Widget)}
};


#if NeedFunctionPrototypes
void XmtRegisterMenuItemsConverter(void)
#else
void XmtRegisterMenuItemsConverter()
#endif
{
    static Boolean registered = False;

    if (!registered) {
	registered = True;
	XtSetTypeConverter(XmRString, XmtRXmtMenuItemList,
			   XmtConvertStringToXmtMenuItems,
			   convert_args, XtNumber(convert_args),
			   XtCacheNone, MenuItemsDestructor);
    }
}