]> git.saurik.com Git - wxWidgets.git/blame - include/wx/layout.h
Fixed a notebook crash and added more tests to sample.
[wxWidgets.git] / include / wx / layout.h
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: layout.h
3// Purpose: Layout classes
4// Author: Julian Smart
5// Modified by:
6// Created: 29/01/98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Julian Smart
a532afbb 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
34138703
JS
12#ifndef _WX_LAYOUTH__
13#define _WX_LAYOUTH__
c801d85f 14
f03fc89f
VZ
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
c801d85f 19#ifdef __GNUG__
f03fc89f 20 #pragma interface "layout.h"
c801d85f
KB
21#endif
22
23#include "wx/defs.h"
24
c801d85f
KB
25// X stupidly defines these in X.h
26#ifdef Above
f03fc89f 27 #undef Above
c801d85f
KB
28#endif
29#ifdef Below
f03fc89f 30 #undef Below
c801d85f
KB
31#endif
32
f03fc89f
VZ
33// ----------------------------------------------------------------------------
34// forward declrations
35// ----------------------------------------------------------------------------
36
37class WXDLLEXPORT wxWindowBase;
38class WXDLLEXPORT wxLayoutConstraints;
39
40// ----------------------------------------------------------------------------
41// constants
42// ----------------------------------------------------------------------------
43
c801d85f
KB
44#define wxLAYOUT_DEFAULT_MARGIN 0
45
f03fc89f
VZ
46enum wxEdge
47{
48 wxLeft, wxTop, wxRight, wxBottom, wxWidth, wxHeight,
49 wxCentre, wxCenter = wxCentre, wxCentreX, wxCentreY
50};
c801d85f 51
f03fc89f 52enum wxRelationship
c801d85f 53{
f03fc89f
VZ
54 wxUnconstrained = 0,
55 wxAsIs,
56 wxPercentOf,
57 wxAbove,
58 wxBelow,
59 wxLeftOf,
60 wxRightOf,
61 wxSameAs,
62 wxAbsolute
c801d85f
KB
63};
64
f03fc89f 65enum wxSizerBehaviour
c801d85f 66{
f03fc89f
VZ
67 wxSizerShrink,
68 wxSizerExpand,
69 wxSizerNone
c801d85f
KB
70};
71
f03fc89f
VZ
72#define wxTYPE_SIZER 90
73
74// =============================================================================
75// classes
76// =============================================================================
77
78// ----------------------------------------------------------------------------
79// wxIndividualLayoutConstraint: a constraint on window position
80// ----------------------------------------------------------------------------
81
82class WXDLLEXPORT wxIndividualLayoutConstraint : public wxObject
83{
84 DECLARE_DYNAMIC_CLASS(wxIndividualLayoutConstraint)
85
86protected:
87 // To be allowed to modify the internal variables
88 friend class wxIndividualLayoutConstraint_Serialize;
89
90 // 'This' window is the parent or sibling of otherWin
91 wxWindowBase *otherWin;
92
93 wxEdge myEdge;
94 wxRelationship relationship;
95 int margin;
96 int value;
97 int percent;
98 wxEdge otherEdge;
99 bool done;
100
101public:
102 wxIndividualLayoutConstraint();
103 ~wxIndividualLayoutConstraint();
104
105 void Set(wxRelationship rel, wxWindowBase *otherW, wxEdge otherE, int val = 0, int marg = wxLAYOUT_DEFAULT_MARGIN);
106
107 //
108 // Sibling relationships
109 //
110 void LeftOf(wxWindowBase *sibling, int marg = wxLAYOUT_DEFAULT_MARGIN);
111 void RightOf(wxWindowBase *sibling, int marg = wxLAYOUT_DEFAULT_MARGIN);
112 void Above(wxWindowBase *sibling, int marg = wxLAYOUT_DEFAULT_MARGIN);
113 void Below(wxWindowBase *sibling, int marg = wxLAYOUT_DEFAULT_MARGIN);
114
115 //
116 // 'Same edge' alignment
117 //
118 void SameAs(wxWindowBase *otherW, wxEdge edge, int marg = wxLAYOUT_DEFAULT_MARGIN);
119
120 // The edge is a percentage of the other window's edge
121 void PercentOf(wxWindowBase *otherW, wxEdge wh, int per);
122
123 //
124 // Edge has absolute value
125 //
126 void Absolute(int val);
127
128 //
129 // Dimension is unconstrained
130 //
131 void Unconstrained() { relationship = wxUnconstrained; }
132
133 //
134 // Dimension is 'as is' (use current size settings)
135 //
136 void AsIs() { relationship = wxAsIs; }
137
138 //
139 // Accessors
140 //
141 wxWindowBase *GetOtherWindow() { return otherWin; }
142 wxEdge GetMyEdge() const { return myEdge; }
143 void SetEdge(wxEdge which) { myEdge = which; }
144 void SetValue(int v) { value = v; }
145 int GetMargin() { return margin; }
146 void SetMargin(int m) { margin = m; }
147 int GetValue() const { return value; }
148 int GetPercent() const { return percent; }
149 int GetOtherEdge() const { return otherEdge; }
150 bool GetDone() const { return done; }
151 void SetDone(bool d) { done = d; }
152 wxRelationship GetRelationship() { return relationship; }
153 void SetRelationship(wxRelationship r) { relationship = r; }
154
155 // Reset constraint if it mentions otherWin
156 bool ResetIfWin(wxWindowBase *otherW);
157
158 // Try to satisfy constraint
159 bool SatisfyConstraint(wxLayoutConstraints *constraints, wxWindowBase *win);
160
161 // Get the value of this edge or dimension, or if this
162 // is not determinable, -1.
163 int GetEdge(wxEdge which, wxWindowBase *thisWin, wxWindowBase *other) const;
164};
165
166// ----------------------------------------------------------------------------
167// wxLayoutConstraints: the complete set of constraints for a window
168// ----------------------------------------------------------------------------
169
170class WXDLLEXPORT wxLayoutConstraints : public wxObject
171{
172 DECLARE_DYNAMIC_CLASS(wxLayoutConstraints)
173
174public:
175 // Edge constraints
176 wxIndividualLayoutConstraint left;
177 wxIndividualLayoutConstraint top;
178 wxIndividualLayoutConstraint right;
179 wxIndividualLayoutConstraint bottom;
180 // Size constraints
181 wxIndividualLayoutConstraint width;
182 wxIndividualLayoutConstraint height;
183 // Centre constraints
184 wxIndividualLayoutConstraint centreX;
185 wxIndividualLayoutConstraint centreY;
186
187 wxLayoutConstraints();
188 ~wxLayoutConstraints();
189
190 bool SatisfyConstraints(wxWindowBase *win, int *noChanges);
191 bool AreSatisfied() const
192 {
193 return left.GetDone() && top.GetDone() && right.GetDone() &&
194 bottom.GetDone() && centreX.GetDone() && centreY.GetDone();
195 }
196};
197
198// ----------------------------------------------------------------------------
199// sizers
200// ----------------------------------------------------------------------------
c801d85f
KB
201
202/*
203
204Algorithm:
205
206 Each sizer has a Layout function.
207
208 wxExpandSizer::Layout ; E.g. for resizeable windows
a532afbb 209
c801d85f
KB
210 - parent size must be known (i.e. called
211 from OnSize or explicitly)
212 - call Layout on each child to give it a chance to resize
213 (e.g. child shrinks around its own children):
214 stop when all children return TRUE, or no change
215 - evaluate constraints on self to set size
216
217 wxShrinkSizer::Layout ; E.g. fit-to-contents windows
218 ; Perhaps 2 rowcols, one above other.
a532afbb 219
c801d85f
KB
220 - call Layout on each child to give it a chance to resize
221 (e.g. child shrinks around its own children):
222 stop when each returns TRUE, or no change
223 - fit around children
224 (what if some want to be centred? E.g. OK/Cancel rowcol.
225 - done by centring e.g. bottom sizer w.r.t. top sizer.
226 (sibling relationship only))
227 - evaluate own constraints (e.g. may be below another window)
228 - IF parent is a real window (remember: a real window can
229 have only one child sizer, although a sizer can have several child
230 (real) windows), then resize this parent WITHOUT invoking Layout
231 again.
232 Frame and dialog box OnSizes can check if the sizer is a shrink
233 sizer; if not, can call layout. Maybe have virtual bool AutoSizeLayout()
234 to determine this.
235
236How to relayout if a child sizer/window changes? Need to go all the way
237to the top of the hierarchy and call Layout() again.
a532afbb 238
c801d85f
KB
239 wxRowColSizer::Layout
240
241 - Similar to wxShrinkSizer only instead of shrinking to fit
242 contents, more sophisticated layout of contents, and THEN
243 shrinking (possibly).
244 - Do the same parent window check/setsize as for wxShrinkSizer.
a532afbb 245
c801d85f
KB
246*/
247
a532afbb 248class WXDLLEXPORT wxSizer : public wxWindow
c801d85f 249{
f03fc89f 250 DECLARE_DYNAMIC_CLASS(wxSizer)
c801d85f 251
a532afbb
VZ
252protected:
253 wxSizerBehaviour sizerBehaviour;
254 int borderX;
255 int borderY;
256 int sizerWidth;
257 int sizerHeight;
258 int sizerX;
259 int sizerY;
260
261public:
262 wxSizer();
f03fc89f 263 wxSizer(wxWindowBase *parent, wxSizerBehaviour behav = wxSizerNone);
a532afbb 264 ~wxSizer();
c801d85f 265
f03fc89f 266 bool Create(wxWindowBase *parent, wxSizerBehaviour behav = wxSizerNone);
acbd13a3 267
32ac755d
RR
268 virtual void DoGetSize(int *w, int *h) const;
269 virtual void DoGetClientSize(int *w, int *h) const { GetSize(w, h); }
270 virtual void DoGetPosition(int *x, int *y) const;
c801d85f 271
a532afbb
VZ
272 void SizerSetSize(int x, int y, int w, int h) { SetSize(x, y, w, h); }
273 void SizerMove(int x, int y) { Move(x, y); }
c801d85f 274
a532afbb
VZ
275 virtual void SetBorder(int w, int h);
276 int GetBorderX() { return borderX ; }
277 int GetBorderY() { return borderY ; }
c801d85f 278
f03fc89f
VZ
279 virtual void AddSizerChild(wxWindowBase *child);
280 virtual void RemoveSizerChild(wxWindowBase *child);
c801d85f 281
a532afbb
VZ
282 virtual void SetBehaviour(wxSizerBehaviour b) { sizerBehaviour = b; }
283 virtual wxSizerBehaviour GetBehaviour() { return sizerBehaviour; }
c801d85f 284
a532afbb
VZ
285 virtual bool LayoutPhase1(int *);
286 virtual bool LayoutPhase2(int *);
bfc6fde4
VZ
287
288protected:
289 virtual void DoSetSize(int x, int y,
290 int width, int height,
291 int sizeFlags = wxSIZE_AUTO);
c801d85f
KB
292};
293
294#define wxSIZER_ROWS TRUE
295#define wxSIZER_COLS FALSE
296
a532afbb 297class WXDLLEXPORT wxRowColSizer : public wxSizer
c801d85f 298{
f03fc89f 299 DECLARE_DYNAMIC_CLASS(wxRowColSizer)
a532afbb
VZ
300
301protected:
302 bool rowOrCol;
303 int rowOrColSize;
304 int xSpacing;
305 int ySpacing;
306
307public:
308 // rowOrCol = TRUE to be laid out in rows, otherwise in columns.
309 wxRowColSizer();
f03fc89f 310 wxRowColSizer(wxWindowBase *parent, bool rowOrCol = wxSIZER_ROWS,
a532afbb
VZ
311 int rowsOrColSize = 20, wxSizerBehaviour = wxSizerShrink);
312 ~wxRowColSizer();
313
f03fc89f 314 bool Create(wxWindowBase *parent, bool rowOrCol = wxSIZER_ROWS,
a532afbb 315 int rowsOrColSize = 20, wxSizerBehaviour = wxSizerShrink);
a532afbb
VZ
316
317 virtual void SetRowOrCol(bool rc) { rowOrCol = rc; }
318 virtual bool GetRowOrCol() { return rowOrCol; }
319 virtual void SetRowOrColSize(int n) { rowOrColSize = n; }
320 virtual int GetRowOrColSize() { return rowOrColSize; }
321 virtual void SetSpacing(int x, int y) { xSpacing = x; ySpacing = y; }
322 virtual void GetSpacing(int *x, int *y) { *x = xSpacing; *y = ySpacing; }
323
324 bool LayoutPhase1(int *);
325 bool LayoutPhase2(int *);
c801d85f
KB
326};
327
a532afbb 328class WXDLLEXPORT wxSpacingSizer : public wxSizer
c801d85f 329{
f03fc89f 330 DECLARE_DYNAMIC_CLASS(wxSpacingSizer)
c801d85f 331
a532afbb
VZ
332public:
333 wxSpacingSizer();
f03fc89f
VZ
334 wxSpacingSizer(wxWindowBase *parent, wxRelationship rel, wxWindowBase *other, int spacing);
335 wxSpacingSizer(wxWindowBase *parent);
a532afbb 336 ~wxSpacingSizer();
c801d85f 337
f03fc89f
VZ
338 bool Create(wxWindowBase *parent, wxRelationship rel, wxWindowBase *other, int sp);
339 bool Create(wxWindowBase *parent);
c801d85f
KB
340};
341
f03fc89f
VZ
342// ----------------------------------------------------------------------------
343// global functions
344// ----------------------------------------------------------------------------
345
346#if WXWIN_COMPATIBILITY
347 extern bool WXDLLEXPORT wxOldDoLayout(wxWindowBase *win);
348#endif // WXWIN_COMPATIBILITY
349
c801d85f 350#endif
34138703 351 // _WX_LAYOUTH__