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