Add wxSizer::Detach so we can detach child sizers without deletion.
[wxWidgets.git] / include / wx / sizer.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sizer.h
3 // Purpose: provide wxSizer class for layouting
4 // Author: Robert Roebling and Robin Dunn
5 // Modified by: Ron Lee
6 // Created:
7 // RCS-ID: $Id$
8 // Copyright: (c) Robin Dunn, Dirk Holtwick and Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef __WXSIZER_H__
13 #define __WXSIZER_H__
14
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "sizer.h"
17 #endif
18
19 #include "wx/defs.h"
20
21 #include "wx/window.h"
22 #include "wx/frame.h"
23 #include "wx/dialog.h"
24
25 //---------------------------------------------------------------------------
26 // classes
27 //---------------------------------------------------------------------------
28
29 class wxSizerItem;
30 class wxSizer;
31 class wxBoxSizer;
32
33 //---------------------------------------------------------------------------
34 // wxSizerItem
35 //---------------------------------------------------------------------------
36
37 class WXDLLEXPORT wxSizerItem: public wxObject
38 {
39 public:
40 // spacer
41 wxSizerItem( int width, int height, int option, int flag, int border, wxObject* userData);
42
43 // window
44 wxSizerItem( wxWindow *window, int option, int flag, int border, wxObject* userData );
45
46 // subsizer
47 wxSizerItem( wxSizer *sizer, int option, int flag, int border, wxObject* userData );
48
49 ~wxSizerItem();
50
51 virtual void DeleteWindows();
52
53 virtual wxSize GetSize();
54 virtual wxSize CalcMin();
55 virtual void SetDimension( wxPoint pos, wxSize size );
56
57 wxSize GetMinSize()
58 { return m_minSize; }
59
60 void SetRatio( int width, int height )
61 // if either of dimensions is zero, ratio is assumed to be 1
62 // to avoid "divide by zero" errors
63 { m_ratio = (width && height) ? ((float) width / (float) height) : 1; }
64 void SetRatio( wxSize size )
65 { m_ratio = (size.x && size.y) ? ((float) size.x / (float) size.y) : 1; }
66 void SetRatio( float ratio )
67 { m_ratio = ratio; }
68 float GetRatio() const
69 { return m_ratio; }
70
71 bool IsWindow();
72 bool IsSizer();
73 bool IsSpacer();
74
75 void SetInitSize( int x, int y )
76 { m_minSize.x = x; m_minSize.y = y; }
77 void SetOption( int option )
78 { m_option = option; }
79 void SetFlag( int flag )
80 { m_flag = flag; }
81 void SetBorder( int border )
82 { m_border = border; }
83 void Show ( bool show )
84 { m_show = show; }
85 void SetDeleteItem( bool deleteItem = TRUE )
86 { m_deleteItem = deleteItem; }
87
88 wxWindow *GetWindow() const
89 { return m_window; }
90 void SetWindow( wxWindow *window )
91 { m_window = window; }
92 wxSizer *GetSizer() const
93 { return m_sizer; }
94 void SetSizer( wxSizer *sizer )
95 { m_sizer = sizer; }
96 int GetOption() const
97 { return m_option; }
98 int GetFlag() const
99 { return m_flag; }
100 int GetBorder() const
101 { return m_border; }
102 bool IsShown() const
103 { return m_show; }
104 wxObject* GetUserData()
105 { return m_userData; }
106 wxPoint GetPosition()
107 { return m_pos; }
108
109 protected:
110 wxWindow *m_window;
111 wxSizer *m_sizer;
112 wxSize m_size;
113 wxPoint m_pos;
114 wxSize m_minSize;
115 int m_option;
116 int m_border;
117 int m_flag;
118
119 // If TRUE, then this item is considered in the layout
120 // calculation. Otherwise, it is skipped over.
121 bool m_show;
122 // als: aspect ratio can always be calculated from m_size,
123 // but this would cause precision loss when the window
124 // is shrinked. it is safer to preserve initial value.
125 float m_ratio;
126
127 // If TRUE, and the item is a sizer, delete it when the
128 // sizeritem is destroyed. Not used for any other type
129 // of item right now.
130 bool m_deleteItem;
131
132 wxObject *m_userData;
133
134 private:
135 DECLARE_CLASS(wxSizerItem);
136 };
137
138 //---------------------------------------------------------------------------
139 // wxSizer
140 //---------------------------------------------------------------------------
141
142 class WXDLLEXPORT wxSizer: public wxObject, public wxClientDataContainer
143 {
144 public:
145 wxSizer();
146 ~wxSizer();
147
148 /* These should be called Append() really. */
149 virtual void Add( wxWindow *window, int option = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
150 virtual void Add( wxSizer *sizer, int option = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
151 virtual void Add( int width, int height, int option = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
152
153 virtual void Insert( int before, wxWindow *window, int option = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
154 virtual void Insert( int before, wxSizer *sizer, int option = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
155 virtual void Insert( int before, int width, int height, int option = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
156
157 virtual void Prepend( wxWindow *window, int option = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
158 virtual void Prepend( wxSizer *sizer, int option = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
159 virtual void Prepend( int width, int height, int option = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
160
161 // Remove will delete a sizer, but not a window.
162 virtual bool Remove( wxWindow *window );
163 virtual bool Remove( wxSizer *sizer );
164 virtual bool Remove( int pos );
165
166 // Detach will never destroy a sizer or window.
167 virtual bool Detach( wxWindow *window )
168 { return Remove( window ); }
169 virtual bool Detach( wxSizer *sizer );
170 virtual bool Detach( int pos );
171
172 virtual void Clear( bool delete_windows=FALSE );
173 virtual void DeleteWindows();
174
175 void SetMinSize( int width, int height )
176 { DoSetMinSize( width, height ); }
177 void SetMinSize( wxSize size )
178 { DoSetMinSize( size.x, size.y ); }
179
180 /* Searches recursively */
181 bool SetItemMinSize( wxWindow *window, int width, int height )
182 { return DoSetItemMinSize( window, width, height ); }
183 bool SetItemMinSize( wxWindow *window, wxSize size )
184 { return DoSetItemMinSize( window, size.x, size.y ); }
185
186 /* Searches recursively */
187 bool SetItemMinSize( wxSizer *sizer, int width, int height )
188 { return DoSetItemMinSize( sizer, width, height ); }
189 bool SetItemMinSize( wxSizer *sizer, wxSize size )
190 { return DoSetItemMinSize( sizer, size.x, size.y ); }
191
192 bool SetItemMinSize( int pos, int width, int height )
193 { return DoSetItemMinSize( pos, width, height ); }
194 bool SetItemMinSize( int pos, wxSize size )
195 { return DoSetItemMinSize( pos, size.x, size.y ); }
196
197 wxSize GetSize()
198 { return m_size; }
199 wxPoint GetPosition()
200 { return m_position; }
201
202 /* Calculate the minimal size or return m_minSize if bigger. */
203 wxSize GetMinSize();
204
205 virtual void RecalcSizes() = 0;
206 virtual wxSize CalcMin() = 0;
207
208 virtual void Layout();
209
210 wxSize Fit( wxWindow *window );
211 void FitInside( wxWindow *window );
212 void SetSizeHints( wxWindow *window );
213 void SetVirtualSizeHints( wxWindow *window );
214
215 wxList& GetChildren()
216 { return m_children; }
217
218 void SetDimension( int x, int y, int width, int height );
219
220 // Manage whether individual windows or sub-sizers are considered
221 // in the layout calculations or not.
222 void Show( wxWindow *window, bool show = TRUE );
223 void Hide( wxWindow *window )
224 { Show (window, FALSE); }
225 void Show( wxSizer *sizer, bool show = TRUE );
226 void Hide( wxSizer *sizer )
227 { Show (sizer, FALSE); }
228
229 bool IsShown( wxWindow *window );
230 bool IsShown( wxSizer *sizer );
231
232 // Recursively call wxWindow::Show () on all sizer items.
233 void ShowItems (bool show);
234
235 protected:
236 wxSize m_size;
237 wxSize m_minSize;
238 wxPoint m_position;
239 wxList m_children;
240
241 wxSize GetMaxWindowSize( wxWindow *window );
242 wxSize GetMinWindowSize( wxWindow *window );
243 wxSize GetMaxClientSize( wxWindow *window );
244 wxSize GetMinClientSize( wxWindow *window );
245 wxSize FitSize( wxWindow *window );
246 wxSize VirtualFitSize( wxWindow *window );
247
248 virtual void DoSetMinSize( int width, int height );
249 virtual bool DoSetItemMinSize( wxWindow *window, int width, int height );
250 virtual bool DoSetItemMinSize( wxSizer *sizer, int width, int height );
251 virtual bool DoSetItemMinSize( int pos, int width, int height );
252
253 private:
254 DECLARE_CLASS(wxSizer);
255 };
256
257 //---------------------------------------------------------------------------
258 // wxGridSizer
259 //---------------------------------------------------------------------------
260
261 class WXDLLEXPORT wxGridSizer: public wxSizer
262 {
263 public:
264 wxGridSizer( int rows, int cols, int vgap, int hgap );
265 wxGridSizer( int cols, int vgap = 0, int hgap = 0 );
266
267 void RecalcSizes();
268 wxSize CalcMin();
269
270 void SetCols( int cols ) { m_cols = cols; }
271 void SetRows( int rows ) { m_rows = rows; }
272 void SetVGap( int gap ) { m_vgap = gap; }
273 void SetHGap( int gap ) { m_hgap = gap; }
274 int GetCols() { return m_cols; }
275 int GetRows() { return m_rows; }
276 int GetVGap() { return m_vgap; }
277 int GetHGap() { return m_hgap; }
278
279 protected:
280 int m_rows;
281 int m_cols;
282 int m_vgap;
283 int m_hgap;
284
285 // return the number of total items and the number of columns and rows
286 int CalcRowsCols(int& rows, int& cols) const;
287
288 void SetItemBounds( wxSizerItem *item, int x, int y, int w, int h );
289
290 private:
291 DECLARE_CLASS(wxGridSizer);
292 };
293
294 //---------------------------------------------------------------------------
295 // wxFlexGridSizer
296 //---------------------------------------------------------------------------
297
298 class WXDLLEXPORT wxFlexGridSizer: public wxGridSizer
299 {
300 public:
301 wxFlexGridSizer( int rows, int cols, int vgap, int hgap );
302 wxFlexGridSizer( int cols, int vgap = 0, int hgap = 0 );
303 ~wxFlexGridSizer();
304
305 void RecalcSizes();
306 wxSize CalcMin();
307
308 void AddGrowableRow( size_t idx );
309 void RemoveGrowableRow( size_t idx );
310 void AddGrowableCol( size_t idx );
311 void RemoveGrowableCol( size_t idx );
312
313 protected:
314 int *m_rowHeights;
315 int *m_colWidths;
316 wxArrayInt m_growableRows;
317 wxArrayInt m_growableCols;
318
319 void CreateArrays();
320
321 private:
322 DECLARE_CLASS(wxFlexGridSizer);
323 };
324
325 //---------------------------------------------------------------------------
326 // wxBoxSizer
327 //---------------------------------------------------------------------------
328
329 class WXDLLEXPORT wxBoxSizer: public wxSizer
330 {
331 public:
332 wxBoxSizer( int orient );
333
334 void RecalcSizes();
335 wxSize CalcMin();
336
337 int GetOrientation()
338 { return m_orient; }
339
340 void SetOrientation(int orient)
341 { m_orient = orient; }
342
343 protected:
344 int m_orient;
345 int m_stretchable;
346 int m_minWidth;
347 int m_minHeight;
348 int m_fixedWidth;
349 int m_fixedHeight;
350
351 private:
352 DECLARE_CLASS(wxBoxSizer);
353 };
354
355 //---------------------------------------------------------------------------
356 // wxStaticBoxSizer
357 //---------------------------------------------------------------------------
358
359 #if wxUSE_STATBOX
360
361 class WXDLLEXPORT wxStaticBox;
362
363 class WXDLLEXPORT wxStaticBoxSizer: public wxBoxSizer
364 {
365 public:
366 wxStaticBoxSizer( wxStaticBox *box, int orient );
367
368 void RecalcSizes();
369 wxSize CalcMin();
370
371 wxStaticBox *GetStaticBox()
372 { return m_staticBox; }
373
374 protected:
375 wxStaticBox *m_staticBox;
376
377 private:
378 DECLARE_CLASS(wxStaticBoxSizer);
379 };
380
381 #endif // wxUSE_STATBOX
382
383 //---------------------------------------------------------------------------
384 // wxNotebookSizer
385 //---------------------------------------------------------------------------
386
387 #if wxUSE_NOTEBOOK
388
389 class WXDLLEXPORT wxNotebook;
390
391 class WXDLLEXPORT wxNotebookSizer: public wxSizer
392 {
393 public:
394 wxNotebookSizer( wxNotebook *nb );
395
396 void RecalcSizes();
397 wxSize CalcMin();
398
399 wxNotebook *GetNotebook()
400 { return m_notebook; }
401
402 protected:
403 wxNotebook *m_notebook;
404
405 private:
406 DECLARE_CLASS(wxNotebookSizer);
407 };
408
409 #endif // wxUSE_NOTEBOOK
410
411
412 #endif
413 // __WXSIZER_H__