From 59af6563dbd703f4755e95811f631bdb8297cee5 Mon Sep 17 00:00:00 2001 From: Albrecht Schlosser Date: Thu, 29 Mar 2018 15:09:32 +0000 Subject: Refactor and simplify Fl_Group::resize(). I renamed variables (more consistent names), fixed code formatting, and refactored the code around the resizing of widgets. I put common code before and after the conditional '#if 1' that separated old code from new "much simpler code from Francois Ostiguy" as a comment in the existing code mentioned. Then I deactivated the old code and activated the new "much simpler" code. Tested, works well. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12813 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- src/Fl_Group.cxx | 100 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/Fl_Group.cxx b/src/Fl_Group.cxx index a0ca7b7fd..8392f8bf4 100644 --- a/src/Fl_Group.cxx +++ b/src/Fl_Group.cxx @@ -691,14 +691,14 @@ int* Fl_Group::sizes() */ void Fl_Group::resize(int X, int Y, int W, int H) { - int dx = X-x(); - int dy = Y-y(); - int dw = W-w(); - int dh = H-h(); + int dx = X - x(); + int dy = Y - y(); + int dw = W - w(); + int dh = H - h(); Fl_Rect* p = bounds(); // save initial sizes and positions - Fl_Widget::resize(X,Y,W,H); // make new xywh values visible for children + Fl_Widget::resize(X, Y, W, H); // make new xywh values visible for children if ((!resizable() || (dw==0 && dh==0 )) && !Fl_Window_Driver::is_a_rescale()) { @@ -706,7 +706,7 @@ void Fl_Group::resize(int X, int Y, int W, int H) { Fl_Widget*const* a = array(); for (int i=children_; i--;) { Fl_Widget* o = *a++; - o->resize(o->x()+dx, o->y()+dy, o->w(), o->h()); + o->resize(o->x() + dx, o->y() + dy, o->w(), o->h()); } } @@ -716,48 +716,62 @@ void Fl_Group::resize(int X, int Y, int W, int H) { dx = X - p->x(); dw = W - p->w(); dy = Y - p->y(); - dh = H - p++->h(); - if (as_window()) dx = dy = 0; + dh = H - p->h(); + if (as_window()) + dx = dy = 0; + p++; + + // Developer note: + // The following code uses T = top, L = left, R = right, and B = bottom + // widget bounds. T and L are equivalent to x() and y(), whereas + // R = x() + w() and B = y() + h(), respectively, i.e. the next pixel + // beyond the widget border. + // RL, RR, RT, and RB are those values of the resizable widget. // get initial size of resizable(): - int IX = p->x(); - int IR = p->r(); - int IY = p->y(); - int IB = p++->b(); + int RL = p->x(); + int RR = RL + p->w(); + int RT = p->y(); + int RB = RT + p->h(); + p++; + // resize children Fl_Widget*const* a = array(); - for (int i=children_; i--;) { + + for (int i = children_; i--; p++) { + Fl_Widget* o = *a++; -#if 1 - int XX = p->x(); - if (XX >= IR) XX += dw; - else if (XX > IX) XX = IX+((XX-IX)*(IR+dw-IX)+(IR-IX)/2)/(IR-IX); - int R = p->r(); - if (R >= IR) R += dw; - else if (R > IX) R = IX+((R-IX)*(IR+dw-IX)+(IR-IX)/2)/(IR-IX); - - int YY = p->y(); - if (YY >= IB) YY += dh; - else if (YY > IY) YY = IY+((YY-IY)*(IB+dh-IY)+(IB-IY)/2)/(IB-IY); - int B = p++->b(); - if (B >= IB) B += dh; - else if (B > IY) B = IY+((B-IY)*(IB+dh-IY)+(IB-IY)/2)/(IB-IY); -#else // much simpler code from Francois Ostiguy: - int XX = p->x(); - if (XX >= IR) XX += dw; - else if (XX > IX) XX += dw * (XX-IX)/(IR-IX); - int R = p->r(); - if (R >= IR) R += dw; - else if (R > IX) R = R + dw * (R-IX)/(IR-IX); - - int YY = p->y(); - if (YY >= IB) YY += dh; - else if (YY > IY) YY = YY + dh*(YY-IY)/(IB-IY); - int B = p++->b(); - if (B >= IB) B += dh; - else if (B > IY) B = B + dh*(B-IY)/(IB-IY); -#endif - o->resize(XX+dx, YY+dy, R-XX, B-YY); + int L = p->x(); + int R = L + p->w(); + int T = p->y(); + int B = T + p->h(); + +#if 0 // old widget resizing code: used up to FLTK 1.3.x, deactivated 29 Mar 2018 + // FIXME: This should be removed before the release of FLTK 1.4.0 + + if (L >= RR) L += dw; + else if (L > RL) L = RL+((L-RL)*(RR+dw-RL)+(RR-RL)/2)/(RR-RL); + if (R >= RR) R += dw; + else if (R > RL) R = RL+((R-RL)*(RR+dw-RL)+(RR-RL)/2)/(RR-RL); + if (T >= RB) T += dh; + else if (T > RT) T = RT+((T-RT)*(RB+dh-RT)+(RB-RT)/2)/(RB-RT); + if (B >= RB) B += dh; + else if (B > RT) B = RT+((B-RT)*(RB+dh-RT)+(RB-RT)/2)/(RB-RT); + +#else // much simpler code from Francois Ostiguy: since FLTK 1.4.0 + + if (L >= RR) L += dw; + else if (L > RL) L += dw * (L-RL) / (RR-RL); + if (R >= RR) R += dw; + else if (R > RL) R = R + dw * (R-RL) / (RR-RL); + if (T >= RB) T += dh; + else if (T > RT) T = T + dh * (T-RT) / (RB-RT); + if (B >= RB) B += dh; + else if (B > RT) B = B + dh * (B-RT) / (RB-RT); + +#endif // old / new (1.4.0++) widget resizing code + + o->resize(L+dx, T+dy, R-L, B-T); } } } -- cgit v1.2.3