GuiOptionMenu.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* GuiOptionMenu.cpp
  2. *
  3. * Copyright (C) 1993-2018 Paul Boersma, 2007 Stefan de Konink, 2013 Tom Naughton
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "GuiP.h"
  19. Thing_implement (GuiOptionMenu, GuiControl, 0);
  20. #if gtk
  21. static void _guiGtkOptionMenu_destroyCallback (GuiObject /* widget */, gpointer void_me) {
  22. iam (GuiOptionMenu);
  23. forget (me);
  24. }
  25. #elif motif
  26. static void _guiMotifOptionMenu_destroyCallback (GuiObject /* widget */, XtPointer void_me, XtPointer /* call */) {
  27. iam (GuiOptionMenu);
  28. forget (me);
  29. }
  30. #elif cocoa
  31. @implementation GuiCocoaOptionMenu {
  32. GuiOptionMenu d_userData;
  33. }
  34. - (void) dealloc { // override
  35. GuiOptionMenu me = d_userData;
  36. [self removeAllItems];
  37. [self setMenu:nil];
  38. forget (me);
  39. trace (U"deleting an option menu");
  40. [super dealloc];
  41. }
  42. - (GuiThing) getUserData {
  43. return d_userData;
  44. }
  45. - (void) setUserData: (GuiThing) userData {
  46. d_userData = (GuiOptionMenu) userData;
  47. }
  48. @end
  49. #endif
  50. void structGuiOptionMenu :: v_show () {
  51. #if gtk
  52. GuiOptionMenu_Parent :: v_show ();
  53. #elif motif
  54. XtManageChild (d_xmMenuBar);
  55. #elif cocoa
  56. //NSLog(@"cocoa structGuiOptionMenu :: v_show"); // ?
  57. #endif
  58. }
  59. void GuiOptionMenu_init (GuiOptionMenu me, GuiForm parent, int left, int right, int top, int bottom, uint32 flags)
  60. {
  61. my d_shell = parent -> d_shell;
  62. my d_parent = parent;
  63. #if gtk
  64. my d_widget = gtk_combo_box_new_text ();
  65. gtk_widget_set_size_request (GTK_WIDGET (my d_widget), right - left, bottom - top + 8);
  66. gtk_fixed_put (GTK_FIXED (parent -> d_widget), GTK_WIDGET (my d_widget), left, top - 6);
  67. gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (my d_widget), false);
  68. GTK_WIDGET_UNSET_FLAGS (my d_widget, GTK_CAN_DEFAULT);
  69. #elif motif
  70. my d_xmMenuBar = XmCreateMenuBar (parent -> d_widget, "UiOptionMenu", nullptr, 0);
  71. XtVaSetValues (my d_xmMenuBar, XmNx, left - 4, XmNy, top - 4,
  72. XmNwidth, right - left + 8, XmNheight, bottom - top + 8, nullptr);
  73. my d_xmCascadeButton = XmCreateCascadeButton (my d_xmMenuBar, "choice", nullptr, 0);
  74. my d_widget = XmCreatePulldownMenu (my d_xmMenuBar, "choice", nullptr, 0);
  75. if (flags & GuiMenu_INSENSITIVE)
  76. XtSetSensitive (my d_widget, False);
  77. XtVaSetValues (my d_xmCascadeButton, XmNsubMenuId, my d_widget, nullptr);
  78. XtManageChild (my d_xmCascadeButton);
  79. XtVaSetValues (my d_xmMenuBar, XmNwidth, right - left + 8, nullptr); // BUG: twice?
  80. XtVaSetValues (my d_xmCascadeButton, XmNx, 4, XmNy, 4, XmNwidth, right - left, XmNheight, bottom - top, nullptr);
  81. #elif cocoa
  82. GuiCocoaOptionMenu *optionMenu = [[GuiCocoaOptionMenu alloc] init];
  83. my d_widget = (GuiObject) optionMenu;
  84. my v_positionInForm (my d_widget, left, right, top - 1, bottom + 1, parent);
  85. [optionMenu setUserData: me];
  86. // [optionMenu setBezelStyle: NSRoundedBezelStyle];
  87. // [optionMenu setBordered: NO];
  88. #endif
  89. #if gtk
  90. g_signal_connect (G_OBJECT (my d_widget), "destroy", G_CALLBACK (_guiGtkOptionMenu_destroyCallback), me);
  91. #elif motif
  92. XtAddCallback (my d_widget, XmNdestroyCallback, _guiMotifOptionMenu_destroyCallback, me);
  93. #elif cocoa
  94. #endif
  95. }
  96. GuiOptionMenu GuiOptionMenu_create (GuiForm parent, int left, int right, int top, int bottom, uint32 flags) {
  97. autoGuiOptionMenu me = Thing_new (GuiOptionMenu);
  98. GuiOptionMenu_init (me.get(), parent, left, right, top, bottom, flags);
  99. return me.releaseToAmbiguousOwner();
  100. }
  101. GuiOptionMenu GuiOptionMenu_createShown (GuiForm parent, int left, int right, int top, int bottom, uint32 flags) {
  102. GuiOptionMenu me = GuiOptionMenu_create (parent, left, right, top, bottom, flags);
  103. GuiThing_show (me);
  104. return me;
  105. }
  106. #if motif
  107. static void cb_optionChanged (GuiObject w, XtPointer void_me, XtPointer call) {
  108. iam (GuiOptionMenu);
  109. (void) call;
  110. for (int i = 1; i <= my d_options.size; i ++) {
  111. GuiMenuItem item = my d_options.at [i];
  112. if (item -> d_widget == w) {
  113. XtVaSetValues (my d_xmCascadeButton, XmNlabelString, Melder_peek32to8 (item -> d_widget -> name.get()), nullptr);
  114. XmToggleButtonSetState (item -> d_widget, true, false);
  115. if (Melder_debug == 11) {
  116. Melder_warning (i, U" \"", item -> d_widget -> name.get(), U"\"");
  117. }
  118. } else {
  119. XmToggleButtonSetState (item -> d_widget, false, false);
  120. }
  121. }
  122. }
  123. #endif
  124. void GuiOptionMenu_addOption (GuiOptionMenu me, conststring32 text) {
  125. #if gtk
  126. gtk_combo_box_append_text (GTK_COMBO_BOX (my d_widget), Melder_peek32to8 (text));
  127. #elif motif
  128. autoGuiMenuItem menuItem = Thing_new (GuiMenuItem);
  129. menuItem -> d_widget = XtVaCreateManagedWidget (Melder_peek32to8 (text), xmToggleButtonWidgetClass, my d_widget, nullptr);
  130. XtAddCallback (menuItem -> d_widget, XmNvalueChangedCallback, cb_optionChanged, (XtPointer) me);
  131. my d_options. addItem_move (menuItem.move());
  132. #elif cocoa
  133. GuiCocoaOptionMenu *menu = (GuiCocoaOptionMenu *) my d_widget;
  134. [menu addItemWithTitle: [NSString stringWithUTF8String: Melder_peek32to8 (text)]];
  135. #endif
  136. }
  137. int GuiOptionMenu_getValue (GuiOptionMenu me) {
  138. my d_value = 0;
  139. #if gtk
  140. // TODO: Graag even een check :)
  141. my d_value = gtk_combo_box_get_active (GTK_COMBO_BOX (my d_widget)) + 1;
  142. #elif motif
  143. for (int i = 1; i <= my d_options.size; i ++) {
  144. GuiMenuItem menuItem = my d_options.at [i];
  145. if (XmToggleButtonGetState (menuItem -> d_widget))
  146. my d_value = i;
  147. }
  148. #elif cocoa
  149. GuiCocoaOptionMenu *menu = (GuiCocoaOptionMenu *) my d_widget;
  150. my d_value = [menu indexOfSelectedItem] + 1;
  151. #endif
  152. return my d_value;
  153. }
  154. void GuiOptionMenu_setValue (GuiOptionMenu me, int value) {
  155. #if gtk
  156. gtk_combo_box_set_active (GTK_COMBO_BOX (my d_widget), value - 1);
  157. #elif motif
  158. for (int i = 1; i <= my d_options.size; i ++) {
  159. GuiMenuItem menuItem = my d_options.at [i];
  160. XmToggleButtonSetState (menuItem -> d_widget, i == value, False);
  161. if (i == value) {
  162. XtVaSetValues (my d_xmCascadeButton, XmNlabelString, Melder_peek32to8 (menuItem -> d_widget -> name.get()), nullptr);
  163. }
  164. }
  165. #elif cocoa
  166. GuiCocoaOptionMenu *menu = (GuiCocoaOptionMenu *) my d_widget;
  167. [menu selectItemAtIndex: value - 1];
  168. #endif
  169. my d_value = value;
  170. }
  171. /* End of file GuiOptionMenu.cpp */