]> git.saurik.com Git - wxWidgets.git/blame - src/common/sizer.cpp
added fool-proof test for sizeof(wchar_t) that works on SGI/Irix. This a workaround...
[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 357{
65ba4113
GT
358 wxSize size = FitSize( window );
359 window->SetSize( size );
5279a24d
RR
360}
361
3417c2cd 362void wxSizer::Layout()
c62ac5b6 363{
42b4e99e 364 CalcMin();
c62ac5b6
RR
365 RecalcSizes();
366}
367
3417c2cd 368void wxSizer::SetSizeHints( wxWindow *window )
5279a24d 369{
65ba4113 370 wxSize size = FitSize( window );
5279a24d
RR
371 window->SetSizeHints( size.x, size.y );
372}
373
65ba4113
GT
374wxSize wxSizer::GetMaxWindowSize( wxWindow *window )
375{
376 wxSize sizeMax = wxGetDisplaySize();
377 // make the max size a bit smaller than the screen, a window which takes
378 // the entire screen doesn't look very nice neither
379 sizeMax.x *= 9;
380 sizeMax.x /= 10;
381
382 sizeMax.y *= 9;
383 sizeMax.y /= 10;
384
385 return sizeMax;
386}
387
3417c2cd 388wxSize wxSizer::GetMinWindowSize( wxWindow *window )
5279a24d 389{
77671fd2 390 wxSize minSize( GetMinSize() );
5279a24d
RR
391 wxSize size( window->GetSize() );
392 wxSize client_size( window->GetClientSize() );
77671fd2 393 return wxSize( minSize.x+size.x-client_size.x,
0c0d686f 394 minSize.y+size.y-client_size.y );
5279a24d
RR
395}
396
65ba4113
GT
397// Return a window size that will fit within the screens dimensions
398wxSize wxSizer::FitSize( wxWindow *window )
399{
400 wxSize size = GetMinWindowSize( window );
401 wxSize sizeMax = GetMaxWindowSize( window );
402
403 if ( size.x > sizeMax.x )
404 size.x = sizeMax.x;
405 if ( size.y > sizeMax.y )
406 size.y = sizeMax.y;
407
408 return size;
409}
410
3417c2cd 411void wxSizer::SetDimension( int x, int y, int width, int height )
5279a24d
RR
412{
413 m_position.x = x;
414 m_position.y = y;
415 m_size.x = width;
416 m_size.y = height;
42b4e99e 417 CalcMin();
5279a24d
RR
418 RecalcSizes();
419}
420
f6bcfd97 421wxSize wxSizer::GetMinSize()
3ca6a5f0 422{
f6bcfd97
BP
423 wxSize ret( CalcMin() );
424 if (ret.x < m_minSize.x) ret.x = m_minSize.x;
425 if (ret.y < m_minSize.y) ret.y = m_minSize.y;
3ca6a5f0 426 return ret;
f6bcfd97
BP
427}
428
429void wxSizer::DoSetMinSize( int width, int height )
430{
431 m_minSize.x = width;
432 m_minSize.y = height;
433}
434
435bool wxSizer::DoSetItemMinSize( wxWindow *window, int width, int height )
436{
437 wxASSERT( window );
438
439 wxNode *node = m_children.First();
440 while (node)
441 {
442 wxSizerItem *item = (wxSizerItem*)node->Data();
3ca6a5f0
BP
443 if (item->GetWindow() == window)
444 {
f6bcfd97 445 item->SetInitSize( width, height );
3ca6a5f0
BP
446 return TRUE;
447 }
f6bcfd97
BP
448 node = node->Next();
449 }
450
451 node = m_children.First();
452 while (node)
453 {
454 wxSizerItem *item = (wxSizerItem*)node->Data();
3ca6a5f0
BP
455 if (item->GetSizer())
456 {
f6bcfd97
BP
457 /* It's a sizer, so lets search recursively. */
458 if (item->GetSizer()->DoSetItemMinSize( window, width, height ))
459 {
460 /* A child sizer found the requested windw, exit. */
3ca6a5f0 461 return TRUE;
f6bcfd97 462 }
3ca6a5f0 463 }
f6bcfd97
BP
464 node = node->Next();
465 }
466
467 return FALSE;
468}
469
470bool wxSizer::DoSetItemMinSize( wxSizer *sizer, int width, int height )
471{
472 wxASSERT( sizer );
473
474 wxNode *node = m_children.First();
475 while (node)
476 {
477 wxSizerItem *item = (wxSizerItem*)node->Data();
3ca6a5f0
BP
478 if (item->GetSizer() == sizer)
479 {
f6bcfd97 480 item->GetSizer()->DoSetMinSize( width, height );
3ca6a5f0
BP
481 return TRUE;
482 }
f6bcfd97
BP
483 node = node->Next();
484 }
485
486 node = m_children.First();
487 while (node)
488 {
489 wxSizerItem *item = (wxSizerItem*)node->Data();
3ca6a5f0
BP
490 if (item->GetSizer())
491 {
f6bcfd97
BP
492 /* It's a sizer, so lets search recursively. */
493 if (item->GetSizer()->DoSetItemMinSize( sizer, width, height ))
494 {
495 /* A child sizer found the requested windw, exit. */
3ca6a5f0 496 return TRUE;
f6bcfd97 497 }
3ca6a5f0 498 }
f6bcfd97
BP
499 node = node->Next();
500 }
501
502 return FALSE;
503}
504
505bool wxSizer::DoSetItemMinSize( int pos, int width, int height )
506{
507 wxNode *node = m_children.Nth( pos );
508 if (!node) return FALSE;
509
510 wxSizerItem *item = (wxSizerItem*) node->Data();
511 if (item->GetSizer())
512 {
513 /* Sizers contains the minimal size in them, if not calculated ... */
514 item->GetSizer()->DoSetMinSize( width, height );
515 }
516 else
517 {
3ca6a5f0 518 /* ... whereas the minimal size of spacers and windows in stored
f6bcfd97
BP
519 in the item */
520 item->SetInitSize( width, height );
521 }
522
523 return TRUE;
524}
525
526//---------------------------------------------------------------------------
527// wxGridSizer
528//---------------------------------------------------------------------------
529
530wxGridSizer::wxGridSizer( int rows, int cols, int vgap, int hgap )
531{
532 m_rows = rows;
533 m_cols = cols;
534 m_vgap = vgap;
535 m_hgap = hgap;
536}
537
538wxGridSizer::wxGridSizer( int cols, int vgap, int hgap )
539{
540 m_rows = 0;
541 m_cols = cols;
542 m_vgap = vgap;
543 m_hgap = hgap;
544}
545
546void wxGridSizer::RecalcSizes()
547{
548 if (m_children.GetCount() == 0)
549 return;
550
551 int nitems = m_children.GetCount();
552 int nrows = m_rows;
553 int ncols = m_cols;
554
555 if (ncols > 0)
556 nrows = (nitems + ncols-1) / ncols;
557 else
558 ncols = (nitems + nrows-1) / nrows;
559
560 wxSize sz( GetSize() );
561 wxPoint pt( GetPosition() );
3ca6a5f0
BP
562
563 int w = (sz.x - (ncols - 1) * m_hgap) / ncols;
564 int h = (sz.y - (nrows - 1) * m_vgap) / nrows;
f6bcfd97
BP
565
566 int x = pt.x;
567 for (int c = 0; c < ncols; c++)
568 {
569 int y = pt.y;
570 for (int r = 0; r < nrows; r++)
571 {
572 int i = r * ncols + c;
573 if (i < nitems)
574 {
575 wxNode *node = m_children.Nth( i );
576 wxASSERT( node );
3ca6a5f0 577
f6bcfd97
BP
578 SetItemBounds( (wxSizerItem*) node->Data(), x, y, w, h);
579 }
580 y = y + h + m_vgap;
581 }
582 x = x + w + m_hgap;
583 }
584}
585
586wxSize wxGridSizer::CalcMin()
587{
588 if (m_children.GetCount() == 0)
589 return wxSize(10,10);
590
591 int nitems = m_children.GetCount();
592 int nrows = m_rows;
593 int ncols = m_cols;
594
595 if (ncols > 0)
596 nrows = (nitems + ncols-1) / ncols;
597 else
598 ncols = (nitems + nrows-1) / nrows;
599
600 /* Find the max width and height for any component */
601 int w = 0;
602 int h = 0;
3ca6a5f0 603
f6bcfd97
BP
604 wxNode *node = m_children.First();
605 while (node)
606 {
607 wxSizerItem *item = (wxSizerItem*)node->Data();
608 wxSize sz( item->CalcMin() );
609 w = wxMax( w, sz.x );
610 h = wxMax( h, sz.y );
3ca6a5f0 611
f6bcfd97
BP
612 node = node->Next();
613 }
3ca6a5f0 614
f6bcfd97
BP
615 return wxSize(ncols * w + (ncols-1) * m_hgap,
616 nrows * h + (nrows-1) * m_vgap);
617}
618
619void wxGridSizer::SetItemBounds( wxSizerItem *item, int x, int y, int w, int h )
620{
621 wxPoint pt( x,y );
622 wxSize sz( item->CalcMin() );
623 int flag = item->GetFlag();
624
625 if ((flag & wxEXPAND) || (flag & wxSHAPED))
626 {
627 sz = wxSize(w, h);
628 }
629 else
630 {
631 if (flag & wxALIGN_CENTER_HORIZONTAL)
632 {
633 pt.x = x + (w - sz.x) / 2;
634 }
635 else if (flag & wxALIGN_RIGHT)
636 {
637 pt.x = x + (w - sz.x);
638 }
3ca6a5f0 639
f6bcfd97
BP
640 if (flag & wxALIGN_CENTER_VERTICAL)
641 {
642 pt.y = y + (h - sz.y) / 2;
643 }
644 else if (flag & wxALIGN_BOTTOM)
645 {
646 pt.y = y + (h - sz.y);
647 }
648 }
3ca6a5f0 649
f6bcfd97
BP
650 item->SetDimension(pt, sz);
651}
652
653//---------------------------------------------------------------------------
654// wxFlexGridSizer
655//---------------------------------------------------------------------------
656
657wxFlexGridSizer::wxFlexGridSizer( int rows, int cols, int vgap, int hgap )
658 : wxGridSizer( rows, cols, vgap, hgap )
3ca6a5f0 659{
f6bcfd97
BP
660 m_rowHeights = (int*) NULL;
661 m_colWidths = (int*) NULL;
662}
663
664wxFlexGridSizer::wxFlexGridSizer( int cols, int vgap, int hgap )
3ca6a5f0
BP
665 : wxGridSizer( cols, vgap, hgap )
666{
f6bcfd97
BP
667 m_rowHeights = (int*) NULL;
668 m_colWidths = (int*) NULL;
669}
3ca6a5f0 670
f6bcfd97
BP
671wxFlexGridSizer::~wxFlexGridSizer()
672{
673 if (m_rowHeights)
674 delete[] m_rowHeights;
675 if (m_colWidths)
676 delete[] m_colWidths;
677}
678
679void wxFlexGridSizer::CreateArrays()
680{
681 if (m_rowHeights)
682 delete[] m_rowHeights;
683 if (m_colWidths)
684 delete[] m_colWidths;
3ca6a5f0 685
f6bcfd97
BP
686 if (m_children.GetCount() == 0)
687 return;
3ca6a5f0 688
f6bcfd97
BP
689 int nitems = m_children.GetCount();
690 int nrows = m_rows;
691 int ncols = m_cols;
692
693 if (ncols > 0)
694 nrows = (nitems + ncols-1) / ncols;
695 else
696 ncols = (nitems + nrows-1) / nrows;
697
698 m_rowHeights = new int[nrows];
699 m_colWidths = new int[ncols];
3ca6a5f0 700
f6bcfd97
BP
701 for (int col = 0; col < ncols; col++)
702 m_colWidths[ col ] = 0;
703 for (int row = 0; row < nrows; row++)
704 m_rowHeights[ row ] = 0;
705}
706
707void wxFlexGridSizer::RecalcSizes()
708{
709 if (m_children.GetCount() == 0)
710 return;
711
712 int nitems = m_children.GetCount();
713 int nrows = m_rows;
714 int ncols = m_cols;
715
716 if (ncols > 0)
717 nrows = (nitems + ncols-1) / ncols;
718 else
719 ncols = (nitems + nrows-1) / nrows;
720
721 wxSize sz( GetSize() );
722 wxSize minsz( CalcMin() );
723 wxPoint pt( GetPosition() );
724 int delta;
725 size_t idx;
726
727 if ((m_growableRows.GetCount() > 0) && (sz.y > minsz.y))
728 {
729 delta = (sz.y - minsz.y) / m_growableRows.GetCount();
730 for (idx = 0; idx < m_growableRows.GetCount(); idx++)
731 m_rowHeights[ m_growableRows[idx] ] += delta;
732 }
3ca6a5f0 733
f6bcfd97
BP
734 if ((m_growableCols.GetCount() > 0) && (sz.x > minsz.x))
735 {
736 delta = (sz.x - minsz.x) / m_growableCols.GetCount();
737 for (idx = 0; idx < m_growableCols.GetCount(); idx++)
738 m_colWidths[ m_growableCols[idx] ] += delta;
739 }
3ca6a5f0 740
f6bcfd97
BP
741 sz = wxSize( pt.x + sz.x, pt.y + sz.y );
742
743 int x = pt.x;
744 for (int c = 0; c < ncols; c++)
745 {
746 int y = pt.y;
747 for (int r = 0; r < nrows; r++)
748 {
749 int i = r * ncols + c;
750 if (i < nitems)
751 {
752 wxNode *node = m_children.Nth( i );
753 wxASSERT( node );
3ca6a5f0 754
f6bcfd97
BP
755 int w = wxMax( 0, wxMin( m_colWidths[c], sz.x - x ) );
756 int h = wxMax( 0, wxMin( m_rowHeights[r], sz.y - y ) );
3ca6a5f0 757
f6bcfd97
BP
758 SetItemBounds( (wxSizerItem*) node->Data(), x, y, w, h);
759 }
760 y = y + m_rowHeights[r] + m_vgap;
761 }
762 x = x + m_colWidths[c] + m_hgap;
763 }
764}
765
766wxSize wxFlexGridSizer::CalcMin()
767{
768 if (m_children.GetCount() == 0)
769 return wxSize(10,10);
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 CreateArrays();
3ca6a5f0 781
f6bcfd97
BP
782 int col;
783 int row;
3ca6a5f0 784
f6bcfd97
BP
785 int i = 0;
786 wxNode *node = m_children.First();
787 while (node)
788 {
789 wxSizerItem *item = (wxSizerItem*)node->Data();
790 wxSize sz( item->CalcMin() );
791 row = i / ncols;
792 col = i % ncols;
793 m_rowHeights[ row ] = wxMax( sz.y, m_rowHeights[ row ] );
794 m_colWidths[ col ] = wxMax( sz.x, m_colWidths[ col ] );
3ca6a5f0 795
f6bcfd97
BP
796 node = node->Next();
797 i++;
798 }
3ca6a5f0 799
f6bcfd97
BP
800 int width = 0;
801 for (col = 0; col < ncols; col++)
802 width += m_colWidths[ col ];
3ca6a5f0 803
f6bcfd97
BP
804 int height = 0;
805 for (row = 0; row < nrows; row++)
806 height += m_rowHeights[ row ];
3ca6a5f0 807
f6bcfd97
BP
808 return wxSize( width + (ncols-1) * m_hgap,
809 height + (nrows-1) * m_vgap);
810}
811
812void wxFlexGridSizer::AddGrowableRow( size_t idx )
813{
814 m_growableRows.Add( idx );
815}
816
3ca6a5f0 817void wxFlexGridSizer::RemoveGrowableRow( size_t WXUNUSED(idx) )
f6bcfd97
BP
818{
819}
820
821void wxFlexGridSizer::AddGrowableCol( size_t idx )
822{
823 m_growableCols.Add( idx );
824}
825
3ca6a5f0 826void wxFlexGridSizer::RemoveGrowableCol( size_t WXUNUSED(idx) )
f6bcfd97
BP
827{
828}
829
c62ac5b6 830//---------------------------------------------------------------------------
92afa2b1 831// wxBoxSizer
61d514bb
RR
832//---------------------------------------------------------------------------
833
92afa2b1 834wxBoxSizer::wxBoxSizer( int orient )
61d514bb
RR
835{
836 m_orient = orient;
837}
838
92afa2b1 839void wxBoxSizer::RecalcSizes()
61d514bb
RR
840{
841 if (m_children.GetCount() == 0)
61d514bb 842 return;
0c0d686f 843
61d514bb
RR
844 int delta = 0;
845 int extra = 0;
846 if (m_stretchable)
847 {
848 if (m_orient == wxHORIZONTAL)
849 {
850 delta = (m_size.x - m_fixedWidth) / m_stretchable;
851 extra = (m_size.x - m_fixedWidth) % m_stretchable;
3ca6a5f0
BP
852 }
853 else
854 {
61d514bb
RR
855 delta = (m_size.y - m_fixedHeight) / m_stretchable;
856 extra = (m_size.y - m_fixedHeight) % m_stretchable;
3ca6a5f0 857 }
61d514bb 858 }
0c0d686f 859
61d514bb 860 wxPoint pt( m_position );
0c0d686f 861
61d514bb
RR
862 wxNode *node = m_children.GetFirst();
863 while (node)
864 {
3417c2cd 865 wxSizerItem *item = (wxSizerItem*) node->Data();
61d514bb 866
3ca6a5f0
BP
867 int weight = 1;
868 if (item->GetOption())
869 weight = item->GetOption();
870
871 wxSize size( item->CalcMin() );
872
873 if (m_orient == wxVERTICAL)
874 {
875 wxCoord height = size.y;
876 if (item->GetOption())
877 {
878 height = (delta * weight) + extra;
879 extra = 0; // only the first item will get the remainder as extra size
880 }
881
882 wxPoint child_pos( pt );
883 wxSize child_size( wxSize( size.x, height) );
884
885 if (item->GetFlag() & (wxEXPAND | wxSHAPED))
886 child_size.x = m_size.x;
887 else if (item->GetFlag() & wxALIGN_RIGHT)
888 child_pos.x += m_size.x - size.x;
889 else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_HORIZONTAL))
890 // XXX wxCENTER is added for backward compatibility;
891 // wxALIGN_CENTER should be used in new code
892 child_pos.x += (m_size.x - size.x) / 2;
893
894 item->SetDimension( child_pos, child_size );
895
896 pt.y += height;
897 }
898 else
899 {
900 wxCoord width = size.x;
901 if (item->GetOption())
902 {
903 width = (delta * weight) + extra;
904 extra = 0; // only the first item will get the remainder as extra size
905 }
906
907 wxPoint child_pos( pt );
908 wxSize child_size( wxSize(width, size.y) );
909
910 if (item->GetFlag() & (wxEXPAND | wxSHAPED))
911 child_size.y = m_size.y;
912 else if (item->GetFlag() & wxALIGN_BOTTOM)
913 child_pos.y += m_size.y - size.y;
914 else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_VERTICAL))
915 // XXX wxCENTER is added for backward compatibility;
916 // wxALIGN_CENTER should be used in new code
917 child_pos.y += (m_size.y - size.y) / 2;
918
919 item->SetDimension( child_pos, child_size );
920
921 pt.x += width;
922 }
923
924 node = node->Next();
61d514bb
RR
925 }
926}
927
92afa2b1 928wxSize wxBoxSizer::CalcMin()
61d514bb
RR
929{
930 if (m_children.GetCount() == 0)
c7a9fa36 931 return wxSize(10,10);
0c0d686f 932
61d514bb
RR
933 m_stretchable = 0;
934 m_minWidth = 0;
935 m_minHeight = 0;
936 m_fixedWidth = 0;
937 m_fixedHeight = 0;
0c0d686f 938
61d514bb
RR
939 wxNode *node = m_children.GetFirst();
940 while (node)
941 {
3417c2cd 942 wxSizerItem *item = (wxSizerItem*) node->Data();
0c0d686f 943
aa21b509
RR
944 m_stretchable += item->GetOption();
945
3ca6a5f0
BP
946 wxSize size( item->CalcMin() );
947
948 if (m_orient == wxHORIZONTAL)
949 {
aa21b509 950 m_minWidth += size.x;
3ca6a5f0
BP
951 m_minHeight = wxMax( m_minHeight, size.y );
952 }
953 else
954 {
aa21b509 955 m_minHeight += size.y;
3ca6a5f0
BP
956 m_minWidth = wxMax( m_minWidth, size.x );
957 }
958
aa21b509 959 if (item->GetOption() == 0)
3ca6a5f0
BP
960 {
961 if (m_orient == wxVERTICAL)
962 {
963 m_fixedHeight += size.y;
964 m_fixedWidth = wxMax( m_fixedWidth, size.x );
965 }
966 else
aa21b509 967 {
3ca6a5f0
BP
968 m_fixedWidth += size.x;
969 m_fixedHeight = wxMax( m_fixedHeight, size.y );
970 }
971 }
972
973 node = node->Next();
61d514bb 974 }
0c0d686f 975
61d514bb
RR
976 return wxSize( m_minWidth, m_minHeight );
977}
27ea1d8a
RR
978
979//---------------------------------------------------------------------------
980// wxStaticBoxSizer
981//---------------------------------------------------------------------------
982
983wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox *box, int orient )
84028727 984 : wxBoxSizer( orient )
27ea1d8a 985{
223d09f6 986 wxASSERT_MSG( box, wxT("wxStaticBoxSizer needs a static box") );
0c0d686f 987
27ea1d8a
RR
988 m_staticBox = box;
989}
0c0d686f 990
84028727
VZ
991static void GetStaticBoxBorders(wxStaticBox *box,
992 int *borderTop, int *borderOther)
993{
994 // this has to be done platform by platform as there is no way to
995 // guess the thickness of a wxStaticBox border
996#ifdef __WXGTK__
997 if ( box->GetLabel().IsEmpty() )
998 *borderTop = 5;
999 else
1000#endif // __WXGTK__
1001 *borderTop = 15;
1002
1003 *borderOther = 5;
1004}
1005
27ea1d8a
RR
1006void wxStaticBoxSizer::RecalcSizes()
1007{
84028727
VZ
1008 int top_border, other_border;
1009 GetStaticBoxBorders(m_staticBox, &top_border, &other_border);
27ea1d8a
RR
1010
1011 m_staticBox->SetSize( m_position.x, m_position.y, m_size.x, m_size.y );
0c0d686f 1012
27ea1d8a
RR
1013 wxPoint old_pos( m_position );
1014 m_position.x += other_border;
1015 m_position.y += top_border;
1016 wxSize old_size( m_size );
1017 m_size.x -= 2*other_border;
1018 m_size.y -= top_border + other_border;
0c0d686f 1019
27ea1d8a 1020 wxBoxSizer::RecalcSizes();
0c0d686f 1021
27ea1d8a
RR
1022 m_position = old_pos;
1023 m_size = old_size;
1024}
1025
1026wxSize wxStaticBoxSizer::CalcMin()
1027{
84028727
VZ
1028 int top_border, other_border;
1029 GetStaticBoxBorders(m_staticBox, &top_border, &other_border);
0c0d686f 1030
27ea1d8a 1031 wxSize ret( wxBoxSizer::CalcMin() );
cae31b8b 1032 ret.x += 2*other_border;
27ea1d8a 1033 ret.y += other_border + top_border;
0c0d686f 1034
27ea1d8a
RR
1035 return ret;
1036}
83edc0a5
RR
1037
1038//---------------------------------------------------------------------------
1039// wxNotebookSizer
1040//---------------------------------------------------------------------------
1041
60be2f47
VS
1042#if wxUSE_NOTEBOOK
1043
83edc0a5
RR
1044wxNotebookSizer::wxNotebookSizer( wxNotebook *nb )
1045{
1046 wxASSERT_MSG( nb, wxT("wxNotebookSizer needs a notebook") );
3ca6a5f0 1047
83edc0a5
RR
1048 m_notebook = nb;
1049}
1050
1051void wxNotebookSizer::RecalcSizes()
1052{
1053 m_notebook->SetSize( m_position.x, m_position.y, m_size.x, m_size.y );
1054}
1055
1056wxSize wxNotebookSizer::CalcMin()
1057{
1058 // This will have to be done platform by platform
1059 // as there is no way to guess the thickness of
1060 // the wxNotebook tabs and border.
3ca6a5f0 1061
83edc0a5
RR
1062 int borderX = 5;
1063 int borderY = 5;
1064 if ((m_notebook->HasFlag(wxNB_RIGHT)) ||
1065 (m_notebook->HasFlag(wxNB_LEFT)))
1066 {
f6bcfd97 1067 borderX += 90; // improvements later..
83edc0a5
RR
1068 }
1069 else
1070 {
f6bcfd97 1071 borderY += 40; // improvements later..
83edc0a5 1072 }
3ca6a5f0 1073
83edc0a5
RR
1074 if (m_notebook->GetChildren().GetCount() == 0)
1075 return wxSize(borderX + 10, borderY + 10);
1076
1077 int maxX = 0;
1078 int maxY = 0;
1079
1080 wxWindowList::Node *node = m_notebook->GetChildren().GetFirst();
1081 while (node)
1082 {
1083 wxWindow *item = node->GetData();
3ca6a5f0
BP
1084 wxSizer *itemsizer = item->GetSizer();
1085
1086 if (itemsizer)
1087 {
83edc0a5 1088 wxSize subsize( itemsizer->CalcMin() );
83edc0a5 1089
3ca6a5f0
BP
1090 if (subsize.x > maxX) maxX = subsize.x;
1091 if (subsize.y > maxY) maxY = subsize.y;
1092 }
1093
1094 node = node->GetNext();
83edc0a5
RR
1095 }
1096
1097 return wxSize( borderX + maxX, borderY + maxY );
1098}
1099
60be2f47 1100#endif // wxUSE_NOTEBOOK