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 Insert( size_t index
,
206 wxObject
* userData
= NULL
);
207 virtual void Insert( size_t index
,
212 wxObject
* userData
= NULL
);
213 virtual void Insert( size_t index
,
219 wxObject
* userData
= NULL
);
220 virtual void Insert( size_t index
,
223 virtual void Prepend( wxWindow
*window
,
227 wxObject
* userData
= NULL
);
228 virtual void Prepend( wxSizer
*sizer
,
232 wxObject
* userData
= NULL
);
233 virtual void Prepend( int width
,
238 wxObject
* userData
= NULL
);
239 virtual void Prepend( wxSizerItem
*item
);
241 // Deprecated in 2.6 since historically it does not delete the window,
242 // use Detach instead.
243 wxDEPRECATED( virtual bool Remove( wxWindow
*window
) );
244 virtual bool Remove( wxSizer
*sizer
);
245 virtual bool Remove( int index
);
247 virtual bool Detach( wxWindow
*window
);
248 virtual bool Detach( wxSizer
*sizer
);
249 virtual bool Detach( int index
);
251 virtual void Clear( bool delete_windows
= false );
252 virtual void DeleteWindows();
254 void SetMinSize( int width
, int height
)
255 { DoSetMinSize( width
, height
); }
256 void SetMinSize( wxSize size
)
257 { DoSetMinSize( size
.x
, size
.y
); }
259 /* Searches recursively */
260 bool SetItemMinSize( wxWindow
*window
, int width
, int height
)
261 { return DoSetItemMinSize( window
, width
, height
); }
262 bool SetItemMinSize( wxWindow
*window
, wxSize size
)
263 { return DoSetItemMinSize( window
, size
.x
, size
.y
); }
265 /* Searches recursively */
266 bool SetItemMinSize( wxSizer
*sizer
, int width
, int height
)
267 { return DoSetItemMinSize( sizer
, width
, height
); }
268 bool SetItemMinSize( wxSizer
*sizer
, wxSize size
)
269 { return DoSetItemMinSize( sizer
, size
.x
, size
.y
); }
271 bool SetItemMinSize( size_t index
, int width
, int height
)
272 { return DoSetItemMinSize( index
, width
, height
); }
273 bool SetItemMinSize( size_t index
, wxSize size
)
274 { return DoSetItemMinSize( index
, size
.x
, size
.y
); }
276 wxSize
GetSize() const
278 wxPoint
GetPosition() const
279 { return m_position
; }
281 /* Calculate the minimal size or return m_minSize if bigger. */
284 virtual void RecalcSizes() = 0;
285 virtual wxSize
CalcMin() = 0;
287 virtual void Layout();
289 wxSize
Fit( wxWindow
*window
);
290 void FitInside( wxWindow
*window
);
291 void SetSizeHints( wxWindow
*window
);
292 void SetVirtualSizeHints( wxWindow
*window
);
294 wxSizerItemList
& GetChildren()
295 { return m_children
; }
297 void SetDimension( int x
, int y
, int width
, int height
);
299 // Manage whether individual scene items are considered
300 // in the layout calculations or not.
301 void Show( wxWindow
*window
, bool show
= true );
302 void Show( wxSizer
*sizer
, bool show
= true );
303 void Show( size_t index
, bool show
= true );
305 void Hide( wxSizer
*sizer
)
306 { Show( sizer
, false ); }
307 void Hide( wxWindow
*window
)
308 { Show( window
, false ); }
309 void Hide( size_t index
)
310 { Show( index
, false ); }
312 bool IsShown( wxWindow
*window
) const;
313 bool IsShown( wxSizer
*sizer
) const;
314 bool IsShown( size_t index
) const;
316 // Recursively call wxWindow::Show () on all sizer items.
317 virtual void ShowItems (bool show
);
323 wxSizerItemList m_children
;
325 wxSize
GetMaxWindowSize( wxWindow
*window
) const;
326 wxSize
GetMinWindowSize( wxWindow
*window
);
327 wxSize
GetMaxClientSize( wxWindow
*window
) const;
328 wxSize
GetMinClientSize( wxWindow
*window
);
329 wxSize
FitSize( wxWindow
*window
);
330 wxSize
VirtualFitSize( wxWindow
*window
);
332 virtual void DoSetMinSize( int width
, int height
);
333 virtual bool DoSetItemMinSize( wxWindow
*window
, int width
, int height
);
334 virtual bool DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
);
335 virtual bool DoSetItemMinSize( size_t index
, int width
, int height
);
338 DECLARE_CLASS(wxSizer
)
341 //---------------------------------------------------------------------------
343 //---------------------------------------------------------------------------
345 class WXDLLEXPORT wxGridSizer
: public wxSizer
348 wxGridSizer( int rows
, int cols
, int vgap
, int hgap
);
349 wxGridSizer( int cols
, int vgap
= 0, int hgap
= 0 );
351 virtual void RecalcSizes();
352 virtual wxSize
CalcMin();
354 void SetCols( int cols
) { m_cols
= cols
; }
355 void SetRows( int rows
) { m_rows
= rows
; }
356 void SetVGap( int gap
) { m_vgap
= gap
; }
357 void SetHGap( int gap
) { m_hgap
= gap
; }
358 int GetCols() const { return m_cols
; }
359 int GetRows() const { return m_rows
; }
360 int GetVGap() const { return m_vgap
; }
361 int GetHGap() const { return m_hgap
; }
369 // return the number of total items and the number of columns and rows
370 int CalcRowsCols(int& rows
, int& cols
) const;
372 void SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
);
375 DECLARE_CLASS(wxGridSizer
)
378 //---------------------------------------------------------------------------
380 //---------------------------------------------------------------------------
382 // the bevaiour for resizing wxFlexGridSizer cells in the "non-flexible"
384 enum wxFlexSizerGrowMode
386 // don't resize the cells in non-flexible direction at all
387 wxFLEX_GROWMODE_NONE
,
389 // uniformly resize only the specified ones (default)
390 wxFLEX_GROWMODE_SPECIFIED
,
392 // uniformly resize all cells
396 class WXDLLEXPORT wxFlexGridSizer
: public wxGridSizer
400 wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
);
401 wxFlexGridSizer( int cols
, int vgap
= 0, int hgap
= 0 );
402 virtual ~wxFlexGridSizer();
405 // set the rows/columns which will grow (the others will remain of the
406 // constant initial size)
407 void AddGrowableRow( size_t idx
, int proportion
= 0 );
408 void RemoveGrowableRow( size_t idx
);
409 void AddGrowableCol( size_t idx
, int proportion
= 0 );
410 void RemoveGrowableCol( size_t idx
);
413 // the sizer cells may grow in both directions, not grow at all or only
414 // grow in one direction but not the other
416 // the direction may be wxVERTICAL, wxHORIZONTAL or wxBOTH (default)
417 void SetFlexibleDirection(int direction
) { m_flexDirection
= direction
; }
418 int GetFlexibleDirection() const { return m_flexDirection
; }
420 // note that the grow mode only applies to the direction which is not
422 void SetNonFlexibleGrowMode(wxFlexSizerGrowMode mode
) { m_growMode
= mode
; }
423 wxFlexSizerGrowMode
GetNonFlexibleGrowMode() const { return m_growMode
; }
425 // Read-only access to the row heights and col widths arrays
426 const wxArrayInt
& GetRowHeights() const { return m_rowHeights
; }
427 const wxArrayInt
& GetColWidths() const { return m_colWidths
; }
430 virtual void RecalcSizes();
431 virtual wxSize
CalcMin();
434 void AdjustForFlexDirection();
435 void AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
436 int nrows
, int ncols
);
438 // the heights/widths of all rows/columns
439 wxArrayInt m_rowHeights
,
442 // indices of the growable columns and rows
443 wxArrayInt m_growableRows
,
446 // proportion values of the corresponding growable rows and columns
447 wxArrayInt m_growableRowsProportions
,
448 m_growableColsProportions
;
450 // parameters describing whether the growable cells should be resized in
451 // both directions or only one
453 wxFlexSizerGrowMode m_growMode
;
455 // saves CalcMin result to optimize RecalcSizes
456 wxSize m_calculatedMinSize
;
459 DECLARE_CLASS(wxFlexGridSizer
)
460 DECLARE_NO_COPY_CLASS(wxFlexGridSizer
)
463 //---------------------------------------------------------------------------
465 //---------------------------------------------------------------------------
467 class WXDLLEXPORT wxBoxSizer
: public wxSizer
470 wxBoxSizer( int orient
);
475 int GetOrientation() const
478 void SetOrientation(int orient
)
479 { m_orient
= orient
; }
490 DECLARE_CLASS(wxBoxSizer
)
493 //---------------------------------------------------------------------------
495 //---------------------------------------------------------------------------
499 class WXDLLEXPORT wxStaticBox
;
501 class WXDLLEXPORT wxStaticBoxSizer
: public wxBoxSizer
504 wxStaticBoxSizer( wxStaticBox
*box
, int orient
);
509 wxStaticBox
*GetStaticBox() const
510 { return m_staticBox
; }
512 // override to hide/show the static box as well
513 virtual void ShowItems (bool show
);
516 wxStaticBox
*m_staticBox
;
519 DECLARE_CLASS(wxStaticBoxSizer
)
520 DECLARE_NO_COPY_CLASS(wxStaticBoxSizer
)
523 #endif // wxUSE_STATBOX
526 #if WXWIN_COMPATIBILITY_2_4
527 // NB: wxBookCtrlSizer and wxNotebookSizer are deprecated, they
528 // don't do anything. wxBookCtrl::DoGetBestSize does the job now.
530 // ----------------------------------------------------------------------------
532 // ----------------------------------------------------------------------------
536 // this sizer works with wxNotebook/wxListbook/... and sizes the control to
538 class WXDLLEXPORT wxBookCtrl
;
540 class WXDLLEXPORT wxBookCtrlSizer
: public wxSizer
543 wxDEPRECATED( wxBookCtrlSizer(wxBookCtrl
*bookctrl
) );
545 wxBookCtrl
*GetControl() const { return m_bookctrl
; }
547 virtual void RecalcSizes();
548 virtual wxSize
CalcMin();
551 // this protected ctor lets us mark the real one above as deprecated
552 // and still has warning-free build of the library itself:
555 wxBookCtrl
*m_bookctrl
;
558 DECLARE_CLASS(wxBookCtrlSizer
)
559 DECLARE_NO_COPY_CLASS(wxBookCtrlSizer
)
565 // before wxBookCtrl we only had wxNotebookSizer, keep it for backwards
567 class WXDLLEXPORT wxNotebook
;
569 class WXDLLEXPORT wxNotebookSizer
: public wxBookCtrlSizer
572 wxDEPRECATED( wxNotebookSizer(wxNotebook
*nb
) );
574 wxNotebook
*GetNotebook() const { return (wxNotebook
*)m_bookctrl
; }
577 DECLARE_CLASS(wxNotebookSizer
)
578 DECLARE_NO_COPY_CLASS(wxNotebookSizer
)
581 #endif // wxUSE_NOTEBOOK
583 #endif // wxUSE_BOOKCTRL
585 #endif // WXWIN_COMPATIBILITY_2_4
588 #endif // __WXSIZER_H__