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