Seems to be a useful initiative,
and as such, would probably merit being put in the fltk software links.


Some remarks though :
- The dynamic_cast should not work in all platforms,
because I believe exceptions are voluntarily disabled to avoid the
significant overhead in fltk, AFAIR.
You can use Fl_Widget::type() for pseudo-rtti type checking,
this should suffice for group detection.

- You should probably add an optional minimum version parameter in your 
save/restore API so that one can detect changes and possibly avoid to restore 
the layout if a version has changed.

- In the same type of considerations, it could be interesting to save the 
number of children so that you can differenciate from the number returned by 
your current main group, as it could change with versions.

Fabien


> Hi,
>
> my application has a main window that has several nested Fl_Tiles, so that the
> user can adjust the workspace to his preferences. The following two functions
> serve to preserve the arrangement between application restarts. Pass a
> Fl_Preferences sub-group, and the main window as arguments.
>
> Maybe it is useful to other fltk developers.
>
> Cheers
> -Richard
>
> --------------------------
>
> #include <FL/Fl_Preferences.H>
> #include <FL/Fl_Group.H>
>
> void fltk_save_layout( Fl_Preferences* p, Fl_Group* g ) {
>         p->set( "group_x", g->x() );
>         p->set( "group_y", g->y() );
>         p->set( "group_w", g->w() );
>         p->set( "group_h", g->h() );
>         int num = g->children();
>         for ( int i = 0; i < num; i++ ) {
>                 Fl_Group* gg = dynamic_cast<Fl_Group*>(g->child(i));
>                 if ( gg ) {
>                         fltk_save_layout( new Fl_Preferences( *p,
> Fl_Preferences::Name( "group_%d", i ) ), gg );
>                 } else {
>                         p->set( Fl_Preferences::Name( "child_%d_x", i ),
> g->child(i)->x() );
>                         p->set( Fl_Preferences::Name( "child_%d_y", i ),
> g->child(i)->y() );
>                         p->set( Fl_Preferences::Name( "child_%d_w", i ),
> g->child(i)->w() );
>                         p->set( Fl_Preferences::Name( "child_%d_h", i ),
> g->child(i)->h() );
>                 }
>         }
> }
>
> void fltk_load_layout( Fl_Preferences* p, Fl_Group* g) {
>         int x, y, w, h;
>         p->get( "group_x", x, g->x() );
>         p->get( "group_y", y, g->y() );
>         p->get( "group_w", w, g->w() );
>         p->get( "group_h", h, g->h() );
>         g->resize( x, y, w, h );
>         int num = g->children();
>         for ( int i = 0; i < num; i++ ) {
>                 Fl_Group* gg = dynamic_cast<Fl_Group*>(g->child(i));
>                 if ( gg ) {
>                         fltk_load_layout( new Fl_Preferences( *p,
> Fl_Preferences::Name( "group_%d", i ) ), gg );
>                 } else {
>                         p->get( Fl_Preferences::Name( "child_%d_x", i ), x,
> g->child(i)->x() );
>                         p->get( Fl_Preferences::Name( "child_%d_y", i ), y,
> g->child(i)->y() );
>                         p->get( Fl_Preferences::Name( "child_%d_w", i ), w,
> g->child(i)->w() );
>                         p->get( Fl_Preferences::Name( "child_%d_h", i ), h,
> g->child(i)->h() );
>                         g->child(i)->resize( x, y, w, h );
>                 }
>         }
>         g->init_sizes();
> }
>

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to