2 // "$Id: table-simple.cxx 8220 2011-01-08 18:41:47Z greg.ercolano $"
4 // Simple example of using Fl_Table - Greg Ercolano 11/29/2010
6 // Demonstrates the simplest use of Fl_Table possible.
7 // Display a 10x10 array of integers with row/col headers.
8 // No interaction; simple display of data only.
9 // See other examples for more complex interactions with the table.
11 // Copyright 2010 Greg Ercolano.
12 // Copyright 1998-2010 by Bill Spitzak and others.
14 // This library is free software; you can redistribute it and/or
15 // modify it under the terms of the GNU Library General Public
16 // License as published by the Free Software Foundation; either
17 // version 2 of the License, or (at your option) any later version.
19 // This library is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 // Library General Public License for more details.
24 // You should have received a copy of the GNU Library General Public
25 // License along with this library; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 // Please report all bugs and problems on the following page:
31 // http://www.fltk.org/str.php
34 #include <FL/Fl_Double_Window.H>
35 #include <FL/Fl_Table.H>
36 #include <FL/fl_draw.H>
41 // Derive a class from Fl_Table
42 class MyTable
: public Fl_Table
{
44 int data
[MAX_ROWS
][MAX_COLS
]; // data array for cells
46 // Draw the row/col headings
47 // Make this a dark thin upbox with the text inside.
49 void DrawHeader(const char *s
, int X
, int Y
, int W
, int H
) {
50 fl_push_clip(X
,Y
,W
,H
);
51 fl_draw_box(FL_THIN_UP_BOX
, X
,Y
,W
,H
, row_header_color());
53 fl_draw(s
, X
,Y
,W
,H
, FL_ALIGN_CENTER
);
57 // Dark gray text on white background with subtle border
59 void DrawData(const char *s
, int X
, int Y
, int W
, int H
) {
60 fl_push_clip(X
,Y
,W
,H
);
62 fl_color(FL_WHITE
); fl_rectf(X
,Y
,W
,H
);
64 fl_color(FL_GRAY0
); fl_draw(s
, X
,Y
,W
,H
, FL_ALIGN_CENTER
);
66 fl_color(color()); fl_rect(X
,Y
,W
,H
);
69 // Handle drawing table's cells
70 // Fl_Table calls this function to draw each visible cell in the table.
71 // It's up to us to use FLTK's drawing functions to draw the cells the way we want.
73 void draw_cell(TableContext context
, int ROW
=0, int COL
=0, int X
=0, int Y
=0, int W
=0, int H
=0) {
76 case CONTEXT_STARTPAGE
: // before page is drawn..
77 fl_font(FL_HELVETICA
, 16); // set the font for our drawing operations
79 case CONTEXT_COL_HEADER
: // Draw column headers
80 sprintf(s
,"%c",'A'+COL
); // "A", "B", "C", etc.
81 DrawHeader(s
,X
,Y
,W
,H
);
83 case CONTEXT_ROW_HEADER
: // Draw row headers
84 sprintf(s
,"%03d:",ROW
); // "001:", "002:", etc
85 DrawHeader(s
,X
,Y
,W
,H
);
87 case CONTEXT_CELL
: // Draw data in cells
88 sprintf(s
,"%d",data
[ROW
][COL
]);
97 // Make our data array, and initialize the table options.
99 MyTable(int X
, int Y
, int W
, int H
, const char *L
=0) : Fl_Table(X
,Y
,W
,H
,L
) {
101 for ( int r
=0; r
<MAX_ROWS
; r
++ )
102 for ( int c
=0; c
<MAX_COLS
; c
++ )
103 data
[r
][c
] = 1000+(r
*1000)+c
;
105 rows(MAX_ROWS
); // how many rows
106 row_header(1); // enable row headers (along left)
107 row_height_all(20); // default height of rows
108 row_resize(0); // disable row resizing
110 cols(MAX_COLS
); // how many columns
111 col_header(1); // enable column headers (along top)
112 col_width_all(80); // default width of columns
113 col_resize(1); // enable column resizing
114 end(); // end the Fl_Table group
119 int main(int argc
, char **argv
) {
120 Fl_Double_Window
win(900, 400, "Simple Table");
121 MyTable
table(10,10,880,380);
123 win
.resizable(table
);
129 // End of "$Id: table-simple.cxx 8220 2011-01-08 18:41:47Z greg.ercolano $".