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