iup_dial.e 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. class IUP_DIAL
  2. -- Creates a dial for regulating a given angular variable. It inherits from
  3. -- IUP_CANVAS.
  4. --
  5. -- Notes:
  6. --
  7. -- When the keyboard arrows are pressed and released the mouse press and the
  8. -- mouse release callbacks are called in this order. If you hold the key down
  9. -- the mouse move callback is also called for every repetition.
  10. --
  11. -- When the wheel is rotated only the mouse move callback is called, and it
  12. -- increments the last angle the dial was rotated.
  13. --
  14. -- In all cases the value is incremented or decremented by Pi/10 (18 degrees).
  15. --
  16. -- If you press Shift while using the arrow keys the increment is reduced to
  17. -- Pi/100 (1.8 degrees). Press the Home key in the circular dial to reset to 0.
  18. inherit
  19. IUP_CANVAS
  20. redefine
  21. set_expand,
  22. set_size,
  23. execute_button_press,
  24. execute_button_release,
  25. execute_mousemove_fnd,
  26. execute_valuechanged
  27. end
  28. IUP_WIDGET_FGCOLOR
  29. redefine
  30. set_rgb_foreground_color
  31. end
  32. IUP_WIDGET_FLAT
  33. create {ANY}
  34. dial_horizontal,
  35. dial_vertical,
  36. dial_circular
  37. feature {ANY}
  38. dial_horizontal
  39. local
  40. type: STRING
  41. a_dial: POINTER
  42. do
  43. type := "HORIZONTAL"
  44. a_dial := int_dial(get_pointer(type.to_c))
  45. set_widget(a_dial)
  46. end
  47. dial_vertical
  48. local
  49. type: STRING
  50. a_dial: POINTER
  51. do
  52. type := "VERTICAL"
  53. a_dial := int_dial(get_pointer(type.to_c))
  54. set_widget(a_dial)
  55. end
  56. dial_circular
  57. local
  58. type: STRING
  59. a_dial: POINTER
  60. do
  61. type := "CIRCULAR"
  62. a_dial := int_dial(get_pointer(type.to_c))
  63. set_widget(a_dial)
  64. end
  65. -- Attributes
  66. set_density (value: REAL)
  67. -- number of lines per pixel in the handle of the dial. Default is
  68. -- "0.2".
  69. require
  70. value >= 0
  71. do
  72. iup_open.set_attribute(Current, "DENSITY", value.out)
  73. end
  74. set_expand (type: STRING)
  75. -- The default value is "False".
  76. do
  77. Precursor (type)
  78. end
  79. set_rgb_foreground_color (red: INTEGER; green: INTEGER; blue: INTEGER)
  80. -- Foreground color. The default value is "64 64 64".
  81. do
  82. Precursor (red, green, blue)
  83. end
  84. set_size (width: INTEGER; height: INTEGER)
  85. -- The initial size is "16x80", "80x16" or "40x35" according to the dial
  86. -- orientation.
  87. do
  88. Precursor (width, height)
  89. end
  90. set_automatic_layout
  91. -- Set to allow the automatic layout use smaller values.
  92. do
  93. iup_open.set_attribute_null(Current, "SIZE")
  94. end
  95. set_unit_degrees
  96. do
  97. iup_open.set_attribute(Current, "UNIT", "DEGREES")
  98. end
  99. set_unit_radians
  100. -- This is the default value.
  101. do
  102. iup_open.set_attribute(Current, "UNIT", "RADIANS")
  103. end
  104. set_value (value: REAL)
  105. -- The dial angular value in radians always. The value is reset to zero
  106. -- when the interaction is started, except for circular orientation. When
  107. -- orientation is vertical or horizontal, the dial measures relative
  108. -- angles. When orientation is circular the dial measure absolute angles,
  109. -- where the origin is at 3 O'clock.
  110. do
  111. iup_open.set_attribute(Current, "VALUE", value.out)
  112. end
  113. get_value: REAL
  114. local
  115. str: STRING
  116. do
  117. str := iup_open.get_attribute(Current, "VALUE")
  118. if str.is_real then
  119. Result := str.to_real
  120. end
  121. end
  122. -- Callbacks
  123. set_cb_button_press (act: detachable FUNCTION[TUPLE[IUP_DIAL, REAL_64], STRING])
  124. -- Called when the user presses the left mouse button over the dial. The
  125. -- angle here is always zero, except for the circular dial.
  126. --
  127. -- ih: identifier of the element that activated the event.
  128. -- angle: the dial value converted according to UNIT.
  129. local
  130. operation: INTEGER
  131. do
  132. cb_button_press := act
  133. if cb_button_press /= Void then
  134. operation := 1
  135. else
  136. operation := 0
  137. end
  138. iup_open.set_callback (Current, "BUTTON_PRESS_CB", "NONEEDED", operation)
  139. end
  140. set_cb_button_release (act: detachable FUNCTION[TUPLE[IUP_DIAL, REAL_64], STRING])
  141. -- Called when the user releases the left mouse button after pressing it
  142. -- over the dial.
  143. --
  144. -- ih: identifier of the element that activated the event.
  145. -- angle: the dial value converted according to UNIT.
  146. local
  147. operation: INTEGER
  148. do
  149. cb_button_release := act
  150. if cb_button_release /= Void then
  151. operation := 1
  152. else
  153. operation := 0
  154. end
  155. iup_open.set_callback (Current, "BUTTON_RELEASE_CB", "NONEEDED", operation)
  156. end
  157. set_cb_mouse_move (act: detachable FUNCTION[TUPLE[IUP_DIAL, REAL_64], STRING])
  158. -- Called each time the user moves the dial with the mouse button
  159. -- pressed. The angle the dial rotated since it was initialized is passed
  160. -- as a parameter.
  161. --
  162. -- ih: identifier of the element that activated the event.
  163. -- angle: the dial value converted according to UNIT.
  164. local
  165. operation: INTEGER
  166. do
  167. cb_mousemove := act
  168. if cb_mousemove /= Void then
  169. operation := 1
  170. else
  171. operation := 0
  172. end
  173. iup_open.set_callback (Current, "MOUSEMOVE_CB", "Fnd", operation)
  174. end
  175. set_cb_value_changed (act: detachable FUNCTION[TUPLE[IUP_DIAL], STRING])
  176. -- Called after the value was interactively changed by the user. It is
  177. -- called whenever a BUTTON_PRESS_CB, a BUTTON_RELEASE_CB or a
  178. -- MOUSEMOVE_CB would also be called, but if defined those callbacks will
  179. -- not be called.
  180. local
  181. operation: INTEGER
  182. do
  183. cb_valuechanged := act
  184. if cb_valuechanged /= Void then
  185. operation := 1
  186. else
  187. operation := 0
  188. end
  189. iup_open.set_callback (Current, "VALUECHANGED_CB", "NONEEDED", operation)
  190. end
  191. feature {IUP}
  192. execute_button_press (angle: REAL_64): STRING
  193. do
  194. if attached cb_button_press as int_cb then
  195. Result := int_cb.item([Current, angle])
  196. else
  197. Result := "IUP_DEFAULT"
  198. end
  199. end
  200. execute_button_release (angle: REAL_64): STRING
  201. do
  202. if attached cb_button_release as int_cb then
  203. Result := int_cb.item([Current, angle])
  204. else
  205. Result := "IUP_DEFAULT"
  206. end
  207. end
  208. execute_mousemove_fnd (angle: REAL_64): STRING
  209. do
  210. if attached cb_mousemove as int_cb then
  211. Result := int_cb.item([Current, angle])
  212. else
  213. Result := "IUP_DEFAULT"
  214. end
  215. end
  216. execute_valuechanged: STRING
  217. do
  218. if attached cb_valuechanged as int_cb then
  219. Result := int_cb.item([Current])
  220. else
  221. Result := "IUP_DEFAULT"
  222. end
  223. end
  224. feature {NONE}
  225. -- Callbacks
  226. cb_button_press: detachable FUNCTION[TUPLE[IUP_DIAL, REAL_64], STRING]
  227. cb_button_release: detachable FUNCTION[TUPLE[IUP_DIAL, REAL_64], STRING]
  228. cb_mousemove: detachable FUNCTION[TUPLE[IUP_DIAL, REAL_64], STRING]
  229. cb_valuechanged: detachable FUNCTION[TUPLE[IUP_DIAL], STRING]
  230. -- Internals
  231. int_dial (type: POINTER): POINTER
  232. external
  233. "C inline use %"eiffel-iup.h%""
  234. alias
  235. "return IupDial ($type);"
  236. end
  237. end -- class IUP_DIAL
  238. -- The MIT License (MIT)
  239. -- Copyright (c) 2019, 2020 by German A. Arias
  240. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  241. -- of this software and associated documentation files (the "Software"), to deal
  242. -- in the Software without restriction, including without limitation the rights
  243. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  244. -- copies of the Software, and to permit persons to whom the Software is
  245. -- furnished to do so, subject to the following conditions:
  246. --
  247. -- The above copyright notice and this permission notice shall be included in
  248. -- all copies or substantial portions of the Software.
  249. --
  250. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  251. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  252. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  253. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  254. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  255. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  256. -- SOFTWARE.