]> git.saurik.com Git - wxWidgets.git/blob - src/common/sizer.cpp
added test for wxNO_DEFAULT in wxMessageBox
[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 Clear();
273 }
274
275 void wxSizer::Add( wxWindow *window, int option, int flag, int border, wxObject* userData )
276 {
277 m_children.Append( new wxSizerItem( window, option, flag, border, userData ) );
278 window->SetContainingSizer(this);
279 }
280
281 void wxSizer::Add( wxSizer *sizer, int option, int flag, int border, wxObject* userData )
282 {
283 m_children.Append( new wxSizerItem( sizer, option, flag, border, userData ) );
284 }
285
286 void wxSizer::Add( int width, int height, int option, int flag, int border, wxObject* userData )
287 {
288 m_children.Append( new wxSizerItem( width, height, option, flag, border, userData ) );
289 }
290
291 void wxSizer::Prepend( wxWindow *window, int option, int flag, int border, wxObject* userData )
292 {
293 m_children.Insert( new wxSizerItem( window, option, flag, border, userData ) );
294 window->SetContainingSizer(this);
295 }
296
297 void wxSizer::Prepend( wxSizer *sizer, int option, int flag, int border, wxObject* userData )
298 {
299 m_children.Insert( new wxSizerItem( sizer, option, flag, border, userData ) );
300 }
301
302 void wxSizer::Prepend( int width, int height, int option, int flag, int border, wxObject* userData )
303 {
304 m_children.Insert( new wxSizerItem( width, height, option, flag, border, userData ) );
305 }
306
307 void wxSizer::Insert( int before, wxWindow *window, int option, int flag, int border, wxObject* userData )
308 {
309 m_children.Insert( before, new wxSizerItem( window, option, flag, border, userData ) );
310 window->SetContainingSizer(this);
311 }
312
313 void wxSizer::Insert( int before, wxSizer *sizer, int option, int flag, int border, wxObject* userData )
314 {
315 m_children.Insert( before, new wxSizerItem( sizer, option, flag, border, userData ) );
316 }
317
318 void wxSizer::Insert( int before, int width, int height, int option, int flag, int border, wxObject* userData )
319 {
320 m_children.Insert( before, new wxSizerItem( width, height, option, flag, border, userData ) );
321 }
322
323 bool wxSizer::Remove( wxWindow *window )
324 {
325 wxASSERT( window );
326
327 wxNode *node = m_children.First();
328 while (node)
329 {
330 wxSizerItem *item = (wxSizerItem*)node->Data();
331 if (item->GetWindow() == window)
332 {
333 item->GetWindow()->SetContainingSizer(NULL);
334 m_children.DeleteNode( node );
335 return TRUE;
336 }
337 node = node->Next();
338 }
339
340 return FALSE;
341 }
342
343 bool wxSizer::Remove( wxSizer *sizer )
344 {
345 wxASSERT( sizer );
346
347 wxNode *node = m_children.First();
348 while (node)
349 {
350 wxSizerItem *item = (wxSizerItem*)node->Data();
351 if (item->GetSizer() == sizer)
352 {
353 m_children.DeleteNode( node );
354 return TRUE;
355 }
356 node = node->Next();
357 }
358
359 return FALSE;
360 }
361
362 bool wxSizer::Remove( int pos )
363 {
364 wxNode *node = m_children.Nth( pos );
365 if (!node) return FALSE;
366
367 m_children.DeleteNode( node );
368
369 return TRUE;
370 }
371
372 void wxSizer::Clear( bool delete_windows )
373 {
374 // First clear the ContainingSizer pointers
375 wxNode *node = m_children.First();
376 while (node)
377 {
378 wxSizerItem *item = (wxSizerItem*)node->Data();
379 if (item->IsWindow())
380 item->GetWindow()->SetContainingSizer(NULL);
381 node = node->Next();
382 }
383
384 // Destroy the windows if needed
385 if (delete_windows)
386 DeleteWindows();
387
388 // Now empty the list
389 m_children.Clear();
390 }
391
392 void wxSizer::DeleteWindows()
393 {
394 wxNode *node = m_children.First();
395 while (node)
396 {
397 wxSizerItem *item = (wxSizerItem*)node->Data();
398 item->DeleteWindows();
399 node = node->Next();
400 }
401 }
402
403 void wxSizer::Fit( wxWindow *window )
404 {
405 wxSize size;
406 if (window->IsTopLevel())
407 size = FitSize( window );
408 else
409 size = GetMinWindowSize( window );
410
411 //window->SetClientSize( size );
412 window->SetSize( size );
413 }
414
415 void wxSizer::Layout()
416 {
417 CalcMin();
418 RecalcSizes();
419 }
420
421 void wxSizer::SetSizeHints( wxWindow *window )
422 {
423 // Preserve the window's max size hints, but set the
424 // lower bound according to the sizer calculations.
425
426 wxSize size = FitSize( window );
427 window->SetSizeHints( size.x,
428 size.y,
429 window->GetMaxWidth(),
430 window->GetMaxHeight() );
431 }
432
433 wxSize wxSizer::GetMaxWindowSize( wxWindow *window )
434 {
435 return window->GetMaxSize();
436 }
437
438 wxSize wxSizer::GetMinWindowSize( wxWindow *window )
439 {
440 wxSize minSize( GetMinSize() );
441 wxSize size( window->GetSize() );
442 wxSize client_size( window->GetClientSize() );
443 return wxSize( minSize.x+size.x-client_size.x,
444 minSize.y+size.y-client_size.y );
445 }
446
447 // Return a window size that will fit within the screens dimensions
448 wxSize wxSizer::FitSize( wxWindow *window )
449 {
450 wxSize size = GetMinWindowSize( window );
451 wxSize sizeMax = GetMaxWindowSize( window );
452
453 // Limit the size if sizeMax != wxDefaultSize
454
455 if ( size.x > sizeMax.x && sizeMax.x != -1 )
456 size.x = sizeMax.x;
457 if ( size.y > sizeMax.y && sizeMax.y != -1 )
458 size.y = sizeMax.y;
459
460 return size;
461 }
462
463 void wxSizer::SetDimension( int x, int y, int width, int height )
464 {
465 m_position.x = x;
466 m_position.y = y;
467 m_size.x = width;
468 m_size.y = height;
469 CalcMin();
470 RecalcSizes();
471 }
472
473 wxSize wxSizer::GetMinSize()
474 {
475 wxSize ret( CalcMin() );
476 if (ret.x < m_minSize.x) ret.x = m_minSize.x;
477 if (ret.y < m_minSize.y) ret.y = m_minSize.y;
478 return ret;
479 }
480
481 void wxSizer::DoSetMinSize( int width, int height )
482 {
483 m_minSize.x = width;
484 m_minSize.y = height;
485 }
486
487 bool wxSizer::DoSetItemMinSize( wxWindow *window, int width, int height )
488 {
489 wxASSERT( window );
490
491 wxNode *node = m_children.First();
492 while (node)
493 {
494 wxSizerItem *item = (wxSizerItem*)node->Data();
495 if (item->GetWindow() == window)
496 {
497 item->SetInitSize( width, height );
498 return TRUE;
499 }
500 node = node->Next();
501 }
502
503 node = m_children.First();
504 while (node)
505 {
506 wxSizerItem *item = (wxSizerItem*)node->Data();
507 if (item->GetSizer())
508 {
509 /* It's a sizer, so lets search recursively. */
510 if (item->GetSizer()->DoSetItemMinSize( window, width, height ))
511 {
512 /* A child sizer found the requested windw, exit. */
513 return TRUE;
514 }
515 }
516 node = node->Next();
517 }
518
519 return FALSE;
520 }
521
522 bool wxSizer::DoSetItemMinSize( wxSizer *sizer, int width, int height )
523 {
524 wxASSERT( sizer );
525
526 wxNode *node = m_children.First();
527 while (node)
528 {
529 wxSizerItem *item = (wxSizerItem*)node->Data();
530 if (item->GetSizer() == sizer)
531 {
532 item->GetSizer()->DoSetMinSize( width, height );
533 return TRUE;
534 }
535 node = node->Next();
536 }
537
538 node = m_children.First();
539 while (node)
540 {
541 wxSizerItem *item = (wxSizerItem*)node->Data();
542 if (item->GetSizer())
543 {
544 /* It's a sizer, so lets search recursively. */
545 if (item->GetSizer()->DoSetItemMinSize( sizer, width, height ))
546 {
547 /* A child sizer found the requested windw, exit. */
548 return TRUE;
549 }
550 }
551 node = node->Next();
552 }
553
554 return FALSE;
555 }
556
557 bool wxSizer::DoSetItemMinSize( int pos, int width, int height )
558 {
559 wxNode *node = m_children.Nth( pos );
560 if (!node) return FALSE;
561
562 wxSizerItem *item = (wxSizerItem*) node->Data();
563 if (item->GetSizer())
564 {
565 /* Sizers contains the minimal size in them, if not calculated ... */
566 item->GetSizer()->DoSetMinSize( width, height );
567 }
568 else
569 {
570 /* ... whereas the minimal size of spacers and windows in stored
571 in the item */
572 item->SetInitSize( width, height );
573 }
574
575 return TRUE;
576 }
577
578 //---------------------------------------------------------------------------
579 // wxGridSizer
580 //---------------------------------------------------------------------------
581
582 wxGridSizer::wxGridSizer( int rows, int cols, int vgap, int hgap )
583 {
584 m_rows = rows;
585 m_cols = cols;
586 m_vgap = vgap;
587 m_hgap = hgap;
588 }
589
590 wxGridSizer::wxGridSizer( int cols, int vgap, int hgap )
591 {
592 m_rows = 0;
593 m_cols = cols;
594 m_vgap = vgap;
595 m_hgap = hgap;
596 }
597
598 void wxGridSizer::RecalcSizes()
599 {
600 if (m_children.GetCount() == 0)
601 return;
602
603 int nitems = m_children.GetCount();
604 int nrows = m_rows;
605 int ncols = m_cols;
606
607 if (ncols > 0)
608 nrows = (nitems + ncols-1) / ncols;
609 else
610 ncols = (nitems + nrows-1) / nrows;
611
612 wxSize sz( GetSize() );
613 wxPoint pt( GetPosition() );
614
615 int w = (sz.x - (ncols - 1) * m_hgap) / ncols;
616 int h = (sz.y - (nrows - 1) * m_vgap) / nrows;
617
618 int x = pt.x;
619 for (int c = 0; c < ncols; c++)
620 {
621 int y = pt.y;
622 for (int r = 0; r < nrows; r++)
623 {
624 int i = r * ncols + c;
625 if (i < nitems)
626 {
627 wxNode *node = m_children.Nth( i );
628 wxASSERT( node );
629
630 SetItemBounds( (wxSizerItem*) node->Data(), x, y, w, h);
631 }
632 y = y + h + m_vgap;
633 }
634 x = x + w + m_hgap;
635 }
636 }
637
638 wxSize wxGridSizer::CalcMin()
639 {
640 if (m_children.GetCount() == 0)
641 return wxSize(10,10);
642
643 int nitems = m_children.GetCount();
644 int nrows = m_rows;
645 int ncols = m_cols;
646
647 if (ncols > 0)
648 nrows = (nitems + ncols-1) / ncols;
649 else
650 ncols = (nitems + nrows-1) / nrows;
651
652 /* Find the max width and height for any component */
653 int w = 0;
654 int h = 0;
655
656 wxNode *node = m_children.First();
657 while (node)
658 {
659 wxSizerItem *item = (wxSizerItem*)node->Data();
660 wxSize sz( item->CalcMin() );
661 w = wxMax( w, sz.x );
662 h = wxMax( h, sz.y );
663
664 node = node->Next();
665 }
666
667 return wxSize(ncols * w + (ncols-1) * m_hgap,
668 nrows * h + (nrows-1) * m_vgap);
669 }
670
671 void wxGridSizer::SetItemBounds( wxSizerItem *item, int x, int y, int w, int h )
672 {
673 wxPoint pt( x,y );
674 wxSize sz( item->CalcMin() );
675 int flag = item->GetFlag();
676
677 if ((flag & wxEXPAND) || (flag & wxSHAPED))
678 {
679 sz = wxSize(w, h);
680 }
681 else
682 {
683 if (flag & wxALIGN_CENTER_HORIZONTAL)
684 {
685 pt.x = x + (w - sz.x) / 2;
686 }
687 else if (flag & wxALIGN_RIGHT)
688 {
689 pt.x = x + (w - sz.x);
690 }
691
692 if (flag & wxALIGN_CENTER_VERTICAL)
693 {
694 pt.y = y + (h - sz.y) / 2;
695 }
696 else if (flag & wxALIGN_BOTTOM)
697 {
698 pt.y = y + (h - sz.y);
699 }
700 }
701
702 item->SetDimension(pt, sz);
703 }
704
705 //---------------------------------------------------------------------------
706 // wxFlexGridSizer
707 //---------------------------------------------------------------------------
708
709 wxFlexGridSizer::wxFlexGridSizer( int rows, int cols, int vgap, int hgap )
710 : wxGridSizer( rows, cols, vgap, hgap )
711 {
712 m_rowHeights = (int*) NULL;
713 m_colWidths = (int*) NULL;
714 }
715
716 wxFlexGridSizer::wxFlexGridSizer( int cols, int vgap, int hgap )
717 : wxGridSizer( cols, vgap, hgap )
718 {
719 m_rowHeights = (int*) NULL;
720 m_colWidths = (int*) NULL;
721 }
722
723 wxFlexGridSizer::~wxFlexGridSizer()
724 {
725 if (m_rowHeights)
726 delete[] m_rowHeights;
727 if (m_colWidths)
728 delete[] m_colWidths;
729 }
730
731 void wxFlexGridSizer::CreateArrays()
732 {
733 if (m_rowHeights)
734 delete[] m_rowHeights;
735 if (m_colWidths)
736 delete[] m_colWidths;
737
738 if (m_children.GetCount() == 0)
739 return;
740
741 int nitems = m_children.GetCount();
742 int nrows = m_rows;
743 int ncols = m_cols;
744
745 if (ncols > 0)
746 nrows = (nitems + ncols-1) / ncols;
747 else
748 ncols = (nitems + nrows-1) / nrows;
749
750 m_rowHeights = new int[nrows];
751 m_colWidths = new int[ncols];
752
753 for (int col = 0; col < ncols; col++)
754 m_colWidths[ col ] = 0;
755 for (int row = 0; row < nrows; row++)
756 m_rowHeights[ row ] = 0;
757 }
758
759 void wxFlexGridSizer::RecalcSizes()
760 {
761 if (m_children.GetCount() == 0)
762 return;
763
764 int nitems = m_children.GetCount();
765 int nrows = m_rows;
766 int ncols = m_cols;
767
768 if (ncols > 0)
769 nrows = (nitems + ncols-1) / ncols;
770 else
771 ncols = (nitems + nrows-1) / nrows;
772
773 wxSize sz( GetSize() );
774 wxSize minsz( CalcMin() );
775 wxPoint pt( GetPosition() );
776 int delta;
777 size_t idx;
778
779 if ((m_growableRows.GetCount() > 0) && (sz.y > minsz.y))
780 {
781 delta = (sz.y - minsz.y) / m_growableRows.GetCount();
782 for (idx = 0; idx < m_growableRows.GetCount(); idx++)
783 m_rowHeights[ m_growableRows[idx] ] += delta;
784 }
785
786 if ((m_growableCols.GetCount() > 0) && (sz.x > minsz.x))
787 {
788 delta = (sz.x - minsz.x) / m_growableCols.GetCount();
789 for (idx = 0; idx < m_growableCols.GetCount(); idx++)
790 m_colWidths[ m_growableCols[idx] ] += delta;
791 }
792
793 sz = wxSize( pt.x + sz.x, pt.y + sz.y );
794
795 int x = pt.x;
796 for (int c = 0; c < ncols; c++)
797 {
798 int y = pt.y;
799 for (int r = 0; r < nrows; r++)
800 {
801 int i = r * ncols + c;
802 if (i < nitems)
803 {
804 wxNode *node = m_children.Nth( i );
805 wxASSERT( node );
806
807 int w = wxMax( 0, wxMin( m_colWidths[c], sz.x - x ) );
808 int h = wxMax( 0, wxMin( m_rowHeights[r], sz.y - y ) );
809
810 SetItemBounds( (wxSizerItem*) node->Data(), x, y, w, h);
811 }
812 y = y + m_rowHeights[r] + m_vgap;
813 }
814 x = x + m_colWidths[c] + m_hgap;
815 }
816 }
817
818 wxSize wxFlexGridSizer::CalcMin()
819 {
820 if (m_children.GetCount() == 0)
821 return wxSize(10,10);
822
823 int nitems = m_children.GetCount();
824 int nrows = m_rows;
825 int ncols = m_cols;
826
827 if (ncols > 0)
828 nrows = (nitems + ncols-1) / ncols;
829 else
830 ncols = (nitems + nrows-1) / nrows;
831
832 CreateArrays();
833
834 int col;
835 int row;
836
837 int i = 0;
838 wxNode *node = m_children.First();
839 while (node)
840 {
841 wxSizerItem *item = (wxSizerItem*)node->Data();
842 wxSize sz( item->CalcMin() );
843 row = i / ncols;
844 col = i % ncols;
845 m_rowHeights[ row ] = wxMax( sz.y, m_rowHeights[ row ] );
846 m_colWidths[ col ] = wxMax( sz.x, m_colWidths[ col ] );
847
848 node = node->Next();
849 i++;
850 }
851
852 int width = 0;
853 for (col = 0; col < ncols; col++)
854 width += m_colWidths[ col ];
855
856 int height = 0;
857 for (row = 0; row < nrows; row++)
858 height += m_rowHeights[ row ];
859
860 return wxSize( width + (ncols-1) * m_hgap,
861 height + (nrows-1) * m_vgap);
862 }
863
864 void wxFlexGridSizer::AddGrowableRow( size_t idx )
865 {
866 m_growableRows.Add( idx );
867 }
868
869 void wxFlexGridSizer::RemoveGrowableRow( size_t WXUNUSED(idx) )
870 {
871 }
872
873 void wxFlexGridSizer::AddGrowableCol( size_t idx )
874 {
875 m_growableCols.Add( idx );
876 }
877
878 void wxFlexGridSizer::RemoveGrowableCol( size_t WXUNUSED(idx) )
879 {
880 }
881
882 //---------------------------------------------------------------------------
883 // wxBoxSizer
884 //---------------------------------------------------------------------------
885
886 wxBoxSizer::wxBoxSizer( int orient )
887 {
888 m_orient = orient;
889 }
890
891 void wxBoxSizer::RecalcSizes()
892 {
893 if (m_children.GetCount() == 0)
894 return;
895
896 int delta = 0;
897 int extra = 0;
898 if (m_stretchable)
899 {
900 if (m_orient == wxHORIZONTAL)
901 {
902 delta = (m_size.x - m_fixedWidth) / m_stretchable;
903 extra = (m_size.x - m_fixedWidth) % m_stretchable;
904 }
905 else
906 {
907 delta = (m_size.y - m_fixedHeight) / m_stretchable;
908 extra = (m_size.y - m_fixedHeight) % m_stretchable;
909 }
910 }
911
912 wxPoint pt( m_position );
913
914 wxNode *node = m_children.GetFirst();
915 while (node)
916 {
917 wxSizerItem *item = (wxSizerItem*) node->Data();
918
919 int weight = 1;
920 if (item->GetOption())
921 weight = item->GetOption();
922
923 wxSize size( item->CalcMin() );
924
925 if (m_orient == wxVERTICAL)
926 {
927 wxCoord height = size.y;
928 if (item->GetOption())
929 {
930 height = (delta * weight) + extra;
931 extra = 0; // only the first item will get the remainder as extra size
932 }
933
934 wxPoint child_pos( pt );
935 wxSize child_size( wxSize( size.x, height) );
936
937 if (item->GetFlag() & (wxEXPAND | wxSHAPED))
938 child_size.x = m_size.x;
939 else if (item->GetFlag() & wxALIGN_RIGHT)
940 child_pos.x += m_size.x - size.x;
941 else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_HORIZONTAL))
942 // XXX wxCENTER is added for backward compatibility;
943 // wxALIGN_CENTER should be used in new code
944 child_pos.x += (m_size.x - size.x) / 2;
945
946 item->SetDimension( child_pos, child_size );
947
948 pt.y += height;
949 }
950 else
951 {
952 wxCoord width = size.x;
953 if (item->GetOption())
954 {
955 width = (delta * weight) + extra;
956 extra = 0; // only the first item will get the remainder as extra size
957 }
958
959 wxPoint child_pos( pt );
960 wxSize child_size( wxSize(width, size.y) );
961
962 if (item->GetFlag() & (wxEXPAND | wxSHAPED))
963 child_size.y = m_size.y;
964 else if (item->GetFlag() & wxALIGN_BOTTOM)
965 child_pos.y += m_size.y - size.y;
966 else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_VERTICAL))
967 // XXX wxCENTER is added for backward compatibility;
968 // wxALIGN_CENTER should be used in new code
969 child_pos.y += (m_size.y - size.y) / 2;
970
971 item->SetDimension( child_pos, child_size );
972
973 pt.x += width;
974 }
975
976 node = node->Next();
977 }
978 }
979
980 wxSize wxBoxSizer::CalcMin()
981 {
982 if (m_children.GetCount() == 0)
983 return wxSize(10,10);
984
985 m_stretchable = 0;
986 m_minWidth = 0;
987 m_minHeight = 0;
988 m_fixedWidth = 0;
989 m_fixedHeight = 0;
990
991 // Find how long each stretch unit needs to be
992 int stretchSize = 1;
993 wxNode *node = m_children.GetFirst();
994 while (node)
995 {
996 wxSizerItem *item = (wxSizerItem*) node->Data();
997 if (item->GetOption() != 0)
998 {
999 int stretch = item->GetOption();
1000 wxSize size( item->CalcMin() );
1001 int sizePerStretch;
1002 // Integer division rounded up is (a + b - 1) / b
1003 if (m_orient == wxHORIZONTAL)
1004 sizePerStretch = ( size.x + stretch - 1 ) / stretch;
1005 else
1006 sizePerStretch = ( size.y + stretch - 1 ) / stretch;
1007 if (sizePerStretch > stretchSize)
1008 stretchSize = sizePerStretch;
1009 }
1010 node = node->Next();
1011 }
1012 // Calculate overall minimum size
1013 node = m_children.GetFirst();
1014 while (node)
1015 {
1016 wxSizerItem *item = (wxSizerItem*) node->Data();
1017
1018 m_stretchable += item->GetOption();
1019
1020 wxSize size( item->CalcMin() );
1021 if (item->GetOption() != 0)
1022 {
1023 if (m_orient == wxHORIZONTAL)
1024 size.x = stretchSize * item->GetOption();
1025 else
1026 size.y = stretchSize * item->GetOption();
1027 }
1028
1029 if (m_orient == wxHORIZONTAL)
1030 {
1031 m_minWidth += size.x;
1032 m_minHeight = wxMax( m_minHeight, size.y );
1033 }
1034 else
1035 {
1036 m_minHeight += size.y;
1037 m_minWidth = wxMax( m_minWidth, size.x );
1038 }
1039
1040 if (item->GetOption() == 0)
1041 {
1042 if (m_orient == wxVERTICAL)
1043 {
1044 m_fixedHeight += size.y;
1045 m_fixedWidth = wxMax( m_fixedWidth, size.x );
1046 }
1047 else
1048 {
1049 m_fixedWidth += size.x;
1050 m_fixedHeight = wxMax( m_fixedHeight, size.y );
1051 }
1052 }
1053
1054 node = node->Next();
1055 }
1056
1057 return wxSize( m_minWidth, m_minHeight );
1058 }
1059
1060 //---------------------------------------------------------------------------
1061 // wxStaticBoxSizer
1062 //---------------------------------------------------------------------------
1063
1064 #if wxUSE_STATBOX
1065
1066 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox *box, int orient )
1067 : wxBoxSizer( orient )
1068 {
1069 wxASSERT_MSG( box, wxT("wxStaticBoxSizer needs a static box") );
1070
1071 m_staticBox = box;
1072 }
1073
1074 static void GetStaticBoxBorders(wxStaticBox *box,
1075 int *borderTop, int *borderOther)
1076 {
1077 // this has to be done platform by platform as there is no way to
1078 // guess the thickness of a wxStaticBox border
1079 #ifdef __WXGTK__
1080 if ( box->GetLabel().IsEmpty() )
1081 *borderTop = 5;
1082 else
1083 #endif // __WXGTK__
1084 *borderTop = 15;
1085 (void)box;
1086 *borderOther = 5;
1087 }
1088
1089 void wxStaticBoxSizer::RecalcSizes()
1090 {
1091 int top_border, other_border;
1092 GetStaticBoxBorders(m_staticBox, &top_border, &other_border);
1093
1094 m_staticBox->SetSize( m_position.x, m_position.y, m_size.x, m_size.y );
1095
1096 wxPoint old_pos( m_position );
1097 m_position.x += other_border;
1098 m_position.y += top_border;
1099 wxSize old_size( m_size );
1100 m_size.x -= 2*other_border;
1101 m_size.y -= top_border + other_border;
1102
1103 wxBoxSizer::RecalcSizes();
1104
1105 m_position = old_pos;
1106 m_size = old_size;
1107 }
1108
1109 wxSize wxStaticBoxSizer::CalcMin()
1110 {
1111 int top_border, other_border;
1112 GetStaticBoxBorders(m_staticBox, &top_border, &other_border);
1113
1114 wxSize ret( wxBoxSizer::CalcMin() );
1115 ret.x += 2*other_border;
1116 ret.y += other_border + top_border;
1117
1118 return ret;
1119 }
1120
1121 #endif // wxUSE_STATBOX
1122
1123 //---------------------------------------------------------------------------
1124 // wxNotebookSizer
1125 //---------------------------------------------------------------------------
1126
1127 #if wxUSE_NOTEBOOK
1128
1129 wxNotebookSizer::wxNotebookSizer( wxNotebook *nb )
1130 {
1131 wxASSERT_MSG( nb, wxT("wxNotebookSizer needs a notebook") );
1132
1133 m_notebook = nb;
1134 }
1135
1136 void wxNotebookSizer::RecalcSizes()
1137 {
1138 m_notebook->SetSize( m_position.x, m_position.y, m_size.x, m_size.y );
1139 }
1140
1141 wxSize wxNotebookSizer::CalcMin()
1142 {
1143 wxSize sizeBorder = m_notebook->CalcSizeFromPage(wxSize(0, 0));
1144
1145 sizeBorder.x += 5;
1146 sizeBorder.y += 5;
1147
1148 if (m_notebook->GetChildren().GetCount() == 0)
1149 {
1150 return wxSize(sizeBorder.x + 10, sizeBorder.y + 10);
1151 }
1152
1153 int maxX = 0;
1154 int maxY = 0;
1155
1156 wxWindowList::Node *node = m_notebook->GetChildren().GetFirst();
1157 while (node)
1158 {
1159 wxWindow *item = node->GetData();
1160 wxSizer *itemsizer = item->GetSizer();
1161
1162 if (itemsizer)
1163 {
1164 wxSize subsize( itemsizer->CalcMin() );
1165
1166 if (subsize.x > maxX)
1167 maxX = subsize.x;
1168 if (subsize.y > maxY)
1169 maxY = subsize.y;
1170 }
1171
1172 node = node->GetNext();
1173 }
1174
1175 return wxSize( maxX, maxY ) + sizeBorder;
1176 }
1177
1178 #endif // wxUSE_NOTEBOOK
1179
1180 // vi:sts=4:sw=4:et