Added table-spreadsheet example.
Various example mods. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@7994 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
parent
4525d46ed9
commit
7457f0dcaf
@ -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) \
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -30,14 +30,17 @@
|
||||
// http://www.fltk.org/str.php
|
||||
//
|
||||
#include <FL/Fl.H>
|
||||
#include <FL/Fl_Window.H>
|
||||
#include <FL/Fl_Double_Window.H>
|
||||
#include <FL/Fl_Table.H>
|
||||
#include <FL/fl_draw.H>
|
||||
|
||||
#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<MAX_ROWS; r++ )
|
||||
for ( int c=0; c<MAX_COLS; c++ )
|
||||
data[r][c] = 1000+(r*1000)+c;
|
||||
// Rows
|
||||
rows(10); // how many rows
|
||||
rows(MAX_ROWS); // how many rows
|
||||
row_header(1); // enable row headers (along left)
|
||||
row_height_all(20); // default height of rows
|
||||
row_resize(0); // disable row resizing
|
||||
// Cols
|
||||
cols(10); // how many columns
|
||||
cols(MAX_COLS); // how many columns
|
||||
col_header(1); // enable column headers (along top)
|
||||
col_width_all(80); // default width of columns
|
||||
col_resize(1); // enable column resizing
|
||||
@ -112,7 +115,7 @@ public:
|
||||
};
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
Fl_Window win(900, 400, "Simple Table");
|
||||
Fl_Double_Window win(900, 400, "Simple Table");
|
||||
MyTable table(10,10,880,380);
|
||||
win.end();
|
||||
win.resizable(table);
|
||||
|
270
examples/table-spreadsheet.cxx
Normal file
270
examples/table-spreadsheet.cxx
Normal file
@ -0,0 +1,270 @@
|
||||
//
|
||||
// "$Id$"
|
||||
//
|
||||
// Simple example of an interactive spreadsheet using Fl_Table.
|
||||
// Uses Mr. Satan's technique of instancing an Fl_Input around.
|
||||
//
|
||||
// Copyright 1998-2010 by Bill Spitzak and others.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Library General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Library General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Library General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
// USA.
|
||||
//
|
||||
// Please report all bugs and problems on the following page:
|
||||
//
|
||||
// http://www.fltk.org/str.php
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <FL/Fl.H>
|
||||
#include <FL/Fl_Double_Window.H>
|
||||
#include <FL/Fl_Int_Input.H>
|
||||
#include <FL/Fl_Table.H>
|
||||
#include <FL/fl_draw.H>
|
||||
|
||||
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; r<MAX_ROWS; ++r)
|
||||
sum += values[r][C];
|
||||
return(sum);
|
||||
}
|
||||
// Return the sum of all cols in this row
|
||||
int sum_cols(int R) {
|
||||
int sum = 0;
|
||||
for (int c=0; c<MAX_COLS; ++c)
|
||||
sum += values[R][c];
|
||||
return(sum);
|
||||
}
|
||||
// Return the sum of all cells in table
|
||||
int sum_all() {
|
||||
int sum = 0;
|
||||
for (int c=0; c<MAX_COLS; ++c)
|
||||
for (int r=0; r<MAX_ROWS; ++r)
|
||||
sum += values[r][c];
|
||||
return(sum);
|
||||
}
|
||||
};
|
||||
|
||||
// Handle drawing all cells in table
|
||||
void Spreadsheet::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: // table about to redraw
|
||||
break;
|
||||
|
||||
case CONTEXT_COL_HEADER: // table wants us to draw a column heading (C is column)
|
||||
fl_font(FL_HELVETICA | FL_BOLD, 14); // set font for heading to bold
|
||||
fl_push_clip(X,Y,W,H); // clip region for text
|
||||
{
|
||||
fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, col_header_color());
|
||||
fl_color(FL_BLACK);
|
||||
if (C == cols()-1) { // Last column? show 'TOTAL'
|
||||
fl_draw("TOTAL", X,Y,W,H, FL_ALIGN_CENTER);
|
||||
} else { // Not last column? show column letter
|
||||
sprintf(s, "%c", 'A' + C);
|
||||
fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER);
|
||||
}
|
||||
}
|
||||
fl_pop_clip();
|
||||
return;
|
||||
|
||||
case CONTEXT_ROW_HEADER: // table wants us to draw a row heading (R is row)
|
||||
fl_font(FL_HELVETICA | FL_BOLD, 14); // set font for row heading to bold
|
||||
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) { // Last row? Show 'Total'
|
||||
fl_draw("TOTAL", X,Y,W,H, FL_ALIGN_CENTER);
|
||||
} else { // Not last row? show row#
|
||||
sprintf(s, "%d", R+1);
|
||||
fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER);
|
||||
}
|
||||
}
|
||||
fl_pop_clip();
|
||||
return;
|
||||
|
||||
case CONTEXT_CELL: { // table wants us to draw a cell
|
||||
if (R == row_edit && C == col_edit && input->visible()) {
|
||||
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$".
|
||||
//
|
@ -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();
|
||||
}
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user