You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1082 lines
33KB

  1. //
  2. // "$Id: Fl_Table.H 8301 2011-01-22 22:40:11Z AlbrechtS $"
  3. //
  4. // Fl_Table -- A table widget
  5. //
  6. // Copyright 2002 by Greg Ercolano.
  7. // Copyright (c) 2004 O'ksi'D
  8. //
  9. // This library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU Library General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. // Library General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU Library General Public
  20. // License along with this library; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  22. // USA.
  23. //
  24. // Please report all bugs and problems to "erco at seriss dot com".
  25. //
  26. // TODO:
  27. // o Auto scroll during dragged selection
  28. // o Keyboard navigation (up/down/left/right arrow)
  29. //
  30. #ifndef _FL_TABLE_H
  31. #define _FL_TABLE_H
  32. #include <sys/types.h>
  33. #include <string.h> // memcpy
  34. #ifdef WIN32
  35. #include <malloc.h> // WINDOWS: malloc/realloc
  36. #else /*WIN32*/
  37. #include <stdlib.h> // UNIX: malloc/realloc
  38. #endif /*WIN32*/
  39. #include <FL/Fl.H>
  40. #include <FL/Fl_Group.H>
  41. #include <FL/Fl_Scroll.H>
  42. #include <FL/Fl_Box.H>
  43. #include <FL/Fl_Scrollbar.H>
  44. /**
  45. A table of widgets or other content.
  46. This is the base class for table widgets.
  47. To be useful it must be subclassed and several virtual functions defined.
  48. Normally applications use widgets derived from this widget, and do not use this
  49. widget directly; this widget is usually too low level to be used directly by
  50. applications.
  51. This widget does \em not handle the data in the table. The draw_cell()
  52. method must be overridden by a subclass to manage drawing the contents of
  53. the cells.
  54. This widget can be used in several ways:
  55. - As a custom widget; see examples/table-simple.cxx and test/table.cxx.
  56. Very optimal for even extremely large tables.
  57. - As a table made up of a single FLTK widget instanced all over the table,
  58. simulating a numeric spreadsheet. See examples/table-spreadsheet.cxx and
  59. examples/table-spreadsheet-with-keyboard-nav.cxx. Optimal for large tables.
  60. - As a regular container of FLTK widgets, one widget per cell.
  61. See examples/table-as-container.cxx. \em Not recommended for large tables.
  62. \image html table-simple.png
  63. \image latex table-simple.png "table-simple example" width=6cm
  64. \image html table-as-container.png
  65. \image latex table-as-container.png "table-as-container example" width=6cm
  66. When acting as part of a custom widget, events on the cells and/or headings
  67. generate callbacks when they are clicked by the user. You control when events
  68. are generated based on the setting for Fl_Table::when().
  69. When acting as a container for FLTK widgets, the FLTK widgets maintain
  70. themselves. Although the draw_cell() method must be overridden, its contents
  71. can be very simple. See the draw_cell() code in examples/table-simple.cxx.
  72. The following variables are available to classes deriving from Fl_Table:
  73. \image html table-dimensions.png
  74. \image latex table-dimensions.png "Fl_Table Dimensions" width=6cm
  75. <table border=0>
  76. <tr><td>x()/y()/w()/h()</td>
  77. <td>Fl_Table widget's outer dimension. The outer edge of the border of the
  78. Fl_Table. (Red in the diagram above)</td></tr>
  79. <tr><td>wix/wiy/wiw/wih</td>
  80. <td>Fl_Table widget's inner dimension. The inner edge of the border of the
  81. Fl_Table. eg. if the Fl_Table's box() is FL_NO_BOX, these values are the same
  82. as x()/y()/w()/h(). (Yellow in the diagram above)</td></tr>
  83. <tr><td>tox/toy/tow/toh</td>
  84. <td>The table's outer dimension. The outer edge of the border around the cells,
  85. but inside the row/col headings and scrollbars. (Green in the diagram above)
  86. </td></tr>
  87. <tr><td>tix/tiy/tiw/tih</td>
  88. <td>The table's inner dimension. The inner edge of the border around the cells,
  89. but inside the row/col headings and scrollbars. AKA the table's clip region.
  90. eg. if the table_box() is FL_NO_BOX, these values are the same as
  91. tox/toyy/tow/toh. (Blue in the diagram above)
  92. </td></tr></table>
  93. CORE DEVELOPERS
  94. - Greg Ercolano : 12/16/2002 - initial implementation 12/16/02. Fl_Table, Fl_Table_Row, docs.
  95. - Jean-Marc Lienher : 02/22/2004 - added keyboard nav + mouse selection, and ported Fl_Table into fltk-utf8-1.1.4
  96. OTHER CONTRIBUTORS
  97. - Inspired by the Feb 2000 version of FLVW's Flvw_Table widget. Mucho thanks to those folks.
  98. - Mister Satan : 04/07/2003 - MinGW porting mods, and singleinput.cxx; a cool Fl_Input oriented spreadsheet example
  99. - Marek Paliwoda : 01/08/2003 - Porting mods for Borland
  100. - Ori Berger : 03/16/2006 - Optimizations for >500k rows/cols
  101. LICENSE
  102. Greg added the following license to the original distribution of Fl_Table. He
  103. kindly gave his permission to integrate Fl_Table and Fl_Table_Row into FLTK,
  104. allowing FLTK license to apply while his widgets are part of the library.
  105. If used on its own, this is the license that applies:
  106. \verbatim
  107. Fl_Table License
  108. December 16, 2002
  109. The Fl_Table library and included programs are provided under the terms
  110. of the GNU Library General Public License (LGPL) with the following
  111. exceptions:
  112. 1. Modifications to the Fl_Table configure script, config
  113. header file, and makefiles by themselves to support
  114. a specific platform do not constitute a modified or
  115. derivative work.
  116. The authors do request that such modifications be
  117. contributed to the Fl_Table project - send all
  118. contributions to "erco at seriss dot com".
  119. 2. Widgets that are subclassed from Fl_Table widgets do not
  120. constitute a derivative work.
  121. 3. Static linking of applications and widgets to the
  122. Fl_Table library does not constitute a derivative work
  123. and does not require the author to provide source
  124. code for the application or widget, use the shared
  125. Fl_Table libraries, or link their applications or
  126. widgets against a user-supplied version of Fl_Table.
  127. If you link the application or widget to a modified
  128. version of Fl_Table, then the changes to Fl_Table must be
  129. provided under the terms of the LGPL in sections
  130. 1, 2, and 4.
  131. 4. You do not have to provide a copy of the Fl_Table license
  132. with programs that are linked to the Fl_Table library, nor
  133. do you have to identify the Fl_Table license in your
  134. program or documentation as required by section 6
  135. of the LGPL.
  136. However, programs must still identify their use of Fl_Table.
  137. The following example statement can be included in user
  138. documentation to satisfy this requirement:
  139. [program/widget] is based in part on the work of
  140. the Fl_Table project http://seriss.com/people/erco/fltk/Fl_Table/
  141. \endverbatim
  142. */
  143. class FL_EXPORT Fl_Table : public Fl_Group {
  144. public:
  145. /**
  146. The context bit flags for Fl_Table related callbacks (eg. draw_cell(), callback(), etc)
  147. */
  148. enum TableContext {
  149. CONTEXT_NONE = 0, ///< no known context
  150. CONTEXT_STARTPAGE = 0x01, ///< before a page is redrawn
  151. CONTEXT_ENDPAGE = 0x02, ///< after a page is redrawn
  152. CONTEXT_ROW_HEADER = 0x04, ///< in the row header
  153. CONTEXT_COL_HEADER = 0x08, ///< in the col header
  154. CONTEXT_CELL = 0x10, ///< in one of the cells
  155. CONTEXT_TABLE = 0x20, ///< in a dead zone of table
  156. CONTEXT_RC_RESIZE = 0x40 ///< column or row being resized
  157. };
  158. private:
  159. int _rows, _cols; // total rows/cols
  160. int _row_header_w; // width of row header
  161. int _col_header_h; // height of column header
  162. int _row_position; // last row_position set (not necessarily == toprow!)
  163. int _col_position; // last col_position set (not necessarily == leftcol!)
  164. char _row_header; // row header enabled?
  165. char _col_header; // col header enabled?
  166. char _row_resize; // row resizing enabled?
  167. char _col_resize; // col resizing enabled?
  168. int _row_resize_min; // row minimum resizing height (default=1)
  169. int _col_resize_min; // col minimum resizing width (default=1)
  170. // OPTIMIZATION: partial row/column redraw variables
  171. int _redraw_toprow;
  172. int _redraw_botrow;
  173. int _redraw_leftcol;
  174. int _redraw_rightcol;
  175. Fl_Color _row_header_color;
  176. Fl_Color _col_header_color;
  177. int _auto_drag;
  178. int _selecting;
  179. // An STL-ish vector without templates
  180. class FL_EXPORT IntVector {
  181. int *arr;
  182. unsigned int _size;
  183. void init() {
  184. arr = NULL;
  185. _size = 0;
  186. }
  187. void copy(int *newarr, unsigned int newsize) {
  188. size(newsize);
  189. memcpy(arr, newarr, newsize * sizeof(int));
  190. }
  191. public:
  192. IntVector() { init(); } // CTOR
  193. ~IntVector() { if ( arr ) free(arr); arr = NULL; } // DTOR
  194. IntVector(IntVector&o) { init(); copy(o.arr, o._size); } // COPY CTOR
  195. IntVector& operator=(IntVector&o) { // ASSIGN
  196. init();
  197. copy(o.arr, o._size);
  198. return(*this);
  199. }
  200. int operator[](int x) const { return(arr[x]); }
  201. int& operator[](int x) { return(arr[x]); }
  202. unsigned int size() { return(_size); }
  203. void size(unsigned int count) {
  204. if ( count != _size ) {
  205. arr = (int*)realloc(arr, count * sizeof(int));
  206. _size = count;
  207. }
  208. }
  209. int pop_back() { int tmp = arr[_size-1]; _size--; return(tmp); }
  210. void push_back(int val) { unsigned int x = _size; size(_size+1); arr[x] = val; }
  211. int back() { return(arr[_size-1]); }
  212. };
  213. IntVector _colwidths; // column widths in pixels
  214. IntVector _rowheights; // row heights in pixels
  215. Fl_Cursor _last_cursor; // last mouse cursor before changed to 'resize' cursor
  216. // EVENT CALLBACK DATA
  217. TableContext _callback_context; // event context
  218. int _callback_row, _callback_col; // event row/col
  219. // handle() state variables.
  220. // Put here instead of local statics in handle(), so more
  221. // than one Fl_Table can exist without crosstalk between them.
  222. //
  223. int _resizing_col; // column being dragged
  224. int _resizing_row; // row being dragged
  225. int _dragging_x; // starting x position for horiz drag
  226. int _dragging_y; // starting y position for vert drag
  227. int _last_row; // last row we FL_PUSH'ed
  228. // Redraw single cell
  229. void _redraw_cell(TableContext context, int R, int C);
  230. void _start_auto_drag();
  231. void _stop_auto_drag();
  232. void _auto_drag_cb();
  233. static void _auto_drag_cb2(void *d);
  234. protected:
  235. enum ResizeFlag {
  236. RESIZE_NONE = 0,
  237. RESIZE_COL_LEFT = 1,
  238. RESIZE_COL_RIGHT = 2,
  239. RESIZE_ROW_ABOVE = 3,
  240. RESIZE_ROW_BELOW = 4
  241. };
  242. int table_w, table_h; // table's virtual size (in pixels)
  243. int toprow, botrow, leftcol, rightcol; // four corners of viewable table
  244. // selection
  245. int current_row, current_col;
  246. int select_row, select_col;
  247. // OPTIMIZATION: Precomputed scroll positions for the toprow/leftcol
  248. int toprow_scrollpos;
  249. int leftcol_scrollpos;
  250. // Dimensions
  251. int tix, tiy, tiw, tih; // data table inner dimension xywh
  252. int tox, toy, tow, toh; // data table outer dimension xywh
  253. int wix, wiy, wiw, wih; // widget inner dimension xywh
  254. Fl_Scroll *table; // container for child fltk widgets (if any)
  255. Fl_Scrollbar *vscrollbar; // vertical scrollbar
  256. Fl_Scrollbar *hscrollbar; // horizontal scrollbar
  257. // Fltk
  258. int handle(int e); // fltk handle() override
  259. // Class maintenance
  260. void recalc_dimensions();
  261. void table_resized(); // table resized; recalc
  262. void table_scrolled(); // table scrolled; recalc
  263. void get_bounds(TableContext context, // return x/y/w/h bounds for context
  264. int &X, int &Y, int &W, int &H);
  265. void change_cursor(Fl_Cursor newcursor); // change mouse cursor to some other shape
  266. TableContext cursor2rowcol(int &R, int &C, ResizeFlag &resizeflag);
  267. // find r/c given current x/y event
  268. int find_cell(TableContext context, // find cell's x/y/w/h given r/c
  269. int R, int C, int &X, int &Y, int &W, int &H);
  270. int row_col_clamp(TableContext context, int &R, int &C);
  271. // clamp r/c to known universe
  272. /**
  273. Subclass should override this method to handle drawing the cells.
  274. This method will be called whenever the table is redrawn, once per cell.
  275. Only cells that are completely (or partially) visible will be told to draw.
  276. \p context will be one of the following:
  277. <table border=1>
  278. <tr>
  279. <td>\p Fl_Table::CONTEXT_STARTPAGE</td>
  280. <td>When table, or parts of the table, are about to be redrawn.<br>
  281. Use to initialize static data, such as font selections.<p>
  282. R/C will be zero,<br>
  283. X/Y/W/H will be the dimensions of the table's entire data area.<br>
  284. (Useful for locking a database before accessing; see
  285. also visible_cells())</td>
  286. </tr><tr>
  287. <td>\p Fl_Table::CONTEXT_ENDPAGE</td>
  288. <td>When table has completed being redrawn.<br>
  289. R/C will be zero, X/Y/W/H dimensions of table's data area.<br>
  290. (Useful for unlocking a database after accessing)</td>
  291. </tr><tr>
  292. <td>\p Fl_Table::CONTEXT_ROW_HEADER</td>
  293. <td>Whenever a row header cell needs to be drawn.<br>
  294. R will be the row number of the header being redrawn,<br>
  295. C will be zero,<br>
  296. X/Y/W/H will be the fltk drawing area of the row header in the window </td>
  297. </tr><tr>
  298. <td>\p Fl_Table::CONTEXT_COL_HEADER</td>
  299. <td>Whenever a column header cell needs to be drawn.<br>
  300. R will be zero, <br>
  301. C will be the column number of the header being redrawn,<br>
  302. X/Y/W/H will be the fltk drawing area of the column header in the window </td>
  303. </tr><tr>
  304. <td>\p Fl_Table::CONTEXT_CELL</td>
  305. <td>Whenever a data cell in the table needs to be drawn.<br>
  306. R/C will be the row/column of the cell to be drawn,<br>
  307. X/Y/W/H will be the fltk drawing area of the cell in the window </td>
  308. </tr><tr>
  309. <td>\p Fl_Table::CONTEXT_RC_RESIZE</td>
  310. <td>Whenever table or row/column is resized or scrolled,
  311. either interactively or via col_width() or row_height().<br>
  312. R/C/X/Y/W/H will all be zero.
  313. <p>
  314. Useful for fltk containers that need to resize or move
  315. the child fltk widgets.</td>
  316. </tr>
  317. </table>
  318. \p row and \p col will be set to the row and column number
  319. of the cell being drawn. In the case of row headers, \p col will be \a 0.
  320. In the case of column headers, \p row will be \a 0.
  321. <tt>x/y/w/h</tt> will be the position and dimensions of where the cell
  322. should be drawn.
  323. In the case of custom widgets, a minimal draw_cell() override might
  324. look like the following. With custom widgets it is up to the caller to handle
  325. drawing everything within the dimensions of the cell, including handling the
  326. selection color. Note all clipping must be handled as well; this allows drawing
  327. outside the dimensions of the cell if so desired for 'custom effects'.
  328. \code
  329. // This is called whenever Fl_Table wants you to draw a cell
  330. void MyTable::draw_cell(TableContext context, int R=0, int C=0, int X=0, int Y=0, int W=0, int H=0) {
  331. static char s[40];
  332. sprintf(s, "%d/%d", R, C); // text for each cell
  333. switch ( context ) {
  334. case CONTEXT_STARTPAGE: // Fl_Table telling us its starting to draw page
  335. fl_font(FL_HELVETICA, 16);
  336. return;
  337. case CONTEXT_ROW_HEADER: // Fl_Table telling us it's draw row/col headers
  338. case CONTEXT_COL_HEADER:
  339. fl_push_clip(X, Y, W, H);
  340. {
  341. fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, color());
  342. fl_color(FL_BLACK);
  343. fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER);
  344. }
  345. fl_pop_clip();
  346. return;
  347. case CONTEXT_CELL: // Fl_Table telling us to draw cells
  348. fl_push_clip(X, Y, W, H);
  349. {
  350. // BG COLOR
  351. fl_color( row_selected(R) ? selection_color() : FL_WHITE);
  352. fl_rectf(X, Y, W, H);
  353. // TEXT
  354. fl_color(FL_BLACK);
  355. fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER);
  356. // BORDER
  357. fl_color(FL_LIGHT2);
  358. fl_rect(X, Y, W, H);
  359. }
  360. fl_pop_clip();
  361. return;
  362. default:
  363. return;
  364. }
  365. //NOTREACHED
  366. }
  367. \endcode
  368. */
  369. virtual void draw_cell(TableContext context, int R=0, int C=0,
  370. int X=0, int Y=0, int W=0, int H=0)
  371. { } // overridden by deriving class
  372. long row_scroll_position(int row); // find scroll position of row (in pixels)
  373. long col_scroll_position(int col); // find scroll position of col (in pixels)
  374. int is_fltk_container() { // does table contain fltk widgets?
  375. return( Fl_Group::children() > 3 ); // (ie. more than box and 2 scrollbars?)
  376. }
  377. static void scroll_cb(Fl_Widget*,void*); // h/v scrollbar callback
  378. void damage_zone(int r1, int c1, int r2, int c2, int r3 = 0, int c3 = 0);
  379. void redraw_range(int toprow, int botrow, int leftcol, int rightcol) {
  380. if ( _redraw_toprow == -1 ) {
  381. // Initialize redraw range
  382. _redraw_toprow = toprow;
  383. _redraw_botrow = botrow;
  384. _redraw_leftcol = leftcol;
  385. _redraw_rightcol = rightcol;
  386. } else {
  387. // Extend redraw range
  388. if ( toprow < _redraw_toprow ) _redraw_toprow = toprow;
  389. if ( botrow > _redraw_botrow ) _redraw_botrow = botrow;
  390. if ( leftcol < _redraw_leftcol ) _redraw_leftcol = leftcol;
  391. if ( rightcol > _redraw_rightcol ) _redraw_rightcol = rightcol;
  392. }
  393. // Indicate partial redraw needed of some cells
  394. damage(FL_DAMAGE_CHILD);
  395. }
  396. public:
  397. /**
  398. The constructor for the Fl_Table.
  399. This creates an empty table with no rows or columns,
  400. with headers and row/column resize behavior disabled.
  401. */
  402. Fl_Table(int X, int Y, int W, int H, const char *l=0);
  403. /**
  404. The destructor for the Fl_Table.
  405. Destroys the table and its associated widgets.
  406. */
  407. ~Fl_Table();
  408. /**
  409. Clears the table to zero rows, zero columns.
  410. Same as rows(0); cols(0);
  411. \see rows(int), cols(int)
  412. */
  413. virtual void clear() { rows(0); cols(0); }
  414. // \todo: add topline(), middleline(), bottomline()
  415. /**
  416. Sets the kind of box drawn around the data table,
  417. the default being FL_NO_BOX. Changing this value will cause the table
  418. to redraw.
  419. */
  420. inline void table_box(Fl_Boxtype val) {
  421. table->box(val);
  422. table_resized();
  423. }
  424. /**
  425. Returns the current box type used for the data table.
  426. */
  427. inline Fl_Boxtype table_box( void ) {
  428. return(table->box());
  429. }
  430. /**
  431. Sets the number of rows in the table, and the table is redrawn.
  432. */
  433. virtual void rows(int val); // set/get number of rows
  434. /**
  435. Returns the number of rows in the table.
  436. */
  437. inline int rows() {
  438. return(_rows);
  439. }
  440. /**
  441. Set the number of columns in the table and redraw.
  442. */
  443. virtual void cols(int val); // set/get number of columns
  444. /**
  445. Get the number of columns in the table.
  446. */
  447. inline int cols() {
  448. return(_cols);
  449. }
  450. /**
  451. Returns the range of row and column numbers for all visible
  452. and partially visible cells in the table.
  453. These values can be used e.g. by your draw_cell() routine during
  454. CONTEXT_STARTPAGE to figure out what cells are about to be redrawn
  455. for the purposes of locking the data from a database before it's drawn.
  456. \code
  457. leftcol rightcol
  458. : :
  459. toprow .. .-------------------.
  460. | |
  461. | V I S I B L E |
  462. | |
  463. | T A B L E |
  464. | |
  465. botrow .. '-------------------`
  466. \endcode
  467. e.g. in a table where the visible rows are 5-20, and the
  468. visible columns are 100-120, then those variables would be:
  469. - toprow = 5
  470. - botrow = 20
  471. - leftcol = 100
  472. - rightcol = 120
  473. */
  474. inline void visible_cells(int& r1, int& r2, int& c1, int& c2) {
  475. r1 = toprow;
  476. r2 = botrow;
  477. c1 = leftcol;
  478. c2 = rightcol;
  479. }
  480. /**
  481. Returns 1 if someone is interactively resizing a row or column.
  482. You can currently call this only from within your callback().
  483. */
  484. int is_interactive_resize() {
  485. return(_resizing_row != -1 || _resizing_col != -1);
  486. }
  487. /**
  488. Returns the current value of this flag.
  489. */
  490. inline int row_resize() {
  491. return(_row_resize);
  492. }
  493. /**
  494. Allows/disallows row resizing by the user.
  495. 1=allow interactive resizing, 0=disallow interactive resizing.
  496. Since interactive resizing is done via the row headers,
  497. row_header() must also be enabled to allow resizing.
  498. */
  499. void row_resize(int flag) { // enable row resizing
  500. _row_resize = flag;
  501. }
  502. /**
  503. Returns the current value of this flag.
  504. */
  505. inline int col_resize() {
  506. return(_col_resize);
  507. }
  508. /**
  509. Allows/disallows column resizing by the user.
  510. 1=allow interactive resizing, 0=disallow interactive resizing.
  511. Since interactive resizing is done via the column headers,
  512. \p col_header() must also be enabled to allow resizing.
  513. */
  514. void col_resize(int flag) { // enable col resizing
  515. _col_resize = flag;
  516. }
  517. /**
  518. Sets the current column minimum resize value.
  519. This is used to prevent the user from interactively resizing
  520. any column to be smaller than 'pixels'. Must be a value >=1.
  521. */
  522. inline int col_resize_min() { // column minimum resizing width
  523. return(_col_resize_min);
  524. }
  525. /**
  526. Returns the current column minimum resize value.
  527. */
  528. void col_resize_min(int val) {
  529. _col_resize_min = ( val < 1 ) ? 1 : val;
  530. }
  531. /**
  532. Returns the current row minimum resize value.
  533. */
  534. inline int row_resize_min() { // column minimum resizing width
  535. return(_row_resize_min);
  536. }
  537. /**
  538. Sets the current row minimum resize value.
  539. This is used to prevent the user from interactively resizing
  540. any row to be smaller than 'pixels'. Must be a value >=1.
  541. */
  542. void row_resize_min(int val) {
  543. _row_resize_min = ( val < 1 ) ? 1 : val;
  544. }
  545. /**
  546. Returns the value of this flag.
  547. */
  548. inline int row_header() { // set/get row header enable flag
  549. return(_row_header);
  550. }
  551. /**
  552. Enables/disables showing the row headers. 1=enabled, 0=disabled.
  553. If changed, the table is redrawn.
  554. */
  555. void row_header(int flag) {
  556. _row_header = flag;
  557. table_resized();
  558. redraw();
  559. }
  560. /**
  561. Returns if column headers are enabled or not.
  562. */
  563. inline int col_header() { // set/get col header enable flag
  564. return(_col_header);
  565. }
  566. /**
  567. Enable or disable column headers.
  568. If changed, the table is redrawn.
  569. */
  570. void col_header(int flag) {
  571. _col_header = flag;
  572. table_resized();
  573. redraw();
  574. }
  575. /**
  576. Sets the height in pixels for column headers and redraws the table.
  577. */
  578. inline void col_header_height(int height) { // set/get col header height
  579. _col_header_h = height;
  580. table_resized();
  581. redraw();
  582. }
  583. /**
  584. Gets the column header height.
  585. */
  586. inline int col_header_height() {
  587. return(_col_header_h);
  588. }
  589. /**
  590. Sets the row header width to n and causes the screen to redraw.
  591. */
  592. inline void row_header_width(int width) { // set/get row header width
  593. _row_header_w = width;
  594. table_resized();
  595. redraw();
  596. }
  597. /**
  598. Returns the current row header width (in pixels).
  599. */
  600. inline int row_header_width() {
  601. return(_row_header_w);
  602. }
  603. /**
  604. Sets the row header color and causes the screen to redraw.
  605. */
  606. inline void row_header_color(Fl_Color val) { // set/get row header color
  607. _row_header_color = val;
  608. redraw();
  609. }
  610. /**
  611. Returns the current row header color.
  612. */
  613. inline Fl_Color row_header_color() {
  614. return(_row_header_color);
  615. }
  616. /**
  617. Sets the color for column headers and redraws the table.
  618. */
  619. inline void col_header_color(Fl_Color val) { // set/get col header color
  620. _col_header_color = val;
  621. redraw();
  622. }
  623. /**
  624. Gets the color for column headers.
  625. */
  626. inline Fl_Color col_header_color() {
  627. return(_col_header_color);
  628. }
  629. /**
  630. Sets the height of the specified row in pixels,
  631. and the table is redrawn.
  632. callback() will be invoked with CONTEXT_RC_RESIZE
  633. if the row's height was actually changed, and when() is FL_WHEN_CHANGED.
  634. */
  635. void row_height(int row, int height); // set/get row height
  636. /**
  637. Returns the current height of the specified row as a value in pixels.
  638. */
  639. inline int row_height(int row) {
  640. return((row<0 || row>=(int)_rowheights.size()) ? 0 : _rowheights[row]);
  641. }
  642. /**
  643. Sets the width of the specified column in pixels, and the table is redrawn.
  644. callback() will be invoked with CONTEXT_RC_RESIZE
  645. if the column's width was actually changed, and when() is FL_WHEN_CHANGED.
  646. */
  647. void col_width(int col, int width); // set/get a column's width
  648. /**
  649. Returns the current width of the specified column in pixels.
  650. */
  651. inline int col_width(int col) {
  652. return((col<0 || col>=(int)_colwidths.size()) ? 0 : _colwidths[col]);
  653. }
  654. /**
  655. Convenience method to set the height of all rows to the
  656. same value, in pixels. The screen is redrawn.
  657. */
  658. void row_height_all(int height) { // set all row/col heights
  659. for ( int r=0; r<rows(); r++ ) {
  660. row_height(r, height);
  661. }
  662. }
  663. /**
  664. Convenience method to set the width of all columns to the
  665. same value, in pixels. The screen is redrawn.
  666. */
  667. void col_width_all(int width) {
  668. for ( int c=0; c<cols(); c++ ) {
  669. col_width(c, width);
  670. }
  671. }
  672. /**
  673. Sets the row scroll position to 'row', and causes the screen to redraw.
  674. */
  675. void row_position(int row); // set/get table's current scroll position
  676. /**
  677. Sets the column scroll position to column 'col', and causes the screen to redraw.
  678. */
  679. void col_position(int col);
  680. /**
  681. Returns the current row scroll position as a row number.
  682. */
  683. int row_position() { // current row position
  684. return(_row_position);
  685. }
  686. /**
  687. Returns the current column scroll position as a column number.
  688. */
  689. int col_position() { // current col position
  690. return(_col_position);
  691. }
  692. /**
  693. Sets which row should be at the top of the table,
  694. scrolling as necessary, and the table is redrawn. If the table
  695. cannot be scrolled that far, it is scrolled as far as possible.
  696. */
  697. inline void top_row(int row) { // set/get top row (deprecated)
  698. row_position(row);
  699. }
  700. /**
  701. Returns the current top row shown in the table.
  702. This row may be partially obscured.
  703. */
  704. inline int top_row() {
  705. return(row_position());
  706. }
  707. int is_selected(int r, int c); // selected cell
  708. void get_selection(int &row_top, int &col_left, int &row_bot, int &col_right);
  709. void set_selection(int row_top, int col_left, int row_bot, int col_right);
  710. int move_cursor(int R, int C);
  711. /**
  712. Changes the size of the Fl_Table, causing it to redraw.
  713. */
  714. void resize(int X, int Y, int W, int H); // fltk resize() override
  715. void draw(void); // fltk draw() override
  716. // This crashes sortapp() during init.
  717. // void box(Fl_Boxtype val) {
  718. // Fl_Group::box(val);
  719. // if ( table ) {
  720. // resize(x(), y(), w(), h());
  721. // }
  722. // }
  723. // Fl_Boxtype box(void) const {
  724. // return(Fl_Group::box());
  725. // }
  726. // Child group
  727. void init_sizes() {
  728. table->init_sizes();
  729. table->redraw();
  730. }
  731. void add(Fl_Widget& w) {
  732. table->add(w);
  733. }
  734. void add(Fl_Widget* w) {
  735. table->add(w);
  736. }
  737. void insert(Fl_Widget& w, int n) {
  738. table->insert(w,n);
  739. }
  740. void insert(Fl_Widget& w, Fl_Widget* w2) {
  741. table->insert(w,w2);
  742. }
  743. void remove(Fl_Widget& w) {
  744. table->remove(w);
  745. }
  746. void begin() {
  747. table->begin();
  748. }
  749. void end() {
  750. table->end();
  751. // HACK: Avoid showing Fl_Scroll; seems to erase screen
  752. // causing unnecessary flicker, even if its box() is FL_NO_BOX.
  753. //
  754. if ( table->children() > 2 ) {
  755. table->show();
  756. } else {
  757. table->hide();
  758. }
  759. Fl_Group::current(Fl_Group::parent());
  760. }
  761. Fl_Widget * const *array() {
  762. return(table->array());
  763. }
  764. /**
  765. Returns the child widget by an index.
  766. When using the Fl_Table as a container for FLTK widgets, this method returns
  767. the widget pointer from the internal array of widgets in the container.
  768. Typically used in loops, eg:
  769. \code
  770. for ( int i=0; i<children(); i++ ) {
  771. Fl_Widget *w = child(i);
  772. [..]
  773. }
  774. \endcode
  775. */
  776. Fl_Widget *child(int n) const {
  777. return(table->child(n));
  778. }
  779. /**
  780. Returns the number of children in the table.
  781. When using the Fl_Table as a container for FLTK widgets, this method returns
  782. how many child widgets the table has.
  783. \see child(int)
  784. */
  785. int children() const {
  786. return(table->children()-2); // -2: skip Fl_Scroll's h/v scrollbar widgets
  787. }
  788. int find(const Fl_Widget *w) const {
  789. return(table->find(w));
  790. }
  791. int find(const Fl_Widget &w) const {
  792. return(table->find(w));
  793. }
  794. // CALLBACKS
  795. /**
  796. * Returns the current row the event occurred on.
  797. *
  798. * This function should only be used from within the user's callback function
  799. */
  800. int callback_row() {
  801. return(_callback_row);
  802. }
  803. /**
  804. * Returns the current column the event occurred on.
  805. *
  806. * This function should only be used from within the user's callback function
  807. */
  808. int callback_col() {
  809. return(_callback_col);
  810. }
  811. /**
  812. * Returns the current 'table context'.
  813. *
  814. * This function should only be used from within the user's callback function
  815. */
  816. TableContext callback_context() {
  817. return(_callback_context);
  818. }
  819. void do_callback(TableContext context, int row, int col) {
  820. _callback_context = context;
  821. _callback_row = row;
  822. _callback_col = col;
  823. Fl_Widget::do_callback();
  824. }
  825. #if FL_DOXYGEN
  826. /**
  827. The Fl_Widget::when() function is used to set a group of flags, determining
  828. when the widget callback is called:
  829. <table border=1>
  830. <tr>
  831. <td>\p FL_WHEN_CHANGED</td>
  832. <td>
  833. callback() will be called when rows or columns are resized (interactively or
  834. via col_width() or row_height()), passing CONTEXT_RC_RESIZE via
  835. callback_context().
  836. </td>
  837. </tr><tr>
  838. <td>\p FL_WHEN_RELEASE</td>
  839. <td>
  840. callback() will be called during FL_RELEASE events, such as when someone
  841. releases a mouse button somewhere on the table.
  842. </td>
  843. </tr>
  844. </table>
  845. The callback() routine is sent a TableContext that indicates the context the
  846. event occurred in, such as in a cell, in a header, or elsewhere on the table.
  847. When an event occurs in a cell or header, callback_row() and
  848. callback_col() can be used to determine the row and column. The callback can
  849. also look at the regular fltk event values (ie. Fl::event() and Fl::button())
  850. to determine what kind of event is occurring.
  851. */
  852. void when(Fl_When flags);
  853. #endif
  854. #if FL_DOXYGEN
  855. /**
  856. Callbacks will be called depending on the setting of Fl_Widget::when().
  857. Callback functions should use the following functions to determine the
  858. context/row/column:
  859. * Fl_Table::callback_row() returns current row
  860. * Fl_Table::callback_col() returns current column
  861. * Fl_Table::callback_context() returns current table context
  862. callback_row() and callback_col() will be set to the row and column number the
  863. event occurred on. If someone clicked on a row header, \p col will be \a 0.
  864. If someone clicked on a column header, \p row will be \a 0.
  865. callback_context() will return one of the following:
  866. <table border=1>
  867. <tr><td><tt>Fl_Table::CONTEXT_ROW_HEADER</tt></td>
  868. <td>Someone clicked on a row header. Excludes resizing.</td>
  869. </tr><tr>
  870. <td><tt>Fl_Table::CONTEXT_COL_HEADER</tt></td>
  871. <td>Someone clicked on a column header. Excludes resizing.</td>
  872. </tr><tr>
  873. <td><tt>Fl_Table::CONTEXT_CELL</tt></td>
  874. <td>
  875. Someone clicked on a cell.
  876. To receive callbacks for FL_RELEASE events, you must set
  877. when(FL_WHEN_RELEASE).
  878. </td>
  879. </tr><tr>
  880. <td><tt>Fl_Table::CONTEXT_RC_RESIZE</tt></td>
  881. <td>
  882. Someone is resizing rows/columns either interactively,
  883. or via the col_width() or row_height() API.
  884. Use is_interactive_resize()
  885. to determine interactive resizing.
  886. If resizing a column, R=0 and C=column being resized.
  887. If resizing a row, C=0 and R=row being resized.
  888. NOTE: To receive resize events, you must set when(FL_WHEN_CHANGED).
  889. </td>
  890. </tr>
  891. </table>
  892. \code
  893. class MyTable : public Fl_Table {
  894. [..]
  895. private:
  896. // Handle events that happen on the table
  897. void event_callback2() {
  898. int R = callback_row(), // row where event occurred
  899. C = callback_col(); // column where event occurred
  900. TableContext context = callback_context(); // which part of table
  901. fprintf(stderr, "callback: Row=%d Col=%d Context=%d Event=%d\n",
  902. R, C, (int)context, (int)Fl::event());
  903. }
  904. // Actual static callback
  905. static void event_callback(Fl_Widget*, void* data) {
  906. MyTable *o = (MyTable*)data;
  907. o-&gt;event_callback2();
  908. }
  909. public:
  910. // Constructor
  911. MyTable() {
  912. [..]
  913. table.callback(&event_callback, (void*)this); // setup callback
  914. table.when(FL_WHEN_CHANGED|FL_WHEN_RELEASE); // when to call it
  915. }
  916. };
  917. \endcode
  918. */
  919. void callback(Fl_Widget*, void*);
  920. #endif
  921. };
  922. #endif /*_FL_TABLE_H*/
  923. //
  924. // End of "$Id: Fl_Table.H 8301 2011-01-22 22:40:11Z AlbrechtS $".
  925. //