iup_menu.e 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. class IUP_MENU
  2. -- Creates a menu element, which groups 3 types of interface elements: item,
  3. -- submenu and separator. Any other interface element defined inside a menu
  4. -- will be an error.
  5. --
  6. -- A menu can be a menu bar of a dialog, defined by the dialog's MENU
  7. -- attribute, or a popup menu.
  8. --
  9. -- A popup menu is displayed for the user using the popup function (usually on
  10. -- the mouse position) and disappears when an item is selected.
  11. --
  12. -- destroy should be called only for popup menus. Menu bars associated with
  13. -- dialogs are automatically destroyed when the dialog is destroyed. But if you
  14. -- change the menu of a dialog for another menu, the previous one should be
  15. -- destroyed destroy. If you replace a menu bar of a dialog, the previous menu
  16. -- is unmapped.
  17. --
  18. -- Any item inside a menu bar can retrieve attributes from the dialog using
  19. -- get_attribute. It is not necessary to call get_dialog.
  20. --
  21. -- The menu can be created with no elements and be dynamic filled using append
  22. -- or insert.
  23. inherit
  24. IUP_WIDGET
  25. redefine
  26. execute_map,
  27. execute_unmap,
  28. execute_destroy,
  29. execute_open,
  30. execute_menuclose
  31. end
  32. IUP_WIDGET_BGCOLOR
  33. IUP_WIDGET_POPUP
  34. IUP_WIDGET_NAME
  35. create {ANY}
  36. menu_empty,
  37. menu
  38. feature {ANY}
  39. menu_empty
  40. -- Create an empty menu
  41. local
  42. p, a_menu: POINTER
  43. do
  44. a_menu := int_empty_menu (p)
  45. set_widget(a_menu)
  46. end
  47. menu (col: ARRAY[IUP_MENU_ELEMENT])
  48. -- Create a new menu containing the list of item
  49. local
  50. iterator: ITERATOR[IUP_MENU_ELEMENT]; i: INTEGER; arg: NATIVE_ARRAY[POINTER]; s: IUP_WIDGET; a_menu: POINTER
  51. do
  52. i := col.count
  53. arg := arg.calloc(i)
  54. iterator := col.new_iterator
  55. i := 0
  56. from
  57. iterator.start
  58. until
  59. iterator.is_off
  60. loop
  61. s := iterator.item
  62. arg.put(s.widget, i)
  63. iterator.next
  64. i := i + 1
  65. end
  66. a_menu := int_menu (arg.to_external)
  67. set_widget(a_menu)
  68. end
  69. -- Attributes
  70. set_radio (state: BOOLEAN)
  71. -- (non inheritable): enables the automatic toggle of one child item.
  72. -- When a child item is selected the other item is automatically
  73. -- deselected. The menu acts like a IUP_RADIO for its children. Submenus
  74. -- and their children are not affected.
  75. do
  76. iup_open.set_attribute(Current, "RADIO", boolean_to_yesno(state))
  77. end
  78. -- Commands to handle heirarchy
  79. append (new_child: IUP_WIDGET): IUP_WIDGET
  80. -- Inserts an interface element at the end of the container, after
  81. -- the last element of the container. Valid for any element that
  82. -- contains other elements like dialog, frame, hbox, vbox, zbox or menu.
  83. -- Returns: the actual parent if the interface element was
  84. -- successfully inserted. Otherwise returns Void.
  85. do
  86. Result := iup_open.iup_append(Current, new_child)
  87. end
  88. insert (ref_child: IUP_WIDGET; new_child: IUP_WIDGET): IUP_WIDGET
  89. -- Inserts an interface element before another child of the
  90. -- container. Valid for any element that contains other elements
  91. -- like dialog, frame, hbox, vbox, zbox, menu, etc.
  92. -- Returns: the actual parent if the interface element was
  93. -- successfully inserted. Otherwise returns NULL
  94. do
  95. Result := iup_open.iup_insert(Current, ref_child, new_child)
  96. end
  97. -- PopUp
  98. popup_mouse_position: STRING
  99. -- Shows the menu at mouse position and restricts user interaction only
  100. -- to the specified element. If there was an error returns IUP_ERROR.
  101. do
  102. Result := iup_open.popup_predefined_xy(Current, "IUP_MOUSEPOS", "IUP_MOUSEPOS")
  103. end
  104. -- Callbacks
  105. -- Common
  106. set_cb_map (act: FUNCTION[TUPLE[IUP_MENU], STRING])
  107. -- Called right after an element is mapped and its attributes updated.
  108. local
  109. operation: INTEGER
  110. do
  111. cb_map := act
  112. if cb_map /= Void then
  113. operation := 1
  114. else
  115. operation := 0
  116. end
  117. iup_open.set_callback (Current, "MAP_CB", "NONEEDED", operation)
  118. end
  119. set_cb_unmap (act: FUNCTION[TUPLE[IUP_MENU], STRING])
  120. -- Called right before an element is unmapped.
  121. local
  122. operation: INTEGER
  123. do
  124. cb_unmap := act
  125. if cb_unmap /= Void then
  126. operation := 1
  127. else
  128. operation := 0
  129. end
  130. iup_open.set_callback (Current, "UNMAP_CB", "NONEEDED", operation)
  131. end
  132. set_cb_destroy (act: FUNCTION[TUPLE[IUP_MENU], STRING])
  133. -- Called right before an element is destroyed.
  134. local
  135. operation: INTEGER
  136. do
  137. cb_destroy := act
  138. if cb_destroy /= Void then
  139. operation := 1
  140. else
  141. operation := 0
  142. end
  143. iup_open.set_callback (Current, "DESTROY_CB", "NONEEDED", operation)
  144. end
  145. set_cb_open (act: FUNCTION[TUPLE[IUP_MENU], STRING])
  146. -- Called just before the menu is opened.
  147. local
  148. operation: INTEGER
  149. do
  150. cb_open := act
  151. if cb_open /= Void then
  152. operation := 1
  153. else
  154. operation := 0
  155. end
  156. iup_open.set_callback (Current, "OPEN_CB", "NONEEDED", operation)
  157. end
  158. set_cb_menu_close (act: FUNCTION[TUPLE[IUP_MENU], STRING])
  159. -- Called just after the menu is closed.
  160. local
  161. operation: INTEGER
  162. do
  163. cb_menuclose := act
  164. if cb_menuclose /= Void then
  165. operation := 1
  166. else
  167. operation := 0
  168. end
  169. iup_open.set_callback (Current, "MENUCLOSE_CB", "NONEEDED", operation)
  170. end
  171. feature {IUP}
  172. -- Common callbacks
  173. execute_map: STRING
  174. do
  175. Result := cb_map.item([Current])
  176. end
  177. execute_unmap: STRING
  178. do
  179. Result := cb_unmap.item([Current])
  180. end
  181. execute_destroy: STRING
  182. do
  183. Result := cb_destroy.item([Current])
  184. end
  185. -- Extra
  186. execute_open: STRING
  187. do
  188. Result := cb_open.item([Current])
  189. end
  190. execute_menuclose: STRING
  191. do
  192. Result := cb_menuclose.item([Current])
  193. end
  194. feature {}
  195. -- For callbacks
  196. cb_map: FUNCTION[TUPLE[IUP_MENU], STRING]
  197. cb_unmap: FUNCTION[TUPLE[IUP_MENU], STRING]
  198. cb_destroy: FUNCTION[TUPLE[IUP_MENU], STRING]
  199. cb_open: FUNCTION[TUPLE[IUP_MENU], STRING]
  200. cb_menuclose: FUNCTION[TUPLE[IUP_MENU], STRING]
  201. -- Internals
  202. int_empty_menu (list: POINTER): POINTER
  203. external "plug_in"
  204. alias "{
  205. location: "${sys}/plugins"
  206. module_name: "iup"
  207. feature_name: "IupMenu"
  208. }"
  209. end
  210. int_menu (list: POINTER): POINTER
  211. external "plug_in"
  212. alias "{
  213. location: "${sys}/plugins"
  214. module_name: "iup"
  215. feature_name: "IupMenuv"
  216. }"
  217. end
  218. end
  219. -- The MIT License (MIT)
  220. -- Copyright (c) 2016, 2017 by German A. Arias
  221. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  222. -- of this software and associated documentation files (the "Software"), to deal
  223. -- in the Software without restriction, including without limitation the rights
  224. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  225. -- copies of the Software, and to permit persons to whom the Software is
  226. -- furnished to do so, subject to the following conditions:
  227. --
  228. -- The above copyright notice and this permission notice shall be included in
  229. -- all copies or substantial portions of the Software.
  230. --
  231. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  232. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  233. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  234. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  235. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  236. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  237. -- SOFTWARE.