File: IconGadget1.c

package info (click to toggle)
motif 2.3.4-13
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 81,160 kB
  • ctags: 51,769
  • sloc: ansic: 596,938; cpp: 3,951; yacc: 2,854; makefile: 2,070; csh: 1,199; sh: 1,070; lex: 455
file content (594 lines) | stat: -rw-r--r-- 13,366 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
/* $XConsortium: IconGadget1.c /main/5 1995/07/15 21:03:51 drk $ */
/*
 * Motif
 *
 * Copyright (c) 1987-2012, The Open Group. All rights reserved.
 *
 * These libraries and programs are free software; you can
 * redistribute them and/or modify them under the terms of the GNU
 * Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * These libraries and programs are distributed in the hope that
 * they will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with these librararies and programs; if not, write
 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301 USA
 * 
 */
/*
 * HISTORY
 */

#include <testlib.h>

typedef XtPointer (*ConvertProc)(String value);
static Widget	        CreateOptionMenu(Widget parent, Cardinal j);
static Widget	        CreateResLine(Widget parent, Cardinal j);
static Widget	        CreatePushButton(Widget parent, Cardinal j, 
					 Cardinal k);
static void		CreateControlPanel(void );
static void		ApplyCB(Widget wid, XtPointer client_data, 
				XtPointer call_data);
static XtPointer        LabelStringConvert(String  value);
static XtPointer        IconPixmapConvert(String  value);
static XtPointer        IconMaskConvert(String  value);
static XtPointer        DetailConvert(String  value);
static XtPointer        CardinalConvert(String  value);
static XtPointer        ViewTypeConvert(String  value);
static XtPointer        VisualEmphasisConvert(String  value);
static void             ProcessIcon(Cardinal i);
static void             ProcessRes(Cardinal i, Cardinal j);

#define NUM_ICONG 3
static Widget IconGad[NUM_ICONG];
static Widget *IconRes;
static Widget IconDA;
static Widget IconTog[NUM_ICONG];

static char *DetailString[3] = {
    "First detail ",
    "Second detail ",
    "Last detail"
};

typedef struct _IconResInfo {
    String name ;
    String * values ;
    Cardinal num_values ;
    ConvertProc convert ;
} IconResInfo ;

static String LabelStringValues[] = {
    "A",
    "Short",
    "Longer label",
    "How about something longer but useless"
};

static String IconPixmapValues[] = {
    "large.xpm",
    "small.xpm",
    "large.xbm",
    "small.xbm",
    "UNSPECIFIED"
};

static String IconMaskValues[] = {
    "large_m.xpm",
    "small_m.xpm",
    "round.xbm",
    "none"
};

static String DetailValues[] = {
    "1 detail",
    "2 details",
    "3 details"
};

static String DetailCountValues[] = {
    "0",
    "1",
    "2",
    "3"
};

static String ShadowThicknessValues[] = {
    "0",
    "1",
    "2",
    "5",
    "10"
};

static String ViewTypeValues[] = {
    "XmLARGE_ICON",
    "XmSMALL_ICON"
};

static String VisualEmphasisValues[] = {
    "XmNOT_SELECTED",
    "XmSELECTED"
};

static IconResInfo icon_res_info[] = {
    {
	XmNlabelString,
	LabelStringValues,
	XtNumber(LabelStringValues),
	LabelStringConvert
    },
    {
	XmNlargeIconPixmap,
	IconPixmapValues,
	XtNumber(IconPixmapValues),
	IconPixmapConvert
    },
    {
	XmNlargeIconMask,
	IconMaskValues,
	XtNumber(IconMaskValues),
	IconMaskConvert
    },
    {
	XmNsmallIconPixmap,
	IconPixmapValues,
	XtNumber(IconPixmapValues),
	IconPixmapConvert
    },
    {
	XmNsmallIconMask,
	IconMaskValues,
	XtNumber(IconMaskValues),
	IconMaskConvert
    },
    {
	XmNdetail,
	DetailValues,
	XtNumber(DetailValues),
	DetailConvert
    },
    {
	XmNdetailCount,
	DetailCountValues,
	XtNumber(DetailCountValues),
	CardinalConvert
    },
    {
	XmNshadowThickness,
	ShadowThicknessValues,
	XtNumber(ShadowThicknessValues),
	CardinalConvert
    },
    {
	XmNviewType,
	ViewTypeValues,
	XtNumber(ViewTypeValues),
	ViewTypeConvert
    },
    {	
	XmNvisualEmphasis,
	VisualEmphasisValues,
	XtNumber(VisualEmphasisValues),
	VisualEmphasisConvert
    }
};

