]> git.saurik.com Git - wxWidgets.git/blob - src/common/sizer.cpp
Fixes ::Fit() and ::SetSizeHints() to keep the dialog sized so that it will not grow...
[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(wxGridSizer, wxSizer);
33 IMPLEMENT_ABSTRACT_CLASS(wxFlexGridSizer, wxGridSizer);
34 IMPLEMENT_ABSTRACT_CLASS(wxBoxSizer, wxSizer);
35 IMPLEMENT_ABSTRACT_CLASS(wxStaticBoxSizer, wxBoxSizer);
36 #if wxUSE_NOTEBOOK
37 IMPLEMENT_ABSTRACT_CLASS(wxNotebookSizer, wxSizer);
38 #endif
39
40 //---------------------------------------------------------------------------
41 // wxSizerItem
42 //---------------------------------------------------------------------------
43
44 wxSizerItem::wxSizerItem( int width, int height, int option, int flag, int border, wxObject* userData )
45 {
46 m_window = (wxWindow *) NULL;
47 m_sizer = (wxSizer *) NULL;
48 m_option = option;
49 m_border = border;
50 m_flag = flag;
51 m_userData = userData;
52
53 // minimal size is the initial size
54 m_minSize.x = width;
55 m_minSize.y = height;
56
57 SetRatio(width, height);
58
59 // size is set directly
60 m_size = m_minSize;
61 }
62
63 wxSizerItem::wxSizerItem( wxWindow *window, int option, int flag, int border, wxObject* userData )
64 {
65 m_window = window;
66 m_sizer = (wxSizer *) NULL;
67 m_option = option;
68 m_border = border;
69 m_flag = flag;
70 m_userData = userData;
71
72 // minimal size is the initial size
73 m_minSize = window->GetSize();
74
75 // aspect ratio calculated from initial size
76 SetRatio(m_minSize);
77
78 // size is calculated later
79 // m_size = ...
80 }
81
82 wxSizerItem::wxSizerItem( wxSizer *sizer, int option, int flag, int border, wxObject* userData )
83 {
84 m_window = (wxWindow *) NULL;
85 m_sizer = sizer;
86 m_option = option;
87 m_border = border;
88 m_flag = flag;
89 m_userData = userData;
90
91 // minimal size is calculated later
92 // m_minSize = ...
93 m_ratio = 0;
94
95 // size is calculated later
96 // m_size = ...
97 }
98
99 wxSizerItem::~wxSizerItem()
100 {
101 if (m_userData)
102 delete m_userData;
103 if (m_sizer)
104 delete m_sizer;
105 }
106
107
108 wxSize wxSizerItem::GetSize()
109 {
110 wxSize ret;
111 if (IsSizer())
112 ret = m_sizer->GetSize();
113 else
114 if (IsWindow())
115 ret = m_window->GetSize();
116 else ret = m_size;
117
118 if (m_flag & wxWEST)
119 ret.x += m_border;
120 if (m_flag & wxEAST)
121 ret.x += m_border;
122 if (m_flag & wxNORTH)
123 ret.y += m_border;
124 if (m_flag & wxSOUTH)
125 ret.y += m_border;
126
127 return ret;
128 }
129
130 wxSize wxSizerItem::CalcMin()
131 {
132 wxSize ret;
133 if (IsSizer())
134 {
135 ret = m_sizer->GetMinSize();
136
137 // if we have to preserve aspect ratio _AND_ this is
138 // the first-time calculation, consider ret to be initial size
139 if ((m_flag & wxSHAPED) && !m_ratio)
140 SetRatio(ret);
141 }
142 else
143 {
144 if ( IsWindow() && (m_flag & wxADJUST_MINSIZE) )
145 {
146 // check if the best (minimal, in fact) window size hadn't changed
147 // by chance: this may happen for, e.g. static text if its label
148 // changed
149 wxSize size = m_window->GetBestSize();
150 if ( size.x > m_minSize.x )
151 m_minSize.x = size.x;
152 if ( size.y > m_minSize.y )
153 m_minSize.y = size.y;
154 }
155
156 ret = m_minSize;
157 }
158
159 if (m_flag & wxWEST)
160 ret.x += m_border;
161 if (m_flag & wxEAST)
162 ret.x += m_border;
163 if (m_flag & wxNORTH)
164 ret.y += m_border;
165 if (m_flag & wxSOUTH)
166 ret.y += m_border;
167
168 return ret;
169 }
170
171 void wxSizerItem::SetDimension( wxPoint pos, wxSize size )
172 {
173 if (m_flag & wxSHAPED)
174 {
175 // adjust aspect ratio
176 int rwidth = (int) (size.y * m_ratio);
177 if (rwidth > size.x)
178 {
179 // fit horizontally
180 int rheight = (int) (size.x / m_ratio);
181 // add vertical space
182 if (m_flag & wxALIGN_CENTER_VERTICAL)
183 pos.y += (size.y - rheight) / 2;
184 else if (m_flag & wxALIGN_BOTTOM)
185 pos.y += (size.y - rheight);
186 // use reduced dimensions
187 size.y =rheight;
188 }
189 else if (rwidth < size.x)
190 {
191 // add horizontal space
192 if (m_flag & wxALIGN_CENTER_HORIZONTAL)
193 pos.x += (size.x - rwidth) / 2;
194 else if (m_flag & wxALIGN_RIGHT)
195 pos.x += (size.x - rwidth);
196 size.x = rwidth;
197 }
198 }
199
200 // This is what GetPosition() returns. Since we calculate
201 // borders afterwards, GetPosition() will be the left/top
202 // corner of the surrounding border.
203 m_pos = pos;
204
205 if (m_flag & wxWEST)
206 {
207 pos.x += m_border;
208 size.x -= m_border;
209 }
210 if (m_flag & wxEAST)
211 {
212 size.x -= m_border;
213 }
214 if (m_flag & wxNORTH)
215 {
216 pos.y += m_border;
217 size.y -= m_border;
218 }
219 if (m_flag & wxSOUTH)
220 {
221 size.y -= m_border;
222 }
223
224 if (IsSizer())
225 m_sizer->SetDimension( pos.x, pos.y, size.x, size.y );
226
227 if (IsWindow())
228 m_window->SetSize( pos.x, pos.y, size.x, size.y, wxSIZE_ALLOW_MINUS_ONE );
229
230 m_size = size;
231 }
232
233 bool wxSizerItem::IsWindow()
234 {
235 return (m_window != NULL);
236 }
237
238 bool wxSizerItem::IsSizer()
239 {
240 return (m_sizer != NULL);
241 }
242
243 bool wxSizerItem::IsSpacer()
244 {
245 return (m_window == NULL) && (m_sizer == NULL);
246 }
247
248 //---------------------------------------------------------------------------
249 // wxSizer
250 //---------------------------------------------------------------------------
251
252 wxSizer::wxSizer()
253 {
254 m_children.DeleteContents( TRUE );
255 m_minSize.x = 0;
256 m_minSize.y = 0;
257 }
258
259 wxSizer::~wxSizer()
260 {
261 }
262
263 void wxSizer::Add( wxWindow *window, int option, int flag, int border, wxObject* userData )
264 {
265 m_children.Append( new wxSizerItem( window, option, flag, border, userData ) );
266 }
267
268 void wxSizer::Add( wxSizer *sizer, int option, int flag, int border, wxObject* userData )
269 {
270 m_children.Append( new wxSizerItem( sizer, option, flag, border, userData ) );
271 }
272
273 void wxSizer::Add( int width, int height, int option, int flag, int border, wxObject* userData )
274 {
275 m_children.Append( new wxSizerItem( width, height, option, flag, border, userData ) );
276 }
277
278 void wxSizer::Prepend( wxWindow *window, int option, int flag, int border, wxObject* userData )
279 {
280 m_children.Insert( new wxSizerItem( window, option, flag, border, userData ) );
281 }
282
283 void wxSizer::Prepend( wxSizer *sizer, int option, int flag, int border, wxObject* userData )
284 {
285 m_children.Insert( new wxSizerItem( sizer, option, flag, border, userData ) );
286 }
287
288 void wxSizer::Prepend( int width, int height, int option, int flag, int border, wxObject* userData )
289 {
290 m_children.Insert( new wxSizerItem( width, height, option, flag, border, userData ) );
291 }
292
293 void wxSizer::Insert( int before, wxWindow *window, int option, int flag, int border, wxObject* userData )
294 {
295 m_children.Insert( before, new wxSizerItem( window, option, flag, border, userData ) );
296 }
297
298 void wxSizer::Insert( int before, wxSizer *sizer, int option, int flag, int border, wxObject* userData )
299 {
300 m_children.Insert( before, new wxSizerItem( sizer, option, flag, border, userData ) );
301 }
302
303 void wxSizer::Insert( int before, int width, int height, int option, int flag, int border, wxObject* userData )
304 {
305 m_children.Insert( before, new wxSizerItem( width, height, option, flag, border, userData ) );
306 }
307
308 bool wxSizer::Remove( wxWindow *window )
309 {
310 wxASSERT( window );
311
312 wxNode *node = m_children.First();
313 while (node)
314 {
315 wxSizerItem *item = (wxSizerItem*)node->Data();
316 if (item->GetWindow() == window)
317 {
318 m_children.DeleteNode( node );
319 return TRUE;
320 }
321 node = node->Next();
322 }
323
324 return FALSE;
325 }
326
327 bool wxSizer::Remove( wxSizer *sizer )
328 {
329 wxASSERT( sizer );
330
331 wxNode *node = m_children.First();
332 while (node)
333 {
334 wxSizerItem *item = (wxSizerItem*)node->Data();
335 if (item->GetSizer() == sizer)
336 {
337 m_children.DeleteNode( node );
338 return TRUE;
339 }
340 node = node->Next();
341 }
342
343 return FALSE;
344 }
345
346 bool wxSizer::Remove( int pos )
347 {
348 wxNode *node = m_children.Nth( pos );
349 if (!node) return FALSE;
350
351 m_children.DeleteNode( node );
352
353 return TRUE;
354 }
355
356 void wxSizer::Fit( wxWindow *window )
357 {
358 wxSize size = FitSize( window );
359 window->SetSize( size );
360 }
361
362 void wxSizer::Layout()
363 {
364 CalcMin();
365 RecalcSizes();
366 }
367
368 void wxSizer::SetSizeHints( wxWindow *window )
369 {
370 wxSize size = FitSize( window );
371 window->SetSizeHints( size.x, size.y );
372 }
373
374 wxSize wxSizer::GetMaxWindowSize( wxWindow *window )
375 {
376 wxSize sizeMax = wxGetDisplaySize();
377 // make the max size a bit smaller than the screen, a window which takes
378 // the entire screen doesn't look very nice neither
379 sizeMax.x *= 9;
380 sizeMax.x /= 10;
381
382 sizeMax.y *= 9;
383 sizeMax.y /= 10;
384
385 return sizeMax;
386 }
387
388 wxSize wxSizer::GetMinWindowSize( wxWindow *window )
389 {
390 wxSize minSize( GetMinSize() );
391 wxSize size( window->GetSize() );
392 wxSize client_size( window->GetClientSize() );
393 return wxSize( minSize.x+size.x-client_size.x,
394 minSize.y+size.y-client_size.y );
395 }
396
397 // Return a window size that will fit within the screens dimensions
398 wxSize wxSizer::FitSize( wxWindow *window )
399 {
400 wxSize size = GetMinWindowSize( window );
401 wxSize sizeMax = GetMaxWindowSize( window );
402
403 if ( size.x > sizeMax.x )
404 size.x = sizeMax.x;
405 if ( size.y > sizeMax.y )
406 size.y = sizeMax.y;
407
408 return size;
409 }
410
411 void wxSizer::SetDimension( int x, int y, int width, int height )
412 {
413 m_position.x = x;
414 m_position.y = y;
415 m_size.x = width;
416 m_size.y = height;
417 CalcMin();
418 RecalcSizes();
419 }
420
421 wxSize wxSizer::GetMinSize()
422 {
423 wxSize ret( CalcMin() );
424 if (ret.x < m_minSize.x) ret.x = m_minSize.x;
425 if (ret.y < m_minSize.y) ret.y = m_minSize.y;
426 return ret;
427 }
428
429 void wxSizer::DoSetMinSize( int width, int height )
430 {
431 m_minSize.x = width;
432 m_minSize.y = height;
433 }
434
435 bool wxSizer::DoSetItemMinSize( wxWindow *window, int width, int height )
436 {
437 wxASSERT( window );
438
439 wxNode *node = m_children.First();
440 while (node)
441 {
442 wxSizerItem *item = (wxSizerItem*)node->Data();
443 if (item->GetWindow() == window)
444 {
445 item->SetInitSize( width, height );
446 return TRUE;
447 }
448 node = node->Next();
449 }
450
451 node = m_children.First();
452 while (node)
453 {
454 wxSizerItem *item = (wxSizerItem*)node->Data();
455 if (item->GetSizer())
456 {
457 /* It's a sizer, so lets search recursively. */
458 if (item->GetSizer()->DoSetItemMinSize( window, width, height ))
459 {
460 /* A child sizer found the requested windw, exit. */
461 return TRUE;
462 }
463 }
464 node = node->Next();
465 }
466
467 return FALSE;
468 }
469
470 bool wxSizer::DoSetItemMinSize( wxSizer *sizer, int width, int height )
471 {
472 wxASSERT( sizer );
473
474 wxNode *node = m_children.First();
475 while (node)
476 {
477 wxSizerItem *item = (wxSizerItem*)node->Data();
478 if (item->GetSizer() == sizer)
479 {
480 item->GetSizer()->DoSetMinSize( width, height );
481 return TRUE;
482 }
483 node = node->Next();
484 }
485
486 node = m_children.First();
487 while (node)
488 {
489 wxSizerItem *item = (wxSizerItem*)node->Data();
490 if (item->GetSizer())
491 {
492 /* It's a sizer, so lets search recursively. */
493 if (item->GetSizer()->DoSetItemMinSize( sizer, width, height ))
494 {
495 /* A child sizer found the requested windw, exit. */
496 return TRUE;
497 }
498 }
499 node = node->Next();
500 }
501
502 return FALSE;
503 }
504
505 bool wxSizer::DoSetItemMinSize( int pos, int width, int height )
506 {
507 wxNode *node = m_children.Nth( pos );
508 if (!node) return FALSE;
509
510 wxSizerItem *item = (wxSizerItem*) node->Data();
511 if (item->GetSizer())
512 {
513 /* Sizers contains the minimal size in them, if not calculated ... */
514 item->GetSizer()->DoSetMinSize( width, height );
515 }
516 else
517 {
518 /* ... whereas the minimal size of spacers and windows in stored
519 in the item */
520 item->SetInitSize( width, height );
521 }
522
523 return TRUE;
524 }
525
526 //---------------------------------------------------------------------------
527 // wxGridSizer
528 //---------------------------------------------------------------------------
529
530 wxGridSizer::wxGridSizer( int rows, int cols, int vgap, int hgap )
531 {
532 m_rows = rows;
533 m_cols = cols;
534 m_vgap = vgap;
535 m_hgap = hgap;
536 }
537
538 wxGridSizer::wxGridSizer( int cols, int vgap, int hgap )
539 {
540 m_rows = 0;
541 m_cols = cols;
542 m_vgap = vgap;
543 m_hgap = hgap;
544 }
545
546 void wxGridSizer::RecalcSizes()
547 {
548 if (m_children.GetCount() == 0)
549 return;
550
551 int nitems = m_children.GetCount();
552 int nrows = m_rows;
553 int ncols = m_cols;
554
555 if (ncols > 0)
556 nrows = (nitems + ncols-1) / ncols;
557 else
558 ncols = (nitems + nrows-1) / nrows;
559
560 wxSize sz( GetSize() );
561 wxPoint pt( GetPosition() );
562
563 int w = (sz.x - (ncols - 1) * m_hgap) / ncols;
564 int h = (sz.y - (nrows - 1) * m_vgap) / nrows;
565
566 int x = pt.x;
567 for (int c = 0; c < ncols; c++)
568 {
569 int y = pt.y;
570 for (int r = 0; r < nrows; r++)
571 {
572 int i = r * ncols + c;
573 if (i < nitems)
574 {
575 wxNode *node = m_children.Nth( i );
576 wxASSERT( node );
577
578 SetItemBounds( (wxSizerItem*) node->Data(), x, y, w, h);
579 }
580 y = y + h + m_vgap;
581 }
582 x = x + w + m_hgap;
583 }
584 }
585
586 wxSize wxGridSizer::CalcMin()
587 {
588 if (m_children.GetCount() == 0)
589 return wxSize(10,10);
590
591 int nitems = m_children.GetCount();
592 int nrows = m_rows;
593 int ncols = m_cols;
594
595 if (ncols > 0)
596 nrows = (nitems + ncols-1) / ncols;
597 else
598 ncols = (nitems + nrows-1) / nrows;
599
600 /* Find the max width and height for any component */
601 int w = 0;
602 int h = 0;
603
604 wxNode *node = m_children.First();
605 while (node)
606 {
607 wxSizerItem *item = (wxSizerItem*)node->Data();
608 wxSize sz( item->CalcMin() );
609 w = wxMax( w, sz.x );
610 h = wxMax( h, sz.y );
611
612 node = node->Next();
613 }
614
615 return wxSize(ncols * w + (ncols-1) * m_hgap,
616 nrows * h + (nrows-1) * m_vgap);
617 }
618
619 void wxGridSizer::SetItemBounds( wxSizerItem *item, int x, int y, int w, int h )
620 {
621 wxPoint pt( x,y );
622 wxSize sz( item->CalcMin() );
623 int flag = item->GetFlag();
624
625 if ((flag & wxEXPAND) || (flag & wxSHAPED))
626 {
627 sz = wxSize(w, h);
628 }
629 else
630 {
631 if (flag & wxALIGN_CENTER_HORIZONTAL)
632 {
633 pt.x = x + (w - sz.x) / 2;
634 }
635 else if (flag & wxALIGN_RIGHT)
636 {
637 pt.x = x + (w - sz.x);
638 }
639
640 if (flag & wxALIGN_CENTER_VERTICAL)
641 {
642 pt.y = y + (h - sz.y) / 2;
643 }
644 else if (flag & wxALIGN_BOTTOM)
645 {
646 pt.y = y + (h - sz.y);
647 }
648 }
649
650 item->SetDimension(pt, sz);
651 }
652
653 //---------------------------------------------------------------------------
654 // wxFlexGridSizer
655 //---------------------------------------------------------------------------
656
657 wxFlexGridSizer::wxFlexGridSizer( int rows, int cols, int vgap, int hgap )
658 : wxGridSizer( rows, cols, vgap, hgap )
659 {
660 m_rowHeights = (int*) NULL;
661 m_colWidths = (int*) NULL;
662 }
663
664 wxFlexGridSizer::wxFlexGridSizer( int cols, int vgap, int hgap )
665 : wxGridSizer( cols, vgap, hgap )
666 {
667 m_rowHeights = (int*) NULL;
668 m_colWidths = (int*) NULL;
669 }
670
671 wxFlexGridSizer::~wxFlexGridSizer()
672 {
673 if (m_rowHeights)
674 delete[] m_rowHeights;
675 if (m_colWidths)
676 delete[] m_colWidths;
677 }
678
679 void wxFlexGridSizer::CreateArrays()
680 {
681 if (m_rowHeights)
682 delete[] m_rowHeights;
683 if (m_colWidths)
684 delete[] m_colWidths;
685
686 if (m_children.GetCount() == 0)
687 return;
688
689 int nitems = m_children.GetCount();
690 int nrows = m_rows;
691 int ncols = m_cols;
692
693 if (ncols > 0)
694 nrows = (nitems + ncols-1) / ncols;
695 else
696 ncols = (nitems + nrows-1) / nrows;
697
698 m_rowHeights = new int[nrows];
699 m_colWidths = new int[ncols];
700
701 for (int col = 0; col < ncols; col++)
702 m_colWidths[ col ] = 0;
703 for (int row = 0; row < nrows; row++)
704 m_rowHeights[ row ] = 0;
705 }
706
707 void wxFlexGridSizer::RecalcSizes()
708 {
709 if (m_children.GetCount() == 0)
710 return;
711
712 int nitems = m_children.GetCount();
713 int nrows = m_rows;
714 int ncols = m_cols;
715
716 if (ncols > 0)
717 nrows = (nitems + ncols-1) / ncols;
718 else
719 ncols = (nitems + nrows-1) / nrows;
720
721 wxSize sz( GetSize() );
722 wxSize minsz( CalcMin() );
723 wxPoint pt( GetPosition() );
724 int delta;
725 size_t idx;
726
727 if ((m_growableRows.GetCount() > 0) && (sz.y > minsz.y))
728 {
729 delta = (sz.y - minsz.y) / m_growableRows.GetCount();
730 for (idx = 0; idx < m_growableRows.GetCount(); idx++)
731 m_rowHeights[ m_growableRows[idx] ] += delta;
732 }
733
734 if ((m_growableCols.GetCount() > 0) && (sz.x > minsz.x))
735 {
736 delta = (sz.x - minsz.x) / m_growableCols.GetCount();
737 for (idx = 0; idx < m_growableCols.GetCount(); idx++)
738 m_colWidths[ m_growableCols[idx] ] += delta;
739 }
740
741 sz = wxSize( pt.x + sz.x, pt.y + sz.y );
742
743 int x = pt.x;
744 for (int c = 0; c < ncols; c++)
745 {
746 int y = pt.y;
747 for (int r = 0; r < nrows; r++)
748 {
749 int i = r * ncols + c;
750 if (i < nitems)
751 {
752 wxNode *node = m_children.Nth( i );
753 wxASSERT( node );
754
755 int w = wxMax( 0, wxMin( m_colWidths[c], sz.x - x ) );
756 int h = wxMax( 0, wxMin( m_rowHeights[r], sz.y - y ) );
757
758 SetItemBounds( (wxSizerItem*) node->Data(), x, y, w, h);
759 }
760 y = y + m_rowHeights[r] + m_vgap;
761 }
762 x = x + m_colWidths[c] + m_hgap;
763 }
764 }
765
766 wxSize wxFlexGridSizer::CalcMin()
767 {
768 if (m_children.GetCount() == 0)
769 return wxSize(10,10);
770
771 int nitems = m_children.GetCount();
772 int nrows = m_rows;
773 int ncols = m_cols;
774
775 if (ncols > 0)
776 nrows = (nitems + ncols-1) / ncols;
777 else
778 ncols = (nitems + nrows-1) / nrows;
779
780 CreateArrays();
781
782 int col;
783 int row;
784
785 int i = 0;
786 wxNode *node = m_children.First();
787 while (node)
788 {
789 wxSizerItem *item = (wxSizerItem*)node->Data();
790 wxSize sz( item->CalcMin() );
791 row = i / ncols;
792 col = i % ncols;
793 m_rowHeights[ row ] = wxMax( sz.y, m_rowHeights[ row ] );
794 m_colWidths[ col ] = wxMax( sz.x, m_colWidths[ col ] );
795
796 node = node->Next();
797 i++;
798 }
799
800 int width = 0;
801 for (col = 0; col < ncols; col++)
802 width += m_colWidths[ col ];
803
804 int height = 0;
805 for (row = 0; row < nrows; row++)
806 height += m_rowHeights[ row ];
807
808 return wxSize( width + (ncols-1) * m_hgap,
809 height + (nrows-1) * m_vgap);
810 }
811
812 void wxFlexGridSizer::AddGrowableRow( size_t idx )
813 {
814 m_growableRows.Add( idx );
815 }
816
817 void wxFlexGridSizer::RemoveGrowableRow( size_t WXUNUSED(idx) )
818 {
819 }
820
821 void wxFlexGridSizer::AddGrowableCol( size_t idx )
822 {
823 m_growableCols.Add( idx );
824 }
825
826 void wxFlexGridSizer::RemoveGrowableCol( size_t WXUNUSED(idx) )
827 {
828 }
829
830 //---------------------------------------------------------------------------
831 // wxBoxSizer
832 //---------------------------------------------------------------------------
833
834 wxBoxSizer::wxBoxSizer( int orient )
835 {
836 m_orient = orient;
837 }
838
839 void wxBoxSizer::RecalcSizes()
840 {
841 if (m_children.GetCount() == 0)
842 return;
843
844 int delta = 0;
845 int extra = 0;
846 if (m_stretchable)
847 {
848 if (m_orient == wxHORIZONTAL)
849 {
850 delta = (m_size.x - m_fixedWidth) / m_stretchable;
851 extra = (m_size.x - m_fixedWidth) % m_stretchable;
852 }
853 else
854 {
855 delta = (m_size.y - m_fixedHeight) / m_stretchable;
856 extra = (m_size.y - m_fixedHeight) % m_stretchable;
857 }
858 }
859
860 wxPoint pt( m_position );
861
862 wxNode *node = m_children.GetFirst();
863 while (node)
864 {
865 wxSizerItem *item = (wxSizerItem*) node->Data();
866
867 int weight = 1;
868 if (item->GetOption())
869 weight = item->GetOption();
870
871 wxSize size( item->CalcMin() );
872
873 if (m_orient == wxVERTICAL)
874 {
875 wxCoord height = size.y;
876 if (item->GetOption())
877 {
878 height = (delta * weight) + extra;
879 extra = 0; // only the first item will get the remainder as extra size
880 }
881
882 wxPoint child_pos( pt );
883 wxSize child_size( wxSize( size.x, height) );
884
885 if (item->GetFlag() & (wxEXPAND | wxSHAPED))
886 child_size.x = m_size.x;
887 else if (item->GetFlag() & wxALIGN_RIGHT)
888 child_pos.x += m_size.x - size.x;
889 else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_HORIZONTAL))
890 // XXX wxCENTER is added for backward compatibility;
891 // wxALIGN_CENTER should be used in new code
892 child_pos.x += (m_size.x - size.x) / 2;
893
894 item->SetDimension( child_pos, child_size );
895
896 pt.y += height;
897 }
898 else
899 {
900 wxCoord width = size.x;
901 if (item->GetOption())
902 {
903 width = (delta * weight) + extra;
904 extra = 0; // only the first item will get the remainder as extra size
905 }
906
907 wxPoint child_pos( pt );
908 wxSize child_size( wxSize(width, size.y) );
909
910 if (item->GetFlag() & (wxEXPAND | wxSHAPED))
911 child_size.y = m_size.y;
912 else if (item->GetFlag() & wxALIGN_BOTTOM)
913 child_pos.y += m_size.y - size.y;
914 else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_VERTICAL))
915 // XXX wxCENTER is added for backward compatibility;
916 // wxALIGN_CENTER should be used in new code
917 child_pos.y += (m_size.y - size.y) / 2;
918
919 item->SetDimension( child_pos, child_size );
920
921 pt.x += width;
922 }
923
924 node = node->Next();
925 }
926 }
927
928 wxSize wxBoxSizer::CalcMin()
929 {
930 if (m_children.GetCount() == 0)
931 return wxSize(10,10);
932
933 m_stretchable = 0;
934 m_minWidth = 0;
935 m_minHeight = 0;
936 m_fixedWidth = 0;
937 m_fixedHeight = 0;
938
939 wxNode *node = m_children.GetFirst();
940 while (node)
941 {
942 wxSizerItem *item = (wxSizerItem*) node->Data();
943
944 m_stretchable += item->GetOption();
945
946 wxSize size( item->CalcMin() );
947
948 if (m_orient == wxHORIZONTAL)
949 {
950 m_minWidth += size.x;
951 m_minHeight = wxMax( m_minHeight, size.y );
952 }
953 else
954 {
955 m_minHeight += size.y;
956 m_minWidth = wxMax( m_minWidth, size.x );
957 }
958
959 if (item->GetOption() == 0)
960 {
961 if (m_orient == wxVERTICAL)
962 {
963 m_fixedHeight += size.y;
964 m_fixedWidth = wxMax( m_fixedWidth, size.x );
965 }
966 else
967 {
968 m_fixedWidth += size.x;
969 m_fixedHeight = wxMax( m_fixedHeight, size.y );
970 }
971 }
972
973 node = node->Next();
974 }
975
976 return wxSize( m_minWidth, m_minHeight );
977 }
978
979 //---------------------------------------------------------------------------
980 // wxStaticBoxSizer
981 //---------------------------------------------------------------------------
982
983 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox *box, int orient )
984 : wxBoxSizer( orient )
985 {
986 wxASSERT_MSG( box, wxT("wxStaticBoxSizer needs a static box") );
987
988 m_staticBox = box;
989 }
990
991 static void GetStaticBoxBorders(wxStaticBox *box,
992 int *borderTop, int *borderOther)
993 {
994 // this has to be done platform by platform as there is no way to
995 // guess the thickness of a wxStaticBox border
996 #ifdef __WXGTK__
997 if ( box->GetLabel().IsEmpty() )
998 *borderTop = 5;
999 else
1000 #endif // __WXGTK__
1001 *borderTop = 15;
1002
1003 *borderOther = 5;
1004 }
1005
1006 void wxStaticBoxSizer::RecalcSizes()
1007 {
1008 int top_border, other_border;
1009 GetStaticBoxBorders(m_staticBox, &top_border, &other_border);
1010
1011 m_staticBox->SetSize( m_position.x, m_position.y, m_size.x, m_size.y );
1012
1013 wxPoint old_pos( m_position );
1014 m_position.x += other_border;
1015 m_position.y += top_border;
1016 wxSize old_size( m_size );
1017 m_size.x -= 2*other_border;
1018 m_size.y -= top_border + other_border;
1019
1020 wxBoxSizer::RecalcSizes();
1021
1022 m_position = old_pos;
1023 m_size = old_size;
1024 }
1025
1026 wxSize wxStaticBoxSizer::CalcMin()
1027 {
1028 int top_border, other_border;
1029 GetStaticBoxBorders(m_staticBox, &top_border, &other_border);
1030
1031 wxSize ret( wxBoxSizer::CalcMin() );
1032 ret.x += 2*other_border;
1033 ret.y += other_border + top_border;
1034
1035 return ret;
1036 }
1037
1038 //---------------------------------------------------------------------------
1039 // wxNotebookSizer
1040 //---------------------------------------------------------------------------
1041
1042 #if wxUSE_NOTEBOOK
1043
1044 wxNotebookSizer::wxNotebookSizer( wxNotebook *nb )
1045 {
1046 wxASSERT_MSG( nb, wxT("wxNotebookSizer needs a notebook") );
1047
1048 m_notebook = nb;
1049 }
1050
1051 void wxNotebookSizer::RecalcSizes()
1052 {
1053 m_notebook->SetSize( m_position.x, m_position.y, m_size.x, m_size.y );
1054 }
1055
1056 wxSize wxNotebookSizer::CalcMin()
1057 {
1058 // This will have to be done platform by platform
1059 // as there is no way to guess the thickness of
1060 // the wxNotebook tabs and border.
1061
1062 int borderX = 5;
1063 int borderY = 5;
1064 if ((m_notebook->HasFlag(wxNB_RIGHT)) ||
1065 (m_notebook->HasFlag(wxNB_LEFT)))
1066 {
1067 borderX += 90; // improvements later..
1068 }
1069 else
1070 {
1071 borderY += 40; // improvements later..
1072 }
1073
1074 if (m_notebook->GetChildren().GetCount() == 0)
1075 return wxSize(borderX + 10, borderY + 10);
1076
1077 int maxX = 0;
1078 int maxY = 0;
1079
1080 wxWindowList::Node *node = m_notebook->GetChildren().GetFirst();
1081 while (node)
1082 {
1083 wxWindow *item = node->GetData();
1084 wxSizer *itemsizer = item->GetSizer();
1085
1086 if (itemsizer)
1087 {
1088 wxSize subsize( itemsizer->CalcMin() );
1089
1090 if (subsize.x > maxX) maxX = subsize.x;
1091 if (subsize.y > maxY) maxY = subsize.y;
1092 }
1093
1094 node = node->GetNext();
1095 }
1096
1097 return wxSize( borderX + maxX, borderY + maxY );
1098 }
1099
1100 #endif // wxUSE_NOTEBOOK