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