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