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