2010-11-30 20:02:11 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// table-sort -- An example application using a sortable Fl_Table
|
2010-11-30 20:02:11 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Originally the 'sortapp.cxx' example program that came with
|
|
|
|
// erco's Fl_Table widget. Added to FLTK in 2010.
|
2010-12-10 11:09:27 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Example of a non-trivial application that uses Fl_Table
|
2010-11-30 20:02:11 +03:00
|
|
|
// with sortable columns. This example is not trying to be simple,
|
2010-12-10 11:09:27 +03:00
|
|
|
// but to demonstrate the complexities of an actual app.
|
2010-11-30 20:02:11 +03:00
|
|
|
//
|
2010-12-10 11:09:27 +03:00
|
|
|
// Copyright 2010 Greg Ercolano.
|
2010-11-30 20:02:11 +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
|
2010-11-30 20:02:11 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
|
|
|
//
|
|
|
|
// https://www.fltk.org/bugs.php
|
2010-11-30 20:02:11 +03:00
|
|
|
//
|
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
2011-01-04 20:19:50 +03:00
|
|
|
#include <FL/Fl_Double_Window.H>
|
2010-11-30 20:02:11 +03:00
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <FL/Fl_Table_Row.H>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2016-04-01 00:48:15 +03:00
|
|
|
#include <ctype.h>
|
2020-07-23 06:14:07 +03:00
|
|
|
#include <string>
|
2010-11-30 20:02:11 +03:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm> // STL sort
|
|
|
|
|
|
|
|
#define MARGIN 20
|
|
|
|
|
2018-02-09 17:39:42 +03:00
|
|
|
#ifdef _WIN32
|
2010-11-30 20:02:11 +03:00
|
|
|
// WINDOWS
|
2011-01-05 20:58:16 +03:00
|
|
|
# define DIRCMD "dir"
|
2013-03-17 10:06:52 +04:00
|
|
|
static const char *G_header[] = { "Date", "Time", "Size", "Filename", "", "", "", "", "", 0 };
|
2011-01-05 20:58:16 +03:00
|
|
|
# ifdef _MSC_VER
|
|
|
|
# define popen _popen
|
|
|
|
# endif
|
2018-02-09 17:39:42 +03:00
|
|
|
#else /* _WIN32 */
|
2010-11-30 20:02:11 +03:00
|
|
|
// UNIX
|
2011-01-05 20:58:16 +03:00
|
|
|
# define DIRCMD "ls -l"
|
2013-03-17 10:06:52 +04:00
|
|
|
static const char *G_header[] = { "Perms", "#L", "Own", "Group", "Size", "Date", "", "", "Filename", 0 };
|
2018-02-09 17:39:42 +03:00
|
|
|
#endif /* _WIN32 */
|
2010-11-30 20:02:11 +03:00
|
|
|
|
2013-03-17 10:06:52 +04:00
|
|
|
// Font face/sizes for header and rows
|
|
|
|
#define HEADER_FONTFACE FL_HELVETICA_BOLD
|
|
|
|
#define HEADER_FONTSIZE 16
|
|
|
|
#define ROW_FONTFACE FL_HELVETICA
|
|
|
|
#define ROW_FONTSIZE 16
|
|
|
|
|
2010-11-30 20:02:11 +03:00
|
|
|
// A single row of columns
|
|
|
|
class Row {
|
|
|
|
public:
|
2020-07-23 06:14:07 +03:00
|
|
|
std::vector<std::string> cols;
|
2010-11-30 20:02:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Sort class to handle sorting column using std::sort
|
|
|
|
class SortColumn {
|
|
|
|
int _col, _reverse;
|
|
|
|
public:
|
|
|
|
SortColumn(int col, int reverse) {
|
|
|
|
_col = col;
|
|
|
|
_reverse = reverse;
|
|
|
|
}
|
|
|
|
bool operator()(const Row &a, const Row &b) {
|
2020-07-23 06:14:07 +03:00
|
|
|
const char *ap = ( _col < (int)a.cols.size() ) ? a.cols[_col].c_str() : "",
|
|
|
|
*bp = ( _col < (int)b.cols.size() ) ? b.cols[_col].c_str() : "";
|
2010-11-30 20:02:11 +03:00
|
|
|
if ( isdigit(*ap) && isdigit(*bp) ) { // cheezy detection of numeric data
|
|
|
|
// Numeric sort
|
|
|
|
int av=0; sscanf(ap, "%d", &av);
|
|
|
|
int bv=0; sscanf(bp, "%d", &bv);
|
|
|
|
return( _reverse ? av < bv : bv < av );
|
|
|
|
} else {
|
|
|
|
// Alphabetic sort
|
|
|
|
return( _reverse ? strcmp(ap, bp) > 0 : strcmp(ap, bp) < 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Derive a custom class from Fl_Table_Row
|
|
|
|
class MyTable : public Fl_Table_Row {
|
|
|
|
private:
|
2020-07-23 06:14:07 +03:00
|
|
|
std::vector<Row> rowdata_; // data in each row
|
|
|
|
int sort_reverse_;
|
|
|
|
int sort_lastcol_;
|
2010-11-30 20:02:11 +03:00
|
|
|
|
|
|
|
static void event_callback(Fl_Widget*, void*);
|
|
|
|
void event_callback2(); // callback for table events
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void draw_cell(TableContext context, int R=0, int C=0, // table cell drawing
|
|
|
|
int X=0, int Y=0, int W=0, int H=0);
|
|
|
|
void sort_column(int col, int reverse=0); // sort table by a column
|
2012-06-12 22:32:36 +04:00
|
|
|
void draw_sort_arrow(int X,int Y,int W,int H);
|
2010-11-30 20:02:11 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Ctor
|
|
|
|
MyTable(int x, int y, int w, int h, const char *l=0) : Fl_Table_Row(x,y,w,h,l) {
|
2020-07-23 06:14:07 +03:00
|
|
|
sort_reverse_ = 0;
|
|
|
|
sort_lastcol_ = -1;
|
2010-11-30 20:02:11 +03:00
|
|
|
end();
|
|
|
|
callback(event_callback, (void*)this);
|
|
|
|
}
|
|
|
|
~MyTable() { } // Dtor
|
|
|
|
void load_command(const char *cmd); // Load the output of a command into table
|
|
|
|
void autowidth(int pad); // Automatically set column widths to data
|
|
|
|
void resize_window(); // Resize parent window to size of table
|
|
|
|
};
|
|
|
|
|
|
|
|
// Sort a column up or down
|
|
|
|
void MyTable::sort_column(int col, int reverse) {
|
2020-07-23 06:14:07 +03:00
|
|
|
std::sort(rowdata_.begin(), rowdata_.end(), SortColumn(col, reverse));
|
2010-11-30 20:02:11 +03:00
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw sort arrow
|
2012-06-12 22:32:36 +04:00
|
|
|
void MyTable::draw_sort_arrow(int X,int Y,int W,int H) {
|
2010-11-30 20:02:11 +03:00
|
|
|
int xlft = X+(W-6)-8;
|
|
|
|
int xctr = X+(W-6)-4;
|
|
|
|
int xrit = X+(W-6)-0;
|
|
|
|
int ytop = Y+(H/2)-4;
|
|
|
|
int ybot = Y+(H/2)+4;
|
2020-07-23 06:14:07 +03:00
|
|
|
if ( sort_reverse_ ) {
|
2010-11-30 20:02:11 +03:00
|
|
|
// Engraved down arrow
|
|
|
|
fl_color(FL_WHITE);
|
|
|
|
fl_line(xrit, ytop, xctr, ybot);
|
|
|
|
fl_color(41); // dark gray
|
|
|
|
fl_line(xlft, ytop, xrit, ytop);
|
|
|
|
fl_line(xlft, ytop, xctr, ybot);
|
|
|
|
} else {
|
|
|
|
// Engraved up arrow
|
|
|
|
fl_color(FL_WHITE);
|
|
|
|
fl_line(xrit, ybot, xctr, ytop);
|
|
|
|
fl_line(xrit, ybot, xlft, ybot);
|
|
|
|
fl_color(41); // dark gray
|
|
|
|
fl_line(xlft, ybot, xctr, ytop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle drawing all cells in table
|
|
|
|
void MyTable::draw_cell(TableContext context, int R, int C, int X, int Y, int W, int H) {
|
2020-07-01 19:03:10 +03:00
|
|
|
const char *s = "";
|
2020-07-23 06:14:07 +03:00
|
|
|
if ( R < (int)rowdata_.size() && C < (int)rowdata_[R].cols.size() )
|
|
|
|
s = rowdata_[R].cols[C].c_str();
|
2010-11-30 20:02:11 +03:00
|
|
|
switch ( context ) {
|
|
|
|
case CONTEXT_COL_HEADER:
|
|
|
|
fl_push_clip(X,Y,W,H); {
|
|
|
|
fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, FL_BACKGROUND_COLOR);
|
|
|
|
if ( C < 9 ) {
|
2020-07-01 19:03:10 +03:00
|
|
|
fl_font(HEADER_FONTFACE, HEADER_FONTSIZE);
|
2010-11-30 20:02:11 +03:00
|
|
|
fl_color(FL_BLACK);
|
2020-07-01 19:03:10 +03:00
|
|
|
fl_draw(G_header[C], X+2,Y,W,H, FL_ALIGN_LEFT, 0, 0); // +2=pad left
|
2010-11-30 20:02:11 +03:00
|
|
|
// Draw sort arrow
|
2020-07-23 06:14:07 +03:00
|
|
|
if ( C == sort_lastcol_ ) {
|
2012-06-12 22:32:36 +04:00
|
|
|
draw_sort_arrow(X,Y,W,H);
|
2010-11-30 20:02:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fl_pop_clip();
|
2020-07-01 19:03:10 +03:00
|
|
|
return;
|
2010-11-30 20:02:11 +03:00
|
|
|
case CONTEXT_CELL: {
|
|
|
|
fl_push_clip(X,Y,W,H); {
|
|
|
|
// Bg color
|
|
|
|
Fl_Color bgcolor = row_selected(R) ? selection_color() : FL_WHITE;
|
2020-07-01 19:03:10 +03:00
|
|
|
fl_color(bgcolor); fl_rectf(X,Y,W,H);
|
|
|
|
fl_font(ROW_FONTFACE, ROW_FONTSIZE);
|
|
|
|
fl_color(FL_BLACK); fl_draw(s, X+2,Y,W,H, FL_ALIGN_LEFT); // +2=pad left
|
2010-11-30 20:02:11 +03:00
|
|
|
// Border
|
|
|
|
fl_color(FL_LIGHT2); fl_rect(X,Y,W,H);
|
|
|
|
}
|
|
|
|
fl_pop_clip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Automatically set column widths to widest data in each column
|
|
|
|
void MyTable::autowidth(int pad) {
|
2013-03-17 10:06:52 +04:00
|
|
|
int w, h;
|
|
|
|
// Initialize all column widths to header width
|
|
|
|
fl_font(HEADER_FONTFACE, HEADER_FONTSIZE);
|
|
|
|
for ( int c=0; G_header[c]; c++ ) {
|
2020-07-01 19:03:10 +03:00
|
|
|
w=0; fl_measure(G_header[c], w, h, 0); // pixel width of header text
|
2013-03-17 10:06:52 +04:00
|
|
|
col_width(c, w+pad);
|
|
|
|
}
|
|
|
|
fl_font(ROW_FONTFACE, ROW_FONTSIZE);
|
2020-07-23 06:14:07 +03:00
|
|
|
for ( int r=0; r<(int)rowdata_.size(); r++ ) {
|
|
|
|
for ( int c=0; c<(int)rowdata_[r].cols.size(); c++ ) {
|
|
|
|
w=0; fl_measure(rowdata_[r].cols[c].c_str(), w, h, 0); // pixel width of row text
|
2010-11-30 20:02:11 +03:00
|
|
|
if ( (w + pad) > col_width(c)) col_width(c, w + pad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
table_resized();
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resize parent window to size of table
|
|
|
|
void MyTable::resize_window() {
|
|
|
|
// Determine exact outer width of table with all columns visible
|
2020-07-23 06:14:07 +03:00
|
|
|
int width = 2; // width of table borders
|
2010-11-30 20:02:11 +03:00
|
|
|
for ( int t=0; t<cols(); t++ ) width += col_width(t); // total width of all columns
|
2020-07-23 06:14:07 +03:00
|
|
|
width += vscrollbar->w(); // include width of scrollbar
|
2010-11-30 20:02:11 +03:00
|
|
|
width += MARGIN*2;
|
|
|
|
if ( width < 200 || width > Fl::w() ) return;
|
|
|
|
window()->resize(window()->x(), window()->y(), width, window()->h()); // resize window to fit
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load table with output of 'cmd'
|
|
|
|
void MyTable::load_command(const char *cmd) {
|
|
|
|
char s[512];
|
|
|
|
FILE *fp = popen(cmd, "r");
|
|
|
|
cols(0);
|
|
|
|
for ( int r=0; fgets(s, sizeof(s)-1, fp); r++ ) {
|
2020-07-23 06:14:07 +03:00
|
|
|
if ( r==0 && strncmp(s,"total ",6)==0) { --r; continue; }
|
2010-11-30 20:02:11 +03:00
|
|
|
// Add a new row
|
2020-07-23 06:14:07 +03:00
|
|
|
Row newrow; rowdata_.push_back(newrow);
|
|
|
|
std::vector<std::string> &rc = rowdata_[r].cols;
|
2010-11-30 20:02:11 +03:00
|
|
|
// Break line into separate word 'columns'
|
|
|
|
char *ss;
|
|
|
|
const char *delim = " \t\n";
|
|
|
|
for(int t=0; (t==0)?(ss=strtok(s,delim)):(ss=strtok(NULL,delim)); t++) {
|
2020-07-23 06:14:07 +03:00
|
|
|
rc.push_back(ss); // char* -> std::string
|
2010-11-30 20:02:11 +03:00
|
|
|
}
|
|
|
|
// Keep track of max # columns
|
|
|
|
if ( (int)rc.size() > cols() ) {
|
|
|
|
cols((int)rc.size());
|
|
|
|
}
|
2020-07-01 19:03:10 +03:00
|
|
|
}
|
2010-11-30 20:02:11 +03:00
|
|
|
// How many rows we loaded
|
2020-07-23 06:14:07 +03:00
|
|
|
rows((int)rowdata_.size());
|
2010-11-30 20:02:11 +03:00
|
|
|
// Auto-calculate widths, with 20 pixel padding
|
|
|
|
autowidth(20);
|
2021-06-23 17:49:38 +03:00
|
|
|
pclose(fp);
|
2010-11-30 20:02:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Callback whenever someone clicks on different parts of the table
|
|
|
|
void MyTable::event_callback(Fl_Widget*, void *data) {
|
|
|
|
MyTable *o = (MyTable*)data;
|
|
|
|
o->event_callback2();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyTable::event_callback2() {
|
|
|
|
//int ROW = callback_row(); // unused
|
|
|
|
int COL = callback_col();
|
|
|
|
TableContext context = callback_context();
|
|
|
|
switch ( context ) {
|
|
|
|
case CONTEXT_COL_HEADER: { // someone clicked on column header
|
|
|
|
if ( Fl::event() == FL_RELEASE && Fl::event_button() == 1 ) {
|
2020-07-23 06:14:07 +03:00
|
|
|
if ( sort_lastcol_ == COL ) { // Click same column? Toggle sort
|
|
|
|
sort_reverse_ ^= 1;
|
2020-07-01 19:03:10 +03:00
|
|
|
} else { // Click diff column? Up sort
|
2020-07-23 06:14:07 +03:00
|
|
|
sort_reverse_ = 0;
|
2010-11-30 20:02:11 +03:00
|
|
|
}
|
2020-07-23 06:14:07 +03:00
|
|
|
sort_column(COL, sort_reverse_);
|
|
|
|
sort_lastcol_ = COL;
|
2010-11-30 20:02:11 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2011-01-04 20:19:50 +03:00
|
|
|
Fl_Double_Window win(900,500,"Table Sorting");
|
2010-11-30 20:02:11 +03:00
|
|
|
MyTable table(MARGIN, MARGIN, win.w()-MARGIN*2, win.h()-MARGIN*2);
|
|
|
|
table.selection_color(FL_YELLOW);
|
|
|
|
table.col_header(1);
|
|
|
|
table.col_resize(1);
|
|
|
|
table.when(FL_WHEN_RELEASE); // handle table events on release
|
|
|
|
table.load_command(DIRCMD); // load table with a directory listing
|
|
|
|
table.row_height_all(18); // height of all rows
|
2020-07-01 19:03:10 +03:00
|
|
|
table.tooltip("Click on column headings to toggle column sorting");
|
|
|
|
table.color(FL_WHITE);
|
2010-11-30 20:02:11 +03:00
|
|
|
win.end();
|
|
|
|
win.resizable(table);
|
|
|
|
table.resize_window();
|
|
|
|
win.show();
|
|
|
|
return(Fl::run());
|
|
|
|
}
|