Fix cairo region leak in restore_clip().
[ntk.git] / examples / table-as-container.cxx
blobb3d6a334fb85879b4f2c2b8e381198cbff34241d
1 //
2 // "$Id: table-as-container.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $"
3 //
4 // Show how FLTK widgets can be parented by Fl_Table. -erco 03/30/2003
5 //
6 // Originally the 'widgettable.cxx' example program that came with
7 // erco's Fl_Table widget. Added to FLTK in 2010.
8 //
9 // This demonstrates how to use Fl_Table as a 'container' for FLTK
10 // widgets; one widget per cell. This isn't optimal for large tables,
11 // where it's better to make one instance of a widget, and move it to
12 // where it's needed. For an example of this, see the example program
13 // "table-spreadsheet.cxx".
15 // Copyright 2010 Greg Ercolano.
16 // Copyright 1998-2010 by Bill Spitzak and others.
18 // This library is free software; you can redistribute it and/or
19 // modify it under the terms of the GNU Library General Public
20 // License as published by the Free Software Foundation; either
21 // version 2 of the License, or (at your option) any later version.
23 // This library is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 // Library General Public License for more details.
28 // You should have received a copy of the GNU Library General Public
29 // License along with this library; if not, write to the Free Software
30 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 // USA.
33 // Please report all bugs and problems to:
35 // http://www.fltk.org/str.php
38 #include <stdio.h>
39 #include <FL/Fl.H>
40 #include <FL/Fl_Double_Window.H>
41 #include <FL/Fl_Light_Button.H>
42 #include <FL/Fl_Input.H>
43 #include <FL/fl_draw.H>
44 #include <FL/Fl_Table.H>
46 void button_cb(Fl_Widget *w, void*);
49 // Simple demonstration class deriving from Fl_Table
51 class WidgetTable : public Fl_Table {
52 protected:
53 void draw_cell(TableContext context, // table cell drawing
54 int R=0, int C=0, int X=0, int Y=0, int W=0, int H=0);
56 public:
57 WidgetTable(int x, int y, int w, int h, const char *l=0) : Fl_Table(x,y,w,h,l) {
58 col_header(1);
59 col_resize(1);
60 col_header_height(25);
61 row_header(1);
62 row_resize(1);
63 row_header_width(80);
64 end();
66 ~WidgetTable() { }
68 void SetSize(int newrows, int newcols) {
69 rows(newrows);
70 cols(newcols);
72 begin(); // start adding widgets to group
74 for ( int r = 0; r<newrows; r++ ) {
75 for ( int c = 0; c<newcols; c++ ) {
76 int X,Y,W,H;
77 find_cell(CONTEXT_TABLE, r, c, X, Y, W, H);
79 char s[40];
80 if ( c & 1 ) {
81 // Create the input widgets
82 sprintf(s, "%d.%d", r, c);
83 Fl_Input *in = new Fl_Input(X,Y,W,H);
84 in->value(s);
85 } else {
86 // Create the light buttons
87 sprintf(s, "%d/%d ", r, c);
88 Fl_Light_Button *butt = new Fl_Light_Button(X,Y,W,H,strdup(s));
89 butt->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
90 butt->callback(button_cb, (void*)0);
91 butt->value( ((r+c*2) & 4 ) ? 1 : 0);
96 end();
100 // Handle drawing all cells in table
101 void WidgetTable::draw_cell(TableContext context,
102 int R, int C, int X, int Y, int W, int H) {
103 switch ( context ) {
104 case CONTEXT_STARTPAGE:
105 fl_font(FL_HELVETICA, 12); // font used by all headers
106 break;
108 case CONTEXT_RC_RESIZE: {
109 int X, Y, W, H;
110 int index = 0;
111 for ( int r = 0; r<rows(); r++ ) {
112 for ( int c = 0; c<cols(); c++ ) {
113 if ( index >= children() ) break;
114 find_cell(CONTEXT_TABLE, r, c, X, Y, W, H);
115 child(index++)->resize(X,Y,W,H);
118 init_sizes(); // tell group children resized
119 return;
122 case CONTEXT_ROW_HEADER:
123 fl_push_clip(X, Y, W, H);
125 static char s[40];
126 sprintf(s, "Row %d", R);
127 fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, row_header_color());
128 fl_color(FL_BLACK);
129 fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER);
131 fl_pop_clip();
132 return;
134 case CONTEXT_COL_HEADER:
135 fl_push_clip(X, Y, W, H);
137 static char s[40];
138 sprintf(s, "Column %d", C);
139 fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, col_header_color());
140 fl_color(FL_BLACK);
141 fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER);
143 fl_pop_clip();
144 return;
146 case CONTEXT_CELL:
147 return; // fltk handles drawing the widgets
149 default:
150 return;
154 void button_cb(Fl_Widget *w, void*) {
155 fprintf(stderr, "BUTTON: %s\n", (const char*)w->label());
158 int main() {
159 Fl_Double_Window win(940, 500, "table as container");
160 WidgetTable table(20, 20, win.w()-40, win.h()-40, "FLTK widget table");
161 table.SetSize(50, 50);
162 win.end();
163 win.resizable(table);
164 win.show();
165 return(Fl::run());
169 // End of "$Id: table-as-container.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $".