Fl_Table.H 36 KB

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