static Cardinal NumIconRes = XtNumber(icon_res_info);

void
main(int argc, char **argv)
{

	int		i;
	Cardinal	n;
	Arg		args[MAX_ARGS];
	Widget          icon_gadget;

	CommonTestInit(argc, argv);

	n = 0 ;
	IconDA = XmCreateDrawingArea(Shell1, "IconGadget1", args, n);
	XtManageChild(IconDA);

	for (i=0; i < NUM_ICONG; i++) {
	    char icon_name[20];

	    sprintf(icon_name, "IconGad%d", i+1);
	    IconGad[i] = XmCreateIconGadget(IconDA, icon_name, NULL, 0);
	}

	XtManageChildren(IconGad, NUM_ICONG);

	CreateControlPanel();

	XtRealizeWidget(Shell1);

	CommonPause();
	CommonPause();
	CommonPause();
	CommonPause();
	CommonPause();

	icon_gadget = XmObjectAtPoint(IconDA, 50, 200);

	if (icon_gadget != NULL) {
	    printf("\nThe following Icon Gadget is located at 50 200: ");
	    printf("%s \n\n", XtName(icon_gadget));
	}
	else
	    printf("\nNo Icon Gadget is located at 50 200\n\n");

	CommonPause();

	XtAppMainLoop(app_context);

}


static void
CreateControlPanel(void )
{

   Arg		args[MAX_ARGS];
   Cardinal	n, i, j;
   Widget	PopupShell, FormDialog,
                RcRes, RcCommand, Apply, RcButtons, frame;


   n = 0;
   XtSetArg(args[n], XtNgeometry, "=+50+300"); n++;
   XtSetArg(args[n], XtNallowShellResize, True); n++;
   PopupShell = XtCreatePopupShell("Icon Gadget Resources", 
                                    topLevelShellWidgetClass, Shell1, 
                                    args, n);

   n = 0;
   FormDialog = XmCreateForm(PopupShell, "FormDialog", args, n);
   XtManageChild(FormDialog);

   n = 0;
   XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
   XtSetArg(args[n], XmNpacking, XmPACK_COLUMN); n++;
   RcRes = XmCreateRowColumn(FormDialog, "RcRes", args, n);
   XtManageChild(RcRes);

   IconRes = (Widget *) XtMalloc(NumIconRes * sizeof(Widget));
   for (j=0; j< NumIconRes; j++) {
       IconRes[j] = CreateResLine(RcRes, j);
   }

   n = 0;
   XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
   XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
   XtSetArg(args[n], XmNtopWidget, RcRes); n++;
   RcCommand = XmCreateRowColumn(FormDialog, "RcCommand", args, n);
   XtManageChild(RcCommand);

   n = 0;
   Apply = XmCreatePushButton(RcCommand, "Apply", args, n);
   XtManageChild(Apply);
   XtAddCallback(Apply, XmNactivateCallback, ApplyCB, NULL);

   n = 0;
   XtSetArg(args[n], XmNshadowType, XmSHADOW_IN); n++;
   frame = XmCreateFrame(RcCommand, "frame", args, n);
   XtManageChild(frame);

   n = 0;
   XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
   RcButtons = XmCreateRowColumn(frame, "RcButtons", args, n);
   XtManageChild(RcButtons);

   for (i=0; i< NUM_ICONG; i++) {
       char     widget_name[20];
       char     name[20];
       XmString tcs;

       sprintf(widget_name, "TB_IconGad%d", i+1);
       sprintf(name, "IconGad%d", i+1);
       tcs = XmStringGenerate(name, XmFONTLIST_DEFAULT_TAG, XmCHARSET_TEXT, 
			      NULL);

       n = 0;
       XtSetArg(args[n], XmNlabelString, tcs); n++;
       IconTog[i] = XmCreateToggleButton(RcButtons, widget_name, args, n);
       XmStringFree(tcs);
   }
   XtManageChildren(IconTog, NUM_ICONG);

   XtPopup(PopupShell, XtGrabNone);
}

