1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
//
// "$Id$"
//
// Show how FLTK widgets can be parented by Fl_Table. -erco 03/30/2003
//
// Originally the 'widgettable.cxx' example program that came with
// erco's Fl_Table widget. Added to FLTK in 2010.
//
// This demonstrates how to use Fl_Table as a 'container' for FLTK
// widgets; one widget per cell. This isn't optimal for large tables,
// where it's better to make one instance of a widget, and move it to
// where it's needed. For an example of this, see the example program
// "table-spreadsheet.cxx".
//
// Copyright 2010 Greg Ercolano.
// Copyright 1998-2010 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems to:
//
// http://www.fltk.org/str.php
//
#include <stdio.h>
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Light_Button.H>
#include <FL/Fl_Input.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Table.H>
void button_cb(Fl_Widget *w, void*);
//
// Simple demonstration class deriving from Fl_Table
//
class WidgetTable : public Fl_Table {
protected:
void draw_cell(TableContext context, // table cell drawing
int R=0, int C=0, int X=0, int Y=0, int W=0, int H=0);
public:
WidgetTable(int x, int y, int w, int h, const char *l=0) : Fl_Table(x,y,w,h,l) {
col_header(1);
col_resize(1);
col_header_height(25);
row_header(1);
row_resize(1);
row_header_width(80);
end();
}
~WidgetTable() { }
void SetSize(int newrows, int newcols) {
clear(); // clear any previous widgets, if any
rows(newrows);
cols(newcols);
begin(); // start adding widgets to group
{
for ( int r = 0; r<newrows; r++ ) {
for ( int c = 0; c<newcols; c++ ) {
int X,Y,W,H;
find_cell(CONTEXT_TABLE, r, c, X, Y, W, H);
char s[40];
if ( c & 1 ) {
// Create the input widgets
sprintf(s, "%d.%d", r, c);
Fl_Input *in = new Fl_Input(X,Y,W,H);
in->value(s);
} else {
// Create the light buttons
sprintf(s, "%d/%d ", r, c);
Fl_Light_Button *butt = new Fl_Light_Button(X,Y,W,H,strdup(s));
butt->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
butt->callback(button_cb, (void*)0);
butt->value( ((r+c*2) & 4 ) ? 1 : 0);
}
}
}
}
end();
}
};
// Handle drawing all cells in table
void WidgetTable::draw_cell(TableContext context,
int R, int C, int X, int Y, int W, int H) {
switch ( context ) {
case CONTEXT_STARTPAGE:
fl_font(FL_HELVETICA, 12); // font used by all headers
break;
case CONTEXT_RC_RESIZE: {
int X, Y, W, H;
int index = 0;
for ( int r = 0; r<rows(); r++ ) {
for ( int c = 0; c<cols(); c++ ) {
if ( index >= children() ) break;
find_cell(CONTEXT_TABLE, r, c, X, Y, W, H);
child(index++)->resize(X,Y,W,H);
}
}
init_sizes(); // tell group children resized
return;
}
case CONTEXT_ROW_HEADER:
fl_push_clip(X, Y, W, H);
{
static char s[40];
sprintf(s, "Row %d", R);
fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, row_header_color());
fl_color(FL_BLACK);
fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER);
}
fl_pop_clip();
return;
case CONTEXT_COL_HEADER:
fl_push_clip(X, Y, W, H);
{
static char s[40];
sprintf(s, "Column %d", C);
fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, col_header_color());
fl_color(FL_BLACK);
fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER);
}
fl_pop_clip();
return;
case CONTEXT_CELL:
return; // fltk handles drawing the widgets
default:
return;
}
}
void button_cb(Fl_Widget *w, void*) {
fprintf(stderr, "BUTTON: %s\n", (const char*)w->label());
}
int main() {
Fl_Double_Window win(940, 500, "table as container");
WidgetTable table(20, 20, win.w()-40, win.h()-40, "FLTK widget table");
table.SetSize(50, 50);
win.end();
win.resizable(table);
win.show();
return(Fl::run());
}
//
// End of "$Id$".
//
|