No real changes, just add wxSizerItem::AddBorderToSize() helper.
[wxWidgets.git] / include / wx / sizer.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/sizer.h
3 // Purpose: provide wxSizer class for layout
4 // Author: Robert Roebling and Robin Dunn
5 // Modified by: Ron Lee, Vadim Zeitlin (wxSizerFlags)
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 #include "wx/defs.h"
16
17 #include "wx/window.h"
18
19 //---------------------------------------------------------------------------
20 // classes
21 //---------------------------------------------------------------------------
22
23 class WXDLLIMPEXP_FWD_CORE wxButton;
24 class WXDLLIMPEXP_FWD_CORE wxBoxSizer;
25 class WXDLLIMPEXP_FWD_CORE wxSizerItem;
26 class WXDLLIMPEXP_FWD_CORE wxSizer;
27
28 #ifndef wxUSE_BORDER_BY_DEFAULT
29 #ifdef __SMARTPHONE__
30 // no borders by default on limited size screen
31 #define wxUSE_BORDER_BY_DEFAULT 0
32 #else
33 #define wxUSE_BORDER_BY_DEFAULT 1
34 #endif
35 #endif
36
37 // ----------------------------------------------------------------------------
38 // wxSizerFlags: flags used for an item in the sizer
39 // ----------------------------------------------------------------------------
40
41 class WXDLLIMPEXP_CORE wxSizerFlags
42 {
43 public:
44 // construct the flags object initialized with the given proportion (0 by
45 // default)
46 wxSizerFlags(int proportion = 0) : m_proportion(proportion)
47 {
48 m_flags = 0;
49 m_borderInPixels = 0;
50 }
51
52 // setters for all sizer flags, they all return the object itself so that
53 // calls to them can be chained
54
55 wxSizerFlags& Proportion(int proportion)
56 {
57 m_proportion = proportion;
58 return *this;
59 }
60
61 wxSizerFlags& Expand()
62 {
63 m_flags |= wxEXPAND;
64 return *this;
65 }
66
67 // notice that Align() replaces the current alignment flags, use specific
68 // methods below such as Top(), Left() &c if you want to set just the
69 // vertical or horizontal alignment
70 wxSizerFlags& Align(int alignment) // combination of wxAlignment values
71 {
72 m_flags &= ~wxALIGN_MASK;
73 m_flags |= alignment;
74
75 return *this;
76 }
77
78 // some shortcuts for Align()
79 wxSizerFlags& Centre() { return Align(wxALIGN_CENTRE); }
80 wxSizerFlags& Center() { return Centre(); }
81
82 wxSizerFlags& Top()
83 {
84 m_flags &= ~(wxALIGN_BOTTOM | wxALIGN_CENTRE_VERTICAL);
85 return *this;
86 }
87
88 wxSizerFlags& Left()
89 {
90 m_flags &= ~(wxALIGN_RIGHT | wxALIGN_CENTRE_HORIZONTAL);
91 return *this;
92 }
93
94 wxSizerFlags& Right()
95 {
96 m_flags = (m_flags & ~wxALIGN_CENTRE_HORIZONTAL) | wxALIGN_RIGHT;
97 return *this;
98 }
99
100 wxSizerFlags& Bottom()
101 {
102 m_flags = (m_flags & ~wxALIGN_CENTRE_VERTICAL) | wxALIGN_BOTTOM;
103 return *this;
104 }
105
106
107 // default border size used by Border() below
108 static int GetDefaultBorder()
109 {
110 #if wxUSE_BORDER_BY_DEFAULT
111 #ifdef __WXGTK20__
112 // GNOME HIG says to use 6px as the base unit:
113 // http://library.gnome.org/devel/hig-book/stable/design-window.html.en
114 return 6;
115 #else
116 // FIXME: default border size shouldn't be hardcoded and at the very
117 // least they should depend on the current font size
118 return 5;
119 #endif
120 #else
121 return 0;
122 #endif
123 }
124
125
126 wxSizerFlags& Border(int direction, int borderInPixels)
127 {
128 m_flags &= ~wxALL;
129 m_flags |= direction;
130
131 m_borderInPixels = borderInPixels;
132
133 return *this;
134 }
135
136 wxSizerFlags& Border(int direction = wxALL)
137 {
138 #if wxUSE_BORDER_BY_DEFAULT
139 return Border(direction, GetDefaultBorder());
140 #else
141 // no borders by default on limited size screen
142 wxUnusedVar(direction);
143
144 return *this;
145 #endif
146 }
147
148 wxSizerFlags& DoubleBorder(int direction = wxALL)
149 {
150 #if wxUSE_BORDER_BY_DEFAULT
151 return Border(direction, 2*GetDefaultBorder());
152 #else
153 wxUnusedVar(direction);
154
155 return *this;
156 #endif
157 }
158
159 wxSizerFlags& TripleBorder(int direction = wxALL)
160 {
161 #if wxUSE_BORDER_BY_DEFAULT
162 return Border(direction, 3*GetDefaultBorder());
163 #else
164 wxUnusedVar(direction);
165
166 return *this;
167 #endif
168 }
169
170 wxSizerFlags& HorzBorder()
171 {
172 #if wxUSE_BORDER_BY_DEFAULT
173 return Border(wxLEFT | wxRIGHT, GetDefaultBorder());
174 #else
175 return *this;
176 #endif
177 }
178
179 wxSizerFlags& DoubleHorzBorder()
180 {
181 #if wxUSE_BORDER_BY_DEFAULT
182 return Border(wxLEFT | wxRIGHT, 2*GetDefaultBorder());
183 #else
184 return *this;
185 #endif
186 }
187
188 // setters for the others flags
189 wxSizerFlags& Shaped()
190 {
191 m_flags |= wxSHAPED;
192
193 return *this;
194 }
195
196 wxSizerFlags& FixedMinSize()
197 {
198 m_flags |= wxFIXED_MINSIZE;
199
200 return *this;
201 }
202
203 // makes the item ignore window's visibility status
204 wxSizerFlags& ReserveSpaceEvenIfHidden()
205 {
206 m_flags |= wxRESERVE_SPACE_EVEN_IF_HIDDEN;
207 return *this;
208 }
209
210 // accessors for wxSizer only
211 int GetProportion() const { return m_proportion; }
212 int GetFlags() const { return m_flags; }
213 int GetBorderInPixels() const { return m_borderInPixels; }
214
215 private:
216 int m_proportion;
217 int m_flags;
218 int m_borderInPixels;
219 };
220
221
222 // ----------------------------------------------------------------------------
223 // wxSizerSpacer: used by wxSizerItem to represent a spacer
224 // ----------------------------------------------------------------------------
225
226 class WXDLLIMPEXP_CORE wxSizerSpacer
227 {
228 public:
229 wxSizerSpacer(const wxSize& size) : m_size(size), m_isShown(true) { }
230
231 void SetSize(const wxSize& size) { m_size = size; }
232 const wxSize& GetSize() const { return m_size; }
233
234 void Show(bool show) { m_isShown = show; }
235 bool IsShown() const { return m_isShown; }
236
237 private:
238 // the size, in pixel
239 wxSize m_size;
240
241 // is the spacer currently shown?
242 bool m_isShown;
243 };
244
245 // ----------------------------------------------------------------------------
246 // wxSizerItem
247 // ----------------------------------------------------------------------------
248
249 class WXDLLIMPEXP_CORE wxSizerItem : public wxObject
250 {
251 public:
252 // window
253 wxSizerItem( wxWindow *window,
254 int proportion=0,
255 int flag=0,
256 int border=0,
257 wxObject* userData=NULL );
258
259 // window with flags
260 wxSizerItem(wxWindow *window, const wxSizerFlags& flags)
261 {
262 Init(flags);
263
264 DoSetWindow(window);
265 }
266
267 // subsizer
268 wxSizerItem( wxSizer *sizer,
269 int proportion=0,
270 int flag=0,
271 int border=0,
272 wxObject* userData=NULL );
273
274 // sizer with flags
275 wxSizerItem(wxSizer *sizer, const wxSizerFlags& flags)
276 {
277 Init(flags);
278
279 DoSetSizer(sizer);
280 }
281
282 // spacer
283 wxSizerItem( int width,
284 int height,
285 int proportion=0,
286 int flag=0,
287 int border=0,
288 wxObject* userData=NULL);
289
290 // spacer with flags
291 wxSizerItem(int width, int height, const wxSizerFlags& flags)
292 {
293 Init(flags);
294
295 DoSetSpacer(wxSize(width, height));
296 }
297
298 wxSizerItem();
299 virtual ~wxSizerItem();
300
301 virtual void DeleteWindows();
302
303 // Enable deleting the SizerItem without destroying the contained sizer.
304 void DetachSizer() { m_sizer = NULL; }
305
306 virtual wxSize GetSize() const;
307 virtual wxSize CalcMin();
308 virtual void SetDimension( const wxPoint& pos, const wxSize& size );
309
310 wxSize GetMinSize() const
311 { return m_minSize; }
312 wxSize GetMinSizeWithBorder() const;
313
314 void SetMinSize(const wxSize& size)
315 {
316 if ( IsWindow() )
317 m_window->SetMinSize(size);
318 m_minSize = size;
319 }
320 void SetMinSize( int x, int y )
321 { SetMinSize(wxSize(x, y)); }
322 void SetInitSize( int x, int y )
323 { SetMinSize(wxSize(x, y)); }
324
325 // if either of dimensions is zero, ratio is assumed to be 1
326 // to avoid "divide by zero" errors
327 void SetRatio(int width, int height)
328 { m_ratio = (width && height) ? ((float) width / (float) height) : 1; }
329 void SetRatio(const wxSize& size)
330 { SetRatio(size.x, size.y); }
331 void SetRatio(float ratio)
332 { m_ratio = ratio; }
333 float GetRatio() const
334 { return m_ratio; }
335
336 virtual wxRect GetRect() { return m_rect; }
337
338 // set a sizer item id (different from a window id, all sizer items,
339 // including spacers, can have an associated id)
340 void SetId(int id) { m_id = id; }
341 int GetId() const { return m_id; }
342
343 bool IsWindow() const { return m_kind == Item_Window; }
344 bool IsSizer() const { return m_kind == Item_Sizer; }
345 bool IsSpacer() const { return m_kind == Item_Spacer; }
346
347 #if WXWIN_COMPATIBILITY_2_6
348 // Deprecated in 2.6, use {G,S}etProportion instead.
349 wxDEPRECATED( void SetOption( int option ) );
350 wxDEPRECATED( int GetOption() const );
351 #endif // WXWIN_COMPATIBILITY_2_6
352
353 void SetProportion( int proportion )
354 { m_proportion = proportion; }
355 int GetProportion() const
356 { return m_proportion; }
357 void SetFlag( int flag )
358 { m_flag = flag; }
359 int GetFlag() const
360 { return m_flag; }
361 void SetBorder( int border )
362 { m_border = border; }
363 int GetBorder() const
364 { return m_border; }
365
366 wxWindow *GetWindow() const
367 { return m_kind == Item_Window ? m_window : NULL; }
368 wxSizer *GetSizer() const
369 { return m_kind == Item_Sizer ? m_sizer : NULL; }
370 wxSize GetSpacer() const;
371
372 // This function behaves obviously for the windows and spacers but for the
373 // sizers it returns true if any sizer element is shown and only returns
374 // false if all of them are hidden. Also, it always returns true if
375 // wxRESERVE_SPACE_EVEN_IF_HIDDEN flag was used.
376 bool IsShown() const;
377
378 void Show(bool show);
379
380 void SetUserData(wxObject* userData)
381 { delete m_userData; m_userData = userData; }
382 wxObject* GetUserData() const
383 { return m_userData; }
384 wxPoint GetPosition() const
385 { return m_pos; }
386
387 // Called once the first component of an item has been decided. This is
388 // used in algorithms that depend on knowing the size in one direction
389 // before the min size in the other direction can be known.
390 // Returns true if it made use of the information (and min size was changed).
391 bool InformFirstDirection( int direction, int size, int availableOtherDir=-1 );
392
393 // these functions delete the current contents of the item if it's a sizer
394 // or a spacer but not if it is a window
395 void AssignWindow(wxWindow *window)
396 {
397 Free();
398 DoSetWindow(window);
399 }
400
401 void AssignSizer(wxSizer *sizer)
402 {
403 Free();
404 DoSetSizer(sizer);
405 }
406
407 void AssignSpacer(const wxSize& size)
408 {
409 Free();
410 DoSetSpacer(size);
411 }
412
413 void AssignSpacer(int w, int h) { AssignSpacer(wxSize(w, h)); }
414
415 #if WXWIN_COMPATIBILITY_2_8
416 // these functions do not free the old sizer/spacer and so can easily
417 // provoke the memory leaks and so shouldn't be used, use Assign() instead
418 wxDEPRECATED( void SetWindow(wxWindow *window) );
419 wxDEPRECATED( void SetSizer(wxSizer *sizer) );
420 wxDEPRECATED( void SetSpacer(const wxSize& size) );
421 wxDEPRECATED( void SetSpacer(int width, int height) );
422 #endif // WXWIN_COMPATIBILITY_2_8
423
424 protected:
425 // common part of several ctors
426 void Init() { m_userData = NULL; m_kind = Item_None; }
427
428 // common part of ctors taking wxSizerFlags
429 void Init(const wxSizerFlags& flags);
430
431 // free current contents
432 void Free();
433
434 // common parts of Set/AssignXXX()
435 void DoSetWindow(wxWindow *window);
436 void DoSetSizer(wxSizer *sizer);
437 void DoSetSpacer(const wxSize& size);
438
439 // Add the border specified for this item to the given size
440 // if it's != wxDefaultSize, just return wxDefaultSize otherwise.
441 wxSize AddBorderToSize(const wxSize& size) const;
442
443 // discriminated union: depending on m_kind one of the fields is valid
444 enum
445 {
446 Item_None,
447 Item_Window,
448 Item_Sizer,
449 Item_Spacer,
450 Item_Max
451 } m_kind;
452 union
453 {
454 wxWindow *m_window;
455 wxSizer *m_sizer;
456 wxSizerSpacer *m_spacer;
457 };
458
459 wxPoint m_pos;
460 wxSize m_minSize;
461 int m_proportion;
462 int m_border;
463 int m_flag;
464 int m_id;
465
466 // on screen rectangle of this item (not including borders)
467 wxRect m_rect;
468
469 // Aspect ratio can always be calculated from m_size,
470 // but this would cause precision loss when the window
471 // is shrunk. It is safer to preserve the initial value.
472 float m_ratio;
473
474 wxObject *m_userData;
475
476 private:
477 DECLARE_CLASS(wxSizerItem)
478 wxDECLARE_NO_COPY_CLASS(wxSizerItem);
479 };
480
481 WX_DECLARE_EXPORTED_LIST( wxSizerItem, wxSizerItemList );
482
483
484 //---------------------------------------------------------------------------
485 // wxSizer
486 //---------------------------------------------------------------------------
487
488 class WXDLLIMPEXP_CORE wxSizer: public wxObject, public wxClientDataContainer
489 {
490 public:
491 wxSizer() { m_containingWindow = NULL; }
492 virtual ~wxSizer();
493
494 // methods for adding elements to the sizer: there are Add/Insert/Prepend
495 // overloads for each of window/sizer/spacer/wxSizerItem
496 wxSizerItem* Add(wxWindow *window,
497 int proportion = 0,
498 int flag = 0,
499 int border = 0,
500 wxObject* userData = NULL);
501 wxSizerItem* Add(wxSizer *sizer,
502 int proportion = 0,
503 int flag = 0,
504 int border = 0,
505 wxObject* userData = NULL);
506 wxSizerItem* Add(int width,
507 int height,
508 int proportion = 0,
509 int flag = 0,
510 int border = 0,
511 wxObject* userData = NULL);
512 wxSizerItem* Add( wxWindow *window, const wxSizerFlags& flags);
513 wxSizerItem* Add( wxSizer *sizer, const wxSizerFlags& flags);
514 wxSizerItem* Add( int width, int height, const wxSizerFlags& flags);
515 wxSizerItem* Add( wxSizerItem *item);
516
517 virtual wxSizerItem *AddSpacer(int size);
518 wxSizerItem* AddStretchSpacer(int prop = 1);
519
520 wxSizerItem* Insert(size_t index,
521 wxWindow *window,
522 int proportion = 0,
523 int flag = 0,
524 int border = 0,
525 wxObject* userData = NULL);
526 wxSizerItem* Insert(size_t index,
527 wxSizer *sizer,
528 int proportion = 0,
529 int flag = 0,
530 int border = 0,
531 wxObject* userData = NULL);
532 wxSizerItem* Insert(size_t index,
533 int width,
534 int height,
535 int proportion = 0,
536 int flag = 0,
537 int border = 0,
538 wxObject* userData = NULL);
539 wxSizerItem* Insert(size_t index,
540 wxWindow *window,
541 const wxSizerFlags& flags);
542 wxSizerItem* Insert(size_t index,
543 wxSizer *sizer,
544 const wxSizerFlags& flags);
545 wxSizerItem* Insert(size_t index,
546 int width,
547 int height,
548 const wxSizerFlags& flags);
549
550 // NB: do _not_ override this function in the derived classes, this one is
551 // virtual for compatibility reasons only to allow old code overriding
552 // it to continue to work, override DoInsert() instead in the new code
553 virtual wxSizerItem* Insert(size_t index, wxSizerItem *item);
554
555 wxSizerItem* InsertSpacer(size_t index, int size);
556 wxSizerItem* InsertStretchSpacer(size_t index, int prop = 1);
557
558 wxSizerItem* Prepend(wxWindow *window,
559 int proportion = 0,
560 int flag = 0,
561 int border = 0,
562 wxObject* userData = NULL);
563 wxSizerItem* Prepend(wxSizer *sizer,
564 int proportion = 0,
565 int flag = 0,
566 int border = 0,
567 wxObject* userData = NULL);
568 wxSizerItem* Prepend(int width,
569 int height,
570 int proportion = 0,
571 int flag = 0,
572 int border = 0,
573 wxObject* userData = NULL);
574 wxSizerItem* Prepend(wxWindow *window, const wxSizerFlags& flags);
575 wxSizerItem* Prepend(wxSizer *sizer, const wxSizerFlags& flags);
576 wxSizerItem* Prepend(int width, int height, const wxSizerFlags& flags);
577 wxSizerItem* Prepend(wxSizerItem *item);
578
579 wxSizerItem* PrependSpacer(int size);
580 wxSizerItem* PrependStretchSpacer(int prop = 1);
581
582 // set (or possibly unset if window is NULL) or get the window this sizer
583 // is used in
584 void SetContainingWindow(wxWindow *window);
585 wxWindow *GetContainingWindow() const { return m_containingWindow; }
586
587 #if WXWIN_COMPATIBILITY_2_6
588 // Deprecated in 2.6 since historically it does not delete the window,
589 // use Detach instead.
590 wxDEPRECATED( virtual bool Remove( wxWindow *window ) );
591 #endif // WXWIN_COMPATIBILITY_2_6
592
593 virtual bool Remove( wxSizer *sizer );
594 virtual bool Remove( int index );
595
596 virtual bool Detach( wxWindow *window );
597 virtual bool Detach( wxSizer *sizer );
598 virtual bool Detach( int index );
599
600 virtual bool Replace( wxWindow *oldwin, wxWindow *newwin, bool recursive = false );
601 virtual bool Replace( wxSizer *oldsz, wxSizer *newsz, bool recursive = false );
602 virtual bool Replace( size_t index, wxSizerItem *newitem );
603
604 virtual void Clear( bool delete_windows = false );
605 virtual void DeleteWindows();
606
607 // Inform sizer about the first direction that has been decided (by parent item)
608 // Returns true if it made use of the information (and recalculated min size)
609 virtual bool InformFirstDirection( int WXUNUSED(direction), int WXUNUSED(size), int WXUNUSED(availableOtherDir) )
610 { return false; }
611
612 void SetMinSize( int width, int height )
613 { DoSetMinSize( width, height ); }
614 void SetMinSize( const wxSize& size )
615 { DoSetMinSize( size.x, size.y ); }
616
617 // Searches recursively
618 bool SetItemMinSize( wxWindow *window, int width, int height )
619 { return DoSetItemMinSize( window, width, height ); }
620 bool SetItemMinSize( wxWindow *window, const wxSize& size )
621 { return DoSetItemMinSize( window, size.x, size.y ); }
622
623 // Searches recursively
624 bool SetItemMinSize( wxSizer *sizer, int width, int height )
625 { return DoSetItemMinSize( sizer, width, height ); }
626 bool SetItemMinSize( wxSizer *sizer, const wxSize& size )
627 { return DoSetItemMinSize( sizer, size.x, size.y ); }
628
629 bool SetItemMinSize( size_t index, int width, int height )
630 { return DoSetItemMinSize( index, width, height ); }
631 bool SetItemMinSize( size_t index, const wxSize& size )
632 { return DoSetItemMinSize( index, size.x, size.y ); }
633
634 wxSize GetSize() const
635 { return m_size; }
636 wxPoint GetPosition() const
637 { return m_position; }
638
639 // Calculate the minimal size or return m_minSize if bigger.
640 wxSize GetMinSize();
641
642 // These virtual functions are used by the layout algorithm: first
643 // CalcMin() is called to calculate the minimal size of the sizer and
644 // prepare for laying it out and then RecalcSizes() is called to really
645 // update all the sizer items
646 virtual wxSize CalcMin() = 0;
647 virtual void RecalcSizes() = 0;
648
649 virtual void Layout();
650
651 wxSize ComputeFittingClientSize(wxWindow *window);
652 wxSize ComputeFittingWindowSize(wxWindow *window);
653
654 wxSize Fit( wxWindow *window );
655 void FitInside( wxWindow *window );
656 void SetSizeHints( wxWindow *window );
657 #if WXWIN_COMPATIBILITY_2_8
658 // This only calls FitInside() since 2.9
659 wxDEPRECATED( void SetVirtualSizeHints( wxWindow *window ) );
660 #endif
661
662 wxSizerItemList& GetChildren()
663 { return m_children; }
664 const wxSizerItemList& GetChildren() const
665 { return m_children; }
666
667 void SetDimension(const wxPoint& pos, const wxSize& size)
668 {
669 m_position = pos;
670 m_size = size;
671 Layout();
672
673 // This call is required for wxWrapSizer to be able to calculate its
674 // minimal size correctly.
675 InformFirstDirection(wxHORIZONTAL, size.x, size.y);
676 }
677 void SetDimension(int x, int y, int width, int height)
678 { SetDimension(wxPoint(x, y), wxSize(width, height)); }
679
680 size_t GetItemCount() const { return m_children.GetCount(); }
681 bool IsEmpty() const { return m_children.IsEmpty(); }
682
683 wxSizerItem* GetItem( wxWindow *window, bool recursive = false );
684 wxSizerItem* GetItem( wxSizer *sizer, bool recursive = false );
685 wxSizerItem* GetItem( size_t index );
686 wxSizerItem* GetItemById( int id, bool recursive = false );
687
688 // Manage whether individual scene items are considered
689 // in the layout calculations or not.
690 bool Show( wxWindow *window, bool show = true, bool recursive = false );
691 bool Show( wxSizer *sizer, bool show = true, bool recursive = false );
692 bool Show( size_t index, bool show = true );
693
694 bool Hide( wxSizer *sizer, bool recursive = false )
695 { return Show( sizer, false, recursive ); }
696 bool Hide( wxWindow *window, bool recursive = false )
697 { return Show( window, false, recursive ); }
698 bool Hide( size_t index )
699 { return Show( index, false ); }
700
701 bool IsShown( wxWindow *window ) const;
702 bool IsShown( wxSizer *sizer ) const;
703 bool IsShown( size_t index ) const;
704
705 // Recursively call wxWindow::Show () on all sizer items.
706 virtual void ShowItems (bool show);
707
708 void Show(bool show) { ShowItems(show); }
709
710 protected:
711 wxSize m_size;
712 wxSize m_minSize;
713 wxPoint m_position;
714 wxSizerItemList m_children;
715
716 // the window this sizer is used in, can be NULL
717 wxWindow *m_containingWindow;
718
719 wxSize GetMaxClientSize( wxWindow *window ) const;
720 wxSize GetMinClientSize( wxWindow *window );
721 wxSize VirtualFitSize( wxWindow *window );
722
723 virtual void DoSetMinSize( int width, int height );
724 virtual bool DoSetItemMinSize( wxWindow *window, int width, int height );
725 virtual bool DoSetItemMinSize( wxSizer *sizer, int width, int height );
726 virtual bool DoSetItemMinSize( size_t index, int width, int height );
727
728 // insert a new item into m_children at given index and return the item
729 // itself
730 virtual wxSizerItem* DoInsert(size_t index, wxSizerItem *item);
731
732 private:
733 DECLARE_CLASS(wxSizer)
734 };
735
736 //---------------------------------------------------------------------------
737 // wxGridSizer
738 //---------------------------------------------------------------------------
739
740 class WXDLLIMPEXP_CORE wxGridSizer: public wxSizer
741 {
742 public:
743 // ctors specifying the number of columns only: number of rows will be
744 // deduced automatically depending on the number of sizer elements
745 wxGridSizer( int cols, int vgap, int hgap );
746 wxGridSizer( int cols, const wxSize& gap = wxSize(0, 0) );
747
748 // ctors specifying the number of rows and columns
749 wxGridSizer( int rows, int cols, int vgap, int hgap );
750 wxGridSizer( int rows, int cols, const wxSize& gap );
751
752 virtual void RecalcSizes();
753 virtual wxSize CalcMin();
754
755 void SetCols( int cols )
756 {
757 wxASSERT_MSG( cols >= 0, "Number of columns must be non-negative");
758 m_cols = cols;
759 }
760
761 void SetRows( int rows )
762 {
763 wxASSERT_MSG( rows >= 0, "Number of rows must be non-negative");
764 m_rows = rows;
765 }
766
767 void SetVGap( int gap ) { m_vgap = gap; }
768 void SetHGap( int gap ) { m_hgap = gap; }
769 int GetCols() const { return m_cols; }
770 int GetRows() const { return m_rows; }
771 int GetVGap() const { return m_vgap; }
772 int GetHGap() const { return m_hgap; }
773
774 int GetEffectiveColsCount() const { return m_cols ? m_cols : CalcCols(); }
775 int GetEffectiveRowsCount() const { return m_rows ? m_rows : CalcRows(); }
776
777 // return the number of total items and the number of columns and rows
778 // (for internal use only)
779 int CalcRowsCols(int& rows, int& cols) const;
780
781 protected:
782 // the number of rows/columns in the sizer, if 0 then it is determined
783 // dynamically depending on the total number of items
784 int m_rows;
785 int m_cols;
786
787 // gaps between rows and columns
788 int m_vgap;
789 int m_hgap;
790
791 virtual wxSizerItem *DoInsert(size_t index, wxSizerItem *item);
792
793 void SetItemBounds( wxSizerItem *item, int x, int y, int w, int h );
794
795 // returns the number of columns/rows needed for the current total number
796 // of children (and the fixed number of rows/columns)
797 int CalcCols() const
798 {
799 wxCHECK_MSG
800 (
801 m_rows, 0,
802 "Can't calculate number of cols if number of rows is not specified"
803 );
804
805 return (m_children.GetCount() + m_rows - 1) / m_rows;
806 }
807
808 int CalcRows() const
809 {
810 wxCHECK_MSG
811 (
812 m_cols, 0,
813 "Can't calculate number of cols if number of rows is not specified"
814 );
815
816 return (m_children.GetCount() + m_cols - 1) / m_cols;
817 }
818
819 private:
820 DECLARE_CLASS(wxGridSizer)
821 };
822
823 //---------------------------------------------------------------------------
824 // wxFlexGridSizer
825 //---------------------------------------------------------------------------
826
827 // values which define the behaviour for resizing wxFlexGridSizer cells in the
828 // "non-flexible" direction
829 enum wxFlexSizerGrowMode
830 {
831 // don't resize the cells in non-flexible direction at all
832 wxFLEX_GROWMODE_NONE,
833
834 // uniformly resize only the specified ones (default)
835 wxFLEX_GROWMODE_SPECIFIED,
836
837 // uniformly resize all cells
838 wxFLEX_GROWMODE_ALL
839 };
840
841 class WXDLLIMPEXP_CORE wxFlexGridSizer: public wxGridSizer
842 {
843 public:
844 // ctors specifying the number of columns only: number of rows will be
845 // deduced automatically depending on the number of sizer elements
846 wxFlexGridSizer( int cols, int vgap, int hgap );
847 wxFlexGridSizer( int cols, const wxSize& gap = wxSize(0, 0) );
848
849 // ctors specifying the number of rows and columns
850 wxFlexGridSizer( int rows, int cols, int vgap, int hgap );
851 wxFlexGridSizer( int rows, int cols, const wxSize& gap );
852
853 // dtor
854 virtual ~wxFlexGridSizer();
855
856 // set the rows/columns which will grow (the others will remain of the
857 // constant initial size)
858 void AddGrowableRow( size_t idx, int proportion = 0 );
859 void RemoveGrowableRow( size_t idx );
860 void AddGrowableCol( size_t idx, int proportion = 0 );
861 void RemoveGrowableCol( size_t idx );
862
863 bool IsRowGrowable( size_t idx );
864 bool IsColGrowable( size_t idx );
865
866 // the sizer cells may grow in both directions, not grow at all or only
867 // grow in one direction but not the other
868
869 // the direction may be wxVERTICAL, wxHORIZONTAL or wxBOTH (default)
870 void SetFlexibleDirection(int direction) { m_flexDirection = direction; }
871 int GetFlexibleDirection() const { return m_flexDirection; }
872
873 // note that the grow mode only applies to the direction which is not
874 // flexible
875 void SetNonFlexibleGrowMode(wxFlexSizerGrowMode mode) { m_growMode = mode; }
876 wxFlexSizerGrowMode GetNonFlexibleGrowMode() const { return m_growMode; }
877
878 // Read-only access to the row heights and col widths arrays
879 const wxArrayInt& GetRowHeights() const { return m_rowHeights; }
880 const wxArrayInt& GetColWidths() const { return m_colWidths; }
881
882 // implementation
883 virtual void RecalcSizes();
884 virtual wxSize CalcMin();
885
886 protected:
887 void AdjustForFlexDirection();
888 void AdjustForGrowables(const wxSize& sz);
889 void FindWidthsAndHeights(int nrows, int ncols);
890
891 // the heights/widths of all rows/columns
892 wxArrayInt m_rowHeights,
893 m_colWidths;
894
895 // indices of the growable columns and rows
896 wxArrayInt m_growableRows,
897 m_growableCols;
898
899 // proportion values of the corresponding growable rows and columns
900 wxArrayInt m_growableRowsProportions,
901 m_growableColsProportions;
902
903 // parameters describing whether the growable cells should be resized in
904 // both directions or only one
905 int m_flexDirection;
906 wxFlexSizerGrowMode m_growMode;
907
908 // saves CalcMin result to optimize RecalcSizes
909 wxSize m_calculatedMinSize;
910
911 private:
912 DECLARE_CLASS(wxFlexGridSizer)
913 wxDECLARE_NO_COPY_CLASS(wxFlexGridSizer);
914 };
915
916 //---------------------------------------------------------------------------
917 // wxBoxSizer
918 //---------------------------------------------------------------------------
919
920 class WXDLLIMPEXP_CORE wxBoxSizer: public wxSizer
921 {
922 public:
923 wxBoxSizer(int orient)
924 {
925 m_orient = orient;
926 m_totalProportion = 0;
927
928 wxASSERT_MSG( m_orient == wxHORIZONTAL || m_orient == wxVERTICAL,
929 wxT("invalid value for wxBoxSizer orientation") );
930 }
931
932 virtual wxSizerItem *AddSpacer(int size);
933
934 int GetOrientation() const { return m_orient; }
935
936 bool IsVertical() const { return m_orient == wxVERTICAL; }
937
938 void SetOrientation(int orient) { m_orient = orient; }
939
940 // implementation of our resizing logic
941 virtual wxSize CalcMin();
942 virtual void RecalcSizes();
943
944 protected:
945 // helpers for our code: this returns the component of the given wxSize in
946 // the direction of the sizer and in the other direction, respectively
947 int GetSizeInMajorDir(const wxSize& sz) const
948 {
949 return m_orient == wxHORIZONTAL ? sz.x : sz.y;
950 }
951
952 int& SizeInMajorDir(wxSize& sz)
953 {
954 return m_orient == wxHORIZONTAL ? sz.x : sz.y;
955 }
956
957 int& PosInMajorDir(wxPoint& pt)
958 {
959 return m_orient == wxHORIZONTAL ? pt.x : pt.y;
960 }
961
962 int GetSizeInMinorDir(const wxSize& sz) const
963 {
964 return m_orient == wxHORIZONTAL ? sz.y : sz.x;
965 }
966
967 int& SizeInMinorDir(wxSize& sz)
968 {
969 return m_orient == wxHORIZONTAL ? sz.y : sz.x;
970 }
971
972 int& PosInMinorDir(wxPoint& pt)
973 {
974 return m_orient == wxHORIZONTAL ? pt.y : pt.x;
975 }
976
977 // another helper: creates wxSize from major and minor components
978 wxSize SizeFromMajorMinor(int major, int minor) const
979 {
980 if ( m_orient == wxHORIZONTAL )
981 {
982 return wxSize(major, minor);
983 }
984 else // wxVERTICAL
985 {
986 return wxSize(minor, major);
987 }
988 }
989
990
991 // either wxHORIZONTAL or wxVERTICAL
992 int m_orient;
993
994 // the sum of proportion of all of our elements
995 int m_totalProportion;
996
997 // the minimal size needed for this sizer as calculated by the last call to
998 // our CalcMin()
999 wxSize m_minSize;
1000
1001 private:
1002 DECLARE_CLASS(wxBoxSizer)
1003 };
1004
1005 //---------------------------------------------------------------------------
1006 // wxStaticBoxSizer
1007 //---------------------------------------------------------------------------
1008
1009 #if wxUSE_STATBOX
1010
1011 class WXDLLIMPEXP_FWD_CORE wxStaticBox;
1012
1013 class WXDLLIMPEXP_CORE wxStaticBoxSizer: public wxBoxSizer
1014 {
1015 public:
1016 wxStaticBoxSizer(wxStaticBox *box, int orient);
1017 wxStaticBoxSizer(int orient, wxWindow *win, const wxString& label = wxEmptyString);
1018 virtual ~wxStaticBoxSizer();
1019
1020 void RecalcSizes();
1021 wxSize CalcMin();
1022
1023 wxStaticBox *GetStaticBox() const
1024 { return m_staticBox; }
1025
1026 // override to hide/show the static box as well
1027 virtual void ShowItems (bool show);
1028
1029 virtual bool Detach( wxWindow *window );
1030 virtual bool Detach( wxSizer *sizer ) { return wxBoxSizer::Detach(sizer); }
1031 virtual bool Detach( int index ) { return wxBoxSizer::Detach(index); }
1032
1033 protected:
1034 wxStaticBox *m_staticBox;
1035
1036 private:
1037 DECLARE_CLASS(wxStaticBoxSizer)
1038 wxDECLARE_NO_COPY_CLASS(wxStaticBoxSizer);
1039 };
1040
1041 #endif // wxUSE_STATBOX
1042
1043 //---------------------------------------------------------------------------
1044 // wxStdDialogButtonSizer
1045 //---------------------------------------------------------------------------
1046
1047 #if wxUSE_BUTTON
1048
1049 class WXDLLIMPEXP_CORE wxStdDialogButtonSizer: public wxBoxSizer
1050 {
1051 public:
1052 // Constructor just creates a new wxBoxSizer, not much else.
1053 // Box sizer orientation is automatically determined here:
1054 // vertical for PDAs, horizontal for everything else?
1055 wxStdDialogButtonSizer();
1056
1057 // Checks button ID against system IDs and sets one of the pointers below
1058 // to this button. Does not do any sizer-related things here.
1059 void AddButton(wxButton *button);
1060
1061 // Use these if no standard ID can/should be used
1062 void SetAffirmativeButton( wxButton *button );
1063 void SetNegativeButton( wxButton *button );
1064 void SetCancelButton( wxButton *button );
1065
1066 // All platform-specific code here, checks which buttons exist and add
1067 // them to the sizer accordingly.
1068 // Note - one potential hack on Mac we could use here,
1069 // if m_buttonAffirmative is wxID_SAVE then ensure wxID_SAVE
1070 // is set to _("Save") and m_buttonNegative is set to _("Don't Save")
1071 // I wouldn't add any other hacks like that into here,
1072 // but this one I can see being useful.
1073 void Realize();
1074
1075 wxButton *GetAffirmativeButton() const { return m_buttonAffirmative; }
1076 wxButton *GetApplyButton() const { return m_buttonApply; }
1077 wxButton *GetNegativeButton() const { return m_buttonNegative; }
1078 wxButton *GetCancelButton() const { return m_buttonCancel; }
1079 wxButton *GetHelpButton() const { return m_buttonHelp; }
1080
1081 protected:
1082 wxButton *m_buttonAffirmative; // wxID_OK, wxID_YES, wxID_SAVE go here
1083 wxButton *m_buttonApply; // wxID_APPLY
1084 wxButton *m_buttonNegative; // wxID_NO
1085 wxButton *m_buttonCancel; // wxID_CANCEL, wxID_CLOSE
1086 wxButton *m_buttonHelp; // wxID_HELP, wxID_CONTEXT_HELP
1087
1088 private:
1089 DECLARE_CLASS(wxStdDialogButtonSizer)
1090 wxDECLARE_NO_COPY_CLASS(wxStdDialogButtonSizer);
1091 };
1092
1093 #endif // wxUSE_BUTTON
1094
1095
1096 // ----------------------------------------------------------------------------
1097 // inline functions implementation
1098 // ----------------------------------------------------------------------------
1099
1100 #if WXWIN_COMPATIBILITY_2_8
1101
1102 inline void wxSizerItem::SetWindow(wxWindow *window)
1103 {
1104 DoSetWindow(window);
1105 }
1106
1107 inline void wxSizerItem::SetSizer(wxSizer *sizer)
1108 {
1109 DoSetSizer(sizer);
1110 }
1111
1112 inline void wxSizerItem::SetSpacer(const wxSize& size)
1113 {
1114 DoSetSpacer(size);
1115 }
1116
1117 inline void wxSizerItem::SetSpacer(int width, int height)
1118 {
1119 DoSetSpacer(wxSize(width, height));
1120 }
1121
1122 #endif // WXWIN_COMPATIBILITY_2_8
1123
1124 inline wxSizerItem*
1125 wxSizer::Insert(size_t index, wxSizerItem *item)
1126 {
1127 return DoInsert(index, item);
1128 }
1129
1130
1131 inline wxSizerItem*
1132 wxSizer::Add( wxSizerItem *item )
1133 {
1134 return Insert( m_children.GetCount(), item );
1135 }
1136
1137 inline wxSizerItem*
1138 wxSizer::Add( wxWindow *window, int proportion, int flag, int border, wxObject* userData )
1139 {
1140 return Add( new wxSizerItem( window, proportion, flag, border, userData ) );
1141 }
1142
1143 inline wxSizerItem*
1144 wxSizer::Add( wxSizer *sizer, int proportion, int flag, int border, wxObject* userData )
1145 {
1146 return Add( new wxSizerItem( sizer, proportion, flag, border, userData ) );
1147 }
1148
1149 inline wxSizerItem*
1150 wxSizer::Add( int width, int height, int proportion, int flag, int border, wxObject* userData )
1151 {
1152 return Add( new wxSizerItem( width, height, proportion, flag, border, userData ) );
1153 }
1154
1155 inline wxSizerItem*
1156 wxSizer::Add( wxWindow *window, const wxSizerFlags& flags )
1157 {
1158 return Add( new wxSizerItem(window, flags) );
1159 }
1160
1161 inline wxSizerItem*
1162 wxSizer::Add( wxSizer *sizer, const wxSizerFlags& flags )
1163 {
1164 return Add( new wxSizerItem(sizer, flags) );
1165 }
1166
1167 inline wxSizerItem*
1168 wxSizer::Add( int width, int height, const wxSizerFlags& flags )
1169 {
1170 return Add( new wxSizerItem(width, height, flags) );
1171 }
1172
1173 inline wxSizerItem*
1174 wxSizer::AddSpacer(int size)
1175 {
1176 return Add(size, size);
1177 }
1178
1179 inline wxSizerItem*
1180 wxSizer::AddStretchSpacer(int prop)
1181 {
1182 return Add(0, 0, prop);
1183 }
1184
1185 inline wxSizerItem*
1186 wxSizer::Prepend( wxSizerItem *item )
1187 {
1188 return Insert( 0, item );
1189 }
1190
1191 inline wxSizerItem*
1192 wxSizer::Prepend( wxWindow *window, int proportion, int flag, int border, wxObject* userData )
1193 {
1194 return Prepend( new wxSizerItem( window, proportion, flag, border, userData ) );
1195 }
1196
1197 inline wxSizerItem*
1198 wxSizer::Prepend( wxSizer *sizer, int proportion, int flag, int border, wxObject* userData )
1199 {
1200 return Prepend( new wxSizerItem( sizer, proportion, flag, border, userData ) );
1201 }
1202
1203 inline wxSizerItem*
1204 wxSizer::Prepend( int width, int height, int proportion, int flag, int border, wxObject* userData )
1205 {
1206 return Prepend( new wxSizerItem( width, height, proportion, flag, border, userData ) );
1207 }
1208
1209 inline wxSizerItem*
1210 wxSizer::PrependSpacer(int size)
1211 {
1212 return Prepend(size, size);
1213 }
1214
1215 inline wxSizerItem*
1216 wxSizer::PrependStretchSpacer(int prop)
1217 {
1218 return Prepend(0, 0, prop);
1219 }
1220
1221 inline wxSizerItem*
1222 wxSizer::Prepend( wxWindow *window, const wxSizerFlags& flags )
1223 {
1224 return Prepend( new wxSizerItem(window, flags) );
1225 }
1226
1227 inline wxSizerItem*
1228 wxSizer::Prepend( wxSizer *sizer, const wxSizerFlags& flags )
1229 {
1230 return Prepend( new wxSizerItem(sizer, flags) );
1231 }
1232
1233 inline wxSizerItem*
1234 wxSizer::Prepend( int width, int height, const wxSizerFlags& flags )
1235 {
1236 return Prepend( new wxSizerItem(width, height, flags) );
1237 }
1238
1239 inline wxSizerItem*
1240 wxSizer::Insert( size_t index,
1241 wxWindow *window,
1242 int proportion,
1243 int flag,
1244 int border,
1245 wxObject* userData )
1246 {
1247 return Insert( index, new wxSizerItem( window, proportion, flag, border, userData ) );
1248 }
1249
1250 inline wxSizerItem*
1251 wxSizer::Insert( size_t index,
1252 wxSizer *sizer,
1253 int proportion,
1254 int flag,
1255 int border,
1256 wxObject* userData )
1257 {
1258 return Insert( index, new wxSizerItem( sizer, proportion, flag, border, userData ) );
1259 }
1260
1261 inline wxSizerItem*
1262 wxSizer::Insert( size_t index,
1263 int width,
1264 int height,
1265 int proportion,
1266 int flag,
1267 int border,
1268 wxObject* userData )
1269 {
1270 return Insert( index, new wxSizerItem( width, height, proportion, flag, border, userData ) );
1271 }
1272
1273 inline wxSizerItem*
1274 wxSizer::Insert( size_t index, wxWindow *window, const wxSizerFlags& flags )
1275 {
1276 return Insert( index, new wxSizerItem(window, flags) );
1277 }
1278
1279 inline wxSizerItem*
1280 wxSizer::Insert( size_t index, wxSizer *sizer, const wxSizerFlags& flags )
1281 {
1282 return Insert( index, new wxSizerItem(sizer, flags) );
1283 }
1284
1285 inline wxSizerItem*
1286 wxSizer::Insert( size_t index, int width, int height, const wxSizerFlags& flags )
1287 {
1288 return Insert( index, new wxSizerItem(width, height, flags) );
1289 }
1290
1291 inline wxSizerItem*
1292 wxSizer::InsertSpacer(size_t index, int size)
1293 {
1294 return Insert(index, size, size);
1295 }
1296
1297 inline wxSizerItem*
1298 wxSizer::InsertStretchSpacer(size_t index, int prop)
1299 {
1300 return Insert(index, 0, 0, prop);
1301 }
1302
1303 #endif // __WXSIZER_H__