iup_toggle.e 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. class IUP_TOGGLE
  2. -- Creates the toggle interface element. It is a two-state (on/off) button
  3. -- that, when selected, generates an action that activates a function in the
  4. -- associated application. Its visual representation can contain a text or an
  5. -- image.
  6. --
  7. -- Toggle with image or text can not change its behavior after mapped. This is
  8. -- a creation attribute. But after creation the image can be changed for
  9. -- another image, and the text for another text.
  10. --
  11. -- Toggles are activated using the Space key.
  12. --
  13. -- To build a set of mutual exclusive toggles, insert them in an IUP_RADIO
  14. -- container. They must be inserted before creation, and their behavior can not
  15. -- be changed. If you need to dynamically remove toggles that belongs to a
  16. -- radio in Windows, then put the radio inside an IUP_FRAME that has a title.
  17. --
  18. -- A toggle that is a child of an IUP_RADIO automatically receives a name when
  19. -- its is mapped into the native system.
  20. inherit
  21. IUP_BUTTON
  22. redefine
  23. execute_map,
  24. execute_unmap,
  25. execute_destroy,
  26. execute_getfocus,
  27. execute_killfocus,
  28. execute_enterwindow,
  29. execute_leavewindow,
  30. execute_k_any,
  31. execute_help,
  32. execute_action_fni,
  33. execute_valuechanged
  34. end
  35. IUP_WIDGET_IGNORERADIO
  36. IUP_WIDGET_IMPRESS_BORDER
  37. export
  38. {NONE} all
  39. end
  40. IUP_WIDGET_IMAGE_POSITION
  41. export
  42. {NONE} all
  43. end
  44. IUP_WIDGET_SPACING
  45. export
  46. {NONE} all
  47. end
  48. IUP_WIDGET_CB_ACTION
  49. export
  50. {NONE} all
  51. end
  52. IUP_WIDGET_CB_BUTTON
  53. export
  54. {NONE} all
  55. end
  56. IUP_WIDGET_TOGGLE
  57. create {ANY}
  58. toggle
  59. feature {ANY}
  60. toggle (title: STRING)
  61. -- A new toggle. title is the text to be shown on the toggle. It can be
  62. -- Void. It will set the TITLE attribute.
  63. local
  64. a_toggle, p: POINTER
  65. do
  66. if title /= Void then
  67. a_toggle := int_toggle(get_pointer(title.to_c), p)
  68. else
  69. a_toggle := int_toggle(p, p)
  70. end
  71. set_widget(a_toggle)
  72. end
  73. -- Attributes
  74. is_radio: BOOLEAN
  75. -- (read-only): returns if the toggle is inside a radio. Valid only after
  76. -- the element is mapped.
  77. local
  78. str: STRING
  79. do
  80. str := iup_open.get_attribute(Current, "RADIO")
  81. Result := yesno_to_boolean(str)
  82. end
  83. set_right_button (state: BOOLEAN)
  84. -- (Windows Only) (creation only): place the check button at the right of
  85. -- the text. Default: "False".
  86. do
  87. iup_open.set_attribute(Current, "RIGHTBUTTON", boolean_to_yesno(state))
  88. end
  89. set_value (value: STRING)
  90. -- (non inheritable): Toggle's state. Values can be "ON" or "OFF". If
  91. -- 3state=True then can also be "NOTDEF". Default: "OFF". In GTK if you
  92. -- change the state of a radio, the unchecked toggle will receive an
  93. -- ACTION callback notification. Can only be set to "ON" for a toggle
  94. -- inside a radio, it will automatically set to OFF the previous toggle
  95. -- that was ON.
  96. require
  97. is_valid_state (value)
  98. do
  99. iup_open.set_attribute(Current, "VALUE", value)
  100. end
  101. get_value: STRING
  102. do
  103. Result := iup_open.get_attribute(Current, "VALUE")
  104. end
  105. set_3state (state: BOOLEAN)
  106. -- (creation only): Enable a three state toggle. Valid for toggles with
  107. -- text only and that do not belong to a radio. Default: "False".
  108. do
  109. iup_open.set_attribute(Current, "3STATE", boolean_to_yesno(state))
  110. end
  111. -- Operations
  112. invert
  113. -- Invert the current state.
  114. do
  115. iup_open.set_attribute(Current, "VALUE", "TOGGLE")
  116. end
  117. -- Callbacks
  118. set_cb_toggle (act: detachable FUNCTION[TUPLE[IUP_TOGGLE, INTEGER], STRING])
  119. -- Action generated when the toggle's state (on/off) was changed. The
  120. -- callback also receives the toggle's state.
  121. --
  122. -- ih: identifier of the element that activated the event.
  123. -- state: 1 if the toggle's state was shifted to on; 0 if it was shifted
  124. -- to off.
  125. --
  126. -- Returns: IUP_CLOSE will be processed.
  127. local
  128. operation: INTEGER
  129. do
  130. cb_action_fni := act
  131. if cb_action_fni /= Void then
  132. operation := 1
  133. else
  134. operation := 0
  135. end
  136. iup_open.set_callback (Current, "ACTION", "Fn", operation)
  137. end
  138. set_cb_value_changed (act: detachable FUNCTION[TUPLE[IUP_TOGGLE], STRING])
  139. -- Called after the value was interactively changed by the
  140. -- user. Called after the ACTION callback, but under the same context.
  141. local
  142. operation: INTEGER
  143. do
  144. cb_valuechanged := act
  145. if cb_valuechanged /= Void then
  146. operation := 1
  147. else
  148. operation := 0
  149. end
  150. iup_open.set_callback (Current, "VALUECHANGED_CB", "NONEEDED", operation)
  151. end
  152. -- Validations
  153. is_valid_state (value: STRING): BOOLEAN
  154. do
  155. if value.is_equal("ON") or
  156. value.is_equal("OFF") or
  157. value.is_equal("NOTDEF") then
  158. Result := True
  159. else
  160. Result := False
  161. end
  162. end
  163. feature {IUP}
  164. -- Common callbacks
  165. execute_map: STRING
  166. do
  167. if attached cb_map as int_cb then
  168. Result := int_cb.item([Current])
  169. else
  170. Result := "IUP_DEFAULT"
  171. end
  172. end
  173. execute_unmap: STRING
  174. do
  175. if attached cb_unmap as int_cb then
  176. Result := int_cb.item([Current])
  177. else
  178. Result := "IUP_DEFAULT"
  179. end
  180. end
  181. execute_destroy: STRING
  182. do
  183. if attached cb_destroy as int_cb then
  184. Result := int_cb.item([Current])
  185. else
  186. Result := "IUP_DEFAULT"
  187. end
  188. end
  189. execute_getfocus: STRING
  190. do
  191. if attached cb_getfocus as int_cb then
  192. Result := int_cb.item([Current])
  193. else
  194. Result := "IUP_DEFAULT"
  195. end
  196. end
  197. execute_killfocus: STRING
  198. do
  199. if attached cb_killfocus as int_Cb then
  200. Result := int_cb.item([Current])
  201. else
  202. Result := "IUP_DEFAULT"
  203. end
  204. end
  205. execute_enterwindow: STRING
  206. do
  207. if attached cb_enterwindow as int_cb then
  208. Result := int_cb.item([Current])
  209. else
  210. Result := "IUP_DEFAULT"
  211. end
  212. end
  213. execute_leavewindow: STRING
  214. do
  215. if attached cb_leavewindow as int_cb then
  216. Result := int_cb.item([Current])
  217. else
  218. Result := "IUP_DEFAULT"
  219. end
  220. end
  221. execute_k_any (c: INTEGER): STRING
  222. do
  223. if attached cb_k_any as int_cb then
  224. Result := int_cb.item([Current, c])
  225. else
  226. Result := "IUP_DEFAULT"
  227. end
  228. end
  229. execute_help
  230. do
  231. if attached cb_help as int_cb then
  232. int_cb.call([Current])
  233. end
  234. end
  235. -- Extra
  236. execute_action_fni (state: INTEGER): STRING
  237. do
  238. if attached cb_action_fni as int_cb then
  239. Result := int_cb.item([Current, state])
  240. else
  241. Result := "IUP_DEFAULT"
  242. end
  243. end
  244. execute_valuechanged: STRING
  245. do
  246. if attached cb_valuechanged as int_cb then
  247. Result := int_cb.item([Current])
  248. else
  249. Result := "IUP_DEFAULT"
  250. end
  251. end
  252. feature {NONE}
  253. -- For callbacks
  254. cb_action_fni: detachable FUNCTION[TUPLE[IUP_TOGGLE, INTEGER], STRING]
  255. cb_valuechanged: detachable FUNCTION[TUPLE[IUP_TOGGLE], STRING]
  256. -- Internals
  257. int_toggle (title: POINTER; action: POINTER): POINTER
  258. external
  259. "C inline use %"eiffel-iup.h%""
  260. alias
  261. "return IupToggle ($title, $action);"
  262. end
  263. end
  264. -- The MIT License (MIT)
  265. -- Copyright (c) 2016, 2017, 2019, 2020 by German A. Arias
  266. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  267. -- of this software and associated documentation files (the "Software"), to deal
  268. -- in the Software without restriction, including without limitation the rights
  269. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  270. -- copies of the Software, and to permit persons to whom the Software is
  271. -- furnished to do so, subject to the following conditions:
  272. --
  273. -- The above copyright notice and this permission notice shall be included in
  274. -- all copies or substantial portions of the Software.
  275. --
  276. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  277. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  278. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  279. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  280. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  281. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  282. -- SOFTWARE.