2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $XConsortium: controls.c /main/4 1995/10/27 10:41:00 rswiston $ */
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.
35 * Example code for libDtWidget controls
36 * (ComboBox, SpinBox, MenuButton)
41 #include <Xm/SSpinB.h>
42 #include <Dt/ComboBox.h>
43 #include <Dt/MenuButton.h>
45 #define ApplicationClass "Controls"
47 static void CreateSpinBoxes(Widget);
48 static void CreateComboBoxes(Widget);
49 static void CreateMenuButtons(Widget);
51 main(int argc, char **argv)
53 XtAppContext appContext;
57 Widget toplevel, mainWindow, spinContainer, comboContainer, menuContainer;
59 toplevel = XtAppInitialize(&appContext, ApplicationClass, NULL, 0,
60 &argc, argv, NULL, NULL, 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);
69 XtSetArg(args[n], XmNspacing, 20); n++;
70 spinContainer = XmCreateWorkArea(mainWindow, "spinContainer", args, n);
71 XtManageChild(spinContainer);
72 CreateSpinBoxes(spinContainer);
75 XtSetArg(args[n], XmNspacing, 20); n++;
76 comboContainer = XmCreateWorkArea(mainWindow, "comboContainer", args, n);
77 XtManageChild(comboContainer);
78 CreateComboBoxes(comboContainer);
81 XtSetArg(args[n], XmNspacing, 20); n++;
82 menuContainer = XmCreateWorkArea(mainWindow, "menuContainer", args, n);
83 XtManageChild(menuContainer);
84 CreateMenuButtons(menuContainer);
86 XtRealizeWidget(toplevel);
87 XtAppMainLoop(appContext);
92 * Example code for XmSimpleSpinBox
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"
104 static void ModifyVerifyCb(Widget, XtPointer, XtPointer);
106 static void CreateSpinBoxes(Widget parent)
108 Widget titleLabel, spinBox;
109 XmString *valueXmstrings;
111 XmString labelString;
115 /* Create value compound strings */
117 numValueStrings = XtNumber(spinValueStrings);
118 valueXmstrings = (XmString *)XtMalloc(numValueStrings * sizeof(XmString*));
119 for (i = 0; i < numValueStrings; i++) {
120 valueXmstrings[i] = XmStringCreateLocalized(spinValueStrings[i]);
123 /* Create title label */
125 labelString = XmStringCreateLocalized("SpinBox Widget");
127 XtSetArg(args[n], XmNlabelString, labelString); n++;
128 titleLabel = XmCreateLabel(parent, "title", args, n);
129 XtManageChild(titleLabel);
130 XmStringFree(labelString);
134 * Create a SimpleSpinBox containing string values.
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);
146 * Create a SpinBox containing numeric values to 3 decimal places.
147 * Position the arrows on either side of the displayed value.
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);
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.
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);
181 XtAddCallback(spinBox, XmNmodifyVerifyCallback, ModifyVerifyCb, NULL);
185 * Create a SpinBox containing string values.
186 * Position the arrows on the left of the display value
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);
199 * Create a SpinBox containing numeric values to 3 decimal places.
200 * Position the arrows on the right of the displayed value.
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);
217 * Free value strings, SpinBox has taken a copy.
220 for (i = 0; i < numValueStrings; i++) {
221 XmStringFree(valueXmstrings[i]);
223 XtFree((char*)valueXmstrings);
229 * modify/verify callback.
231 * Allow/disallow alternate user changes
234 static void ModifyVerifyCb(Widget w, XtPointer cd, XtPointer cb)
236 XmSpinBoxCallbackStruct *scb= (XmSpinBoxCallbackStruct*)cb;
237 static Boolean allowChange = True;
239 scb->doit = allowChange;
241 if (allowChange == False) {
242 printf("XmSpinBox: XmNmodifyVerifyCallback. Change disallowed.\n");
243 XBell(XtDisplay(w), 0);
246 allowChange = (allowChange == True) ? False : True;
252 * Example code for DtComboBox
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"
265 static char *colorStrings[] = { "Red", "Yellow", "Green", "Brown", "Blue" };
267 static void CreateComboBoxes(Widget parent)
269 Widget titleLabel, comboBox, list;
270 XmString *valueXmstrings, *colorXmstrings;
271 int numValueStrings, numColorStrings;
272 XmString labelString, xmString;
276 /* Create value compound strings */
278 numValueStrings = XtNumber(comboValueStrings);
279 valueXmstrings = (XmString *)XtMalloc(numValueStrings * sizeof(XmString*));
280 for (i = 0; i < numValueStrings; i++) {
281 valueXmstrings[i] = XmStringCreateLocalized(comboValueStrings[i]);
284 /* Create color compound strings */
286 numColorStrings = XtNumber(colorStrings);
287 colorXmstrings = (XmString *)XtMalloc(numColorStrings * sizeof(XmString*));
288 for (i = 0; i < numColorStrings; i++) {
289 colorXmstrings[i] = XmStringCreateLocalized(colorStrings[i]);
292 /* Create title label */
294 labelString = XmStringCreateLocalized("ComboBox Widget");
296 XtSetArg(args[n], XmNlabelString, labelString); n++;
297 titleLabel = XmCreateLabel(parent, "title", args, n);
298 XtManageChild(titleLabel);
299 XmStringFree(labelString);
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.
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);
317 list = XtNameToWidget(comboBox, "*List");
318 XmListAddItems(list, valueXmstrings, 10, 0);
319 XtVaSetValues(list, XmNvisibleItemCount, 10, NULL);
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.
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);
335 list = XtNameToWidget(comboBox, "*List");
336 XmListAddItems(list, valueXmstrings, 7, 0);
337 XtVaSetValues(list, XmNvisibleItemCount, 7, NULL);
338 XtVaSetValues(comboBox, DtNselectedPosition, 3, NULL);
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.
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);
355 xmString = XmStringCreateLocalized("gamma");
356 XtVaSetValues(comboBox, DtNselectedItem, xmString, NULL);
357 XmStringFree(xmString);
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.
367 XtSetArg(args[n], DtNorientation, DtRIGHT); n++;
368 XtSetArg(args[n], DtNvisibleItemCount, 8); n++;
369 comboBox = DtCreateComboBox(parent, "comboBox4", args, n);
370 XtManageChild(comboBox);
372 for (i = 0; i < numValueStrings; i++) {
373 DtComboBoxAddItem(comboBox, valueXmstrings[i], 0, True);
375 XtVaSetValues(comboBox, DtNselectedPosition, 4, NULL);
379 * Free value and color strings, ComboBox has taken a copy.
382 for (i = 0; i < numValueStrings; i++) {
383 XmStringFree(valueXmstrings[i]);
385 XtFree((char*)valueXmstrings);
387 for (i = 0; i < numColorStrings; i++) {
388 XmStringFree(colorXmstrings[i]);
390 XtFree((char*)colorXmstrings);
396 * Example code for DtMenuButton
399 /* MenuButton custom glyph */
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};
408 static void CreateMenuButtons(Widget parent)
410 Widget menuButton, submenu, titleLabel, button;
411 Pixmap cascadePixmap;
414 XmString labelString;
418 /* Create title label */
420 labelString = XmStringCreateLocalized("MenuButton Widget");
422 XtSetArg(args[n], XmNlabelString, labelString); n++;
423 titleLabel = XmCreateLabel(parent, "title", args, n);
424 XtManageChild(titleLabel);
425 XmStringFree(labelString);
429 * Create a MenuButton.
430 * Add push buttons to the built-in popup menu.
433 labelString = XmStringCreateLocalized("Action");
435 XtSetArg(args[n], XmNlabelString, labelString); n++;
436 menuButton = DtCreateMenuButton(parent, "menuButton1", args, n);
437 XtManageChild(menuButton);
438 XmStringFree(labelString);
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);
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.
455 labelString = XmStringCreateLocalized("Movement");
457 XtSetArg(args[n], XmNlabelString, labelString); n++;
458 menuButton = DtCreateMenuButton(parent, "menuButton1", args, n);
459 XtManageChild(menuButton);
460 XmStringFree(labelString);
462 /* Create a tear-off menu */
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);
474 XtVaSetValues(menuButton, DtNsubMenuId, submenu, NULL);
476 /* Create a pixmap using the menu button's colors and depth */
478 XtVaGetValues(menuButton,
484 cascadePixmap = XCreatePixmapFromBitmapData(XtDisplay(menuButton),
485 DefaultRootWindow(XtDisplay(menuButton)),
486 (char*)menu_glyph_bits,
487 menu_glyph_width, menu_glyph_height,
489 XtVaSetValues(menuButton, DtNcascadePixmap, cascadePixmap, NULL);