iup_button.e 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. class IUP_BUTTON
  2. -- Creates an interface element that is a button. When selected, this element
  3. -- activates a function in the application. Its visual presentation can contain
  4. -- a text and/or an image.
  5. --
  6. -- Buttons with images and/or texts can not change its behavior after mapped.
  7. -- This is a creation dependency. But after creation the image can be changed
  8. -- for another image, and the text for another text.
  9. --
  10. -- Buttons are activated using Enter or Space keys.
  11. --
  12. -- Buttons are not activated if the user clicks inside the button but moves the
  13. -- cursor and releases outside the button area. Also in Windows the highlight
  14. -- feedback when that happens is different if the button has CANFOCUS enabled or
  15. -- not.
  16. --
  17. -- Usually toolbar buttons have FLAT=True and CANFOCUS=False.
  18. inherit
  19. IUP_WIDGET
  20. redefine
  21. execute_map,
  22. execute_unmap,
  23. execute_destroy,
  24. execute_getfocus,
  25. execute_killfocus,
  26. execute_enterwindow,
  27. execute_leavewindow,
  28. execute_k_any,
  29. execute_help,
  30. execute_action,
  31. execute_button
  32. end
  33. IUP_WIDGET_BGCOLOR
  34. IUP_WIDGET_FGCOLOR
  35. IUP_WIDGET_TITLE
  36. IUP_WIDGET_ACTIVE
  37. IUP_WIDGET_FONT
  38. IUP_WIDGET_EXPAND
  39. IUP_WIDGET_SCREENPOSITION
  40. IUP_WIDGET_POSITION
  41. IUP_WIDGET_MAXMIN_SIZE
  42. IUP_WIDGET_TIP
  43. IUP_WIDGET_SIZE
  44. IUP_WIDGET_RASTERSIZE
  45. IUP_WIDGET_USERSIZE
  46. IUP_WIDGET_ZORDER
  47. IUP_WIDGET_VISIBLE
  48. IUP_WIDGET_CHILD
  49. IUP_WIDGET_NAME
  50. IUP_WIDGET_FOCUS
  51. IUP_WIDGET_PROPAGATEFOCUS
  52. IUP_WIDGET_CUSTOM_ATTRIBUTES
  53. IUP_WIDGET_PADDING
  54. insert
  55. IUP_WIDGET_IMPRESS_BORDER
  56. IUP_WIDGET_IMAGE_POSITION
  57. IUP_WIDGET_SPACING
  58. IUP_WIDGET_CB_ACTION
  59. IUP_WIDGET_CB_BUTTON
  60. create {ANY}
  61. button
  62. create {IUP_GET_PARAM}
  63. button_widget
  64. feature {ANY}
  65. button (title: STRING)
  66. -- New button with the specified title.
  67. local
  68. a, b, a_button: POINTER
  69. do
  70. if title /= Void then
  71. a := title.to_external
  72. end
  73. a_button := int_button (a, b)
  74. set_widget(a_button)
  75. end
  76. -- Attributes
  77. set_alignment (horizontal, vertical: STRING)
  78. -- (non inheritable): horizontal and vertical alignment. Possible values:
  79. -- "ALEFT", "ACENTER" and "ARIGHT", combined to "ATOP", "ACENTER" and
  80. -- "ABOTTOM". Default: "ACENTER:ACENTER". In Motif, vertical alignment is
  81. -- restricted to "ACENTER". In GTK, horizontal alignment for multiple
  82. -- lines will align only the text block.
  83. require
  84. is_valid_alignment(horizontal, vertical)
  85. local
  86. str: STRING
  87. do
  88. create str.copy(horizontal)
  89. str.append_string(":")
  90. str.append_string(vertical)
  91. iup_open.set_attribute(Current, "ALIGNMENT", str)
  92. end
  93. set_can_focus (state: BOOLEAN)
  94. -- (creation only) (non inheritable): enables the focus traversal of the
  95. -- control. In Windows the button will respect CANFOCUS differently to
  96. -- some other controls. Default: True.
  97. do
  98. iup_open.set_attribute(Current, "CANFOCUS", boolean_to_yesno(state))
  99. end
  100. set_flat (state: BOOLEAN)
  101. -- (creation only): Hides the button borders until the mouse cursor
  102. -- enters the button area. Can be True or False. Default: False.
  103. do
  104. iup_open.set_attribute(Current, "FLAT", boolean_to_yesno(state))
  105. end
  106. set_image (name: STRING)
  107. -- (non inheritable): Image name. If set before map defines the behavior
  108. -- of the button to contain an image. The natural size will be size of
  109. -- the image in pixels, plus the button borders. Use set_attribute_handle
  110. -- to associate an image to a name. See also IUP_IMAGE. If TITLE is also
  111. -- defined and not empty both will be shown (except in Motif).
  112. do
  113. iup_open.set_attribute(Current, "IMAGE", name)
  114. end
  115. set_image_inactive (name: STRING)
  116. -- (non inheritable): Image name of the element when inactive. If it is
  117. -- not defined then the IMAGE is used and the colors will be replaced by
  118. -- a modified version of the background color creating the disabled
  119. -- effect. GTK will also change the inactive image to look like other
  120. -- inactive objects.
  121. do
  122. iup_open.set_attribute(Current, "IMINACTIVE", name)
  123. end
  124. set_image_press (name: STRING)
  125. -- (non inheritable): Image name of the pressed button. If IMPRESS and
  126. -- IMAGE are defined, the button borders are not shown and not computed
  127. -- in natural size. When the button is clicked the pressed image does not
  128. -- offset. In Motif the button will lose its focus feedback also.
  129. do
  130. iup_open.set_attribute(Current, "IMPRESS", name)
  131. end
  132. set_markup (state: BOOLEAN)
  133. -- [GTK only]: allows the title string to contains pango markup
  134. -- commands. Works only if a mnemonic is NOT defined in the title. Can
  135. -- be "True" or "False". Default: "False".
  136. do
  137. iup_open.set_attribute(Current, "MARKUP", boolean_to_yesno(state))
  138. end
  139. -- For callbacks
  140. -- Common
  141. set_cb_map (act: FUNCTION[TUPLE[IUP_BUTTON], STRING])
  142. -- Called right after an element is mapped and its attributes updated.
  143. local
  144. operation: INTEGER
  145. do
  146. cb_map := act
  147. if cb_map /= Void then
  148. operation := 1
  149. else
  150. operation := 0
  151. end
  152. iup_open.set_callback (Current, "MAP_CB", "NONEEDED", operation)
  153. end
  154. set_cb_unmap (act: FUNCTION[TUPLE[IUP_BUTTON], STRING])
  155. -- Called right before an element is unmapped.
  156. local
  157. operation: INTEGER
  158. do
  159. cb_unmap := act
  160. if cb_unmap /= Void then
  161. operation := 1
  162. else
  163. operation := 0
  164. end
  165. iup_open.set_callback (Current, "UNMAP_CB", "NONEEDED", operation)
  166. end
  167. set_cb_destroy (act: FUNCTION[TUPLE[IUP_BUTTON], STRING])
  168. -- Called right before an element is destroyed.
  169. local
  170. operation: INTEGER
  171. do
  172. cb_destroy := act
  173. if cb_destroy /= Void then
  174. operation := 1
  175. else
  176. operation := 0
  177. end
  178. iup_open.set_callback (Current, "DESTROY_CB", "NONEEDED", operation)
  179. end
  180. set_cb_get_focus (act: FUNCTION[TUPLE[IUP_BUTTON], STRING])
  181. -- Action generated when an element is given keyboard focus.
  182. -- This callback is called after the KILLFOCUS_CB of the element
  183. -- that loosed the focus. The {IUP}.get_focus function during the
  184. -- callback returns the element that loosed the focus.
  185. local
  186. operation: INTEGER
  187. do
  188. cb_getfocus := act
  189. if cb_getfocus /= Void then
  190. operation := 1
  191. else
  192. operation := 0
  193. end
  194. iup_open.set_callback (Current, "GETFOCUS_CB", "NONEEDED", operation)
  195. end
  196. set_cb_kill_focus (act: FUNCTION[TUPLE[IUP_BUTTON], STRING])
  197. -- Action generated when an element loses keyboard focus. This
  198. -- callback is called before the GETFOCUS_CB of the element that
  199. -- gets the focus.
  200. local
  201. operation: INTEGER
  202. do
  203. cb_killfocus := act
  204. if cb_killfocus /= Void then
  205. operation := 1
  206. else
  207. operation := 0
  208. end
  209. iup_open.set_callback (Current, "KILLFOCUS_CB", "NONEEDED", operation)
  210. end
  211. set_cb_enter_window (act: FUNCTION[TUPLE[IUP_BUTTON], STRING])
  212. -- Action generated when the mouse enters the native element.
  213. local
  214. operation: INTEGER
  215. do
  216. cb_enterwindow := act
  217. if cb_enterwindow /= Void then
  218. operation := 1
  219. else
  220. operation := 0
  221. end
  222. iup_open.set_callback (Current, "ENTERWINDOW_CB", "NONEEDED", operation)
  223. end
  224. set_cb_leave_window (act: FUNCTION[TUPLE[IUP_BUTTON], STRING])
  225. -- Action generated when the mouse leaves the native element.
  226. local
  227. operation: INTEGER
  228. do
  229. cb_leavewindow := act
  230. if cb_leavewindow /= Void then
  231. operation := 1
  232. else
  233. operation := 0
  234. end
  235. iup_open.set_callback (Current, "LEAVEWINDOW_CB", "NONEEDED", operation)
  236. end
  237. set_cb_k_any (act: FUNCTION[TUPLE[IUP_BUTTON, INTEGER], STRING])
  238. -- Action generated when a keyboard event occurs.
  239. -- IUP_WIDGET the element that activated the event.
  240. -- INTEGER identifier of typed key. Please refer to the Keyboard
  241. -- Codes table for a list of possible values.
  242. --
  243. -- Returns: If IUP_IGNORE is returned the key is ignored and not
  244. -- processed by the control and not propagated. If returns
  245. -- IUP_CONTINUE, the key will be processed and the event will be
  246. -- propagated to the parent of the element receiving it, this is
  247. -- the default behavior. If returns IUP_DEFAULT the key is processed
  248. -- but it is not propagated. IUP_CLOSE will be processed.
  249. local
  250. operation: INTEGER
  251. do
  252. cb_k_any := act
  253. if cb_k_any /= Void then
  254. operation := 1
  255. else
  256. operation := 0
  257. end
  258. iup_open.set_callback (Current, "K_ANY", "NONEEDED", operation)
  259. end
  260. set_cb_help (act: PROCEDURE[TUPLE[IUP_BUTTON]])
  261. -- Action generated when the user press F1 at a control. In Motif
  262. -- is also activated by the Help button in some workstations
  263. -- keyboard.
  264. -- Returns: IUP_CLOSE will be processed.
  265. local
  266. operation: INTEGER
  267. do
  268. cb_help := act
  269. if cb_help /= Void then
  270. operation := 1
  271. else
  272. operation := 0
  273. end
  274. iup_open.set_callback (Current, "HELP_CB", "NONEEDED", operation)
  275. end
  276. feature {IUP}
  277. -- Common callbacks
  278. execute_map: STRING
  279. do
  280. Result := cb_map.item([Current])
  281. end
  282. execute_unmap: STRING
  283. do
  284. Result := cb_unmap.item([Current])
  285. end
  286. execute_destroy: STRING
  287. do
  288. Result := cb_destroy.item([Current])
  289. end
  290. execute_getfocus: STRING
  291. do
  292. Result := cb_getfocus.item([Current])
  293. end
  294. execute_killfocus: STRING
  295. do
  296. Result := cb_getfocus.item([Current])
  297. end
  298. execute_enterwindow: STRING
  299. do
  300. Result := cb_enterwindow.item([Current])
  301. end
  302. execute_leavewindow: STRING
  303. do
  304. Result := cb_leavewindow.item([Current])
  305. end
  306. execute_k_any (c: INTEGER): STRING
  307. do
  308. Result := cb_k_any.item([Current, c])
  309. end
  310. execute_help
  311. do
  312. cb_help.call([Current])
  313. end
  314. execute_action: STRING
  315. do
  316. Result := cb_action.item([Current])
  317. end
  318. -- Extra
  319. execute_button (btn, pressed, x, y: INTEGER; status: STRING): STRING
  320. do
  321. Result := cb_button.item([Current, btn, pressed, x, y, status])
  322. end
  323. feature {IUP_GET_PARAM}
  324. button_widget (a_button: POINTER)
  325. do
  326. set_widget(a_button)
  327. end
  328. feature {}
  329. -- For callbacks
  330. cb_map: FUNCTION[TUPLE[IUP_BUTTON], STRING]
  331. cb_unmap: FUNCTION[TUPLE[IUP_BUTTON], STRING]
  332. cb_destroy: FUNCTION[TUPLE[IUP_BUTTON], STRING]
  333. cb_getfocus: FUNCTION[TUPLE[IUP_BUTTON], STRING]
  334. cb_killfocus: FUNCTION[TUPLE[IUP_BUTTON], STRING]
  335. cb_enterwindow: FUNCTION[TUPLE[IUP_BUTTON], STRING]
  336. cb_leavewindow: FUNCTION[TUPLE[IUP_BUTTON], STRING]
  337. cb_k_any: FUNCTION[TUPLE[IUP_BUTTON, INTEGER], STRING]
  338. cb_help: PROCEDURE[TUPLE[IUP_BUTTON]]
  339. -- Internals
  340. int_button (title: POINTER; action: POINTER): POINTER
  341. external "plug_in"
  342. alias "{
  343. location: "${sys}/plugins"
  344. module_name: "iup"
  345. feature_name: "IupButton"
  346. }"
  347. end
  348. -- Validations
  349. is_valid_alignment (horizontal, vertical: STRING): BOOLEAN
  350. local
  351. h, v: BOOLEAN
  352. do
  353. if horizontal.is_equal("ALEFT") or
  354. horizontal.is_equal("ACENTER") or
  355. horizontal.is_equal("ARIGHT") then
  356. h := True
  357. else
  358. h := False
  359. end
  360. if vertical.is_equal("ATOP") or
  361. vertical.is_equal("ACENTER") or
  362. vertical.is_equal("ABOTTOM") then
  363. v := True
  364. else
  365. v := False
  366. end
  367. if h and v then
  368. Result := True
  369. else
  370. Result := False
  371. end
  372. end
  373. end -- end IUP_BUTTON
  374. -- The MIT License (MIT)
  375. -- Copyright (c) 2016, 2017, 2019 by German A. Arias
  376. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  377. -- of this software and associated documentation files (the "Software"), to deal
  378. -- in the Software without restriction, including without limitation the rights
  379. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  380. -- copies of the Software, and to permit persons to whom the Software is
  381. -- furnished to do so, subject to the following conditions:
  382. --
  383. -- The above copyright notice and this permission notice shall be included in
  384. -- all copies or substantial portions of the Software.
  385. --
  386. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  387. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  388. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  389. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  390. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  391. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  392. -- SOFTWARE.