]>
Commit | Line | Data |
---|---|---|
5279a24d RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: sizer.cpp | |
3417c2cd | 3 | // Purpose: provide new wxSizer class for layounting |
5279a24d RR |
4 | // Author: Robert Roebling and Robin Dunn |
5 | // Modified by: | |
6 | // Created: | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Robin Dunn, Dirk Holtwick and Robert Roebling | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
5279a24d | 12 | #ifdef __GNUG__ |
c62ac5b6 | 13 | #pragma implementation "sizer.h" |
5279a24d RR |
14 | #endif |
15 | ||
77671fd2 VZ |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
5279a24d | 23 | #include "wx/sizer.h" |
61d514bb | 24 | #include "wx/utils.h" |
5279a24d RR |
25 | |
26 | //--------------------------------------------------------------------------- | |
3417c2cd | 27 | // wxSizerItem |
5279a24d RR |
28 | //--------------------------------------------------------------------------- |
29 | ||
3417c2cd | 30 | wxSizerItem::wxSizerItem( int width, int height, int option, int flag, int border ) |
5279a24d RR |
31 | { |
32 | m_window = (wxWindow *) NULL; | |
3417c2cd | 33 | m_sizer = (wxSizer *) NULL; |
d597fcb7 RR |
34 | m_option = option; |
35 | m_border = border; | |
36 | m_flag = flag; | |
37 | ||
38 | // minimal size is the initial size | |
5279a24d | 39 | m_minSize.x = width; |
c62ac5b6 | 40 | m_minSize.y = height; |
d597fcb7 RR |
41 | |
42 | // size is set directly | |
43 | m_size = m_minSize; | |
5279a24d RR |
44 | } |
45 | ||
3417c2cd | 46 | wxSizerItem::wxSizerItem( wxWindow *window, int option, int flag, int border ) |
5279a24d RR |
47 | { |
48 | m_window = window; | |
3417c2cd | 49 | m_sizer = (wxSizer *) NULL; |
5279a24d | 50 | m_option = option; |
d597fcb7 RR |
51 | m_border = border; |
52 | m_flag = flag; | |
53 | ||
54 | // minimal size is the initial size | |
55 | m_minSize = window->GetSize(); | |
56 | ||
57 | // size is calculated later | |
58 | // m_size = ... | |
5279a24d RR |
59 | } |
60 | ||
3417c2cd | 61 | wxSizerItem::wxSizerItem( wxSizer *sizer, int option, int flag, int border ) |
5279a24d RR |
62 | { |
63 | m_window = (wxWindow *) NULL; | |
64 | m_sizer = sizer; | |
5279a24d | 65 | m_option = option; |
d597fcb7 RR |
66 | m_border = border; |
67 | m_flag = flag; | |
68 | ||
69 | // minimal size is calculated later | |
70 | // m_minSize = ... | |
71 | ||
72 | // size is calculated later | |
73 | // m_size = ... | |
5279a24d RR |
74 | } |
75 | ||
3417c2cd | 76 | wxSize wxSizerItem::GetSize() |
5279a24d | 77 | { |
d597fcb7 | 78 | wxSize ret; |
3417c2cd | 79 | if (IsSizer()) |
d597fcb7 RR |
80 | ret = m_sizer->GetSize(); |
81 | else | |
c62ac5b6 | 82 | if (IsWindow()) |
d597fcb7 RR |
83 | ret = m_window->GetSize(); |
84 | else ret = m_size; | |
85 | ||
86 | if (m_flag & wxWEST) | |
87 | ret.x += m_border; | |
88 | if (m_flag & wxEAST) | |
89 | ret.x += m_border; | |
90 | if (m_flag & wxNORTH) | |
91 | ret.y += m_border; | |
92 | if (m_flag & wxSOUTH) | |
93 | ret.y += m_border; | |
94 | ||
95 | return ret; | |
5279a24d RR |
96 | } |
97 | ||
3417c2cd | 98 | wxSize wxSizerItem::CalcMin() |
c62ac5b6 | 99 | { |
d597fcb7 | 100 | wxSize ret; |
3417c2cd | 101 | if (IsSizer()) |
d597fcb7 RR |
102 | ret = m_sizer->CalcMin(); |
103 | /* | |
104 | The minimum size of a window should be the | |
105 | initial size, as saved in m_minSize, not the | |
106 | current size. | |
107 | ||
108 | else | |
c62ac5b6 | 109 | if (IsWindow()) |
d597fcb7 RR |
110 | ret = m_window->GetSize(); |
111 | */ | |
112 | else ret = m_minSize; | |
113 | ||
114 | if (m_flag & wxWEST) | |
115 | ret.x += m_border; | |
116 | if (m_flag & wxEAST) | |
117 | ret.x += m_border; | |
118 | if (m_flag & wxNORTH) | |
119 | ret.y += m_border; | |
120 | if (m_flag & wxSOUTH) | |
121 | ret.y += m_border; | |
122 | ||
123 | return ret; | |
c62ac5b6 RR |
124 | } |
125 | ||
3417c2cd | 126 | void wxSizerItem::SetDimension( wxPoint pos, wxSize size ) |
c62ac5b6 | 127 | { |
d597fcb7 RR |
128 | if (m_flag & wxWEST) |
129 | { | |
130 | pos.x += m_border; | |
131 | size.x -= m_border; | |
132 | } | |
133 | if (m_flag & wxEAST) | |
134 | { | |
135 | size.x -= m_border; | |
136 | } | |
137 | if (m_flag & wxNORTH) | |
138 | { | |
139 | pos.y += m_border; | |
140 | size.y -= m_border; | |
141 | } | |
142 | if (m_flag & wxSOUTH) | |
143 | { | |
144 | size.y -= m_border; | |
145 | } | |
146 | ||
3417c2cd | 147 | if (IsSizer()) |
c62ac5b6 RR |
148 | m_sizer->SetDimension( pos.x, pos.y, size.x, size.y ); |
149 | ||
150 | if (IsWindow()) | |
151 | m_window->SetSize( pos.x, pos.y, size.x, size.y ); | |
d597fcb7 RR |
152 | |
153 | m_size = size; | |
c62ac5b6 RR |
154 | } |
155 | ||
3417c2cd | 156 | bool wxSizerItem::IsWindow() |
5279a24d RR |
157 | { |
158 | return (m_window != NULL); | |
159 | } | |
160 | ||
3417c2cd | 161 | bool wxSizerItem::IsSizer() |
5279a24d RR |
162 | { |
163 | return (m_sizer != NULL); | |
164 | } | |
165 | ||
3417c2cd | 166 | bool wxSizerItem::IsSpacer() |
5279a24d RR |
167 | { |
168 | return (m_window == NULL) && (m_sizer == NULL); | |
169 | } | |
170 | ||
171 | //--------------------------------------------------------------------------- | |
3417c2cd | 172 | // wxSizer |
5279a24d RR |
173 | //--------------------------------------------------------------------------- |
174 | ||
3417c2cd | 175 | wxSizer::wxSizer() |
5279a24d RR |
176 | { |
177 | m_children.DeleteContents( TRUE ); | |
178 | } | |
179 | ||
3417c2cd | 180 | wxSizer::~wxSizer() |
5279a24d RR |
181 | { |
182 | } | |
183 | ||
3417c2cd | 184 | void wxSizer::Add( wxWindow *window, int option, int flag, int border ) |
5279a24d | 185 | { |
3417c2cd | 186 | m_children.Append( new wxSizerItem( window, option, flag, border ) ); |
5279a24d RR |
187 | } |
188 | ||
3417c2cd | 189 | void wxSizer::Add( wxSizer *sizer, int option, int flag, int border ) |
5279a24d | 190 | { |
3417c2cd | 191 | m_children.Append( new wxSizerItem( sizer, option, flag, border ) ); |
5279a24d RR |
192 | } |
193 | ||
3417c2cd | 194 | void wxSizer::Add( int width, int height, int option, int flag, int border ) |
5279a24d | 195 | { |
3417c2cd | 196 | m_children.Append( new wxSizerItem( width, height, option, flag, border ) ); |
5279a24d RR |
197 | } |
198 | ||
3417c2cd | 199 | void wxSizer::Fit( wxWindow *window ) |
5279a24d RR |
200 | { |
201 | window->SetSize( GetMinWindowSize( window ) ); | |
202 | } | |
203 | ||
3417c2cd | 204 | void wxSizer::Layout() |
c62ac5b6 RR |
205 | { |
206 | m_size = CalcMin(); | |
207 | RecalcSizes(); | |
208 | } | |
209 | ||
3417c2cd | 210 | void wxSizer::SetSizeHints( wxWindow *window ) |
5279a24d RR |
211 | { |
212 | wxSize size( GetMinWindowSize( window ) ); | |
213 | window->SetSizeHints( size.x, size.y ); | |
214 | } | |
215 | ||
3417c2cd | 216 | wxSize wxSizer::GetMinWindowSize( wxWindow *window ) |
5279a24d | 217 | { |
77671fd2 | 218 | wxSize minSize( GetMinSize() ); |
5279a24d RR |
219 | wxSize size( window->GetSize() ); |
220 | wxSize client_size( window->GetClientSize() ); | |
77671fd2 VZ |
221 | return wxSize( minSize.x+size.x-client_size.x, |
222 | minSize.y+size.y-client_size.y ); | |
5279a24d RR |
223 | } |
224 | ||
3417c2cd | 225 | void wxSizer::SetDimension( int x, int y, int width, int height ) |
5279a24d RR |
226 | { |
227 | m_position.x = x; | |
228 | m_position.y = y; | |
229 | m_size.x = width; | |
230 | m_size.y = height; | |
231 | RecalcSizes(); | |
232 | } | |
233 | ||
c62ac5b6 | 234 | //--------------------------------------------------------------------------- |
d597fcb7 | 235 | // wxBox |
61d514bb RR |
236 | //--------------------------------------------------------------------------- |
237 | ||
d597fcb7 | 238 | wxBox::wxBox( int orient ) |
61d514bb RR |
239 | { |
240 | m_orient = orient; | |
241 | } | |
242 | ||
d597fcb7 | 243 | void wxBox::RecalcSizes() |
61d514bb RR |
244 | { |
245 | if (m_children.GetCount() == 0) | |
246 | { | |
247 | SetDimension( m_position.x, m_position.y, 2, 2 ); | |
248 | return; | |
249 | } | |
250 | ||
251 | int delta = 0; | |
252 | int extra = 0; | |
253 | if (m_stretchable) | |
254 | { | |
255 | if (m_orient == wxHORIZONTAL) | |
256 | { | |
257 | delta = (m_size.x - m_fixedWidth) / m_stretchable; | |
258 | extra = (m_size.x - m_fixedWidth) % m_stretchable; | |
259 | } | |
260 | else | |
261 | { | |
262 | delta = (m_size.y - m_fixedHeight) / m_stretchable; | |
263 | extra = (m_size.y - m_fixedHeight) % m_stretchable; | |
264 | } | |
265 | } | |
266 | ||
267 | wxPoint pt( m_position ); | |
268 | ||
269 | wxNode *node = m_children.GetFirst(); | |
270 | while (node) | |
271 | { | |
3417c2cd | 272 | wxSizerItem *item = (wxSizerItem*) node->Data(); |
61d514bb RR |
273 | |
274 | int weight = 1; | |
275 | if (item->GetOption()) | |
276 | weight = item->GetOption(); | |
277 | ||
278 | wxSize size( item->CalcMin() ); | |
279 | ||
280 | if (m_orient == wxVERTICAL) | |
281 | { | |
282 | long height = size.y; | |
283 | if (item->GetOption()) | |
284 | { | |
285 | height = (delta * weight) + extra; | |
286 | extra = 0; // only the first item will get the remainder as extra size | |
287 | } | |
d597fcb7 RR |
288 | |
289 | wxPoint child_pos( pt ); | |
290 | wxSize child_size( wxSize( size.x, height) ); | |
291 | ||
292 | if (item->GetFlag() & wxALIGN_RIGHT) | |
293 | child_pos.x += m_size.x - size.x; | |
294 | else if (item->GetFlag() & wxCENTER) | |
295 | child_pos.x += (m_size.x - size.x) / 2; | |
296 | else if (item->GetFlag() & wxEXPAND) | |
297 | child_size.x = m_size.x; | |
298 | ||
299 | item->SetDimension( child_pos, child_size ); | |
300 | ||
61d514bb RR |
301 | pt.y += height; |
302 | } | |
303 | else | |
304 | { | |
305 | long width = size.x; | |
306 | if (item->GetOption()) | |
307 | { | |
308 | width = (delta * weight) + extra; | |
309 | extra = 0; // only the first item will get the remainder as extra size | |
310 | } | |
d597fcb7 RR |
311 | |
312 | wxPoint child_pos( pt ); | |
313 | wxSize child_size( wxSize(width, size.y) ); | |
314 | ||
315 | if (item->GetFlag() & wxALIGN_BOTTOM) | |
316 | child_pos.y += m_size.y - size.y; | |
317 | else if (item->GetFlag() & wxCENTER) | |
318 | child_pos.y += (m_size.y - size.y) / 2; | |
319 | else if (item->GetFlag() & wxEXPAND) | |
320 | child_size.y = m_size.y; | |
321 | ||
322 | item->SetDimension( child_pos, child_size ); | |
323 | ||
61d514bb RR |
324 | pt.x += width; |
325 | } | |
326 | ||
327 | node = node->Next(); | |
328 | } | |
329 | } | |
330 | ||
d597fcb7 | 331 | wxSize wxBox::CalcMin() |
61d514bb RR |
332 | { |
333 | if (m_children.GetCount() == 0) | |
334 | return wxSize(2,2); | |
335 | ||
336 | m_stretchable = 0; | |
337 | m_minWidth = 0; | |
338 | m_minHeight = 0; | |
339 | m_fixedWidth = 0; | |
340 | m_fixedHeight = 0; | |
341 | ||
342 | wxNode *node = m_children.GetFirst(); | |
343 | while (node) | |
344 | { | |
3417c2cd | 345 | wxSizerItem *item = (wxSizerItem*) node->Data(); |
61d514bb RR |
346 | |
347 | int weight = 1; | |
348 | if (item->GetOption()) | |
349 | weight = item->GetOption(); | |
350 | ||
351 | wxSize size( item->CalcMin() ); | |
352 | ||
353 | if (m_orient == wxHORIZONTAL) | |
354 | { | |
355 | m_minWidth += (size.x * weight); | |
356 | m_minHeight = wxMax( m_minHeight, size.y ); | |
357 | } | |
358 | else | |
359 | { | |
360 | m_minHeight += (size.y * weight); | |
361 | m_minWidth = wxMax( m_minWidth, size.x ); | |
362 | } | |
363 | ||
364 | if (item->GetOption()) | |
365 | { | |
366 | m_stretchable += weight; | |
367 | } | |
368 | else | |
369 | { | |
370 | if (m_orient == wxVERTICAL) | |
371 | { | |
372 | m_fixedHeight += size.y; | |
373 | m_fixedWidth = wxMax( m_fixedWidth, size.x ); | |
374 | } | |
375 | else | |
376 | { | |
377 | m_fixedWidth += size.x; | |
378 | m_fixedHeight = wxMax( m_fixedHeight, size.y ); | |
379 | } | |
380 | } | |
381 | ||
382 | node = node->Next(); | |
383 | } | |
384 | ||
385 | return wxSize( m_minWidth, m_minHeight ); | |
386 | } |