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