On 01/03/17 10:46, Greg Ercolano wrote:
> On 01/03/17 01:06, Duncan Gibson wrote:
>> The vertical scrollbar to be shown automatically, as expected.
>> However, triggering the vertical scrollbar cuts the horizontal
>> display area, so the horizontal scrollbar is triggered as well,
>> and this takes up one of the precious rows:
>>
>> +=====+==============================+
>> | #Id | Date / Time |^|
>> +-----+----------------------------+-+
>> | #21 | 2017-01-02 / 09:20:25.1 |#|
>> +-----+----------------------------+ +
>> | #25 | 2017-01-02 / 09:32:02.5 |V|
>> +-+---+----------------------------+-+
>> |<|################### |>| |
>> +=+===+============================+=+
>>
>> I could reduce the width of the second column but that will
>> leave a grey area when the vertical scrollbar is not shown.
>>
>> So my question: is there a simple way to disable the display of
>> the horizontal scrollbar, or do I have to derive my own Table
>> and implement/override the scrollbar behaviour?
>
> [..]
> I'd start by implementing your method that adds new rows to the table,
> doing checks there after the row is added to see if the table's
> size needs the vertical scrollbar, and if so, trigger the above behavior.
>
> If the user can also change the size of the window, then override
> resize() too, so that the above logic can be called.
Here's an example that should look a lot like what you described.
The horizontal scrollbar shouldn't appear when the window is resized
or rows added/removed, esp. when the vert scroll appears/disappears.
This is just a modified version of the fltk examples/simple-table.cxx
code. The real work is done by the FixColumnSize() method, just a few
lines of added code:
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Table.H>
#include <FL/fl_draw.H>
// Demonstrate resizing right column to fit window to avoid horiz scrollbar appearance
// erco 01/03/17
// Derive a class from Fl_Table
class MyTable : public Fl_Table {
// 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_LEFT);
fl_pop_clip();
}
// 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
fl_color(FL_GRAY0); fl_draw(s, X,Y,W,H, FL_ALIGN_LEFT);
// Draw box border
fl_color(color()); fl_rect(X,Y,W,H);
fl_pop_clip();
}
// 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.
//
void draw_cell(TableContext context, int ROW=0, int COL=0, int X=0, int Y=0, int W=0, int H=0) {
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
return;
case CONTEXT_COL_HEADER: // Draw column headers
switch(COL) {
case 0: DrawHeader(" #Id",X,Y,W,H); break;
case 1: DrawHeader(" Date / Time",X,Y,W,H); break;
}
return;
case CONTEXT_ROW_HEADER: // Draw row headers
return;
case CONTEXT_CELL: // Draw data in cells
switch(COL) {
case 0: sprintf(s, " #%d", ROW); DrawData(s,X,Y,W,H); break;
case 1: sprintf(s, " 2017-01-02 / 09:20:25.%d", ROW); DrawData(s,X,Y,W,H); break;
}
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) {
// Rows
rows(10); // how many rows
row_header(0); // disable row headers (along left)
row_height_all(20); // default height of rows
row_resize(0); // disable interactive row resizing
// Cols
cols(2); // how many columns
col_header(1); // enable column headers (along top)
col_width(0, 50); // fixed width for left column
col_width(1, 300); // fixed width for right column (changed later by FixColumnSize()..)
col_resize(0); // disable interactive column resizing
end(); // end the Fl_Table group
FixColumnSize(); // apply our auto-column-sizing behavior
}
~MyTable() { }
// Fix the right column's size to precisely match width of window
void FixColumnSize() {
int X,Y,W,H;
find_cell(CONTEXT_CELL,0,1,X,Y,W,H); // get xywh of right column cell in first row
int off = (X+W) - (tox+tow); // we just need X pos and width. Compute offset from table's outer size
int oldw = col_width(1); // save old col width
col_width(1, oldw - off); // set new column width based on offset for perfect fit
}
// Handle window resizing
void resize(int X,int Y,int W,int H) {
Fl_Table::resize(X,Y,W,H);
FixColumnSize(); // after letting window resize, fix our right most column
}
};
// Add more rows
void More_CB(Fl_Widget *w, void *d) {
MyTable *table = (MyTable*)d;
table->rows(table->rows()+1);
table->FixColumnSize();
}
// Remove rows
void Less_CB(Fl_Widget *w, void *d) {
MyTable *table = (MyTable*)d;
if ( table->rows() > 1 ) table->rows(table->rows()-1);
table->FixColumnSize();
}
int main(int argc, char **argv) {
Fl_Double_Window *win = new Fl_Double_Window(500, 400, "Simple Table");
MyTable *table = new MyTable(10,10,win->w()-20,340);
Fl_Button more(10, 360,100,25,"+ Row"); more.callback(More_CB, (void*)table);
Fl_Button less(220,360,100,25,"- Row"); less.callback(Less_CB, (void*)table);
win->end();
win->resizable(table);
win->show(argc,argv);
return(Fl::run());
}