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