iup_toggle.e 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. create {ANY}
  37. toggle
  38. feature {ANY}
  39. toggle (title: STRING)
  40. -- A new toggle. title is the text to be shown on the toggle. It can be
  41. -- Void. It will set the TITLE attribute.
  42. local
  43. a_toggle, p: POINTER
  44. do
  45. if title /= Void then
  46. a_toggle := int_toggle(title.to_external, p)
  47. else
  48. a_toggle := int_toggle(p, p)
  49. end
  50. set_widget(a_toggle)
  51. end
  52. -- Attributes
  53. is_radio: BOOLEAN
  54. -- (read-only): returns if the toggle is inside a radio. Valid only after
  55. -- the element is mapped.
  56. local
  57. str: STRING
  58. do
  59. str := iup_open.get_attribute(Current, "RADIO")
  60. Result := yesno_to_boolean(str)
  61. end
  62. set_right_button (state: BOOLEAN)
  63. -- (Windows Only) (creation only): place the check button at the right of
  64. -- the text. Default: "False".
  65. do
  66. iup_open.set_attribute(Current, "RIGHTBUTTON", boolean_to_yesno(state))
  67. end
  68. set_value (value: STRING)
  69. -- (non inheritable): Toggle's state. Values can be "ON" or "OFF". If
  70. -- 3state=True then can also be "NOTDEF". Default: "OFF". In GTK if you
  71. -- change the state of a radio, the unchecked toggle will receive an
  72. -- ACTION callback notification. Can only be set to "Yes" for a toggle
  73. -- inside a radio, it will automatically set to OFF the previous toggle
  74. -- that was ON.
  75. do
  76. iup_open.set_attribute(Current, "VALUE", value)
  77. end
  78. get_value: STRING
  79. do
  80. Result := iup_open.get_attribute(Current, "VALUE")
  81. end
  82. set_3state (state: BOOLEAN)
  83. -- (creation only): Enable a three state toggle. Valid for toggles with
  84. -- text only and that do not belong to a radio. Default: "False".
  85. do
  86. iup_open.set_attribute(Current, "3STATE", boolean_to_yesno(state))
  87. end
  88. -- Operations
  89. invert
  90. -- Invert the current state.
  91. do
  92. iup_open.set_attribute(Current, "VALUE", "TOGGLE")
  93. end
  94. -- Callbacks
  95. set_cb_toggle (act: FUNCTION[TUPLE[IUP_TOGGLE, INTEGER], STRING])
  96. -- Action generated when the toggle's state (on/off) was changed. The
  97. -- callback also receives the toggle's state.
  98. --
  99. -- ih: identifier of the element that activated the event.
  100. -- state: 1 if the toggle's state was shifted to on; 0 if it was shifted
  101. -- to off.
  102. --
  103. -- Returns: IUP_CLOSE will be processed.
  104. local
  105. operation: INTEGER
  106. do
  107. cb_action_fni := act
  108. if cb_action_fni /= Void then
  109. operation := 1
  110. else
  111. operation := 0
  112. end
  113. iup_open.set_callback (Current, "ACTION", "Fn", operation)
  114. end
  115. set_cb_value_changed (act: FUNCTION[TUPLE[IUP_TOGGLE], STRING])
  116. -- Called after the value was interactively changed by the
  117. -- user. Called after the ACTION callback, but under the same context.
  118. local
  119. operation: INTEGER
  120. do
  121. cb_valuechanged := act
  122. if cb_valuechanged /= Void then
  123. operation := 1
  124. else
  125. operation := 0
  126. end
  127. iup_open.set_callback (Current, "VALUECHANGED_CB", "NONEEDED", operation)
  128. end
  129. feature {IUP}
  130. -- Common callbacks
  131. execute_map: STRING
  132. do
  133. Result := cb_map.item([Current])
  134. end
  135. execute_unmap: STRING
  136. do
  137. Result := cb_unmap.item([Current])
  138. end
  139. execute_destroy: STRING
  140. do
  141. Result := cb_destroy.item([Current])
  142. end
  143. execute_getfocus: STRING
  144. do
  145. Result := cb_getfocus.item([Current])
  146. end
  147. execute_killfocus: STRING
  148. do
  149. Result := cb_getfocus.item([Current])
  150. end
  151. execute_enterwindow: STRING
  152. do
  153. Result := cb_enterwindow.item([Current])
  154. end
  155. execute_leavewindow: STRING
  156. do
  157. Result := cb_leavewindow.item([Current])
  158. end
  159. execute_k_any (c: INTEGER): STRING
  160. do
  161. Result := cb_k_any.item([Current, c])
  162. end
  163. execute_help
  164. do
  165. cb_help.call([Current])
  166. end
  167. -- Extra
  168. execute_action_fni (state: INTEGER): STRING
  169. do
  170. Result := cb_action_fni.item([Current, state])
  171. end
  172. execute_valuechanged: STRING
  173. do
  174. Result := cb_valuechanged.item([Current])
  175. end
  176. feature {}
  177. -- For callbacks
  178. cb_action_fni: FUNCTION[TUPLE[IUP_TOGGLE, INTEGER], STRING]
  179. cb_valuechanged: FUNCTION[TUPLE[IUP_TOGGLE], STRING]
  180. -- Internals
  181. int_toggle (title: POINTER; action: POINTER): POINTER
  182. external "plug_in"
  183. alias "{
  184. location: "${sys}/plugins"
  185. module_name: "iup"
  186. feature_name: "IupToggle"
  187. }"
  188. end
  189. end
  190. -- The MIT License (MIT)
  191. -- Copyright (c) 2016, 2017 by German A. Arias
  192. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  193. -- of this software and associated documentation files (the "Software"), to deal
  194. -- in the Software without restriction, including without limitation the rights
  195. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  196. -- copies of the Software, and to permit persons to whom the Software is
  197. -- furnished to do so, subject to the following conditions:
  198. --
  199. -- The above copyright notice and this permission notice shall be included in
  200. -- all copies or substantial portions of the Software.
  201. --
  202. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  203. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  204. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  205. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  206. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  207. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  208. -- SOFTWARE.