NTK 1.3.0
Fl_Table.H
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
31#ifndef _FL_TABLE_H
32#define _FL_TABLE_H
33
34#include <sys/types.h>
35#include <string.h> // memcpy
36#ifdef WIN32
37#include <malloc.h> // WINDOWS: malloc/realloc
38#else /*WIN32*/
39#include <stdlib.h> // UNIX: malloc/realloc
40#endif /*WIN32*/
41
42#include <FL/Fl.H>
43#include <FL/Fl_Group.H>
44#include <FL/Fl_Scroll.H>
45#include <FL/Fl_Box.H>
46#include <FL/Fl_Scrollbar.H>
47
181class FL_EXPORT Fl_Table : public Fl_Group {
182public:
196
197private:
198 int _rows, _cols; // total rows/cols
199 int _row_header_w; // width of row header
200 int _col_header_h; // height of column header
201 int _row_position; // last row_position set (not necessarily == toprow!)
202 int _col_position; // last col_position set (not necessarily == leftcol!)
203
204 char _row_header; // row header enabled?
205 char _col_header; // col header enabled?
206 char _row_resize; // row resizing enabled?
207 char _col_resize; // col resizing enabled?
208 int _row_resize_min; // row minimum resizing height (default=1)
209 int _col_resize_min; // col minimum resizing width (default=1)
210
211 // OPTIMIZATION: partial row/column redraw variables
212 int _redraw_toprow;
213 int _redraw_botrow;
214 int _redraw_leftcol;
215 int _redraw_rightcol;
216 Fl_Color _row_header_color;
217 Fl_Color _col_header_color;
218
219 int _auto_drag;
220 int _selecting;
221
222 // An STL-ish vector without templates
223 class FL_EXPORT IntVector {
224 int *arr;
225 unsigned int _size;
226 void init() {
227 arr = NULL;
228 _size = 0;
229 }
230 void copy(int *newarr, unsigned int newsize) {
231 size(newsize);
232 memcpy(arr, newarr, newsize * sizeof(int));
233 }
234 public:
235 IntVector() { init(); } // CTOR
236 ~IntVector() { if ( arr ) free(arr); arr = NULL; } // DTOR
237 IntVector(IntVector&o) { init(); copy(o.arr, o._size); } // COPY CTOR
238 IntVector& operator=(IntVector&o) { // ASSIGN
239 init();
240 copy(o.arr, o._size);
241 return(*this);
242 }
243 int operator[](int x) const { return(arr[x]); }
244 int& operator[](int x) { return(arr[x]); }
245 unsigned int size() { return(_size); }
246 void size(unsigned int count) {
247 if ( count != _size ) {
248 arr = (int*)realloc(arr, count * sizeof(int));
249 _size = count;
250 }
251 }
252 int pop_back() { int tmp = arr[_size-1]; _size--; return(tmp); }
253 void push_back(int val) { unsigned int x = _size; size(_size+1); arr[x] = val; }
254 int back() { return(arr[_size-1]); }
255 };
256
257 IntVector _colwidths; // column widths in pixels
258 IntVector _rowheights; // row heights in pixels
259
260 Fl_Cursor _last_cursor; // last mouse cursor before changed to 'resize' cursor
261
262 // EVENT CALLBACK DATA
263 TableContext _callback_context; // event context
264 int _callback_row, _callback_col; // event row/col
265
266 // handle() state variables.
267 // Put here instead of local statics in handle(), so more
268 // than one Fl_Table can exist without crosstalk between them.
269 //
270 int _resizing_col; // column being dragged
271 int _resizing_row; // row being dragged
272 int _dragging_x; // starting x position for horiz drag
273 int _dragging_y; // starting y position for vert drag
274 int _last_row; // last row we FL_PUSH'ed
275
276 // Redraw single cell
277 void _redraw_cell(TableContext context, int R, int C);
278
279 void _start_auto_drag();
280 void _stop_auto_drag();
281 void _auto_drag_cb();
282 static void _auto_drag_cb2(void *d);
283
284protected:
285 enum ResizeFlag {
286 RESIZE_NONE = 0,
287 RESIZE_COL_LEFT = 1,
288 RESIZE_COL_RIGHT = 2,
289 RESIZE_ROW_ABOVE = 3,
290 RESIZE_ROW_BELOW = 4
291 };
292
293 int table_w, table_h; // table's virtual size (in pixels)
294 int toprow, botrow, leftcol, rightcol; // four corners of viewable table
295
296 // selection
297 int current_row, current_col;
298 int select_row, select_col;
299
300 // OPTIMIZATION: Precomputed scroll positions for the toprow/leftcol
301 int toprow_scrollpos;
302 int leftcol_scrollpos;
303
304 // Dimensions
305 int tix, tiy, tiw, tih; // data table inner dimension xywh
306 int tox, toy, tow, toh; // data table outer dimension xywh
307 int wix, wiy, wiw, wih; // widget inner dimension xywh
308
309 Fl_Scroll *table; // container for child fltk widgets (if any)
310 Fl_Scrollbar *vscrollbar; // vertical scrollbar
311 Fl_Scrollbar *hscrollbar; // horizontal scrollbar
312
313 // Fltk
314 int handle(int e); // fltk handle() override
315
316 // Class maintenance
317 void recalc_dimensions();
318 void table_resized(); // table resized; recalc
319 void table_scrolled(); // table scrolled; recalc
320 void get_bounds(TableContext context, // return x/y/w/h bounds for context
321 int &X, int &Y, int &W, int &H);
322 void change_cursor(Fl_Cursor newcursor); // change mouse cursor to some other shape
323 TableContext cursor2rowcol(int &R, int &C, ResizeFlag &resizeflag);
324 // find r/c given current x/y event
325 int find_cell(TableContext context, // find cell's x/y/w/h given r/c
326 int R, int C, int &X, int &Y, int &W, int &H);
327 int row_col_clamp(TableContext context, int &R, int &C);
328 // clamp r/c to known universe
329
440 virtual void draw_cell(TableContext context, int R=0, int C=0,
441 int X=0, int Y=0, int W=0, int H=0)
442 { } // overridden by deriving class
443
444 long row_scroll_position(int row); // find scroll position of row (in pixels)
445 long col_scroll_position(int col); // find scroll position of col (in pixels)
446
447 int is_fltk_container() { // does table contain fltk widgets?
448 return( Fl_Group::children() > 3 ); // (ie. more than box and 2 scrollbars?)
449 }
450
451 static void scroll_cb(Fl_Widget*,void*); // h/v scrollbar callback
452
453 void damage_zone(int r1, int c1, int r2, int c2, int r3 = 0, int c3 = 0);
454
455 void redraw_range(int toprow, int botrow, int leftcol, int rightcol) {
456 if ( _redraw_toprow == -1 ) {
457 // Initialize redraw range
458 _redraw_toprow = toprow;
459 _redraw_botrow = botrow;
460 _redraw_leftcol = leftcol;
461 _redraw_rightcol = rightcol;
462 } else {
463 // Extend redraw range
464 if ( toprow < _redraw_toprow ) _redraw_toprow = toprow;
465 if ( botrow > _redraw_botrow ) _redraw_botrow = botrow;
466 if ( leftcol < _redraw_leftcol ) _redraw_leftcol = leftcol;
467 if ( rightcol > _redraw_rightcol ) _redraw_rightcol = rightcol;
468 }
469
470 // Indicate partial redraw needed of some cells
472 }
473
474public:
480 Fl_Table(int X, int Y, int W, int H, const char *l=0);
481
486 ~Fl_Table();
487
493 virtual void clear() { rows(0); cols(0); }
494
495 // \todo: add topline(), middleline(), bottomline()
496
502 inline void table_box(Fl_Boxtype val) {
503 table->box(val);
504 table_resized();
505 }
506
510 inline Fl_Boxtype table_box( void ) {
511 return(table->box());
512 }
513
517 virtual void rows(int val); // set/get number of rows
518
522 inline int rows() {
523 return(_rows);
524 }
525
529 virtual void cols(int val); // set/get number of columns
530
534 inline int cols() {
535 return(_cols);
536 }
537
566 inline void visible_cells(int& r1, int& r2, int& c1, int& c2) {
567 r1 = toprow;
568 r2 = botrow;
569 c1 = leftcol;
570 c2 = rightcol;
571 }
572
578 return(_resizing_row != -1 || _resizing_col != -1);
579 }
580
584 inline int row_resize() {
585 return(_row_resize);
586 }
587
594 void row_resize(int flag) { // enable row resizing
595 _row_resize = flag;
596 }
597
601 inline int col_resize() {
602 return(_col_resize);
603 }
604
610 void col_resize(int flag) { // enable col resizing
611 _col_resize = flag;
612 }
613
619 inline int col_resize_min() { // column minimum resizing width
620 return(_col_resize_min);
621 }
622
626 void col_resize_min(int val) {
627 _col_resize_min = ( val < 1 ) ? 1 : val;
628 }
629
633 inline int row_resize_min() { // column minimum resizing width
634 return(_row_resize_min);
635 }
636
642 void row_resize_min(int val) {
643 _row_resize_min = ( val < 1 ) ? 1 : val;
644 }
645
649 inline int row_header() { // set/get row header enable flag
650 return(_row_header);
651 }
652
657 void row_header(int flag) {
658 _row_header = flag;
659 table_resized();
660 redraw();
661 }
662
666 inline int col_header() { // set/get col header enable flag
667 return(_col_header);
668 }
669
674 void col_header(int flag) {
675 _col_header = flag;
676 table_resized();
677 redraw();
678 }
679
683 inline void col_header_height(int height) { // set/get col header height
684 _col_header_h = height;
685 table_resized();
686 redraw();
687 }
688
692 inline int col_header_height() {
693 return(_col_header_h);
694 }
695
699 inline void row_header_width(int width) { // set/get row header width
700 _row_header_w = width;
701 table_resized();
702 redraw();
703 }
704
708 inline int row_header_width() {
709 return(_row_header_w);
710 }
711
715 inline void row_header_color(Fl_Color val) { // set/get row header color
716 _row_header_color = val;
717 redraw();
718 }
719
724 return(_row_header_color);
725 }
726
730 inline void col_header_color(Fl_Color val) { // set/get col header color
731 _col_header_color = val;
732 redraw();
733 }
734
739 return(_col_header_color);
740 }
741
748 void row_height(int row, int height); // set/get row height
749
753 inline int row_height(int row) {
754 return((row<0 || row>=(int)_rowheights.size()) ? 0 : _rowheights[row]);
755 }
756
762 void col_width(int col, int width); // set/get a column's width
763
767 inline int col_width(int col) {
768 return((col<0 || col>=(int)_colwidths.size()) ? 0 : _colwidths[col]);
769 }
770
775 void row_height_all(int height) { // set all row/col heights
776 for ( int r=0; r<rows(); r++ ) {
777 row_height(r, height);
778 }
779 }
780
785 void col_width_all(int width) {
786 for ( int c=0; c<cols(); c++ ) {
787 col_width(c, width);
788 }
789 }
790
794 void row_position(int row); // set/get table's current scroll position
795
799 void col_position(int col);
800
804 int row_position() { // current row position
805 return(_row_position);
806 }
807
811 int col_position() { // current col position
812 return(_col_position);
813 }
814
820 inline void top_row(int row) { // set/get top row (deprecated)
821 row_position(row);
822 }
823
828 inline int top_row() {
829 return(row_position());
830 }
831 int is_selected(int r, int c); // selected cell
832 void get_selection(int &row_top, int &col_left, int &row_bot, int &col_right);
833 void set_selection(int row_top, int col_left, int row_bot, int col_right);
834 int move_cursor(int R, int C);
835
839 void resize(int X, int Y, int W, int H); // fltk resize() override
840 void draw(void); // fltk draw() override
841
842 // This crashes sortapp() during init.
843 // void box(Fl_Boxtype val) {
844 // Fl_Group::box(val);
845 // if ( table ) {
846 // resize(x(), y(), w(), h());
847 // }
848 // }
849 // Fl_Boxtype box(void) const {
850 // return(Fl_Group::box());
851 // }
852
853 // Child group
854 void init_sizes() {
855 table->init_sizes();
856 table->redraw();
857 }
858 void add(Fl_Widget& w) {
859 table->add(w);
860 }
861 void add(Fl_Widget* w) {
862 table->add(w);
863 }
864 void insert(Fl_Widget& w, int n) {
865 table->insert(w,n);
866 }
867 void insert(Fl_Widget& w, Fl_Widget* w2) {
868 table->insert(w,w2);
869 }
870 void remove(Fl_Widget& w) {
871 table->remove(w);
872 }
873 void begin() {
874 table->begin();
875 }
876 void end() {
877 table->end();
878 // HACK: Avoid showing Fl_Scroll; seems to erase screen
879 // causing unnecessary flicker, even if its box() is FL_NO_BOX.
880 //
881 if ( table->children() > 2 ) {
882 table->show();
883 } else {
884 table->hide();
885 }
887 }
888 Fl_Widget * const *array() {
889 return(table->array());
890 }
891
906 Fl_Widget *child(int n) const {
907 return(table->child(n));
908 }
909
918 int children() const {
919 return(table->children()-2); // -2: skip Fl_Scroll's h/v scrollbar widgets
920 }
921 int find(const Fl_Widget *w) const {
922 return(table->find(w));
923 }
924 int find(const Fl_Widget &w) const {
925 return(table->find(w));
926 }
927 // CALLBACKS
928
935 return(_callback_row);
936 }
937
944 return(_callback_col);
945 }
946
953 return(_callback_context);
954 }
955
956 void do_callback(TableContext context, int row, int col) {
957 _callback_context = context;
958 _callback_row = row;
959 _callback_col = col;
961 }
962
963#if FL_DOXYGEN
993#endif
994
995#if FL_DOXYGEN
1073 void callback(Fl_Widget*, void*);
1074#endif
1075};
1076
1077#endif /*_FL_TABLE_H*/
1078
1079//
1080// End of "$Id: Fl_Table.H 8301 2011-01-22 22:40:11Z AlbrechtS $".
1081//
Fl_Cursor
The following constants define the mouse cursors that are available in FLTK.
Definition Enumerations.H:879
unsigned int Fl_Color
an FLTK color value
Definition Enumerations.H:764
@ FL_DAMAGE_CHILD
A child needs to be redrawn.
Definition Enumerations.H:935
Fl_When
These constants determine when a callback is performed.
Definition Enumerations.H:313
Fl_Boxtype
Definition Enumerations.H:472
Fl static class.
void end()
Exactly the same as current(this->parent()).
Definition Fl_Group.cxx:80
void add(Fl_Widget &)
The widget is removed from its current group (if any) and then added to the end of this group.
Definition Fl_Group.cxx:494
int handle(int)
Handles the specified event.
Definition Fl_Group.cxx:150
void insert(Fl_Widget &, int i)
The widget is removed from its current group (if any) and then inserted into this group.
Definition Fl_Group.cxx:461
int children() const
Returns how many child widgets the group has.
Definition Fl_Group.H:79
void draw()
Draws the widget.
Definition Fl_Group.cxx:742
void begin()
Sets the current group so you can build the widget tree by just constructing the widgets.
Definition Fl_Group.cxx:74
void resize(int, int, int, int)
Resizes the Fl_Group widget and all of its children.
Definition Fl_Group.cxx:637
Fl_Widget *const * array() const
Returns a pointer to the array of children.
Definition Fl_Group.cxx:49
int find(const Fl_Widget *) const
Searches the child array for the widget and returns the index.
Definition Fl_Group.cxx:57
void init_sizes()
Resets the internal array of widget sizes and positions.
Definition Fl_Group.cxx:575
void remove(int index)
Removes the widget at index from the group but does not delete it.
Definition Fl_Group.cxx:506
static Fl_Group * current()
Returns the currently active group.
Definition Fl_Group.cxx:89
void col_resize_min(int val)
Returns the current column minimum resize value.
Definition Fl_Table.H:626
void row_height(int row, int height)
Sets the height of the specified row in pixels, and the table is redrawn.
Definition Fl_Table.cxx:171
int is_interactive_resize()
Returns 1 if someone is interactively resizing a row or column.
Definition Fl_Table.H:577
void col_width_all(int width)
Convenience method to set the width of all columns to the same value, in pixels.
Definition Fl_Table.H:785
void row_resize_min(int val)
Sets the current row minimum resize value.
Definition Fl_Table.H:642
void row_header_width(int width)
Sets the row header width to n and causes the screen to redraw.
Definition Fl_Table.H:699
void table_box(Fl_Boxtype val)
Sets the kind of box drawn around the data table, the default being FL_NO_BOX.
Definition Fl_Table.H:502
virtual void draw_cell(TableContext context, int R=0, int C=0, int X=0, int Y=0, int W=0, int H=0)
Subclass should override this method to handle drawing the cells.
Definition Fl_Table.H:440
int row_resize_min()
Returns the current row minimum resize value.
Definition Fl_Table.H:633
void row_header(int flag)
Enables/disables showing the row headers.
Definition Fl_Table.H:657
virtual void clear()
Clears the table to zero rows, zero columns.
Definition Fl_Table.H:493
int row_height(int row)
Returns the current height of the specified row as a value in pixels.
Definition Fl_Table.H:753
void row_height_all(int height)
Convenience method to set the height of all rows to the same value, in pixels.
Definition Fl_Table.H:775
int callback_col()
Returns the current column the event occurred on.
Definition Fl_Table.H:943
void row_header_color(Fl_Color val)
Sets the row header color and causes the screen to redraw.
Definition Fl_Table.H:715
int children() const
Returns the number of children in the table.
Definition Fl_Table.H:918
int callback_row()
Returns the current row the event occurred on.
Definition Fl_Table.H:934
int row_header_width()
Returns the current row header width (in pixels).
Definition Fl_Table.H:708
TableContext callback_context()
Returns the current 'table context'.
Definition Fl_Table.H:952
int col_header_height()
Gets the column header height.
Definition Fl_Table.H:692
TableContext
The context bit flags for Fl_Table related callbacks (eg.
Definition Fl_Table.H:186
@ CONTEXT_COL_HEADER
in the col header
Definition Fl_Table.H:191
@ CONTEXT_TABLE
in a dead zone of table
Definition Fl_Table.H:193
@ CONTEXT_STARTPAGE
before a page is redrawn
Definition Fl_Table.H:188
@ CONTEXT_RC_RESIZE
column or row being resized
Definition Fl_Table.H:194
@ CONTEXT_ROW_HEADER
in the row header
Definition Fl_Table.H:190
@ CONTEXT_ENDPAGE
after a page is redrawn
Definition Fl_Table.H:189
@ CONTEXT_CELL
in one of the cells
Definition Fl_Table.H:192
@ CONTEXT_NONE
no known context
Definition Fl_Table.H:187
int col_header()
Returns if column headers are enabled or not.
Definition Fl_Table.H:666
Fl_Color row_header_color()
Returns the current row header color.
Definition Fl_Table.H:723
int cols()
Get the number of columns in the table.
Definition Fl_Table.H:534
Fl_Table(int X, int Y, int W, int H, const char *l=0)
The constructor for the Fl_Table.
Definition Fl_Table.cxx:102
void col_width(int col, int width)
Sets the width of the specified column in pixels, and the table is redrawn.
Definition Fl_Table.cxx:195
void visible_cells(int &r1, int &r2, int &c1, int &c2)
Returns the range of row and column numbers for all visible and partially visible cells in the table.
Definition Fl_Table.H:566
void top_row(int row)
Sets which row should be at the top of the table, scrolling as necessary, and the table is redrawn.
Definition Fl_Table.H:820
virtual void rows(int val)
Sets the number of rows in the table, and the table is redrawn.
Definition Fl_Table.cxx:598
virtual void cols(int val)
Set the number of columns in the table and redraw.
Definition Fl_Table.cxx:620
int col_width(int col)
Returns the current width of the specified column in pixels.
Definition Fl_Table.H:767
int col_resize()
Returns the current value of this flag.
Definition Fl_Table.H:601
Fl_Boxtype table_box(void)
Returns the current box type used for the data table.
Definition Fl_Table.H:510
void col_header_height(int height)
Sets the height in pixels for column headers and redraws the table.
Definition Fl_Table.H:683
int row_position()
Returns the current row scroll position as a row number.
Definition Fl_Table.H:804
void row_resize(int flag)
Allows/disallows row resizing by the user.
Definition Fl_Table.H:594
Fl_Widget * child(int n) const
Returns the child widget by an index.
Definition Fl_Table.H:906
void col_header(int flag)
Enable or disable column headers.
Definition Fl_Table.H:674
int top_row()
Returns the current top row shown in the table.
Definition Fl_Table.H:828
Fl_Color col_header_color()
Gets the color for column headers.
Definition Fl_Table.H:738
int col_resize_min()
Sets the current column minimum resize value.
Definition Fl_Table.H:619
int row_header()
Returns the value of this flag.
Definition Fl_Table.H:649
void when(Fl_When flags)
The Fl_Widget::when() function is used to set a group of flags, determining when the widget callback ...
void row_position(int row)
Sets the row scroll position to 'row', and causes the screen to redraw.
Definition Fl_Table.cxx:36
void col_header_color(Fl_Color val)
Sets the color for column headers and redraws the table.
Definition Fl_Table.H:730
int col_position()
Returns the current column scroll position as a column number.
Definition Fl_Table.H:811
int row_resize()
Returns the current value of this flag.
Definition Fl_Table.H:584
void col_resize(int flag)
Allows/disallows column resizing by the user.
Definition Fl_Table.H:610
int rows()
Returns the number of rows in the table.
Definition Fl_Table.H:522
void callback(Fl_Widget *, void *)
Callbacks will be called depending on the setting of Fl_Widget::when().
Fl_Widget is the base class for all widgets in FLTK.
Definition Fl_Widget.H:111
virtual void hide()
Makes a widget invisible.
Definition Fl_Widget.cxx:242
void do_callback()
Calls the widget callback.
Definition Fl_Widget.H:838
unsigned int flags() const
Gets the widget flags mask.
Definition Fl_Widget.H:158
void redraw()
Schedules the drawing of the widget.
Definition Fl.cxx:1571
virtual void show()
Makes a widget visible.
Definition Fl_Widget.cxx:230
Fl_Group * parent() const
Returns a pointer to the parent widget.
Definition Fl_Widget.H:253
fl_damage_t damage() const
Returns non-zero if draw() needs to be called.
Definition Fl_Widget.H:891
void size(int W, int H)
Changes the size of the widget.
Definition Fl_Widget.H:340