File: wx292.htm

package info (click to toggle)
wxwin2-doc 2.01-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,540 kB
  • ctags: 5,968
  • sloc: cpp: 15,157; makefile: 434; sh: 6
file content (169 lines) | stat: -rw-r--r-- 8,240 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<HTML>
<head><title>Constraints overview</title></head>

<BODY BGCOLOR=#FFFFFF>
<A NAME="constraintsoverview"></A><CENTER>
<A HREF="wx.htm"><img align=center src="contents.gif" BORDER=0 ALT="Contents"></A> <A HREF="wx278.htm#overviews"><img align=center src="up.gif" BORDER=0 ALT="Up"></A> <A HREF="wx291.htm#commondialogsoverview"><img align=center src="back.gif" BORDER=0 ALT="Previous"></A> <A HREF="wx293.htm#odbcoverview"><img align=center src="forward.gif" BORDER=0 ALT="Next"></A> </CENTER><HR>

<H2>Constraints overview</H2>
<P>
Classes: <A HREF="wx126.htm#wxlayoutconstraints">wxLayoutConstraints</A>, <A HREF="wx118.htm#wxindividuallayoutconstraint">wxIndividualLayoutConstraint</A>.<P>
Objects of class wxLayoutConstraint can be associated with a window to define the
way its subwindows are laid out, with respect to their siblings or parent.<P>
The class consists of the following eight constraints of class wxIndividualLayoutConstraint,
some or all of which should be accessed directly to set the appropriate
constraints.<P>
<UL>

<LI> <B>left:</B> represents the left hand edge of the window
<LI> <B>right:</B> represents the right hand edge of the window
<LI> <B>top:</B> represents the top edge of the window
<LI> <B>bottom:</B> represents the bottom edge of the window
<LI> <B>width:</B> represents the width of the window
<LI> <B>height:</B> represents the height of the window
<LI> <B>centreX:</B> represents the horizontal centre point of the window
<LI> <B>centreY:</B> represents the vertical centre point of the window
</UL>
<P>
Most constraints are initially set to have the relationship wxUnconstrained,
which means that their values should be calculated by looking at known constraints.
The exceptions are <I>width</I> and <I>height</I>, which are set to wxAsIs to
ensure that if the user does not specify a constraint, the existing
width and height will be used, to be compatible with panel items which often
have take a default size. If the constraint is wxAsIs, the dimension will
not be changed.<P>
To call the <A HREF="wx260.htm#wxwindowlayout">wxWindow::Layout</A> function which evaluates
constraints, you can either call wxWindow::SetAutoLayout to tell
default OnSize handlers to call Layout, or override OnSize and call Layout yourself.<P>
<A HREF="#topic1124">Constraint layout: more detail</A><BR>
<A HREF="#layoutexamples">Window layout examples</A><BR>
<P>

<HR>
<A NAME="topic1124"></A>
<H3>Constraint layout: more detail</H3>
<P>
By default, windows do not have a wxLayoutConstraints object. In this case, much layout
must be done explicitly, by performing calculations in OnSize members, except
for the case of frames that have one subwindow, where wxFrame::OnSize takes care
of resizing the child.<P>
To avoid the need for these rather awkward calculations, the user can create
a wxLayoutConstraints object and associate it with a window with wxWindow::SetConstraints.
This object contains a constraint for each of the window edges, two for the centre point,
and two for the window size. By setting some or all of these constraints appropriately,
the user can achieve quite complex layout by defining relationships between windows.<P>
In wxWindows, each window can be constrained relative to either its <I>
siblings</I> on the same window, or the <I>parent</I>. The layout algorithm
therefore operates in a top-down manner, finding the correct layout for
the children of a window, then the layout for the grandchildren, and so
on. Note that this differs markedly from native Motif layout, where
constraints can ripple upwards and can eventually change the frame
window or dialog box size. We assume in wxWindows that the <I>user</I> is
always 'boss' and specifies the size of the outer window, to which
subwindows must conform. Obviously, this might be a limitation in some
circumstances, but it suffices for most situations, and the
simplification avoids some of the nightmarish problems associated with
programming Motif.<P>
When the user sets constraints, many of the constraints for windows
edges and dimensions remain unconstrained. For a given window,
the wxWindow::Layout algorithm first resets all constraints
in all children to have unknown edge or dimension values, and then iterates through the constraints,
evaulating them. For unconstrained edges and dimensions, it
tries to find the value using known relationships that always hold. For example,
an unconstrained <I>width</I> may be calculated from the <I>left</I> and <I>right edges</I>, if
both are currently known. For edges and dimensions with user-supplied constraints, these
constraints are evaulated if the inputs of the constraint are known.<P>
The algorithm stops when all child edges and dimension are known (success), or there
there are unknown edges or dimensions but there has been no change in this cycle (failure).<P>
It then sets all the window positions and sizes according to the values it has found.<P>
Because the algorithm is iterative, the order in which constraints are considered is
irrelevant.<P>

