1 //
2 // "$Id$"
3 //
4 // Simple example of using Fl_Table - Greg Ercolano 11/29/2010
5 //
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.
10 //
11 // Copyright 2010 Greg Ercolano.
12 // Copyright 1998-2010 by Bill Spitzak and others.
13 //
14 // This library is free software. Distribution and use rights are outlined in
15 // the file "COPYING" which should have been included with this file. If this
16 // file is missing or damaged, see the license at:
17 //
18 // http://www.fltk.org/COPYING.php
19 //
20 // Please report all bugs and problems on the following page:
21 //
22 // http://www.fltk.org/str.php
23 //
24 #include <FL/Fl.H>
25 #include <FL/Fl_Double_Window.H>
26 #include <FL/Fl_Table.H>
27 #include <FL/fl_draw.H>
28
29 #define MAX_ROWS 30
30 #define MAX_COLS 26 // A-Z
31
32 // Derive a class from Fl_Table
33 class MyTable : public Fl_Table {
34
35 int data[MAX_ROWS][MAX_COLS]; // data array for cells
36
37 // Draw the row/col headings
38 // Make this a dark thin upbox with the text inside.
39 //
DrawHeader(const char * s,int X,int Y,int W,int H)40 void DrawHeader(const char *s, int X, int Y, int W, int H) {
41 fl_push_clip(X,Y,W,H);
42 fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, row_header_color());
43 fl_color(FL_BLACK);
44 fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER);
45 fl_pop_clip();
46 }
47 // Draw the cell data
48 // Dark gray text on white background with subtle border
49 //
DrawData(const char * s,int X,int Y,int W,int H)50 void DrawData(const char *s, int X, int Y, int W, int H) {
51 fl_push_clip(X,Y,W,H);
52 // Draw cell bg
53 fl_color(FL_WHITE); fl_rectf(X,Y,W,H);
54 // Draw cell data
55 fl_color(FL_GRAY0); fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER);
56 // Draw box border
57 fl_color(color()); fl_rect(X,Y,W,H);
58 fl_pop_clip();
59 }
60 // Handle drawing table's cells
61 // Fl_Table calls this function to draw each visible cell in the table.
62 // It's up to us to use FLTK's drawing functions to draw the cells the way we want.
63 //
draw_cell(TableContext context,int ROW=0,int COL=0,int X=0,int Y=0,int W=0,int H=0)64 void draw_cell(TableContext context, int ROW=0, int COL=0, int X=0, int Y=0, int W=0, int H=0) {
65 static char s[40];
66 switch ( context ) {
67 case CONTEXT_STARTPAGE: // before page is drawn..
68 fl_font(FL_HELVETICA, 16); // set the font for our drawing operations
69 return;
70 case CONTEXT_COL_HEADER: // Draw column headers
71 sprintf(s,"%c",'A'+COL); // "A", "B", "C", etc.
72 DrawHeader(s,X,Y,W,H);
73 return;
74 case CONTEXT_ROW_HEADER: // Draw row headers
75 sprintf(s,"%03d:",ROW); // "001:", "002:", etc
76 DrawHeader(s,X,Y,W,H);
77 return;
78 case CONTEXT_CELL: // Draw data in cells
79 sprintf(s,"%d",data[ROW][COL]);
80 DrawData(s,X,Y,W,H);
81 return;
82 default:
83 return;
84 }
85 }
86 public:
87 // Constructor
88 // Make our data array, and initialize the table options.
89 //
MyTable(int X,int Y,int W,int H,const char * L=0)90 MyTable(int X, int Y, int W, int H, const char *L=0) : Fl_Table(X,Y,W,H,L) {
91 // Fill data array
92 for ( int r=0; r<MAX_ROWS; r++ )
93 for ( int c=0; c<MAX_COLS; c++ )
94 data[r][c] = 1000+(r*1000)+c;
95 // Rows
96 rows(MAX_ROWS); // how many rows
97 row_header(1); // enable row headers (along left)
98 row_height_all(20); // default height of rows
99 row_resize(0); // disable row resizing
100 // Cols
101 cols(MAX_COLS); // how many columns
102 col_header(1); // enable column headers (along top)
103 col_width_all(80); // default width of columns
104 col_resize(1); // enable column resizing
105 end(); // end the Fl_Table group
106 }
~MyTable()107 ~MyTable() { }
108 };
109
main(int argc,char ** argv)110 int main(int argc, char **argv) {
111 Fl_Double_Window win(900, 400, "Simple Table");
112 MyTable table(10,10,880,380);
113 win.end();
114 win.resizable(table);
115 win.show(argc,argv);
116 return(Fl::run());
117 }
118
119 //
120 // End of "$Id$".
121 //
122