2 // exercisetablerow -- Exercise all aspects of the Fl_Table_Row widget
8 #include <stdlib.h> // atoi
12 #include <FL/Fl_Window.H>
13 #include <FL/Fl_Input.H>
14 #include <FL/Fl_Check_Button.H>
15 #include <FL/Fl_Choice.H>
16 #include <FL/fl_draw.H>
17 #include <FL/fl_ask.H>
18 #include <FL/Fl_Table_Row.H>
20 // Simple demonstration class to derive from Fl_Table_Row
21 class DemoTable
: public Fl_Table_Row
24 Fl_Color cell_bgcolor
; // color of cell's bg color
25 Fl_Color cell_fgcolor
; // color of cell's fg color
28 void draw_cell(TableContext context
, // table cell drawing
29 int R
=0, int C
=0, int X
=0, int Y
=0, int W
=0, int H
=0);
30 static void event_callback(Fl_Widget
*, void*);
31 void event_callback2(); // callback for table events
34 DemoTable(int x
, int y
, int w
, int h
, const char *l
=0) : Fl_Table_Row(x
,y
,w
,h
,l
)
36 cell_bgcolor
= FL_WHITE
;
37 cell_fgcolor
= FL_BLACK
;
38 callback(&event_callback
, (void*)this);
42 Fl_Color
GetCellFGColor() const { return(cell_fgcolor
); }
43 Fl_Color
GetCellBGColor() const { return(cell_bgcolor
); }
44 void SetCellFGColor(Fl_Color val
) { cell_fgcolor
= val
; }
45 void SetCellBGColor(Fl_Color val
) { cell_bgcolor
= val
; }
48 // Handle drawing all cells in table
49 void DemoTable::draw_cell(TableContext context
,
50 int R
, int C
, int X
, int Y
, int W
, int H
)
53 sprintf(s
, "%d/%d", R
, C
); // text for each cell
57 case CONTEXT_STARTPAGE
:
58 fl_font(FL_HELVETICA
, 16);
61 case CONTEXT_COL_HEADER
:
62 fl_push_clip(X
, Y
, W
, H
);
64 fl_draw_box(FL_THIN_UP_BOX
, X
, Y
, W
, H
, col_header_color());
66 fl_draw(s
, X
, Y
, W
, H
, FL_ALIGN_CENTER
);
71 case CONTEXT_ROW_HEADER
:
72 fl_push_clip(X
, Y
, W
, H
);
74 fl_draw_box(FL_THIN_UP_BOX
, X
, Y
, W
, H
, row_header_color());
76 fl_draw(s
, X
, Y
, W
, H
, FL_ALIGN_CENTER
);
83 fl_push_clip(X
, Y
, W
, H
);
86 fl_color( row_selected(R
) ? selection_color() : cell_bgcolor
);
90 fl_color(cell_fgcolor
);
91 fl_draw(s
, X
, Y
, W
, H
, FL_ALIGN_CENTER
);
102 fprintf(stderr
, "TABLE CONTEXT CALLED\n");
105 case CONTEXT_ENDPAGE
:
106 case CONTEXT_RC_RESIZE
:
112 // Callback whenever someone clicks on different parts of the table
113 void DemoTable::event_callback(Fl_Widget
*, void *data
)
115 DemoTable
*o
= (DemoTable
*)data
;
116 o
->event_callback2();
119 void DemoTable::event_callback2()
121 int R
= callback_row(),
123 TableContext context
= callback_context();
124 printf("'%s' callback: ", (label() ? label() : "?"));
125 printf("Row=%d Col=%d Context=%d Event=%d InteractiveResize? %d\n",
126 R
, C
, (int)context
, (int)Fl::event(), (int)is_interactive_resize());
129 // GLOBAL TABLE WIDGET
130 static DemoTable
*G_table
= 0;
132 void setrows_cb(Fl_Widget
*, void *data
)
134 Fl_Input
*in
= (Fl_Input
*)data
;
135 int rows
= atoi(in
->value());
136 if ( rows
< 0 ) rows
= 0;
140 void setcols_cb(Fl_Widget
*, void *data
)
142 Fl_Input
*in
= (Fl_Input
*)data
;
143 int cols
= atoi(in
->value());
144 if ( cols
< 0 ) cols
= 0;
148 void setrowheader_cb(Fl_Widget
*, void *data
)
150 Fl_Check_Button
*check
= (Fl_Check_Button
*)data
;
151 G_table
->row_header(check
->value());
154 void setcolheader_cb(Fl_Widget
*, void *data
)
156 Fl_Check_Button
*check
= (Fl_Check_Button
*)data
;
157 G_table
->col_header(check
->value());
160 void setrowresize_cb(Fl_Widget
*, void *data
)
162 Fl_Check_Button
*check
= (Fl_Check_Button
*)data
;
163 G_table
->row_resize(check
->value());
166 void setcolresize_cb(Fl_Widget
*, void *data
)
168 Fl_Check_Button
*check
= (Fl_Check_Button
*)data
;
169 G_table
->col_resize(check
->value());
172 void setpositionrow_cb(Fl_Widget
*w
, void *data
)
174 Fl_Input
*in
= (Fl_Input
*)data
;
175 int toprow
= atoi(in
->value());
176 if ( toprow
< 0 || toprow
>= G_table
->rows() )
177 { fl_alert("Must be in range 0 thru #rows"); }
179 { G_table
->row_position(toprow
); }
182 void setpositioncol_cb(Fl_Widget
*w
, void *data
)
184 Fl_Input
*in
= (Fl_Input
*)data
;
185 int leftcol
= atoi(in
->value());
186 if ( leftcol
< 0 || leftcol
>= G_table
->cols() )
187 { fl_alert("Must be in range 0 thru #cols"); }
189 { G_table
->col_position(leftcol
); }
192 void setrowheaderwidth_cb(Fl_Widget
*w
, void *data
)
194 Fl_Input
*in
= (Fl_Input
*)data
;
195 int val
= atoi(in
->value());
196 if ( val
< 1 ) { val
= 1; in
->value("1"); }
197 G_table
->row_header_width(val
);
200 void setcolheaderheight_cb(Fl_Widget
*w
, void *data
)
202 Fl_Input
*in
= (Fl_Input
*)data
;
203 int val
= atoi(in
->value());
204 if ( val
< 1 ) { val
= 1; in
->value("1"); }
205 G_table
->col_header_height(val
);
208 void setrowheadercolor_cb(Fl_Widget
*w
, void *data
)
210 Fl_Input
*in
= (Fl_Input
*)data
;
211 int val
= atoi(in
->value());
212 if ( val
< 0 ) { fl_alert("Must be a color >0"); }
213 else { G_table
->row_header_color(Fl_Color(val
)); }
216 void setcolheadercolor_cb(Fl_Widget
*w
, void *data
)
218 Fl_Input
*in
= (Fl_Input
*)data
;
219 int val
= atoi(in
->value());
220 if ( val
< 0 ) { fl_alert("Must be a color >0"); }
221 else { G_table
->col_header_color(Fl_Color(val
)); }
224 void setrowheightall_cb(Fl_Widget
*w
, void *data
)
226 Fl_Input
*in
= (Fl_Input
*)data
;
227 int val
= atoi(in
->value());
228 if ( val
< 0 ) { val
= 0; in
->value("0"); }
229 G_table
->row_height_all(val
);
232 void setcolwidthall_cb(Fl_Widget
*w
, void *data
)
234 Fl_Input
*in
= (Fl_Input
*)data
;
235 int val
= atoi(in
->value());
236 if ( val
< 0 ) { val
= 0; in
->value("0"); }
237 G_table
->col_width_all(val
);
240 void settablecolor_cb(Fl_Widget
*w
, void *data
)
242 Fl_Input
*in
= (Fl_Input
*)data
;
243 int val
= atoi(in
->value());
244 if ( val
< 0 ) { fl_alert("Must be a color >0"); }
245 else { G_table
->color(Fl_Color(val
)); }
249 void setcellfgcolor_cb(Fl_Widget
*w
, void *data
)
251 Fl_Input
*in
= (Fl_Input
*)data
;
252 int val
= atoi(in
->value());
253 if ( val
< 0 ) { fl_alert("Must be a color >0"); }
254 else { G_table
->SetCellFGColor(Fl_Color(val
)); }
258 void setcellbgcolor_cb(Fl_Widget
*w
, void *data
)
260 Fl_Input
*in
= (Fl_Input
*)data
;
261 int val
= atoi(in
->value());
262 if ( val
< 0 ) { fl_alert("Must be a color >0"); }
263 else { G_table
->SetCellBGColor(Fl_Color(val
)); }
270 sprintf(s
, "%d", val
);
274 void tablebox_choice_cb(Fl_Widget
*w
, void *data
)
276 G_table
->table_box((Fl_Boxtype
)(fl_intptr_t
)data
);
280 void widgetbox_choice_cb(Fl_Widget
*w
, void *data
)
282 G_table
->box((Fl_Boxtype
)(fl_intptr_t
)data
);
283 G_table
->resize(G_table
->x(), G_table
->y(), G_table
->w(), G_table
->h());
286 void type_choice_cb(Fl_Widget
*w
, void *data
)
288 G_table
->type((Fl_Table_Row::TableRowSelectMode
)(fl_intptr_t
)data
);
291 Fl_Menu_Item tablebox_choices
[] = {
292 {"No Box", 0, tablebox_choice_cb
, (void*)FL_NO_BOX
},
293 {"Flat Box", 0, tablebox_choice_cb
, (void*)FL_FLAT_BOX
},
294 {"Up Box", 0, tablebox_choice_cb
, (void*)FL_UP_BOX
},
295 {"Down Box", 0, tablebox_choice_cb
, (void*)FL_DOWN_BOX
},
296 {"Up Frame", 0, tablebox_choice_cb
, (void*)FL_UP_FRAME
},
297 {"Down Frame", 0, tablebox_choice_cb
, (void*)FL_DOWN_FRAME
},
298 {"Thin Up Box", 0, tablebox_choice_cb
, (void*)FL_THIN_UP_BOX
},
299 {"Thin Down Box", 0, tablebox_choice_cb
, (void*)FL_THIN_DOWN_BOX
},
300 {"Thin Up Frame", 0, tablebox_choice_cb
, (void*)FL_THIN_UP_FRAME
},
301 {"Thin Down Frame",0, tablebox_choice_cb
, (void*)FL_THIN_DOWN_FRAME
},
302 {"Engraved Box", 0, tablebox_choice_cb
, (void*)FL_ENGRAVED_BOX
},
303 {"Embossed Box", 0, tablebox_choice_cb
, (void*)FL_EMBOSSED_BOX
},
304 {"Engraved Frame", 0, tablebox_choice_cb
, (void*)FL_ENGRAVED_FRAME
},
305 {"Embossed Frame", 0, tablebox_choice_cb
, (void*)FL_EMBOSSED_FRAME
},
306 {"Border Box", 0, tablebox_choice_cb
, (void*)FL_BORDER_BOX
},
307 {"Shadow Box", 0, tablebox_choice_cb
, (void*)FL_SHADOW_BOX
},
308 {"Border Frame", 0, tablebox_choice_cb
, (void*)FL_BORDER_FRAME
},
312 Fl_Menu_Item widgetbox_choices
[] = {
313 {"No Box", 0, widgetbox_choice_cb
, (void*)FL_NO_BOX
},
314 //{"Flat Box", 0, widgetbox_choice_cb, (void*)FL_FLAT_BOX },
315 //{"Up Box", 0, widgetbox_choice_cb, (void*)FL_UP_BOX },
316 //{"Down Box", 0, widgetbox_choice_cb, (void*)FL_DOWN_BOX },
317 {"Up Frame", 0, widgetbox_choice_cb
, (void*)FL_UP_FRAME
},
318 {"Down Frame", 0, widgetbox_choice_cb
, (void*)FL_DOWN_FRAME
},
319 //{"Thin Up Box", 0, widgetbox_choice_cb, (void*)FL_THIN_UP_BOX },
320 //{"Thin Down Box", 0, widgetbox_choice_cb, (void*)FL_THIN_DOWN_BOX },
321 {"Thin Up Frame", 0, widgetbox_choice_cb
, (void*)FL_THIN_UP_FRAME
},
322 {"Thin Down Frame",0, widgetbox_choice_cb
, (void*)FL_THIN_DOWN_FRAME
},
323 //{"Engraved Box", 0, widgetbox_choice_cb, (void*)FL_ENGRAVED_BOX },
324 //{"Embossed Box", 0, widgetbox_choice_cb, (void*)FL_EMBOSSED_BOX },
325 {"Engraved Frame", 0, widgetbox_choice_cb
, (void*)FL_ENGRAVED_FRAME
},
326 {"Embossed Frame", 0, widgetbox_choice_cb
, (void*)FL_EMBOSSED_FRAME
},
327 //{"Border Box", 0, widgetbox_choice_cb, (void*)FL_BORDER_BOX },
328 //{"Shadow Box", 0, widgetbox_choice_cb, (void*)FL_SHADOW_BOX },
329 {"Border Frame", 0, widgetbox_choice_cb
, (void*)FL_BORDER_FRAME
},
333 Fl_Menu_Item type_choices
[] = {
334 {"SelectNone", 0, type_choice_cb
, (void*)Fl_Table_Row::SELECT_NONE
},
335 {"SelectSingle", 0, type_choice_cb
, (void*)Fl_Table_Row::SELECT_SINGLE
},
336 {"SelectMulti", 0, type_choice_cb
, (void*)Fl_Table_Row::SELECT_MULTI
},
340 int main(int argc
, char **argv
)
342 Fl_Window
win(900, 730);
344 G_table
= new DemoTable(20, 20, 860, 460, "Demo");
345 G_table
->selection_color(FL_YELLOW
);
346 G_table
->when(FL_WHEN_RELEASE
|FL_WHEN_CHANGED
);
347 G_table
->table_box(FL_NO_BOX
);
348 G_table
->col_resize_min(4);
349 G_table
->row_resize_min(4);
352 G_table
->row_header(1);
353 G_table
->row_header_width(60);
354 G_table
->row_resize(1);
356 G_table
->row_height_all(20);
360 G_table
->col_header(1);
361 G_table
->col_header_height(25);
362 G_table
->col_resize(1);
363 G_table
->col_width_all(80);
365 // Add children to window
369 Fl_Input
setrows(150, 500, 120, 25, "Rows");
370 setrows
.labelsize(12);
371 setrows
.value(itoa(G_table
->rows()));
372 setrows
.callback(setrows_cb
, (void*)&setrows
);
373 setrows
.when(FL_WHEN_RELEASE
);
375 Fl_Input
rowheightall(400, 500, 120, 25, "Row Height");
376 rowheightall
.labelsize(12);
377 rowheightall
.value(itoa(G_table
->row_height(0)));
378 rowheightall
.callback(setrowheightall_cb
, (void*)&rowheightall
);
379 rowheightall
.when(FL_WHEN_RELEASE
);
381 Fl_Input
positionrow(650, 500, 120, 25, "Row Position");
382 positionrow
.labelsize(12);
383 positionrow
.value("1");
384 positionrow
.callback(setpositionrow_cb
, (void*)&positionrow
);
385 positionrow
.when(FL_WHEN_RELEASE
);
388 Fl_Input
setcols(150, 530, 120, 25, "Cols");
389 setcols
.labelsize(12);
390 setcols
.value(itoa(G_table
->cols()));
391 setcols
.callback(setcols_cb
, (void*)&setcols
);
392 setcols
.when(FL_WHEN_RELEASE
);
394 Fl_Input
colwidthall(400, 530, 120, 25, "Col Width");
395 colwidthall
.labelsize(12);
396 colwidthall
.value(itoa(G_table
->col_width(0)));
397 colwidthall
.callback(setcolwidthall_cb
, (void*)&colwidthall
);
398 colwidthall
.when(FL_WHEN_RELEASE
);
400 Fl_Input
positioncol(650, 530, 120, 25, "Col Position");
401 positioncol
.labelsize(12);
402 positioncol
.value("1");
403 positioncol
.callback(setpositioncol_cb
, (void*)&positioncol
);
404 positioncol
.when(FL_WHEN_RELEASE
);
407 Fl_Input
rowheaderwidth(150, 570, 120, 25, "Row Header Width");
408 rowheaderwidth
.labelsize(12);
409 rowheaderwidth
.value(itoa(G_table
->row_header_width()));
410 rowheaderwidth
.callback(setrowheaderwidth_cb
, (void*)&rowheaderwidth
);
411 rowheaderwidth
.when(FL_WHEN_RELEASE
);
413 Fl_Input
rowheadercolor(400, 570, 120, 25, "Row Header Color");
414 rowheadercolor
.labelsize(12);
415 rowheadercolor
.value(itoa((int)G_table
->row_header_color()));
416 rowheadercolor
.callback(setrowheadercolor_cb
, (void*)&rowheadercolor
);
417 rowheadercolor
.when(FL_WHEN_RELEASE
);
419 Fl_Check_Button
rowheader(550, 570, 120, 25, "Row Headers?");
420 rowheader
.labelsize(12);
421 rowheader
.callback(setrowheader_cb
, (void*)&rowheader
);
422 rowheader
.value(G_table
->row_header() ? 1 : 0);
424 Fl_Check_Button
rowresize(700, 570, 120, 25, "Row Resize?");
425 rowresize
.labelsize(12);
426 rowresize
.callback(setrowresize_cb
, (void*)&rowresize
);
427 rowresize
.value(G_table
->row_resize() ? 1 : 0);
430 Fl_Input
colheaderheight(150, 600, 120, 25, "Col Header Height");
431 colheaderheight
.labelsize(12);
432 colheaderheight
.value(itoa(G_table
->col_header_height()));
433 colheaderheight
.callback(setcolheaderheight_cb
, (void*)&colheaderheight
);
434 colheaderheight
.when(FL_WHEN_RELEASE
);
436 Fl_Input
colheadercolor(400, 600, 120, 25, "Col Header Color");
437 colheadercolor
.labelsize(12);
438 colheadercolor
.value(itoa((int)G_table
->col_header_color()));
439 colheadercolor
.callback(setcolheadercolor_cb
, (void*)&colheadercolor
);
440 colheadercolor
.when(FL_WHEN_RELEASE
);
442 Fl_Check_Button
colheader(550, 600, 120, 25, "Col Headers?");
443 colheader
.labelsize(12);
444 colheader
.callback(setcolheader_cb
, (void*)&colheader
);
445 colheader
.value(G_table
->col_header() ? 1 : 0);
447 Fl_Check_Button
colresize(700, 600, 120, 25, "Col Resize?");
448 colresize
.labelsize(12);
449 colresize
.callback(setcolresize_cb
, (void*)&colresize
);
450 colresize
.value(G_table
->col_resize() ? 1 : 0);
452 Fl_Choice
tablebox(150, 640, 120, 25, "Table Box");
453 tablebox
.labelsize(12);
454 tablebox
.textsize(12);
455 tablebox
.menu(tablebox_choices
);
458 Fl_Choice
widgetbox(150, 670, 120, 25, "Widget Box");
459 widgetbox
.labelsize(12);
460 widgetbox
.textsize(12);
461 widgetbox
.menu(widgetbox_choices
);
462 widgetbox
.value(2); // down frame
464 Fl_Input
tablecolor(400, 640, 120, 25, "Table Color");
465 tablecolor
.labelsize(12);
466 tablecolor
.value(itoa((int)G_table
->color()));
467 tablecolor
.callback(settablecolor_cb
, (void*)&tablecolor
);
468 tablecolor
.when(FL_WHEN_RELEASE
);
470 Fl_Input
cellbgcolor(400, 670, 120, 25, "Cell BG Color");
471 cellbgcolor
.labelsize(12);
472 cellbgcolor
.value(itoa((int)G_table
->GetCellBGColor()));
473 cellbgcolor
.callback(setcellbgcolor_cb
, (void*)&cellbgcolor
);
474 cellbgcolor
.when(FL_WHEN_RELEASE
);
476 Fl_Input
cellfgcolor(400, 700, 120, 25, "Cell FG Color");
477 cellfgcolor
.labelsize(12);
478 cellfgcolor
.value(itoa((int)G_table
->GetCellFGColor()));
479 cellfgcolor
.callback(setcellfgcolor_cb
, (void*)&cellfgcolor
);
480 cellfgcolor
.when(FL_WHEN_RELEASE
);
482 Fl_Choice
type(650, 640, 120, 25, "Type");
485 type
.menu(type_choices
);
489 win
.resizable(*G_table
);
490 win
.show(argc
, argv
);