Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / examples / dtwidget / controls.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: controls.c /main/4 1995/10/27 10:41:00 rswiston $ */
24 /*
25  * (c) Copyright 1993, 1994 Hewlett-Packard Company     
26  * (c) Copyright 1993, 1994 International Business Machines Corp.
27  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
28  * (c) Copyright 1993, 1994 Novell, Inc.
29  */
30
31
32 /*
33  * controls.c
34  *
35  * Example code for libDtWidget controls
36  * (ComboBox, SpinBox, MenuButton)
37  */
38
39 #include <Xm/XmAll.h>
40
41 #include <Xm/SSpinB.h>
42 #include <Dt/ComboBox.h>
43 #include <Dt/MenuButton.h>
44
45 #define ApplicationClass "Controls"
46
47 static void CreateSpinBoxes(Widget);
48 static void CreateComboBoxes(Widget);
49 static void CreateMenuButtons(Widget);
50
51 main(int argc, char **argv)
52 {
53     XtAppContext appContext;
54     Arg args[20];
55     int n;
56
57     Widget toplevel, mainWindow, spinContainer, comboContainer, menuContainer;
58     
59     toplevel = XtAppInitialize(&appContext, ApplicationClass, NULL, 0,
60                                                 &argc, argv, NULL, NULL, 0);
61
62     n = 0;
63     XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
64     XtSetArg(args[n], XmNspacing, 40); n++;
65     mainWindow = XmCreateWorkArea(toplevel, "mainWindow", args, n);
66     XtManageChild(mainWindow);
67
68     n = 0;
69     XtSetArg(args[n], XmNspacing, 20); n++;
70     spinContainer = XmCreateWorkArea(mainWindow, "spinContainer", args, n);
71     XtManageChild(spinContainer);
72     CreateSpinBoxes(spinContainer);
73
74     n = 0;
75     XtSetArg(args[n], XmNspacing, 20); n++;
76     comboContainer = XmCreateWorkArea(mainWindow, "comboContainer", args, n);
77     XtManageChild(comboContainer);
78     CreateComboBoxes(comboContainer);
79
80     n = 0;
81     XtSetArg(args[n], XmNspacing, 20); n++;
82     menuContainer = XmCreateWorkArea(mainWindow, "menuContainer", args, n);
83     XtManageChild(menuContainer);
84     CreateMenuButtons(menuContainer);
85
86     XtRealizeWidget(toplevel);
87     XtAppMainLoop(appContext);
88 }
89
90
91 /*
92  * Example code for XmSimpleSpinBox
93  */
94
95 static char *spinValueStrings[] = {
96         "alpha", "beta", "gamma", "delta",
97         "epsilon", "zeta", "eta", "theta",
98         "iota", "kappa", "lambda", "mu",
99         "nu", "xi", "omicron", "pi",
100         "rho", "sigma", "tau", "upsilon",
101         "phi", "chi", "psi", "omega"
102 };
103
104 static void ModifyVerifyCb(Widget, XtPointer, XtPointer);
105
106 static void CreateSpinBoxes(Widget parent)
107 {
108     Widget titleLabel, spinBox;
109     XmString *valueXmstrings;
110     int numValueStrings;
111     XmString labelString;
112     Arg args[20];
113     int i, n;
114     
115     /* Create value compound strings */
116
117     numValueStrings = XtNumber(spinValueStrings);
118     valueXmstrings = (XmString *)XtMalloc(numValueStrings * sizeof(XmString*));
119     for (i = 0; i < numValueStrings; i++) {
120         valueXmstrings[i] = XmStringCreateLocalized(spinValueStrings[i]);
121     }
122
123     /* Create title label */
124
125     labelString = XmStringCreateLocalized("SpinBox Widget");
126     n = 0;
127     XtSetArg(args[n], XmNlabelString, labelString); n++;
128     titleLabel = XmCreateLabel(parent, "title", args, n);
129     XtManageChild(titleLabel);
130     XmStringFree(labelString);
131
132
133     /*
134      * Create a SimpleSpinBox containing string values.
135      */
136
137     n = 0;
138     XtSetArg(args[n], XmNvalues, valueXmstrings); n++;
139     XtSetArg(args[n], XmNnumValues, numValueStrings); n++;
140     XtSetArg(args[n], XmNcolumns, 10); n++;
141     spinBox = XmCreateSimpleSpinBox(parent, "spinBox1", args, n);
142     XtManageChild(spinBox);
143
144
145     /*
146      * Create a SpinBox containing numeric values to 3 decimal places.
147      * Position the arrows on either side of the displayed value.
148      */
149
150     n = 0; 
151     XtSetArg(args[n], XmNspinBoxChildType, XmNUMERIC); n++;
152     XtSetArg(args[n], XmNminimumValue, 1000); n++;
153     XtSetArg(args[n], XmNmaximumValue, 100000); n++;
154     XtSetArg(args[n], XmNincrementValue,1000); n++;
155     XtSetArg(args[n], XmNdecimalPoints,3); n++;
156     XtSetArg(args[n], XmNposition,1000); n++;
157     XtSetArg(args[n], XmNarrowLayout, XmARROWS_SPLIT); n++;
158     XtSetArg(args[n], XmNcolumns, 10); n++;
159     spinBox = XmCreateSimpleSpinBox(parent, "spinBox2", args, n);
160     XtManageChild(spinBox);
161
162
163     /*
164      * Create a SpinBox containing numeric values to 2 decimal places.
165      * Position the arrows on the left of the displayed value.
166      * Disallow alternate user changes by adding a modify/verify callback.
167      */
168
169     n = 0; 
170     XtSetArg(args[n], XmNspinBoxChildType, XmNUMERIC); n++;
171     XtSetArg(args[n], XmNminimumValue, 1500); n++;
172     XtSetArg(args[n], XmNmaximumValue, 60500); n++;
173     XtSetArg(args[n], XmNincrementValue,1500); n++;
174     XtSetArg(args[n], XmNdecimalPoints,2); n++;
175     XtSetArg(args[n], XmNposition,7500); n++;
176     XtSetArg(args[n], XmNarrowLayout, XmARROWS_FLAT_BEGINNING); n++;
177     XtSetArg(args[n], XmNcolumns, 10); n++;
178     spinBox = XmCreateSimpleSpinBox(parent, "spinBox3", args, n);
179     XtManageChild(spinBox);
180
181     XtAddCallback(spinBox, XmNmodifyVerifyCallback, ModifyVerifyCb, NULL);
182
183
184     /*
185      * Create a SpinBox containing string values.
186      * Position the arrows on the left of the display value
187      */
188
189     n = 0; 
190     XtSetArg(args[n], XmNvalues, valueXmstrings); n++;
191     XtSetArg(args[n], XmNnumValues, numValueStrings); n++;
192     XtSetArg(args[n], XmNarrowLayout, XmARROWS_BEGINNING); n++;
193     XtSetArg(args[n], XmNcolumns, 10); n++;
194     spinBox = XmCreateSimpleSpinBox(parent, "spinBox4", args, n);
195     XtManageChild(spinBox);
196
197
198     /*
199      * Create a SpinBox containing numeric values to 3 decimal places.
200      * Position the arrows on the right of the displayed value.
201      */
202
203     n = 0;
204     XtSetArg(args[n], XmNspinBoxChildType, XmNUMERIC); n++;
205     XtSetArg(args[n], XmNminimumValue, 1000); n++;
206     XtSetArg(args[n], XmNmaximumValue, 100000); n++;
207     XtSetArg(args[n], XmNincrementValue,1000); n++;
208     XtSetArg(args[n], XmNdecimalPoints,3); n++;
209     XtSetArg(args[n], XmNposition,1000); n++;
210     XtSetArg(args[n], XmNarrowLayout, XmARROWS_FLAT_END); n++;
211     XtSetArg(args[n], XmNcolumns, 10); n++;
212     spinBox = XmCreateSimpleSpinBox(parent, "spinBox5", args, n);
213     XtManageChild(spinBox);
214
215
216     /*
217      * Free value strings, SpinBox has taken a copy.
218      */
219
220     for (i = 0; i < numValueStrings; i++) {
221         XmStringFree(valueXmstrings[i]);
222     }
223     XtFree((char*)valueXmstrings);
224
225 }
226
227
228 /*
229  * modify/verify callback.
230  *
231  * Allow/disallow alternate user changes
232  */
233
234 static void ModifyVerifyCb(Widget w, XtPointer cd, XtPointer cb)
235 {
236     XmSpinBoxCallbackStruct *scb= (XmSpinBoxCallbackStruct*)cb;
237     static Boolean allowChange = True;
238
239     scb->doit = allowChange;
240
241     if (allowChange == False) {
242         printf("XmSpinBox: XmNmodifyVerifyCallback. Change disallowed.\n");
243         XBell(XtDisplay(w), 0);
244     }
245
246     allowChange = (allowChange == True) ? False : True;
247 }
248
249
250
251 /*
252  * Example code for DtComboBox
253  */
254
255
256 static char *comboValueStrings[] = {
257         "alpha", "beta", "gamma", "delta",
258         "epsilon", "zeta", "eta", "theta",
259         "iota", "kappa", "lambda", "mu",
260         "nu", "xi", "omicron", "pi",
261         "rho", "sigma", "tau", "upsilon",
262         "phi", "chi", "psi", "omega"
263 };
264
265 static char *colorStrings[] = { "Red", "Yellow", "Green", "Brown", "Blue" };
266
267 static void CreateComboBoxes(Widget parent)
268 {
269     Widget titleLabel, comboBox, list;
270     XmString *valueXmstrings, *colorXmstrings;
271     int numValueStrings, numColorStrings;
272     XmString labelString, xmString;
273     Arg args[20];
274     int i, n;
275     
276     /* Create value compound strings */
277
278     numValueStrings = XtNumber(comboValueStrings);
279     valueXmstrings = (XmString *)XtMalloc(numValueStrings * sizeof(XmString*));
280     for (i = 0; i < numValueStrings; i++) {
281         valueXmstrings[i] = XmStringCreateLocalized(comboValueStrings[i]);
282     }
283
284     /* Create color compound strings */
285
286     numColorStrings = XtNumber(colorStrings);
287     colorXmstrings = (XmString *)XtMalloc(numColorStrings * sizeof(XmString*));
288     for (i = 0; i < numColorStrings; i++) {
289         colorXmstrings[i] = XmStringCreateLocalized(colorStrings[i]);
290     }
291
292     /* Create title label */
293
294     labelString = XmStringCreateLocalized("ComboBox Widget");
295     n = 0;
296     XtSetArg(args[n], XmNlabelString, labelString); n++;
297     titleLabel = XmCreateLabel(parent, "title", args, n);
298     XtManageChild(titleLabel);
299     XmStringFree(labelString);
300
301
302     /*
303      * Create an editable ComboBox containing the color strings.
304      * Get the widget id of the drop down list, add some greek
305      * letter names to it, and make more items visible.
306      */
307
308     n = 0;
309     XtSetArg(args[n], DtNcomboBoxType, DtDROP_DOWN_COMBO_BOX); n++;
310     XtSetArg(args[n], DtNitems, colorXmstrings); n++;
311     XtSetArg(args[n], DtNitemCount, numColorStrings); n++;
312     XtSetArg(args[n], DtNvisibleItemCount, 5); n++;
313     XtSetArg(args[n], DtNcolumns, 10); n++;
314     comboBox = DtCreateComboBox(parent, "comboBox1", args, n);
315     XtManageChild(comboBox);
316
317     list = XtNameToWidget(comboBox, "*List");
318     XmListAddItems(list, valueXmstrings, 10, 0);
319     XtVaSetValues(list, XmNvisibleItemCount, 10, NULL);
320
321
322     /*
323      * Create an editable ComboBox with no entries.
324      * Get the widget id of the drop down list, add some greek
325      * letter names to it and select the third item in the list.
326      */
327
328     n = 0;
329     XtSetArg(args[n], DtNcomboBoxType, DtDROP_DOWN_COMBO_BOX); n++;
330     XtSetArg(args[n], DtNorientation, DtLEFT); n++;
331     XtSetArg(args[n], DtNcolumns, 10); n++;
332     comboBox = DtCreateComboBox(parent, "comboBox2", args, n);
333     XtManageChild(comboBox);
334
335     list = XtNameToWidget(comboBox, "*List");        
336     XmListAddItems(list, valueXmstrings, 7, 0);
337     XtVaSetValues(list, XmNvisibleItemCount, 7, NULL);
338     XtVaSetValues(comboBox, DtNselectedPosition, 3, NULL);
339
340
341     /*
342      * Create a non-editable ComboBox containing some greek letter names.
343      * Position the arrow on the left.
344      * Select the 'gamma' item in the list.
345      */
346
347     n = 0;
348     XtSetArg(args[n], DtNorientation, DtLEFT); n++;
349     XtSetArg(args[n], DtNitems, valueXmstrings); n++;
350     XtSetArg(args[n], DtNitemCount, numValueStrings); n++;
351     XtSetArg(args[n], DtNvisibleItemCount, 8); n++;
352     comboBox = DtCreateComboBox(parent, "comboBox3", args, n);
353     XtManageChild(comboBox);
354
355     xmString = XmStringCreateLocalized("gamma");
356     XtVaSetValues(comboBox, DtNselectedItem, xmString, NULL);
357     XmStringFree(xmString);
358
359
360     /*
361      * Create a non-editable ComboBox with no entries.
362      * Position the arrow on the right.
363      * Add the greek letter names to the list and select the fourth item. 
364      */
365
366     n = 0;
367     XtSetArg(args[n], DtNorientation, DtRIGHT); n++;
368     XtSetArg(args[n], DtNvisibleItemCount, 8); n++;
369     comboBox = DtCreateComboBox(parent, "comboBox4", args, n);
370     XtManageChild(comboBox);
371
372     for (i = 0; i < numValueStrings; i++) {
373         DtComboBoxAddItem(comboBox, valueXmstrings[i], 0, True);
374     }
375     XtVaSetValues(comboBox, DtNselectedPosition, 4, NULL);
376
377
378     /*
379      * Free value and color strings, ComboBox has taken a copy.
380      */
381
382     for (i = 0; i < numValueStrings; i++) {
383         XmStringFree(valueXmstrings[i]);
384     }
385     XtFree((char*)valueXmstrings);
386
387     for (i = 0; i < numColorStrings; i++) {
388         XmStringFree(colorXmstrings[i]);
389     }
390     XtFree((char*)colorXmstrings);
391 }
392
393
394
395 /*
396  * Example code for DtMenuButton
397  */
398
399 /* MenuButton custom glyph */
400
401 #define menu_glyph_width 16
402 #define menu_glyph_height 16
403 static unsigned char menu_glyph_bits[] = {
404    0xe0, 0x03, 0x98, 0x0f, 0x84, 0x1f, 0x82, 0x3f, 0x82, 0x3f, 0x81, 0x7f,
405    0x81, 0x7f, 0xff, 0x7f, 0xff, 0x40, 0xff, 0x40, 0xfe, 0x20, 0xfe, 0x20,
406    0xfc, 0x10, 0xf8, 0x0c, 0xe0, 0x03, 0x00, 0x00};
407
408 static void CreateMenuButtons(Widget parent)
409 {
410     Widget menuButton, submenu, titleLabel, button;
411     Pixmap cascadePixmap;
412     Pixel fg, bg;
413     Cardinal depth;
414     XmString labelString;
415     Arg args[20];
416     int i, n;
417
418     /* Create title label */
419
420     labelString = XmStringCreateLocalized("MenuButton Widget");
421     n = 0;
422     XtSetArg(args[n], XmNlabelString, labelString); n++;
423     titleLabel = XmCreateLabel(parent, "title", args, n);
424     XtManageChild(titleLabel);
425     XmStringFree(labelString);
426
427
428     /*
429      * Create a MenuButton.
430      * Add push buttons to the built-in popup menu.
431      */
432
433     labelString = XmStringCreateLocalized("Action");
434     n = 0;
435     XtSetArg(args[n], XmNlabelString, labelString); n++;
436     menuButton = DtCreateMenuButton(parent, "menuButton1", args, n);
437     XtManageChild(menuButton);
438     XmStringFree(labelString);
439
440     XtVaGetValues(menuButton, DtNsubMenuId, &submenu, NULL);
441     button = XmCreatePushButton(submenu, "Push", NULL, 0);
442     XtManageChild(button);
443     button = XmCreatePushButton(submenu, "Pull", NULL, 0);
444     XtManageChild(button);
445     button = XmCreatePushButton(submenu, "Turn", NULL, 0);
446     XtManageChild(button);
447
448
449     /*
450      * Create a MenuButton.
451      * Replace the built-in popup menu with a tear-off menu.
452      * Add a custom pixmap in the colors of the MenuButton.
453      */
454
455     labelString = XmStringCreateLocalized("Movement");
456     n = 0;
457     XtSetArg(args[n], XmNlabelString, labelString); n++;
458     menuButton = DtCreateMenuButton(parent, "menuButton1", args, n);
459     XtManageChild(menuButton);
460     XmStringFree(labelString);
461
462     /* Create a tear-off menu */
463
464     n = 0;
465     XtSetArg(args[0], XmNtearOffModel, XmTEAR_OFF_ENABLED); n++;
466     submenu = XmCreatePopupMenu(menuButton, "submenu", args, n);
467     button = XmCreatePushButton(submenu, "Run", NULL, 0);
468     XtManageChild(button);
469     button = XmCreatePushButton(submenu, "Jump", NULL, 0);
470     XtManageChild(button);
471     button = XmCreatePushButton(submenu, "Stop", NULL, 0);
472     XtManageChild(button);
473
474     XtVaSetValues(menuButton, DtNsubMenuId, submenu, NULL);
475
476     /* Create a pixmap using the menu button's colors and depth */
477
478     XtVaGetValues(menuButton,
479                         XmNforeground, &fg,
480                         XmNbackground, &bg,
481                         XmNdepth, &depth,
482                         NULL);
483
484     cascadePixmap = XCreatePixmapFromBitmapData(XtDisplay(menuButton),
485                                 DefaultRootWindow(XtDisplay(menuButton)),
486                                 (char*)menu_glyph_bits,
487                                 menu_glyph_width, menu_glyph_height,
488                                 fg, bg, depth);
489     XtVaSetValues(menuButton, DtNcascadePixmap, cascadePixmap, NULL);
490 }
491