iup_color_bar.e 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. class IUP_COLOR_BAR
  2. -- Creates a color palette to enable a color selection from several samples.
  3. -- It can select one or two colors. The primary color is selected with the
  4. -- left mouse button, and the secondary color is selected with the right mouse
  5. -- button. You can double click a cell to change its color and you can double
  6. -- click the preview area to switch between primary and secondary colors.
  7. --
  8. -- This is an additional control that depends on the CD library.
  9. --
  10. -- It inherits from IUP_CANVAS.
  11. --
  12. -- NOTES
  13. --
  14. -- When the control has the focus the keyboard can be used to change the
  15. -- colors and activate the callbacks. Use the arrow keys to move from cell to
  16. -- cell, Home goes to the first cell, End goes to the last cell. Space will
  17. -- activate the SELECT callback for the primary color, Ctrl+Space will
  18. -- activate the SELECT callback for the secondary color. Shift+Space will
  19. -- activate the EXTENTED callback. Shift+Enter will activate the CELL callback.
  20. inherit
  21. IUP_CONTROLS
  22. redefine
  23. set_size,
  24. execute_cell,
  25. execute_extended,
  26. execute_select,
  27. execute_switch
  28. end
  29. create {ANY}
  30. color_bar
  31. feature {ANY}
  32. color_bar
  33. local
  34. a_color_bar: POINTER
  35. do
  36. a_color_bar := int_color_bar
  37. set_widget(a_color_bar)
  38. end
  39. -- Attributes
  40. set_bufferize (state: BOOLEAN)
  41. -- (non inheritable): Disables the automatic redrawing of the control,
  42. -- so many attributes can be changed without many redraws.
  43. -- Default: "False". When set to "False" the control is redrawn.
  44. do
  45. iup_open.set_attribute(Current, "BUFFERIZE", boolean_to_yesno(state))
  46. end
  47. set_cell_n_color (n, red, green, blue: INTEGER)
  48. -- Set the color of the "n" cell. "n" can be from 0 to number of
  49. -- cells - 1.
  50. do
  51. iup_open.set_attribute_id(Current, "CELL", n, rgb_to_string(red, green, blue))
  52. end
  53. get_cell_n_color (n: INTEGER): TUPLE[INTEGER, INTEGER, INTEGER]
  54. -- Get the color of the "n" cell. "n" can be from 0 to number of
  55. -- cells - 1.
  56. local
  57. cl: STRING
  58. do
  59. cl := iup_open.get_attribute_id(Current, "CELL", n)
  60. Result := string_to_rgb(cl)
  61. end
  62. set_number_of_cells (n: INTEGER)
  63. -- (non inheritable): Contains the number of color cells. Default: "16".
  64. -- The maximum number of colors is 256. The default colors use the
  65. -- same set of IUP_IMAGE.
  66. require
  67. n > 0
  68. do
  69. iup_open.set_attribute(Current, "NUM_CELLS", n.out)
  70. end
  71. get_count: INTEGER
  72. -- Returns the number of color cells.
  73. do
  74. Result := iup_open.get_int(Current, "COUNT")
  75. end
  76. set_number_of_parts (num: INTEGER)
  77. -- (non inheritable): Set the number of lines or columns. Default: "1".
  78. require
  79. num > 0
  80. do
  81. iup_open.set_attribute(Current, "NUM_PARTS", num.out)
  82. end
  83. get_number_of_parts: INTEGER
  84. -- Returns the number of lines or columns.
  85. do
  86. Result := iup_open.get_int(Current, "NUM_PARTS")
  87. end
  88. set_horizontal_orientation
  89. do
  90. iup_open.set_attribute(Current, "ORIENTATION", "HORIZONTAL")
  91. end
  92. set_vertical_orientation
  93. -- This is the default value.
  94. do
  95. iup_open.set_attribute(Current, "ORIENTATION", "VERTICAL")
  96. end
  97. is_horizontal: BOOLEAN
  98. local
  99. str: STRING
  100. do
  101. str := iup_open.get_attribute(Current, "ORIENTATION")
  102. if str.is_equal("HORIZONTAL") then
  103. Result := True
  104. else
  105. Result := False
  106. end
  107. end
  108. is_vertical: BOOLEAN
  109. local
  110. str: STRING
  111. do
  112. str := iup_open.get_attribute(Current, "ORIENTATION")
  113. if str.is_equal("VERTICAL") then
  114. Result := True
  115. else
  116. Result := False
  117. end
  118. end
  119. set_preview_size (width, height: INTEGER)
  120. -- (non inheritable): Fixes the size of the preview area in pixels.
  121. -- The default size is dynamically calculated from the size of the
  122. -- control. The size is reset to the default when "set_show_preview"
  123. -- is set to False.
  124. require
  125. width > 0
  126. height > 0
  127. local
  128. size: STRING
  129. do
  130. size := width.out
  131. size.append_string("x")
  132. size.append_string(height.out)
  133. iup_open.set_attribute(Current, "PREVIEW_SIZE", size)
  134. end
  135. set_show_preview (state: BOOLEAN)
  136. -- Controls the display of the preview area. Default: "True".
  137. do
  138. iup_open.set_attribute(Current, "SHOW_PREVIEW", boolean_to_yesno(state))
  139. end
  140. set_show_secondary (state: BOOLEAN)
  141. -- Controls the existence of a secondary color selection.
  142. -- Default: "False".
  143. do
  144. iup_open.set_attribute(Current, "SHOW_SECONDARY", boolean_to_yesno(state))
  145. end
  146. set_size (width, height: INTEGER)
  147. -- Specifies the element User size, and returns the Current size, in
  148. -- units proportional to the size of a character.
  149. -- You must define "size" or "raster size".
  150. do
  151. Precursor (width, height)
  152. end
  153. set_primary_cell (index: INTEGER)
  154. -- (non inheritable): Set the index of the primary color.
  155. -- Default "0" (black).
  156. require
  157. index >= 0
  158. do
  159. iup_open.set_attribute(Current, "PRIMARY_CELL", index.out)
  160. end
  161. get_primary_cell: INTEGER
  162. -- Return the index of the primary color.
  163. do
  164. Result := iup_open.get_int(Current, "PRIMARY_CELL")
  165. end
  166. set_seconday_cell (index: INTEGER)
  167. -- (non inheritable): Set the index of the secondary color.
  168. -- Default "15" (white).
  169. require
  170. index >= 0
  171. do
  172. iup_open.set_attribute(Current, "SECONDARY_CELL", index.out)
  173. end
  174. get_secondary_cell: INTEGER
  175. -- Return the index of the secondary color.
  176. do
  177. Result := iup_open.get_int(Current, "SECONDARY_CELL")
  178. end
  179. set_squared (state: BOOLEAN)
  180. -- Controls the aspect ratio of the color cells. Non square cells
  181. -- expand equally to occupy all of the control area. Default: "True".
  182. do
  183. iup_open.set_attribute(Current, "SQUARED", boolean_to_yesno(state))
  184. end
  185. set_shadowed (state: BOOLEAN)
  186. -- Controls the 3D effect of the color cells. Default: "True".
  187. do
  188. iup_open.set_attribute(Current, "SHADOWED", boolean_to_yesno(state))
  189. end
  190. set_transparency (red, green, blue: INTEGER)
  191. -- Contains a color that will be not rendered in the color palette.
  192. -- The color cell will have a white and gray chess pattern. It can
  193. -- be used to create a palette with less colors than the number of cells.
  194. do
  195. iup_open.set_attribute(Current, "TRANSPARENCY", rgb_to_string(red, green, blue))
  196. end
  197. -- Callbacks
  198. set_cb_cell (act: detachable FUNCTION[TUPLE[IUP_COLOR_BAR, INTEGER], TUPLE[INTEGER, INTEGER, INTEGER]])
  199. -- Called when the user double clicks a color cell to change its value.
  200. --
  201. -- ih: identifier of the element that activated the event.
  202. -- cell: index of the selected cell. If the user double click a preview
  203. -- cell, the respective index is returned.
  204. --
  205. -- Returns: a tuple with new color (r, g, b) integers, or void to ignore
  206. -- the change. By default nothing is changed.
  207. local
  208. operation: INTEGER
  209. do
  210. cb_cell := act
  211. if cb_cell /= Void then
  212. operation := 1
  213. else
  214. operation := 0
  215. end
  216. iup_open.set_callback (Current, "CELL_CB", "NONEEDED", operation)
  217. end
  218. set_cb_extended (act: detachable FUNCTION[TUPLE[IUP_COLOR_BAR, INTEGER], STRING])
  219. -- Called when the user right click a cell with the Shift key pressed.
  220. -- It is independent of the "set_show_secondary" attribute.
  221. local
  222. operation: INTEGER
  223. do
  224. cb_extended := act
  225. if cb_extended /= Void then
  226. operation := 1
  227. else
  228. operation := 0
  229. end
  230. iup_open.set_callback (Current, "EXTENDED_CB", "NONEEDED", operation)
  231. end
  232. set_cb_select (act: detachable FUNCTION[TUPLE[IUP_COLOR_BAR, INTEGER, INTEGER], STRING])
  233. -- Called when a color is selected. The primary color is selected with
  234. -- the left mouse button, and if existent the secondary is selected
  235. -- with the right mouse button.
  236. --
  237. -- ih: identifier of the element that activated the event.
  238. -- cell: index of the selected cell.
  239. -- type: indicates if the user selected a primary or secondary color.
  240. -- In can be: primary (1) or secondary (2).
  241. --
  242. -- Returns: If "IUP_IGNORE" the selection is not accepted. By default
  243. -- the selection is always accepted.
  244. local
  245. operation: INTEGER
  246. do
  247. cb_select := act
  248. if cb_select /= Void then
  249. operation := 1
  250. else
  251. operation := 0
  252. end
  253. iup_open.set_callback (Current, "SELECT_CB", "NONEEDED", operation)
  254. end
  255. set_cb_switch (act: detachable FUNCTION[TUPLE[IUP_COLOR_BAR, INTEGER, INTEGER], STRING])
  256. -- Called when the user double clicks the preview area outside the
  257. -- preview cells to switch the primary and secondary selections.
  258. -- It is only called if "set_show_secondary" is True.
  259. --
  260. -- ih: identifier of the element that activated the event.
  261. -- prim_cell: index of the actual primary cell.
  262. -- sec_cell: index of the actual secondary cell.
  263. --
  264. -- Returns: If "IUP_IGNORE" the switch is not accepted. By default
  265. -- the switch is always accepted.
  266. local
  267. operation: INTEGER
  268. do
  269. cb_switch := act
  270. if cb_switch /= Void then
  271. operation := 1
  272. else
  273. operation := 0
  274. end
  275. iup_open.set_callback (Current, "SWITCH_CB", "NONEEDED", operation)
  276. end
  277. feature {IUP}
  278. execute_cell (cell: INTEGER): TUPLE[INTEGER, INTEGER, INTEGER]
  279. do
  280. if attached cb_cell as int_cb then
  281. Result := int_cb.item([Current, cell])
  282. else
  283. Result := [0, 0, 0]
  284. end
  285. end
  286. execute_extended (cell: INTEGER): STRING
  287. do
  288. if attached cb_extended as int_cb then
  289. Result := int_cb.item([Current, cell])
  290. else
  291. Result := "IUP_DEFAULT"
  292. end
  293. end
  294. execute_select (cell, type: INTEGER): STRING
  295. local
  296. i: INTEGER
  297. do
  298. if attached cb_select as int_cb then
  299. i := -1*type
  300. Result := int_cb.item([Current, cell, i])
  301. else
  302. Result := "IUP_DEFAULT"
  303. end
  304. end
  305. execute_switch (prim_cell, sec_cell: INTEGER): STRING
  306. do
  307. if attached cb_switch as int_cb then
  308. Result := int_cb.item([Current, prim_cell, sec_cell])
  309. else
  310. Result := "IUP_DEFAULT"
  311. end
  312. end
  313. feature {NONE}
  314. cb_cell: detachable FUNCTION[TUPLE[IUP_COLOR_BAR, INTEGER], TUPLE[INTEGER, INTEGER, INTEGER]]
  315. cb_extended: detachable FUNCTION[TUPLE[IUP_COLOR_BAR, INTEGER], STRING]
  316. cb_select: detachable FUNCTION[TUPLE[IUP_COLOR_BAR, INTEGER, INTEGER], STRING]
  317. cb_switch: detachable FUNCTION[TUPLE[IUP_COLOR_BAR, INTEGER, INTEGER], STRING]
  318. -- Internal
  319. int_color_bar: POINTER
  320. external
  321. "C inline use %"eiffel-iup.h%""
  322. alias
  323. "return IupColorbar();"
  324. end
  325. end
  326. -- The MIT License (MIT)
  327. -- Copyright (c) 2016, 2019, 2020 by German A. Arias
  328. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  329. -- of this software and associated documentation files (the "Software"), to deal
  330. -- in the Software without restriction, including without limitation the rights
  331. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  332. -- copies of the Software, and to permit persons to whom the Software is
  333. -- furnished to do so, subject to the following conditions:
  334. --
  335. -- The above copyright notice and this permission notice shall be included in
  336. -- all copies or substantial portions of the Software.
  337. --
  338. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  339. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  340. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  341. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  342. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  343. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  344. -- SOFTWARE.