diff --git a/examples/Makefile b/examples/Makefile index ce001fc13..a58897545 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -7,6 +7,7 @@ ALL = howto-add_fd-and-popen$(EXEEXT) \ table-as-container$(EXEEXT) \ table-simple$(EXEEXT) \ table-sort$(EXEEXT) \ + table-spreadsheet$(EXEEXT) \ table-with-keyboard-nav$(EXEEXT) \ tabs-simple$(EXEEXT) \ textdisplay-with-colors$(EXEEXT) \ diff --git a/examples/howto-add_fd-and-popen.cxx b/examples/howto-add_fd-and-popen.cxx index 5e125d84c..c1f7df315 100644 --- a/examples/howto-add_fd-and-popen.cxx +++ b/examples/howto-add_fd-and-popen.cxx @@ -9,6 +9,7 @@ // while the command is running, keyboard navigation works, // text can be highlighted, and the interface can be resized. // +// Copyright 2010 Greg Ercolano. // Copyright 1998-2010 by Bill Spitzak and others. // // This library is free software; you can redistribute it and/or diff --git a/examples/howto-text-over-image-button.cxx b/examples/howto-text-over-image-button.cxx index a34782961..f4fad549f 100644 --- a/examples/howto-text-over-image-button.cxx +++ b/examples/howto-text-over-image-button.cxx @@ -1,9 +1,18 @@ // // "$Id$" // -// Simple example of a button with text over an image -// Originally from erco's cheat sheet 10/25/2010, permission by author. +// Simple example of a button with text over an image +// Originally from erco's cheat sheet 10/25/2010, permission by author. // +// This shows how to include an 'inline' image (.xpm) +// and have it appear on an Fl_Button. Demonstrates the use of the +// FL_ALIGN_IMAGE_BACKDROP align() flag (new in FLTK 1.3.0). +// +// Note that the XPM can just as easily be in an #include file, +// but to keep the example self contained, the image (a gray scale +// gradient) is included here. +// +// Copyright 2010 Greg Ercolano. // Copyright 1998-2010 by Bill Spitzak and others. // // This library is free software; you can redistribute it and/or diff --git a/examples/table-as-container.cxx b/examples/table-as-container.cxx index c86f00d3c..bc13db9a1 100644 --- a/examples/table-as-container.cxx +++ b/examples/table-as-container.cxx @@ -1,8 +1,18 @@ // // "$Id$" // -// Show how FLTK widgets can be parented by Fl_Table. +// Show how FLTK widgets can be parented by Fl_Table. -erco 03/30/2003 // +// Originally the 'widgettable.cxx' example program that came with +// erco's Fl_Table widget. Added to FLTK in 2010. +// +// This demonstrates how to use Fl_Table as a 'container' for FLTK +// widgets; one widget per cell. This isn't optimal for large tables, +// where it's better to make one instance of a widget, and move it to +// where it's needed. For an example of this, see the example program +// "table-simple-spreadsheet.cxx" +// +// Copyright 2010 Greg Ercolano. // Copyright 1998-2010 by Bill Spitzak and others. // // This library is free software; you can redistribute it and/or diff --git a/examples/table-simple.cxx b/examples/table-simple.cxx index 63c62b854..4db51b468 100644 --- a/examples/table-simple.cxx +++ b/examples/table-simple.cxx @@ -30,14 +30,17 @@ // http://www.fltk.org/str.php // #include -#include +#include #include #include +#define MAX_ROWS 30 +#define MAX_COLS 30 + // Derive a class from Fl_Table class MyTable : public Fl_Table { - int data[10][10]; // our 10x10 data array + int data[MAX_ROWS][MAX_COLS]; // data array for cells // Draw the row/col headings // Make this a dark thin upbox with the text inside. @@ -57,7 +60,7 @@ class MyTable : public Fl_Table { // Draw cell bg fl_color(FL_WHITE); fl_rectf(X,Y,W,H); // Draw cell data - fl_color(FL_DARK3); fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER); + fl_color(FL_GRAY0); fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER); // Draw box border fl_color(color()); fl_rect(X,Y,W,H); fl_pop_clip(); @@ -94,16 +97,16 @@ public: // MyTable(int X, int Y, int W, int H, const char *L=0) : Fl_Table(X,Y,W,H,L) { // Fill data array - for ( int x=0; x<10; x++ ) - for ( int y=0; y<10; y++ ) - data[x][y] = 1000+x*1000+y; + for ( int r=0; r +#include +#include +#include +#include +#include +#include + +const int MAX_COLS = 10; +const int MAX_ROWS = 10; + +class Spreadsheet : public Fl_Table { + Fl_Int_Input *input; // single instance of Fl_Int_Input widget + int values[MAX_ROWS][MAX_COLS]; // array of data for cells + int row_edit, col_edit; // row/col being modified + +protected: + void draw_cell(TableContext context, int=0, int=0, int=0, int=0, int=0, int=0); + void event_callback2(); // table's event callback (instance) + static void event_callback(Fl_Widget*, void *v) { // table's event callback (static) + ((Spreadsheet*)v)->event_callback2(); + } + // Handle input widget's callback + static void input_cb(Fl_Widget*, void* v) { + ((Spreadsheet*)v)->set_value_hide(); + } + +public: + Spreadsheet(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()); + // Create input widget that we'll use whenever user clicks on a cell + 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); // callback triggered when user hits Enter + input->maximum_size(5); + for (int c = 0; c < MAX_COLS; c++) + for (int r = 0; r < MAX_ROWS; r++) + values[r][c] = c + (r*MAX_COLS); // initialize cells + end(); + } + ~Spreadsheet() { } + + // Apply value from input widget to values[row][col] array and hide (done editing) + void set_value_hide() { + values[row_edit][col_edit] = atoi(input->value()); + input->hide(); + window()->cursor(FL_CURSOR_DEFAULT); // XXX: if we don't do this, cursor can disappear! + } + // Start editing a new cell: move the Fl_Int_Input widget to specified row/column + // Preload the widget with the cell's current value, + // and make the widget 'appear' at the cell's location. + // + void start_editing(int R, int C) { + row_edit = R; // Now editing this row/col + col_edit = C; + int X,Y,W,H; + find_cell(CONTEXT_CELL, R,C, X,Y,W,H); // Find X/Y/W/H of cell + input->resize(X,Y,W,H); // Move Fl_Input widget there + char s[30]; sprintf(s, "%d", values[R][C]); // Load input widget with cell's current value + input->value(s); + input->position(0,strlen(s)); // Select entire input field + input->show(); // Show the input widget, now that we've positioned it + input->take_focus(); + } + // Tell the input widget it's done editing, and to 'hide' + void done_editing() { + if (input->visible()) { // input widget visible, ie. edit in progress? + set_value_hide(); // Transfer its current contents to cell and hide + } + } + // Return the sum of all rows in this column + int sum_rows(int C) { + int sum = 0; + for (int r=0; rvisible()) { + return; // dont draw for cell with input widget over it + } + // Background + if ( C < cols()-1 && R < rows()-1 ) { + fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, FL_WHITE); + } else { + fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, 0xbbddbb00); + } + // Text + fl_push_clip(X+3, Y+3, W-6, H-6); + { + fl_color(FL_BLACK); + if (C == cols()-1 || R == rows()-1) { // Last row or col? Show total + fl_font(FL_HELVETICA | FL_BOLD, 14); // ..in bold font + if (C == cols()-1 && R == rows()-1) { // Last row+col? Total all cells + sprintf(s, "%d", sum_all()); + } else if (C == cols()-1) { // Row subtotal + sprintf(s, "%d", sum_cols(R)); + } else if (R == rows()-1) { // Col subtotal + sprintf(s, "%d", sum_rows(C)); + } + fl_draw(s, X+3,Y+3,W-6,H-6, FL_ALIGN_RIGHT); + } else { // Not last row or col? Show cell contents + fl_font(FL_HELVETICA, 14); // ..in regular font + sprintf(s, "%d", values[R][C]); + fl_draw(s, X+3,Y+3,W-6,H-6, FL_ALIGN_RIGHT); + } + } + fl_pop_clip(); + return; + } + + default: + return; + } +} + +// Callback whenever someone clicks on different parts of the table +void Spreadsheet::event_callback2() { + int R = callback_row(); + int C = callback_col(); + TableContext context = callback_context(); + + switch ( context ) { + case CONTEXT_CELL: { // A table event occurred on a cell + switch (Fl::event()) { // see what FLTK event caused it + case FL_PUSH: // mouse click? + if (!Fl::event_clicks()) { // single click? + done_editing(); // finish editing + } else { + Fl::event_clicks(0); // double click? zero clicks, fallthrough + if (R != rows()-1 && C != cols()-1 ) // only edit cells not in total's columns + start_editing(R,C); // start new edit + } + return; + + case FL_KEYBOARD: { + if ( Fl::event_key() == FL_Escape ) exit(0); // ESC closes app + if (C == cols()-1 || R == rows()-1) return; // can't edit totals column + done_editing(); // finish any previous editing + start_editing(R,C); // start new edit + if (Fl::event() == FL_KEYBOARD && Fl::e_text[0] != '\r') { + input->handle(Fl::event()); // pass keypress to input widget + } + return; + } + } + return; + } + + case CONTEXT_TABLE: // A table event occurred on dead zone in table + case CONTEXT_ROW_HEADER: // A table event occurred on row/column header + case CONTEXT_COL_HEADER: + done_editing(); // done editing, hide + return; + + default: + return; + } +} + +int main() { + Fl_Double_Window *win = new Fl_Double_Window(862, 322, "Fl_Table Spreadsheet"); + Spreadsheet *table = new Spreadsheet(10, 10, win->w()-20, win->h()-20); + // Table rows + table->row_header(1); + table->row_header_width(70); + table->row_resize(1); + table->rows(MAX_ROWS+1); // +1: leaves room for 'total row' + table->row_height_all(25); + // Table cols + table->col_header(1); + table->col_header_height(25); + table->col_resize(1); + table->cols(MAX_COLS+1); // +1: leaves room for 'total column' + table->col_width_all(70); + // Show window + win->end(); + win->resizable(table); + win->show(); + return Fl::run(); +} + +// +// End of "$Id$". +// diff --git a/examples/table-with-keyboard-nav.cxx b/examples/table-with-keyboard-nav.cxx index f101a71de..750dc151e 100644 --- a/examples/table-with-keyboard-nav.cxx +++ b/examples/table-with-keyboard-nav.cxx @@ -70,7 +70,6 @@ public: values[i][j] = (i + 2) * (j + 3); } } - (new Fl_Box(9999,9999,0,0))->hide(); // HACK: prevent flickering in Fl_Scroll end(); } ~SingleInput() { } @@ -161,16 +160,12 @@ void SingleInput::draw_cell(TableContext context, } // BACKGROUND - fl_push_clip(X, Y, W, H); - { - // Keyboard nav and mouse selection highlighting - if (R >= s_top && R <= s_bottom && C >= s_left && C <= s_right) { - 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); - } + // Keyboard nav and mouse selection highlighting + if (R >= s_top && R <= s_bottom && C >= s_left && C <= s_right) { + 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); @@ -311,9 +306,8 @@ void setrows_cb(Fl_Widget* w, void* v) { } int main() { - Fl_Double_Window win(600, 400, "table with keyboard nav"); - - SingleInput* table = new SingleInput(20, 20, win.w()-80, win.h()-80); + Fl_Double_Window *win = new Fl_Double_Window(600, 400, "table with keyboard nav"); + SingleInput* table = new SingleInput(20, 20, win->w()-80, win->h()-80); // Table rows table->row_header(1); table->row_header_width(70); @@ -329,10 +323,10 @@ int main() { table->set_selection(0,0,0,0); // select top/left cell // Add children to window - win.begin(); + win->begin(); // Row slider - Fl_Value_Slider setrows(win.w()-40,20,20,win.h()-80, 0); + Fl_Value_Slider setrows(win->w()-40,20,20,win->h()-80, 0); setrows.type(FL_VERT_NICE_SLIDER); setrows.bounds(2,MAX_ROWS); setrows.step(1); @@ -342,7 +336,7 @@ int main() { setrows.clear_visible_focus(); // Column slider - Fl_Value_Slider setcols(20,win.h()-40,win.w()-80,20, 0); + Fl_Value_Slider setcols(20,win->h()-40,win->w()-80,20, 0); setcols.type(FL_HOR_NICE_SLIDER); setcols.bounds(2,MAX_COLS); setcols.step(1); @@ -351,9 +345,9 @@ int main() { setcols.when(FL_WHEN_CHANGED); setcols.clear_visible_focus(); - win.end(); - win.resizable(table); - win.show(); + win->end(); + win->resizable(table); + win->show(); return Fl::run(); } diff --git a/examples/textdisplay-with-colors.cxx b/examples/textdisplay-with-colors.cxx index 9ccf04aa4..050674ca1 100644 --- a/examples/textdisplay-with-colors.cxx +++ b/examples/textdisplay-with-colors.cxx @@ -1,8 +1,14 @@ // // "$Id$" // -// A simple example of Fl_Text_Display with colors. -// For a color text editor, see the 'editor' example in the test directory. +// How to use Fl_Text_Display with colors. -erco 11/09/2010 +// Originally from erco's cheat sheet, permission by author. +// +// Shows how to use the two Fl_Text_Buffer's needed to manage +// the text and style info separately. +// +// For an example of a color text *editor*, see the 'editor' +// example in the test directory. // // Copyright 2010 Greg Ercolano. // Copyright 1998-2010 by Bill Spitzak and others.