static Widget
CreateResLine(Widget parent, Cardinal j)

{
    Widget      RcResLine, TogSet, ResPulldown, OptRes;
    Arg		args[MAX_ARGS];
    Cardinal	n, k;
    char        toggle_name[30];
    XmString    tcs;
    
    n = 0;
    XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
    RcResLine = XmCreateRowColumn(parent, "RcResLine", args, n);
    XtManageChild(RcResLine);

    tcs = XmStringGenerate("", XmFONTLIST_DEFAULT_TAG, XmCHARSET_TEXT, 
			   NULL);
    n = 0;
    XtSetArg(args[n], XmNlabelString, tcs); n++;
    XtSetArg(args[n], XmNindicatorOn, XmINDICATOR_CHECK_BOX); n++;
    XtSetArg(args[n], XmNindicatorSize, 20); n++;

    /* Name of the toggle button has the form "TogSet_resourceName" */

    sprintf(toggle_name, "TogSet_%s", icon_res_info[j].name);
    TogSet = XmCreateToggleButton(RcResLine, toggle_name, args, n);
    XtManageChild(TogSet);
    XmStringFree(tcs);

    n = 0;
    ResPulldown = XmCreatePulldownMenu(RcResLine, "ResPulldown", args, n);

    for (k=0; k< icon_res_info[j].num_values; k++) {
	Widget pushb ;
	pushb = CreatePushButton(ResPulldown, j, k);
	XtSetArg(args[0], XmNuserData, k);
	XtSetValues(pushb, args, 1);
   }

    OptRes = CreateOptionMenu(RcResLine, j);

    n = 0;
    XtSetArg(args[n], XmNsubMenuId, ResPulldown); n++;
    XtSetValues(OptRes, args, n);
    
    return RcResLine ;
}


static void
ApplyCB(Widget wid, XtPointer client_data, XtPointer call_data)
{
    Cardinal i ;

    for (i=0; i < NUM_ICONG; i++) {
	if (XmToggleButtonGetState(IconTog[i]))
	    ProcessIcon(i);
    }    
}

static void
ProcessIcon(Cardinal i)

{
    Cardinal j ;

    for (j=0; j < NumIconRes; j++) {
	ProcessRes(i, j);
    }

}

static void
ProcessRes(Cardinal i, Cardinal j)

{
    Widget TogSet, OptRes, cur_pushb ;
    XtPointer user_data, cur_val ;
    int cur_k ;
    Arg args[1] ;
    char name[30];

    /* if the TogSet is On, process */
    sprintf(name, "TogSet_%s", icon_res_info[j].name);
    TogSet = XtNameToWidget(IconRes[j], name);
    
    if (!XmToggleButtonGetState(TogSet)) return ;

    /* find the current value in the option */
    sprintf(name, "OM_%s", icon_res_info[j].name);
    OptRes = XtNameToWidget(IconRes[j], name);
    
    XtVaGetValues(OptRes, XmNmenuHistory, &cur_pushb, NULL);
    
    XtVaGetValues(cur_pushb, XmNuserData, &user_data, NULL);
    cur_k = (int) user_data ;

    cur_val = icon_res_info[j].convert(icon_res_info[j].values[cur_k]) ;

    XtSetArg(args[0], icon_res_info[j].name, cur_val);
    XtSetValues(IconGad[i], args, 1);
}

static Widget
CreatePushButton(Widget parent, Cardinal j, Cardinal k)

