]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/LayoutAnchors.py
Patch from Victor V. Kryukov
[wxWidgets.git] / wxPython / demo / LayoutAnchors.py
1
2 import wx
3 import wx.lib.anchors as anchors
4
5 #----------------------------------------------------------------------
6
7 # Nifty little trick here; apply wx.NewId() to generate a series of
8 # IDs used later on in the app.
9
10 [ ID_ANCHORSDEMOFRAMEANCHOREDPANEL,
11 ID_ANCHORSDEMOFRAMEHELPSTATICTEXT,
12 ID_ANCHORSDEMOFRAMEMAINPANEL,
13 ID_ANCHORSDEMOFRAMEBACKGROUNDPANEL,
14 ID_ANCHORSDEMOFRAMERIGHTCHECKBOX,
15 ID_ANCHORSDEMOFRAMEOKBUTTON,
16 ID_ANCHORSDEMOFRAMETOPCHECKBOX,
17 ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX,
18 ID_ANCHORSDEMOFRAME,
19 ID_ANCHORSDEMOFRAMELEFTCHECKBOX,
20 ] = map(lambda _init_ctrls: wx.NewId(), range(10))
21
22 # A small note here: while only certain parts of this frame are actually demonstrating
23 # the capabilities of the LayoutAnchors feature, all the controls are within the same
24 # frame; therefore, all controls and windows within the frame must use LayoutAnchors.
25 # You can't mix LayoutAnchors and sizers.
26 class AnchorsDemoFrame(wx.Frame):
27 def _init_utils(self):
28 pass
29
30 def _init_ctrls(self, prnt):
31 wx.Frame.__init__(
32 self, size=(328, 187), id=ID_ANCHORSDEMOFRAME,
33 title='LayoutAnchors Demonstration', parent=prnt,
34 name='AnchorsDemoFrame',
35 style = wx.DEFAULT_FRAME_STYLE | wx.CLIP_CHILDREN, pos=(261, 123)
36 )
37
38 self._init_utils()
39
40 self.mainPanel = wx.Panel(
41 size=(320, 160), parent=self,
42 id=ID_ANCHORSDEMOFRAMEMAINPANEL, name='panel1',
43 style=wx.TAB_TRAVERSAL | wx.CLIP_CHILDREN,
44 pos=(0, 0)
45 )
46
47 self.mainPanel.SetAutoLayout(True)
48
49 self.okButton = wx.Button(
50 label='OK', id=ID_ANCHORSDEMOFRAMEOKBUTTON,
51 parent=self.mainPanel, name='okButton',
52 size=(72, 24), style=0, pos=(240, 128)
53 )
54
55 self.okButton.SetConstraints(
56 anchors.LayoutAnchors(self.okButton, False, False, True, True)
57 )
58
59 self.Bind(
60 wx.EVT_BUTTON, self.OnOkButtonButton, id=ID_ANCHORSDEMOFRAMEOKBUTTON
61 )
62
63 self.backgroundPanel = wx.Panel(
64 size=(304, 80), parent=self.mainPanel,
65 id=ID_ANCHORSDEMOFRAMEBACKGROUNDPANEL,
66 name='backgroundPanel',
67 style=wx.SIMPLE_BORDER | wx.CLIP_CHILDREN,
68 pos = (8, 40)
69 )
70
71 self.backgroundPanel.SetBackgroundColour(wx.Colour(255, 255, 255))
72 self.backgroundPanel.SetConstraints(
73 anchors.LayoutAnchors(self.backgroundPanel, True, True, True, True)
74 )
75
76 self.anchoredPanel = wx.Panel(
77 size=(88, 48), id=ID_ANCHORSDEMOFRAMEANCHOREDPANEL,
78 parent=self.backgroundPanel, name='anchoredPanel',
79 style=wx.SIMPLE_BORDER, pos=(104, 16)
80 )
81
82 self.anchoredPanel.SetBackgroundColour(wx.Colour(0, 0, 222))
83 self.anchoredPanel.SetConstraints(
84 anchors.LayoutAnchors(self.anchoredPanel, False, False, False, False)
85 )
86
87 self.leftCheckBox = wx.CheckBox(
88 label='Left', id=ID_ANCHORSDEMOFRAMELEFTCHECKBOX,
89 parent=self.mainPanel, name='leftCheckBox',
90 size=(40, 16), style=0, pos=(8, 8)
91 )
92
93 self.leftCheckBox.SetConstraints(
94 anchors.LayoutAnchors(self.leftCheckBox, False, True, False, False)
95 )
96
97 self.Bind(
98 wx.EVT_CHECKBOX, self.OnCheckboxCheckbox, source=self.leftCheckBox,
99 id=ID_ANCHORSDEMOFRAMELEFTCHECKBOX
100 )
101
102 self.topCheckBox = wx.CheckBox(
103 label='Top', id=ID_ANCHORSDEMOFRAMETOPCHECKBOX,
104 parent=self.mainPanel, name='topCheckBox',
105 size=(40, 16), style=0, pos=(88, 8)
106 )
107
108 self.topCheckBox.SetConstraints(
109 anchors.LayoutAnchors(self.topCheckBox, False, True, False, False)
110 )
111
112 self.Bind(
113 wx.EVT_CHECKBOX, self.OnCheckboxCheckbox, source=self.topCheckBox,
114 id=ID_ANCHORSDEMOFRAMETOPCHECKBOX
115 )
116
117 self.rightCheckBox = wx.CheckBox(
118 label='Right', id=ID_ANCHORSDEMOFRAMERIGHTCHECKBOX,
119 parent=self.mainPanel, name='rightCheckBox',
120 size=(48, 16), style=0, pos=(168, 8)
121 )
122
123 self.rightCheckBox.SetConstraints(
124 anchors.LayoutAnchors(self.rightCheckBox, False, True, False, False)
125 )
126
127 self.Bind(
128 wx.EVT_CHECKBOX, self.OnCheckboxCheckbox, source=self.rightCheckBox,
129 id=ID_ANCHORSDEMOFRAMERIGHTCHECKBOX
130 )
131
132 self.bottomCheckBox = wx.CheckBox(
133 label='Bottom', id=ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX,
134 parent=self.mainPanel, name='bottomCheckBox',
135 size=(56, 16), style=0, pos=(248, 8)
136 )
137
138 self.bottomCheckBox.SetConstraints(
139 anchors.LayoutAnchors(self.bottomCheckBox, False, True, False, False)
140 )
141
142 self.Bind(
143 wx.EVT_CHECKBOX, self.OnCheckboxCheckbox, source=self.bottomCheckBox,
144 id=ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX
145 )
146
147 self.helpStaticText = wx.StaticText(
148 label='Select anchor options above, then resize window to see the effect',
149 id=ID_ANCHORSDEMOFRAMEHELPSTATICTEXT,
150 parent=self.mainPanel, name='helpStaticText',
151 size=(224, 24), style=wx.ST_NO_AUTORESIZE,
152 pos=(8, 128)
153 )
154
155 self.helpStaticText.SetConstraints(
156 anchors.LayoutAnchors(self.helpStaticText, True, False, True, True)
157 )
158
159 def __init__(self, parent):
160 self._init_ctrls(parent)
161
162 # Based on the values of the above checkboxes, we will adjust the layout constraints
163 # on the sample window whenever one of the checkboxes changes state.
164 def OnCheckboxCheckbox(self, event):
165 self.anchoredPanel.SetConstraints(
166 anchors.LayoutAnchors(self.anchoredPanel,
167 self.leftCheckBox.GetValue(), self.topCheckBox.GetValue(),
168 self.rightCheckBox.GetValue(), self.bottomCheckBox.GetValue()
169 )
170 )
171
172 def OnOkButtonButton(self, event):
173 self.Close()
174
175 #---------------------------------------------------------------------------
176
177 class TestPanel(wx.Panel):
178 def __init__(self, parent, log):
179 self.log = log
180 wx.Panel.__init__(self, parent, -1)
181
182 b = wx.Button(self, -1, "Show the LayoutAnchors sample", (50,50))
183 self.Bind(wx.EVT_BUTTON, self.OnButton, b)
184
185
186 def OnButton(self, evt):
187 win = AnchorsDemoFrame(self)
188 win.Show(True)
189
190
191 #---------------------------------------------------------------------------
192
193
194 def runTest(frame, nb, log):
195 win = TestPanel(nb, log)
196 return win
197
198 #----------------------------------------------------------------------
199
200
201 overview = """<html><body>
202 <h2>LayoutAnchors</h2>
203 A class that implements Delphi's Anchors with wxLayoutConstraints.
204 <p>
205 Anchored sides maintain the distance from the edge of the
206 control to the same edge of the parent.
207 When neither side is selected, the control keeps the same
208 relative position to both sides.
209 <p>
210 The current position and size of the control and it's parent
211 is used when setting up the constraints. To change the size or
212 position of an already anchored control, set the constraints to
213 None, reposition or resize and reapply the anchors.
214 <p>
215 Examples:
216 <p>
217 Let's anchor the right and bottom edge of a control and
218 resize it's parent.
219 <p>
220 <pre>
221 ctrl.SetConstraints(LayoutAnchors(ctrl, left=0, top=0, right=1, bottom=1))
222
223 +=========+ +===================+
224 | +-----+ | | |
225 | | * | -> | |
226 | +--*--+ | | +-----+ |
227 +---------+ | | * |
228 | +--*--+ |
229 +-------------------+
230 * = anchored edge
231 </pre>
232 <p>
233 When anchored on both sides the control will stretch horizontally.
234 <p>
235 <pre>
236 ctrl.SetConstraints(LayoutAnchors(ctrl, 1, 0, 1, 1))
237
238 +=========+ +===================+
239 | +-----+ | | |
240 | * * | -> | |
241 | +--*--+ | | +---------------+ |
242 +---------+ | * ctrl * |
243 | +-------*-------+ |
244 +-------------------+
245 * = anchored edge
246 </pre>
247 </html></body>
248 """
249
250
251
252 if __name__ == '__main__':
253 import sys,os
254 import run
255 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
256