iup_val.e 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. class IUP_VAL
  2. -- Creates a Valuator control. Selects a value in a limited interval. Also
  3. -- known as Scale or Trackbar in native systems.
  4. --
  5. -- In Motif, after the user clicks the handler a KILLFOCUS will be ignored when
  6. -- the control loses its focus.
  7. --
  8. -- Keyboard Mapping
  9. --
  10. -- This is the default mapping when horizontal and INVERTED has the default
  11. -- value (False):
  12. --
  13. -- Keys Action for HORIZONTAL
  14. -- ============================================================
  15. -- Right Arrow move right, increment by one step
  16. -- Left Arrow move left, decrement by one step
  17. -- Ctrl+Right Arrow or PgDn move right, increment by one page step
  18. -- Ctrl+Left Arrow or PgUp move left, decrement by one page step
  19. -- Home move all left, set to minimum
  20. -- End move all right, set to maximum
  21. --
  22. --
  23. -- This is the default mapping when vertical and INVERTED has the default
  24. -- value (True):
  25. --
  26. -- Keys Action for VERTICAL
  27. -- ============================================================
  28. -- Up Arrow move up, increment by one step
  29. -- Down Arrow move down, decrement by one step
  30. -- Ctrl+Up Arrow or PgUp move up, increment by one page step
  31. -- Ctrl+Down Arrow or PgDn move down, decrement by one page step
  32. -- Home move all up, set to maximum
  33. -- End move all down, set to minimum
  34. --
  35. -- Visually all the keys move to the same direction independent from the
  36. -- INVERTED attribute.
  37. --
  38. -- Semantically all the keys change the value depending on the INVERTED
  39. -- attribute.
  40. --
  41. -- This behavior is slightly different from the defined by the native systems
  42. -- (Home and End keys are different). But it is the same in all systems.
  43. inherit
  44. IUP_WIDGET
  45. redefine
  46. execute_map,
  47. execute_unmap,
  48. execute_destroy,
  49. execute_getfocus,
  50. execute_killfocus,
  51. execute_enterwindow,
  52. execute_leavewindow,
  53. execute_k_any,
  54. execute_help,
  55. execute_valuechanged
  56. end
  57. IUP_WIDGET_BGCOLOR
  58. IUP_WIDGET_RASTERSIZE
  59. IUP_WIDGET_USERSIZE
  60. IUP_WIDGET_ACTIVE
  61. IUP_WIDGET_EXPAND
  62. IUP_WIDGET_FONT
  63. IUP_WIDGET_SCREENPOSITION
  64. IUP_WIDGET_POSITION
  65. IUP_WIDGET_MAXMIN_SIZE
  66. IUP_WIDGET_TIP
  67. IUP_WIDGET_SIZE
  68. IUP_WIDGET_ZORDER
  69. IUP_WIDGET_VISIBLE
  70. IUP_WIDGET_CHILD
  71. IUP_WIDGET_NAME
  72. IUP_WIDGET_FOCUS
  73. IUP_WIDGET_PROPAGATEFOCUS
  74. IUP_WIDGET_CUSTOM_ATTRIBUTES
  75. create {ANY}
  76. horizontal_val,
  77. vertical_val
  78. feature {ANY}
  79. horizontal_val
  80. local
  81. a_val: POINTER
  82. orientation: STRING
  83. do
  84. orientation := "HORIZONTAL"
  85. a_val := int_val(get_pointer(orientation.to_c))
  86. set_widget(a_val)
  87. minv := 0
  88. maxv := 1
  89. end
  90. vertical_val
  91. local
  92. a_val: POINTER
  93. orientation: STRING
  94. do
  95. orientation := "VERTICAL"
  96. a_val := int_val(get_pointer(orientation.to_c))
  97. set_widget(a_val)
  98. minv := 0
  99. maxv := 1
  100. end
  101. -- Attributes
  102. set_can_focus (state: BOOLEAN)
  103. -- (creation only) (non inheritable): enables the focus traversal of the
  104. -- control. In Windows the control will still get the focus when
  105. -- clicked. Default: True.
  106. do
  107. iup_open.set_attribute(Current, "CANFOCUS", boolean_to_yesno(state))
  108. end
  109. set_inverted (state: BOOLEAN)
  110. -- Invert the minimum and maximum positions on screen. When INVERTED=True
  111. -- maximum is at top and left (minimum is bottom and right), when
  112. -- INVERTED=False maximum is at bottom and right (minimum is top and
  113. -- left). The initial value depends on the orientation, if vertical
  114. -- default is True, if horizontal default is False.
  115. do
  116. iup_open.set_attribute(Current, "INVERTED", boolean_to_yesno(state))
  117. end
  118. set_max (max_value: REAL)
  119. -- Contains the maximum valuator value. Default is "1". When changed the
  120. -- display will not be updated until a call to set_value.
  121. require
  122. minv < max_value
  123. do
  124. iup_open.set_attribute(Current, "MAX", max_value.out)
  125. -- Update the max value
  126. maxv := max_value
  127. ensure
  128. updated: maxv = max_value
  129. end
  130. set_min (min_value: REAL)
  131. -- Contains the minimum valuator value. Default is "0". When changed the
  132. -- display will not be updated until a call to set_value.
  133. require
  134. maxv > min_value
  135. do
  136. iup_open.set_attribute(Current, "MIN", min_value.out)
  137. -- Update the min value
  138. minv := min_value
  139. ensure
  140. updated: minv = min_value
  141. end
  142. set_page_step (value: REAL)
  143. -- Controls the increment for pagedown and pageup keys. It is not the
  144. -- size of the increment. The increment size is "pagestep*(max-min)", so
  145. -- it must be 0<pagestep<1. Default is "0.1".
  146. require
  147. value >= 0
  148. value <= 1
  149. do
  150. iup_open.set_attribute(Current, "PAGESTEP", value.out)
  151. end
  152. set_show_ticks (value: INTEGER)
  153. -- [Windows and Motif Only]: The number of tick marks along the valuator
  154. -- trail. Minimum value is "2". Default is "0", in this case the ticks
  155. -- are not shown. It can not be changed to 0 from a non zero value, or
  156. -- vice-versa, after the control is mapped. GTK does not support
  157. -- ticks.
  158. require
  159. value >= 0
  160. value /= 1
  161. do
  162. iup_open.set_attribute(Current, "SHOWTICKS", value.out)
  163. end
  164. set_step (value: REAL)
  165. -- Controls the increment for keyboard control and the mouse wheel. It is
  166. -- not the size of the increment. The increment size is "step*(max-min)",
  167. -- so it must be 0<step<1. Default is "0.01".
  168. require
  169. value >= 0
  170. value <= 1
  171. do
  172. iup_open.set_attribute(Current, "STEP", value.out)
  173. end
  174. set_ticks_pos (value: STRING)
  175. -- [Windows Only] (creation only): Allows to position the ticks in both
  176. -- sides (BOTH) or in the reverse side (REVERSE). Default: NORMAL. The
  177. -- normal position for horizontal orientation is at the top of the
  178. -- control, and for vertical orientation is at the left of the control.
  179. -- In Motif, the ticks position is always normal.
  180. require
  181. is_valid_ticks_pos(value)
  182. do
  183. iup_open.set_attribute(Current, "TICKSPOS", value)
  184. end
  185. is_horizontal: BOOLEAN
  186. local
  187. str: STRING
  188. do
  189. str := iup_open.get_attribute(Current, "ORIENTATION")
  190. if str.is_equal("HORIZONTAL") then
  191. Result := True
  192. else
  193. Result := False
  194. end
  195. end
  196. is_vertical: BOOLEAN
  197. local
  198. str: STRING
  199. do
  200. str := iup_open.get_attribute(Current, "ORIENTATION")
  201. if str.is_equal("VERTICAL") then
  202. Result := True
  203. else
  204. Result := False
  205. end
  206. end
  207. set_value (value: REAL)
  208. -- (non inheritable): Contains a number between min and max, indicating
  209. -- the valuator position. Default: "0.0".
  210. do
  211. iup_open.set_attribute(Current, "VALUE", value.out)
  212. end
  213. get_value: REAL
  214. -- The value.
  215. local
  216. str: STRING
  217. do
  218. str := iup_open.get_attribute(Current, "VALUE")
  219. if str.is_real then
  220. Result := str.to_real
  221. end
  222. end
  223. -- Callbacks
  224. -- Common
  225. set_cb_map (act: detachable FUNCTION[TUPLE[IUP_VAL], STRING])
  226. -- Called right after an element is mapped and its attributes updated.
  227. local
  228. operation: INTEGER
  229. do
  230. cb_map := act
  231. if cb_map /= Void then
  232. operation := 1
  233. else
  234. operation := 0
  235. end
  236. iup_open.set_callback (Current, "MAP_CB", "NONEEDED", operation)
  237. end
  238. set_cb_unmap (act: detachable FUNCTION[TUPLE[IUP_VAL], STRING])
  239. -- Called right before an element is unmapped.
  240. local
  241. operation: INTEGER
  242. do
  243. cb_unmap := act
  244. if cb_unmap /= Void then
  245. operation := 1
  246. else
  247. operation := 0
  248. end
  249. iup_open.set_callback (Current, "UNMAP_CB", "NONEEDED", operation)
  250. end
  251. set_cb_destroy (act: detachable FUNCTION[TUPLE[IUP_VAL], STRING])
  252. -- Called right before an element is destroyed.
  253. local
  254. operation: INTEGER
  255. do
  256. cb_destroy := act
  257. if cb_destroy /= Void then
  258. operation := 1
  259. else
  260. operation := 0
  261. end
  262. iup_open.set_callback (Current, "DESTROY_CB", "NONEEDED", operation)
  263. end
  264. set_cb_get_focus (act: detachable FUNCTION[TUPLE[IUP_VAL], STRING])
  265. -- Action generated when an element is given keyboard focus.
  266. -- This callback is called after the KILLFOCUS_CB of the element
  267. -- that loosed the focus. The {IUP}.get_focus function during the
  268. -- callback returns the element that loosed the focus.
  269. local
  270. operation: INTEGER
  271. do
  272. cb_getfocus := act
  273. if cb_getfocus /= Void then
  274. operation := 1
  275. else
  276. operation := 0
  277. end
  278. iup_open.set_callback (Current, "GETFOCUS_CB", "NONEEDED", operation)
  279. end
  280. set_cb_kill_focus (act: detachable FUNCTION[TUPLE[IUP_VAL], STRING])
  281. -- Action generated when an element loses keyboard focus. This
  282. -- callback is called before the GETFOCUS_CB of the element that
  283. -- gets the focus.
  284. local
  285. operation: INTEGER
  286. do
  287. cb_killfocus := act
  288. if cb_killfocus /= Void then
  289. operation := 1
  290. else
  291. operation := 0
  292. end
  293. iup_open.set_callback (Current, "KILLFOCUS_CB", "NONEEDED", operation)
  294. end
  295. set_cb_enter_window (act: detachable FUNCTION[TUPLE[IUP_VAL], STRING])
  296. -- Action generated when the mouse enters the native element.
  297. local
  298. operation: INTEGER
  299. do
  300. cb_enterwindow := act
  301. if cb_enterwindow /= Void then
  302. operation := 1
  303. else
  304. operation := 0
  305. end
  306. iup_open.set_callback (Current, "ENTERWINDOW_CB", "NONEEDED", operation)
  307. end
  308. set_cb_leave_window (act: detachable FUNCTION[TUPLE[IUP_VAL], STRING])
  309. -- Action generated when the mouse leaves the native element.
  310. local
  311. operation: INTEGER
  312. do
  313. cb_leavewindow := act
  314. if cb_leavewindow /= Void then
  315. operation := 1
  316. else
  317. operation := 0
  318. end
  319. iup_open.set_callback (Current, "LEAVEWINDOW_CB", "NONEEDED", operation)
  320. end
  321. set_cb_k_any (act: detachable FUNCTION[TUPLE[IUP_VAL, INTEGER], STRING])
  322. -- Action generated when a keyboard event occurs.
  323. -- IUP_WIDGET the element that activated the event.
  324. -- INTEGER identifier of typed key. Please refer to the Keyboard
  325. -- Codes table for a list of possible values.
  326. --
  327. -- Returns: If IUP_IGNORE is returned the key is ignored and not
  328. -- processed by the control and not propagated. If returns
  329. -- IUP_CONTINUE, the key will be processed and the event will be
  330. -- propagated to the parent of the element receiving it, this is
  331. -- the default behavior. If returns IUP_DEFAULT the key is processed
  332. -- but it is not propagated. IUP_CLOSE will be processed.
  333. local
  334. operation: INTEGER
  335. do
  336. cb_k_any := act
  337. if cb_k_any /= Void then
  338. operation := 1
  339. else
  340. operation := 0
  341. end
  342. iup_open.set_callback (Current, "K_ANY", "NONEEDED", operation)
  343. end
  344. set_cb_help (act: detachable PROCEDURE[TUPLE[IUP_VAL]])
  345. -- Action generated when the user press F1 at a control. In Motif
  346. -- is also activated by the Help button in some workstations
  347. -- keyboard.
  348. -- Returns: IUP_CLOSE will be processed.
  349. local
  350. operation: INTEGER
  351. do
  352. cb_help := act
  353. if cb_help /= Void then
  354. operation := 1
  355. else
  356. operation := 0
  357. end
  358. iup_open.set_callback (Current, "HELP_CB", "NONEEDED", operation)
  359. end
  360. -- Extra
  361. set_cb_value_changed (act: detachable FUNCTION[TUPLE[IUP_VAL], STRING])
  362. -- Called after the value was interactively changed by the user.
  363. local
  364. operation: INTEGER
  365. do
  366. cb_valuechanged := act
  367. if cb_valuechanged /= Void then
  368. operation := 1
  369. else
  370. operation := 0
  371. end
  372. iup_open.set_callback (Current, "VALUECHANGED_CB", "NONEEDED", operation)
  373. end
  374. -- Validations
  375. minv, maxv: REAL
  376. is_valid_ticks_pos (value: STRING): BOOLEAN
  377. do
  378. if value.is_equal("BOTH") or
  379. value.is_equal("REVERSE") or
  380. value.is_equal("NORMAL") then
  381. Result := True
  382. else
  383. Result := False
  384. end
  385. end
  386. feature {IUP}
  387. execute_map: STRING
  388. do
  389. if attached cb_map as int_cb then
  390. Result := int_cb.item([Current])
  391. else
  392. Result := "IUP_DEFAULT"
  393. end
  394. end
  395. execute_unmap: STRING
  396. do
  397. if attached cb_unmap as int_cb then
  398. Result := int_cb.item([Current])
  399. else
  400. Result := "IUP_DEFAULT"
  401. end
  402. end
  403. execute_destroy: STRING
  404. do
  405. if attached cb_destroy as int_cb then
  406. Result := int_cb.item([Current])
  407. else
  408. Result := "IUP_DEFAULT"
  409. end
  410. end
  411. execute_getfocus: STRING
  412. do
  413. if attached cb_getfocus as int_cb then
  414. Result := int_cb.item([Current])
  415. else
  416. Result := "IUP_DEFAULT"
  417. end
  418. end
  419. execute_killfocus: STRING
  420. do
  421. if attached cb_killfocus as int_cb then
  422. Result := int_cb.item([Current])
  423. else
  424. Result := "IUP_DEFAULT"
  425. end
  426. end
  427. execute_enterwindow: STRING
  428. do
  429. if attached cb_enterwindow as int_cb then
  430. Result := int_cb.item([Current])
  431. else
  432. Result := "IUP_DEFAULT"
  433. end
  434. end
  435. execute_leavewindow: STRING
  436. do
  437. if attached cb_leavewindow as int_cb then
  438. Result := int_cb.item([Current])
  439. else
  440. Result := "IUP_DEFAULT"
  441. end
  442. end
  443. execute_k_any (c: INTEGER): STRING
  444. do
  445. if attached cb_k_any as int_cb then
  446. Result := int_cb.item([Current, c])
  447. else
  448. Result := "IUP_DEFAULT"
  449. end
  450. end
  451. execute_help
  452. do
  453. if attached cb_help as int_cb then
  454. int_cb.call([Current])
  455. end
  456. end
  457. execute_valuechanged: STRING
  458. do
  459. if attached cb_valuechanged as int_cb then
  460. Result := int_cb.item([Current])
  461. else
  462. Result := "IUP_DEFAULT"
  463. end
  464. end
  465. feature {NONE}
  466. -- For callbacks
  467. cb_map: detachable FUNCTION[TUPLE[IUP_VAL], STRING]
  468. cb_unmap: detachable FUNCTION[TUPLE[IUP_VAL], STRING]
  469. cb_destroy: detachable FUNCTION[TUPLE[IUP_VAL], STRING]
  470. cb_getfocus: detachable FUNCTION[TUPLE[IUP_VAL], STRING]
  471. cb_killfocus: detachable FUNCTION[TUPLE[IUP_VAL], STRING]
  472. cb_enterwindow: detachable FUNCTION[TUPLE[IUP_VAL], STRING]
  473. cb_leavewindow: detachable FUNCTION[TUPLE[IUP_VAL], STRING]
  474. cb_k_any: detachable FUNCTION[TUPLE[IUP_VAL, INTEGER], STRING]
  475. cb_help: detachable PROCEDURE[TUPLE[IUP_VAL]]
  476. cb_valuechanged: detachable FUNCTION[TUPLE[IUP_VAL], STRING]
  477. -- Internal
  478. int_val(orientation: POINTER): POINTER
  479. external
  480. "C inline use %"eiffel-iup.h%""
  481. alias
  482. "return IupVal ($orientation);"
  483. end
  484. end
  485. -- The MIT License (MIT)
  486. -- Copyright (c) 2016, 2017, 2019, 2020 by German A. Arias
  487. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  488. -- of this software and associated documentation files (the "Software"), to deal
  489. -- in the Software without restriction, including without limitation the rights
  490. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  491. -- copies of the Software, and to permit persons to whom the Software is
  492. -- furnished to do so, subject to the following conditions:
  493. --
  494. -- The above copyright notice and this permission notice shall be included in
  495. -- all copies or substantial portions of the Software.
  496. --
  497. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  498. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  499. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  500. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  501. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  502. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  503. -- SOFTWARE.