iup_cells.e 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. class IUP_CELLS
  2. -- Creates a grid widget (set of cells) that enables several
  3. -- application-specific drawing, such as: chess tables, tiles editors, degrade
  4. -- scales, drawable spreadsheets and so forth.
  5. --
  6. -- This element is mostly based on application callbacks functions that
  7. -- determine the number of cells (rows and columns), their appearance and
  8. -- interaction. This mechanism offers full flexibility to applications, but
  9. -- requires programmers attention to avoid infinite loops inside this
  10. -- functions. Using callbacks, cells can be also grouped to form major or
  11. -- hierarchical elements, such as headers, footers etc. This callback approach
  12. -- was intentionally chosen to allow all cells to be dynamically and directly
  13. -- changed based on application's data structures. Since the size of each cell
  14. -- is given by the application the size of the control also must be given using
  15. -- size or raster_size attributes.
  16. --
  17. -- It inherits from IUP_CANVAS.
  18. inherit
  19. IUP_CONTROLS
  20. rename
  21. canvas as cells
  22. redefine
  23. cells,
  24. get_cd_canvas,
  25. execute_draw,
  26. execute_height,
  27. execute_hspan,
  28. execute_mouseclick,
  29. execute_mousemotion,
  30. execute_ncols,
  31. execute_nlines,
  32. execute_scrolling,
  33. execute_vspan,
  34. execute_width
  35. end
  36. create {ANY}
  37. cells
  38. feature {ANY}
  39. cells
  40. local
  41. a_cells: POINTER
  42. do
  43. a_cells := int_cells
  44. set_widget(a_cells)
  45. end
  46. -- Attributes
  47. set_boxed (state: BOOLEAN)
  48. -- Determines if the bounding cells' regions should be drawn with black
  49. -- lines. Default: "True". If the "span" attributes are used, set this
  50. -- attribute to "False" to avoid grid drawing over spanned cells.
  51. do
  52. iup_open.set_attribute(Current, "BOXED", boolean_to_yesno(state))
  53. end
  54. set_bufferize (state: BOOLEAN)
  55. -- Disables the automatic redrawing of the control, so many attributes
  56. -- can be changed without many redraws. When set to "False" the control
  57. -- is redrawn. When REPAINT attribute is set, BUFFERIZE is automatically
  58. -- set to "False". Default: "False".
  59. do
  60. iup_open.set_attribute(Current, "BUFFERIZE", boolean_to_yesno(state))
  61. end
  62. get_cd_canvas: CD_IUP
  63. -- (non inheritable): Returns the internal IUP CD canvas. This
  64. -- attribute should be used only in specific cases and by experienced
  65. -- CD programmers.
  66. local
  67. p: POINTER
  68. do
  69. p := iup_open.get_attribute_ihandle(Current, "CANVAS")
  70. Result := internal_cd(p)
  71. end
  72. set_clipped (state: BOOLEAN)
  73. -- Determines if, before cells drawing, each bounding region should be
  74. -- clipped. This attribute should be changed in few specific cases.
  75. -- Default: "True".
  76. do
  77. iup_open.set_attribute(Current, "CLIPPED", boolean_to_yesno(state))
  78. end
  79. get_first_column: INTEGER
  80. -- Returns the number of the first visible column.
  81. local
  82. str: STRING
  83. do
  84. str := iup_open.get_attribute(Current, "FIRST_COL")
  85. if str.is_integer then
  86. Result := str.to_integer
  87. end
  88. end
  89. get_first_line: INTEGER
  90. -- Returns the number of the first visible line.
  91. local
  92. str: STRING
  93. do
  94. str := iup_open.get_attribute(Current, "FIRST_LINE")
  95. if str.is_integer then
  96. Result := str.to_integer
  97. end
  98. end
  99. set_full_visible (line, column: INTEGER)
  100. -- Tries to show completely a specific cell (considering any vertical or
  101. -- horizontal header or scrollbar position).
  102. local
  103. value: STRING
  104. do
  105. create value.make_from_string(line.out)
  106. value.append_string(":")
  107. value.append_string(column.out)
  108. iup_open.set_attribute(Current, "FULL_VISIBLE", value)
  109. end
  110. -- Skip image_canvas
  111. get_limits (line, column: INTEGER): TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
  112. -- Returns the limits of a given cell.
  113. local
  114. str: STRING
  115. do
  116. str := iup_open.get_attribute_id2 (Current, "LIMITS", line, column)
  117. Result := components_of_limits(str)
  118. end
  119. set_non_scrollable_lines (number: INTEGER)
  120. -- Determines the number of non-scrollable lines (vertical headers) that
  121. -- should always be visible despite the vertical scrollbar position. It
  122. -- can be any non-negative integer value. Default: "0".
  123. require
  124. number >= 0
  125. do
  126. iup_open.set_attribute(Current, "NON_SCROLLABLE_LINES",
  127. number.out)
  128. end
  129. set_non_scrollable_columns (number: INTEGER)
  130. -- Determines the number of non-scrollable columns (horizontal headers)
  131. -- that should always be visible despite the horizontal scrollbar
  132. -- position. It can be any non-negative integer value. Default: "0".
  133. require
  134. number >= 0
  135. do
  136. iup_open.set_attribute(Current, "NON_SCROLLABLE_COLS",
  137. number.out)
  138. end
  139. set_origin (line, column: INTEGER)
  140. -- Sets the first visible line and column positions.
  141. local
  142. value: STRING
  143. do
  144. create value.make_from_string(line.out)
  145. value.append_string(":")
  146. value.append_string(column.out)
  147. iup_open.set_attribute(Current, "ORIGIN", value)
  148. end
  149. set_scrollbar (state: BOOLEAN)
  150. -- Default: "True".
  151. do
  152. iup_open.set_attribute(Current, "SCROLLBAR", boolean_to_yesno(state))
  153. end
  154. -- Operations
  155. repaint
  156. -- Provokes the control to be redrawn.
  157. do
  158. iup_open.set_attribute(Current, "REPAINT", "Yes")
  159. end
  160. -- Callbacks
  161. set_cb_draw (act: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, CD_IUP], STRING])
  162. -- Called when a specific cell needs to be redrawn.
  163. --
  164. -- ih: identifier of the element that activated the event.
  165. -- line, column: the grid position inside the control that is being
  166. -- redrawn, in grid coordinates.
  167. -- xmin, xmax, ymin, ymax: the raster bounding box of the redrawn cells,
  168. -- where the application can use CD functions to draw anything. If
  169. -- the attribute IUP_CLIPPED is set (the default), all CD graphical
  170. -- primitives is clipped to the bounding region.
  171. -- canvas: internal canvas CD used to draw the cells.
  172. --
  173. -- Returns: "IUP_DEFAULT", "IUP_CONTINUE" or "IUP_IGNORE"
  174. local
  175. operation: INTEGER
  176. do
  177. cb_draw := act
  178. if cb_draw /= Void then
  179. operation := 1
  180. else
  181. operation := 0
  182. end
  183. iup_open.set_callback (Current, "DRAW_CB", "NONEEDED", operation)
  184. end
  185. set_cb_height (act: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER], INTEGER])
  186. -- Called when the controls needs to know a (eventually new) line height.
  187. --
  188. -- ih: identifier of the element that activated the event.
  189. -- line: the line index
  190. --
  191. -- Returns: an integer that specifies the desired height (in pixels).
  192. -- Default is 30 pixels.
  193. local
  194. operation: INTEGER
  195. do
  196. cb_height := act
  197. if cb_height /= Void then
  198. operation := 1
  199. else
  200. operation := 0
  201. end
  202. iup_open.set_callback (Current, "HEIGHT_CB", "NONEEDED", operation)
  203. end
  204. set_cb_horizontal_span (act: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER, INTEGER], INTEGER])
  205. -- Called when the control needs to know if a cell should be horizontally
  206. -- spanned.
  207. --
  208. -- ih: identifier of the element that activated the event.
  209. -- line, column: the line and column indexes (in grid coordinates)
  210. --
  211. -- Returns: an integer that specifies the desired span.
  212. -- Default is 1 (no span).
  213. local
  214. operation: INTEGER
  215. do
  216. cb_hspan := act
  217. if cb_hspan /= Void then
  218. operation := 1
  219. else
  220. operation := 0
  221. end
  222. iup_open.set_callback (Current, "HSPAN_CB", "NONEEDED", operation)
  223. end
  224. set_cb_mouse_click (act: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, STRING], STRING])
  225. -- Same as "set_cb_button" at IUP_CANVAS callback with two additional
  226. -- parameters (the third and fourth integers):
  227. --
  228. -- IUP_CELLS: identifies the element that activated the event.
  229. -- button: as in "set_cb_button"
  230. -- pressed: as in "set_cb_button"
  231. -- line, column: the grid position in the control where the event has
  232. -- occurred, in grid coordinates.
  233. -- x, y: as in "set_cb_button"
  234. -- status: as in "set_cb_button"
  235. local
  236. operation: INTEGER
  237. do
  238. cb_mouseclick := act
  239. if cb_mouseclick /= Void then
  240. operation := 1
  241. else
  242. operation := 0
  243. end
  244. iup_open.set_callback (Current, "MOUSECLICK_CB", "NONEEDED", operation)
  245. end
  246. set_cb_mouse_motion (act: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER, INTEGER, INTEGER, INTEGER, STRING], STRING])
  247. -- Called when the mouse moves over the control.
  248. --
  249. -- Same as the "set_cb_motion" IUP_CANVAS callback with two additional
  250. -- parameters:
  251. --
  252. -- line, column: the grid position in the control where the event has
  253. -- occurred, in grid coordinates.
  254. local
  255. operation: INTEGER
  256. do
  257. cb_mousemotion := act
  258. if cb_mousemotion /= Void then
  259. operation := 1
  260. else
  261. operation := 0
  262. end
  263. iup_open.set_callback (Current, "MOUSEMOTION_CB", "NONEEDED", operation)
  264. end
  265. set_cb_number_of_columns (act: detachable FUNCTION[TUPLE[IUP_CELLS], INTEGER])
  266. -- Called when then controls needs to know its number of columns.
  267. --
  268. -- ih: identifier of the element that activated the event.
  269. --
  270. -- Returns: an integer that specifies the number of columns.
  271. -- Default is 10 columns.
  272. local
  273. operation: INTEGER
  274. do
  275. cb_ncols := act
  276. if cb_ncols /= Void then
  277. operation := 1
  278. else
  279. operation := 0
  280. end
  281. iup_open.set_callback (Current, "NCOLS_CB", "NONEEDED", operation)
  282. end
  283. set_cb_number_of_lines (act: detachable FUNCTION[TUPLE[IUP_CELLS], INTEGER])
  284. -- Called when then controls needs to know its number of lines.
  285. --
  286. -- ih: identifier of the element that activated the event.
  287. --
  288. -- Returns: an integer that specifies the number of lines.
  289. -- Default is 10 lines.
  290. local
  291. operation: INTEGER
  292. do
  293. cb_nlines := act
  294. if cb_nlines /= Void then
  295. operation := 1
  296. else
  297. operation := 0
  298. end
  299. iup_open.set_callback (Current, "NLINES_CB", "NONEEDED", operation)
  300. end
  301. set_cb_scrolling (act: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER, INTEGER], STRING])
  302. -- Called when the scrollbars are activated.
  303. --
  304. -- ih: identifier of the element that activated the event.
  305. -- line, column: the first visible line and column indexes (in grid
  306. -- coordinates)
  307. --
  308. -- Returns: If "IUP_IGNORE" the cell is not redrawn. By default the cell
  309. -- is always redrawn.
  310. local
  311. operation: INTEGER
  312. do
  313. cb_scrolling := act
  314. if cb_scrolling /= Void then
  315. operation := 1
  316. else
  317. operation := 0
  318. end
  319. iup_open.set_callback (Current, "SCROLLING_CB", "NONEEDED", operation)
  320. end
  321. set_cb_vertical_span (act: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER, INTEGER], INTEGER])
  322. -- Called when the control needs to know if a cell should be vertically
  323. -- spanned.
  324. --
  325. -- ih: identifier of the element that activated the event.
  326. -- line, column: the line and column indexes (in grid coordinates)
  327. --
  328. -- Returns: an integer that specifies the desired span.
  329. -- Default is 1 (no span).
  330. local
  331. operation: INTEGER
  332. do
  333. cb_vspan := act
  334. if cb_vspan /= Void then
  335. operation := 1
  336. else
  337. operation := 0
  338. end
  339. iup_open.set_callback (Current, "VSPAN_CB", "NONEEDED", operation)
  340. end
  341. set_cb_width (act: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER], INTEGER])
  342. -- Called when the controls needs to know the column width.
  343. --
  344. -- ih: identifier of the element that activated the event.
  345. -- column: the column index
  346. --
  347. -- Returns: an integer that specifies the desired width (in pixels).
  348. -- Default is 60 pixels.
  349. local
  350. operation: INTEGER
  351. do
  352. cb_width := act
  353. if cb_width /= Void then
  354. operation := 1
  355. else
  356. operation := 0
  357. end
  358. iup_open.set_callback (Current, "WIDTH_CB", "NONEEDED", operation)
  359. end
  360. feature {IUP}
  361. execute_draw (line, column, xmin, xmax, ymin, ymax: INTEGER; canvas: POINTER): STRING
  362. do
  363. if attached cb_draw as int_cb then
  364. Result := int_cb.item([Current, line, column, xmin, xmax, ymin, ymax, internal_cd(canvas)])
  365. else
  366. Result := "IUP_DEFAULT"
  367. end
  368. end
  369. execute_height (line: INTEGER): INTEGER
  370. do
  371. if attached cb_height as int_cb then
  372. Result := int_cb.item([Current, line])
  373. else
  374. Result := 0
  375. end
  376. end
  377. execute_hspan (line, column: INTEGER): INTEGER
  378. do
  379. if attached cb_hspan as int_cb then
  380. Result := int_cb.item([Current, line, column])
  381. else
  382. Result := 0
  383. end
  384. end
  385. execute_mouseclick (btn, prd, l, c, x, y: INTEGER; s: STRING): STRING
  386. do
  387. if attached cb_mouseclick as int_cb then
  388. Result := int_cb.item([Current, btn, prd, l, c, x, y, s])
  389. else
  390. Result := "IUP_DEFAULT"
  391. end
  392. end
  393. execute_mousemotion (l, c, x, y: INTEGER; r: STRING): STRING
  394. do
  395. if attached cb_mousemotion as int_cb then
  396. Result := int_cb.item([Current, l, c, x, y, r])
  397. else
  398. Result := "IUP_DEFAULT"
  399. end
  400. end
  401. execute_ncols: INTEGER
  402. do
  403. if attached cb_ncols as int_cb then
  404. Result := int_cb.item([Current])
  405. else
  406. Result := 0
  407. end
  408. end
  409. execute_nlines: INTEGER
  410. do
  411. if attached cb_nlines as int_cb then
  412. Result := int_cb.item([Current])
  413. else
  414. Result := 0
  415. end
  416. end
  417. execute_scrolling (l, c: INTEGER): STRING
  418. do
  419. if attached cb_scrolling as int_cb then
  420. Result := int_cb.item([Current, l, c])
  421. else
  422. Result := "IUP_DEFAULT"
  423. end
  424. end
  425. execute_vspan (l, c: INTEGER): INTEGER
  426. do
  427. if attached cb_vspan as int_cb then
  428. Result := int_cb.item([Current, l, c])
  429. else
  430. Result := 0
  431. end
  432. end
  433. execute_width (c: INTEGER): INTEGER
  434. do
  435. if attached cb_width as int_cb then
  436. Result := int_cb.item([Current, c])
  437. else
  438. Result := 0
  439. end
  440. end
  441. feature {NONE}
  442. cb_draw: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, CD_IUP], STRING]
  443. cb_height: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER], INTEGER]
  444. cb_hspan: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER, INTEGER], INTEGER]
  445. cb_mouseclick: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, STRING], STRING]
  446. cb_mousemotion: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER, INTEGER, INTEGER, INTEGER, STRING], STRING]
  447. cb_ncols: detachable FUNCTION[TUPLE[IUP_CELLS], INTEGER]
  448. cb_nlines: detachable FUNCTION[TUPLE[IUP_CELLS], INTEGER]
  449. cb_scrolling: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER, INTEGER], STRING]
  450. cb_vspan: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER, INTEGER], INTEGER]
  451. cb_width: detachable FUNCTION[TUPLE[IUP_CELLS, INTEGER], INTEGER]
  452. -- Internals
  453. int_cells: POINTER
  454. external
  455. "C inline use %"eiffel-iup.h%""
  456. alias
  457. "return IupCells();"
  458. end
  459. end
  460. -- The MIT License (MIT)
  461. -- Copyright (c) 2016, 2019, 2020 by German A. Arias
  462. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  463. -- of this software and associated documentation files (the "Software"), to deal
  464. -- in the Software without restriction, including without limitation the rights
  465. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  466. -- copies of the Software, and to permit persons to whom the Software is
  467. -- furnished to do so, subject to the following conditions:
  468. --
  469. -- The above copyright notice and this permission notice shall be included in
  470. -- all copies or substantial portions of the Software.
  471. --
  472. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  473. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  474. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  475. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  476. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  477. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  478. -- SOFTWARE.