FL/Fl_Scalepack: Give scalepack the ability contain a resizable() child.

This commit is contained in:
Jonathan Moore Liles 2010-02-24 08:58:47 -06:00
parent 82583365f6
commit ee705fffa3
2 changed files with 165 additions and 25 deletions

View File

@ -25,16 +25,20 @@
fit itself. Of course, this only works well with highly flexible fit itself. Of course, this only works well with highly flexible
widgets, but the task comes up often enough to warrent this class. widgets, but the task comes up often enough to warrent this class.
If any child happens to be the resizable() widget, it is given half If and child happens to be the resizable() widget, then it will be
of the available area, with the other widgets packed around it in resized so the all the other children can fit around it, with their
the remaining space. current sizes (and the size of the Fl_Scalepack) maintained.
NOTES: An Fl_Pack as a direct child will not work, because Fl_Pack
changes its size in draw(), which throws off our resize
calculation. The whole idea of widgets being able to resize
themselves within draw() is horribly broken...
*/ */
#include "Fl_Scalepack.H" #include "Fl_Scalepack.H"
#include <FL/Fl.H> #include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <stdio.h> #include <stdio.h>
Fl_Scalepack::Fl_Scalepack ( int X, int Y, int W, int H, const char *L ) : Fl_Scalepack::Fl_Scalepack ( int X, int Y, int W, int H, const char *L ) :
@ -44,9 +48,23 @@ Fl_Scalepack::Fl_Scalepack ( int X, int Y, int W, int H, const char *L ) :
_spacing = 0; _spacing = 0;
} }
void
Fl_Scalepack::resize ( int X, int Y, int W, int H )
{
/* Fl_Group's resize will change our child widget sizes, which
interferes with our own resizing method. */
Fl_Widget::resize( X, Y, W, H );
}
void void
Fl_Scalepack::draw ( void ) Fl_Scalepack::draw ( void )
{ {
if ( resizable() == this )
/* this resizable( this ) is the default for Fl_Group and is
* reset by Fl_Group::clear(), but it is not our default... */
resizable( 0 );
int tx = x() + Fl::box_dx( box() ); int tx = x() + Fl::box_dx( box() );
int ty = y() + Fl::box_dy( box() ); int ty = y() + Fl::box_dy( box() );
int tw = w() - Fl::box_dw( box() ); int tw = w() - Fl::box_dw( box() );
@ -54,45 +72,165 @@ Fl_Scalepack::draw ( void )
if ( damage() & FL_DAMAGE_ALL ) if ( damage() & FL_DAMAGE_ALL )
{ {
draw_box(); if ( box() == FL_NO_BOX )
fl_rectf( x(), y(), w(), h(), FL_BACKGROUND_COLOR );
else
draw_box();
draw_label(); draw_label();
} }
int v = 0; int v = 0;
int cth = 0;
int ctw = 0;
Fl_Widget * const * a = array();
for ( int i = children(); i--; ) for ( int i = children(); i--; )
if ( child( i )->visible() ) {
Fl_Widget *o = *a++;
if ( o->visible() )
{
++v; ++v;
if ( o != this->resizable() )
{
cth += o->h();
ctw += o->w();
}
cth += _spacing;
ctw += _spacing;
}
}
ctw -= _spacing;
cth -= _spacing;
if ( 0 == v ) if ( 0 == v )
return; return;
int sz, pos; if ( this->resizable() )
if ( type() == HORIZONTAL )
{ {
sz = tw / v; int pos = 0;
pos = tw - ( tw % sz );
Fl_Widget * const * a = array();
for ( int i = children(); i--; )
{
Fl_Widget *o = *a++;
if ( o->visible() )
{
int X, Y, W, H;
if ( type() == HORIZONTAL )
{
X = tx + pos;
Y = ty;
W = o->w();
H = th;
}
else
{
X = tx;
Y = ty + pos;
W = tw;
H = o->h();
}
if ( this->resizable() == o )
{
if ( type() == HORIZONTAL )
W = tw - ctw - 3;
else
H = th - cth;
}
if (X != o->x() || Y != o->y() || W != o->w() || H != o->h() )
{
o->resize(X,Y,W,H);
o->clear_damage(FL_DAMAGE_ALL);
}
if ( damage() & FL_DAMAGE_ALL )
{
draw_child( *o );
draw_outside_label( *o );
}
else
update_child( *o );
/* if ( this->resizable() == o ) */
/* fl_rect( o->x(), o->y(), o->w(), o->h(), type() == VERTICAL ? FL_GREEN : FL_BLUE ); */
if ( type() == HORIZONTAL )
pos += o->w() + spacing();
else
pos += o->h() + spacing();
}
}
} }
else else
{ {
sz = th / v; int sz = 0;
pos = th - ( th % sz ); int pos = 0;
}
for ( int i = children(); i--; ) if ( type() == HORIZONTAL )
if ( child( i )->visible() ) sz = (tw - (_spacing * (v - 1))) / v;
else
sz = (th - (_spacing * (v - 1))) / v;
Fl_Widget * const * a = array();
for ( int i = children(); i--; )
{ {
pos -= sz; Fl_Widget *o = *a++;
if ( type() == HORIZONTAL ) if ( o->visible() )
child( i )->resize( tx + pos, ty, sz, th ); {
else int X, Y, W, H;
child( i )->resize( tx, ty + pos, tw, sz );
if ( damage() & FL_DAMAGE_CHILD ) if ( type() == HORIZONTAL )
update_child( *child( i ) ); {
else X = tx + pos;
draw_child( *child( i ) ); Y = ty;
W = sz;
H = th;
}
else
{
X = tx;
Y = ty + pos;
W = tw;
H = sz;
}
if (X != o->x() || Y != o->y() || W != o->w() || H != o->h() )
{
o->resize(X,Y,W,H);
o->clear_damage(FL_DAMAGE_ALL);
}
if ( damage() & FL_DAMAGE_ALL )
{
draw_child( *o );
draw_outside_label( *o );
}
else
update_child( *o );
// fl_rect( o->x(), o->y(), o->w(), o->h(), type() == VERTICAL ? FL_RED : FL_YELLOW );
if ( type() == HORIZONTAL )
pos += o->w() + spacing();
else
pos += o->h() + spacing();
}
} }
}
} }

View File

@ -36,6 +36,8 @@ public:
int spacing ( void ) const { return _spacing; } int spacing ( void ) const { return _spacing; }
void spacing ( int v ) { _spacing = v; redraw(); } void spacing ( int v ) { _spacing = v; redraw(); }
virtual void resize ( int, int, int, int );
virtual void draw ( void ); virtual void draw ( void );
}; };