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"
24 #include "wx/bookctrl.h"
26 //---------------------------------------------------------------------------
28 //---------------------------------------------------------------------------
30 class WXDLLEXPORT wxSizerItem
;
31 class WXDLLEXPORT wxSizer
;
32 class WXDLLEXPORT wxBoxSizer
;
34 //---------------------------------------------------------------------------
36 //---------------------------------------------------------------------------
38 class WXDLLEXPORT wxSizerItem
: public wxObject
42 wxSizerItem( int width
,
50 wxSizerItem( wxWindow
*window
,
57 wxSizerItem( wxSizer
*sizer
,
64 virtual ~wxSizerItem();
66 virtual void DeleteWindows();
68 // Enable deleting the SizerItem without destroying the contained sizer.
72 virtual wxSize
GetSize() const;
73 virtual wxSize
CalcMin();
74 virtual void SetDimension( wxPoint pos
, wxSize size
);
76 wxSize
GetMinSize() const
78 wxSize
GetMinSizeWithBorder() const;
80 void SetMinSize(const wxSize
& size
)
82 if (IsWindow()) m_window
->SetMinSize(size
);
85 void SetMinSize( int x
, int y
)
86 { SetMinSize(wxSize(x
, y
)); }
87 void SetInitSize( int x
, int y
)
88 { SetMinSize(wxSize(x
, y
)); }
90 void SetRatio( int width
, int height
)
91 // if either of dimensions is zero, ratio is assumed to be 1
92 // to avoid "divide by zero" errors
93 { m_ratio
= (width
&& height
) ? ((float) width
/ (float) height
) : 1; }
94 void SetRatio( wxSize size
)
95 { m_ratio
= (size
.x
&& size
.y
) ? ((float) size
.x
/ (float) size
.y
) : 1; }
96 void SetRatio( float ratio
)
98 float GetRatio() const
101 bool IsWindow() const;
102 bool IsSizer() const;
103 bool IsSpacer() const;
105 // Deprecated in 2.6, use {G,S}etProportion instead.
106 wxDEPRECATED( void SetOption( int option
) );
107 wxDEPRECATED( int GetOption() const );
109 void SetProportion( int proportion
)
110 { m_proportion
= proportion
; }
111 int GetProportion() const
112 { return m_proportion
; }
113 void SetFlag( int flag
)
117 void SetBorder( int border
)
118 { m_border
= border
; }
119 int GetBorder() const
122 wxWindow
*GetWindow() const
124 void SetWindow( wxWindow
*window
)
125 { m_window
= window
; m_minSize
= window
->GetSize(); }
126 wxSizer
*GetSizer() const
128 void SetSizer( wxSizer
*sizer
)
130 const wxSize
&GetSpacer() const
132 void SetSpacer( const wxSize
&size
)
133 { m_size
= size
; m_minSize
= size
; }
135 void Show ( bool show
);
139 wxObject
* GetUserData() const
140 { return m_userData
; }
141 wxPoint
GetPosition() const
154 // If true, then this item is considered in the layout
155 // calculation. Otherwise, it is skipped over.
158 // Aspect ratio can always be calculated from m_size,
159 // but this would cause precision loss when the window
160 // is shrunk. It is safer to preserve the initial value.
163 wxObject
*m_userData
;
166 DECLARE_CLASS(wxSizerItem
)
167 DECLARE_NO_COPY_CLASS(wxSizerItem
)
170 WX_DECLARE_EXPORTED_LIST( wxSizerItem
, wxSizerItemList
);
173 //---------------------------------------------------------------------------
175 //---------------------------------------------------------------------------
177 class WXDLLEXPORT wxSizer
: public wxObject
, public wxClientDataContainer
183 /* These should be called Append() really. */
184 virtual void Add( wxWindow
*window
,
188 wxObject
* userData
= NULL
);
189 virtual void Add( wxSizer
*sizer
,
193 wxObject
* userData
= NULL
);
194 virtual void Add( int width
,
199 wxObject
* userData
= NULL
);
200 virtual void Add( wxSizerItem
*item
);
202 virtual void AddSpacer(int size
);
203 virtual void AddStretchSpacer(int prop
= 1);
205 virtual void Insert( size_t index
,
210 wxObject
* userData
= NULL
);
211 virtual void Insert( size_t index
,
216 wxObject
* userData
= NULL
);
217 virtual void Insert( size_t index
,
223 wxObject
* userData
= NULL
);
224 virtual void Insert( size_t index
,
227 virtual void InsertSpacer(size_t index
, int size
);
228 virtual void InsertStretchSpacer(size_t index
, int prop
= 1);
230 virtual void Prepend( wxWindow
*window
,
234 wxObject
* userData
= NULL
);
235 virtual void Prepend( wxSizer
*sizer
,
239 wxObject
* userData
= NULL
);
240 virtual void Prepend( int width
,
245 wxObject
* userData
= NULL
);
246 virtual void Prepend( wxSizerItem
*item
);
248 virtual void PrependSpacer(int size
);
249 virtual void PrependStretchSpacer(int prop
= 1);
251 // Deprecated in 2.6 since historically it does not delete the window,
252 // use Detach instead.
253 wxDEPRECATED( virtual bool Remove( wxWindow
*window
) );
254 virtual bool Remove( wxSizer
*sizer
);
255 virtual bool Remove( int index
);
257 virtual bool Detach( wxWindow
*window
);
258 virtual bool Detach( wxSizer
*sizer
);
259 virtual bool Detach( int index
);
261 virtual void Clear( bool delete_windows
= false );
262 virtual void DeleteWindows();
264 void SetMinSize( int width
, int height
)
265 { DoSetMinSize( width
, height
); }
266 void SetMinSize( wxSize size
)
267 { DoSetMinSize( size
.x
, size
.y
); }
269 /* Searches recursively */
270 bool SetItemMinSize( wxWindow
*window
, int width
, int height
)
271 { return DoSetItemMinSize( window
, width
, height
); }
272 bool SetItemMinSize( wxWindow
*window
, wxSize size
)
273 { return DoSetItemMinSize( window
, size
.x
, size
.y
); }
275 /* Searches recursively */
276 bool SetItemMinSize( wxSizer
*sizer
, int width
, int height
)
277 { return DoSetItemMinSize( sizer
, width
, height
); }
278 bool SetItemMinSize( wxSizer
*sizer
, wxSize size
)
279 { return DoSetItemMinSize( sizer
, size
.x
, size
.y
); }
281 bool SetItemMinSize( size_t index
, int width
, int height
)
282 { return DoSetItemMinSize( index
, width
, height
); }
283 bool SetItemMinSize( size_t index
, wxSize size
)
284 { return DoSetItemMinSize( index
, size
.x
, size
.y
); }
286 wxSize
GetSize() const
288 wxPoint
GetPosition() const
289 { return m_position
; }
291 /* Calculate the minimal size or return m_minSize if bigger. */
294 virtual void RecalcSizes() = 0;
295 virtual wxSize
CalcMin() = 0;
297 virtual void Layout();
299 wxSize
Fit( wxWindow
*window
);
300 void FitInside( wxWindow
*window
);
301 void SetSizeHints( wxWindow
*window
);
302 void SetVirtualSizeHints( wxWindow
*window
);
304 wxSizerItemList
& GetChildren()
305 { return m_children
; }
307 void SetDimension( int x
, int y
, int width
, int height
);
309 // Manage whether individual scene items are considered
310 // in the layout calculations or not.
311 bool Show( wxWindow
*window
, bool show
= true, bool recursive
= false );
312 bool Show( wxSizer
*sizer
, bool show
= true, bool recursive
= false );
313 bool Show( size_t index
, bool show
= true );
315 bool Hide( wxSizer
*sizer
, bool recursive
= false )
316 { return Show( sizer
, false, recursive
); }
317 bool Hide( wxWindow
*window
, bool recursive
= false )
318 { return Show( window
, false, recursive
); }
319 bool Hide( size_t index
)
320 { return Show( index
, false ); }
322 bool IsShown( wxWindow
*window
) const;
323 bool IsShown( wxSizer
*sizer
) const;
324 bool IsShown( size_t index
) const;
326 // Recursively call wxWindow::Show () on all sizer items.
327 virtual void ShowItems (bool show
);
333 wxSizerItemList m_children
;
335 wxSize
GetMaxWindowSize( wxWindow
*window
) const;
336 wxSize
GetMinWindowSize( wxWindow
*window
);
337 wxSize
GetMaxClientSize( wxWindow
*window
) const;
338 wxSize
GetMinClientSize( wxWindow
*window
);
339 wxSize
FitSize( wxWindow
*window
);
340 wxSize
VirtualFitSize( wxWindow
*window
);
342 virtual void DoSetMinSize( int width
, int height
);
343 virtual bool DoSetItemMinSize( wxWindow
*window
, int width
, int height
);
344 virtual bool DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
);
345 virtual bool DoSetItemMinSize( size_t index
, int width
, int height
);
348 DECLARE_CLASS(wxSizer
)
351 //---------------------------------------------------------------------------
353 //---------------------------------------------------------------------------
355 class WXDLLEXPORT wxGridSizer
: public wxSizer
358 wxGridSizer( int rows
, int cols
, int vgap
, int hgap
);
359 wxGridSizer( int cols
, int vgap
= 0, int hgap
= 0 );
361 virtual void RecalcSizes();
362 virtual wxSize
CalcMin();
364 void SetCols( int cols
) { m_cols
= cols
; }
365 void SetRows( int rows
) { m_rows
= rows
; }
366 void SetVGap( int gap
) { m_vgap
= gap
; }
367 void SetHGap( int gap
) { m_hgap
= gap
; }
368 int GetCols() const { return m_cols
; }
369 int GetRows() const { return m_rows
; }
370 int GetVGap() const { return m_vgap
; }
371 int GetHGap() const { return m_hgap
; }
379 // return the number of total items and the number of columns and rows
380 int CalcRowsCols(int& rows
, int& cols
) const;
382 void SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
);
385 DECLARE_CLASS(wxGridSizer
)
388 //---------------------------------------------------------------------------
390 //---------------------------------------------------------------------------
392 // the bevaiour for resizing wxFlexGridSizer cells in the "non-flexible"
394 enum wxFlexSizerGrowMode
396 // don't resize the cells in non-flexible direction at all
397 wxFLEX_GROWMODE_NONE
,
399 // uniformly resize only the specified ones (default)
400 wxFLEX_GROWMODE_SPECIFIED
,
402 // uniformly resize all cells
406 class WXDLLEXPORT wxFlexGridSizer
: public wxGridSizer
410 wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
);
411 wxFlexGridSizer( int cols
, int vgap
= 0, int hgap
= 0 );
412 virtual ~wxFlexGridSizer();
415 // set the rows/columns which will grow (the others will remain of the
416 // constant initial size)
417 void AddGrowableRow( size_t idx
, int proportion
= 0 );
418 void RemoveGrowableRow( size_t idx
);
419 void AddGrowableCol( size_t idx
, int proportion
= 0 );
420 void RemoveGrowableCol( size_t idx
);
423 // the sizer cells may grow in both directions, not grow at all or only
424 // grow in one direction but not the other
426 // the direction may be wxVERTICAL, wxHORIZONTAL or wxBOTH (default)
427 void SetFlexibleDirection(int direction
) { m_flexDirection
= direction
; }
428 int GetFlexibleDirection() const { return m_flexDirection
; }
430 // note that the grow mode only applies to the direction which is not
432 void SetNonFlexibleGrowMode(wxFlexSizerGrowMode mode
) { m_growMode
= mode
; }
433 wxFlexSizerGrowMode
GetNonFlexibleGrowMode() const { return m_growMode
; }
435 // Read-only access to the row heights and col widths arrays
436 const wxArrayInt
& GetRowHeights() const { return m_rowHeights
; }
437 const wxArrayInt
& GetColWidths() const { return m_colWidths
; }
440 virtual void RecalcSizes();
441 virtual wxSize
CalcMin();
444 void AdjustForFlexDirection();
445 void AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
446 int nrows
, int ncols
);
448 // the heights/widths of all rows/columns
449 wxArrayInt m_rowHeights
,
452 // indices of the growable columns and rows
453 wxArrayInt m_growableRows
,
456 // proportion values of the corresponding growable rows and columns
457 wxArrayInt m_growableRowsProportions
,
458 m_growableColsProportions
;
460 // parameters describing whether the growable cells should be resized in
461 // both directions or only one
463 wxFlexSizerGrowMode m_growMode
;
465 // saves CalcMin result to optimize RecalcSizes
466 wxSize m_calculatedMinSize
;
469 DECLARE_CLASS(wxFlexGridSizer
)
470 DECLARE_NO_COPY_CLASS(wxFlexGridSizer
)
473 //---------------------------------------------------------------------------
475 //---------------------------------------------------------------------------
477 class WXDLLEXPORT wxBoxSizer
: public wxSizer
480 wxBoxSizer( int orient
);
485 int GetOrientation() const
488 void SetOrientation(int orient
)
489 { m_orient
= orient
; }
500 DECLARE_CLASS(wxBoxSizer
)
503 //---------------------------------------------------------------------------
505 //---------------------------------------------------------------------------
509 class WXDLLEXPORT wxStaticBox
;
511 class WXDLLEXPORT wxStaticBoxSizer
: public wxBoxSizer
514 wxStaticBoxSizer( wxStaticBox
*box
, int orient
);
519 wxStaticBox
*GetStaticBox() const
520 { return m_staticBox
; }
522 // override to hide/show the static box as well
523 virtual void ShowItems (bool show
);
526 wxStaticBox
*m_staticBox
;
529 DECLARE_CLASS(wxStaticBoxSizer
)
530 DECLARE_NO_COPY_CLASS(wxStaticBoxSizer
)
533 #endif // wxUSE_STATBOX
536 #if WXWIN_COMPATIBILITY_2_4
537 // NB: wxBookCtrlSizer and wxNotebookSizer are deprecated, they
538 // don't do anything. wxBookCtrl::DoGetBestSize does the job now.
540 // ----------------------------------------------------------------------------
542 // ----------------------------------------------------------------------------
546 // this sizer works with wxNotebook/wxListbook/wxChoicebook... and sizes the control to
548 class WXDLLEXPORT wxBookCtrl
;
550 class WXDLLEXPORT wxBookCtrlSizer
: public wxSizer
553 wxDEPRECATED( wxBookCtrlSizer(wxBookCtrl
*bookctrl
) );
555 wxBookCtrl
*GetControl() const { return m_bookctrl
; }
557 virtual void RecalcSizes();
558 virtual wxSize
CalcMin();
561 // this protected ctor lets us mark the real one above as deprecated
562 // and still have warning-free build of the library itself:
565 wxBookCtrl
*m_bookctrl
;
568 DECLARE_CLASS(wxBookCtrlSizer
)
569 DECLARE_NO_COPY_CLASS(wxBookCtrlSizer
)
575 // before wxBookCtrl we only had wxNotebookSizer, keep it for backwards
577 class WXDLLEXPORT wxNotebook
;
579 class WXDLLEXPORT wxNotebookSizer
: public wxBookCtrlSizer
582 wxDEPRECATED( wxNotebookSizer(wxNotebook
*nb
) );
584 wxNotebook
*GetNotebook() const { return (wxNotebook
*)m_bookctrl
; }
587 DECLARE_CLASS(wxNotebookSizer
)
588 DECLARE_NO_COPY_CLASS(wxNotebookSizer
)
591 #endif // wxUSE_NOTEBOOK
593 #endif // wxUSE_BOOKCTRL
595 #endif // WXWIN_COMPATIBILITY_2_4
598 #endif // __WXSIZER_H__