Use "non-negative" in assert message instead of "positive".
[wxWidgets.git] / include / wx / sizer.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/sizer.h
3 // Purpose: provide wxSizer class for layout
4 // Author: Robert Roebling and Robin Dunn
5 // Modified by: Ron Lee, Vadim Zeitlin (wxSizerFlags)
6 // Created:
7 // RCS-ID: $Id$
8 // Copyright: (c) Robin Dunn, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef __WXSIZER_H__
13 #define __WXSIZER_H__
14
15 #include "wx/defs.h"
16
17 #include "wx/window.h"
18
19 //---------------------------------------------------------------------------
20 // classes
21 //---------------------------------------------------------------------------
22
23 class WXDLLIMPEXP_FWD_CORE wxButton;
24 class WXDLLIMPEXP_FWD_CORE wxBoxSizer;
25 class WXDLLIMPEXP_FWD_CORE wxSizerItem;
26 class WXDLLIMPEXP_FWD_CORE wxSizer;
27
28 #ifndef wxUSE_BORDER_BY_DEFAULT
29 #ifdef __SMARTPHONE__
30 // no borders by default on limited size screen
31 #define wxUSE_BORDER_BY_DEFAULT 0
32 #else
33 #define wxUSE_BORDER_BY_DEFAULT 1
34 #endif
35 #endif
36
37 // ----------------------------------------------------------------------------
38 // wxSizerFlags: flags used for an item in the sizer
39 // ----------------------------------------------------------------------------
40
41 class WXDLLIMPEXP_CORE wxSizerFlags
42 {
43 public:
44 // construct the flags object initialized with the given proportion (0 by
45 // default)
46 wxSizerFlags(int proportion = 0) : m_proportion(proportion)
47 {
48 m_flags = 0;
49 m_borderInPixels = 0;
50 }
51
52 // setters for all sizer flags, they all return the object itself so that
53 // calls to them can be chained
54
55 wxSizerFlags& Proportion(int proportion)
56 {
57 m_proportion = proportion;
58 return *this;
59 }
60
61 wxSizerFlags& Expand()
62 {
63 m_flags |= wxEXPAND;
64 return *this;
65 }
66
67 // notice that Align() replaces the current alignment flags, use specific
68 // methods below such as Top(), Left() &c if you want to set just the
69 // vertical or horizontal alignment
70 wxSizerFlags& Align(int alignment) // combination of wxAlignment values
71 {
72 m_flags &= ~wxALIGN_MASK;
73 m_flags |= alignment;
74
75 return *this;
76 }
77
78 // some shortcuts for Align()
79 wxSizerFlags& Centre() { return Align(wxALIGN_CENTRE); }
80 wxSizerFlags& Center() { return Centre(); }
81
82 wxSizerFlags& Top()
83 {
84 m_flags &= ~(wxALIGN_BOTTOM | wxALIGN_CENTRE_VERTICAL);
85 return *this;
86 }
87
88 wxSizerFlags& Left()
89 {
90 m_flags &= ~(wxALIGN_RIGHT | wxALIGN_CENTRE_HORIZONTAL);
91 return *this;
92 }
93
94 wxSizerFlags& Right()
95 {
96 m_flags = (m_flags & ~wxALIGN_CENTRE_HORIZONTAL) | wxALIGN_RIGHT;
97 return *this;
98 }
99
100 wxSizerFlags& Bottom()
101 {
102 m_flags = (m_flags & ~wxALIGN_CENTRE_VERTICAL) | wxALIGN_BOTTOM;
103 return *this;
104 }
105
106
107 // default border size used by Border() below
108 static int GetDefaultBorder()
109 {
110 #if wxUSE_BORDER_BY_DEFAULT
111 #ifdef __WXGTK20__
112 // GNOME HIG says to use 6px as the base unit:
113 // http://library.gnome.org/devel/hig-book/stable/design-window.html.en
114 return 6;
115 #else
116 // FIXME: default border size shouldn't be hardcoded and at the very
117 // least they should depend on the current font size
118 return 5;
119 #endif
120 #else
121 return 0;
122 #endif
123 }
124
125
126 wxSizerFlags& Border(int direction, int borderInPixels)
127 {
128 m_flags &= ~wxALL;
129 m_flags |= direction;
130
131 m_borderInPixels = borderInPixels;
132
133 return *this;
134 }
135
136 wxSizerFlags& Border(int direction = wxALL)
137 {
138 #if wxUSE_BORDER_BY_DEFAULT
139 return Border(direction, GetDefaultBorder());
140 #else
141 // no borders by default on limited size screen
142 wxUnusedVar(direction);
143
144 return *this;
145 #endif
146 }
147
148 wxSizerFlags& DoubleBorder(int direction = wxALL)
149 {
150 #if wxUSE_BORDER_BY_DEFAULT
151 return Border(direction, 2*GetDefaultBorder());
152 #else
153 wxUnusedVar(direction);
154
155 return *this;
156 #endif
157 }
158
159 wxSizerFlags& TripleBorder(int direction = wxALL)
160 {
161 #if wxUSE_BORDER_BY_DEFAULT
162 return Border(direction, 3*GetDefaultBorder());
163 #else
164 wxUnusedVar(direction);
165
166 return *this;
167 #endif
168 }
169
170 wxSizerFlags& HorzBorder()
171 {
172 #if wxUSE_BORDER_BY_DEFAULT
173 return Border(wxLEFT | wxRIGHT, GetDefaultBorder());
174 #else
175 return *this;
176 #endif
177 }
178
179 wxSizerFlags& DoubleHorzBorder()
180 {
181 #if wxUSE_BORDER_BY_DEFAULT
182 return Border(wxLEFT | wxRIGHT, 2*GetDefaultBorder());
183 #else
184 return *this;
185 #endif
186 }
187
188 // setters for the others flags
189 wxSizerFlags& Shaped()
190 {
191 m_flags |= wxSHAPED;
192
193 return *this;
194 }
195
196 wxSizerFlags& FixedMinSize()
197 {
198 m_flags |= wxFIXED_MINSIZE;
199
200 return *this;
201 }
202
203 // makes the item ignore window's visibility status
204 wxSizerFlags& ReserveSpaceEvenIfHidden()
205 {
206 m_flags |= wxRESERVE_SPACE_EVEN_IF_HIDDEN;
207 return *this;
208 }
209
210 // accessors for wxSizer only
211 int GetProportion() const { return m_proportion; }
212 int GetFlags() const { return m_flags; }
213 int GetBorderInPixels() const { return m_borderInPixels; }
214
215 private:
216 int m_proportion;
217 int m_flags;
218 int m_borderInPixels;
219 };
220
221
222 // ----------------------------------------------------------------------------
223 // wxSizerSpacer: used by wxSizerItem to represent a spacer
224 // ----------------------------------------------------------------------------
225
226 class WXDLLIMPEXP_CORE wxSizerSpacer
227 {
228 public:
229 wxSizerSpacer(const wxSize& size) : m_size(size), m_isShown(true) { }
230
231 void SetSize(const wxSize& size) { m_size = size; }
232 const wxSize& GetSize() const { return m_size; }
233
234 void Show(bool show) { m_isShown = show; }
235 bool IsShown() const { return m_isShown; }
236
237 private:
238 // the size, in pixel
239 wxSize m_size;
240
241 // is the spacer currently shown?
242 bool m_isShown;
243 };
244
245 // ----------------------------------------------------------------------------
246 // wxSizerItem
247 // ----------------------------------------------------------------------------
248
249 class WXDLLIMPEXP_CORE wxSizerItem : public wxObject
250 {
251 public:
252 // window
253 wxSizerItem( wxWindow *window,
254 int proportion,
255 int flag,
256 int border,
257 wxObject* userData );
258
259 // window with flags
260 wxSizerItem(wxWindow *window, const wxSizerFlags& flags)
261 {
262 Init(flags);
263
264 DoSetWindow(window);
265 }
266
267 // subsizer
268 wxSizerItem( wxSizer *sizer,
269 int proportion,
270 int flag,
271 int border,
272 wxObject* userData );
273
274 // sizer with flags
275 wxSizerItem(wxSizer *sizer, const wxSizerFlags& flags)
276 {
277 Init(flags);
278
279 DoSetSizer(sizer);
280 }
281
282 // spacer
283 wxSizerItem( int width,
284 int height,
285 int proportion,
286 int flag,
287 int border,
288 wxObject* userData);
289
290 // spacer with flags
291 wxSizerItem(int width, int height, const wxSizerFlags& flags)
292 {
293 Init(flags);
294
295 DoSetSpacer(wxSize(width, height));
296 }
297
298 wxSizerItem();
299 virtual ~wxSizerItem();
300
301 virtual void DeleteWindows();
302
303 // Enable deleting the SizerItem without destroying the contained sizer.
304 void DetachSizer() { m_sizer = NULL; }
305
306 virtual wxSize GetSize() const;
307 virtual wxSize CalcMin();
308 virtual void SetDimension( const wxPoint& pos, const wxSize& size );
309
310 wxSize GetMinSize() const
311 { return m_minSize; }
312 wxSize GetMinSizeWithBorder() const;
313
314 void SetMinSize(const wxSize& size)
315 {
316 if ( IsWindow() )
317 m_window->SetMinSize(size);
318 m_minSize = size;
319 }
320 void SetMinSize( int x, int y )
321 { SetMinSize(wxSize(x, y)); }
322 void SetInitSize( int x, int y )
323 { SetMinSize(wxSize(x, y)); }
324
325 // if either of dimensions is zero, ratio is assumed to be 1
326 // to avoid "divide by zero" errors
327 void SetRatio(int width, int height)
328 { m_ratio = (width && height) ? ((float) width / (float) height) : 1; }
329 void SetRatio(const wxSize& size)
330 { SetRatio(size.x, size.y); }
331 void SetRatio(float ratio)
332 { m_ratio = ratio; }
333 float GetRatio() const
334 { return m_ratio; }
335
336 virtual wxRect GetRect() { return m_rect; }
337
338 // set a sizer item id (different from a window id, all sizer items,
339 // including spacers, can have an associated id)
340 void SetId(int id) { m_id = id; }
341 int GetId() const { return m_id; }
342
343 bool IsWindow() const { return m_kind == Item_Window; }
344 bool IsSizer() const { return m_kind == Item_Sizer; }
345 bool IsSpacer() const { return m_kind == Item_Spacer; }
346
347 #if WXWIN_COMPATIBILITY_2_6
348 // Deprecated in 2.6, use {G,S}etProportion instead.
349 wxDEPRECATED( void SetOption( int option ) );
350 wxDEPRECATED( int GetOption() const );
351 #endif // WXWIN_COMPATIBILITY_2_6
352
353 void SetProportion( int proportion )
354 { m_proportion = proportion; }
355 int GetProportion() const
356 { return m_proportion; }
357 void SetFlag( int flag )
358 { m_flag = flag; }
359 int GetFlag() const
360 { return m_flag; }
361 void SetBorder( int border )
362 { m_border = border; }
363 int GetBorder() const
364 { return m_border; }
365
366 wxWindow *GetWindow() const
367 { return m_kind == Item_Window ? m_window : NULL; }
368 wxSizer *GetSizer() const
369 { return m_kind == Item_Sizer ? m_sizer : NULL; }
370 wxSize GetSpacer() const;
371
372 // This function behaves obviously for the windows and spacers but for the
373 // sizers it returns true if any sizer element is shown and only returns
374 // false if all of them are hidden. Also, it always returns true if
375 // wxRESERVE_SPACE_EVEN_IF_HIDDEN flag was used.
376 bool IsShown() const;
377
378 void Show(bool show);
379
380 void SetUserData(wxObject* userData)
381 { delete m_userData; m_userData = userData; }
382 wxObject* GetUserData() const
383 { return m_userData; }
384 wxPoint GetPosition() const
385 { return m_pos; }
386
387 // Called once the first component of an item has been decided. This is
388 // used in algorithms that depend on knowing the size in one direction
389 // before the min size in the other direction can be known.
390 // Returns true if it made use of the information (and min size was changed).
391 bool InformFirstDirection( int direction, int size, int availableOtherDir=-1 );
392
393 // these functions delete the current contents of the item if it's a sizer
394 // or a spacer but not if it is a window
395 void AssignWindow(wxWindow *window)
396 {
397 Free();
398 DoSetWindow(window);
399 }
400
401 void AssignSizer(wxSizer *sizer)
402 {
403 Free();
404 DoSetSizer(sizer);
405 }
406
407 void AssignSpacer(const wxSize& size)
408 {
409 Free();
410 DoSetSpacer(size);
411 }
412
413 void AssignSpacer(int w, int h) { AssignSpacer(wxSize(w, h)); }
414
415 #if WXWIN_COMPATIBILITY_2_8
416 // these functions do not free the old sizer/spacer and so can easily
417 // provoke the memory leaks and so shouldn't be used, use Assign() instead
418 wxDEPRECATED( void SetWindow(wxWindow *window) );
419 wxDEPRECATED( void SetSizer(wxSizer *sizer) );
420 wxDEPRECATED( void SetSpacer(const wxSize& size) );
421 wxDEPRECATED( void SetSpacer(int width, int height) );
422 #endif // WXWIN_COMPATIBILITY_2_8
423
424 protected:
425 // common part of several ctors
426 void Init() { m_userData = NULL; m_kind = Item_None; }
427
428 // common part of ctors taking wxSizerFlags
429 void Init(const wxSizerFlags& flags);
430
431 // free current contents
432 void Free();
433
434 // common parts of Set/AssignXXX()
435 void DoSetWindow(wxWindow *window);
436 void DoSetSizer(wxSizer *sizer);
437 void DoSetSpacer(const wxSize& size);
438
439 // discriminated union: depending on m_kind one of the fields is valid
440 enum
441 {
442 Item_None,
443 Item_Window,
444 Item_Sizer,
445 Item_Spacer,
446 Item_Max
447 } m_kind;
448 union
449 {
450 wxWindow *m_window;
451 wxSizer *m_sizer;
452 wxSizerSpacer *m_spacer;
453 };
454
455 wxPoint m_pos;
456 wxSize m_minSize;
457 int m_proportion;
458 int m_border;
459 int m_flag;
460 int m_id;
461
462 // on screen rectangle of this item (not including borders)
463 wxRect m_rect;
464
465 // Aspect ratio can always be calculated from m_size,
466 // but this would cause precision loss when the window
467 // is shrunk. It is safer to preserve the initial value.
468 float m_ratio;
469
470 wxObject *m_userData;
471
472 private:
473 DECLARE_CLASS(wxSizerItem)
474 wxDECLARE_NO_COPY_CLASS(wxSizerItem);
475 };
476
477 WX_DECLARE_EXPORTED_LIST( wxSizerItem, wxSizerItemList );
478
479
480 //---------------------------------------------------------------------------
481 // wxSizer
482 //---------------------------------------------------------------------------
483
484 class WXDLLIMPEXP_CORE wxSizer: public wxObject, public wxClientDataContainer
485 {
486 public:
487 wxSizer() { m_containingWindow = NULL; }
488 virtual ~wxSizer();
489
490 // methods for adding elements to the sizer: there are Add/Insert/Prepend
491 // overloads for each of window/sizer/spacer/wxSizerItem
492 wxSizerItem* Add(wxWindow *window,
493 int proportion = 0,
494 int flag = 0,
495 int border = 0,
496 wxObject* userData = NULL);
497 wxSizerItem* Add(wxSizer *sizer,
498 int proportion = 0,
499 int flag = 0,
500 int border = 0,
501 wxObject* userData = NULL);
502 wxSizerItem* Add(int width,
503 int height,
504 int proportion = 0,
505 int flag = 0,
506 int border = 0,
507 wxObject* userData = NULL);
508 wxSizerItem* Add( wxWindow *window, const wxSizerFlags& flags);
509 wxSizerItem* Add( wxSizer *sizer, const wxSizerFlags& flags);
510 wxSizerItem* Add( int width, int height, const wxSizerFlags& flags);
511 wxSizerItem* Add( wxSizerItem *item);
512
513 wxSizerItem* AddSpacer(int size);
514 wxSizerItem* AddStretchSpacer(int prop = 1);
515
516 wxSizerItem* Insert(size_t index,
517 wxWindow *window,
518 int proportion = 0,
519 int flag = 0,
520 int border = 0,
521 wxObject* userData = NULL);
522 wxSizerItem* Insert(size_t index,
523 wxSizer *sizer,
524 int proportion = 0,
525 int flag = 0,
526 int border = 0,
527 wxObject* userData = NULL);
528 wxSizerItem* Insert(size_t index,
529 int width,
530 int height,
531 int proportion = 0,
532 int flag = 0,
533 int border = 0,
534 wxObject* userData = NULL);
535 wxSizerItem* Insert(size_t index,
536 wxWindow *window,
537 const wxSizerFlags& flags);
538 wxSizerItem* Insert(size_t index,
539 wxSizer *sizer,
540 const wxSizerFlags& flags);
541 wxSizerItem* Insert(size_t index,
542 int width,
543 int height,
544 const wxSizerFlags& flags);
545 virtual wxSizerItem* Insert( size_t index, wxSizerItem *item);
546
547 wxSizerItem* InsertSpacer(size_t index, int size);
548 wxSizerItem* InsertStretchSpacer(size_t index, int prop = 1);
549
550 wxSizerItem* Prepend(wxWindow *window,
551 int proportion = 0,
552 int flag = 0,
553 int border = 0,
554 wxObject* userData = NULL);
555 wxSizerItem* Prepend(wxSizer *sizer,
556 int proportion = 0,
557 int flag = 0,
558 int border = 0,
559 wxObject* userData = NULL);
560 wxSizerItem* Prepend(int width,
561 int height,
562 int proportion = 0,
563 int flag = 0,
564 int border = 0,
565 wxObject* userData = NULL);
566 wxSizerItem* Prepend(wxWindow *window, const wxSizerFlags& flags);
567 wxSizerItem* Prepend(wxSizer *sizer, const wxSizerFlags& flags);
568 wxSizerItem* Prepend(int width, int height, const wxSizerFlags& flags);
569 wxSizerItem* Prepend(wxSizerItem *item);
570
571 wxSizerItem* PrependSpacer(int size);
572 wxSizerItem* PrependStretchSpacer(int prop = 1);
573
574 // set (or possibly unset if window is NULL) or get the window this sizer
575 // is used in
576 void SetContainingWindow(wxWindow *window);
577 wxWindow *GetContainingWindow() const { return m_containingWindow; }
578
579 #if WXWIN_COMPATIBILITY_2_6
580 // Deprecated in 2.6 since historically it does not delete the window,
581 // use Detach instead.
582 wxDEPRECATED( virtual bool Remove( wxWindow *window ) );
583 #endif // WXWIN_COMPATIBILITY_2_6
584
585 virtual bool Remove( wxSizer *sizer );
586 virtual bool Remove( int index );
587
588 virtual bool Detach( wxWindow *window );
589 virtual bool Detach( wxSizer *sizer );
590 virtual bool Detach( int index );
591
592 virtual bool Replace( wxWindow *oldwin, wxWindow *newwin, bool recursive = false );
593 virtual bool Replace( wxSizer *oldsz, wxSizer *newsz, bool recursive = false );
594 virtual bool Replace( size_t index, wxSizerItem *newitem );
595
596 virtual void Clear( bool delete_windows = false );
597 virtual void DeleteWindows();
598
599 // Inform sizer about the first direction that has been decided (by parent item)
600 // Returns true if it made use of the informtion (and recalculated min size)
601 virtual bool InformFirstDirection( int WXUNUSED(direction), int WXUNUSED(size), int WXUNUSED(availableOtherDir) )
602 { return false; }
603
604 void SetMinSize( int width, int height )
605 { DoSetMinSize( width, height ); }
606 void SetMinSize( const wxSize& size )
607 { DoSetMinSize( size.x, size.y ); }
608
609 // Searches recursively
610 bool SetItemMinSize( wxWindow *window, int width, int height )
611 { return DoSetItemMinSize( window, width, height ); }
612 bool SetItemMinSize( wxWindow *window, const wxSize& size )
613 { return DoSetItemMinSize( window, size.x, size.y ); }
614
615 // Searches recursively
616 bool SetItemMinSize( wxSizer *sizer, int width, int height )
617 { return DoSetItemMinSize( sizer, width, height ); }
618 bool SetItemMinSize( wxSizer *sizer, const wxSize& size )
619 { return DoSetItemMinSize( sizer, size.x, size.y ); }
620
621 bool SetItemMinSize( size_t index, int width, int height )
622 { return DoSetItemMinSize( index, width, height ); }
623 bool SetItemMinSize( size_t index, const wxSize& size )
624 { return DoSetItemMinSize( index, size.x, size.y ); }
625
626 wxSize GetSize() const
627 { return m_size; }
628 wxPoint GetPosition() const
629 { return m_position; }
630
631 // Calculate the minimal size or return m_minSize if bigger.
632 wxSize GetMinSize();
633
634 // These virtual functions are used by the layout algorithm: first
635 // CalcMin() is called to calculate the minimal size of the sizer and
636 // prepare for laying it out and then RecalcSizes() is called to really
637 // update all the sizer items
638 virtual wxSize CalcMin() = 0;
639 virtual void RecalcSizes() = 0;
640
641 virtual void Layout();
642
643 wxSize ComputeFittingClientSize(wxWindow *window);
644 wxSize ComputeFittingWindowSize(wxWindow *window);
645
646 wxSize Fit( wxWindow *window );
647 void FitInside( wxWindow *window );
648 void SetSizeHints( wxWindow *window );
649 #if WXWIN_COMPATIBILITY_2_8
650 // This only calls FitInside() since 2.9
651 wxDEPRECATED( void SetVirtualSizeHints( wxWindow *window ) );
652 #endif
653
654 wxSizerItemList& GetChildren()
655 { return m_children; }
656 const wxSizerItemList& GetChildren() const
657 { return m_children; }
658
659 void SetDimension(const wxPoint& pos, const wxSize& size)
660 {
661 m_position = pos;
662 m_size = size;
663 Layout();
664 }
665 void SetDimension(int x, int y, int width, int height)
666 { SetDimension(wxPoint(x, y), wxSize(width, height)); }
667
668 size_t GetItemCount() const { return m_children.GetCount(); }
669 bool IsEmpty() const { return m_children.IsEmpty(); }
670
671 wxSizerItem* GetItem( wxWindow *window, bool recursive = false );
672 wxSizerItem* GetItem( wxSizer *sizer, bool recursive = false );
673 wxSizerItem* GetItem( size_t index );
674 wxSizerItem* GetItemById( int id, bool recursive = false );
675
676 // Manage whether individual scene items are considered
677 // in the layout calculations or not.
678 bool Show( wxWindow *window, bool show = true, bool recursive = false );
679 bool Show( wxSizer *sizer, bool show = true, bool recursive = false );
680 bool Show( size_t index, bool show = true );
681
682 bool Hide( wxSizer *sizer, bool recursive = false )
683 { return Show( sizer, false, recursive ); }
684 bool Hide( wxWindow *window, bool recursive = false )
685 { return Show( window, false, recursive ); }
686 bool Hide( size_t index )
687 { return Show( index, false ); }
688
689 bool IsShown( wxWindow *window ) const;
690 bool IsShown( wxSizer *sizer ) const;
691 bool IsShown( size_t index ) const;
692
693 // Recursively call wxWindow::Show () on all sizer items.
694 virtual void ShowItems (bool show);
695
696 void Show(bool show) { ShowItems(show); }
697
698 protected:
699 wxSize m_size;
700 wxSize m_minSize;
701 wxPoint m_position;
702 wxSizerItemList m_children;
703
704 // the window this sizer is used in, can be NULL
705 wxWindow *m_containingWindow;
706
707 wxSize GetMaxClientSize( wxWindow *window ) const;
708 wxSize GetMinClientSize( wxWindow *window );
709 wxSize VirtualFitSize( wxWindow *window );
710
711 virtual void DoSetMinSize( int width, int height );
712 virtual bool DoSetItemMinSize( wxWindow *window, int width, int height );
713 virtual bool DoSetItemMinSize( wxSizer *sizer, int width, int height );
714 virtual bool DoSetItemMinSize( size_t index, int width, int height );
715
716 private:
717 DECLARE_CLASS(wxSizer)
718 };
719
720 //---------------------------------------------------------------------------
721 // wxGridSizer
722 //---------------------------------------------------------------------------
723
724 class WXDLLIMPEXP_CORE wxGridSizer: public wxSizer
725 {
726 public:
727 // ctors specifying the number of columns only: number of rows will be
728 // deduced automatically depending on the number of sizer elements
729 wxGridSizer( int cols, int vgap, int hgap );
730 wxGridSizer( int cols, const wxSize& gap = wxSize(0, 0) );
731
732 // ctors specifying the number of rows and columns
733 wxGridSizer( int rows, int cols, int vgap, int hgap );
734 wxGridSizer( int rows, int cols, const wxSize& gap );
735
736 virtual wxSizerItem *Insert(size_t index, wxSizerItem *item);
737
738 virtual void RecalcSizes();
739 virtual wxSize CalcMin();
740
741 void SetCols( int cols )
742 {
743 wxASSERT_MSG( cols >= 0, "Number of columns must be non-negative");
744 m_cols = cols;
745 }
746
747 void SetRows( int rows )
748 {
749 wxASSERT_MSG( rows >= 0, "Number of rows must be non-negative");
750 m_rows = rows;
751 }
752
753 void SetVGap( int gap ) { m_vgap = gap; }
754 void SetHGap( int gap ) { m_hgap = gap; }
755 int GetCols() const { return m_cols; }
756 int GetRows() const { return m_rows; }
757 int GetVGap() const { return m_vgap; }
758 int GetHGap() const { return m_hgap; }
759
760 // return the number of total items and the number of columns and rows
761 // (for internal use only)
762 int CalcRowsCols(int& rows, int& cols) const;
763
764 protected:
765 int m_rows;
766 int m_cols;
767 int m_vgap;
768 int m_hgap;
769
770 void SetItemBounds( wxSizerItem *item, int x, int y, int w, int h );
771
772 private:
773 DECLARE_CLASS(wxGridSizer)
774 };
775
776 //---------------------------------------------------------------------------
777 // wxFlexGridSizer
778 //---------------------------------------------------------------------------
779
780 // values which define the behaviour for resizing wxFlexGridSizer cells in the
781 // "non-flexible" direction
782 enum wxFlexSizerGrowMode
783 {
784 // don't resize the cells in non-flexible direction at all
785 wxFLEX_GROWMODE_NONE,
786
787 // uniformly resize only the specified ones (default)
788 wxFLEX_GROWMODE_SPECIFIED,
789
790 // uniformly resize all cells
791 wxFLEX_GROWMODE_ALL
792 };
793
794 class WXDLLIMPEXP_CORE wxFlexGridSizer: public wxGridSizer
795 {
796 public:
797 // ctors specifying the number of columns only: number of rows will be
798 // deduced automatically depending on the number of sizer elements
799 wxFlexGridSizer( int cols, int vgap, int hgap );
800 wxFlexGridSizer( int cols, const wxSize& gap = wxSize(0, 0) );
801
802 // ctors specifying the number of rows and columns
803 wxFlexGridSizer( int rows, int cols, int vgap, int hgap );
804 wxFlexGridSizer( int rows, int cols, const wxSize& gap );
805
806 // dtor
807 virtual ~wxFlexGridSizer();
808
809 // set the rows/columns which will grow (the others will remain of the
810 // constant initial size)
811 void AddGrowableRow( size_t idx, int proportion = 0 );
812 void RemoveGrowableRow( size_t idx );
813 void AddGrowableCol( size_t idx, int proportion = 0 );
814 void RemoveGrowableCol( size_t idx );
815
816 bool IsRowGrowable( size_t idx );
817 bool IsColGrowable( size_t idx );
818
819 // the sizer cells may grow in both directions, not grow at all or only
820 // grow in one direction but not the other
821
822 // the direction may be wxVERTICAL, wxHORIZONTAL or wxBOTH (default)
823 void SetFlexibleDirection(int direction) { m_flexDirection = direction; }
824 int GetFlexibleDirection() const { return m_flexDirection; }
825
826 // note that the grow mode only applies to the direction which is not
827 // flexible
828 void SetNonFlexibleGrowMode(wxFlexSizerGrowMode mode) { m_growMode = mode; }
829 wxFlexSizerGrowMode GetNonFlexibleGrowMode() const { return m_growMode; }
830
831 // Read-only access to the row heights and col widths arrays
832 const wxArrayInt& GetRowHeights() const { return m_rowHeights; }
833 const wxArrayInt& GetColWidths() const { return m_colWidths; }
834
835 // implementation
836 virtual void RecalcSizes();
837 virtual wxSize CalcMin();
838
839 protected:
840 void AdjustForFlexDirection();
841 void AdjustForGrowables(const wxSize& sz);
842 void FindWidthsAndHeights(int nrows, int ncols);
843
844 // the heights/widths of all rows/columns
845 wxArrayInt m_rowHeights,
846 m_colWidths;
847
848 // indices of the growable columns and rows
849 wxArrayInt m_growableRows,
850 m_growableCols;
851
852 // proportion values of the corresponding growable rows and columns
853 wxArrayInt m_growableRowsProportions,
854 m_growableColsProportions;
855
856 // parameters describing whether the growable cells should be resized in
857 // both directions or only one
858 int m_flexDirection;
859 wxFlexSizerGrowMode m_growMode;
860
861 // saves CalcMin result to optimize RecalcSizes
862 wxSize m_calculatedMinSize;
863
864 private:
865 DECLARE_CLASS(wxFlexGridSizer)
866 wxDECLARE_NO_COPY_CLASS(wxFlexGridSizer);
867 };
868
869 //---------------------------------------------------------------------------
870 // wxBoxSizer
871 //---------------------------------------------------------------------------
872
873 class WXDLLIMPEXP_CORE wxBoxSizer: public wxSizer
874 {
875 public:
876 wxBoxSizer(int orient)
877 {
878 m_orient = orient;
879 m_totalProportion = 0;
880
881 wxASSERT_MSG( m_orient == wxHORIZONTAL || m_orient == wxVERTICAL,
882 wxT("invalid value for wxBoxSizer orientation") );
883 }
884
885 int GetOrientation() const { return m_orient; }
886
887 bool IsVertical() const { return m_orient == wxVERTICAL; }
888
889 void SetOrientation(int orient) { m_orient = orient; }
890
891 // implementation of our resizing logic
892 virtual wxSize CalcMin();
893 virtual void RecalcSizes();
894
895 protected:
896 // helpers for our code: this returns the component of the given wxSize in
897 // the direction of the sizer and in the other direction, respectively
898 int GetSizeInMajorDir(const wxSize& sz) const
899 {
900 return m_orient == wxHORIZONTAL ? sz.x : sz.y;
901 }
902
903 int& SizeInMajorDir(wxSize& sz)
904 {
905 return m_orient == wxHORIZONTAL ? sz.x : sz.y;
906 }
907
908 int& PosInMajorDir(wxPoint& pt)
909 {
910 return m_orient == wxHORIZONTAL ? pt.x : pt.y;
911 }
912
913 int GetSizeInMinorDir(const wxSize& sz) const
914 {
915 return m_orient == wxHORIZONTAL ? sz.y : sz.x;
916 }
917
918 int& SizeInMinorDir(wxSize& sz)
919 {
920 return m_orient == wxHORIZONTAL ? sz.y : sz.x;
921 }
922
923 int& PosInMinorDir(wxPoint& pt)
924 {
925 return m_orient == wxHORIZONTAL ? pt.y : pt.x;
926 }
927
928 // another helper: creates wxSize from major and minor components
929 wxSize SizeFromMajorMinor(int major, int minor) const
930 {
931 if ( m_orient == wxHORIZONTAL )
932 {
933 return wxSize(major, minor);
934 }
935 else // wxVERTICAL
936 {
937 return wxSize(minor, major);
938 }
939 }
940
941
942 // either wxHORIZONTAL or wxVERTICAL
943 int m_orient;
944
945 // the sum of proportion of all of our elements
946 int m_totalProportion;
947
948 // the minimal size needed for this sizer as calculated by the last call to
949 // our CalcMin()
950 wxSize m_minSize;
951
952 private:
953 DECLARE_CLASS(wxBoxSizer)
954 };
955
956 //---------------------------------------------------------------------------
957 // wxStaticBoxSizer
958 //---------------------------------------------------------------------------
959
960 #if wxUSE_STATBOX
961
962 class WXDLLIMPEXP_FWD_CORE wxStaticBox;
963
964 class WXDLLIMPEXP_CORE wxStaticBoxSizer: public wxBoxSizer
965 {
966 public:
967 wxStaticBoxSizer(wxStaticBox *box, int orient);
968 wxStaticBoxSizer(int orient, wxWindow *win, const wxString& label = wxEmptyString);
969 virtual ~wxStaticBoxSizer();
970
971 void RecalcSizes();
972 wxSize CalcMin();
973
974 wxStaticBox *GetStaticBox() const
975 { return m_staticBox; }
976
977 // override to hide/show the static box as well
978 virtual void ShowItems (bool show);
979
980 virtual bool Detach( wxWindow *window );
981 virtual bool Detach( wxSizer *sizer ) { return wxBoxSizer::Detach(sizer); }
982 virtual bool Detach( int index ) { return wxBoxSizer::Detach(index); }
983
984 protected:
985 wxStaticBox *m_staticBox;
986
987 private:
988 DECLARE_CLASS(wxStaticBoxSizer)
989 wxDECLARE_NO_COPY_CLASS(wxStaticBoxSizer);
990 };
991
992 #endif // wxUSE_STATBOX
993
994 //---------------------------------------------------------------------------
995 // wxStdDialogButtonSizer
996 //---------------------------------------------------------------------------
997
998 #if wxUSE_BUTTON
999
1000 class WXDLLIMPEXP_CORE wxStdDialogButtonSizer: public wxBoxSizer
1001 {
1002 public:
1003 // Constructor just creates a new wxBoxSizer, not much else.
1004 // Box sizer orientation is automatically determined here:
1005 // vertical for PDAs, horizontal for everything else?
1006 wxStdDialogButtonSizer();
1007
1008 // Checks button ID against system IDs and sets one of the pointers below
1009 // to this button. Does not do any sizer-related things here.
1010 void AddButton(wxButton *button);
1011
1012 // Use these if no standard ID can/should be used
1013 void SetAffirmativeButton( wxButton *button );
1014 void SetNegativeButton( wxButton *button );
1015 void SetCancelButton( wxButton *button );
1016
1017 // All platform-specific code here, checks which buttons exist and add
1018 // them to the sizer accordingly.
1019 // Note - one potential hack on Mac we could use here,
1020 // if m_buttonAffirmative is wxID_SAVE then ensure wxID_SAVE
1021 // is set to _("Save") and m_buttonNegative is set to _("Don't Save")
1022 // I wouldn't add any other hacks like that into here,
1023 // but this one I can see being useful.
1024 void Realize();
1025
1026 wxButton *GetAffirmativeButton() const { return m_buttonAffirmative; }
1027 wxButton *GetApplyButton() const { return m_buttonApply; }
1028 wxButton *GetNegativeButton() const { return m_buttonNegative; }
1029 wxButton *GetCancelButton() const { return m_buttonCancel; }
1030 wxButton *GetHelpButton() const { return m_buttonHelp; }
1031
1032 protected:
1033 wxButton *m_buttonAffirmative; // wxID_OK, wxID_YES, wxID_SAVE go here
1034 wxButton *m_buttonApply; // wxID_APPLY
1035 wxButton *m_buttonNegative; // wxID_NO
1036 wxButton *m_buttonCancel; // wxID_CANCEL, wxID_CLOSE
1037 wxButton *m_buttonHelp; // wxID_HELP, wxID_CONTEXT_HELP
1038
1039 private:
1040 DECLARE_CLASS(wxStdDialogButtonSizer)
1041 wxDECLARE_NO_COPY_CLASS(wxStdDialogButtonSizer);
1042 };
1043
1044 #endif // wxUSE_BUTTON
1045
1046
1047 // ----------------------------------------------------------------------------
1048 // inline functions implementation
1049 // ----------------------------------------------------------------------------
1050
1051 #if WXWIN_COMPATIBILITY_2_8
1052
1053 inline void wxSizerItem::SetWindow(wxWindow *window)
1054 {
1055 DoSetWindow(window);
1056 }
1057
1058 inline void wxSizerItem::SetSizer(wxSizer *sizer)
1059 {
1060 DoSetSizer(sizer);
1061 }
1062
1063 inline void wxSizerItem::SetSpacer(const wxSize& size)
1064 {
1065 DoSetSpacer(size);
1066 }
1067
1068 inline void wxSizerItem::SetSpacer(int width, int height)
1069 {
1070 DoSetSpacer(wxSize(width, height));
1071 }
1072
1073 #endif // WXWIN_COMPATIBILITY_2_8
1074
1075
1076 inline wxSizerItem*
1077 wxSizer::Add( wxSizerItem *item )
1078 {
1079 return Insert( m_children.GetCount(), item );
1080 }
1081
1082 inline wxSizerItem*
1083 wxSizer::Add( wxWindow *window, int proportion, int flag, int border, wxObject* userData )
1084 {
1085 return Add( new wxSizerItem( window, proportion, flag, border, userData ) );
1086 }
1087
1088 inline wxSizerItem*
1089 wxSizer::Add( wxSizer *sizer, int proportion, int flag, int border, wxObject* userData )
1090 {
1091 return Add( new wxSizerItem( sizer, proportion, flag, border, userData ) );
1092 }
1093
1094 inline wxSizerItem*
1095 wxSizer::Add( int width, int height, int proportion, int flag, int border, wxObject* userData )
1096 {
1097 return Add( new wxSizerItem( width, height, proportion, flag, border, userData ) );
1098 }
1099
1100 inline wxSizerItem*
1101 wxSizer::Add( wxWindow *window, const wxSizerFlags& flags )
1102 {
1103 return Add( new wxSizerItem(window, flags) );
1104 }
1105
1106 inline wxSizerItem*
1107 wxSizer::Add( wxSizer *sizer, const wxSizerFlags& flags )
1108 {
1109 return Add( new wxSizerItem(sizer, flags) );
1110 }
1111
1112 inline wxSizerItem*
1113 wxSizer::Add( int width, int height, const wxSizerFlags& flags )
1114 {
1115 return Add( new wxSizerItem(width, height, flags) );
1116 }
1117
1118 inline wxSizerItem*
1119 wxSizer::AddSpacer(int size)
1120 {
1121 return Add(size, size);
1122 }
1123
1124 inline wxSizerItem*
1125 wxSizer::AddStretchSpacer(int prop)
1126 {
1127 return Add(0, 0, prop);
1128 }
1129
1130 inline wxSizerItem*
1131 wxSizer::Prepend( wxSizerItem *item )
1132 {
1133 return Insert( 0, item );
1134 }
1135
1136 inline wxSizerItem*
1137 wxSizer::Prepend( wxWindow *window, int proportion, int flag, int border, wxObject* userData )
1138 {
1139 return Prepend( new wxSizerItem( window, proportion, flag, border, userData ) );
1140 }
1141
1142 inline wxSizerItem*
1143 wxSizer::Prepend( wxSizer *sizer, int proportion, int flag, int border, wxObject* userData )
1144 {
1145 return Prepend( new wxSizerItem( sizer, proportion, flag, border, userData ) );
1146 }
1147
1148 inline wxSizerItem*
1149 wxSizer::Prepend( int width, int height, int proportion, int flag, int border, wxObject* userData )
1150 {
1151 return Prepend( new wxSizerItem( width, height, proportion, flag, border, userData ) );
1152 }
1153
1154 inline wxSizerItem*
1155 wxSizer::PrependSpacer(int size)
1156 {
1157 return Prepend(size, size);
1158 }
1159
1160 inline wxSizerItem*
1161 wxSizer::PrependStretchSpacer(int prop)
1162 {
1163 return Prepend(0, 0, prop);
1164 }
1165
1166 inline wxSizerItem*
1167 wxSizer::Prepend( wxWindow *window, const wxSizerFlags& flags )
1168 {
1169 return Prepend( new wxSizerItem(window, flags) );
1170 }
1171
1172 inline wxSizerItem*
1173 wxSizer::Prepend( wxSizer *sizer, const wxSizerFlags& flags )
1174 {
1175 return Prepend( new wxSizerItem(sizer, flags) );
1176 }
1177
1178 inline wxSizerItem*
1179 wxSizer::Prepend( int width, int height, const wxSizerFlags& flags )
1180 {
1181 return Prepend( new wxSizerItem(width, height, flags) );
1182 }
1183
1184 inline wxSizerItem*
1185 wxSizer::Insert( size_t index,
1186 wxWindow *window,
1187 int proportion,
1188 int flag,
1189 int border,
1190 wxObject* userData )
1191 {
1192 return Insert( index, new wxSizerItem( window, proportion, flag, border, userData ) );
1193 }
1194
1195 inline wxSizerItem*
1196 wxSizer::Insert( size_t index,
1197 wxSizer *sizer,
1198 int proportion,
1199 int flag,
1200 int border,
1201 wxObject* userData )
1202 {
1203 return Insert( index, new wxSizerItem( sizer, proportion, flag, border, userData ) );
1204 }
1205
1206 inline wxSizerItem*
1207 wxSizer::Insert( size_t index,
1208 int width,
1209 int height,
1210 int proportion,
1211 int flag,
1212 int border,
1213 wxObject* userData )
1214 {
1215 return Insert( index, new wxSizerItem( width, height, proportion, flag, border, userData ) );
1216 }
1217
1218 inline wxSizerItem*
1219 wxSizer::Insert( size_t index, wxWindow *window, const wxSizerFlags& flags )
1220 {
1221 return Insert( index, new wxSizerItem(window, flags) );
1222 }
1223
1224 inline wxSizerItem*
1225 wxSizer::Insert( size_t index, wxSizer *sizer, const wxSizerFlags& flags )
1226 {
1227 return Insert( index, new wxSizerItem(sizer, flags) );
1228 }
1229
1230 inline wxSizerItem*
1231 wxSizer::Insert( size_t index, int width, int height, const wxSizerFlags& flags )
1232 {
1233 return Insert( index, new wxSizerItem(width, height, flags) );
1234 }
1235
1236 inline wxSizerItem*
1237 wxSizer::InsertSpacer(size_t index, int size)
1238 {
1239 return Insert(index, size, size);
1240 }
1241
1242 inline wxSizerItem*
1243 wxSizer::InsertStretchSpacer(size_t index, int prop)
1244 {
1245 return Insert(index, 0, 0, prop);
1246 }
1247
1248 #endif // __WXSIZER_H__