On 03/31/12 16:01, Jeff Paranich wrote:
> I won't be in front of the code again until Monday but I will try
> the advice below first thing Monday and see where it leads.
Sounds good.
Here's some original example code that came with the original
Fl_Table LGPL project that I should probably move over to FLTK.
I'm in the process of making some tweaks to it to simplify and
be FLTK code compliant, but before I finish it, you can use this
to play with.
It shows how the arrow keys and shift-arrow keys can be used
to make a 'box' selection. This can be easily modified (see
previous post) to do entire row selections.
In fact, I was thinking of modifying the example to include a
checkbox to let one set 'row' or 'cell' selection, so that one
can easily see how to do each.
But meanwhile, this shows how to do cell selection, and shift-arrow
can be used to make box selections. It doesn't have a key-nav way
to toggle selections (eg. Enter or Spacebar) because in this example
(a spreadsheet) these can be used for cell input.
I should probably make an alternate example that doesn't include
the cell-input stuff to keep the example even simpler. Anyway:
//
// Test Jean-Marc's mods for keyboard nav and mouse selection, using a modified
// version of the singleinput program.
//
// 1.00 04/18/03 Mister Satan -- Initial implementation
// 1.10 05/17/03 Greg Ercolano -- Small mods to follow changes to
Fl_Table
// 1.20 02/22/04 Jean-Marc Lienher -- Keyboard nav and mouse selection
// 1.21 02/22/04 Greg Ercolano -- Small reformatting mods, comments
// 1.30 03/30/12 Greg Ercolano -- Imported to FLTK 1.3.1
//
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Int_Input.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Table.H>
#include <stdio.h>
#include <stdlib.h>
const int MAX_COLS = 26;
const int MAX_ROWS = 500;
class SingleInput : public Fl_Table {
Fl_Int_Input* input;
int data[MAX_ROWS][MAX_COLS];
int row_edit, col_edit;
int row_beg, col_beg, row_end, col_end; // kb nav + mouse selection
protected:
void draw_cell(TableContext context, int=0, int=0, int=0, int=0, int=0,
int=0);
static void event_callback(Fl_Widget*, void *v) {
((SingleInput*)v)->event_callback(); }
void event_callback();
// Someone finished typing something into the input field
static void input_cb(Fl_Widget*, void* v) {
((SingleInput*)v)->set_value();
((SingleInput*)v)->window()->cursor(FL_CURSOR_DEFAULT);
}
public:
void set_value() { data[row_edit][col_edit] = atoi(input->value());
input->hide(); }
// CTOR
SingleInput(int x, int y, int w, int h, const char *l=0) :
Fl_Table(x,y,w,h,l) {
callback(&event_callback, (void*)this);
when(FL_WHEN_NOT_CHANGED|when());
// Integer input widget
begin();
input = new Fl_Int_Input(w/2,h/2,0,0);
input->hide();
input->callback(input_cb, (void*)this);
input->when(FL_WHEN_ENTER_KEY_ALWAYS);
input->maximum_size(5);
end();
// Initialize table data to some ordered values
for (int R=0; R<MAX_ROWS; R++)
for (int C=0; C<MAX_COLS; C++)
data[R][C] = (R+2)*(C+3);
// Rows
row_header(1);
row_header_width(70);
row_resize(1);
rows(11);
row_height_all(20);
// Cols
col_header(1);
col_header_height(20);
col_resize(1);
cols(11);
col_width_all(70);
}
~SingleInput() { }
void rows(int val) { if (input->visible()) input->do_callback();
Fl_Table::rows(val); }
void cols(int val) { if (input->visible()) input->do_callback();
Fl_Table::cols(val); }
int rows() { return Fl_Table::rows(); }
int cols() { return Fl_Table::cols(); }
};
// Handle drawing all cells in table
void SingleInput::draw_cell(TableContext context, int R,int C, int X,int Y,int
W,int H) {
static char s[30];
switch ( context ) {
case CONTEXT_STARTPAGE:
get_selection(row_beg, col_beg, col_end, row_end); // get
keyboard nav 'selection region'
break;
case CONTEXT_COL_HEADER:
fl_font(FL_HELVETICA | FL_BOLD, 14);
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);
if (C != cols()-1) {
s[0] = 'A' + C;
s[1] = '\0';
fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER);
} else {
fl_draw("TOTAL", X, Y, W, H, FL_ALIGN_CENTER);
}
}
fl_pop_clip();
return;
case CONTEXT_ROW_HEADER:
fl_font(FL_HELVETICA | FL_BOLD, 14);
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);
if (R != rows()-1) {
sprintf(s, "%d", R+1);
fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER);
} else {
fl_draw("TOTAL", X, Y, W, H, FL_ALIGN_CENTER);
}
}
fl_pop_clip();
return;
case CONTEXT_CELL: {
if (R == row_edit && C == col_edit && input->visible()) return;
// BACKGROUND
fl_push_clip(X, Y, W, H);
{
// Keyboard nav and mouse selection highlighting
if (R >= row_beg && R <= col_end && C >= col_beg && C <=
row_end) {
fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, FL_YELLOW);
} else {
fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, FL_WHITE);
}
}
fl_pop_clip();
// TEXT
fl_push_clip(X+3, Y+3, W-6, H-6);
{
fl_color(FL_BLACK);
if (C != cols()-1 && R != rows()-1) {
fl_font(FL_HELVETICA, 14);
sprintf(s, "%d", data[R][C]);
fl_draw(s, X+3, Y+3, W-6, H-6, FL_ALIGN_RIGHT);
} else {
int T = 0;
fl_font(FL_HELVETICA | FL_BOLD, 14);
if (C == cols()-1 && R == rows()-1) { // TOTAL
for (int c=0; c<cols()-1; ++c)
for (int r=0; r<rows()-1; ++r)
T += data[r][c];
} else if (C == cols()-1) { // ROW SUBTOTAL
for (int c=0; c<cols()-1; ++c)
T += data[R][c];
} else if (R == rows()-1) { // COL SUBTOTAL
for (int r=0; r<rows()-1; ++r)
T += data[r][C];
}
sprintf(s, "%d", T);
fl_draw(s, X+3, Y+3, W-6, H-6, FL_ALIGN_RIGHT);
}
}
fl_pop_clip();
return;
}
case CONTEXT_RC_RESIZE: {
if (!input->visible()) return;
find_cell(CONTEXT_TABLE, row_edit, col_edit, X, Y, W, H);
if (X==input->x() && Y==input->y() && W==input->w() &&
H==input->h()) {
return;
}
input->resize(X,Y,W,H);
return;
}
default:
return;
}
}
// Someone clicked on the table
void SingleInput::event_callback() {
int R = callback_row();
int C = callback_col();
TableContext context = callback_context();
switch ( context ) {
case CONTEXT_CELL: {
fprintf(stderr, "CALLBACK: CONTEXT_CELL: for R/C: %d / %d\n", R, C);
switch (Fl::event()) {
case FL_PUSH:
if (!Fl::event_clicks()) {
if (input->visible()) input->do_callback();
input->hide();
return;
}
Fl::event_clicks(0);
//FALLTHROUGH
case FL_KEYBOARD:
if (Fl::event() == FL_KEYBOARD && Fl::event_key() ==
FL_Escape ) exit(1);
if (Fl::event() == FL_KEYBOARD && Fl::e_length == 0)
return;// non-text char?
if (C == cols()-1 || R == rows()-1) return;
// ignore last row/cols (has totals)
if (input->visible()) {
input->do_callback();
// widget already visible?
} else {
row_edit = R;
// keep track of row/col we're editing
col_edit = C;
set_selection(R,C,R,C);
// set selection to this widget
int XX,YY,WW,HH;
find_cell(CONTEXT_CELL, R, C, XX, YY, WW, HH);
input->resize(XX,YY,WW,HH);
char s[30];
sprintf(s, "%d", data[R][C]);
input->value(s);
input->show();
input->take_focus();
}
if (Fl::event() == FL_KEYBOARD && Fl::e_text[0] != '\r') {
input->handle(Fl::event());
}
return;
}
return;
}
case CONTEXT_ROW_HEADER:
case CONTEXT_COL_HEADER:
if (input->visible()) input->do_callback();
input->hide();
return;
case CONTEXT_TABLE:
if (R < 0 && C < 0) {
if (input->visible()) input->do_callback();
input->hide();
}
return;
default:
return;
}
}
int main() {
Fl_Double_Window win(862, 262, "table-with-keynav");
SingleInput* table = new SingleInput(10, 10, win.w()-20, win.h()-20);
table->tooltip("Use arrow keys to move cell 'cursor'.\n"
"Use Shift + arrow keys to enlarge cell selection.\n");
win.resizable(table);
win.show();
return Fl::run();
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk