controls.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 libraries 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. * controls.c
  32. *
  33. * Example code for libDtWidget controls
  34. * (ComboBox, SpinBox, MenuButton)
  35. */
  36. #include <Xm/XmAll.h>
  37. #include <Xm/SSpinB.h>
  38. #include <Dt/ComboBox.h>
  39. #include <Dt/MenuButton.h>
  40. #define ApplicationClass "Controls"
  41. static void CreateSpinBoxes(Widget);
  42. static void CreateComboBoxes(Widget);
  43. static void CreateMenuButtons(Widget);
  44. main(int argc, char **argv)
  45. {
  46. XtAppContext appContext;
  47. Arg args[20];
  48. int n;
  49. Widget toplevel, mainWindow, spinContainer, comboContainer, menuContainer;
  50. toplevel = XtAppInitialize(&appContext, ApplicationClass, NULL, 0,
  51. &argc, argv, NULL, NULL, 0);
  52. n = 0;
  53. XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
  54. XtSetArg(args[n], XmNspacing, 40); n++;
  55. mainWindow = XmCreateWorkArea(toplevel, "mainWindow", args, n);
  56. XtManageChild(mainWindow);
  57. n = 0;
  58. XtSetArg(args[n], XmNspacing, 20); n++;
  59. spinContainer = XmCreateWorkArea(mainWindow, "spinContainer", args, n);
  60. XtManageChild(spinContainer);
  61. CreateSpinBoxes(spinContainer);
  62. n = 0;
  63. XtSetArg(args[n], XmNspacing, 20); n++;
  64. comboContainer = XmCreateWorkArea(mainWindow, "comboContainer", args, n);
  65. XtManageChild(comboContainer);
  66. CreateComboBoxes(comboContainer);
  67. n = 0;
  68. XtSetArg(args[n], XmNspacing, 20); n++;
  69. menuContainer = XmCreateWorkArea(mainWindow, "menuContainer", args, n);
  70. XtManageChild(menuContainer);
  71. CreateMenuButtons(menuContainer);
  72. XtRealizeWidget(toplevel);
  73. XtAppMainLoop(appContext);
  74. }
  75. /*
  76. * Example code for XmSimpleSpinBox
  77. */
  78. static char *spinValueStrings[] = {
  79. "alpha", "beta", "gamma", "delta",
  80. "epsilon", "zeta", "eta", "theta",
  81. "iota", "kappa", "lambda", "mu",
  82. "nu", "xi", "omicron", "pi",
  83. "rho", "sigma", "tau", "upsilon",
  84. "phi", "chi", "psi", "omega"
  85. };
  86. static void ModifyVerifyCb(Widget, XtPointer, XtPointer);
  87. static void CreateSpinBoxes(Widget parent)
  88. {
  89. Widget titleLabel, spinBox;
  90. XmString *valueXmstrings;
  91. int numValueStrings;
  92. XmString labelString;
  93. Arg args[20];
  94. int i, n;
  95. /* Create value compound strings */
  96. numValueStrings = XtNumber(spinValueStrings);
  97. valueXmstrings = (XmString *)XtMalloc(numValueStrings * sizeof(XmString*));
  98. for (i = 0; i < numValueStrings; i++) {
  99. valueXmstrings[i] = XmStringCreateLocalized(spinValueStrings[i]);
  100. }
  101. /* Create title label */
  102. labelString = XmStringCreateLocalized("SpinBox Widget");
  103. n = 0;
  104. XtSetArg(args[n], XmNlabelString, labelString); n++;
  105. titleLabel = XmCreateLabel(parent, "title", args, n);
  106. XtManageChild(titleLabel);
  107. XmStringFree(labelString);
  108. /*
  109. * Create a SimpleSpinBox containing string values.
  110. */
  111. n = 0;
  112. XtSetArg(args[n], XmNvalues, valueXmstrings); n++;
  113. XtSetArg(args[n], XmNnumValues, numValueStrings); n++;
  114. XtSetArg(args[n], XmNcolumns, 10); n++;
  115. spinBox = XmCreateSimpleSpinBox(parent, "spinBox1", args, n);
  116. XtManageChild(spinBox);
  117. /*
  118. * Create a SpinBox containing numeric values to 3 decimal places.
  119. * Position the arrows on either side of the displayed value.
  120. */
  121. n = 0;
  122. XtSetArg(args[n], XmNspinBoxChildType, XmNUMERIC); n++;
  123. XtSetArg(args[n], XmNminimumValue, 1000); n++;
  124. XtSetArg(args[n], XmNmaximumValue, 100000); n++;
  125. XtSetArg(args[n], XmNincrementValue,1000); n++;
  126. XtSetArg(args[n], XmNdecimalPoints,3); n++;
  127. XtSetArg(args[n], XmNposition,1000); n++;
  128. XtSetArg(args[n], XmNarrowLayout, XmARROWS_SPLIT); n++;
  129. XtSetArg(args[n], XmNcolumns, 10); n++;
  130. spinBox = XmCreateSimpleSpinBox(parent, "spinBox2", args, n);
  131. XtManageChild(spinBox);
  132. /*
  133. * Create a SpinBox containing numeric values to 2 decimal places.
  134. * Position the arrows on the left of the displayed value.
  135. * Disallow alternate user changes by adding a modify/verify callback.
  136. */
  137. n = 0;
  138. XtSetArg(args[n], XmNspinBoxChildType, XmNUMERIC); n++;
  139. XtSetArg(args[n], XmNminimumValue, 1500); n++;
  140. XtSetArg(args[n], XmNmaximumValue, 60500); n++;
  141. XtSetArg(args[n], XmNincrementValue,1500); n++;
  142. XtSetArg(args[n], XmNdecimalPoints,2); n++;
  143. XtSetArg(args[n], XmNposition,7500); n++;
  144. XtSetArg(args[n], XmNarrowLayout, XmARROWS_FLAT_BEGINNING); n++;
  145. XtSetArg(args[n], XmNcolumns, 10); n++;
  146. spinBox = XmCreateSimpleSpinBox(parent, "spinBox3", args, n);
  147. XtManageChild(spinBox);
  148. XtAddCallback(spinBox, XmNmodifyVerifyCallback, ModifyVerifyCb, NULL);
  149. /*
  150. * Create a SpinBox containing string values.
  151. * Position the arrows on the left of the display value
  152. */
  153. n = 0;
  154. XtSetArg(args[n], XmNvalues, valueXmstrings); n++;
  155. XtSetArg(args[n], XmNnumValues, numValueStrings); n++;
  156. XtSetArg(args[n], XmNarrowLayout, XmARROWS_BEGINNING); n++;
  157. XtSetArg(args[n], XmNcolumns, 10); n++;
  158. spinBox = XmCreateSimpleSpinBox(parent, "spinBox4", args, n);
  159. XtManageChild(spinBox);
  160. /*
  161. * Create a SpinBox containing numeric values to 3 decimal places.
  162. * Position the arrows on the right of the displayed value.
  163. */
  164. n = 0;
  165. XtSetArg(args[n], XmNspinBoxChildType, XmNUMERIC); n++;
  166. XtSetArg(args[n], XmNminimumValue, 1000); n++;
  167. XtSetArg(args[n], XmNmaximumValue, 100000); n++;
  168. XtSetArg(args[n], XmNincrementValue,1000); n++;
  169. XtSetArg(args[n], XmNdecimalPoints,3); n++;
  170. XtSetArg(args[n], XmNposition,1000); n++;
  171. XtSetArg(args[n], XmNarrowLayout, XmARROWS_FLAT_END); n++;
  172. XtSetArg(args[n], XmNcolumns, 10); n++;
  173. spinBox = XmCreateSimpleSpinBox(parent, "spinBox5", args, n);
  174. XtManageChild(spinBox);
  175. /*
  176. * Free value strings, SpinBox has taken a copy.
  177. */
  178. for (i = 0; i < numValueStrings; i++) {
  179. XmStringFree(valueXmstrings[i]);
  180. }
  181. XtFree((char*)valueXmstrings);
  182. }
  183. /*
  184. * modify/verify callback.
  185. *
  186. * Allow/disallow alternate user changes
  187. */
  188. static void ModifyVerifyCb(Widget w, XtPointer cd, XtPointer cb)
  189. {
  190. XmSpinBoxCallbackStruct *scb= (XmSpinBoxCallbackStruct*)cb;
  191. static Boolean allowChange = True;
  192. scb->doit = allowChange;
  193. if (allowChange == False) {
  194. printf("XmSpinBox: XmNmodifyVerifyCallback. Change disallowed.\n");
  195. XBell(XtDisplay(w), 0);
  196. }
  197. allowChange = (allowChange == True) ? False : True;
  198. }
  199. /*
  200. * Example code for DtComboBox
  201. */
  202. static char *comboValueStrings[] = {
  203. "alpha", "beta", "gamma", "delta",
  204. "epsilon", "zeta", "eta", "theta",
  205. "iota", "kappa", "lambda", "mu",
  206. "nu", "xi", "omicron", "pi",
  207. "rho", "sigma", "tau", "upsilon",
  208. "phi", "chi", "psi", "omega"
  209. };
  210. static char *colorStrings[] = { "Red", "Yellow", "Green", "Brown", "Blue" };
  211. static void CreateComboBoxes(Widget parent)
  212. {
  213. Widget titleLabel, comboBox, list;
  214. XmString *valueXmstrings, *colorXmstrings;
  215. int numValueStrings, numColorStrings;
  216. XmString labelString, xmString;
  217. Arg args[20];
  218. int i, n;
  219. /* Create value compound strings */
  220. numValueStrings = XtNumber(comboValueStrings);
  221. valueXmstrings = (XmString *)XtMalloc(numValueStrings * sizeof(XmString*));
  222. for (i = 0; i < numValueStrings; i++) {
  223. valueXmstrings[i] = XmStringCreateLocalized(comboValueStrings[i]);
  224. }
  225. /* Create color compound strings */
  226. numColorStrings = XtNumber(colorStrings);
  227. colorXmstrings = (XmString *)XtMalloc(numColorStrings * sizeof(XmString*));
  228. for (i = 0; i < numColorStrings; i++) {
  229. colorXmstrings[i] = XmStringCreateLocalized(colorStrings[i]);
  230. }
  231. /* Create title label */
  232. labelString = XmStringCreateLocalized("ComboBox Widget");
  233. n = 0;
  234. XtSetArg(args[n], XmNlabelString, labelString); n++;
  235. titleLabel = XmCreateLabel(parent, "title", args, n);
  236. XtManageChild(titleLabel);
  237. XmStringFree(labelString);
  238. /*
  239. * Create an editable ComboBox containing the color strings.
  240. * Get the widget id of the drop down list, add some greek
  241. * letter names to it, and make more items visible.
  242. */
  243. n = 0;
  244. XtSetArg(args[n], DtNcomboBoxType, DtDROP_DOWN_COMBO_BOX); n++;
  245. XtSetArg(args[n], DtNitems, colorXmstrings); n++;
  246. XtSetArg(args[n], DtNitemCount, numColorStrings); n++;
  247. XtSetArg(args[n], DtNvisibleItemCount, 5); n++;
  248. XtSetArg(args[n], DtNcolumns, 10); n++;
  249. comboBox = DtCreateComboBox(parent, "comboBox1", args, n);
  250. XtManageChild(comboBox);
  251. list = XtNameToWidget(comboBox, "*List");
  252. XmListAddItems(list, valueXmstrings, 10, 0);
  253. XtVaSetValues(list, XmNvisibleItemCount, 10, NULL);
  254. /*
  255. * Create an editable ComboBox with no entries.
  256. * Get the widget id of the drop down list, add some greek
  257. * letter names to it and select the third item in the list.
  258. */
  259. n = 0;
  260. XtSetArg(args[n], DtNcomboBoxType, DtDROP_DOWN_COMBO_BOX); n++;
  261. XtSetArg(args[n], DtNorientation, DtLEFT); n++;
  262. XtSetArg(args[n], DtNcolumns, 10); n++;
  263. comboBox = DtCreateComboBox(parent, "comboBox2", args, n);
  264. XtManageChild(comboBox);
  265. list = XtNameToWidget(comboBox, "*List");
  266. XmListAddItems(list, valueXmstrings, 7, 0);
  267. XtVaSetValues(list, XmNvisibleItemCount, 7, NULL);
  268. XtVaSetValues(comboBox, DtNselectedPosition, 3, NULL);
  269. /*
  270. * Create a non-editable ComboBox containing some greek letter names.
  271. * Position the arrow on the left.
  272. * Select the 'gamma' item in the list.
  273. */
  274. n = 0;
  275. XtSetArg(args[n], DtNorientation, DtLEFT); n++;
  276. XtSetArg(args[n], DtNitems, valueXmstrings); n++;
  277. XtSetArg(args[n], DtNitemCount, numValueStrings); n++;
  278. XtSetArg(args[n], DtNvisibleItemCount, 8); n++;
  279. comboBox = DtCreateComboBox(parent, "comboBox3", args, n);
  280. XtManageChild(comboBox);
  281. xmString = XmStringCreateLocalized("gamma");
  282. XtVaSetValues(comboBox, DtNselectedItem, xmString, NULL);
  283. XmStringFree(xmString);
  284. /*
  285. * Create a non-editable ComboBox with no entries.
  286. * Position the arrow on the right.
  287. * Add the greek letter names to the list and select the fourth item.
  288. */
  289. n = 0;
  290. XtSetArg(args[n], DtNorientation, DtRIGHT); n++;
  291. XtSetArg(args[n], DtNvisibleItemCount, 8); n++;
  292. comboBox = DtCreateComboBox(parent, "comboBox4", args, n);
  293. XtManageChild(comboBox);
  294. for (i = 0; i < numValueStrings; i++) {
  295. DtComboBoxAddItem(comboBox, valueXmstrings[i], 0, True);
  296. }
  297. XtVaSetValues(comboBox, DtNselectedPosition, 4, NULL);
  298. /*
  299. * Free value and color strings, ComboBox has taken a copy.
  300. */
  301. for (i = 0; i < numValueStrings; i++) {
  302. XmStringFree(valueXmstrings[i]);
  303. }
  304. XtFree((char*)valueXmstrings);
  305. for (i = 0; i < numColorStrings; i++) {
  306. XmStringFree(colorXmstrings[i]);
  307. }
  308. XtFree((char*)colorXmstrings);
  309. }
  310. /*
  311. * Example code for DtMenuButton
  312. */
  313. /* MenuButton custom glyph */
  314. #define menu_glyph_width 16
  315. #define menu_glyph_height 16
  316. static unsigned char menu_glyph_bits[] = {
  317. 0xe0, 0x03, 0x98, 0x0f, 0x84, 0x1f, 0x82, 0x3f, 0x82, 0x3f, 0x81, 0x7f,
  318. 0x81, 0x7f, 0xff, 0x7f, 0xff, 0x40, 0xff, 0x40, 0xfe, 0x20, 0xfe, 0x20,
  319. 0xfc, 0x10, 0xf8, 0x0c, 0xe0, 0x03, 0x00, 0x00};
  320. static void CreateMenuButtons(Widget parent)
  321. {
  322. Widget menuButton, submenu, titleLabel, button;
  323. Pixmap cascadePixmap;
  324. Pixel fg, bg;
  325. Cardinal depth;
  326. XmString labelString;
  327. Arg args[20];
  328. int i, n;
  329. /* Create title label */
  330. labelString = XmStringCreateLocalized("MenuButton Widget");
  331. n = 0;
  332. XtSetArg(args[n], XmNlabelString, labelString); n++;
  333. titleLabel = XmCreateLabel(parent, "title", args, n);
  334. XtManageChild(titleLabel);
  335. XmStringFree(labelString);
  336. /*
  337. * Create a MenuButton.
  338. * Add push buttons to the built-in popup menu.
  339. */
  340. labelString = XmStringCreateLocalized("Action");
  341. n = 0;
  342. XtSetArg(args[n], XmNlabelString, labelString); n++;
  343. menuButton = DtCreateMenuButton(parent, "menuButton1", args, n);
  344. XtManageChild(menuButton);
  345. XmStringFree(labelString);
  346. XtVaGetValues(menuButton, DtNsubMenuId, &submenu, NULL);
  347. button = XmCreatePushButton(submenu, "Push", NULL, 0);
  348. XtManageChild(button);
  349. button = XmCreatePushButton(submenu, "Pull", NULL, 0);
  350. XtManageChild(button);
  351. button = XmCreatePushButton(submenu, "Turn", NULL, 0);
  352. XtManageChild(button);
  353. /*
  354. * Create a MenuButton.
  355. * Replace the built-in popup menu with a tear-off menu.
  356. * Add a custom pixmap in the colors of the MenuButton.
  357. */
  358. labelString = XmStringCreateLocalized("Movement");
  359. n = 0;
  360. XtSetArg(args[n], XmNlabelString, labelString); n++;
  361. menuButton = DtCreateMenuButton(parent, "menuButton1", args, n);
  362. XtManageChild(menuButton);
  363. XmStringFree(labelString);
  364. /* Create a tear-off menu */
  365. n = 0;
  366. XtSetArg(args[0], XmNtearOffModel, XmTEAR_OFF_ENABLED); n++;
  367. submenu = XmCreatePopupMenu(menuButton, "submenu", args, n);
  368. button = XmCreatePushButton(submenu, "Run", NULL, 0);
  369. XtManageChild(button);
  370. button = XmCreatePushButton(submenu, "Jump", NULL, 0);
  371. XtManageChild(button);
  372. button = XmCreatePushButton(submenu, "Stop", NULL, 0);
  373. XtManageChild(button);
  374. XtVaSetValues(menuButton, DtNsubMenuId, submenu, NULL);
  375. /* Create a pixmap using the menu button's colors and depth */
  376. XtVaGetValues(menuButton,
  377. XmNforeground, &fg,
  378. XmNbackground, &bg,
  379. XmNdepth, &depth,
  380. NULL);
  381. cascadePixmap = XCreatePixmapFromBitmapData(XtDisplay(menuButton),
  382. DefaultRootWindow(XtDisplay(menuButton)),
  383. (char*)menu_glyph_bits,
  384. menu_glyph_width, menu_glyph_height,
  385. fg, bg, depth);
  386. XtVaSetValues(menuButton, DtNcascadePixmap, cascadePixmap, NULL);
  387. }