]> git.saurik.com Git - wxWidgets.git/blob - src/common/sizer.cpp
Watcom C++ fixup in tbar95.cpp; removed WXWIN_COMPATIBILITY for 'old' menu
[wxWidgets.git] / src / common / sizer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sizer.cpp
3 // Purpose: provide new wxSizer class for layout
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
12 #ifdef __GNUG__
13 #pragma implementation "sizer.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/sizer.h"
24 #include "wx/utils.h"
25 #include "wx/statbox.h"
26
27 //---------------------------------------------------------------------------
28
29 IMPLEMENT_ABSTRACT_CLASS(wxSizerItem, wxObject);
30 IMPLEMENT_ABSTRACT_CLASS(wxSizer, wxObject);
31 IMPLEMENT_ABSTRACT_CLASS(wxBoxSizer, wxSizer);
32 IMPLEMENT_ABSTRACT_CLASS(wxStaticBoxSizer, wxBoxSizer);
33
34 //---------------------------------------------------------------------------
35 // wxSizerItem
36 //---------------------------------------------------------------------------
37
38 wxSizerItem::wxSizerItem( int width, int height, int option, int flag, int border, wxObject* userData )
39 {
40 m_window = (wxWindow *) NULL;
41 m_sizer = (wxSizer *) NULL;
42 m_option = option;
43 m_border = border;
44 m_flag = flag;
45 m_userData = userData;
46
47 // minimal size is the initial size
48 m_minSize.x = width;
49 m_minSize.y = height;
50
51 SetRatio(width, height);
52
53 // size is set directly
54 m_size = m_minSize;
55 }
56
57 wxSizerItem::wxSizerItem( wxWindow *window, int option, int flag, int border, wxObject* userData )
58 {
59 m_window = window;
60 m_sizer = (wxSizer *) NULL;
61 m_option = option;
62 m_border = border;
63 m_flag = flag;
64 m_userData = userData;
65
66 // minimal size is the initial size
67 m_minSize = window->GetSize();
68
69 // aspect ratio calculated from initial size
70 SetRatio(m_minSize);
71
72 // size is calculated later
73 // m_size = ...
74 }
75
76 wxSizerItem::wxSizerItem( wxSizer *sizer, int option, int flag, int border, wxObject* userData )
77 {
78 m_window = (wxWindow *) NULL;
79 m_sizer = sizer;
80 m_option = option;
81 m_border = border;
82 m_flag = flag;
83 m_userData = userData;
84
85 // minimal size is calculated later
86 // m_minSize = ...
87 m_ratio = 0;
88
89 // size is calculated later
90 // m_size = ...
91 }
92
93 wxSizerItem::~wxSizerItem()
94 {
95 if (m_userData)
96 delete m_userData;
97 if (m_sizer)
98 delete m_sizer;
99 }
100
101
102 wxSize wxSizerItem::GetSize()
103 {
104 wxSize ret;
105 if (IsSizer())
106 ret = m_sizer->GetSize();
107 else
108 if (IsWindow())
109 ret = m_window->GetSize();
110 else ret = m_size;
111
112 if (m_flag & wxWEST)
113 ret.x += m_border;
114 if (m_flag & wxEAST)
115 ret.x += m_border;
116 if (m_flag & wxNORTH)
117 ret.y += m_border;
118 if (m_flag & wxSOUTH)
119 ret.y += m_border;
120
121 return ret;
122 }
123
124 wxSize wxSizerItem::CalcMin()
125 {
126 wxSize ret;
127 if (IsSizer())
128 {
129 ret = m_sizer->CalcMin();
130 // if we have to preserve aspect ratio _AND_ this is
131 // the first-time calculation, consider ret to be initial size
132 if ((m_flag & wxSHAPED) && !m_ratio) SetRatio(ret);
133 }
134
135 /*
136 The minimum size of a window should be the
137 initial size, as saved in m_minSize, not the
138 current size.
139
140 else
141 if (IsWindow())
142 ret = m_window->GetSize();
143 */
144 else ret = m_minSize;
145
146 if (m_flag & wxWEST)
147 ret.x += m_border;
148 if (m_flag & wxEAST)
149 ret.x += m_border;
150 if (m_flag & wxNORTH)
151 ret.y += m_border;
152 if (m_flag & wxSOUTH)
153 ret.y += m_border;
154
155 return ret;
156 }
157
158 void wxSizerItem::SetDimension( wxPoint pos, wxSize size )
159 {
160 if (m_flag & wxWEST)
161 {
162 pos.x += m_border;
163 size.x -= m_border;
164 }
165 if (m_flag & wxEAST)
166 {
167 size.x -= m_border;
168 }
169 if (m_flag & wxNORTH)
170 {
171 pos.y += m_border;
172 size.y -= m_border;
173 }
174 if (m_flag & wxSOUTH)
175 {
176 size.y -= m_border;
177 }
178 if (m_flag & wxSHAPED) {
179 // adjust aspect ratio
180 int rwidth = (int) (size.y * m_ratio);
181 if (rwidth > size.x) {
182 // fit horizontally
183 int rheight = (int) (size.x / m_ratio);
184 // add vertical space
185 if (m_flag & wxALIGN_CENTER_VERTICAL)
186 pos.y += (size.y - rheight) / 2;
187 else if (m_flag & wxALIGN_BOTTOM)
188 pos.y += (size.y - rheight);
189 // use reduced dimensions
190 size.y =rheight;
191 } else if (rwidth < size.x) {
192 // add horizontal space
193 if (m_flag & wxALIGN_CENTER_HORIZONTAL)
194 pos.x += (size.x - rwidth) / 2;
195 else if (m_flag & wxALIGN_RIGHT)
196 pos.x += (size.x - rwidth);
197 size.x = rwidth;
198 }
199 }
200
201 if (IsSizer())
202 m_sizer->SetDimension( pos.x, pos.y, size.x, size.y );
203
204 if (IsWindow())
205 m_window->SetSize( pos.x, pos.y, size.x, size.y );
206
207 m_size = size;
208 }
209
210 bool wxSizerItem::IsWindow()
211 {
212 return (m_window != NULL);
213 }
214
215 bool wxSizerItem::IsSizer()
216 {
217 return (m_sizer != NULL);
218 }
219
220 bool wxSizerItem::IsSpacer()
221 {
222 return (m_window == NULL) && (m_sizer == NULL);
223 }
224
225 //---------------------------------------------------------------------------
226 // wxSizer
227 //---------------------------------------------------------------------------
228
229 wxSizer::wxSizer()
230 {
231 m_children.DeleteContents( TRUE );
232 }
233
234 wxSizer::~wxSizer()
235 {
236 }
237
238 void wxSizer::Add( wxWindow *window, int option, int flag, int border, wxObject* userData )
239 {
240 m_children.Append( new wxSizerItem( window, option, flag, border, userData ) );
241 }
242
243 void wxSizer::Add( wxSizer *sizer, int option, int flag, int border, wxObject* userData )
244 {
245 m_children.Append( new wxSizerItem( sizer, option, flag, border, userData ) );
246 }
247
248 void wxSizer::Add( int width, int height, int option, int flag, int border, wxObject* userData )
249 {
250 m_children.Append( new wxSizerItem( width, height, option, flag, border, userData ) );
251 }
252
253 void wxSizer::Prepend( wxWindow *window, int option, int flag, int border, wxObject* userData )
254 {
255 m_children.Insert( new wxSizerItem( window, option, flag, border, userData ) );
256 }
257
258 void wxSizer::Prepend( wxSizer *sizer, int option, int flag, int border, wxObject* userData )
259 {
260 m_children.Insert( new wxSizerItem( sizer, option, flag, border, userData ) );
261 }
262
263 void wxSizer::Prepend( int width, int height, int option, int flag, int border, wxObject* userData )
264 {
265 m_children.Insert( new wxSizerItem( width, height, option, flag, border, userData ) );
266 }
267
268 bool wxSizer::Remove( wxWindow *window )
269 {
270 wxASSERT( window );
271
272 wxNode *node = m_children.First();
273 while (node)
274 {
275 wxSizerItem *item = (wxSizerItem*)node->Data();
276 if (item->GetWindow() == window)
277 {
278 m_children.DeleteNode( node );
279 return TRUE;
280 }
281 node = node->Next();
282 }
283
284 return FALSE;
285 }
286
287 bool wxSizer::Remove( wxSizer *sizer )
288 {
289 wxASSERT( sizer );
290
291 wxNode *node = m_children.First();
292 while (node)
293 {
294 wxSizerItem *item = (wxSizerItem*)node->Data();
295 if (item->GetSizer() == sizer)
296 {
297 m_children.DeleteNode( node );
298 return TRUE;
299 }
300 node = node->Next();
301 }
302
303 return FALSE;
304 }
305
306 bool wxSizer::Remove( int pos )
307 {
308 wxNode *node = m_children.Nth( pos );
309 if (!node) return FALSE;
310
311 m_children.DeleteNode( node );
312
313 return TRUE;
314 }
315
316 void wxSizer::Fit( wxWindow *window )
317 {
318 window->SetSize( GetMinWindowSize( window ) );
319 }
320
321 void wxSizer::Layout()
322 {
323 CalcMin();
324 RecalcSizes();
325 }
326
327 void wxSizer::SetSizeHints( wxWindow *window )
328 {
329 wxSize size( GetMinWindowSize( window ) );
330 window->SetSizeHints( size.x, size.y );
331 }
332
333 wxSize wxSizer::GetMinWindowSize( wxWindow *window )
334 {
335 wxSize minSize( GetMinSize() );
336 wxSize size( window->GetSize() );
337 wxSize client_size( window->GetClientSize() );
338 return wxSize( minSize.x+size.x-client_size.x,
339 minSize.y+size.y-client_size.y );
340 }
341
342 void wxSizer::SetDimension( int x, int y, int width, int height )
343 {
344 m_position.x = x;
345 m_position.y = y;
346 m_size.x = width;
347 m_size.y = height;
348 CalcMin();
349 RecalcSizes();
350 }
351
352 //---------------------------------------------------------------------------
353 // wxBoxSizer
354 //---------------------------------------------------------------------------
355
356 wxBoxSizer::wxBoxSizer( int orient )
357 {
358 m_orient = orient;
359 }
360
361 void wxBoxSizer::RecalcSizes()
362 {
363 if (m_children.GetCount() == 0)
364 return;
365
366 int delta = 0;
367 int extra = 0;
368 if (m_stretchable)
369 {
370 if (m_orient == wxHORIZONTAL)
371 {
372 delta = (m_size.x - m_fixedWidth) / m_stretchable;
373 extra = (m_size.x - m_fixedWidth) % m_stretchable;
374 }
375 else
376 {
377 delta = (m_size.y - m_fixedHeight) / m_stretchable;
378 extra = (m_size.y - m_fixedHeight) % m_stretchable;
379 }
380 }
381
382 wxPoint pt( m_position );
383
384 wxNode *node = m_children.GetFirst();
385 while (node)
386 {
387 wxSizerItem *item = (wxSizerItem*) node->Data();
388
389 int weight = 1;
390 if (item->GetOption())
391 weight = item->GetOption();
392
393 wxSize size( item->CalcMin() );
394
395 if (m_orient == wxVERTICAL)
396 {
397 long height = size.y;
398 if (item->GetOption())
399 {
400 height = (delta * weight) + extra;
401 extra = 0; // only the first item will get the remainder as extra size
402 }
403
404 wxPoint child_pos( pt );
405 wxSize child_size( wxSize( size.x, height) );
406
407 if (item->GetFlag() & (wxEXPAND | wxSHAPED))
408 child_size.x = m_size.x;
409 else if (item->GetFlag() & wxALIGN_RIGHT)
410 child_pos.x += m_size.x - size.x;
411 else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_HORIZONTAL))
412 // XXX wxCENTER is added for backward compatibility;
413 // wxALIGN_CENTER should be used in new code
414 child_pos.x += (m_size.x - size.x) / 2;
415
416 item->SetDimension( child_pos, child_size );
417
418 pt.y += height;
419 }
420 else
421 {
422 long width = size.x;
423 if (item->GetOption())
424 {
425 width = (delta * weight) + extra;
426 extra = 0; // only the first item will get the remainder as extra size
427 }
428
429 wxPoint child_pos( pt );
430 wxSize child_size( wxSize(width, size.y) );
431
432 if (item->GetFlag() & (wxEXPAND | wxSHAPED))
433 child_size.y = m_size.y;
434 else if (item->GetFlag() & wxALIGN_BOTTOM)
435 child_pos.y += m_size.y - size.y;
436 else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_VERTICAL))
437 // XXX wxCENTER is added for backward compatibility;
438 // wxALIGN_CENTER should be used in new code
439 child_pos.y += (m_size.y - size.y) / 2;
440
441 item->SetDimension( child_pos, child_size );
442
443 pt.x += width;
444 }
445
446 node = node->Next();
447 }
448 }
449
450 wxSize wxBoxSizer::CalcMin()
451 {
452 if (m_children.GetCount() == 0)
453 return wxSize(2,2);
454
455 m_stretchable = 0;
456 m_minWidth = 0;
457 m_minHeight = 0;
458 m_fixedWidth = 0;
459 m_fixedHeight = 0;
460
461 wxNode *node = m_children.GetFirst();
462 while (node)
463 {
464 wxSizerItem *item = (wxSizerItem*) node->Data();
465
466 int weight = 1;
467 if (item->GetOption())
468 weight = item->GetOption();
469
470 wxSize size( item->CalcMin() );
471
472 if (m_orient == wxHORIZONTAL)
473 {
474 m_minWidth += (size.x * weight);
475 m_minHeight = wxMax( m_minHeight, size.y );
476 }
477 else
478 {
479 m_minHeight += (size.y * weight);
480 m_minWidth = wxMax( m_minWidth, size.x );
481 }
482
483 if (item->GetOption())
484 {
485 m_stretchable += weight;
486 }
487 else
488 {
489 if (m_orient == wxVERTICAL)
490 {
491 m_fixedHeight += size.y;
492 m_fixedWidth = wxMax( m_fixedWidth, size.x );
493 }
494 else
495 {
496 m_fixedWidth += size.x;
497 m_fixedHeight = wxMax( m_fixedHeight, size.y );
498 }
499 }
500
501 node = node->Next();
502 }
503
504 return wxSize( m_minWidth, m_minHeight );
505 }
506
507 //---------------------------------------------------------------------------
508 // wxStaticBoxSizer
509 //---------------------------------------------------------------------------
510
511 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox *box, int orient )
512 : wxBoxSizer( orient )
513 {
514 wxASSERT_MSG( box, wxT("wxStaticBoxSizer needs a static box") );
515
516 m_staticBox = box;
517 }
518
519 void wxStaticBoxSizer::RecalcSizes()
520 {
521 // this will have to be done platform by platform
522 // as there is no way to guess the thickness of
523 // a wxStaticBox border
524 int top_border = 15;
525 if (m_staticBox->GetLabel().IsEmpty()) top_border = 5;
526 int other_border = 5;
527
528 m_staticBox->SetSize( m_position.x, m_position.y, m_size.x, m_size.y );
529
530 wxPoint old_pos( m_position );
531 m_position.x += other_border;
532 m_position.y += top_border;
533 wxSize old_size( m_size );
534 m_size.x -= 2*other_border;
535 m_size.y -= top_border + other_border;
536
537 wxBoxSizer::RecalcSizes();
538
539 m_position = old_pos;
540 m_size = old_size;
541 }
542
543 wxSize wxStaticBoxSizer::CalcMin()
544 {
545 // this will have to be done platform by platform
546 // as there is no way to guess the thickness of
547 // a wxStaticBox border
548 int top_border = 15;
549 if (m_staticBox->GetLabel().IsEmpty()) top_border = 5;
550 int other_border = 5;
551
552 wxSize ret( wxBoxSizer::CalcMin() );
553 ret.x += 2*top_border;
554 ret.y += other_border + top_border;
555
556 return ret;
557 }