]> git.saurik.com Git - wxWidgets.git/blob - include/wx/sizer.h
Respect item max sizes in wxBoxSizer.
[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 wxSize GetMaxSize() const
315 { return IsWindow() ? m_window->GetMaxSize() : wxDefaultSize; }
316 wxSize GetMaxSizeWithBorder() const;
317
318 void SetMinSize(const wxSize& size)
319 {
320 if ( IsWindow() )
321 m_window->SetMinSize(size);
322 m_minSize = size;
323 }
324 void SetMinSize( int x, int y )
325 { SetMinSize(wxSize(x, y)); }
326 void SetInitSize( int x, int y )
327 { SetMinSize(wxSize(x, y)); }
328
329 // if either of dimensions is zero, ratio is assumed to be 1
330 // to avoid "divide by zero" errors
331 void SetRatio(int width, int height)
332 { m_ratio = (width && height) ? ((float) width / (float) height) : 1; }
333 void SetRatio(const wxSize& size)
334 { SetRatio(size.x, size.y); }
335 void SetRatio(float ratio)
336 { m_ratio = ratio; }
337 float GetRatio() const
338 { return m_ratio; }
339
340 virtual wxRect GetRect() { return m_rect; }
341
342 // set a sizer item id (different from a window id, all sizer items,
343 // including spacers, can have an associated id)
344 void SetId(int id) { m_id = id; }
345 int GetId() const { return m_id; }
346
347 bool IsWindow() const { return m_kind == Item_Window; }
348 bool IsSizer() const { return m_kind == Item_Sizer; }
349 bool IsSpacer() const { return m_kind == Item_Spacer; }
350
351 #if WXWIN_COMPATIBILITY_2_6
352 // Deprecated in 2.6, use {G,S}etProportion instead.
353 wxDEPRECATED( void SetOption( int option ) );
354 wxDEPRECATED( int GetOption() const );
355 #endif // WXWIN_COMPATIBILITY_2_6
356
357 void SetProportion( int proportion )
358 { m_proportion = proportion; }
359 int GetProportion() const
360 { return m_proportion; }
361 void SetFlag( int flag )
362 { m_flag = flag; }
363 int GetFlag() const
364 { return m_flag; }
365 void SetBorder( int border )
366 { m_border = border; }
367 int GetBorder() const
368 { return m_border; }
369
370 wxWindow *GetWindow() const
371 { return m_kind == Item_Window ? m_window : NULL; }
372 wxSizer *GetSizer() const
373 { return m_kind == Item_Sizer ? m_sizer : NULL; }
374 wxSize GetSpacer() const;
375
376 // This function behaves obviously for the windows and spacers but for the
377 // sizers it returns true if any sizer element is shown and only returns
378 // false if all of them are hidden. Also, it always returns true if
379 // wxRESERVE_SPACE_EVEN_IF_HIDDEN flag was used.
380 bool IsShown() const;
381
382 void Show(bool show);
383
384 void SetUserData(wxObject* userData)
385 { delete m_userData; m_userData = userData; }
386 wxObject* GetUserData() const
387 { return m_userData; }
388 wxPoint GetPosition() const
389 { return m_pos; }
390
391 // Called once the first component of an item has been decided. This is
392 // used in algorithms that depend on knowing the size in one direction
393 // before the min size in the other direction can be known.
394 // Returns true if it made use of the information (and min size was changed).
395 bool InformFirstDirection( int direction, int size, int availableOtherDir=-1 );
396
397 // these functions delete the current contents of the item if it's a sizer
398 // or a spacer but not if it is a window
399 void AssignWindow(wxWindow *window)
400 {
401 Free();
402 DoSetWindow(window);
403 }
404
405 void AssignSizer(wxSizer *sizer)
406 {
407 Free();
408 DoSetSizer(sizer);
409 }
410
411 void AssignSpacer(const wxSize& size)
412 {
413 Free();
414 DoSetSpacer(size);
415 }
416
417 void AssignSpacer(int w, int h) { AssignSpacer(wxSize(w, h)); }
418
419 #if WXWIN_COMPATIBILITY_2_8
420 // these functions do not free the old sizer/spacer and so can easily
421 // provoke the memory leaks and so shouldn't be used, use Assign() instead
422 wxDEPRECATED( void SetWindow(wxWindow *window) );
423 wxDEPRECATED( void SetSizer(wxSizer *sizer) );
424 wxDEPRECATED( void SetSpacer(const wxSize& size) );
425 wxDEPRECATED( void SetSpacer(int width, int height) );
426 #endif // WXWIN_COMPATIBILITY_2_8
427
428 protected:
429 // common part of several ctors
430 void Init() { m_userData = NULL; m_kind = Item_None; }
431
432 // common part of ctors taking wxSizerFlags
433 void Init(const wxSizerFlags& flags);
434
435 // free current contents
436 void Free();
437
438 // common parts of Set/AssignXXX()
439 void DoSetWindow(wxWindow *window);
440 void DoSetSizer(wxSizer *sizer);
441 void DoSetSpacer(const wxSize& size);
442
443 // Add the border specified for this item to the given size
444 // if it's != wxDefaultSize, just return wxDefaultSize otherwise.
445 wxSize AddBorderToSize(const wxSize& size) const;
446
447 // discriminated union: depending on m_kind one of the fields is valid
448 enum
449 {
450 Item_None,
451 Item_Window,
452 Item_Sizer,
453 Item_Spacer,
454 Item_Max
455 } m_kind;
456 union
457 {
458 wxWindow *m_window;
459 wxSizer *m_sizer;
460 wxSizerSpacer *m_spacer;
461 };
462
463 wxPoint m_pos;
464 wxSize m_minSize;
465 int m_proportion;
466 int m_border;
467 int m_flag;
468 int m_id;
469
470 // on screen rectangle of this item (not including borders)
471 wxRect m_rect;
472
473 // Aspect ratio can always be calculated from m_size,
474 // but this would cause precision loss when the window
475 // is shrunk. It is safer to preserve the initial value.
476 float m_ratio;
477
478 wxObject *m_userData;
479
480 private:
481 DECLARE_CLASS(wxSizerItem)
482 wxDECLARE_NO_COPY_CLASS(wxSizerItem);
483 };
484
485 WX_DECLARE_EXPORTED_LIST( wxSizerItem, wxSizerItemList );
486
487
488 //---------------------------------------------------------------------------
489 // wxSizer
490 //---------------------------------------------------------------------------
491
492 class WXDLLIMPEXP_CORE wxSizer: public wxObject, public wxClientDataContainer
493 {
494 public:
495 wxSizer() { m_containingWindow = NULL; }
496 virtual ~wxSizer();
497
498 // methods for adding elements to the sizer: there are Add/Insert/Prepend
499 // overloads for each of window/sizer/spacer/wxSizerItem
500 wxSizerItem* Add(wxWindow *window,
501 int proportion = 0,
502 int flag = 0,
503 int border = 0,
504 wxObject* userData = NULL);
505 wxSizerItem* Add(wxSizer *sizer,
506 int proportion = 0,
507 int flag = 0,
508 int border = 0,
509 wxObject* userData = NULL);
510 wxSizerItem* Add(int width,
511 int height,
512 int proportion = 0,
513 int flag = 0,
514 int border = 0,
515 wxObject* userData = NULL);
516 wxSizerItem* Add( wxWindow *window, const wxSizerFlags& flags);
517 wxSizerItem* Add( wxSizer *sizer, const wxSizerFlags& flags);
518 wxSizerItem* Add( int width, int height, const wxSizerFlags& flags);
519 wxSizerItem* Add( wxSizerItem *item);
520
521 virtual wxSizerItem *AddSpacer(int size);
522 wxSizerItem* AddStretchSpacer(int prop = 1);
523
524 wxSizerItem* Insert(size_t index,
525 wxWindow *window,
526 int proportion = 0,
527 int flag = 0,
528 int border = 0,
529 wxObject* userData = NULL);
530 wxSizerItem* Insert(size_t index,
531 wxSizer *sizer,
532 int proportion = 0,
533 int flag = 0,
534 int border = 0,
535 wxObject* userData = NULL);
536 wxSizerItem* Insert(size_t index,
537 int width,
538 int height,
539 int proportion = 0,
540 int flag = 0,
541 int border = 0,
542 wxObject* userData = NULL);
543 wxSizerItem* Insert(size_t index,
544 wxWindow *window,
545 const wxSizerFlags& flags);
546 wxSizerItem* Insert(size_t index,
547 wxSizer *sizer,
548 const wxSizerFlags& flags);
549 wxSizerItem* Insert(size_t index,
550 int width,
551 int height,
552 const wxSizerFlags& flags);
553
554 // NB: do _not_ override this function in the derived classes, this one is
555 // virtual for compatibility reasons only to allow old code overriding
556 // it to continue to work, override DoInsert() instead in the new code
557 virtual wxSizerItem* Insert(size_t index, wxSizerItem *item);
558
559 wxSizerItem* InsertSpacer(size_t index, int size);
560 wxSizerItem* InsertStretchSpacer(size_t index, int prop = 1);
561
562 wxSizerItem* Prepend(wxWindow *window,
563 int proportion = 0,
564 int flag = 0,
565 int border = 0,
566 wxObject* userData = NULL);
567 wxSizerItem* Prepend(wxSizer *sizer,
568 int proportion = 0,
569 int flag = 0,
570 int border = 0,
571 wxObject* userData = NULL);
572 wxSizerItem* Prepend(int width,
573 int height,
574 int proportion = 0,
575 int flag = 0,
576 int border = 0,
577 wxObject* userData = NULL);
578 wxSizerItem* Prepend(wxWindow *window, const wxSizerFlags& flags);
579 wxSizerItem* Prepend(wxSizer *sizer, const wxSizerFlags& flags);
580 wxSizerItem* Prepend(int width, int height, const wxSizerFlags& flags);
581 wxSizerItem* Prepend(wxSizerItem *item);
582
583 wxSizerItem* PrependSpacer(int size);
584 wxSizerItem* PrependStretchSpacer(int prop = 1);
585
586 // set (or possibly unset if window is NULL) or get the window this sizer
587 // is used in
588 void SetContainingWindow(wxWindow *window);
589 wxWindow *GetContainingWindow() const { return m_containingWindow; }
590
591 #if WXWIN_COMPATIBILITY_2_6
592 // Deprecated in 2.6 since historically it does not delete the window,
593 // use Detach instead.
594 wxDEPRECATED( virtual bool Remove( wxWindow *window ) );
595 #endif // WXWIN_COMPATIBILITY_2_6
596
597 virtual bool Remove( wxSizer *sizer );
598 virtual bool Remove( int index );
599
600 virtual bool Detach( wxWindow *window );
601 virtual bool Detach( wxSizer *sizer );
602 virtual bool Detach( int index );
603
604 virtual bool Replace( wxWindow *oldwin, wxWindow *newwin, bool recursive = false );
605 virtual bool Replace( wxSizer *oldsz, wxSizer *newsz, bool recursive = false );
606 virtual bool Replace( size_t index, wxSizerItem *newitem );
607
608 virtual void Clear( bool delete_windows = false );
609 virtual void DeleteWindows();
610
611 // Inform sizer about the first direction that has been decided (by parent item)
612 // Returns true if it made use of the information (and recalculated min size)
613 virtual bool InformFirstDirection( int WXUNUSED(direction), int WXUNUSED(size), int WXUNUSED(availableOtherDir) )
614 { return false; }
615
616 void SetMinSize( int width, int height )
617 { DoSetMinSize( width, height ); }
618 void SetMinSize( const wxSize& size )
619 { DoSetMinSize( size.x, size.y ); }
620
621 // Searches recursively
622 bool SetItemMinSize( wxWindow *window, int width, int height )
623 { return DoSetItemMinSize( window, width, height ); }
624 bool SetItemMinSize( wxWindow *window, const wxSize& size )
625 { return DoSetItemMinSize( window, size.x, size.y ); }
626
627 // Searches recursively
628 bool SetItemMinSize( wxSizer *sizer, int width, int height )
629 { return DoSetItemMinSize( sizer, width, height ); }
630 bool SetItemMinSize( wxSizer *sizer, const wxSize& size )
631 { return DoSetItemMinSize( sizer, size.x, size.y ); }
632
633 bool SetItemMinSize( size_t index, int width, int height )
634 { return DoSetItemMinSize( index, width, height ); }
635 bool SetItemMinSize( size_t index, const wxSize& size )
636 { return DoSetItemMinSize( index, size.x, size.y ); }
637
638 wxSize GetSize() const
639 { return m_size; }
640 wxPoint GetPosition() const
641 { return m_position; }
642
643 // Calculate the minimal size or return m_minSize if bigger.
644 wxSize GetMinSize();
645
646 // These virtual functions are used by the layout algorithm: first
647 // CalcMin() is called to calculate the minimal size of the sizer and
648 // prepare for laying it out and then RecalcSizes() is called to really
649 // update all the sizer items
650 virtual wxSize CalcMin() = 0;
651 virtual void RecalcSizes() = 0;
652
653 virtual void Layout();
654
655 wxSize ComputeFittingClientSize(wxWindow *window);
656 wxSize ComputeFittingWindowSize(wxWindow *window);
657
658 wxSize Fit( wxWindow *window );
659 void FitInside( wxWindow *window );
660 void SetSizeHints( wxWindow *window );
661 #if WXWIN_COMPATIBILITY_2_8
662 // This only calls FitInside() since 2.9
663 wxDEPRECATED( void SetVirtualSizeHints( wxWindow *window ) );
664 #endif
665
666 wxSizerItemList& GetChildren()
667 { return m_children; }
668 const wxSizerItemList& GetChildren() const
669 { return m_children; }
670
671 void SetDimension(const wxPoint& pos, const wxSize& size)
672 {
673 m_position = pos;
674 m_size = size;
675 Layout();
676
677 // This call is required for wxWrapSizer to be able to calculate its
678 // minimal size correctly.
679 InformFirstDirection(wxHORIZONTAL, size.x, size.y);
680 }
681 void SetDimension(int x, int y, int width, int height)
682 { SetDimension(wxPoint(x, y), wxSize(width, height)); }
683
684 size_t GetItemCount() const { return m_children.GetCount(); }
685 bool IsEmpty() const { return m_children.IsEmpty(); }
686
687 wxSizerItem* GetItem( wxWindow *window, bool recursive = false );
688 wxSizerItem* GetItem( wxSizer *sizer, bool recursive = false );
689 wxSizerItem* GetItem( size_t index );
690 wxSizerItem* GetItemById( int id, bool recursive = false );
691
692 // Manage whether individual scene items are considered
693 // in the layout calculations or not.
694 bool Show( wxWindow *window, bool show = true, bool recursive = false );
695 bool Show( wxSizer *sizer, bool show = true, bool recursive = false );
696 bool Show( size_t index, bool show = true );
697
698 bool Hide( wxSizer *sizer, bool recursive = false )
699 { return Show( sizer, false, recursive ); }
700 bool Hide( wxWindow *window, bool recursive = false )
701 { return Show( window, false, recursive ); }
702 bool Hide( size_t index )
703 { return Show( index, false ); }
704
705 bool IsShown( wxWindow *window ) const;
706 bool IsShown( wxSizer *sizer ) const;
707 bool IsShown( size_t index ) const;
708
709 // Recursively call wxWindow::Show () on all sizer items.
710 virtual void ShowItems (bool show);
711
712 void Show(bool show) { ShowItems(show); }
713
714 protected:
715 wxSize m_size;
716 wxSize m_minSize;
717 wxPoint m_position;
718 wxSizerItemList m_children;
719
720 // the window this sizer is used in, can be NULL
721 wxWindow *m_containingWindow;
722
723 wxSize GetMaxClientSize( wxWindow *window ) const;
724 wxSize GetMinClientSize( wxWindow *window );
725 wxSize VirtualFitSize( wxWindow *window );
726
727 virtual void DoSetMinSize( int width, int height );
728 virtual bool DoSetItemMinSize( wxWindow *window, int width, int height );
729 virtual bool DoSetItemMinSize( wxSizer *sizer, int width, int height );
730 virtual bool DoSetItemMinSize( size_t index, int width, int height );
731
732 // insert a new item into m_children at given index and return the item
733 // itself
734 virtual wxSizerItem* DoInsert(size_t index, wxSizerItem *item);
735
736 private:
737 DECLARE_CLASS(wxSizer)
738 };
739
740 //---------------------------------------------------------------------------
741 // wxGridSizer
742 //---------------------------------------------------------------------------
743
744 class WXDLLIMPEXP_CORE wxGridSizer: public wxSizer
745 {
746 public:
747 // ctors specifying the number of columns only: number of rows will be
748 // deduced automatically depending on the number of sizer elements
749 wxGridSizer( int cols, int vgap, int hgap );
750 wxGridSizer( int cols, const wxSize& gap = wxSize(0, 0) );
751
752 // ctors specifying the number of rows and columns
753 wxGridSizer( int rows, int cols, int vgap, int hgap );
754 wxGridSizer( int rows, int cols, const wxSize& gap );
755
756 virtual void RecalcSizes();
757 virtual wxSize CalcMin();
758
759 void SetCols( int cols )
760 {
761 wxASSERT_MSG( cols >= 0, "Number of columns must be non-negative");
762 m_cols = cols;
763 }
764
765 void SetRows( int rows )
766 {
767 wxASSERT_MSG( rows >= 0, "Number of rows must be non-negative");
768 m_rows = rows;
769 }
770
771 void SetVGap( int gap ) { m_vgap = gap; }
772 void SetHGap( int gap ) { m_hgap = gap; }
773 int GetCols() const { return m_cols; }
774 int GetRows() const { return m_rows; }
775 int GetVGap() const { return m_vgap; }
776 int GetHGap() const { return m_hgap; }
777
778 int GetEffectiveColsCount() const { return m_cols ? m_cols : CalcCols(); }
779 int GetEffectiveRowsCount() const { return m_rows ? m_rows : CalcRows(); }
780
781 // return the number of total items and the number of columns and rows
782 // (for internal use only)
783 int CalcRowsCols(int& rows, int& cols) const;
784
785 protected:
786 // the number of rows/columns in the sizer, if 0 then it is determined
787 // dynamically depending on the total number of items
788 int m_rows;
789 int m_cols;
790
791 // gaps between rows and columns
792 int m_vgap;
793 int m_hgap;
794
795 virtual wxSizerItem *DoInsert(size_t index, wxSizerItem *item);
796
797 void SetItemBounds( wxSizerItem *item, int x, int y, int w, int h );
798
799 // returns the number of columns/rows needed for the current total number
800 // of children (and the fixed number of rows/columns)
801 int CalcCols() const
802 {
803 wxCHECK_MSG
804 (
805 m_rows, 0,
806 "Can't calculate number of cols if number of rows is not specified"
807 );
808
809 return (m_children.GetCount() + m_rows - 1) / m_rows;
810 }
811
812 int CalcRows() const
813 {
814 wxCHECK_MSG
815 (
816 m_cols, 0,
817 "Can't calculate number of cols if number of rows is not specified"
818 );
819
820 return (m_children.GetCount() + m_cols - 1) / m_cols;
821 }
822
823 private:
824 DECLARE_CLASS(wxGridSizer)
825 };
826
827 //---------------------------------------------------------------------------
828 // wxFlexGridSizer
829 //---------------------------------------------------------------------------
830
831 // values which define the behaviour for resizing wxFlexGridSizer cells in the
832 // "non-flexible" direction
833 enum wxFlexSizerGrowMode
834 {
835 // don't resize the cells in non-flexible direction at all
836 wxFLEX_GROWMODE_NONE,
837
838 // uniformly resize only the specified ones (default)
839 wxFLEX_GROWMODE_SPECIFIED,
840
841 // uniformly resize all cells
842 wxFLEX_GROWMODE_ALL
843 };
844
845 class WXDLLIMPEXP_CORE wxFlexGridSizer: public wxGridSizer
846 {
847 public:
848 // ctors specifying the number of columns only: number of rows will be
849 // deduced automatically depending on the number of sizer elements
850 wxFlexGridSizer( int cols, int vgap, int hgap );
851 wxFlexGridSizer( int cols, const wxSize& gap = wxSize(0, 0) );
852
853 // ctors specifying the number of rows and columns
854 wxFlexGridSizer( int rows, int cols, int vgap, int hgap );
855 wxFlexGridSizer( int rows, int cols, const wxSize& gap );
856
857 // dtor
858 virtual ~wxFlexGridSizer();
859
860 // set the rows/columns which will grow (the others will remain of the
861 // constant initial size)
862 void AddGrowableRow( size_t idx, int proportion = 0 );
863 void RemoveGrowableRow( size_t idx );
864 void AddGrowableCol( size_t idx, int proportion = 0 );
865 void RemoveGrowableCol( size_t idx );
866
867 bool IsRowGrowable( size_t idx );
868 bool IsColGrowable( size_t idx );
869
870 // the sizer cells may grow in both directions, not grow at all or only
871 // grow in one direction but not the other
872
873 // the direction may be wxVERTICAL, wxHORIZONTAL or wxBOTH (default)
874 void SetFlexibleDirection(int direction) { m_flexDirection = direction; }
875 int GetFlexibleDirection() const { return m_flexDirection; }
876
877 // note that the grow mode only applies to the direction which is not
878 // flexible
879 void SetNonFlexibleGrowMode(wxFlexSizerGrowMode mode) { m_growMode = mode; }
880 wxFlexSizerGrowMode GetNonFlexibleGrowMode() const { return m_growMode; }
881
882 // Read-only access to the row heights and col widths arrays
883 const wxArrayInt& GetRowHeights() const { return m_rowHeights; }
884 const wxArrayInt& GetColWidths() const { return m_colWidths; }
885
886 // implementation
887 virtual void RecalcSizes();
888 virtual wxSize CalcMin();
889
890 protected:
891 void AdjustForFlexDirection();
892 void AdjustForGrowables(const wxSize& sz);
893 void FindWidthsAndHeights(int nrows, int ncols);
894
895 // the heights/widths of all rows/columns
896 wxArrayInt m_rowHeights,
897 m_colWidths;
898
899 // indices of the growable columns and rows
900 wxArrayInt m_growableRows,
901 m_growableCols;
902
903 // proportion values of the corresponding growable rows and columns
904 wxArrayInt m_growableRowsProportions,
905 m_growableColsProportions;
906
907 // parameters describing whether the growable cells should be resized in
908 // both directions or only one
909 int m_flexDirection;
910 wxFlexSizerGrowMode m_growMode;
911
912 // saves CalcMin result to optimize RecalcSizes
913 wxSize m_calculatedMinSize;
914
915 private:
916 DECLARE_CLASS(wxFlexGridSizer)
917 wxDECLARE_NO_COPY_CLASS(wxFlexGridSizer);
918 };
919
920 //---------------------------------------------------------------------------
921 // wxBoxSizer
922 //---------------------------------------------------------------------------
923
924 class WXDLLIMPEXP_CORE wxBoxSizer: public wxSizer
925 {
926 public:
927 wxBoxSizer(int orient)
928 {
929 m_orient = orient;
930 m_totalProportion = 0;
931
932 wxASSERT_MSG( m_orient == wxHORIZONTAL || m_orient == wxVERTICAL,
933 wxT("invalid value for wxBoxSizer orientation") );
934 }
935
936 virtual wxSizerItem *AddSpacer(int size);
937
938 int GetOrientation() const { return m_orient; }
939
940 bool IsVertical() const { return m_orient == wxVERTICAL; }
941
942 void SetOrientation(int orient) { m_orient = orient; }
943
944 // implementation of our resizing logic
945 virtual wxSize CalcMin();
946 virtual void RecalcSizes();
947
948 protected:
949 // helpers for our code: this returns the component of the given wxSize in
950 // the direction of the sizer and in the other direction, respectively
951 int GetSizeInMajorDir(const wxSize& sz) const
952 {
953 return m_orient == wxHORIZONTAL ? sz.x : sz.y;
954 }
955
956 int& SizeInMajorDir(wxSize& sz)
957 {
958 return m_orient == wxHORIZONTAL ? sz.x : sz.y;
959 }
960
961 int& PosInMajorDir(wxPoint& pt)
962 {
963 return m_orient == wxHORIZONTAL ? pt.x : pt.y;
964 }
965
966 int GetSizeInMinorDir(const wxSize& sz) const
967 {
968 return m_orient == wxHORIZONTAL ? sz.y : sz.x;
969 }
970
971 int& SizeInMinorDir(wxSize& sz)
972 {
973 return m_orient == wxHORIZONTAL ? sz.y : sz.x;
974 }
975
976 int& PosInMinorDir(wxPoint& pt)
977 {
978 return m_orient == wxHORIZONTAL ? pt.y : pt.x;
979 }
980
981 // another helper: creates wxSize from major and minor components
982 wxSize SizeFromMajorMinor(int major, int minor) const
983 {
984 if ( m_orient == wxHORIZONTAL )
985 {
986 return wxSize(major, minor);
987 }
988 else // wxVERTICAL
989 {
990 return wxSize(minor, major);
991 }
992 }
993
994
995 // either wxHORIZONTAL or wxVERTICAL
996 int m_orient;
997
998 // the sum of proportion of all of our elements
999 int m_totalProportion;
1000
1001 // the minimal size needed for this sizer as calculated by the last call to
1002 // our CalcMin()
1003 wxSize m_minSize;
1004
1005 private:
1006 DECLARE_CLASS(wxBoxSizer)
1007 };
1008
1009 //---------------------------------------------------------------------------
1010 // wxStaticBoxSizer
1011 //---------------------------------------------------------------------------
1012
1013 #if wxUSE_STATBOX
1014
1015 class WXDLLIMPEXP_FWD_CORE wxStaticBox;
1016
1017 class WXDLLIMPEXP_CORE wxStaticBoxSizer: public wxBoxSizer
1018 {
1019 public:
1020 wxStaticBoxSizer(wxStaticBox *box, int orient);
1021 wxStaticBoxSizer(int orient, wxWindow *win, const wxString& label = wxEmptyString);
1022 virtual ~wxStaticBoxSizer();
1023
1024 void RecalcSizes();
1025 wxSize CalcMin();
1026
1027 wxStaticBox *GetStaticBox() const
1028 { return m_staticBox; }
1029
1030 // override to hide/show the static box as well
1031 virtual void ShowItems (bool show);
1032
1033 virtual bool Detach( wxWindow *window );
1034 virtual bool Detach( wxSizer *sizer ) { return wxBoxSizer::Detach(sizer); }
1035 virtual bool Detach( int index ) { return wxBoxSizer::Detach(index); }
1036
1037 protected:
1038 wxStaticBox *m_staticBox;
1039
1040 private:
1041 DECLARE_CLASS(wxStaticBoxSizer)
1042 wxDECLARE_NO_COPY_CLASS(wxStaticBoxSizer);
1043 };
1044
1045 #endif // wxUSE_STATBOX
1046
1047 //---------------------------------------------------------------------------
1048 // wxStdDialogButtonSizer
1049 //---------------------------------------------------------------------------
1050
1051 #if wxUSE_BUTTON
1052
1053 class WXDLLIMPEXP_CORE wxStdDialogButtonSizer: public wxBoxSizer
1054 {
1055 public:
1056 // Constructor just creates a new wxBoxSizer, not much else.
1057 // Box sizer orientation is automatically determined here:
1058 // vertical for PDAs, horizontal for everything else?
1059 wxStdDialogButtonSizer();
1060
1061 // Checks button ID against system IDs and sets one of the pointers below
1062 // to this button. Does not do any sizer-related things here.
1063 void AddButton(wxButton *button);
1064
1065 // Use these if no standard ID can/should be used
1066 void SetAffirmativeButton( wxButton *button );
1067 void SetNegativeButton( wxButton *button );
1068 void SetCancelButton( wxButton *button );
1069
1070 // All platform-specific code here, checks which buttons exist and add
1071 // them to the sizer accordingly.
1072 // Note - one potential hack on Mac we could use here,
1073 // if m_buttonAffirmative is wxID_SAVE then ensure wxID_SAVE
1074 // is set to _("Save") and m_buttonNegative is set to _("Don't Save")
1075 // I wouldn't add any other hacks like that into here,
1076 // but this one I can see being useful.
1077 void Realize();
1078
1079 wxButton *GetAffirmativeButton() const { return m_buttonAffirmative; }
1080 wxButton *GetApplyButton() const { return m_buttonApply; }
1081 wxButton *GetNegativeButton() const { return m_buttonNegative; }
1082 wxButton *GetCancelButton() const { return m_buttonCancel; }
1083 wxButton *GetHelpButton() const { return m_buttonHelp; }
1084
1085 protected:
1086 wxButton *m_buttonAffirmative; // wxID_OK, wxID_YES, wxID_SAVE go here
1087 wxButton *m_buttonApply; // wxID_APPLY
1088 wxButton *m_buttonNegative; // wxID_NO
1089 wxButton *m_buttonCancel; // wxID_CANCEL, wxID_CLOSE
1090 wxButton *m_buttonHelp; // wxID_HELP, wxID_CONTEXT_HELP
1091
1092 private:
1093 DECLARE_CLASS(wxStdDialogButtonSizer)
1094 wxDECLARE_NO_COPY_CLASS(wxStdDialogButtonSizer);
1095 };
1096
1097 #endif // wxUSE_BUTTON
1098
1099
1100 // ----------------------------------------------------------------------------
1101 // inline functions implementation
1102 // ----------------------------------------------------------------------------
1103
1104 #if WXWIN_COMPATIBILITY_2_8
1105
1106 inline void wxSizerItem::SetWindow(wxWindow *window)
1107 {
1108 DoSetWindow(window);
1109 }
1110
1111 inline void wxSizerItem::SetSizer(wxSizer *sizer)
1112 {
1113 DoSetSizer(sizer);
1114 }
1115
1116 inline void wxSizerItem::SetSpacer(const wxSize& size)
1117 {
1118 DoSetSpacer(size);
1119 }
1120
1121 inline void wxSizerItem::SetSpacer(int width, int height)
1122 {
1123 DoSetSpacer(wxSize(width, height));
1124 }
1125
1126 #endif // WXWIN_COMPATIBILITY_2_8
1127
1128 inline wxSizerItem*
1129 wxSizer::Insert(size_t index, wxSizerItem *item)
1130 {
1131 return DoInsert(index, item);
1132 }
1133
1134
1135 inline wxSizerItem*
1136 wxSizer::Add( wxSizerItem *item )
1137 {
1138 return Insert( m_children.GetCount(), item );
1139 }
1140
1141 inline wxSizerItem*
1142 wxSizer::Add( wxWindow *window, int proportion, int flag, int border, wxObject* userData )
1143 {
1144 return Add( new wxSizerItem( window, proportion, flag, border, userData ) );
1145 }
1146
1147 inline wxSizerItem*
1148 wxSizer::Add( wxSizer *sizer, int proportion, int flag, int border, wxObject* userData )
1149 {
1150 return Add( new wxSizerItem( sizer, proportion, flag, border, userData ) );
1151 }
1152
1153 inline wxSizerItem*
1154 wxSizer::Add( int width, int height, int proportion, int flag, int border, wxObject* userData )
1155 {
1156 return Add( new wxSizerItem( width, height, proportion, flag, border, userData ) );
1157 }
1158
1159 inline wxSizerItem*
1160 wxSizer::Add( wxWindow *window, const wxSizerFlags& flags )
1161 {
1162 return Add( new wxSizerItem(window, flags) );
1163 }
1164
1165 inline wxSizerItem*
1166 wxSizer::Add( wxSizer *sizer, const wxSizerFlags& flags )
1167 {
1168 return Add( new wxSizerItem(sizer, flags) );
1169 }
1170
1171 inline wxSizerItem*
1172 wxSizer::Add( int width, int height, const wxSizerFlags& flags )
1173 {
1174 return Add( new wxSizerItem(width, height, flags) );
1175 }
1176
1177 inline wxSizerItem*
1178 wxSizer::AddSpacer(int size)
1179 {
1180 return Add(size, size);
1181 }
1182
1183 inline wxSizerItem*
1184 wxSizer::AddStretchSpacer(int prop)
1185 {
1186 return Add(0, 0, prop);
1187 }
1188
1189 inline wxSizerItem*
1190 wxSizer::Prepend( wxSizerItem *item )
1191 {
1192 return Insert( 0, item );
1193 }
1194
1195 inline wxSizerItem*
1196 wxSizer::Prepend( wxWindow *window, int proportion, int flag, int border, wxObject* userData )
1197 {
1198 return Prepend( new wxSizerItem( window, proportion, flag, border, userData ) );
1199 }
1200
1201 inline wxSizerItem*
1202 wxSizer::Prepend( wxSizer *sizer, int proportion, int flag, int border, wxObject* userData )
1203 {
1204 return Prepend( new wxSizerItem( sizer, proportion, flag, border, userData ) );
1205 }
1206
1207 inline wxSizerItem*
1208 wxSizer::Prepend( int width, int height, int proportion, int flag, int border, wxObject* userData )
1209 {
1210 return Prepend( new wxSizerItem( width, height, proportion, flag, border, userData ) );
1211 }
1212
1213 inline wxSizerItem*
1214 wxSizer::PrependSpacer(int size)
1215 {
1216 return Prepend(size, size);
1217 }
1218
1219 inline wxSizerItem*
1220 wxSizer::PrependStretchSpacer(int prop)
1221 {
1222 return Prepend(0, 0, prop);
1223 }
1224
1225 inline wxSizerItem*
1226 wxSizer::Prepend( wxWindow *window, const wxSizerFlags& flags )
1227 {
1228 return Prepend( new wxSizerItem(window, flags) );
1229 }
1230
1231 inline wxSizerItem*
1232 wxSizer::Prepend( wxSizer *sizer, const wxSizerFlags& flags )
1233 {
1234 return Prepend( new wxSizerItem(sizer, flags) );
1235 }
1236
1237 inline wxSizerItem*
1238 wxSizer::Prepend( int width, int height, const wxSizerFlags& flags )
1239 {
1240 return Prepend( new wxSizerItem(width, height, flags) );
1241 }
1242
1243 inline wxSizerItem*
1244 wxSizer::Insert( size_t index,
1245 wxWindow *window,
1246 int proportion,
1247 int flag,
1248 int border,
1249 wxObject* userData )
1250 {
1251 return Insert( index, new wxSizerItem( window, proportion, flag, border, userData ) );
1252 }
1253
1254 inline wxSizerItem*
1255 wxSizer::Insert( size_t index,
1256 wxSizer *sizer,
1257 int proportion,
1258 int flag,
1259 int border,
1260 wxObject* userData )
1261 {
1262 return Insert( index, new wxSizerItem( sizer, proportion, flag, border, userData ) );
1263 }
1264
1265 inline wxSizerItem*
1266 wxSizer::Insert( size_t index,
1267 int width,
1268 int height,
1269 int proportion,
1270 int flag,
1271 int border,
1272 wxObject* userData )
1273 {
1274 return Insert( index, new wxSizerItem( width, height, proportion, flag, border, userData ) );
1275 }
1276
1277 inline wxSizerItem*
1278 wxSizer::Insert( size_t index, wxWindow *window, const wxSizerFlags& flags )
1279 {
1280 return Insert( index, new wxSizerItem(window, flags) );
1281 }
1282
1283 inline wxSizerItem*
1284 wxSizer::Insert( size_t index, wxSizer *sizer, const wxSizerFlags& flags )
1285 {
1286 return Insert( index, new wxSizerItem(sizer, flags) );
1287 }
1288
1289 inline wxSizerItem*
1290 wxSizer::Insert( size_t index, int width, int height, const wxSizerFlags& flags )
1291 {
1292 return Insert( index, new wxSizerItem(width, height, flags) );
1293 }
1294
1295 inline wxSizerItem*
1296 wxSizer::InsertSpacer(size_t index, int size)
1297 {
1298 return Insert(index, size, size);
1299 }
1300
1301 inline wxSizerItem*
1302 wxSizer::InsertStretchSpacer(size_t index, int prop)
1303 {
1304 return Insert(index, 0, 0, prop);
1305 }
1306
1307 #endif // __WXSIZER_H__