2012-04-14 23:45:50 +04:00
|
|
|
//
|
|
|
|
// Example of Fl_Table with keyboard selection navigation - Greg Ercolano 04/14/2012
|
|
|
|
//
|
|
|
|
// Display a 10x10 multiplication table, and allow the user to
|
|
|
|
// make cell or row selections (with mouse or keyboard navigation)
|
|
|
|
// to select areas of the table, and show the sum of the cell's values.
|
|
|
|
//
|
|
|
|
// Started with the "testkeyboardnav.cxx" example from the original
|
|
|
|
// Fl_Table project, using Jean-Marc Lienher's additions for keyboard nav.
|
|
|
|
//
|
|
|
|
// Copyright 2003, 2012 Greg Ercolano.
|
|
|
|
// Copyright 2004 Jean-Marc Lienher
|
|
|
|
// 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:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
2012-04-14 23:45:50 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2012-04-14 23:45:50 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2012-04-14 23:45:50 +04:00
|
|
|
//
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <FL/Fl_Double_Window.H>
|
|
|
|
#include <FL/Fl_Toggle_Button.H>
|
|
|
|
#include <FL/Fl_Output.H>
|
|
|
|
#include <FL/Fl_Table_Row.H>
|
2013-03-26 03:20:29 +04:00
|
|
|
#include <FL/names.h>
|
2012-04-14 23:45:50 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
// GLOBALS
|
|
|
|
class MyTable;
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Toggle_Button *G_rowselect = 0; // toggle to enable row selection
|
|
|
|
MyTable *G_table = 0; // table widget
|
|
|
|
Fl_Output *G_sum = 0; // displays sum of user's selection
|
2012-04-14 23:45:50 +04:00
|
|
|
|
|
|
|
class MyTable : public Fl_Table_Row {
|
|
|
|
protected:
|
|
|
|
// Handle drawing all cells in table
|
2022-12-30 21:14:36 +03:00
|
|
|
void draw_cell(TableContext context, int R=0,int C=0, int X=0,int Y=0,int W=0,int H=0) FL_OVERRIDE {
|
2020-07-01 19:03:10 +03:00
|
|
|
static char s[30];
|
|
|
|
switch ( context ) {
|
|
|
|
case CONTEXT_COL_HEADER:
|
|
|
|
case CONTEXT_ROW_HEADER:
|
|
|
|
fl_font(FL_HELVETICA | FL_BOLD, 14);
|
|
|
|
fl_push_clip(X, Y, W, H);
|
|
|
|
{
|
|
|
|
Fl_Color c = (context==CONTEXT_COL_HEADER) ? col_header_color() : row_header_color();
|
|
|
|
fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, c);
|
|
|
|
fl_color(FL_BLACK);
|
|
|
|
// Draw text for headers
|
|
|
|
sprintf(s, "%d", (context == CONTEXT_COL_HEADER) ? C : R);
|
|
|
|
fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER);
|
|
|
|
}
|
|
|
|
fl_pop_clip();
|
|
|
|
return;
|
|
|
|
case CONTEXT_CELL: {
|
|
|
|
// Keyboard nav and mouse selection highlighting
|
|
|
|
int selected = G_rowselect->value() ? row_selected(R) : is_selected(R,C);
|
|
|
|
fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, selected ? FL_YELLOW : FL_WHITE);
|
|
|
|
// Draw text for the cell
|
|
|
|
fl_push_clip(X+3, Y+3, W-6, H-6);
|
|
|
|
{
|
|
|
|
fl_font(FL_HELVETICA, 14);
|
|
|
|
fl_color(FL_BLACK);
|
|
|
|
sprintf(s, "%d", R*C); // factor row + col for data cells
|
|
|
|
fl_draw(s, X+3, Y+3, W-6, H-6, FL_ALIGN_RIGHT);
|
|
|
|
}
|
|
|
|
fl_pop_clip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2012-04-14 23:45:50 +04:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
// CTOR
|
|
|
|
MyTable(int x, int y, int w, int h, const char *l=0) : Fl_Table_Row(x,y,w,h,l) {
|
2020-07-01 19:03:10 +03:00
|
|
|
// Row init
|
|
|
|
row_header(1);
|
|
|
|
row_header_width(70);
|
|
|
|
row_resize(1);
|
|
|
|
rows(11);
|
|
|
|
row_height_all(20);
|
|
|
|
// Col init
|
|
|
|
col_header(1);
|
|
|
|
col_header_height(20);
|
|
|
|
col_resize(1);
|
|
|
|
cols(11);
|
|
|
|
col_width_all(70);
|
|
|
|
end(); // Fl_Table derives from Fl_Group, so end() it
|
2012-04-14 23:45:50 +04:00
|
|
|
}
|
|
|
|
~MyTable() { }
|
|
|
|
// Update the displayed sum value
|
|
|
|
int GetSelectionSum() {
|
|
|
|
int sum = -1;
|
2013-03-20 01:02:12 +04:00
|
|
|
for ( int R=0; R<rows(); R++ ) {
|
2020-07-01 19:03:10 +03:00
|
|
|
for ( int C=0; C<cols(); C++ ) {
|
|
|
|
if ( G_rowselect->value() ? row_selected(R) : is_selected(R,C) ) {
|
|
|
|
if ( sum == -1 ) sum = 0;
|
|
|
|
sum += R*C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(sum);
|
2012-04-14 23:45:50 +04:00
|
|
|
}
|
|
|
|
// Update the "Selection sum:" display
|
|
|
|
void UpdateSum() {
|
2020-07-01 19:03:10 +03:00
|
|
|
static char s[80];
|
|
|
|
int sum = GetSelectionSum();
|
|
|
|
if ( sum == -1 ) { sprintf(s, "(nothing selected)"); G_sum->color(48); }
|
|
|
|
else { sprintf(s, "%d", sum); G_sum->color(FL_WHITE); }
|
|
|
|
// Update only if different (lets one copy/paste from sum)
|
|
|
|
if ( strcmp(s,G_sum->value()))
|
|
|
|
{ G_sum->value(s); G_sum->redraw(); }
|
2012-04-14 23:45:50 +04:00
|
|
|
}
|
|
|
|
// Keyboard and mouse events
|
2022-12-30 21:14:36 +03:00
|
|
|
int handle(int e) FL_OVERRIDE {
|
2013-03-20 01:02:12 +04:00
|
|
|
int ret = Fl_Table_Row::handle(e);
|
2020-07-01 19:03:10 +03:00
|
|
|
if ( e == FL_KEYBOARD && Fl::event_key() == FL_Escape ) exit(0);
|
2012-04-14 23:45:50 +04:00
|
|
|
switch (e) {
|
2020-07-01 19:03:10 +03:00
|
|
|
case FL_PUSH:
|
|
|
|
case FL_RELEASE:
|
|
|
|
case FL_KEYUP:
|
|
|
|
case FL_KEYDOWN:
|
|
|
|
case FL_DRAG: {
|
|
|
|
//ret = 1; // *don't* indicate we 'handled' these, just update ('handling' prevents e.g. tab nav)
|
|
|
|
UpdateSum();
|
|
|
|
redraw();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FL_FOCUS: // tells FLTK we're interested in keyboard events
|
|
|
|
case FL_UNFOCUS:
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return(ret);
|
2012-04-14 23:45:50 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// User changed the 'row select' toggle button
|
|
|
|
void RowSelect_CB(Fl_Widget *w, void*) {
|
2020-07-01 19:03:10 +03:00
|
|
|
w->window()->redraw(); // redraw with changes applied
|
2012-04-14 23:45:50 +04:00
|
|
|
G_table->UpdateSum();
|
|
|
|
}
|
|
|
|
int main() {
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl::option(Fl::OPTION_ARROW_FOCUS, 0); // disable arrow focus nav (we want arrows to control cells)
|
2012-04-14 23:45:50 +04:00
|
|
|
Fl_Double_Window win(862, 312, "table-with-keynav");
|
|
|
|
win.begin();
|
2020-07-01 19:03:10 +03:00
|
|
|
// Create table
|
|
|
|
G_table = new MyTable(10, 30, win.w()-20, win.h()-70, "Times Table");
|
|
|
|
G_table->tooltip("Use mouse or Shift + Arrow Keys to make selections.\n"
|
|
|
|
"Sum of selected values is shown.");
|
|
|
|
// Row select toggle button
|
|
|
|
G_rowselect = new Fl_Toggle_Button(140,10,12,12,"Row selection");
|
|
|
|
G_rowselect->align(FL_ALIGN_LEFT);
|
|
|
|
G_rowselect->value(0);
|
|
|
|
G_rowselect->selection_color(FL_YELLOW);
|
|
|
|
G_rowselect->callback(RowSelect_CB);
|
|
|
|
G_rowselect->tooltip("Click to toggle row vs. row/col selection");
|
|
|
|
// Selection sum display
|
|
|
|
win.end();
|
|
|
|
win.begin();
|
|
|
|
G_sum = new Fl_Output(140,G_table->y()+G_table->h()+10,160,25,"Selection Sum:");
|
|
|
|
G_sum->value("(nothing selected)");
|
|
|
|
G_sum->color(48);
|
|
|
|
G_sum->tooltip("This field shows the sum of the selected cells in the table");
|
2012-04-14 23:45:50 +04:00
|
|
|
win.end();
|
|
|
|
win.resizable(G_table);
|
2020-07-01 19:03:10 +03:00
|
|
|
win.show();
|
2012-04-14 23:45:50 +04:00
|
|
|
return Fl::run();
|
|
|
|
}
|