iup_flat_toggle.e 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. class IUP_FLAT_TOGGLE
  2. -- Creates an interface element that is a toggle, 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_TOGGLE, but since it is not a native control it
  7. -- has more flexibility for additional options.
  8. --
  9. -- It inherits from IUP_CANVAS.
  10. --
  11. -- Notes:
  12. --
  13. -- The IUP_FLAT_TOGGLE can contain the check box, text and image simultaneously.
  14. --
  15. -- The natural size will be a combination of the size of the image and the
  16. -- title, if any, plus PADDING and SPACING (if both image and title are
  17. -- present), and the check box if visible.
  18. --
  19. -- Borders are drawn only when the button is highlighted reproducing the
  20. -- behavior of the IUP_BUTTON when FLAT=True. When the check box is shown the
  21. -- borders are not shown, and the background is not highlighted.
  22. --
  23. -- Buttons are activated using Enter or Space keys.
  24. --
  25. -- To build a set of mutual exclusive toggles, insert them in a IUP_RADIO
  26. -- container. They must be inserted before creation, and their behavior can not
  27. -- be changed.
  28. --
  29. -- The toggle that is a child of an IUP_RADIO automatically receives a name
  30. -- when its is mapped into the native system.
  31. --
  32. -- When the IUP_FLAT_TOGGLE displays only a text it will look like a label, use
  33. -- set_show_border=True to force the display of the borders all the time.
  34. inherit
  35. IUP_CANVAS
  36. redefine
  37. set_border,
  38. set_can_focus,
  39. set_expand,
  40. execute_flat_action,
  41. execute_flat_button,
  42. execute_flat_focus,
  43. execute_flat_enterwindow,
  44. execute_flat_leavewindow,
  45. execute_valuechanged
  46. end
  47. IUP_WIDGET_FGCOLOR
  48. IUP_WIDGET_SPACING
  49. IUP_WIDGET_TITLE
  50. IUP_WIDGET_FLAT_TEXT
  51. redefine
  52. set_alignment
  53. end
  54. IUP_WIDGET_BACK_IMAGE_2
  55. IUP_WIDGET_FRONT_IMAGE_2
  56. IUP_WIDGET_IMAGE_2
  57. IUP_WIDGET_PADDING
  58. IUP_WIDGET_BORDER
  59. IUP_WIDGET_HAS_FOCUS
  60. IUP_WIDGET_HIGHLIGHT
  61. IUP_WIDGET_PRESS
  62. IUP_WIDGET_IGNORERADIO
  63. IUP_WIDGET_FOCUSFEEDBACK
  64. IUP_WIDGET_TOGGLE
  65. create {ANY}
  66. flat_toggle_empty,
  67. flat_toggle
  68. feature {ANY}
  69. flat_toggle_empty
  70. local
  71. a_flat_toggle, p: POINTER
  72. do
  73. a_flat_toggle := int_flat_toggle(p)
  74. set_widget(a_flat_toggle)
  75. end
  76. flat_toggle (text: STRING)
  77. local
  78. a_flat_toggle: POINTER
  79. do
  80. a_flat_toggle := int_flat_toggle(get_pointer(text.to_c))
  81. set_widget(a_flat_toggle)
  82. end
  83. -- Attributes
  84. set_alignment (horizontal, vertical: STRING)
  85. -- (non inheritable): horizontal and vertical alignment of the set
  86. -- image+text. Possible values: "ALEFT", "ACENTER" and "ARIGHT", combined
  87. -- to "ATOP", "ACENTER" and "ABOTTOM". Default: "ACENTER:ACENTER".
  88. -- Alignment does not includes the padding area.
  89. do
  90. Precursor (horizontal, vertical)
  91. end
  92. set_border (state: BOOLEAN)
  93. -- (creation only): Shows a border around the canvas. Default: "False".
  94. do
  95. Precursor (state)
  96. end
  97. set_can_focus (state: BOOLEAN)
  98. -- (creation only) (non inheritable): enables the focus traversal of the
  99. -- control. In Windows the button will respect CANFOCUS in opposite to
  100. -- the other controls. Default: True.
  101. do
  102. Precursor (state)
  103. end
  104. set_check_size (value: INTEGER)
  105. -- (non inheritable): size of the check box when visible. Default depends
  106. -- on the resolution: 16 (dpi <= 120), or 24 (dpi > 120). Set it to 0 to
  107. -- hide the check box. When the check box is shown the borders are not
  108. -- shown, and the background is not highlighted.
  109. require
  110. value > -1
  111. do
  112. iup_open.set_attribute(Current, "CHECKSIZE", value.out)
  113. end
  114. set_check_at_right (state: BOOLEAN)
  115. -- (non inheritable): place the check box at the right. Default: "False".
  116. do
  117. iup_open.set_attribute(Current, "CHECKRIGHT", boolean_to_yesno(state))
  118. end
  119. set_check_spacing (value: INTEGER)
  120. -- (non inheritable): spacing between the check box and the image+text.
  121. -- The space occupies the image+text area. Default: 5
  122. require
  123. value > -1
  124. do
  125. iup_open.set_attribute(Current, "CHECKSPACING", value.out)
  126. end
  127. set_check_alignment (value: STRING)
  128. -- (non inheritable): vertical alignment of the check box. Can be "ATOP",
  129. -- "ACENTER" and "ABOTTOM". Default: ACENTER.
  130. require
  131. is_valid_vertical_alignment(value)
  132. do
  133. iup_open.set_attribute(Current, "CHECKALIGN", value)
  134. end
  135. set_chek_image (name: STRING)
  136. -- (non inheritable): image name to be used as check box when value is
  137. -- OFF, be sure the image size is equal to CHECKSIZE-2. If this attribute
  138. -- is defined the check box is not drawn, the images will be used instead.
  139. do
  140. iup_open.set_attribute(Current, "CHECKIMAGE", name)
  141. end
  142. set_check_image_highlight (name: STRING)
  143. -- (non inheritable): check box image name of the element in highlight
  144. -- state when value os OFF. If it is not defined then the CHECKIMAGE is
  145. -- used.
  146. do
  147. iup_open.set_attribute(Current, "CHECKIMAGEHIGHLIGHT", name)
  148. end
  149. set_check_image_inactive (name: STRING)
  150. -- (non inheritable): check box image name of the element when inactive
  151. -- and value is OFF. If it is not defined then the CHECKIMAGE is used and
  152. -- its colors will be replaced by a modified version creating the
  153. -- disabled effect.
  154. do
  155. iup_open.set_attribute(Current, "CHECKIMAGEINACTIVE", name)
  156. end
  157. set_check_image_press (name: STRING)
  158. -- (non inheritable): check box image name of the element in pressed
  159. -- state when value is OFF. If it is not defined then the CHECKIMAGE is
  160. -- used.
  161. do
  162. iup_open.set_attribute(Current, "CHECKIMAGEPRESS", name)
  163. end
  164. set_check_image_on (name: STRING)
  165. -- (non inheritable): (non inheritable): image name to be used as check
  166. -- box when value is ON.
  167. do
  168. iup_open.set_attribute(Current, "CHECKIMAGEON*", name)
  169. end
  170. set_check_image_not_def (name: STRING)
  171. -- (non inheritable): (non inheritable): image name to be used as check
  172. -- box when value is NOTDEF.
  173. do
  174. iup_open.set_attribute(Current, "CHECKIMAGENOTDEF*", name)
  175. end
  176. set_expand (type: STRING)
  177. -- (non inheritable): The default value is "False".
  178. do
  179. Precursor (type)
  180. end
  181. is_radio: BOOLEAN
  182. -- (read-only): returns if the toggle is inside a radio.
  183. local
  184. str: STRING
  185. do
  186. str := iup_open.get_attribute(Current, "RADIO")
  187. Result := yesno_to_boolean(str)
  188. end
  189. set_selected_notify (state: BOOLEAN)
  190. -- For a toggle inside a radio notify the selected toggle when pressed
  191. -- again. Default False.
  192. do
  193. iup_open.set_attribute(Current, "SELECTEDNOTIFY", boolean_to_yesno(state))
  194. end
  195. set_value (value: STRING)
  196. -- (non inheritable): Toggle's state. Values can be "ON" or "OFF". If
  197. -- 3state=True then can also be "NOTDEF". Default: "OFF". Can only be set
  198. -- to ON for a toggle inside a radio, it will automatically set to OFF
  199. -- the previous toggle that was ON.
  200. require
  201. is_valid_state(value)
  202. do
  203. iup_open.set_attribute(Current, "VALUE", value)
  204. end
  205. get_value: STRING
  206. do
  207. Result := iup_open.get_attribute(Current, "VALUE")
  208. end
  209. set_3state (state: BOOLEAN)
  210. -- (creation only): Enable a three state toggle. Valid for toggles with
  211. -- text only and that do not belong to a radio. Default: "False".
  212. do
  213. iup_open.set_attribute(Current, "3STATE", boolean_to_yesno(state))
  214. end
  215. -- Operations
  216. invert
  217. -- Invert the current state.
  218. do
  219. iup_open.set_attribute(Current, "VALUE", "TOGGLE")
  220. end
  221. -- Extra callbacks
  222. set_cb_flat_action (act: detachable FUNCTION[TUPLE[IUP_FLAT_TOGGLE], STRING])
  223. -- Action generated when the button 1 (usually left) is selected. This
  224. -- callback is called only after the mouse is released and when it is
  225. -- released inside the button area. Called only when
  226. -- set_drop_on_arrow=True
  227. --
  228. -- ih: identifier of the element that activated the event.
  229. --
  230. -- Returns: IUP_CLOSE will be processed.
  231. local
  232. operation: INTEGER
  233. do
  234. cb_flat_action := act
  235. if cb_flat_action /= Void then
  236. operation := 1
  237. else
  238. operation := 0
  239. end
  240. iup_open.set_callback (Current, "FLAT_ACTION", "Fnff", operation)
  241. end
  242. set_cb_value_changed (act: detachable FUNCTION[TUPLE[IUP_FLAT_TOGGLE], STRING])
  243. -- Called after the value was interactively changed by the user.
  244. local
  245. operation: INTEGER
  246. do
  247. cb_valuechanged := act
  248. if cb_valuechanged /= Void then
  249. operation := 1
  250. else
  251. operation := 0
  252. end
  253. iup_open.set_callback (Current, "VALUECHANGED_CB", "NONEEDED", operation)
  254. end
  255. ----------------------
  256. set_cb_flat_button (act: detachable FUNCTION[TUPLE[IUP_FLAT_TOGGLE, INTEGER, INTEGER, INTEGER, INTEGER, STRING], STRING])
  257. -- Action generated when any mouse button is pressed and when it is
  258. -- released. Both calls occur before the ACTION callback when button 1 is
  259. -- being used.
  260. -- IUP_CANVAS: identifies the element that activated the event.
  261. -- button: identifies the activated mouse button:
  262. --
  263. -- 1 - left mouse button (button 1);
  264. -- 2 - middle mouse button (button 2);
  265. -- 3 - right mouse button (button 3).
  266. --
  267. -- pressed: indicates the state of the button:
  268. --
  269. -- 0 - mouse button was released;
  270. -- 1 - mouse button was pressed.
  271. --
  272. -- x, y: position in the canvas where the event has occurred, in pixels.
  273. --
  274. -- status: status of the mouse buttons and some keyboard keys at the
  275. -- moment the event is generated. The following macros must be used for
  276. -- verification:
  277. --
  278. -- Returns: IUP_CLOSE will be processed. On some controls if IUP_IGNORE
  279. -- is returned the action is ignored (this is system dependent).
  280. local
  281. operation: INTEGER
  282. do
  283. cb_flat_button := act
  284. if cb_flat_button /= Void then
  285. operation := 1
  286. else
  287. operation := 0
  288. end
  289. iup_open.set_callback (Current, "FLAT_BUTTON_CB", "NONEEDED", operation)
  290. end
  291. set_cb_flat_focus (act: detachable FUNCTION[TUPLE[IUP_FLAT_TOGGLE, INTEGER], STRING])
  292. -- Called when the canvas gets or looses the focus. It is called after
  293. -- the common callbacks GETFOCUS_CB and KILL_FOCUS_CB.
  294. -- ih: identifier of the element that activated the event.
  295. -- focus: is non zero if the canvas is getting the focus, is zero if it
  296. -- is loosing the focus.
  297. local
  298. operation: INTEGER
  299. do
  300. cb_flat_focus := act
  301. if cb_focus /= Void then
  302. operation := 1
  303. else
  304. operation := 0
  305. end
  306. iup_open.set_callback (Current, "FLAT_FOCUS_CB", "NONEEDED", operation)
  307. end
  308. set_cb_flat_enter_window (act: detachable FUNCTION[TUPLE[IUP_FLAT_TOGGLE], STRING])
  309. -- Action generated when the mouse enters the native element.
  310. local
  311. operation: INTEGER
  312. do
  313. cb_flat_enterwindow := act
  314. if cb_enterwindow /= Void then
  315. operation := 1
  316. else
  317. operation := 0
  318. end
  319. iup_open.set_callback (Current, "FLAT_ENTERWINDOW_CB", "NONEEDED", operation)
  320. end
  321. set_cb_flat_leave_window (act: detachable FUNCTION[TUPLE[IUP_FLAT_TOGGLE], STRING])
  322. -- Action generated when the mouse leaves the native element.
  323. local
  324. operation: INTEGER
  325. do
  326. cb_flat_leavewindow := act
  327. if cb_leavewindow /= Void then
  328. operation := 1
  329. else
  330. operation := 0
  331. end
  332. iup_open.set_callback (Current, "FLAT_LEAVEWINDOW_CB", "NONEEDED", operation)
  333. end
  334. -- Validations
  335. is_valid_state (value: STRING): BOOLEAN
  336. do
  337. if value.is_equal("ON") or
  338. value.is_equal("OFF") or
  339. value.is_equal("NOTDEF") then
  340. Result := True
  341. else
  342. Result := False
  343. end
  344. end
  345. feature {IUP}
  346. -- Callbacks
  347. execute_flat_action: STRING
  348. do
  349. if attached cb_flat_action as int_cb then
  350. Result := int_cb.item([Current])
  351. else
  352. Result := "IUP_DEFAULT"
  353. end
  354. end
  355. execute_valuechanged: STRING
  356. do
  357. if attached cb_valuechanged as int_cb then
  358. Result := int_cb.item([Current])
  359. else
  360. Result := "IUP_DEFAULT"
  361. end
  362. end
  363. execute_flat_button (btn, pressed, x, y: INTEGER; status: STRING): STRING
  364. do
  365. if attached cb_flat_button as int_cb then
  366. Result := int_cb.item([Current, btn, pressed, x, y, status])
  367. else
  368. Result := "IUP_DEFAULT"
  369. end
  370. end
  371. execute_flat_focus (focus: INTEGER): STRING
  372. do
  373. if attached cb_flat_focus as int_cb then
  374. Result := int_cb.item([Current, focus])
  375. else
  376. Result := "IUP_DEFAULT"
  377. end
  378. end
  379. execute_flat_enterwindow: STRING
  380. do
  381. if attached cb_flat_enterwindow as int_cb then
  382. Result := int_cb.item([Current])
  383. else
  384. Result := "IUP_DEFAULT"
  385. end
  386. end
  387. execute_flat_leavewindow: STRING
  388. do
  389. if attached cb_flat_leavewindow as int_cb then
  390. Result := int_cb.item([Current])
  391. else
  392. Result := "IUP_DEFAULT"
  393. end
  394. end
  395. feature {NONE}
  396. cb_flat_action: detachable FUNCTION[TUPLE[IUP_FLAT_TOGGLE], STRING]
  397. cb_valuechanged: detachable FUNCTION[TUPLE[IUP_FLAT_TOGGLE], STRING]
  398. cb_flat_button: detachable FUNCTION[TUPLE[IUP_FLAT_TOGGLE, INTEGER, INTEGER, INTEGER, INTEGER, STRING], STRING]
  399. cb_flat_focus: detachable FUNCTION[TUPLE[IUP_FLAT_TOGGLE, INTEGER], STRING]
  400. cb_flat_enterwindow: detachable FUNCTION[TUPLE[IUP_FLAT_TOGGLE], STRING]
  401. cb_flat_leavewindow: detachable FUNCTION[TUPLE[IUP_FLAT_TOGGLE], STRING]
  402. -- Internals
  403. int_flat_toggle (t: POINTER): POINTER
  404. external
  405. "C inline use %"eiffel-iup.h%""
  406. alias
  407. "return IupFlatToggle ($t);"
  408. end
  409. end -- class IUP_FLAT_TOGGLE
  410. -- The MIT License (MIT)
  411. -- Copyright (c) 2019, 2020, 2021 by German A. Arias
  412. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  413. -- of this software and associated documentation files (the "Software"), to deal
  414. -- in the Software without restriction, including without limitation the rights
  415. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  416. -- copies of the Software, and to permit persons to whom the Software is
  417. -- furnished to do so, subject to the following conditions:
  418. --
  419. -- The above copyright notice and this permission notice shall be included in
  420. -- all copies or substantial portions of the Software.
  421. --
  422. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  423. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  424. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  425. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  426. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  427. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  428. -- SOFTWARE.