How do I make a dynamic table

50 views
Skip to first unread message

Hannu Vuolasaho

unread,
Jul 10, 2014, 7:04:25 PM7/10/14
to fltkg...@googlegroups.com
Hello everyone!

I have been playing around with fltk week or so and I've hit
maybe the most powerful widget there is. Fl_Table can be
(mis)used so many ways.

Now. Every example so far I have seen has constructor and for
loop there which isn't what I'm looking for.

I want something like this

container * cont = new container(x,y,w,h);
... later ...
cont->appendRow(1,2,"foo"); //zero to 65535 times
cont->sort();

I need to subclass something, but which way is right way?
There was pack and scroll example and some other ways to
do this.

And at least Fl_Table has understanding of rows only. How
columns are handled?

I'm also worrying about column width in sense how to handle
easily longer data. And in the code I don't like to do so much
tedious coordinate calculation.

What should I read more?

Best regards,
Hannu Vuolasaho

Greg Ercolano

unread,
Jul 10, 2014, 7:52:19 PM7/10/14
to fltkg...@googlegroups.com
On 07/10/14 16:04, Hannu Vuolasaho wrote:
> I want something like this
>
> container * cont = new container(x,y,w,h);
> ... later ...
> cont->appendRow(1,2,"foo"); //zero to 65535 times
> cont->sort();
>
> I need to subclass something, but which way is right way?


Look at examples/table-simple.cxx and examples/table-spreadsheet.cxx

table-simple shows a read only way to look at a table of
integer values (int data[MAX_ROWS][MAX_COLS]).

table-spreadsheet shows an editable table of integers
(int values[MAX_ROWS][MAX_COLS]), and lets you do things
like cell selections (left drags).

The only thing different about your case is you want strings
instead of integers, so work with a copy of one of those examples
and change it around to use e.g. a std::vector of std::string,
or a database, or a const char * array of string pointers.

Data management is entirely up to you; Fl_Table handles the UI
component, your derived class handles managing the data, and Fl_Table
invokes your derived class's draw_cell() method to tell it which
cell to draw, and will tell you the xywh value of where to draw it.

Fl_Table handles the UI drawing, but does not manage your data.
This allows you to manage the data in whatever way is best for
your application. For some people managing the data in an array
is best, for some a database, and for others the table data isn't
in memory at all, and can be purely algorithmic.

> And at least Fl_Table has understanding of rows only. How
> columns are handled?

See above examples.

> I'm also worrying about column width in sense how to handle
> easily longer data.

See the Fl_Table::col_width(col,width) method.

This lets you change on a per-column basis what the column's
width would be, and you can enable the user to interactively
resize column widths by dragging them (see Fl_Table::row_resize()).

Ditto for columns.

Since your draw_cell() method handles drawing the data,
you handle how to best display the data, e.g. left/center/right
justified in the cell, how clipping and colors and fonts are
handled, etc.

> And in the code I don't like to do so much
> tedious coordinate calculation.

Fl_Table handles coordinate calculations for you; that's the UI component.

> What should I read more?

See the examples that come with FLTK, e.g. examples/table-*.cxx
There's also a test program in test/table.
And of course the docs..
http://www.fltk.org/doc-1.3/classFl__Table.html#details

Hannu Vuolasaho

unread,
Jul 10, 2014, 8:35:36 PM7/10/14
to fltkg...@googlegroups.com, erco_...@seriss.com


On Friday, July 11, 2014 2:52:19 AM UTC+3, Greg Ercolano wrote:


        Look at examples/table-simple.cxx and examples/table-spreadsheet.cxx

        table-simple shows a read only way to look at a table of
        integer values (int data[MAX_ROWS][MAX_COLS]).

Now I'm embarassed. I have thought all the time int data[][] being part of Fl_Table
and it being static size. I wondered why fltk would make such a stupid assumption,
when it can do all the things with just simple hints.

Thanks for clarification.


        Data management is entirely up to you; Fl_Table handles the UI
        component, your derived class handles managing the data, and Fl_Table
        invokes your derived class's draw_cell() method to tell it which
        cell to draw, and will tell you the xywh value of where to draw it.

        Fl_Table handles the UI drawing, but does not manage your data.
        This allows you to manage the data in whatever way is best for
        your application. For some people managing the data in an array
        is best, for some a database, and for others the table data isn't
        in memory at all, and can be purely algorithmic.

This is why I chose fltk and not Qt. I can manage data and do time critical
stuff on my program while fltk provides UI and it is updated when it is
possible. One people sees my UI but 100 might depend on what
happens elsewhere in that program.

> And at least Fl_Table has understanding of rows only. How
> columns are handled?

        See above examples.

> I'm also worrying about column width in sense how to handle
> easily longer data.

        See the Fl_Table::col_width(col,width) method.

        This lets you change on a per-column basis what the column's
        width would be, and you can enable the user to interactively
        resize column widths by dragging them (see Fl_Table::row_resize()).

        Ditto for columns.

        Since your draw_cell() method handles drawing the data,
        you handle how to best display the data, e.g. left/center/right
        justified in the cell, how clipping and colors and fonts are
        handled, etc.

Yep. This has been one of those things I've keep wondering why this
so unique but efficent.


> And in the code I don't like to do so much
> tedious coordinate calculation.

        Fl_Table handles coordinate calculations for you; that's the UI component.

Yes. Now that I'm readind that over again with my eyes opened it became clear.
However I did some tweaking with z_hossain's LCD widget and I ended up huge
cordinate jugling. I believe that happens when working with low level drawing
functions.

Thank you for your kind reply.

Hannu Vuolasaho

Dmitrij K

unread,
Jul 10, 2014, 11:25:53 PM7/10/14
to fltkg...@googlegroups.com
> I have thought all the time int data[][] being part of Fl_Table
> and it being static size.

For dynamic array you can use similarly this is:

[CODE=CPP]
#include <iostream>
#include <string>
#include <vector>

#define ROWS 3
#define COLS 5

typedef std::vector < std::vector < std::string>> a3d_t;

void resize (a3d_t &a, int rows, int cols) {
 a.resize(rows);
 for (int i = 0; i < rows; i++) a[i].resize(cols);
}

// g++ 3d.cxx -o 3d && ./3d
int main(){
 
 a3d_t arr3d;
 resize(arr3d, ROWS, COLS);
 arr3d[0][0] = "Hello";
 
 std::cout << arr3d[0][0] << std::endl;
 
 return 0;
}
[/CODE]

Reply all
Reply to author
Forward
0 new messages