1 /********************************************************************************
2 * *
3 * V e r t i c a l C o n t a i n e r O b j e c t *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1997,2005 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 *********************************************************************************
22 * $Id: FXVerticalFrame.cpp,v 1.23 2005/01/16 16:06:07 fox Exp $ *
23 ********************************************************************************/
24 #include "xincs.h"
25 #include "fxver.h"
26 #include "fxdefs.h"
27 #include "FXHash.h"
28 #include "FXThread.h"
29 #include "FXStream.h"
30 #include "FXString.h"
31 #include "FXSize.h"
32 #include "FXPoint.h"
33 #include "FXRectangle.h"
34 #include "FXRegistry.h"
35 #include "FXApp.h"
36 #include "FXVerticalFrame.h"
37
38
39 /*
40 Notes:
41 - Filled items shrink as well as stretch.
42 - Stretch is proportional to default size; this way, at default size,
43 it is exactly correct.
44 - Tabbing order takes widget layout into account
45 */
46
47 using namespace FX;
48
49
50 /*******************************************************************************/
51
52 namespace FX {
53
54 // Map
55 FXDEFMAP(FXVerticalFrame) FXVerticalFrameMap[]={
56 FXMAPFUNC(SEL_FOCUS_PREV,0,FXVerticalFrame::onFocusUp),
57 FXMAPFUNC(SEL_FOCUS_NEXT,0,FXVerticalFrame::onFocusDown),
58 };
59
60
61 // Object implementation
FXIMPLEMENT(FXVerticalFrame,FXPacker,FXVerticalFrameMap,ARRAYNUMBER (FXVerticalFrameMap))62 FXIMPLEMENT(FXVerticalFrame,FXPacker,FXVerticalFrameMap,ARRAYNUMBER(FXVerticalFrameMap))
63
64
65 // Make a vertical one
66 FXVerticalFrame::FXVerticalFrame(FXComposite* p,FXuint opts,FXint x,FXint y,FXint w,FXint h,FXint pl,FXint pr,FXint pt,FXint pb,FXint hs,FXint vs):
67 FXPacker(p,opts,x,y,w,h,pl,pr,pt,pb,hs,vs){
68 }
69
70
71 // Compute minimum width based on child layout hints
getDefaultWidth()72 FXint FXVerticalFrame::getDefaultWidth(){
73 register FXint w,wmax,mw=0;
74 register FXWindow* child;
75 register FXuint hints;
76 wmax=0;
77 if(options&PACK_UNIFORM_WIDTH) mw=maxChildWidth();
78 for(child=getFirst(); child; child=child->getNext()){
79 if(child->shown()){
80 hints=child->getLayoutHints();
81 if(hints&LAYOUT_FIX_WIDTH) w=child->getWidth();
82 else if(options&PACK_UNIFORM_WIDTH) w=mw;
83 else w=child->getDefaultWidth();
84 if((hints&LAYOUT_RIGHT)&&(hints&LAYOUT_CENTER_X)){ // LAYOUT_FIX_X
85 w=child->getX()+w;
86 }
87 if(wmax<w) wmax=w;
88 }
89 }
90 return padleft+padright+wmax+(border<<1);
91 }
92
93
94 // Compute minimum height based on child layout hints
getDefaultHeight()95 FXint FXVerticalFrame::getDefaultHeight(){
96 register FXint h,hcum,hmax,numc,mh=0;
97 register FXWindow* child;
98 register FXuint hints;
99 hcum=hmax=numc=0;
100 if(options&PACK_UNIFORM_HEIGHT) mh=maxChildHeight();
101 for(child=getFirst(); child; child=child->getNext()){
102 if(child->shown()){
103 hints=child->getLayoutHints();
104 if(hints&LAYOUT_FIX_HEIGHT) h=child->getHeight();
105 else if(options&PACK_UNIFORM_HEIGHT) h=mh;
106 else h=child->getDefaultHeight();
107 if((hints&LAYOUT_BOTTOM)&&(hints&LAYOUT_CENTER_Y)){ // LAYOUT_FIX_Y
108 h=child->getY()+h;
109 }
110 else{
111 hcum+=h; numc++;
112 }
113 if(hmax<h) hmax=h;
114 }
115 }
116 if(numc>1) hcum+=(numc-1)*vspacing;
117 if(hmax<hcum) hmax=hcum;
118 return padtop+padbottom+hmax+(border<<1);
119 }
120
121
122 // Recalculate layout
layout()123 void FXVerticalFrame::layout(){
124 register FXint left,right,top,bottom,remain,extra_space,total_space,t,x,y,w,h;
125 register FXWindow* child;
126 register FXint sumexpand=0;
127 register FXint numexpand=0;
128 register FXint mw=0;
129 register FXint mh=0;
130 register FXint e=0;
131 register FXuint hints;
132
133 // Placement rectangle; right/bottom non-inclusive
134 left=border+padleft;
135 right=width-border-padright;
136 top=border+padtop;
137 bottom=height-border-padbottom;
138 remain=bottom-top;
139
140 // Get maximum child size
141 if(options&PACK_UNIFORM_WIDTH) mw=maxChildWidth();
142 if(options&PACK_UNIFORM_HEIGHT) mh=maxChildHeight();
143
144 // Find number of paddable children and total height
145 for(child=getFirst(); child; child=child->getNext()){
146 if(child->shown()){
147 hints=child->getLayoutHints();
148 if(!((hints&LAYOUT_BOTTOM)&&(hints&LAYOUT_CENTER_Y))){ // LAYOUT_FIX_Y
149 if(hints&LAYOUT_FIX_HEIGHT) h=child->getHeight();
150 else if(options&PACK_UNIFORM_HEIGHT) h=mh;
151 else h=child->getDefaultHeight();
152 FXASSERT(h>=0);
153 if((hints&LAYOUT_CENTER_Y) || ((hints&LAYOUT_FILL_Y) && !(hints&LAYOUT_FIX_HEIGHT))){
154 sumexpand+=h;
155 numexpand+=1;
156 }
157 else{
158 remain-=h;
159 }
160 remain-=vspacing;
161 }
162 }
163 }
164
165 // Child spacing correction
166 remain+=vspacing;
167
168 // Do the layout
169 for(child=getFirst(); child; child=child->getNext()){
170 if(child->shown()){
171 hints=child->getLayoutHints();
172
173 // Determine child width
174 if(hints&LAYOUT_FIX_WIDTH) w=child->getWidth();
175 else if(options&PACK_UNIFORM_WIDTH) w=mw;
176 else if(hints&LAYOUT_FILL_X) w=right-left;
177 else w=child->getDefaultWidth();
178
179 // Determine child x-position
180 if((hints&LAYOUT_RIGHT)&&(hints&LAYOUT_CENTER_X)) x=child->getX();
181 else if(hints&LAYOUT_CENTER_X) x=left+(right-left-w)/2;
182 else if(hints&LAYOUT_RIGHT) x=right-w;
183 else x=left;
184
185 // Layout child in Y
186 y=child->getY();
187 if(hints&LAYOUT_FIX_HEIGHT) h=child->getHeight();
188 else if(options&PACK_UNIFORM_HEIGHT) h=mh;
189 else h=child->getDefaultHeight();
190 if(!((hints&LAYOUT_BOTTOM)&&(hints&LAYOUT_CENTER_Y))){ // LAYOUT_FIX_Y
191 extra_space=0;
192 total_space=0;
193 if((hints&LAYOUT_FILL_Y) && !(hints&LAYOUT_FIX_HEIGHT)){
194 if(sumexpand>0){ // Divide space proportionally to height
195 t=h*remain;
196 FXASSERT(sumexpand>0);
197 h=t/sumexpand;
198 e+=t%sumexpand;
199 if(e>=sumexpand){h++;e-=sumexpand;}
200 }
201 else{ // Divide the space equally
202 FXASSERT(numexpand>0);
203 h=remain/numexpand;
204 e+=remain%numexpand;
205 if(e>=numexpand){h++;e-=numexpand;}
206 }
207 }
208 else if(hints&LAYOUT_CENTER_Y){
209 if(sumexpand>0){ // Divide space proportionally to height
210 t=h*remain;
211 FXASSERT(sumexpand>0);
212 total_space=t/sumexpand-h;
213 e+=t%sumexpand;
214 if(e>=sumexpand){total_space++;e-=sumexpand;}
215 }
216 else{ // Divide the space equally
217 FXASSERT(numexpand>0);
218 total_space=remain/numexpand-h;
219 e+=remain%numexpand;
220 if(e>=numexpand){total_space++;e-=numexpand;}
221 }
222 extra_space=total_space/2;
223 }
224 if(hints&LAYOUT_BOTTOM){
225 y=bottom-h-extra_space;
226 bottom=bottom-h-hspacing-total_space;
227 }
228 else{
229 y=top+extra_space;
230 top=top+h+vspacing+total_space;
231 }
232 }
233 child->position(x,y,w,h);
234 }
235 }
236 flags&=~FLAG_DIRTY;
237 }
238
239 }
240
241