iup_flat_button.e 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. class IUP_FLAT_BUTTON
  2. -- Creates an interface element that is a button, but it does not have native
  3. -- decorations. When selected, this element activates a function in the
  4. -- application. Its visual presentation can contain a text and/or an image.
  5. --
  6. -- It behaves just like an IUP_BUTTON, but since it is not a native control it
  7. -- has more flexibility for additional options. It can also behave like an
  8. -- IUP_TOGGLE (without the checkmark).
  9. --
  10. -- It inherits from IUP_CANVAS. Inherits all callbacks of the IUP_CANVAS, but
  11. -- redefines a few of them. Including ACTION, BUTTON_CB, FOCUS_CB,
  12. -- LEAVEWINDOW_CB, and ENTERWINDOW_CB. To allow the application to use those
  13. -- callbacks the same callbacks are exported with the "FLAT_" prefix using the
  14. -- same parameters, except the FLAT_ACTION callback that now mimics the
  15. -- IUP_BUTTON ACTION. They are all called before the internal callbacks and if
  16. -- they return IUP_IGNORE the internal callbacks are not processed.
  17. inherit
  18. IUP_CANVAS
  19. redefine
  20. set_border,
  21. execute_flat_action,
  22. execute_flat_button,
  23. execute_flat_focus,
  24. execute_flat_enterwindow,
  25. execute_flat_leavewindow,
  26. execute_valuechanged
  27. end
  28. IUP_WIDGET_FGCOLOR
  29. IUP_WIDGET_SPACING
  30. IUP_WIDGET_TITLE
  31. IUP_WIDGET_IGNORERADIO
  32. IUP_WIDGET_PADDING
  33. IUP_WIDGET_BACK_IMAGE_2
  34. IUP_WIDGET_BORDER
  35. IUP_WIDGET_FRONT_IMAGE_2
  36. IUP_WIDGET_IMAGE_2
  37. IUP_WIDGET_FLAT_TEXT
  38. redefine
  39. set_alignment
  40. end
  41. IUP_WIDGET_HIGHLIGHT
  42. IUP_WIDGET_PRESS
  43. IUP_WIDGET_FOCUSFEEDBACK
  44. create {ANY}
  45. flat_button
  46. feature {ANY}
  47. flat_button (title: STRING)
  48. --
  49. local
  50. a_flat_button, p: POINTER
  51. do
  52. if title /= Void then
  53. a_flat_button := int_flat_button(get_pointer(title.to_c))
  54. else
  55. a_flat_button := int_flat_button(p)
  56. end
  57. set_widget(a_flat_button)
  58. end
  59. -- Attributes
  60. set_alignment (horizontal, vertical: STRING)
  61. -- (non inheritable): horizontal and vertical alignment. Possible values:
  62. -- "ALEFT", "ACENTER" and "ARIGHT", combined to "ATOP", "ACENTER" and
  63. -- "ABOTTOM". Default: "ACENTER:ACENTER".
  64. do
  65. Precursor (horizontal, vertical)
  66. end
  67. set_border (state: BOOLEAN)
  68. -- (creation only): Shows a border around the canvas. Default: "False".
  69. do
  70. Precursor (state)
  71. end
  72. is_radio: BOOLEAN
  73. -- returns if the toggle is inside a radio. Valid only after the element
  74. -- is mapped and TOGGLE=True.
  75. local
  76. str: STRING
  77. do
  78. str := iup_open.get_attribute(Current, "RADIO")
  79. if is_yes_no(str) then
  80. Result := yesno_to_boolean(str)
  81. else
  82. Result := False
  83. end
  84. end
  85. set_toggle (state: BOOLEAN)
  86. -- enabled the toggle behavior. Default: False.
  87. do
  88. iup_open.set_attribute(Current, "TOGGLE", boolean_to_yesno(state))
  89. end
  90. get_value: STRING
  91. -- Toggle's state. Values can be "ON", "OFF" or "TOGGLE".
  92. do
  93. Result := iup_open.get_attribute(Current, "VALUE")
  94. end
  95. -- Operations
  96. set_on
  97. do
  98. iup_open.set_attribute(Current, "VALUE", "ON")
  99. end
  100. set_off
  101. do
  102. iup_open.set_attribute(Current, "VALUE", "OFF")
  103. end
  104. invert
  105. -- Invert the current state.
  106. do
  107. iup_open.set_attribute(Current, "VALUE", "TOGGLE")
  108. end
  109. -- Extra callbacks
  110. set_cb_flat_action (act: detachable FUNCTION[TUPLE[IUP_FLAT_BUTTON], STRING])
  111. -- Action generated when the button 1 (usually left) is selected. This
  112. -- callback is called only after the mouse is released and when it is
  113. -- released inside the button area.
  114. --
  115. -- ih: identifier of the element that activated the event.
  116. --
  117. -- Returns: IUP_CLOSE will be processed.
  118. local
  119. operation: INTEGER
  120. do
  121. cb_flat_action := act
  122. if cb_flat_action /= Void then
  123. operation := 1
  124. else
  125. operation := 0
  126. end
  127. iup_open.set_callback (Current, "FLAT_ACTION", "Fnff", operation)
  128. end
  129. set_cb_flat_button (act: detachable FUNCTION[TUPLE[IUP_FLAT_BUTTON, INTEGER, INTEGER, INTEGER, INTEGER, STRING], STRING])
  130. -- Action generated when any mouse button is pressed and when it is
  131. -- released. Both calls occur before the ACTION callback when button 1 is
  132. -- being used.
  133. -- IUP_CANVAS: identifies the element that activated the event.
  134. -- button: identifies the activated mouse button:
  135. --
  136. -- 1 - left mouse button (button 1);
  137. -- 2 - middle mouse button (button 2);
  138. -- 3 - right mouse button (button 3).
  139. --
  140. -- pressed: indicates the state of the button:
  141. --
  142. -- 0 - mouse button was released;
  143. -- 1 - mouse button was pressed.
  144. --
  145. -- x, y: position in the canvas where the event has occurred, in pixels.
  146. --
  147. -- status: status of the mouse buttons and some keyboard keys at the
  148. -- moment the event is generated. The following macros must be used for
  149. -- verification:
  150. --
  151. -- Returns: IUP_CLOSE will be processed. On some controls if IUP_IGNORE
  152. -- is returned the action is ignored (this is system dependent).
  153. local
  154. operation: INTEGER
  155. do
  156. cb_flat_button := act
  157. if cb_flat_button /= Void then
  158. operation := 1
  159. else
  160. operation := 0
  161. end
  162. iup_open.set_callback (Current, "FLAT_BUTTON_CB", "NONEEDED", operation)
  163. end
  164. set_cb_flat_focus (act: detachable FUNCTION[TUPLE[IUP_FLAT_BUTTON, INTEGER], STRING])
  165. -- Called when the canvas gets or looses the focus. It is called after
  166. -- the common callbacks GETFOCUS_CB and KILL_FOCUS_CB.
  167. -- ih: identifier of the element that activated the event.
  168. -- focus: is non zero if the canvas is getting the focus, is zero if it
  169. -- is loosing the focus.
  170. local
  171. operation: INTEGER
  172. do
  173. cb_flat_focus := act
  174. if cb_focus /= Void then
  175. operation := 1
  176. else
  177. operation := 0
  178. end
  179. iup_open.set_callback (Current, "FLAT_FOCUS_CB", "NONEEDED", operation)
  180. end
  181. set_cb_flat_enter_window (act: detachable FUNCTION[TUPLE[IUP_FLAT_BUTTON], STRING])
  182. -- Action generated when the mouse enters the native element.
  183. local
  184. operation: INTEGER
  185. do
  186. cb_flat_enterwindow := act
  187. if cb_enterwindow /= Void then
  188. operation := 1
  189. else
  190. operation := 0
  191. end
  192. iup_open.set_callback (Current, "FLAT_ENTERWINDOW_CB", "NONEEDED", operation)
  193. end
  194. set_cb_flat_leave_window (act: detachable FUNCTION[TUPLE[IUP_FLAT_BUTTON], STRING])
  195. -- Action generated when the mouse leaves the native element.
  196. local
  197. operation: INTEGER
  198. do
  199. cb_flat_leavewindow := act
  200. if cb_leavewindow /= Void then
  201. operation := 1
  202. else
  203. operation := 0
  204. end
  205. iup_open.set_callback (Current, "FLAT_LEAVEWINDOW_CB", "NONEEDED", operation)
  206. end
  207. set_cb_value_changed (act: detachable FUNCTION[TUPLE[IUP_FLAT_BUTTON], STRING])
  208. -- Called after the value was interactively changed by the
  209. -- user. Called after the ACTION callback, but under the same context.
  210. local
  211. operation: INTEGER
  212. do
  213. cb_valuechanged := act
  214. if cb_valuechanged /= Void then
  215. operation := 1
  216. else
  217. operation := 0
  218. end
  219. iup_open.set_callback (Current, "VALUECHANGED_CB", "NONEEDED", operation)
  220. end
  221. feature {IUP}
  222. -- Callbacks
  223. execute_flat_action: STRING
  224. do
  225. if attached cb_flat_action as int_cb then
  226. Result := int_cb.item([Current])
  227. else
  228. Result := "IUP_DEFAULT"
  229. end
  230. end
  231. execute_flat_button (btn, pressed, x, y: INTEGER; status: STRING): STRING
  232. do
  233. if attached cb_flat_button as int_cb then
  234. Result := int_cb.item([Current, btn, pressed, x, y, status])
  235. else
  236. Result := "IUP_DEFAULT"
  237. end
  238. end
  239. execute_flat_focus (focus: INTEGER): STRING
  240. do
  241. if attached cb_flat_focus as int_cb then
  242. Result := int_cb.item([Current, focus])
  243. else
  244. Result := "IUP_DEFAULT"
  245. end
  246. end
  247. execute_flat_enterwindow: STRING
  248. do
  249. if attached cb_flat_enterwindow as int_cb then
  250. Result := int_cb.item([Current])
  251. else
  252. Result := "IUP_DEFAULT"
  253. end
  254. end
  255. execute_flat_leavewindow: STRING
  256. do
  257. if attached cb_flat_leavewindow as int_cb then
  258. Result := int_cb.item([Current])
  259. else
  260. Result := "IUP_DEFAULT"
  261. end
  262. end
  263. execute_valuechanged: STRING
  264. do
  265. if attached cb_valuechanged as int_cb then
  266. Result := int_cb.item([Current])
  267. else
  268. Result := "IUP_DEFAULT"
  269. end
  270. end
  271. feature {NONE}
  272. cb_flat_action: detachable FUNCTION[TUPLE[IUP_FLAT_BUTTON], STRING]
  273. cb_flat_button: detachable FUNCTION[TUPLE[IUP_FLAT_BUTTON, INTEGER, INTEGER, INTEGER, INTEGER, STRING], STRING]
  274. cb_flat_focus: detachable FUNCTION[TUPLE[IUP_FLAT_BUTTON, INTEGER], STRING]
  275. cb_flat_enterwindow: detachable FUNCTION[TUPLE[IUP_FLAT_BUTTON], STRING]
  276. cb_flat_leavewindow: detachable FUNCTION[TUPLE[IUP_FLAT_BUTTON], STRING]
  277. cb_valuechanged: detachable FUNCTION[TUPLE[IUP_FLAT_BUTTON], STRING]
  278. -- Internals
  279. int_flat_button (title: POINTER): POINTER
  280. external
  281. "C inline use %"eiffel-iup.h%""
  282. alias
  283. "return IupFlatButton ($title);"
  284. end
  285. end -- class IUP_FLAT_BUTTON
  286. -- The MIT License (MIT)
  287. -- Copyright (c) 2016, 2017, 2018, 2019, 2020 by German A. Arias
  288. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  289. -- of this software and associated documentation files (the "Software"), to deal
  290. -- in the Software without restriction, including without limitation the rights
  291. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  292. -- copies of the Software, and to permit persons to whom the Software is
  293. -- furnished to do so, subject to the following conditions:
  294. --
  295. -- The above copyright notice and this permission notice shall be included in
  296. -- all copies or substantial portions of the Software.
  297. --
  298. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  299. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  300. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  301. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  302. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  303. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  304. -- SOFTWARE.