2 // "$Id: table-as-container.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $"
4 // Show how FLTK widgets can be parented by Fl_Table. -erco 03/30/2003
6 // Originally the 'widgettable.cxx' example program that came with
7 // erco's Fl_Table widget. Added to FLTK in 2010.
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
33 // Please report all bugs and problems to:
35 // http://www.fltk.org/str.php
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
{
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);
57 WidgetTable(int x
, int y
, int w
, int h
, const char *l
=0) : Fl_Table(x
,y
,w
,h
,l
) {
60 col_header_height(25);
68 void SetSize(int newrows
, int newcols
) {
72 begin(); // start adding widgets to group
74 for ( int r
= 0; r
<newrows
; r
++ ) {
75 for ( int c
= 0; c
<newcols
; c
++ ) {
77 find_cell(CONTEXT_TABLE
, r
, c
, X
, Y
, W
, H
);
81 // Create the input widgets
82 sprintf(s
, "%d.%d", r
, c
);
83 Fl_Input
*in
= new Fl_Input(X
,Y
,W
,H
);
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);
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
) {
104 case CONTEXT_STARTPAGE
:
105 fl_font(FL_HELVETICA
, 12); // font used by all headers
108 case CONTEXT_RC_RESIZE
: {
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
122 case CONTEXT_ROW_HEADER
:
123 fl_push_clip(X
, Y
, W
, H
);
126 sprintf(s
, "Row %d", R
);
127 fl_draw_box(FL_THIN_UP_BOX
, X
, Y
, W
, H
, row_header_color());
129 fl_draw(s
, X
, Y
, W
, H
, FL_ALIGN_CENTER
);
134 case CONTEXT_COL_HEADER
:
135 fl_push_clip(X
, Y
, W
, H
);
138 sprintf(s
, "Column %d", C
);
139 fl_draw_box(FL_THIN_UP_BOX
, X
, Y
, W
, H
, col_header_color());
141 fl_draw(s
, X
, Y
, W
, H
, FL_ALIGN_CENTER
);
147 return; // fltk handles drawing the widgets
154 void button_cb(Fl_Widget
*w
, void*) {
155 fprintf(stderr
, "BUTTON: %s\n", (const char*)w
->label());
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);
163 win
.resizable(table
);
169 // End of "$Id: table-as-container.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $".