iup_menu_item.e 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. class IUP_MENU_ITEM
  2. -- Creates an item of the menu interface element. When selected, it generates
  3. -- an action.
  4. -- Menu items are activated using the Enter key.
  5. --
  6. -- In Motif and GTK, the text font will be affected by the dialog font when the
  7. -- menu is mapped.
  8. --
  9. -- Since GTK 2.14 to have a menu item that can be marked you must set the VALUE
  10. -- attribute to ON or OFF, or set HIDEMARK=False, before mapping the control.
  11. inherit
  12. IUP_MENU_ELEMENT
  13. redefine
  14. execute_map,
  15. execute_unmap,
  16. execute_destroy,
  17. execute_help,
  18. execute_action,
  19. execute_highlight
  20. end
  21. IUP_WIDGET_TITLE
  22. IUP_WIDGET_ACTIVE
  23. IUP_WIDGET_NAME
  24. create {ANY}
  25. item
  26. feature {ANY}
  27. item (title: STRING)
  28. -- A new item.
  29. -- title: Text to be shown on the item. It can be Void. The "&" character
  30. -- can be used to define a mnemonic, the next character will be used as
  31. -- key. Use "&&" to show the "&" character instead on defining a
  32. -- mnemonic. When in a menu bar an item that has a mnemonic can be
  33. -- activated from any control in the dialog using the "Alt+key"
  34. -- combination.
  35. --
  36. -- The text also accepts the control character '%T' to force text
  37. -- alignment to the right after this character. This is used to add
  38. -- shortcut keys to the menu, aligned to the right, ex: "Save%TCtrl+S",
  39. -- but notice that the shortcut key (also known as Accelerator or Hot
  40. -- Key) still has to be implemented. To implement a shortcut use the K_*
  41. -- callbacks in the dialog.
  42. local
  43. a_item, a, p: POINTER
  44. do
  45. if title /= Void then
  46. a := title.to_external
  47. end
  48. a_item := int_item (a, p)
  49. set_widget(a_item)
  50. end
  51. -- Attibutes
  52. set_auto_toggle (state: BOOLEAN)
  53. -- (non inheritable): enables the automatic toggle of VALUE state when
  54. -- the item is activated. Default: False.
  55. do
  56. iup_open.set_attribute(Current, "AUTOTOGGLE", boolean_to_yesno(state))
  57. end
  58. set_hide_mark (state: BOOLEAN)
  59. -- [Motif and GTK Only]: If enabled the item cannot be checked, since the
  60. -- check box will not be shown. If all items in a menu enable it, then no
  61. -- empty space will be shown in front of the items. Normally the unmarked
  62. -- check box will not be shown, but since GTK 2.14 the unmarked check box
  63. -- is always shown. If your item will not be marked you must set
  64. -- HIDEMARK=True, since this is the most common case we changed the
  65. -- default value to True for this version of GTK, but if VALUE is defined
  66. -- the default goes back to False. Default: False.
  67. do
  68. iup_open.set_attribute(Current, "HIDEMARK", boolean_to_yesno(state))
  69. end
  70. set_image (name: STRING)
  71. -- [Windows and GTK Only] (non inheritable): Image name of the check mark
  72. -- image when VALUE=OFF. In Windows, an item in a menu bar cannot have a
  73. -- check mark. Ignored if item in a menu bar. A recommended size would be
  74. -- 16x16 to fit the image in the menu item. In Windows, if larger than
  75. -- the check mark area it will be cropped.
  76. do
  77. iup_open.set_attribute(Current, "IMAGE", name)
  78. end
  79. set_image_press (name: STRING)
  80. -- [Windows and GTK Only] (non inheritable): Image name of the check mark
  81. -- image when VALUE=ON.
  82. do
  83. iup_open.set_attribute(Current, "IMPRESS", name)
  84. end
  85. set_title_image (name: STRING)
  86. -- (non inheritable): Image name of the title image. In Windows, it
  87. -- appears before of the title text and after the check mark area (so
  88. -- both title and title image can be visible). In Motif, it must be at
  89. -- least defined during map, it replaces the text, and only images will
  90. -- be possible to set (TITLE will be hidden). In GTK, it will appear on
  91. -- the check mark area.
  92. do
  93. iup_open.set_attribute(Current, "TITLEIMAGE", name)
  94. end
  95. set_value (value: BOOLEAN)
  96. -- (non inheritable): Indicates the item's state. When the value is ON, a
  97. -- mark will be displayed to the left of the item. Default: OFF. An item
  98. -- in a menu bar cannot have a check mark. When IMAGE is used, the
  99. -- checkmark is not shown. See the item AUTOTOGGLE attribute and the menu
  100. -- RADIO attribute.
  101. do
  102. if value then
  103. iup_open.set_attribute(Current, "VALUE", "ON")
  104. else
  105. iup_open.set_attribute(Current, "VALUE", "OFF")
  106. end
  107. end
  108. get_value: BOOLEAN
  109. -- The value.
  110. local
  111. str: STRING
  112. do
  113. str := iup_open.get_attribute(Current, "VALUE")
  114. if str.is_equal("ON") then
  115. Result := True
  116. else
  117. Result := False
  118. end
  119. end
  120. -- Callbacks
  121. -- Common
  122. set_cb_map (act: FUNCTION[TUPLE[IUP_MENU_ITEM], STRING])
  123. -- Called right after an element is mapped and its attributes updated.
  124. local
  125. operation: INTEGER
  126. do
  127. cb_map := act
  128. if cb_map /= Void then
  129. operation := 1
  130. else
  131. operation := 0
  132. end
  133. iup_open.set_callback (Current, "MAP_CB", "NONEEDED", operation)
  134. end
  135. set_cb_unmap (act: FUNCTION[TUPLE[IUP_MENU_ITEM], STRING])
  136. -- Called right before an element is unmapped.
  137. local
  138. operation: INTEGER
  139. do
  140. cb_unmap := act
  141. if cb_unmap /= Void then
  142. operation := 1
  143. else
  144. operation := 0
  145. end
  146. iup_open.set_callback (Current, "UNMAP_CB", "NONEEDED", operation)
  147. end
  148. set_cb_destroy (act: FUNCTION[TUPLE[IUP_MENU_ITEM], STRING])
  149. -- Called right before an element is destroyed.
  150. local
  151. operation: INTEGER
  152. do
  153. cb_destroy := act
  154. if cb_destroy /= Void then
  155. operation := 1
  156. else
  157. operation := 0
  158. end
  159. iup_open.set_callback (Current, "DESTROY_CB", "NONEEDED", operation)
  160. end
  161. set_cb_help (act: PROCEDURE[TUPLE[IUP_MENU_ITEM]])
  162. -- Action generated when the user press F1 at a control. In Motif
  163. -- is also activated by the Help button in some workstations
  164. -- keyboard.
  165. -- Returns: IUP_CLOSE will be processed.
  166. local
  167. operation: INTEGER
  168. do
  169. cb_help := act
  170. if cb_help /= Void then
  171. operation := 1
  172. else
  173. operation := 0
  174. end
  175. iup_open.set_callback (Current, "HELP_CB", "NONEEDED", operation)
  176. end
  177. set_cb_action (act: FUNCTION[TUPLE[IUP_MENU_ITEM], STRING])
  178. -- Action generated when the item is selected. IUP_CLOSE will be
  179. -- processed. Even if inside a popup menu when IUP_CLOSE is returned, the
  180. -- current popup dialog or the main loop will be closed.
  181. local
  182. operation: INTEGER
  183. do
  184. cb_action := act
  185. if cb_action /= Void then
  186. operation := 1
  187. else
  188. operation := 0
  189. end
  190. iup_open.set_callback (Current, "ACTION", "Fn", operation)
  191. end
  192. -- Extra
  193. set_cb_highlight (act: FUNCTION[TUPLE[IUP_MENU_ITEM], STRING])
  194. -- Action generated when the item is highlighted.
  195. local
  196. operation: INTEGER
  197. do
  198. cb_highlight := act
  199. if cb_highlight /= Void then
  200. operation := 1
  201. else
  202. operation := 0
  203. end
  204. iup_open.set_callback (Current, "HIGHLIGHT_CB", "NONEEDED", operation)
  205. end
  206. feature {IUP}
  207. -- Common callbacks
  208. execute_map: STRING
  209. do
  210. Result := cb_map.item([Current])
  211. end
  212. execute_unmap: STRING
  213. do
  214. Result := cb_unmap.item([Current])
  215. end
  216. execute_destroy: STRING
  217. do
  218. Result := cb_destroy.item([Current])
  219. end
  220. execute_help
  221. do
  222. cb_help.call([Current])
  223. end
  224. execute_action: STRING
  225. do
  226. Result := cb_action.item([Current])
  227. end
  228. -- Extra
  229. execute_highlight: STRING
  230. do
  231. Result := cb_highlight.item([Current])
  232. end
  233. feature {}
  234. -- For callbacks
  235. cb_map: FUNCTION[TUPLE[IUP_MENU_ITEM], STRING]
  236. cb_unmap: FUNCTION[TUPLE[IUP_MENU_ITEM], STRING]
  237. cb_destroy: FUNCTION[TUPLE[IUP_MENU_ITEM], STRING]
  238. cb_help: PROCEDURE[TUPLE[IUP_MENU_ITEM]]
  239. cb_action: FUNCTION[TUPLE[IUP_MENU_ITEM], STRING]
  240. cb_highlight: FUNCTION[TUPLE[IUP_MENU_ITEM], STRING]
  241. -- Internals
  242. int_item(title, action: POINTER): POINTER
  243. external "plug_in"
  244. alias "{
  245. location: "${sys}/plugins"
  246. module_name: "iup"
  247. feature_name: "IupItem"
  248. }"
  249. end
  250. end
  251. -- The MIT License (MIT)
  252. -- Copyright (c) 2016, 2017, 2018 by German A. Arias
  253. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  254. -- of this software and associated documentation files (the "Software"), to deal
  255. -- in the Software without restriction, including without limitation the rights
  256. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  257. -- copies of the Software, and to permit persons to whom the Software is
  258. -- furnished to do so, subject to the following conditions:
  259. --
  260. -- The above copyright notice and this permission notice shall be included in
  261. -- all copies or substantial portions of the Software.
  262. --
  263. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  264. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  265. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  266. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  267. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  268. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  269. -- SOFTWARE.