File: table.cxx

package info (click to toggle)
fltk1.3 1.3.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,224 kB
  • sloc: cpp: 105,017; ansic: 91,383; sh: 6,825; makefile: 1,948; perl: 24; xml: 7
file content (493 lines) | stat: -rw-r--r-- 15,505 bytes parent folder | download | duplicates (7)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//
// exercisetablerow -- Exercise all aspects of the Fl_Table_Row widget
//

#include <stdio.h>
#include <string.h>
#ifdef WIN32
#include <stdlib.h>	// atoi
#endif /*WIN32*/

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Check_Button.H>
#include <FL/Fl_Choice.H>
#include <FL/fl_draw.H>
#include <FL/fl_ask.H>
#include <FL/Fl_Table_Row.H>

// Simple demonstration class to derive from Fl_Table_Row
class DemoTable : public Fl_Table_Row
{
private:
    Fl_Color cell_bgcolor;				// color of cell's bg color
    Fl_Color cell_fgcolor;				// color of cell's fg color

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);
    static void event_callback(Fl_Widget*, void*);
    void event_callback2();				// callback for table events

public:
    DemoTable(int x, int y, int w, int h, const char *l=0) : Fl_Table_Row(x,y,w,h,l)
    {
        cell_bgcolor = FL_WHITE;
        cell_fgcolor = FL_BLACK;
        callback(&event_callback, (void*)this);
	end();
    }
    ~DemoTable() { }
    Fl_Color GetCellFGColor() const { return(cell_fgcolor); }
    Fl_Color GetCellBGColor() const { return(cell_bgcolor); }
    void SetCellFGColor(Fl_Color val) { cell_fgcolor = val; }
    void SetCellBGColor(Fl_Color val) { cell_bgcolor = val; }
};

