1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: provide wxSizer class for layout
4 // Author: Robert Roebling and Robin Dunn
5 // Modified by: Ron Lee
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"
25 //---------------------------------------------------------------------------
27 //---------------------------------------------------------------------------
29 class WXDLLEXPORT wxSizerItem
;
30 class WXDLLEXPORT wxSizer
;
31 class WXDLLEXPORT wxBoxSizer
;
33 //---------------------------------------------------------------------------
35 //---------------------------------------------------------------------------
37 class WXDLLEXPORT wxSizerItem
: public wxObject
41 wxSizerItem( int width
,
49 wxSizerItem( wxWindow
*window
,
56 wxSizerItem( wxSizer
*sizer
,
63 virtual ~wxSizerItem();
65 virtual void DeleteWindows();
67 // Enable deleting the SizerItem without destroying the contained sizer.
71 virtual wxSize
GetSize() const;
72 virtual wxSize
CalcMin();
73 virtual void SetDimension( wxPoint pos
, wxSize size
);
75 wxSize
GetMinSize() const
77 wxSize
GetMinSizeWithBorder() const;
79 void SetMinSize(const wxSize
& size
)
81 if (IsWindow()) m_window
->SetMinSize(size
);
84 void SetMinSize( int x
, int y
)
85 { SetMinSize(wxSize(x
, y
)); }
86 void SetInitSize( int x
, int y
)
87 { SetMinSize(wxSize(x
, y
)); }
89 void SetRatio( int width
, int height
)
90 // if either of dimensions is zero, ratio is assumed to be 1
91 // to avoid "divide by zero" errors
92 { m_ratio
= (width
&& height
) ? ((float) width
/ (float) height
) : 1; }
93 void SetRatio( wxSize size
)
94 { m_ratio
= (size
.x
&& size
.y
) ? ((float) size
.x
/ (float) size
.y
) : 1; }
95 void SetRatio( float ratio
)
97 float GetRatio() const
100 bool IsWindow() const;
101 bool IsSizer() const;
102 bool IsSpacer() const;
104 // Deprecated in 2.6, use {G,S}etProportion instead.
105 wxDEPRECATED( void SetOption( int option
) );
106 wxDEPRECATED( int GetOption() const );
108 void SetProportion( int proportion
)
109 { m_proportion
= proportion
; }
110 int GetProportion() const
111 { return m_proportion
; }
112 void SetFlag( int flag
)
116 void SetBorder( int border
)
117 { m_border
= border
; }
118 int GetBorder() const
121 wxWindow
*GetWindow() const
123 void SetWindow( wxWindow
*window
)
124 { m_window
= window
; m_minSize
= window
->GetSize(); }
125 wxSizer
*GetSizer() const
127 void SetSizer( wxSizer
*sizer
)
129 const wxSize
&GetSpacer() const
131 void SetSpacer( const wxSize
&size
)
132 { m_size
= size
; m_minSize
= size
; }
134 void Show ( bool show
);
138 wxObject
* GetUserData() const
139 { return m_userData
; }
140 wxPoint
GetPosition() const
153 // If true, then this item is considered in the layout
154 // calculation. Otherwise, it is skipped over.
157 // Aspect ratio can always be calculated from m_size,
158 // but this would cause precision loss when the window
159 // is shrunk. It is safer to preserve the initial value.
162 wxObject
*m_userData
;
165 DECLARE_CLASS(wxSizerItem
)
166 DECLARE_NO_COPY_CLASS(wxSizerItem
)
169 WX_DECLARE_EXPORTED_LIST( wxSizerItem
, wxSizerItemList
);
172 //---------------------------------------------------------------------------
174 //---------------------------------------------------------------------------
176 class WXDLLEXPORT wxSizer
: public wxObject
, public wxClientDataContainer
182 /* These should be called Append() really. */
183 virtual void Add( wxWindow
*window
,
187 wxObject
* userData
= NULL
);
188 virtual void Add( wxSizer
*sizer
,
192 wxObject
* userData
= NULL
);
193 virtual void Add( int width
,
198 wxObject
* userData
= NULL
);
199 virtual void Add( wxSizerItem
*item
);
201 virtual void AddSpacer(int size
);
202 virtual void AddStretchSpacer(int prop
= 1);
204 virtual void Insert( size_t index
,
209 wxObject
* userData
= NULL
);
210 virtual void Insert( size_t index
,
215 wxObject
* userData
= NULL
);
216 virtual void Insert( size_t index
,
222 wxObject
* userData
= NULL
);
223 virtual void Insert( size_t index
,
226 virtual void InsertSpacer(size_t index
, int size
);
227 virtual void InsertStretchSpacer(size_t index
, int prop
= 1);
229 virtual void Prepend( wxWindow
*window
,
233 wxObject
* userData
= NULL
);
234 virtual void Prepend( wxSizer
*sizer
,
238 wxObject
* userData
= NULL
);
239 virtual void Prepend( int width
,
244 wxObject
* userData
= NULL
);
245 virtual void Prepend( wxSizerItem
*item
);
247 virtual void PrependSpacer(int size
);
248 virtual void PrependStretchSpacer(int prop
= 1);
250 // Deprecated in 2.6 since historically it does not delete the window,
251 // use Detach instead.
252 wxDEPRECATED( virtual bool Remove( wxWindow
*window
) );
253 virtual bool Remove( wxSizer
*sizer
);
254 virtual bool Remove( int index
);
256 virtual bool Detach( wxWindow
*window
);
257 virtual bool Detach( wxSizer
*sizer
);
258 virtual bool Detach( int index
);
260 virtual void Clear( bool delete_windows
= false );
261 virtual void DeleteWindows();
263 void SetMinSize( int width
, int height
)
264 { DoSetMinSize( width
, height
); }
265 void SetMinSize( wxSize size
)
266 { DoSetMinSize( size
.x
, size
.y
); }
268 /* Searches recursively */
269 bool SetItemMinSize( wxWindow
*window
, int width
, int height
)
270 { return DoSetItemMinSize( window
, width
, height
); }
271 bool SetItemMinSize( wxWindow
*window
, wxSize size
)
272 { return DoSetItemMinSize( window
, size
.x
, size
.y
); }
274 /* Searches recursively */
275 bool SetItemMinSize( wxSizer
*sizer
, int width
, int height
)
276 { return DoSetItemMinSize( sizer
, width
, height
); }
277 bool SetItemMinSize( wxSizer
*sizer
, wxSize size
)
278 { return DoSetItemMinSize( sizer
, size
.x
, size
.y
); }
280 bool SetItemMinSize( size_t index
, int width
, int height
)
281 { return DoSetItemMinSize( index
, width
, height
); }
282 bool SetItemMinSize( size_t index
, wxSize size
)
283 { return DoSetItemMinSize( index
, size
.x
, size
.y
); }
285 wxSize
GetSize() const
287 wxPoint
GetPosition() const
288 { return m_position
; }
290 /* Calculate the minimal size or return m_minSize if bigger. */
293 virtual void RecalcSizes() = 0;
294 virtual wxSize
CalcMin() = 0;
296 virtual void Layout();
298 wxSize
Fit( wxWindow
*window
);
299 void FitInside( wxWindow
*window
);
300 void SetSizeHints( wxWindow
*window
);
301 void SetVirtualSizeHints( wxWindow
*window
);
303 wxSizerItemList
& GetChildren()
304 { return m_children
; }
306 void SetDimension( int x
, int y
, int width
, int height
);
308 // Manage whether individual scene items are considered
309 // in the layout calculations or not.
310 void Show( wxWindow
*window
, bool show
= true );
311 void Show( wxSizer
*sizer
, bool show
= true );
312 void Show( size_t index
, bool show
= true );
314 void Hide( wxSizer
*sizer
)
315 { Show( sizer
, false ); }
316 void Hide( wxWindow
*window
)
317 { Show( window
, false ); }
318 void Hide( size_t index
)
319 { Show( index
, false ); }
321 bool IsShown( wxWindow
*window
) const;
322 bool IsShown( wxSizer
*sizer
) const;
323 bool IsShown( size_t index
) const;
325 // Recursively call wxWindow::Show () on all sizer items.
326 virtual void ShowItems (bool show
);
332 wxSizerItemList m_children
;
334 wxSize
GetMaxWindowSize( wxWindow
*window
) const;
335 wxSize
GetMinWindowSize( wxWindow
*window
);
336 wxSize
GetMaxClientSize( wxWindow
*window
) const;
337 wxSize
GetMinClientSize( wxWindow
*window
);
338 wxSize
FitSize( wxWindow
*window
);
339 wxSize
VirtualFitSize( wxWindow
*window
);
341 virtual void DoSetMinSize( int width
, int height
);
342 virtual bool DoSetItemMinSize( wxWindow
*window
, int width
, int height
);
343 virtual bool DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
);
344 virtual bool DoSetItemMinSize( size_t index
, int width
, int height
);
347 DECLARE_CLASS(wxSizer
)
350 //---------------------------------------------------------------------------
352 //---------------------------------------------------------------------------
354 class WXDLLEXPORT wxGridSizer
: public wxSizer
357 wxGridSizer( int rows
, int cols
, int vgap
, int hgap
);
358 wxGridSizer( int cols
, int vgap
= 0, int hgap
= 0 );
360 virtual void RecalcSizes();
361 virtual wxSize
CalcMin();
363 void SetCols( int cols
) { m_cols
= cols
; }
364 void SetRows( int rows
) { m_rows
= rows
; }
365 void SetVGap( int gap
) { m_vgap
= gap
; }
366 void SetHGap( int gap
) { m_hgap
= gap
; }
367 int GetCols() const { return m_cols
; }
368 int GetRows() const { return m_rows
; }
369 int GetVGap() const { return m_vgap
; }
370 int GetHGap() const { return m_hgap
; }
378 // return the number of total items and the number of columns and rows
379 int CalcRowsCols(int& rows
, int& cols
) const;
381 void SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
);
384 DECLARE_CLASS(wxGridSizer
)
387 //---------------------------------------------------------------------------
389 //---------------------------------------------------------------------------
391 // the bevaiour for resizing wxFlexGridSizer cells in the "non-flexible"
393 enum wxFlexSizerGrowMode
395 // don't resize the cells in non-flexible direction at all
396 wxFLEX_GROWMODE_NONE
,
398 // uniformly resize only the specified ones (default)
399 wxFLEX_GROWMODE_SPECIFIED
,
401 // uniformly resize all cells
405 class WXDLLEXPORT wxFlexGridSizer
: public wxGridSizer
409 wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
);
410 wxFlexGridSizer( int cols
, int vgap
= 0, int hgap
= 0 );
411 virtual ~wxFlexGridSizer();
414 // set the rows/columns which will grow (the others will remain of the
415 // constant initial size)
416 void AddGrowableRow( size_t idx
, int proportion
= 0 );
417 void RemoveGrowableRow( size_t idx
);
418 void AddGrowableCol( size_t idx
, int proportion
= 0 );
419 void RemoveGrowableCol( size_t idx
);
422 // the sizer cells may grow in both directions, not grow at all or only
423 // grow in one direction but not the other
425 // the direction may be wxVERTICAL, wxHORIZONTAL or wxBOTH (default)
426 void SetFlexibleDirection(int direction
) { m_flexDirection
= direction
; }
427 int GetFlexibleDirection() const { return m_flexDirection
; }
429 // note that the grow mode only applies to the direction which is not
431 void SetNonFlexibleGrowMode(wxFlexSizerGrowMode mode
) { m_growMode
= mode
; }
432 wxFlexSizerGrowMode
GetNonFlexibleGrowMode() const { return m_growMode
; }
434 // Read-only access to the row heights and col widths arrays
435 const wxArrayInt
& GetRowHeights() const { return m_rowHeights
; }
436 const wxArrayInt
& GetColWidths() const { return m_colWidths
; }
439 virtual void RecalcSizes();
440 virtual wxSize
CalcMin();
443 void AdjustForFlexDirection();
444 void AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
445 int nrows
, int ncols
);
447 // the heights/widths of all rows/columns
448 wxArrayInt m_rowHeights
,
451 // indices of the growable columns and rows
452 wxArrayInt m_growableRows
,
455 // proportion values of the corresponding growable rows and columns
456 wxArrayInt m_growableRowsProportions
,
457 m_growableColsProportions
;
459 // parameters describing whether the growable cells should be resized in
460 // both directions or only one
462 wxFlexSizerGrowMode m_growMode
;
464 // saves CalcMin result to optimize RecalcSizes
465 wxSize m_calculatedMinSize
;
468 DECLARE_CLASS(wxFlexGridSizer
)
469 DECLARE_NO_COPY_CLASS(wxFlexGridSizer
)
472 //---------------------------------------------------------------------------
474 //---------------------------------------------------------------------------
476 class WXDLLEXPORT wxBoxSizer
: public wxSizer
479 wxBoxSizer( int orient
);
484 int GetOrientation() const
487 void SetOrientation(int orient
)
488 { m_orient
= orient
; }
499 DECLARE_CLASS(wxBoxSizer
)
502 //---------------------------------------------------------------------------
504 //---------------------------------------------------------------------------
508 class WXDLLEXPORT wxStaticBox
;
510 class WXDLLEXPORT wxStaticBoxSizer
: public wxBoxSizer
513 wxStaticBoxSizer( wxStaticBox
*box
, int orient
);
518 wxStaticBox
*GetStaticBox() const
519 { return m_staticBox
; }
521 // override to hide/show the static box as well
522 virtual void ShowItems (bool show
);
525 wxStaticBox
*m_staticBox
;
528 DECLARE_CLASS(wxStaticBoxSizer
)
529 DECLARE_NO_COPY_CLASS(wxStaticBoxSizer
)
532 #endif // wxUSE_STATBOX
535 #if WXWIN_COMPATIBILITY_2_4
536 // NB: wxBookCtrlSizer and wxNotebookSizer are deprecated, they
537 // don't do anything. wxBookCtrl::DoGetBestSize does the job now.
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
545 // this sizer works with wxNotebook/wxListbook/... and sizes the control to
547 class WXDLLEXPORT wxBookCtrl
;
549 class WXDLLEXPORT wxBookCtrlSizer
: public wxSizer
552 wxDEPRECATED( wxBookCtrlSizer(wxBookCtrl
*bookctrl
) );
554 wxBookCtrl
*GetControl() const { return m_bookctrl
; }
556 virtual void RecalcSizes();
557 virtual wxSize
CalcMin();
560 // this protected ctor lets us mark the real one above as deprecated
561 // and still have warning-free build of the library itself:
564 wxBookCtrl
*m_bookctrl
;
567 DECLARE_CLASS(wxBookCtrlSizer
)
568 DECLARE_NO_COPY_CLASS(wxBookCtrlSizer
)
574 // before wxBookCtrl we only had wxNotebookSizer, keep it for backwards
576 class WXDLLEXPORT wxNotebook
;
578 class WXDLLEXPORT wxNotebookSizer
: public wxBookCtrlSizer
581 wxDEPRECATED( wxNotebookSizer(wxNotebook
*nb
) );
583 wxNotebook
*GetNotebook() const { return (wxNotebook
*)m_bookctrl
; }
586 DECLARE_CLASS(wxNotebookSizer
)
587 DECLARE_NO_COPY_CLASS(wxNotebookSizer
)
590 #endif // wxUSE_NOTEBOOK
592 #endif // wxUSE_BOOKCTRL
594 #endif // WXWIN_COMPATIBILITY_2_4
597 #endif // __WXSIZER_H__