STL-ification patch for wxMSW and wxGTK.
[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, contributions by
5 // Dirk Holtwick, Ron Lee
6 // Modified by: Ron Lee
7 // Created:
8 // RCS-ID: $Id$
9 // Copyright: (c) Robin Dunn, Robert Roebling
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifdef __GNUG__
14 #pragma implementation "sizer.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #include "wx/sizer.h"
25 #include "wx/utils.h"
26 #include "wx/statbox.h"
27 #include "wx/notebook.h"
28 #include <wx/listimpl.cpp>
29
30 //---------------------------------------------------------------------------
31
32 IMPLEMENT_CLASS(wxSizerItem, wxObject)
33 IMPLEMENT_CLASS(wxSizer, wxObject)
34 IMPLEMENT_CLASS(wxGridSizer, wxSizer)
35 IMPLEMENT_CLASS(wxFlexGridSizer, wxGridSizer)
36 IMPLEMENT_CLASS(wxBoxSizer, wxSizer)
37 #if wxUSE_STATBOX
38 IMPLEMENT_CLASS(wxStaticBoxSizer, wxBoxSizer)
39 #endif
40 #if wxUSE_NOTEBOOK
41 IMPLEMENT_CLASS(wxNotebookSizer, wxSizer)
42 #endif
43
44 WX_DEFINE_EXPORTED_LIST( wxSizerItemList );
45
46
47 //---------------------------------------------------------------------------
48 // wxSizerItem
49 //---------------------------------------------------------------------------
50
51 wxSizerItem::wxSizerItem( int width, int height, int proportion, int flag, int border, wxObject* userData )
52 : m_window( NULL )
53 , m_sizer( NULL )
54 , m_size( wxSize( width, height ) ) // size is set directly
55 , m_minSize( m_size ) // minimal size is the initial size
56 , m_proportion( proportion )
57 , m_border( border )
58 , m_flag( flag )
59 , m_show( true )
60 , m_userData( userData )
61 {
62 SetRatio( m_size );
63 }
64
65 wxSizerItem::wxSizerItem( wxWindow *window, int proportion, int flag, int border, wxObject* userData )
66 : m_window( window )
67 , m_sizer( NULL )
68 , m_minSize( window->GetSize() ) // minimal size is the initial size
69 , m_proportion( proportion )
70 , m_border( border )
71 , m_flag( flag )
72 , m_show( true )
73 , m_userData( userData )
74 {
75 // aspect ratio calculated from initial size
76 SetRatio( m_minSize );
77
78 // m_size is calculated later
79 }
80
81 wxSizerItem::wxSizerItem( wxSizer *sizer, int proportion, int flag, int border, wxObject* userData )
82 : m_window( NULL )
83 , m_sizer( sizer )
84 , m_proportion( proportion )
85 , m_border( border )
86 , m_flag( flag )
87 , m_show( true )
88 , m_ratio( 0.0 )
89 , m_userData( userData )
90 {
91 // m_minSize is calculated later
92 // m_size is calculated later
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() const
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->GetMinSize();
132
133 // if we have to preserve aspect ratio _AND_ this is
134 // the first-time calculation, consider ret to be initial size
135 if ((m_flag & wxSHAPED) && !m_ratio)
136 SetRatio(ret);
137 }
138 else
139 {
140 if ( IsWindow() && (m_flag & wxADJUST_MINSIZE) )
141 {
142 // By user request, keep the minimal size for this item
143 // in sync with the largest of BestSize and any user supplied
144 // minimum size hint. Useful in cases where the item is
145 // changeable -- static text labels, etc.
146 m_minSize = m_window->GetAdjustedBestSize();
147 }
148
149 ret = m_minSize;
150 }
151
152 if (m_flag & wxWEST)
153 ret.x += m_border;
154 if (m_flag & wxEAST)
155 ret.x += m_border;
156 if (m_flag & wxNORTH)
157 ret.y += m_border;
158 if (m_flag & wxSOUTH)
159 ret.y += m_border;
160
161 return ret;
162 }
163
164 void wxSizerItem::SetDimension( wxPoint pos, wxSize size )
165 {
166 if (m_flag & wxSHAPED)
167 {
168 // adjust aspect ratio
169 int rwidth = (int) (size.y * m_ratio);
170 if (rwidth > size.x)
171 {
172 // fit horizontally
173 int rheight = (int) (size.x / m_ratio);
174 // add vertical space
175 if (m_flag & wxALIGN_CENTER_VERTICAL)
176 pos.y += (size.y - rheight) / 2;
177 else if (m_flag & wxALIGN_BOTTOM)
178 pos.y += (size.y - rheight);
179 // use reduced dimensions
180 size.y =rheight;
181 }
182 else if (rwidth < size.x)
183 {
184 // add horizontal space
185 if (m_flag & wxALIGN_CENTER_HORIZONTAL)
186 pos.x += (size.x - rwidth) / 2;
187 else if (m_flag & wxALIGN_RIGHT)
188 pos.x += (size.x - rwidth);
189 size.x = rwidth;
190 }
191 }
192
193 // This is what GetPosition() returns. Since we calculate
194 // borders afterwards, GetPosition() will be the left/top
195 // corner of the surrounding border.
196 m_pos = pos;
197
198 if (m_flag & wxWEST)
199 {
200 pos.x += m_border;
201 size.x -= m_border;
202 }
203 if (m_flag & wxEAST)
204 {
205 size.x -= m_border;
206 }
207 if (m_flag & wxNORTH)
208 {
209 pos.y += m_border;
210 size.y -= m_border;
211 }
212 if (m_flag & wxSOUTH)
213 {
214 size.y -= m_border;
215 }
216
217 if (IsSizer())
218 m_sizer->SetDimension( pos.x, pos.y, size.x, size.y );
219
220 if (IsWindow())
221 m_window->SetSize( pos.x, pos.y, size.x, size.y, wxSIZE_ALLOW_MINUS_ONE );
222
223 m_size = size;
224 }
225
226 void wxSizerItem::DeleteWindows()
227 {
228 if (m_window)
229 m_window->Destroy();
230
231 if (m_sizer)
232 m_sizer->DeleteWindows();
233 }
234
235 bool wxSizerItem::IsWindow() const
236 {
237 return (m_window != NULL);
238 }
239
240 bool wxSizerItem::IsSizer() const
241 {
242 return (m_sizer != NULL);
243 }
244
245 bool wxSizerItem::IsSpacer() const
246 {
247 return (m_window == NULL) && (m_sizer == NULL);
248 }
249
250 void wxSizerItem::Show( bool show )
251 {
252 m_show = show;
253
254 if( IsWindow() )
255 m_window->Show( show );
256 else if( IsSizer() )
257 m_sizer->ShowItems( show );
258
259 // ... nothing else to do to hide/show spacers
260 }
261
262 void wxSizerItem::SetOption( int option )
263 {
264 SetProportion( option );
265 }
266
267 int wxSizerItem::GetOption() const
268 {
269 return GetProportion();
270 }
271
272
273 //---------------------------------------------------------------------------
274 // wxSizer
275 //---------------------------------------------------------------------------
276
277 wxSizer::wxSizer()
278 : m_minSize( wxSize( 0, 0 ) )
279 {
280 }
281
282 wxSizer::~wxSizer()
283 {
284 WX_CLEAR_LIST(wxSizerItemList, m_children);
285 }
286
287 void wxSizer::Add( wxWindow *window, int proportion, int flag, int border, wxObject* userData )
288 {
289 m_children.Append( new wxSizerItem( window, proportion, flag, border, userData ) );
290 window->SetContainingSizer( this );
291 }
292
293 void wxSizer::Add( wxSizer *sizer, int proportion, int flag, int border, wxObject* userData )
294 {
295 m_children.Append( new wxSizerItem( sizer, proportion, flag, border, userData ) );
296 }
297
298 void wxSizer::Add( int width, int height, int proportion, int flag, int border, wxObject* userData )
299 {
300 m_children.Append( new wxSizerItem( width, height, proportion, flag, border, userData ) );
301 }
302
303 void wxSizer::Add( wxSizerItem *item )
304 {
305 m_children.Append( item );
306
307 if( item->GetWindow() )
308 item->GetWindow()->SetContainingSizer( this );
309 }
310
311 void wxSizer::Prepend( wxWindow *window, int proportion, int flag, int border, wxObject* userData )
312 {
313 m_children.Insert( new wxSizerItem( window, proportion, flag, border, userData ) );
314 window->SetContainingSizer( this );
315 }
316
317 void wxSizer::Prepend( wxSizer *sizer, int proportion, int flag, int border, wxObject* userData )
318 {
319 m_children.Insert( new wxSizerItem( sizer, proportion, flag, border, userData ) );
320 }
321
322 void wxSizer::Prepend( int width, int height, int proportion, int flag, int border, wxObject* userData )
323 {
324 m_children.Insert( new wxSizerItem( width, height, proportion, flag, border, userData ) );
325 }
326
327 void wxSizer::Prepend( wxSizerItem *item )
328 {
329 m_children.Insert( item );
330
331 if( item->GetWindow() )
332 item->GetWindow()->SetContainingSizer( this );
333 }
334
335 void wxSizer::Insert( size_t index,
336 wxWindow *window,
337 int proportion,
338 int flag,
339 int border,
340 wxObject* userData )
341 {
342 m_children.Insert( index,
343 new wxSizerItem( window, proportion, flag, border, userData ) );
344 window->SetContainingSizer( this );
345 }
346
347 void wxSizer::Insert( size_t index,
348 wxSizer *sizer,
349 int proportion,
350 int flag,
351 int border,
352 wxObject* userData )
353 {
354 m_children.Insert( index,
355 new wxSizerItem( sizer, proportion, flag, border, userData ) );
356 }
357
358 void wxSizer::Insert( size_t index,
359 int width,
360 int height,
361 int proportion,
362 int flag,
363 int border,
364 wxObject* userData )
365 {
366 m_children.Insert( index,
367 new wxSizerItem( width, height, proportion, flag, border, userData ) );
368 }
369
370 void wxSizer::Insert( size_t index, wxSizerItem *item )
371 {
372 m_children.Insert( index, item );
373
374 if( item->GetWindow() )
375 item->GetWindow()->SetContainingSizer( this );
376 }
377
378 bool wxSizer::Remove( wxWindow *window )
379 {
380 return Detach( window );
381 }
382
383 bool wxSizer::Remove( wxSizer *sizer )
384 {
385 wxASSERT_MSG( sizer, _T("Removing NULL sizer") );
386
387 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
388 while (node)
389 {
390 wxSizerItem *item = node->GetData();
391
392 if (item->GetSizer() == sizer)
393 {
394 delete item;
395 m_children.Erase( node );
396 return true;
397 }
398
399 node = node->GetNext();
400 }
401
402 return false;
403 }
404
405 bool wxSizer::Remove( int index )
406 {
407 wxCHECK_MSG( index >= 0 && (size_t)index < m_children.GetCount(),
408 false,
409 _T("Remove index is out of range") );
410
411 wxSizerItemList::compatibility_iterator node = m_children.Item( index );
412
413 wxCHECK_MSG( node, false, _T("Failed to find child node") );
414
415 wxSizerItem *item = node->GetData();
416
417 if( item->IsWindow() )
418 item->GetWindow()->SetContainingSizer( NULL );
419
420 delete item;
421 m_children.Erase( node );
422 return true;
423 }
424
425 bool wxSizer::Detach( wxSizer *sizer )
426 {
427 wxASSERT_MSG( sizer, _T("Detaching NULL sizer") );
428
429 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
430 while (node)
431 {
432 wxSizerItem *item = node->GetData();
433
434 if (item->GetSizer() == sizer)
435 {
436 item->DetachSizer();
437 m_children.Erase( node );
438 return true;
439 }
440 node = node->GetNext();
441 }
442
443 return false;
444 }
445
446 bool wxSizer::Detach( wxWindow *window )
447 {
448 wxASSERT_MSG( window, _T("Detaching NULL window") );
449
450 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
451 while (node)
452 {
453 wxSizerItem *item = node->GetData();
454
455 if (item->GetWindow() == window)
456 {
457 item->GetWindow()->SetContainingSizer( NULL );
458 m_children.Erase( node );
459 return true;
460 }
461 node = node->GetNext();
462 }
463
464 return false;
465 }
466
467 bool wxSizer::Detach( int index )
468 {
469 wxCHECK_MSG( index >= 0 && (size_t)index < m_children.GetCount(),
470 false,
471 _T("Detach index is out of range") );
472
473 wxSizerItemList::compatibility_iterator node = m_children.Item( index );
474
475 wxCHECK_MSG( node, false, _T("Failed to find child node") );
476
477 wxSizerItem *item = node->GetData();
478
479 if( item->IsSizer() )
480 item->DetachSizer();
481 else if( item->IsWindow() )
482 item->GetWindow()->SetContainingSizer( NULL );
483
484 m_children.Erase( node );
485 return true;
486 }
487
488 void wxSizer::Clear( bool delete_windows )
489 {
490 // First clear the ContainingSizer pointers
491 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
492 while (node)
493 {
494 wxSizerItem *item = node->GetData();
495
496 if (item->IsWindow())
497 item->GetWindow()->SetContainingSizer( NULL );
498 node = node->GetNext();
499 }
500
501 // Destroy the windows if needed
502 if (delete_windows)
503 DeleteWindows();
504
505 // Now empty the list
506 WX_CLEAR_LIST(wxSizerItemList, m_children);
507 }
508
509 void wxSizer::DeleteWindows()
510 {
511 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
512 while (node)
513 {
514 wxSizerItem *item = node->GetData();
515
516 item->DeleteWindows();
517 node = node->GetNext();
518 }
519 }
520
521 wxSize wxSizer::Fit( wxWindow *window )
522 {
523 wxSize size;
524 if (window->IsTopLevel())
525 size = FitSize( window );
526 else
527 size = GetMinWindowSize( window );
528
529 window->SetSize( size );
530
531 return size;
532 }
533
534 void wxSizer::FitInside( wxWindow *window )
535 {
536 wxSize size;
537 if (window->IsTopLevel())
538 size = VirtualFitSize( window );
539 else
540 size = GetMinClientSize( window );
541
542 window->SetVirtualSize( size );
543 }
544
545 void wxSizer::Layout()
546 {
547 CalcMin();
548 RecalcSizes();
549 }
550
551 void wxSizer::SetSizeHints( wxWindow *window )
552 {
553 // Preserve the window's max size hints, but set the
554 // lower bound according to the sizer calculations.
555
556 wxSize size = Fit( window );
557
558 window->SetSizeHints( size.x,
559 size.y,
560 window->GetMaxWidth(),
561 window->GetMaxHeight() );
562 }
563
564 void wxSizer::SetVirtualSizeHints( wxWindow *window )
565 {
566 // Preserve the window's max size hints, but set the
567 // lower bound according to the sizer calculations.
568
569 FitInside( window );
570 wxSize size( window->GetVirtualSize() );
571 window->SetVirtualSizeHints( size.x,
572 size.y,
573 window->GetMaxWidth(),
574 window->GetMaxHeight() );
575 }
576
577 wxSize wxSizer::GetMaxWindowSize( wxWindow *window ) const
578 {
579 return window->GetMaxSize();
580 }
581
582 wxSize wxSizer::GetMinWindowSize( wxWindow *window )
583 {
584 wxSize minSize( GetMinSize() );
585 wxSize size( window->GetSize() );
586 wxSize client_size( window->GetClientSize() );
587
588 return wxSize( minSize.x+size.x-client_size.x,
589 minSize.y+size.y-client_size.y );
590 }
591
592 // Return a window size that will fit within the screens dimensions
593 wxSize wxSizer::FitSize( wxWindow *window )
594 {
595 wxSize size = GetMinWindowSize( window );
596 wxSize sizeMax = GetMaxWindowSize( window );
597
598 // Limit the size if sizeMax != wxDefaultSize
599
600 if ( size.x > sizeMax.x && sizeMax.x != -1 )
601 size.x = sizeMax.x;
602 if ( size.y > sizeMax.y && sizeMax.y != -1 )
603 size.y = sizeMax.y;
604
605 return size;
606 }
607
608 wxSize wxSizer::GetMaxClientSize( wxWindow *window ) const
609 {
610 wxSize maxSize( window->GetMaxSize() );
611
612 if( maxSize != wxDefaultSize )
613 {
614 wxSize size( window->GetSize() );
615 wxSize client_size( window->GetClientSize() );
616
617 return wxSize( maxSize.x + client_size.x - size.x,
618 maxSize.y + client_size.y - size.y );
619 }
620 else
621 return wxDefaultSize;
622 }
623
624 wxSize wxSizer::GetMinClientSize( wxWindow *WXUNUSED(window) )
625 {
626 return GetMinSize(); // Already returns client size.
627 }
628
629 wxSize wxSizer::VirtualFitSize( wxWindow *window )
630 {
631 wxSize size = GetMinClientSize( window );
632 wxSize sizeMax = GetMaxClientSize( window );
633
634 // Limit the size if sizeMax != wxDefaultSize
635
636 if ( size.x > sizeMax.x && sizeMax.x != -1 )
637 size.x = sizeMax.x;
638 if ( size.y > sizeMax.y && sizeMax.y != -1 )
639 size.y = sizeMax.y;
640
641 return size;
642 }
643
644 void wxSizer::SetDimension( int x, int y, int width, int height )
645 {
646 m_position.x = x;
647 m_position.y = y;
648 m_size.x = width;
649 m_size.y = height;
650 Layout();
651 }
652
653 wxSize wxSizer::GetMinSize()
654 {
655 wxSize ret( CalcMin() );
656 if (ret.x < m_minSize.x) ret.x = m_minSize.x;
657 if (ret.y < m_minSize.y) ret.y = m_minSize.y;
658 return ret;
659 }
660
661 void wxSizer::DoSetMinSize( int width, int height )
662 {
663 m_minSize.x = width;
664 m_minSize.y = height;
665 }
666
667 bool wxSizer::DoSetItemMinSize( wxWindow *window, int width, int height )
668 {
669 wxASSERT_MSG( window, _T("SetMinSize for NULL window") );
670
671 // Is it our immediate child?
672
673 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
674 while (node)
675 {
676 wxSizerItem *item = node->GetData();
677
678 if (item->GetWindow() == window)
679 {
680 item->SetInitSize( width, height );
681 return true;
682 }
683 node = node->GetNext();
684 }
685
686 // No? Search any subsizers we own then
687
688 node = m_children.GetFirst();
689 while (node)
690 {
691 wxSizerItem *item = node->GetData();
692
693 if ( item->GetSizer() &&
694 item->GetSizer()->DoSetItemMinSize( window, width, height ) )
695 {
696 // A child sizer found the requested windw, exit.
697 return true;
698 }
699 node = node->GetNext();
700 }
701
702 return false;
703 }
704
705 bool wxSizer::DoSetItemMinSize( wxSizer *sizer, int width, int height )
706 {
707 wxASSERT_MSG( sizer, _T("SetMinSize for NULL sizer") );
708
709 // Is it our immediate child?
710
711 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
712 while (node)
713 {
714 wxSizerItem *item = node->GetData();
715
716 if (item->GetSizer() == sizer)
717 {
718 item->GetSizer()->DoSetMinSize( width, height );
719 return true;
720 }
721 node = node->GetNext();
722 }
723
724 // No? Search any subsizers we own then
725
726 node = m_children.GetFirst();
727 while (node)
728 {
729 wxSizerItem *item = node->GetData();
730
731 if ( item->GetSizer() &&
732 item->GetSizer()->DoSetItemMinSize( sizer, width, height ) )
733 {
734 // A child found the requested sizer, exit.
735 return true;
736 }
737 node = node->GetNext();
738 }
739
740 return false;
741 }
742
743 bool wxSizer::DoSetItemMinSize( size_t index, int width, int height )
744 {
745 wxSizerItemList::compatibility_iterator node = m_children.Item( index );
746
747 wxCHECK_MSG( node, false, _T("Failed to find child node") );
748
749 wxSizerItem *item = node->GetData();
750
751 if (item->GetSizer())
752 {
753 // Sizers contains the minimal size in them, if not calculated ...
754 item->GetSizer()->DoSetMinSize( width, height );
755 }
756 else
757 {
758 // ... but the minimal size of spacers and windows in stored in them
759 item->SetInitSize( width, height );
760 }
761
762 return true;
763 }
764
765 void wxSizer::Show( wxWindow *window, bool show )
766 {
767 wxASSERT_MSG( window, _T("Show for NULL window") );
768
769 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
770 while (node)
771 {
772 wxSizerItem *item = node->GetData();
773
774 if (item->GetWindow() == window)
775 {
776 item->Show( show );
777 break;
778 }
779 node = node->GetNext();
780 }
781 }
782
783 void wxSizer::Show( wxSizer *sizer, bool show )
784 {
785 wxASSERT_MSG( sizer, _T("Show for NULL sizer") );
786
787 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
788 while (node)
789 {
790 wxSizerItem *item = node->GetData();
791
792 if (item->GetSizer() == sizer)
793 {
794 item->Show( show );
795 break;
796 }
797 node = node->GetNext();
798 }
799 }
800
801 void wxSizer::Show( size_t index, bool show )
802 {
803 wxCHECK_RET( index < m_children.GetCount(),
804 _T("Show index is out of range") );
805
806 m_children.Item( index )->GetData()->Show( show );
807 }
808
809 void wxSizer::ShowItems( bool show )
810 {
811 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
812 while (node)
813 {
814 node->GetData()->Show( show );
815 node = node->GetNext();
816 }
817 }
818
819 bool wxSizer::IsShown( wxWindow *window ) const
820 {
821 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
822 while (node)
823 {
824 wxSizerItem *item = node->GetData();
825
826 if (item->GetWindow() == window)
827 {
828 return item->IsShown();
829 }
830 node = node->GetNext();
831 }
832
833 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
834
835 return false;
836 }
837
838 bool wxSizer::IsShown( wxSizer *sizer ) const
839 {
840 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
841 while (node)
842 {
843 wxSizerItem *item = node->GetData();
844
845 if (item->GetSizer() == sizer)
846 {
847 return item->IsShown();
848 }
849 node = node->GetNext();
850 }
851
852 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
853
854 return false;
855 }
856
857 bool wxSizer::IsShown( size_t index ) const
858 {
859 wxCHECK_MSG( index < m_children.GetCount(),
860 false,
861 _T("IsShown index is out of range") );
862
863 return m_children.Item( index )->GetData()->IsShown();
864 }
865
866
867 //---------------------------------------------------------------------------
868 // wxGridSizer
869 //---------------------------------------------------------------------------
870
871 wxGridSizer::wxGridSizer( int rows, int cols, int vgap, int hgap )
872 : m_rows( rows )
873 , m_cols( cols )
874 , m_vgap( vgap )
875 , m_hgap( hgap )
876 {
877 }
878
879 wxGridSizer::wxGridSizer( int cols, int vgap, int hgap )
880 : m_rows( 0 )
881 , m_cols( cols )
882 , m_vgap( vgap )
883 , m_hgap( hgap )
884 {
885 }
886
887 int wxGridSizer::CalcRowsCols(int& nrows, int& ncols) const
888 {
889 int nitems = m_children.GetCount();
890 if ( nitems)
891 {
892 if ( m_cols )
893 {
894 ncols = m_cols;
895 nrows = (nitems + m_cols - 1) / m_cols;
896 }
897 else if ( m_rows )
898 {
899 ncols = (nitems + m_rows - 1) / m_rows;
900 nrows = m_rows;
901 }
902 else // 0 columns, 0 rows?
903 {
904 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
905
906 nrows = ncols = 0;
907 }
908 }
909
910 return nitems;
911 }
912
913 void wxGridSizer::RecalcSizes()
914 {
915 int nitems, nrows, ncols;
916 if ( (nitems = CalcRowsCols(nrows, ncols)) == 0 )
917 return;
918
919 wxSize sz( GetSize() );
920 wxPoint pt( GetPosition() );
921
922 int w = (sz.x - (ncols - 1) * m_hgap) / ncols;
923 int h = (sz.y - (nrows - 1) * m_vgap) / nrows;
924
925 int x = pt.x;
926 for (int c = 0; c < ncols; c++)
927 {
928 int y = pt.y;
929 for (int r = 0; r < nrows; r++)
930 {
931 int i = r * ncols + c;
932 if (i < nitems)
933 {
934 wxSizerItemList::compatibility_iterator node = m_children.Item( i );
935
936 wxASSERT_MSG( node, _T("Failed to find SizerItemList node") );
937
938 SetItemBounds( node->GetData(), x, y, w, h);
939 }
940 y = y + h + m_vgap;
941 }
942 x = x + w + m_hgap;
943 }
944 }
945
946 wxSize wxGridSizer::CalcMin()
947 {
948 int nitems, nrows, ncols;
949 if ( (nitems = CalcRowsCols(nrows, ncols)) == 0 )
950 return wxSize(10, 10);
951
952 // Find the max width and height for any component
953 int w = 0;
954 int h = 0;
955
956 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
957 while (node)
958 {
959 wxSizerItem *item = node->GetData();
960 wxSize sz( item->CalcMin() );
961
962 w = wxMax( w, sz.x );
963 h = wxMax( h, sz.y );
964
965 node = node->GetNext();
966 }
967
968 return wxSize( ncols * w + (ncols-1) * m_hgap,
969 nrows * h + (nrows-1) * m_vgap );
970 }
971
972 void wxGridSizer::SetItemBounds( wxSizerItem *item, int x, int y, int w, int h )
973 {
974 wxPoint pt( x,y );
975 wxSize sz( item->CalcMin() );
976 int flag = item->GetFlag();
977
978 if ((flag & wxEXPAND) || (flag & wxSHAPED))
979 {
980 sz = wxSize(w, h);
981 }
982 else
983 {
984 if (flag & wxALIGN_CENTER_HORIZONTAL)
985 {
986 pt.x = x + (w - sz.x) / 2;
987 }
988 else if (flag & wxALIGN_RIGHT)
989 {
990 pt.x = x + (w - sz.x);
991 }
992
993 if (flag & wxALIGN_CENTER_VERTICAL)
994 {
995 pt.y = y + (h - sz.y) / 2;
996 }
997 else if (flag & wxALIGN_BOTTOM)
998 {
999 pt.y = y + (h - sz.y);
1000 }
1001 }
1002
1003 item->SetDimension(pt, sz);
1004 }
1005
1006 //---------------------------------------------------------------------------
1007 // wxFlexGridSizer
1008 //---------------------------------------------------------------------------
1009
1010 wxFlexGridSizer::wxFlexGridSizer( int rows, int cols, int vgap, int hgap )
1011 : wxGridSizer( rows, cols, vgap, hgap ),
1012 m_flexDirection(wxBOTH),
1013 m_growMode(wxFLEX_GROWMODE_SPECIFIED)
1014 {
1015 }
1016
1017 wxFlexGridSizer::wxFlexGridSizer( int cols, int vgap, int hgap )
1018 : wxGridSizer( cols, vgap, hgap ),
1019 m_flexDirection(wxBOTH),
1020 m_growMode(wxFLEX_GROWMODE_SPECIFIED)
1021 {
1022 }
1023
1024 wxFlexGridSizer::~wxFlexGridSizer()
1025 {
1026 }
1027
1028 void wxFlexGridSizer::RecalcSizes()
1029 {
1030 int nitems, nrows, ncols;
1031 if ( (nitems = CalcRowsCols(nrows, ncols)) == 0 )
1032 return;
1033
1034 wxSize sz( GetSize() );
1035 wxSize minsz( CalcMin() );
1036 wxPoint pt( GetPosition() );
1037
1038 // what to do with the rows? by default, resize them proportionally
1039 if ( sz.y > minsz.y && ( (m_flexDirection & wxVERTICAL) || (m_growMode == wxFLEX_GROWMODE_SPECIFIED) ) )
1040 {
1041 int sum_proportions = 0;
1042 int growable_space = 0;
1043 int num = 0;
1044 size_t idx;
1045 for (idx = 0; idx < m_growableRows.GetCount(); idx++)
1046 {
1047 // Since the number of rows/columns can change as items are inserted/deleted, we need
1048 // to verify at runtime that the requested growable rows/columns are still valid.
1049 if (m_growableRows[idx] >= nrows)
1050 continue;
1051 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1052 // This causes the row/column to be hidden completely.
1053 if (m_rowHeights[ m_growableRows[idx] ] == -1)
1054 continue;
1055 sum_proportions += m_growableRowsProportions[idx];
1056 growable_space += m_rowHeights[ m_growableRows[idx] ];
1057 num++;
1058 }
1059
1060 if (num > 0)
1061 {
1062 for (idx = 0; idx < m_growableRows.GetCount(); idx++)
1063 {
1064 if (m_growableRows[idx] >= nrows )
1065 continue;
1066 if (m_rowHeights[ m_growableRows[idx] ] == -1)
1067 m_rowHeights[ m_growableRows[idx] ] = 0;
1068 else
1069 {
1070 int delta = (sz.y - minsz.y);
1071 if (sum_proportions == 0)
1072 delta = (delta/num) + m_rowHeights[ m_growableRows[idx] ];
1073 else
1074 delta = ((delta+growable_space)*m_growableRowsProportions[idx]) / sum_proportions;
1075 m_rowHeights[ m_growableRows[idx] ] = delta;
1076 }
1077 }
1078 }
1079 }
1080 else if ( (m_growMode == wxFLEX_GROWMODE_ALL) && (sz.y > minsz.y) )
1081 {
1082 // rounding problem?
1083 for ( int row = 0; row < nrows; ++row )
1084 m_rowHeights[ row ] = sz.y / nrows;
1085 }
1086
1087 // the same logic as above but for the columns
1088 if ( sz.x > minsz.x && ( (m_flexDirection & wxHORIZONTAL) || (m_growMode == wxFLEX_GROWMODE_SPECIFIED) ) )
1089 {
1090 int sum_proportions = 0;
1091 int growable_space = 0;
1092 int num = 0;
1093 size_t idx;
1094 for (idx = 0; idx < m_growableCols.GetCount(); idx++)
1095 {
1096 // Since the number of rows/columns can change as items are inserted/deleted, we need
1097 // to verify at runtime that the requested growable rows/columns are still valid.
1098 if (m_growableCols[idx] >= ncols)
1099 continue;
1100 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1101 // This causes the column to be hidden completely.
1102 if (m_colWidths[ m_growableCols[idx] ] == -1)
1103 continue;
1104 sum_proportions += m_growableColsProportions[idx];
1105 // wtb 5/12/02 bugfix - was m_ColWidths[idx]!!
1106 growable_space += m_colWidths[ m_growableCols[idx] ];
1107 num++;
1108 }
1109
1110 if (num > 0)
1111 {
1112 for (idx = 0; idx < m_growableCols.GetCount(); idx++)
1113 {
1114 if (m_growableCols[idx] >= ncols )
1115 continue;
1116 if (m_colWidths[ m_growableCols[idx] ] == -1)
1117 m_colWidths[ m_growableCols[idx] ] = 0;
1118 else
1119 {
1120 int delta = (sz.x - minsz.x);
1121 if (sum_proportions == 0)
1122 delta = (delta/num) + m_colWidths[ m_growableCols[idx] ];
1123 else
1124 delta = ((delta+growable_space)*m_growableColsProportions[idx])/sum_proportions;
1125 m_colWidths[ m_growableCols[idx] ] = delta;
1126 }
1127 }
1128 }
1129 }
1130 else if ( (m_growMode == wxFLEX_GROWMODE_ALL) && (sz.x > minsz.x) )
1131 {
1132 for ( int col=0; col < ncols; ++col )
1133 m_colWidths[ col ] = sz.x / ncols;
1134 }
1135
1136 sz = wxSize( pt.x + sz.x, pt.y + sz.y );
1137
1138 int x = pt.x;
1139 for (int c = 0; c < ncols; c++)
1140 {
1141 int y = pt.y;
1142 for (int r = 0; r < nrows; r++)
1143 {
1144 int i = r * ncols + c;
1145 if (i < nitems)
1146 {
1147 wxSizerItemList::compatibility_iterator node = m_children.Item( i );
1148
1149 wxASSERT_MSG( node, _T("Failed to find node") );
1150
1151 int w = wxMax( 0, wxMin( m_colWidths[c], sz.x - x ) );
1152 int h = wxMax( 0, wxMin( m_rowHeights[r], sz.y - y ) );
1153
1154 SetItemBounds( node->GetData(), x, y, w, h);
1155 }
1156 y = y + m_rowHeights[r] + m_vgap;
1157 }
1158 x = x + m_colWidths[c] + m_hgap;
1159 }
1160 }
1161
1162 wxSize wxFlexGridSizer::CalcMin()
1163 {
1164 int nrows,
1165 ncols;
1166 size_t i, s;
1167
1168 // Number of rows/columns can change as items are added or removed.
1169 if ( !CalcRowsCols(nrows, ncols) )
1170 return wxSize(10, 10);
1171
1172 m_rowHeights.SetCount(nrows);
1173 m_colWidths.SetCount(ncols);
1174
1175 // We have to recalcuate the sizes in case an item has wxADJUST_MINSIZE, has changed
1176 // minimum size since the previous layout, or has been hidden using wxSizer::Show().
1177 // If all the items in a row/column are hidden, the final dimension of the row/column
1178 // will be -1, indicating that the column itself is hidden.
1179 for( s = m_rowHeights.GetCount(), i = 0; i < s; ++i )
1180 m_rowHeights[ i ] = -1;
1181 for( s = m_colWidths.GetCount(), i = 0; i < s; ++i )
1182 m_colWidths[ i ] = -1;
1183
1184 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
1185
1186 i = 0;
1187 while (node)
1188 {
1189 wxSizerItem *item = node->GetData();
1190 if ( item->IsShown() )
1191 {
1192 wxSize sz( item->CalcMin() );
1193 int row = i / ncols;
1194 int col = i % ncols;
1195
1196 m_rowHeights[ row ] = wxMax( wxMax( 0, sz.y ), m_rowHeights[ row ] );
1197 m_colWidths[ col ] = wxMax( wxMax( 0, sz.x ), m_colWidths[ col ] );
1198 }
1199
1200 node = node->GetNext();
1201 i++;
1202 }
1203
1204 // the logic above works when we resize flexibly in both directions but
1205 // maybe this is not the case
1206 if ( m_flexDirection != wxBOTH )
1207 {
1208 // select the array corresponding to the direction in which we do *not*
1209 // resize flexibly
1210 wxArrayInt& array = m_flexDirection == wxVERTICAL ? m_colWidths
1211 : m_rowHeights;
1212
1213 const int count = array.GetCount();
1214
1215 // find the largest value in this array
1216 int n, largest = 0;
1217 for ( n = 0; n < count; ++n )
1218 {
1219 if ( array[n] > largest )
1220 largest = array[n];
1221 }
1222
1223 // and now fill it with the largest value
1224 for ( n = 0; n < count; ++n )
1225 {
1226 array[n] = largest;
1227 }
1228 }
1229
1230 // Sum total minimum size, including gaps between rows/columns.
1231 // -1 is used as a magic number meaning empty column.
1232 int width = 0;
1233 for (int col = 0; col < ncols; col++)
1234 if ( m_colWidths[ col ] != -1 )
1235 width += m_colWidths[ col ] + ( col == ncols-1 ? 0 : m_hgap );
1236
1237 int height = 0;
1238 for (int row = 0; row < nrows; row++)
1239 if ( m_rowHeights[ row ] != -1 )
1240 height += m_rowHeights[ row ] + ( row == nrows-1 ? 0 : m_vgap );
1241
1242 return wxSize( width, height );
1243 }
1244
1245 void wxFlexGridSizer::AddGrowableRow( size_t idx, int proportion )
1246 {
1247 m_growableRows.Add( idx );
1248 m_growableRowsProportions.Add( proportion );
1249 }
1250
1251 void wxFlexGridSizer::RemoveGrowableRow( size_t WXUNUSED(idx) )
1252 {
1253 }
1254
1255 void wxFlexGridSizer::AddGrowableCol( size_t idx, int proportion )
1256 {
1257 m_growableCols.Add( idx );
1258 m_growableColsProportions.Add( proportion );
1259 }
1260
1261 void wxFlexGridSizer::RemoveGrowableCol( size_t WXUNUSED(idx) )
1262 {
1263 }
1264
1265 //---------------------------------------------------------------------------
1266 // wxBoxSizer
1267 //---------------------------------------------------------------------------
1268
1269 wxBoxSizer::wxBoxSizer( int orient )
1270 : m_orient( orient )
1271 {
1272 }
1273
1274 void wxBoxSizer::RecalcSizes()
1275 {
1276 if (m_children.GetCount() == 0)
1277 return;
1278
1279 int delta = 0;
1280 int extra = 0;
1281 if (m_stretchable)
1282 {
1283 if (m_orient == wxHORIZONTAL)
1284 {
1285 delta = (m_size.x - m_fixedWidth) / m_stretchable;
1286 extra = (m_size.x - m_fixedWidth) % m_stretchable;
1287 }
1288 else
1289 {
1290 delta = (m_size.y - m_fixedHeight) / m_stretchable;
1291 extra = (m_size.y - m_fixedHeight) % m_stretchable;
1292 }
1293 }
1294
1295 wxPoint pt( m_position );
1296
1297 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
1298 while (node)
1299 {
1300 wxSizerItem *item = node->GetData();
1301
1302 if (item->IsShown())
1303 {
1304 int weight = 1;
1305 if (item->GetProportion())
1306 weight = item->GetProportion();
1307
1308 wxSize size( item->CalcMin() );
1309
1310 if (m_orient == wxVERTICAL)
1311 {
1312 wxCoord height = size.y;
1313 if (item->GetProportion())
1314 {
1315 height = (delta * weight) + extra;
1316 extra = 0; // only the first item will get the remainder as extra size
1317 }
1318
1319 wxPoint child_pos( pt );
1320 wxSize child_size( wxSize( size.x, height) );
1321
1322 if (item->GetFlag() & (wxEXPAND | wxSHAPED))
1323 child_size.x = m_size.x;
1324 else if (item->GetFlag() & wxALIGN_RIGHT)
1325 child_pos.x += m_size.x - size.x;
1326 else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_HORIZONTAL))
1327 // XXX wxCENTER is added for backward compatibility;
1328 // wxALIGN_CENTER should be used in new code
1329 child_pos.x += (m_size.x - size.x) / 2;
1330
1331 item->SetDimension( child_pos, child_size );
1332
1333 pt.y += height;
1334 }
1335 else
1336 {
1337 wxCoord width = size.x;
1338 if (item->GetProportion())
1339 {
1340 width = (delta * weight) + extra;
1341 extra = 0; // only the first item will get the remainder as extra size
1342 }
1343
1344 wxPoint child_pos( pt );
1345 wxSize child_size( wxSize(width, size.y) );
1346
1347 if (item->GetFlag() & (wxEXPAND | wxSHAPED))
1348 child_size.y = m_size.y;
1349 else if (item->GetFlag() & wxALIGN_BOTTOM)
1350 child_pos.y += m_size.y - size.y;
1351 else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_VERTICAL))
1352 // XXX wxCENTER is added for backward compatibility;
1353 // wxALIGN_CENTER should be used in new code
1354 child_pos.y += (m_size.y - size.y) / 2;
1355
1356 item->SetDimension( child_pos, child_size );
1357
1358 pt.x += width;
1359 }
1360 }
1361
1362 node = node->GetNext();
1363 }
1364 }
1365
1366 wxSize wxBoxSizer::CalcMin()
1367 {
1368 if (m_children.GetCount() == 0)
1369 return wxSize(10,10);
1370
1371 m_stretchable = 0;
1372 m_minWidth = 0;
1373 m_minHeight = 0;
1374 m_fixedWidth = 0;
1375 m_fixedHeight = 0;
1376
1377 // Find how long each stretch unit needs to be
1378 int stretchSize = 1;
1379 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
1380
1381 while (node)
1382 {
1383 wxSizerItem *item = node->GetData();
1384
1385 if (item->IsShown() && item->GetProportion() != 0)
1386 {
1387 int stretch = item->GetProportion();
1388 wxSize size( item->CalcMin() );
1389 int sizePerStretch;
1390 // Integer division rounded up is (a + b - 1) / b
1391 if (m_orient == wxHORIZONTAL)
1392 sizePerStretch = ( size.x + stretch - 1 ) / stretch;
1393 else
1394 sizePerStretch = ( size.y + stretch - 1 ) / stretch;
1395 if (sizePerStretch > stretchSize)
1396 stretchSize = sizePerStretch;
1397 }
1398 node = node->GetNext();
1399 }
1400
1401 // Calculate overall minimum size
1402 node = m_children.GetFirst();
1403 while (node)
1404 {
1405 wxSizerItem *item = node->GetData();
1406
1407 if (item->IsShown())
1408 {
1409 m_stretchable += item->GetProportion();
1410
1411 wxSize size( item->CalcMin() );
1412 if (item->GetProportion() != 0)
1413 {
1414 if (m_orient == wxHORIZONTAL)
1415 size.x = stretchSize * item->GetProportion();
1416 else
1417 size.y = stretchSize * item->GetProportion();
1418 }
1419
1420 if (m_orient == wxHORIZONTAL)
1421 {
1422 m_minWidth += size.x;
1423 m_minHeight = wxMax( m_minHeight, size.y );
1424 }
1425 else
1426 {
1427 m_minHeight += size.y;
1428 m_minWidth = wxMax( m_minWidth, size.x );
1429 }
1430
1431 if (item->GetProportion() == 0)
1432 {
1433 if (m_orient == wxVERTICAL)
1434 {
1435 m_fixedHeight += size.y;
1436 m_fixedWidth = wxMax( m_fixedWidth, size.x );
1437 }
1438 else
1439 {
1440 m_fixedWidth += size.x;
1441 m_fixedHeight = wxMax( m_fixedHeight, size.y );
1442 }
1443 }
1444 }
1445 node = node->GetNext();
1446 }
1447
1448 return wxSize( m_minWidth, m_minHeight );
1449 }
1450
1451 //---------------------------------------------------------------------------
1452 // wxStaticBoxSizer
1453 //---------------------------------------------------------------------------
1454
1455 #if wxUSE_STATBOX
1456
1457 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox *box, int orient )
1458 : wxBoxSizer( orient )
1459 , m_staticBox( box )
1460 {
1461 wxASSERT_MSG( box, wxT("wxStaticBoxSizer needs a static box") );
1462 }
1463
1464 static void GetStaticBoxBorders( wxStaticBox *box,
1465 int *borderTop,
1466 int *borderOther)
1467 {
1468 // this has to be done platform by platform as there is no way to
1469 // guess the thickness of a wxStaticBox border
1470 #ifdef __WXCOCOA__
1471 box->GetBordersForSizer(borderTop,borderOther);
1472 #else // __WXCOCOA__
1473 #ifdef __WXGTK__
1474 if ( box->GetLabel().IsEmpty() )
1475 *borderTop = 5;
1476 else
1477 #endif // __WXGTK__
1478 *borderTop = box->GetCharHeight();
1479
1480 *borderOther = 5;
1481 #endif // __WXCOCOA__
1482 }
1483
1484 void wxStaticBoxSizer::RecalcSizes()
1485 {
1486 int top_border, other_border;
1487 GetStaticBoxBorders(m_staticBox, &top_border, &other_border);
1488
1489 m_staticBox->SetSize( m_position.x, m_position.y, m_size.x, m_size.y );
1490
1491 wxPoint old_pos( m_position );
1492 m_position.x += other_border;
1493 m_position.y += top_border;
1494 wxSize old_size( m_size );
1495 m_size.x -= 2*other_border;
1496 m_size.y -= top_border + other_border;
1497
1498 wxBoxSizer::RecalcSizes();
1499
1500 m_position = old_pos;
1501 m_size = old_size;
1502 }
1503
1504 wxSize wxStaticBoxSizer::CalcMin()
1505 {
1506 int top_border, other_border;
1507 GetStaticBoxBorders(m_staticBox, &top_border, &other_border);
1508
1509 wxSize ret( wxBoxSizer::CalcMin() );
1510 ret.x += 2*other_border;
1511 ret.y += other_border + top_border;
1512
1513 return ret;
1514 }
1515
1516 #endif // wxUSE_STATBOX
1517
1518 //---------------------------------------------------------------------------
1519 // wxNotebookSizer
1520 //---------------------------------------------------------------------------
1521
1522 #if wxUSE_NOTEBOOK
1523
1524 wxNotebookSizer::wxNotebookSizer( wxNotebook *nb )
1525 : m_notebook( nb )
1526 {
1527 wxASSERT_MSG( nb, wxT("wxNotebookSizer needs a notebook") );
1528 }
1529
1530 void wxNotebookSizer::RecalcSizes()
1531 {
1532 m_notebook->SetSize( m_position.x, m_position.y, m_size.x, m_size.y );
1533 }
1534
1535 wxSize wxNotebookSizer::CalcMin()
1536 {
1537 wxSize sizeBorder = m_notebook->CalcSizeFromPage(wxSize(0, 0));
1538
1539 sizeBorder.x += 5;
1540 sizeBorder.y += 5;
1541
1542 if (m_notebook->GetChildren().GetCount() == 0)
1543 {
1544 return wxSize(sizeBorder.x + 10, sizeBorder.y + 10);
1545 }
1546
1547 int maxX = 0;
1548 int maxY = 0;
1549
1550 wxWindowList::compatibility_iterator node = m_notebook->GetChildren().GetFirst();
1551 while (node)
1552 {
1553 wxWindow *item = node->GetData();
1554 wxSizer *itemsizer = item->GetSizer();
1555
1556 if (itemsizer)
1557 {
1558 wxSize subsize( itemsizer->CalcMin() );
1559
1560 if (subsize.x > maxX)
1561 maxX = subsize.x;
1562 if (subsize.y > maxY)
1563 maxY = subsize.y;
1564 }
1565
1566 node = node->GetNext();
1567 }
1568
1569 return wxSize( maxX, maxY ) + sizeBorder;
1570 }
1571
1572 #endif // wxUSE_NOTEBOOK
1573
1574 // vi:sts=4:sw=4:et