2010-11-30 07:39:30 +03:00
|
|
|
//
|
|
|
|
// Simple example of using Fl_Table - Greg Ercolano 11/29/2010
|
|
|
|
//
|
|
|
|
// Demonstrates the simplest use of Fl_Table possible.
|
|
|
|
// Display a 10x10 array of integers with row/col headers.
|
|
|
|
// No interaction; simple display of data only.
|
|
|
|
// See other examples for more complex interactions with the table.
|
|
|
|
//
|
2010-12-10 11:09:27 +03:00
|
|
|
// Copyright 2010 Greg Ercolano.
|
2010-11-30 07:39:30 +03:00
|
|
|
// Copyright 1998-2010 by Bill Spitzak and others.
|
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// Please see the following page on how to report bugs and issues:
|
2010-11-30 07:39:30 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2010-11-30 07:39:30 +03:00
|
|
|
//
|
|
|
|
#include <FL/Fl.H>
|
2010-12-10 10:49:22 +03:00
|
|
|
#include <FL/Fl_Double_Window.H>
|
2010-11-30 07:39:30 +03:00
|
|
|
#include <FL/Fl_Table.H>
|
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
|
2010-12-10 10:49:22 +03:00
|
|
|
#define MAX_ROWS 30
|
2020-07-01 19:03:10 +03:00
|
|
|
#define MAX_COLS 26 // A-Z
|
2010-12-10 10:49:22 +03:00
|
|
|
|
2010-11-30 07:39:30 +03:00
|
|
|
// Derive a class from Fl_Table
|
|
|
|
class MyTable : public Fl_Table {
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
int data[MAX_ROWS][MAX_COLS]; // data array for cells
|
2010-11-30 07:39:30 +03:00
|
|
|
|
|
|
|
// Draw the row/col headings
|
|
|
|
// Make this a dark thin upbox with the text inside.
|
|
|
|
//
|
|
|
|
void DrawHeader(const char *s, int X, int Y, int W, int H) {
|
|
|
|
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);
|
|
|
|
fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER);
|
|
|
|
fl_pop_clip();
|
2020-07-01 19:03:10 +03:00
|
|
|
}
|
2010-11-30 07:39:30 +03:00
|
|
|
// Draw the cell data
|
|
|
|
// Dark gray text on white background with subtle border
|
|
|
|
//
|
|
|
|
void DrawData(const char *s, int X, int Y, int W, int H) {
|
|
|
|
fl_push_clip(X,Y,W,H);
|
|
|
|
// Draw cell bg
|
|
|
|
fl_color(FL_WHITE); fl_rectf(X,Y,W,H);
|
|
|
|
// Draw cell data
|
2010-12-10 10:49:22 +03:00
|
|
|
fl_color(FL_GRAY0); fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER);
|
2010-11-30 07:39:30 +03:00
|
|
|
// Draw box border
|
|
|
|
fl_color(color()); fl_rect(X,Y,W,H);
|
|
|
|
fl_pop_clip();
|
2020-07-01 19:03:10 +03:00
|
|
|
}
|
2010-11-30 07:39:30 +03:00
|
|
|
// Handle drawing table's cells
|
|
|
|
// Fl_Table calls this function to draw each visible cell in the table.
|
|
|
|
// It's up to us to use FLTK's drawing functions to draw the cells the way we want.
|
|
|
|
//
|
2022-12-30 21:14:36 +03:00
|
|
|
void draw_cell(TableContext context, int ROW=0, int COL=0, int X=0, int Y=0, int W=0, int H=0) FL_OVERRIDE {
|
2010-11-30 07:39:30 +03:00
|
|
|
static char s[40];
|
|
|
|
switch ( context ) {
|
|
|
|
case CONTEXT_STARTPAGE: // before page is drawn..
|
|
|
|
fl_font(FL_HELVETICA, 16); // set the font for our drawing operations
|
2020-07-01 19:03:10 +03:00
|
|
|
return;
|
2010-11-30 07:39:30 +03:00
|
|
|
case CONTEXT_COL_HEADER: // Draw column headers
|
|
|
|
sprintf(s,"%c",'A'+COL); // "A", "B", "C", etc.
|
|
|
|
DrawHeader(s,X,Y,W,H);
|
2020-07-01 19:03:10 +03:00
|
|
|
return;
|
2010-11-30 07:39:30 +03:00
|
|
|
case CONTEXT_ROW_HEADER: // Draw row headers
|
|
|
|
sprintf(s,"%03d:",ROW); // "001:", "002:", etc
|
|
|
|
DrawHeader(s,X,Y,W,H);
|
2020-07-01 19:03:10 +03:00
|
|
|
return;
|
2010-11-30 07:39:30 +03:00
|
|
|
case CONTEXT_CELL: // Draw data in cells
|
|
|
|
sprintf(s,"%d",data[ROW][COL]);
|
|
|
|
DrawData(s,X,Y,W,H);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
// Constructor
|
|
|
|
// Make our data array, and initialize the table options.
|
|
|
|
//
|
|
|
|
MyTable(int X, int Y, int W, int H, const char *L=0) : Fl_Table(X,Y,W,H,L) {
|
|
|
|
// Fill data array
|
2010-12-10 10:49:22 +03:00
|
|
|
for ( int r=0; r<MAX_ROWS; r++ )
|
|
|
|
for ( int c=0; c<MAX_COLS; c++ )
|
|
|
|
data[r][c] = 1000+(r*1000)+c;
|
2010-11-30 07:39:30 +03:00
|
|
|
// Rows
|
2010-12-10 10:49:22 +03:00
|
|
|
rows(MAX_ROWS); // how many rows
|
2010-11-30 07:39:30 +03:00
|
|
|
row_header(1); // enable row headers (along left)
|
|
|
|
row_height_all(20); // default height of rows
|
|
|
|
row_resize(0); // disable row resizing
|
|
|
|
// Cols
|
2010-12-10 10:49:22 +03:00
|
|
|
cols(MAX_COLS); // how many columns
|
2010-11-30 07:39:30 +03:00
|
|
|
col_header(1); // enable column headers (along top)
|
|
|
|
col_width_all(80); // default width of columns
|
|
|
|
col_resize(1); // enable column resizing
|
2020-07-01 19:03:10 +03:00
|
|
|
end(); // end the Fl_Table group
|
2010-11-30 07:39:30 +03:00
|
|
|
}
|
|
|
|
~MyTable() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2023-12-15 01:43:46 +03:00
|
|
|
Fl_Double_Window win(900, 400, "Table Simple");
|
2010-11-30 07:39:30 +03:00
|
|
|
MyTable table(10,10,880,380);
|
|
|
|
win.end();
|
|
|
|
win.resizable(table);
|
|
|
|
win.show(argc,argv);
|
|
|
|
return(Fl::run());
|
|
|
|
}
|