1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: provide wxSizer class for layout
4 // Author: Robert Roebling and Robin Dunn
5 // Modified by: Ron Lee, Vadim Zeitlin (wxSizerFlags)
8 // Copyright: (c) Robin Dunn, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "sizer.h"
21 #include "wx/window.h"
23 #include "wx/dialog.h"
24 #include "wx/bookctrl.h"
26 //---------------------------------------------------------------------------
28 //---------------------------------------------------------------------------
30 class WXDLLEXPORT wxSizerItem
;
31 class WXDLLEXPORT wxSizer
;
32 class WXDLLEXPORT wxBoxSizer
;
35 // ----------------------------------------------------------------------------
36 // wxSizerFlags: flags used for an item in the sizer
37 // ----------------------------------------------------------------------------
39 class WXDLLEXPORT wxSizerFlags
42 // construct the flags object initialized with the given proportion (0 by
44 wxSizerFlags(int proportion
= 0) : m_proportion(proportion
)
50 // setters for all sizer flags, they all return the object itself so that
51 // calls to them can be chained
53 wxSizerFlags
& Proportion(int proportion
)
55 m_proportion
= proportion
;
59 wxSizerFlags
& Align(int alignment
) // combination of wxAlignment values
67 // some shortcuts for Align()
68 wxSizerFlags
& Expand() { return Align(wxEXPAND
); }
69 wxSizerFlags
& Centre() { return Align(wxCENTRE
); }
70 wxSizerFlags
& Center() { return Centre(); }
73 wxSizerFlags
& Border(int direction
, int borderInPixels
)
78 m_borderInPixels
= borderInPixels
;
83 wxSizerFlags
& Border(int direction
= wxALL
)
85 // FIXME: default border size shouldn't be hardcoded
86 return Border(direction
, 5);
90 // accessors for wxSizer only
91 int GetProportion() const { return m_proportion
; }
92 int GetFlags() const { return m_flags
; }
93 int GetBorderInPixels() const { return m_borderInPixels
; }
102 //---------------------------------------------------------------------------
104 //---------------------------------------------------------------------------
106 class WXDLLEXPORT wxSizerItem
: public wxObject
110 wxSizerItem(wxWindow
*window
, const wxSizerFlags
& flags
)
118 wxSizerItem(wxSizer
*sizer
, const wxSizerFlags
& flags
)
126 wxSizerItem( wxWindow
*window
,
130 wxObject
* userData
);
133 wxSizerItem( wxSizer
*sizer
,
137 wxObject
* userData
);
140 wxSizerItem( int width
,
148 virtual ~wxSizerItem();
150 virtual void DeleteWindows();
152 // Enable deleting the SizerItem without destroying the contained sizer.
156 virtual wxSize
GetSize() const;
157 virtual wxSize
CalcMin();
158 virtual void SetDimension( wxPoint pos
, wxSize size
);
160 wxSize
GetMinSize() const
161 { return m_minSize
; }
162 wxSize
GetMinSizeWithBorder() const;
164 void SetMinSize(const wxSize
& size
)
166 if (IsWindow()) m_window
->SetMinSize(size
);
169 void SetMinSize( int x
, int y
)
170 { SetMinSize(wxSize(x
, y
)); }
171 void SetInitSize( int x
, int y
)
172 { SetMinSize(wxSize(x
, y
)); }
174 void SetRatio( int width
, int height
)
175 // if either of dimensions is zero, ratio is assumed to be 1
176 // to avoid "divide by zero" errors
177 { m_ratio
= (width
&& height
) ? ((float) width
/ (float) height
) : 1; }
178 void SetRatio( wxSize size
)
179 { m_ratio
= (size
.x
&& size
.y
) ? ((float) size
.x
/ (float) size
.y
) : 1; }
180 void SetRatio( float ratio
)
182 float GetRatio() const
185 bool IsWindow() const;
186 bool IsSizer() const;
187 bool IsSpacer() const;
189 // Deprecated in 2.6, use {G,S}etProportion instead.
190 wxDEPRECATED( void SetOption( int option
) );
191 wxDEPRECATED( int GetOption() const );
193 void SetProportion( int proportion
)
194 { m_proportion
= proportion
; }
195 int GetProportion() const
196 { return m_proportion
; }
197 void SetFlag( int flag
)
201 void SetBorder( int border
)
202 { m_border
= border
; }
203 int GetBorder() const
206 wxWindow
*GetWindow() const
208 void SetWindow( wxWindow
*window
)
209 { m_window
= window
; m_minSize
= window
->GetSize(); }
210 wxSizer
*GetSizer() const
212 void SetSizer( wxSizer
*sizer
)
214 const wxSize
&GetSpacer() const
216 void SetSpacer( const wxSize
&size
)
217 { m_size
= size
; m_minSize
= size
; }
219 void Show ( bool show
);
223 wxObject
* GetUserData() const
224 { return m_userData
; }
225 wxPoint
GetPosition() const
229 // common part of several ctors
232 // common part of ctors taking wxSizerFlags
233 void Init(const wxSizerFlags
& flags
);
245 // If true, then this item is considered in the layout
246 // calculation. Otherwise, it is skipped over.
249 // Aspect ratio can always be calculated from m_size,
250 // but this would cause precision loss when the window
251 // is shrunk. It is safer to preserve the initial value.
254 wxObject
*m_userData
;
257 DECLARE_CLASS(wxSizerItem
)
258 DECLARE_NO_COPY_CLASS(wxSizerItem
)
261 WX_DECLARE_EXPORTED_LIST( wxSizerItem
, wxSizerItemList
);
264 //---------------------------------------------------------------------------
266 //---------------------------------------------------------------------------
268 class WXDLLEXPORT wxSizer
: public wxObject
, public wxClientDataContainer
274 // methods for adding elements to the sizer: there are Add/Insert/Prepend
275 // overloads for each of window/sizer/spacer/wxSizerItem
276 inline void Add( wxWindow
*window
,
280 wxObject
* userData
= NULL
);
281 inline void Add( wxSizer
*sizer
,
285 wxObject
* userData
= NULL
);
286 inline void Add( int width
,
291 wxObject
* userData
= NULL
);
292 inline void Add( wxWindow
*window
, const wxSizerFlags
& flags
);
293 inline void Add( wxSizer
*sizer
, const wxSizerFlags
& flags
);
294 inline void Add( wxSizerItem
*item
);
296 inline void AddSpacer(int size
);
297 inline void AddStretchSpacer(int prop
= 1);
299 inline void Insert( size_t index
,
304 wxObject
* userData
= NULL
);
305 inline void Insert( size_t index
,
310 wxObject
* userData
= NULL
);
311 inline void Insert( size_t index
,
317 wxObject
* userData
= NULL
);
318 inline void Insert( size_t index
,
320 const wxSizerFlags
& flags
);
321 inline void Insert( size_t index
,
323 const wxSizerFlags
& flags
);
324 virtual void Insert( size_t index
, wxSizerItem
*item
);
326 inline void InsertSpacer(size_t index
, int size
);
327 inline void InsertStretchSpacer(size_t index
, int prop
= 1);
329 inline void Prepend( wxWindow
*window
,
333 wxObject
* userData
= NULL
);
334 inline void Prepend( wxSizer
*sizer
,
338 wxObject
* userData
= NULL
);
339 inline void Prepend( int width
,
344 wxObject
* userData
= NULL
);
345 inline void Prepend( wxWindow
*window
, const wxSizerFlags
& flags
);
346 inline void Prepend( wxSizer
*sizer
, const wxSizerFlags
& flags
);
347 inline void Prepend( wxSizerItem
*item
);
349 inline void PrependSpacer(int size
);
350 inline void PrependStretchSpacer(int prop
= 1);
353 // Deprecated in 2.6 since historically it does not delete the window,
354 // use Detach instead.
355 wxDEPRECATED( virtual bool Remove( wxWindow
*window
) );
356 virtual bool Remove( wxSizer
*sizer
);
357 virtual bool Remove( int index
);
359 virtual bool Detach( wxWindow
*window
);
360 virtual bool Detach( wxSizer
*sizer
);
361 virtual bool Detach( int index
);
363 virtual void Clear( bool delete_windows
= false );
364 virtual void DeleteWindows();
366 void SetMinSize( int width
, int height
)
367 { DoSetMinSize( width
, height
); }
368 void SetMinSize( wxSize size
)
369 { DoSetMinSize( size
.x
, size
.y
); }
371 /* Searches recursively */
372 bool SetItemMinSize( wxWindow
*window
, int width
, int height
)
373 { return DoSetItemMinSize( window
, width
, height
); }
374 bool SetItemMinSize( wxWindow
*window
, wxSize size
)
375 { return DoSetItemMinSize( window
, size
.x
, size
.y
); }
377 /* Searches recursively */
378 bool SetItemMinSize( wxSizer
*sizer
, int width
, int height
)
379 { return DoSetItemMinSize( sizer
, width
, height
); }
380 bool SetItemMinSize( wxSizer
*sizer
, wxSize size
)
381 { return DoSetItemMinSize( sizer
, size
.x
, size
.y
); }
383 bool SetItemMinSize( size_t index
, int width
, int height
)
384 { return DoSetItemMinSize( index
, width
, height
); }
385 bool SetItemMinSize( size_t index
, wxSize size
)
386 { return DoSetItemMinSize( index
, size
.x
, size
.y
); }
388 wxSize
GetSize() const
390 wxPoint
GetPosition() const
391 { return m_position
; }
393 /* Calculate the minimal size or return m_minSize if bigger. */
396 virtual void RecalcSizes() = 0;
397 virtual wxSize
CalcMin() = 0;
399 virtual void Layout();
401 wxSize
Fit( wxWindow
*window
);
402 void FitInside( wxWindow
*window
);
403 void SetSizeHints( wxWindow
*window
);
404 void SetVirtualSizeHints( wxWindow
*window
);
406 wxSizerItemList
& GetChildren()
407 { return m_children
; }
409 void SetDimension( int x
, int y
, int width
, int height
);
411 wxSizerItem
* GetItem( wxWindow
*window
, bool recursive
= false );
412 wxSizerItem
* GetItem( wxSizer
*sizer
, bool recursive
= false );
413 wxSizerItem
* GetItem( size_t index
);
415 // Manage whether individual scene items are considered
416 // in the layout calculations or not.
417 bool Show( wxWindow
*window
, bool show
= true, bool recursive
= false );
418 bool Show( wxSizer
*sizer
, bool show
= true, bool recursive
= false );
419 bool Show( size_t index
, bool show
= true );
421 bool Hide( wxSizer
*sizer
, bool recursive
= false )
422 { return Show( sizer
, false, recursive
); }
423 bool Hide( wxWindow
*window
, bool recursive
= false )
424 { return Show( window
, false, recursive
); }
425 bool Hide( size_t index
)
426 { return Show( index
, false ); }
428 bool IsShown( wxWindow
*window
) const;
429 bool IsShown( wxSizer
*sizer
) const;
430 bool IsShown( size_t index
) const;
432 // Recursively call wxWindow::Show () on all sizer items.
433 virtual void ShowItems (bool show
);
439 wxSizerItemList m_children
;
441 wxSize
GetMaxWindowSize( wxWindow
*window
) const;
442 wxSize
GetMinWindowSize( wxWindow
*window
);
443 wxSize
GetMaxClientSize( wxWindow
*window
) const;
444 wxSize
GetMinClientSize( wxWindow
*window
);
445 wxSize
FitSize( wxWindow
*window
);
446 wxSize
VirtualFitSize( wxWindow
*window
);
448 virtual void DoSetMinSize( int width
, int height
);
449 virtual bool DoSetItemMinSize( wxWindow
*window
, int width
, int height
);
450 virtual bool DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
);
451 virtual bool DoSetItemMinSize( size_t index
, int width
, int height
);
454 DECLARE_CLASS(wxSizer
)
457 //---------------------------------------------------------------------------
459 //---------------------------------------------------------------------------
461 class WXDLLEXPORT wxGridSizer
: public wxSizer
464 wxGridSizer( int rows
, int cols
, int vgap
, int hgap
);
465 wxGridSizer( int cols
, int vgap
= 0, int hgap
= 0 );
467 virtual void RecalcSizes();
468 virtual wxSize
CalcMin();
470 void SetCols( int cols
) { m_cols
= cols
; }
471 void SetRows( int rows
) { m_rows
= rows
; }
472 void SetVGap( int gap
) { m_vgap
= gap
; }
473 void SetHGap( int gap
) { m_hgap
= gap
; }
474 int GetCols() const { return m_cols
; }
475 int GetRows() const { return m_rows
; }
476 int GetVGap() const { return m_vgap
; }
477 int GetHGap() const { return m_hgap
; }
485 // return the number of total items and the number of columns and rows
486 int CalcRowsCols(int& rows
, int& cols
) const;
488 void SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
);
491 DECLARE_CLASS(wxGridSizer
)
494 //---------------------------------------------------------------------------
496 //---------------------------------------------------------------------------
498 // the bevaiour for resizing wxFlexGridSizer cells in the "non-flexible"
500 enum wxFlexSizerGrowMode
502 // don't resize the cells in non-flexible direction at all
503 wxFLEX_GROWMODE_NONE
,
505 // uniformly resize only the specified ones (default)
506 wxFLEX_GROWMODE_SPECIFIED
,
508 // uniformly resize all cells
512 class WXDLLEXPORT wxFlexGridSizer
: public wxGridSizer
516 wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
);
517 wxFlexGridSizer( int cols
, int vgap
= 0, int hgap
= 0 );
518 virtual ~wxFlexGridSizer();
521 // set the rows/columns which will grow (the others will remain of the
522 // constant initial size)
523 void AddGrowableRow( size_t idx
, int proportion
= 0 );
524 void RemoveGrowableRow( size_t idx
);
525 void AddGrowableCol( size_t idx
, int proportion
= 0 );
526 void RemoveGrowableCol( size_t idx
);
529 // the sizer cells may grow in both directions, not grow at all or only
530 // grow in one direction but not the other
532 // the direction may be wxVERTICAL, wxHORIZONTAL or wxBOTH (default)
533 void SetFlexibleDirection(int direction
) { m_flexDirection
= direction
; }
534 int GetFlexibleDirection() const { return m_flexDirection
; }
536 // note that the grow mode only applies to the direction which is not
538 void SetNonFlexibleGrowMode(wxFlexSizerGrowMode mode
) { m_growMode
= mode
; }
539 wxFlexSizerGrowMode
GetNonFlexibleGrowMode() const { return m_growMode
; }
541 // Read-only access to the row heights and col widths arrays
542 const wxArrayInt
& GetRowHeights() const { return m_rowHeights
; }
543 const wxArrayInt
& GetColWidths() const { return m_colWidths
; }
546 virtual void RecalcSizes();
547 virtual wxSize
CalcMin();
550 void AdjustForFlexDirection();
551 void AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
552 int nrows
, int ncols
);
554 // the heights/widths of all rows/columns
555 wxArrayInt m_rowHeights
,
558 // indices of the growable columns and rows
559 wxArrayInt m_growableRows
,
562 // proportion values of the corresponding growable rows and columns
563 wxArrayInt m_growableRowsProportions
,
564 m_growableColsProportions
;
566 // parameters describing whether the growable cells should be resized in
567 // both directions or only one
569 wxFlexSizerGrowMode m_growMode
;
571 // saves CalcMin result to optimize RecalcSizes
572 wxSize m_calculatedMinSize
;
575 DECLARE_CLASS(wxFlexGridSizer
)
576 DECLARE_NO_COPY_CLASS(wxFlexGridSizer
)
579 //---------------------------------------------------------------------------
581 //---------------------------------------------------------------------------
583 class WXDLLEXPORT wxBoxSizer
: public wxSizer
586 wxBoxSizer( int orient
);
591 int GetOrientation() const
594 void SetOrientation(int orient
)
595 { m_orient
= orient
; }
606 DECLARE_CLASS(wxBoxSizer
)
609 //---------------------------------------------------------------------------
611 //---------------------------------------------------------------------------
615 class WXDLLEXPORT wxStaticBox
;
617 class WXDLLEXPORT wxStaticBoxSizer
: public wxBoxSizer
620 wxStaticBoxSizer( wxStaticBox
*box
, int orient
);
625 wxStaticBox
*GetStaticBox() const
626 { return m_staticBox
; }
628 // override to hide/show the static box as well
629 virtual void ShowItems (bool show
);
632 wxStaticBox
*m_staticBox
;
635 DECLARE_CLASS(wxStaticBoxSizer
)
636 DECLARE_NO_COPY_CLASS(wxStaticBoxSizer
)
639 #endif // wxUSE_STATBOX
642 #if WXWIN_COMPATIBILITY_2_4
643 // NB: wxBookCtrlSizer and wxNotebookSizer are deprecated, they
644 // don't do anything. wxBookCtrl::DoGetBestSize does the job now.
646 // ----------------------------------------------------------------------------
648 // ----------------------------------------------------------------------------
652 // this sizer works with wxNotebook/wxListbook/... and sizes the control to
654 class WXDLLEXPORT wxBookCtrl
;
656 class WXDLLEXPORT wxBookCtrlSizer
: public wxSizer
659 wxDEPRECATED( wxBookCtrlSizer(wxBookCtrl
*bookctrl
) );
661 wxBookCtrl
*GetControl() const { return m_bookctrl
; }
663 virtual void RecalcSizes();
664 virtual wxSize
CalcMin();
667 // this protected ctor lets us mark the real one above as deprecated
668 // and still have warning-free build of the library itself:
671 wxBookCtrl
*m_bookctrl
;
674 DECLARE_CLASS(wxBookCtrlSizer
)
675 DECLARE_NO_COPY_CLASS(wxBookCtrlSizer
)
681 // before wxBookCtrl we only had wxNotebookSizer, keep it for backwards
683 class WXDLLEXPORT wxNotebook
;
685 class WXDLLEXPORT wxNotebookSizer
: public wxBookCtrlSizer
688 wxDEPRECATED( wxNotebookSizer(wxNotebook
*nb
) );
690 wxNotebook
*GetNotebook() const { return (wxNotebook
*)m_bookctrl
; }
693 DECLARE_CLASS(wxNotebookSizer
)
694 DECLARE_NO_COPY_CLASS(wxNotebookSizer
)
697 #endif // wxUSE_NOTEBOOK
699 #endif // wxUSE_BOOKCTRL
701 #endif // WXWIN_COMPATIBILITY_2_4
703 // ----------------------------------------------------------------------------
704 // inline functions implementation
705 // ----------------------------------------------------------------------------
708 wxSizer::Add( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
710 Add( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
714 wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
716 Add( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
720 wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
722 Add( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
726 wxSizer::Add( wxWindow
*window
, const wxSizerFlags
& flags
)
728 Add( new wxSizerItem(window
, flags
) );
732 wxSizer::Add( wxSizer
*sizer
, const wxSizerFlags
& flags
)
734 Add( new wxSizerItem(sizer
, flags
) );
738 wxSizer::Add( wxSizerItem
*item
)
740 Insert( m_children
.GetCount(), item
);
744 wxSizer::AddSpacer(int size
)
750 wxSizer::AddStretchSpacer(int prop
)
756 wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
758 Prepend( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
762 wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
764 Prepend( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
768 wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
770 Prepend( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
774 wxSizer::Prepend( wxSizerItem
*item
)
780 wxSizer::PrependSpacer(int size
)
786 wxSizer::PrependStretchSpacer(int prop
)
792 wxSizer::Prepend( wxWindow
*window
, const wxSizerFlags
& flags
)
794 Prepend( new wxSizerItem(window
, flags
) );
798 wxSizer::Prepend( wxSizer
*sizer
, const wxSizerFlags
& flags
)
800 Prepend( new wxSizerItem(sizer
, flags
) );
804 wxSizer::Insert( size_t index
,
811 Insert( index
, new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
815 wxSizer::Insert( size_t index
,
822 Insert( index
, new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
826 wxSizer::Insert( size_t index
,
834 Insert( index
, new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
838 wxSizer::Insert( size_t index
, wxWindow
*window
, const wxSizerFlags
& flags
)
840 Insert( index
, new wxSizerItem(window
, flags
) );
844 wxSizer::Insert( size_t index
, wxSizer
*sizer
, const wxSizerFlags
& flags
)
846 Insert( index
, new wxSizerItem(sizer
, flags
) );
850 wxSizer::InsertSpacer(size_t index
, int size
)
852 Insert(index
, size
, size
);
856 wxSizer::InsertStretchSpacer(size_t index
, int prop
)
858 Insert(index
, 0, 0, prop
);
862 #endif // __WXSIZER_H__