o Simplified example to take advantage of Fl_Table's existing features.
o Replaced hard coded row/col counts. o Enabled arrow keyboard navigation (setting Fl::OPTION_ARROW_FOCUS) o Fixed small bug in handle() that was calling Fl_Table::handle() instead of Fl_Table_Row::handle() that was preventing Fl_Table_Row's row selection from working.. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@9840 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
parent
9c6014f2d5
commit
bdd227806f
@ -40,24 +40,11 @@ MyTable *G_table = 0; // table widget
|
||||
Fl_Output *G_sum = 0; // displays sum of user's selection
|
||||
|
||||
class MyTable : public Fl_Table_Row {
|
||||
int row_beg, col_beg, row_end, col_end; // kb nav + mouse selection
|
||||
protected:
|
||||
// See if row R and column C is inside selection area
|
||||
int IsSelected(int R, int C) {
|
||||
if ( G_rowselect->value() == 0 )
|
||||
return( (R >= row_beg && R <= col_end &&
|
||||
C >= col_beg && C <= row_end) ? 1 : 0);
|
||||
else
|
||||
return( (R >= row_beg && R <= col_end) ? 1 : 0);
|
||||
}
|
||||
// Handle drawing all cells in table
|
||||
void draw_cell(TableContext context, int R=0,int C=0, int X=0,int Y=0,int W=0,int H=0) {
|
||||
static char s[30];
|
||||
switch ( context ) {
|
||||
case CONTEXT_STARTPAGE:
|
||||
// Whenever we redraw the table, update row/col selection vals first
|
||||
get_selection(row_beg, col_beg, col_end, row_end);
|
||||
break;
|
||||
case CONTEXT_COL_HEADER:
|
||||
case CONTEXT_ROW_HEADER:
|
||||
fl_font(FL_HELVETICA | FL_BOLD, 14);
|
||||
@ -74,8 +61,8 @@ protected:
|
||||
return;
|
||||
case CONTEXT_CELL: {
|
||||
// Keyboard nav and mouse selection highlighting
|
||||
int is_select = IsSelected(R,C);
|
||||
fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, is_select ? FL_YELLOW : FL_WHITE);
|
||||
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);
|
||||
{
|
||||
@ -112,9 +99,9 @@ public:
|
||||
// Update the displayed sum value
|
||||
int GetSelectionSum() {
|
||||
int sum = -1;
|
||||
for ( int R=0; R<11; R++ ) {
|
||||
for ( int C=0; C<11; C++ ) {
|
||||
if ( IsSelected(R,C) ) {
|
||||
for ( int R=0; R<rows(); R++ ) {
|
||||
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;
|
||||
}
|
||||
@ -133,10 +120,7 @@ public:
|
||||
}
|
||||
// Keyboard and mouse events
|
||||
int handle(int e) {
|
||||
// See if selection changed
|
||||
static int lastselect = 0;
|
||||
int thisselect = row_beg + (row_end*11) + (col_beg*11*2) + (col_end*11*3);
|
||||
int ret = Fl_Table::handle(e);
|
||||
int ret = Fl_Table_Row::handle(e);
|
||||
if ( e == FL_KEYBOARD && Fl::event_key() == FL_Escape ) exit(0);
|
||||
switch (e) {
|
||||
case FL_PUSH:
|
||||
@ -144,11 +128,8 @@ public:
|
||||
case FL_KEYUP:
|
||||
case FL_KEYDOWN:
|
||||
case FL_DRAG: {
|
||||
if ( lastselect != thisselect ) { // Selection changed?
|
||||
UpdateSum(); // update the sum
|
||||
redraw(); // XXX: needed for row selection to redraw properly
|
||||
lastselect = thisselect;
|
||||
}
|
||||
UpdateSum();
|
||||
redraw();
|
||||
ret = 1;
|
||||
break;
|
||||
}
|
||||
@ -167,11 +148,12 @@ void RowSelect_CB(Fl_Widget *w, void*) {
|
||||
G_table->UpdateSum();
|
||||
}
|
||||
int main() {
|
||||
Fl::option(Fl::OPTION_ARROW_FOCUS, 0); // disable arrow focus nav (we want arrows to control cells)
|
||||
Fl_Double_Window win(862, 312, "table-with-keynav");
|
||||
win.begin();
|
||||
// Create table
|
||||
G_table = new MyTable(10, 30, win.w()-20, win.h()-70, "Times Table");
|
||||
G_table->tooltip("Use mouse or arrow keys to make selections.\n"
|
||||
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");
|
||||
@ -186,6 +168,7 @@ int main() {
|
||||
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");
|
||||
win.end();
|
||||
win.resizable(G_table);
|
||||
win.show();
|
||||
|
Loading…
x
Reference in New Issue
Block a user