<HR>
<A NAME="layoutexamples"></A>
<H3>Window layout examples</H3>
<P>

<HR>
<A NAME="topic1125"></A>
<H3>Example 1: subwindow layout</H3>
<P>
This example specifies a panel and a window side by side,
with a text subwindow below it. <P>
<PRE>
  frame-&gt;panel = new wxPanel(frame, -1, wxPoint(0, 0), wxSize(1000, 500), 0);
  frame-&gt;scrollWindow = new MyScrolledWindow(frame, -1, wxPoint(0, 0), wxSize(400, 400), wxRETAINED);
  frame-&gt;text_window = new MyTextWindow(frame, -1, wxPoint(0, 250), wxSize(400, 250));

  // Set constraints for panel subwindow
  wxLayoutConstraints *c1 = new wxLayoutConstraints;

  c1-&gt;left.SameAs       (frame, wxLeft);
  c1-&gt;top.SameAs        (frame, wxTop);
  c1-&gt;right.PercentOf   (frame, wxWidth, 50);
  c1-&gt;height.PercentOf  (frame, wxHeight, 50);

  frame-&gt;panel-&gt;SetConstraints(c1);

  // Set constraints for scrollWindow subwindow
  wxLayoutConstraints *c2 = new wxLayoutConstraints;

  c2-&gt;left.SameAs       (frame-&gt;panel, wxRight);
  c2-&gt;top.SameAs        (frame, wxTop);
  c2-&gt;right.SameAs      (frame, wxRight);
  c2-&gt;height.PercentOf  (frame, wxHeight, 50);

  frame-&gt;scrollWindow-&gt;SetConstraints(c2);

  // Set constraints for text subwindow
  wxLayoutConstraints *c3 = new wxLayoutConstraints;
  c3-&gt;left.SameAs       (frame, wxLeft);
  c3-&gt;top.Below         (frame-&gt;panel);
  c3-&gt;right.SameAs      (frame, wxRight);
  c3-&gt;bottom.SameAs     (frame, wxBottom);

  frame-&gt;text_window-&gt;SetConstraints(c3);
</PRE>

<HR>
<A NAME="topic1126"></A>
<H3>Example 2: panel item layout</H3>
<P>
This example sizes a button width to 80 percent of the panel width, and centres
it horizontally. A listbox and multitext item are placed below it. The listbox
takes up 40 percent of the panel width, and the multitext item takes up
the remainder of the width. Margins of 5 pixels are used.<P>
<PRE>
  // Create some panel items
  wxButton *btn1 = new wxButton(frame-&gt;panel, -1, "A button") ;

  wxLayoutConstraints *b1 = new wxLayoutConstraints;
  b1-&gt;centreX.SameAs    (frame-&gt;panel, wxCentreX);
  b1-&gt;top.SameAs        (frame-&gt;panel, wxTop, 5);
  b1-&gt;width.PercentOf   (frame-&gt;panel, wxWidth, 80);
  b1-&gt;height.PercentOf  (frame-&gt;panel, wxHeight, 10);
  btn1-&gt;SetConstraints(b1);

  wxListBox *list = new wxListBox(frame-&gt;panel, -1, "A list",
                                  wxPoint(-1, -1), wxSize(200, 100));

  wxLayoutConstraints *b2 = new wxLayoutConstraints;
  b2-&gt;top.Below         (btn1, 5);
  b2-&gt;left.SameAs       (frame-&gt;panel, wxLeft, 5);
  b2-&gt;width.PercentOf   (frame-&gt;panel, wxWidth, 40);
  b2-&gt;bottom.SameAs     (frame-&gt;panel, wxBottom, 5);
  list-&gt;SetConstraints(b2);

  wxTextCtrl *mtext = new wxTextCtrl(frame-&gt;panel, -1, "Multiline text", "Some text",
                        wxPoint(-1, -1), wxSize(150, 100), wxTE_MULTILINE);
  
  wxLayoutConstraints *b3 = new wxLayoutConstraints;
  b3-&gt;top.Below         (btn1, 5);
  b3-&gt;left.RightOf      (list, 5);
  b3-&gt;right.SameAs      (frame-&gt;panel, wxRight, 5);
  b3-&gt;bottom.SameAs     (frame-&gt;panel, wxBottom, 5);
  mtext-&gt;SetConstraints(b3);
</PRE>


</BODY></HTML>