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