{

    Widget          widget;
    register int    n;
    Arg             args[MAX_ARGS];
    XmString        tcs;
    char            name[25];

    /* Name of a push button in the pulldown has the form "resourcePBnumber" 
       where resource is the resource name, and number is the rang in the
       resource value array */

    sprintf(name, "%sPB%d", icon_res_info[j].name, k);

    tcs = XmStringGenerate(icon_res_info[j].values[k], XmFONTLIST_DEFAULT_TAG,
			   XmCHARSET_TEXT, NULL);

    n = 0;
    XtSetArg(args[n], XmNlabelString, tcs); n++;
    widget = XmCreatePushButton(parent, name, args, n);
    XtManageChild(widget);

    XmStringFree(tcs);

    return(widget);

}

static Widget
CreateOptionMenu(Widget parent, Cardinal j)

{

    Widget          widget;
    register int    n;
    Arg             args[MAX_ARGS];
    XmString        tcs;
    char            name[25];

    /* Name of the Option Menu has the form "OM_resourceName" */

    sprintf(name, "OM_%s", icon_res_info[j].name);

    tcs = XmStringGenerate(icon_res_info[j].name, XmFONTLIST_DEFAULT_TAG, 
			   XmCHARSET_TEXT, NULL);

    n = 0;
    XtSetArg(args[n], XmNlabelString, tcs); n++;
    widget = XmCreateOptionMenu(parent, name, args, n);
    XtManageChild(widget);

    XmStringFree(tcs);

    return(widget);

}

static XtPointer 
LabelStringConvert(String value)

{
    XmString s ;

    s = XmStringGenerate(value, NULL, XmCHARSET_TEXT, NULL);

    return (XtPointer) s ;
}


static XtPointer 
IconPixmapConvert(String value)

{
    Pixmap    p;
    Pixel     foreground, background;
    Arg       args[3];
    int       n;

    if (strcmp(value, "UNSPECIFIED") == 0)
	return (XtPointer) XmUNSPECIFIED_PIXMAP;

    n = 0;
    XtSetArg (args[n], XmNforeground, &foreground);       n++;
    XtSetArg (args[n], XmNbackground, &background);       n++;
    XtGetValues (IconDA, args, n);

    p = XmGetPixmap(XtScreen(Shell1), value, foreground, background);

    return (XtPointer) p ;
}


static XtPointer 
IconMaskConvert(String value)

{
    Pixmap    p;

    if (strcmp(value, "none") == 0)
	return (XtPointer) 0;

    p = XmGetPixmapByDepth(XtScreen(Shell1), value, 1, 0, 1);

    return (XtPointer) p ;
}


static XtPointer 
DetailConvert(String value)

{
    XmStringTable st;
    int           num_details, i;
    

    if (strcmp(value, DetailValues[0]) == 0)
	num_details = 1;
    if (strcmp(value, DetailValues[1]) == 0)
	num_details = 2;
    if (strcmp(value, DetailValues[2]) == 0)
	num_details = 3;

    st = (XmStringTable) XtMalloc (num_details * sizeof(XmString));

    for (i = 0; i < num_details; i++)
	st[i] = XmStringGenerate(DetailString[i], NULL, XmCHARSET_TEXT, NULL);
    
    return (XtPointer)st ;
}


static XtPointer 
CardinalConvert(String value)

{
    Dimension d ;
    XtPointer return_value;

    d = atoi(value);

    return_value = (XtPointer) ((int) d);

    return (return_value);
}


static XtPointer 
ViewTypeConvert(String value)

{
    unsigned char c;
    XtPointer return_value;

    if (strcmp(value, "XmLARGE_ICON") == 0)
	c = XmLARGE_ICON;
    if (strcmp(value, "XmSMALL_ICON") == 0)
	c = XmSMALL_ICON;

    return_value = (XtPointer) ((int) c);

    return (return_value);
}

static XtPointer 
VisualEmphasisConvert(String value)

{
    unsigned char c;
    XtPointer return_value;

    if (strcmp(value, "XmNOT_SELECTED") == 0)
	c = XmNOT_SELECTED;
    if (strcmp(value, "XmSELECTED") == 0)
	c = XmSELECTED;

    return_value = (XtPointer) ((int) c);

    return (return_value);
}