// Handle drawing all cells in table
void DemoTable::draw_cell(TableContext context, 
			  int R, int C, int X, int Y, int W, int H)
{
    static char s[40];
    sprintf(s, "%d/%d", R, C);		// text for each cell

    switch ( context )
    {
	case CONTEXT_STARTPAGE:
	    fl_font(FL_HELVETICA, 16);
	    return;

	case CONTEXT_COL_HEADER:
	    fl_push_clip(X, Y, W, H);
	    {
		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_ROW_HEADER:
	    fl_push_clip(X, Y, W, H);
	    {
		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_CELL:
	{
	    fl_push_clip(X, Y, W, H);
	    {
	        // BG COLOR
		fl_color( row_selected(R) ? selection_color() : cell_bgcolor);
		fl_rectf(X, Y, W, H);

		// TEXT
		fl_color(cell_fgcolor);
		fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER);

		// BORDER
		fl_color(color()); 
		fl_rect(X, Y, W, H);
	    }
	    fl_pop_clip();
	    return;
	}

	case CONTEXT_TABLE:
	    fprintf(stderr, "TABLE CONTEXT CALLED\n");
	    return;

	case CONTEXT_ENDPAGE:
	case CONTEXT_RC_RESIZE:
	case CONTEXT_NONE:
	    return;
    }
}

// Callback whenever someone clicks on different parts of the table
void DemoTable::event_callback(Fl_Widget*, void *data)
{
    DemoTable *o = (DemoTable*)data;
    o->event_callback2();
}

void DemoTable::event_callback2()
{
    int R = callback_row(),
        C = callback_col();
    TableContext context = callback_context();
    printf("'%s' callback: ", (label() ? label() : "?"));
    printf("Row=%d Col=%d Context=%d Event=%d InteractiveResize? %d\n",
	    R, C, (int)context, (int)Fl::event(), (int)is_interactive_resize());
}

// GLOBAL TABLE WIDGET
static DemoTable *G_table = 0;

void setrows_cb(Fl_Widget*, void *data)
{
    Fl_Input *in = (Fl_Input*)data;
    int rows = atoi(in->value());
    if ( rows < 0 ) rows = 0;
    G_table->rows(rows);
}

void setcols_cb(Fl_Widget*, void *data)
{
    Fl_Input *in = (Fl_Input*)data;
    int cols = atoi(in->value());
    if ( cols < 0 ) cols = 0;
    G_table->cols(cols);
}

void setrowheader_cb(Fl_Widget*, void *data)
{
    Fl_Check_Button *check = (Fl_Check_Button*)data;
    G_table->row_header(check->value());
}

void setcolheader_cb(Fl_Widget*, void *data)
{
    Fl_Check_Button *check = (Fl_Check_Button*)data;
    G_table->col_header(check->value());
}

void setrowresize_cb(Fl_Widget*, void *data)
{
    Fl_Check_Button *check = (Fl_Check_Button*)data;
    G_table->row_resize(check->value());
}

void setcolresize_cb(Fl_Widget*, void *data)
{
    Fl_Check_Button *check = (Fl_Check_Button*)data;
    G_table->col_resize(check->value());
}

void setpositionrow_cb(Fl_Widget *w, void *data)
{
    Fl_Input *in = (Fl_Input*)data;
    int toprow = atoi(in->value());
    if ( toprow < 0 || toprow >= G_table->rows() ) 
        { fl_alert("Must be in range 0 thru #rows"); }
    else
        { G_table->row_position(toprow); }
}

void setpositioncol_cb(Fl_Widget *w, void *data)
{
    Fl_Input *in = (Fl_Input*)data;
    int leftcol = atoi(in->value());
    if ( leftcol < 0 || leftcol >= G_table->cols() ) 
        { fl_alert("Must be in range 0 thru #cols"); }
    else
        { G_table->col_position(leftcol); }
}

void setrowheaderwidth_cb(Fl_Widget *w, void *data)
{
    Fl_Input *in = (Fl_Input*)data;
    int val = atoi(in->value());
    if ( val < 1 ) { val = 1; in->value("1"); }
    G_table->row_header_width(val);
}

void setcolheaderheight_cb(Fl_Widget *w, void *data)
{
    Fl_Input *in = (Fl_Input*)data;
    int val = atoi(in->value());
    if ( val < 1 ) { val = 1; in->value("1"); }
    G_table->col_header_height(val);
}

void setrowheadercolor_cb(Fl_Widget *w, void *data)
{
    Fl_Input *in = (Fl_Input*)data;
    int val = atoi(in->value());
    if ( val < 0 ) { fl_alert("Must be a color >0"); }
    else { G_table->row_header_color(Fl_Color(val)); }
}

void setcolheadercolor_cb(Fl_Widget *w, void *data)
{
    Fl_Input *in = (Fl_Input*)data;
    int val = atoi(in->value());
    if ( val < 0 ) { fl_alert("Must be a color >0"); }
    else { G_table->col_header_color(Fl_Color(val)); }
}

void setrowheightall_cb(Fl_Widget *w, void *data)
{
    Fl_Input *in = (Fl_Input*)data;
    int val = atoi(in->value());
    if ( val < 0 ) { val = 0; in->value("0"); }
    G_table->row_height_all(val);
}

void setcolwidthall_cb(Fl_Widget *w, void *data)
{
    Fl_Input *in = (Fl_Input*)data;
    int val = atoi(in->value());
    if ( val < 0 ) { val = 0; in->value("0"); }
    G_table->col_width_all(val);
}

void settablecolor_cb(Fl_Widget *w, void *data)
{
    Fl_Input *in = (Fl_Input*)data;
    int val = atoi(in->value());
    if ( val < 0 ) { fl_alert("Must be a color >0"); }
    else { G_table->color(Fl_Color(val)); }
    G_table->redraw();
}

void setcellfgcolor_cb(Fl_Widget *w, void *data)
{
    Fl_Input *in = (Fl_Input*)data;
    int val = atoi(in->value());
    if ( val < 0 ) { fl_alert("Must be a color >0"); }
    else { G_table->SetCellFGColor(Fl_Color(val)); }
    G_table->redraw();
}

void setcellbgcolor_cb(Fl_Widget *w, void *data)
{
    Fl_Input *in = (Fl_Input*)data;
    int val = atoi(in->value());
    if ( val < 0 ) { fl_alert("Must be a color >0"); }
    else { G_table->SetCellBGColor(Fl_Color(val)); }
    G_table->redraw();
}

char *itoa(int val)
{
    static char s[80];
    sprintf(s, "%d", val);
    return(s);
}

void tablebox_choice_cb(Fl_Widget *w, void *data)
{
    G_table->table_box((Fl_Boxtype)(fl_intptr_t)data);
    G_table->redraw();
}

void widgetbox_choice_cb(Fl_Widget *w, void *data)
{
    G_table->box((Fl_Boxtype)(fl_intptr_t)data);
    G_table->resize(G_table->x(), G_table->y(), G_table->w(), G_table->h());
}

void type_choice_cb(Fl_Widget *w, void *data)
{
    G_table->type((Fl_Table_Row::TableRowSelectMode)(fl_intptr_t)data);
}

Fl_Menu_Item tablebox_choices[] = {
  {"No Box",         0, tablebox_choice_cb, (void*)FL_NO_BOX },
  {"Flat Box",       0, tablebox_choice_cb, (void*)FL_FLAT_BOX },
  {"Up Box",         0, tablebox_choice_cb, (void*)FL_UP_BOX },
  {"Down Box",       0, tablebox_choice_cb, (void*)FL_DOWN_BOX },
  {"Up Frame",       0, tablebox_choice_cb, (void*)FL_UP_FRAME },
  {"Down Frame",     0, tablebox_choice_cb, (void*)FL_DOWN_FRAME },
  {"Thin Up Box",    0, tablebox_choice_cb, (void*)FL_THIN_UP_BOX },
  {"Thin Down Box",  0, tablebox_choice_cb, (void*)FL_THIN_DOWN_BOX },
  {"Thin Up Frame",  0, tablebox_choice_cb, (void*)FL_THIN_UP_FRAME },
  {"Thin Down Frame",0, tablebox_choice_cb, (void*)FL_THIN_DOWN_FRAME },
  {"Engraved Box",   0, tablebox_choice_cb, (void*)FL_ENGRAVED_BOX },
  {"Embossed Box",   0, tablebox_choice_cb, (void*)FL_EMBOSSED_BOX },
  {"Engraved Frame", 0, tablebox_choice_cb, (void*)FL_ENGRAVED_FRAME },
  {"Embossed Frame", 0, tablebox_choice_cb, (void*)FL_EMBOSSED_FRAME },
  {"Border Box",     0, tablebox_choice_cb, (void*)FL_BORDER_BOX },
  {"Shadow Box",     0, tablebox_choice_cb, (void*)FL_SHADOW_BOX },
  {"Border Frame",   0, tablebox_choice_cb, (void*)FL_BORDER_FRAME },
  {0}
};

Fl_Menu_Item widgetbox_choices[] = {
  {"No Box",         0, widgetbox_choice_cb, (void*)FL_NO_BOX },
//{"Flat Box",       0, widgetbox_choice_cb, (void*)FL_FLAT_BOX },
//{"Up Box",         0, widgetbox_choice_cb, (void*)FL_UP_BOX },
//{"Down Box",       0, widgetbox_choice_cb, (void*)FL_DOWN_BOX },
  {"Up Frame",       0, widgetbox_choice_cb, (void*)FL_UP_FRAME },
  {"Down Frame",     0, widgetbox_choice_cb, (void*)FL_DOWN_FRAME },
//{"Thin Up Box",    0, widgetbox_choice_cb, (void*)FL_THIN_UP_BOX },
//{"Thin Down Box",  0, widgetbox_choice_cb, (void*)FL_THIN_DOWN_BOX },
  {"Thin Up Frame",  0, widgetbox_choice_cb, (void*)FL_THIN_UP_FRAME },
  {"Thin Down Frame",0, widgetbox_choice_cb, (void*)FL_THIN_DOWN_FRAME },
//{"Engraved Box",   0, widgetbox_choice_cb, (void*)FL_ENGRAVED_BOX },
//{"Embossed Box",   0, widgetbox_choice_cb, (void*)FL_EMBOSSED_BOX },
  {"Engraved Frame", 0, widgetbox_choice_cb, (void*)FL_ENGRAVED_FRAME },
  {"Embossed Frame", 0, widgetbox_choice_cb, (void*)FL_EMBOSSED_FRAME },
//{"Border Box",     0, widgetbox_choice_cb, (void*)FL_BORDER_BOX },
//{"Shadow Box",     0, widgetbox_choice_cb, (void*)FL_SHADOW_BOX },
  {"Border Frame",   0, widgetbox_choice_cb, (void*)FL_BORDER_FRAME },
  {0}
};

Fl_Menu_Item type_choices[] = {
  {"SelectNone",         0, type_choice_cb, (void*)Fl_Table_Row::SELECT_NONE },
  {"SelectSingle",       0, type_choice_cb, (void*)Fl_Table_Row::SELECT_SINGLE },
  {"SelectMulti",        0, type_choice_cb, (void*)Fl_Table_Row::SELECT_MULTI },
  {0}
};

int main(int argc, char **argv)
{
    Fl_Window win(900, 730);

    G_table = new DemoTable(20, 20, 860, 460, "Demo");
    G_table->selection_color(FL_YELLOW);
    G_table->when(FL_WHEN_RELEASE|FL_WHEN_CHANGED);
    G_table->table_box(FL_NO_BOX);
    G_table->col_resize_min(4);
    G_table->row_resize_min(4);

    // ROWS
    G_table->row_header(1);
    G_table->row_header_width(60);
    G_table->row_resize(1);
    G_table->rows(500);
    G_table->row_height_all(20);

    // COLS
    G_table->cols(500);
    G_table->col_header(1);
    G_table->col_header_height(25);
    G_table->col_resize(1);
    G_table->col_width_all(80);

    // Add children to window
    win.begin();

    // ROW
    Fl_Input setrows(150, 500, 120, 25, "Rows");
    setrows.labelsize(12);
    setrows.value(itoa(G_table->rows()));
    setrows.callback(setrows_cb, (void*)&setrows);
    setrows.when(FL_WHEN_RELEASE);

    Fl_Input rowheightall(400, 500, 120, 25, "Row Height");
    rowheightall.labelsize(12);
    rowheightall.value(itoa(G_table->row_height(0)));
    rowheightall.callback(setrowheightall_cb, (void*)&rowheightall);
    rowheightall.when(FL_WHEN_RELEASE);

    Fl_Input positionrow(650, 500, 120, 25, "Row Position");
    positionrow.labelsize(12);
    positionrow.value("1");
    positionrow.callback(setpositionrow_cb, (void*)&positionrow);
    positionrow.when(FL_WHEN_RELEASE);

    // COL
    Fl_Input setcols(150, 530, 120, 25, "Cols");
    setcols.labelsize(12);
    setcols.value(itoa(G_table->cols()));
    setcols.callback(setcols_cb, (void*)&setcols);
    setcols.when(FL_WHEN_RELEASE);

    Fl_Input colwidthall(400, 530, 120, 25, "Col Width");
    colwidthall.labelsize(12);
    colwidthall.value(itoa(G_table->col_width(0)));
    colwidthall.callback(setcolwidthall_cb, (void*)&colwidthall);
    colwidthall.when(FL_WHEN_RELEASE);

    Fl_Input positioncol(650, 530, 120, 25, "Col Position");
    positioncol.labelsize(12);
    positioncol.value("1");
    positioncol.callback(setpositioncol_cb, (void*)&positioncol);
    positioncol.when(FL_WHEN_RELEASE);

    // ROW HEADER
    Fl_Input rowheaderwidth(150, 570, 120, 25, "Row Header Width");
    rowheaderwidth.labelsize(12);
    rowheaderwidth.value(itoa(G_table->row_header_width()));
    rowheaderwidth.callback(setrowheaderwidth_cb, (void*)&rowheaderwidth);
    rowheaderwidth.when(FL_WHEN_RELEASE);

    Fl_Input rowheadercolor(400, 570, 120, 25, "Row Header Color");
    rowheadercolor.labelsize(12);
    rowheadercolor.value(itoa((int)G_table->row_header_color()));
    rowheadercolor.callback(setrowheadercolor_cb, (void*)&rowheadercolor);
    rowheadercolor.when(FL_WHEN_RELEASE);

    Fl_Check_Button rowheader(550, 570, 120, 25, "Row Headers?");
    rowheader.labelsize(12);
    rowheader.callback(setrowheader_cb, (void*)&rowheader);
    rowheader.value(G_table->row_header() ? 1 : 0);

    Fl_Check_Button rowresize(700, 570, 120, 25, "Row Resize?");
    rowresize.labelsize(12);
    rowresize.callback(setrowresize_cb, (void*)&rowresize);
    rowresize.value(G_table->row_resize() ? 1 : 0);

    // COL HEADER
    Fl_Input colheaderheight(150, 600, 120, 25, "Col Header Height");
    colheaderheight.labelsize(12);
    colheaderheight.value(itoa(G_table->col_header_height()));
    colheaderheight.callback(setcolheaderheight_cb, (void*)&colheaderheight);
    colheaderheight.when(FL_WHEN_RELEASE);

    Fl_Input colheadercolor(400, 600, 120, 25, "Col Header Color");
    colheadercolor.labelsize(12);
    colheadercolor.value(itoa((int)G_table->col_header_color()));
    colheadercolor.callback(setcolheadercolor_cb, (void*)&colheadercolor);
    colheadercolor.when(FL_WHEN_RELEASE);

    Fl_Check_Button colheader(550, 600, 120, 25, "Col Headers?");
    colheader.labelsize(12);
    colheader.callback(setcolheader_cb, (void*)&colheader);
    colheader.value(G_table->col_header() ? 1 : 0);

    Fl_Check_Button colresize(700, 600, 120, 25, "Col Resize?");
    colresize.labelsize(12);
    colresize.callback(setcolresize_cb, (void*)&colresize);
    colresize.value(G_table->col_resize() ? 1 : 0);

    Fl_Choice tablebox(150, 640, 120, 25, "Table Box");
    tablebox.labelsize(12);
    tablebox.textsize(12);
    tablebox.menu(tablebox_choices);
    tablebox.value(0);

    Fl_Choice widgetbox(150, 670, 120, 25, "Widget Box");
    widgetbox.labelsize(12);
    widgetbox.textsize(12);
    widgetbox.menu(widgetbox_choices);
    widgetbox.value(2);		// down frame

    Fl_Input tablecolor(400, 640, 120, 25, "Table Color");
    tablecolor.labelsize(12);
    tablecolor.value(itoa((int)G_table->color()));
    tablecolor.callback(settablecolor_cb, (void*)&tablecolor);
    tablecolor.when(FL_WHEN_RELEASE);

    Fl_Input cellbgcolor(400, 670, 120, 25, "Cell BG Color");
    cellbgcolor.labelsize(12);
    cellbgcolor.value(itoa((int)G_table->GetCellBGColor()));
    cellbgcolor.callback(setcellbgcolor_cb, (void*)&cellbgcolor);
    cellbgcolor.when(FL_WHEN_RELEASE);

    Fl_Input cellfgcolor(400, 700, 120, 25, "Cell FG Color");
    cellfgcolor.labelsize(12);
    cellfgcolor.value(itoa((int)G_table->GetCellFGColor()));
    cellfgcolor.callback(setcellfgcolor_cb, (void*)&cellfgcolor);
    cellfgcolor.when(FL_WHEN_RELEASE);

    Fl_Choice type(650, 640, 120, 25, "Type");
    type.labelsize(12);
    type.textsize(12);
    type.menu(type_choices);
    type.value(2);

    win.end();
    win.resizable(*G_table);
    win.show(argc, argv);

    return(Fl::run());
}