added shortcut functions AddSpacer and AddStretchSpacer (plus Prepend and Insert...
[wxWidgets.git] / include / wx / sizer.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sizer.h
3 // Purpose: provide wxSizer class for layout
4 // Author: Robert Roebling and Robin Dunn
5 // Modified by: Ron Lee
6 // Created:
7 // RCS-ID: $Id$
8 // Copyright: (c) Robin Dunn, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef __WXSIZER_H__
13 #define __WXSIZER_H__
14
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 WXDLLEXPORT wxSizerItem;
30 class WXDLLEXPORT wxSizer;
31 class WXDLLEXPORT wxBoxSizer;
32
33 //---------------------------------------------------------------------------
34 // wxSizerItem
35 //---------------------------------------------------------------------------
36
37 class WXDLLEXPORT wxSizerItem: public wxObject
38 {
39 public:
40 // spacer
41 wxSizerItem( int width,
42 int height,
43 int proportion,
44 int flag,
45 int border,
46 wxObject* userData);
47
48 // window
49 wxSizerItem( wxWindow *window,
50 int proportion,
51 int flag,
52 int border,
53 wxObject* userData );
54
55 // subsizer
56 wxSizerItem( wxSizer *sizer,
57 int proportion,
58 int flag,
59 int border,
60 wxObject* userData );
61
62 wxSizerItem();
63 virtual ~wxSizerItem();
64
65 virtual void DeleteWindows();
66
67 // Enable deleting the SizerItem without destroying the contained sizer.
68 void DetachSizer()
69 { m_sizer = 0; }
70
71 virtual wxSize GetSize() const;
72 virtual wxSize CalcMin();
73 virtual void SetDimension( wxPoint pos, wxSize size );
74
75 wxSize GetMinSize() const
76 { return m_minSize; }
77 wxSize GetMinSizeWithBorder() const;
78
79 void SetMinSize(const wxSize& size)
80 {
81 if (IsWindow()) m_window->SetMinSize(size);
82 m_minSize = size;
83 }
84 void SetMinSize( int x, int y )
85 { SetMinSize(wxSize(x, y)); }
86 void SetInitSize( int x, int y )
87 { SetMinSize(wxSize(x, y)); }
88
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 )
96 { m_ratio = ratio; }
97 float GetRatio() const
98 { return m_ratio; }
99
100 bool IsWindow() const;
101 bool IsSizer() const;
102 bool IsSpacer() const;
103
104 // Deprecated in 2.6, use {G,S}etProportion instead.
105 wxDEPRECATED( void SetOption( int option ) );
106 wxDEPRECATED( int GetOption() const );
107
108 void SetProportion( int proportion )
109 { m_proportion = proportion; }
110 int GetProportion() const
111 { return m_proportion; }
112 void SetFlag( int flag )
113 { m_flag = flag; }
114 int GetFlag() const
115 { return m_flag; }
116 void SetBorder( int border )
117 { m_border = border; }
118 int GetBorder() const
119 { return m_border; }
120
121 wxWindow *GetWindow() const
122 { return m_window; }
123 void SetWindow( wxWindow *window )
124 { m_window = window; m_minSize = window->GetSize(); }
125 wxSizer *GetSizer() const
126 { return m_sizer; }
127 void SetSizer( wxSizer *sizer )
128 { m_sizer = sizer; }
129 const wxSize &GetSpacer() const
130 { return m_size; }
131 void SetSpacer( const wxSize &size )
132 { m_size = size; m_minSize = size; }
133
134 void Show ( bool show );
135 bool IsShown() const
136 { return m_show; }
137
138 wxObject* GetUserData() const
139 { return m_userData; }
140 wxPoint GetPosition() const
141 { return m_pos; }
142
143 protected:
144 wxWindow *m_window;
145 wxSizer *m_sizer;
146 wxSize m_size;
147 wxPoint m_pos;
148 wxSize m_minSize;
149 int m_proportion;
150 int m_border;
151 int m_flag;
152
153 // If true, then this item is considered in the layout
154 // calculation. Otherwise, it is skipped over.
155 bool m_show;
156
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.
160 float m_ratio;
161
162 wxObject *m_userData;
163
164 private:
165 DECLARE_CLASS(wxSizerItem)
166 DECLARE_NO_COPY_CLASS(wxSizerItem)
167 };
168
169 WX_DECLARE_EXPORTED_LIST( wxSizerItem, wxSizerItemList );
170
171
172 //---------------------------------------------------------------------------
173 // wxSizer
174 //---------------------------------------------------------------------------
175
176 class WXDLLEXPORT wxSizer: public wxObject, public wxClientDataContainer
177 {
178 public:
179 wxSizer();
180 ~wxSizer();
181
182 /* These should be called Append() really. */
183 virtual void Add( wxWindow *window,
184 int proportion = 0,
185 int flag = 0,
186 int border = 0,
187 wxObject* userData = NULL );
188 virtual void Add( wxSizer *sizer,
189 int proportion = 0,
190 int flag = 0,
191 int border = 0,
192 wxObject* userData = NULL );
193 virtual void Add( int width,
194 int height,
195 int proportion = 0,
196 int flag = 0,
197 int border = 0,
198 wxObject* userData = NULL );
199 virtual void Add( wxSizerItem *item );
200
201 virtual void AddSpacer(int size);
202 virtual void AddStretchSpacer(int prop = 1);
203
204 virtual void Insert( size_t index,
205 wxWindow *window,
206 int proportion = 0,
207 int flag = 0,
208 int border = 0,
209 wxObject* userData = NULL );
210 virtual void Insert( size_t index,
211 wxSizer *sizer,
212 int proportion = 0,
213 int flag = 0,
214 int border = 0,
215 wxObject* userData = NULL );
216 virtual void Insert( size_t index,
217 int width,
218 int height,
219 int proportion = 0,
220 int flag = 0,
221 int border = 0,
222 wxObject* userData = NULL );
223 virtual void Insert( size_t index,
224 wxSizerItem *item );
225
226 virtual void InsertSpacer(size_t index, int size);
227 virtual void InsertStretchSpacer(size_t index, int prop = 1);
228
229 virtual void Prepend( wxWindow *window,
230 int proportion = 0,
231 int flag = 0,
232 int border = 0,
233 wxObject* userData = NULL );
234 virtual void Prepend( wxSizer *sizer,
235 int proportion = 0,
236 int flag = 0,
237 int border = 0,
238 wxObject* userData = NULL );
239 virtual void Prepend( int width,
240 int height,
241 int proportion = 0,
242 int flag = 0,
243 int border = 0,
244 wxObject* userData = NULL );
245 virtual void Prepend( wxSizerItem *item );
246
247 virtual void PrependSpacer(int size);
248 virtual void PrependStretchSpacer(int prop = 1);
249
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 );
255
256 virtual bool Detach( wxWindow *window );
257 virtual bool Detach( wxSizer *sizer );
258 virtual bool Detach( int index );
259
260 virtual void Clear( bool delete_windows = false );
261 virtual void DeleteWindows();
262
263 void SetMinSize( int width, int height )
264 { DoSetMinSize( width, height ); }
265 void SetMinSize( wxSize size )
266 { DoSetMinSize( size.x, size.y ); }
267
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 ); }
273
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 ); }
279
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 ); }
284
285 wxSize GetSize() const
286 { return m_size; }
287 wxPoint GetPosition() const
288 { return m_position; }
289
290 /* Calculate the minimal size or return m_minSize if bigger. */
291 wxSize GetMinSize();
292
293 virtual void RecalcSizes() = 0;
294 virtual wxSize CalcMin() = 0;
295
296 virtual void Layout();
297
298 wxSize Fit( wxWindow *window );
299 void FitInside( wxWindow *window );
300 void SetSizeHints( wxWindow *window );
301 void SetVirtualSizeHints( wxWindow *window );
302
303 wxSizerItemList& GetChildren()
304 { return m_children; }
305
306 void SetDimension( int x, int y, int width, int height );
307
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 );
313
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 ); }
320
321 bool IsShown( wxWindow *window ) const;
322 bool IsShown( wxSizer *sizer ) const;
323 bool IsShown( size_t index ) const;
324
325 // Recursively call wxWindow::Show () on all sizer items.
326 virtual void ShowItems (bool show);
327
328 protected:
329 wxSize m_size;
330 wxSize m_minSize;
331 wxPoint m_position;
332 wxSizerItemList m_children;
333
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 );
340
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 );
345
346 private:
347 DECLARE_CLASS(wxSizer)
348 };
349
350 //---------------------------------------------------------------------------
351 // wxGridSizer
352 //---------------------------------------------------------------------------
353
354 class WXDLLEXPORT wxGridSizer: public wxSizer
355 {
356 public:
357 wxGridSizer( int rows, int cols, int vgap, int hgap );
358 wxGridSizer( int cols, int vgap = 0, int hgap = 0 );
359
360 virtual void RecalcSizes();
361 virtual wxSize CalcMin();
362
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; }
371
372 protected:
373 int m_rows;
374 int m_cols;
375 int m_vgap;
376 int m_hgap;
377
378 // return the number of total items and the number of columns and rows
379 int CalcRowsCols(int& rows, int& cols) const;
380
381 void SetItemBounds( wxSizerItem *item, int x, int y, int w, int h );
382
383 private:
384 DECLARE_CLASS(wxGridSizer)
385 };
386
387 //---------------------------------------------------------------------------
388 // wxFlexGridSizer
389 //---------------------------------------------------------------------------
390
391 // the bevaiour for resizing wxFlexGridSizer cells in the "non-flexible"
392 // direction
393 enum wxFlexSizerGrowMode
394 {
395 // don't resize the cells in non-flexible direction at all
396 wxFLEX_GROWMODE_NONE,
397
398 // uniformly resize only the specified ones (default)
399 wxFLEX_GROWMODE_SPECIFIED,
400
401 // uniformly resize all cells
402 wxFLEX_GROWMODE_ALL
403 };
404
405 class WXDLLEXPORT wxFlexGridSizer: public wxGridSizer
406 {
407 public:
408 // ctors/dtor
409 wxFlexGridSizer( int rows, int cols, int vgap, int hgap );
410 wxFlexGridSizer( int cols, int vgap = 0, int hgap = 0 );
411 virtual ~wxFlexGridSizer();
412
413
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 );
420
421
422 // the sizer cells may grow in both directions, not grow at all or only
423 // grow in one direction but not the other
424
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; }
428
429 // note that the grow mode only applies to the direction which is not
430 // flexible
431 void SetNonFlexibleGrowMode(wxFlexSizerGrowMode mode) { m_growMode = mode; }
432 wxFlexSizerGrowMode GetNonFlexibleGrowMode() const { return m_growMode; }
433
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; }
437
438 // implementation
439 virtual void RecalcSizes();
440 virtual wxSize CalcMin();
441
442 protected:
443 void AdjustForFlexDirection();
444 void AdjustForGrowables(const wxSize& sz, const wxSize& minsz,
445 int nrows, int ncols);
446
447 // the heights/widths of all rows/columns
448 wxArrayInt m_rowHeights,
449 m_colWidths;
450
451 // indices of the growable columns and rows
452 wxArrayInt m_growableRows,
453 m_growableCols;
454
455 // proportion values of the corresponding growable rows and columns
456 wxArrayInt m_growableRowsProportions,
457 m_growableColsProportions;
458
459 // parameters describing whether the growable cells should be resized in
460 // both directions or only one
461 int m_flexDirection;
462 wxFlexSizerGrowMode m_growMode;
463
464 // saves CalcMin result to optimize RecalcSizes
465 wxSize m_calculatedMinSize;
466
467 private:
468 DECLARE_CLASS(wxFlexGridSizer)
469 DECLARE_NO_COPY_CLASS(wxFlexGridSizer)
470 };
471
472 //---------------------------------------------------------------------------
473 // wxBoxSizer
474 //---------------------------------------------------------------------------
475
476 class WXDLLEXPORT wxBoxSizer: public wxSizer
477 {
478 public:
479 wxBoxSizer( int orient );
480
481 void RecalcSizes();
482 wxSize CalcMin();
483
484 int GetOrientation() const
485 { return m_orient; }
486
487 void SetOrientation(int orient)
488 { m_orient = orient; }
489
490 protected:
491 int m_orient;
492 int m_stretchable;
493 int m_minWidth;
494 int m_minHeight;
495 int m_fixedWidth;
496 int m_fixedHeight;
497
498 private:
499 DECLARE_CLASS(wxBoxSizer)
500 };
501
502 //---------------------------------------------------------------------------
503 // wxStaticBoxSizer
504 //---------------------------------------------------------------------------
505
506 #if wxUSE_STATBOX
507
508 class WXDLLEXPORT wxStaticBox;
509
510 class WXDLLEXPORT wxStaticBoxSizer: public wxBoxSizer
511 {
512 public:
513 wxStaticBoxSizer( wxStaticBox *box, int orient );
514
515 void RecalcSizes();
516 wxSize CalcMin();
517
518 wxStaticBox *GetStaticBox() const
519 { return m_staticBox; }
520
521 // override to hide/show the static box as well
522 virtual void ShowItems (bool show);
523
524 protected:
525 wxStaticBox *m_staticBox;
526
527 private:
528 DECLARE_CLASS(wxStaticBoxSizer)
529 DECLARE_NO_COPY_CLASS(wxStaticBoxSizer)
530 };
531
532 #endif // wxUSE_STATBOX
533
534
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.
538
539 // ----------------------------------------------------------------------------
540 // wxBookCtrlSizer
541 // ----------------------------------------------------------------------------
542
543 #if wxUSE_BOOKCTRL
544
545 // this sizer works with wxNotebook/wxListbook/... and sizes the control to
546 // fit its pages
547 class WXDLLEXPORT wxBookCtrl;
548
549 class WXDLLEXPORT wxBookCtrlSizer : public wxSizer
550 {
551 public:
552 wxDEPRECATED( wxBookCtrlSizer(wxBookCtrl *bookctrl) );
553
554 wxBookCtrl *GetControl() const { return m_bookctrl; }
555
556 virtual void RecalcSizes();
557 virtual wxSize CalcMin();
558
559 protected:
560 // this protected ctor lets us mark the real one above as deprecated
561 // and still have warning-free build of the library itself:
562 wxBookCtrlSizer() {}
563
564 wxBookCtrl *m_bookctrl;
565
566 private:
567 DECLARE_CLASS(wxBookCtrlSizer)
568 DECLARE_NO_COPY_CLASS(wxBookCtrlSizer)
569 };
570
571
572 #if wxUSE_NOTEBOOK
573
574 // before wxBookCtrl we only had wxNotebookSizer, keep it for backwards
575 // compatibility
576 class WXDLLEXPORT wxNotebook;
577
578 class WXDLLEXPORT wxNotebookSizer : public wxBookCtrlSizer
579 {
580 public:
581 wxDEPRECATED( wxNotebookSizer(wxNotebook *nb) );
582
583 wxNotebook *GetNotebook() const { return (wxNotebook *)m_bookctrl; }
584
585 private:
586 DECLARE_CLASS(wxNotebookSizer)
587 DECLARE_NO_COPY_CLASS(wxNotebookSizer)
588 };
589
590 #endif // wxUSE_NOTEBOOK
591
592 #endif // wxUSE_BOOKCTRL
593
594 #endif // WXWIN_COMPATIBILITY_2_4
595
596
597 #endif // __WXSIZER_H__
598