iup_button.e 13 KB

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