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