]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/LayoutAnchors.py
Replaced wx/lib/pubsub.py with a version that uses weak references to
[wxWidgets.git] / wxPython / demo / LayoutAnchors.py
CommitLineData
1b62f00d 1
8fa876ca
RD
2import wx
3import wx.lib.anchors as anchors
1b62f00d
RD
4
5#----------------------------------------------------------------------
6
8fa876ca
RD
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.
26class AnchorsDemoFrame(wx.Frame):
1b62f00d
RD
27 def _init_utils(self):
28 pass
29
30 def _init_ctrls(self, prnt):
8fa876ca
RD
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 )
1b62f00d 37
8fa876ca 38 self._init_utils()
1b62f00d 39
8fa876ca
RD
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 )
1b62f00d 46
8fa876ca 47 self.mainPanel.SetAutoLayout(True)
1b62f00d 48
8fa876ca
RD
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 )
1b62f00d
RD
158
159 def __init__(self, parent):
160 self._init_ctrls(parent)
161
8fa876ca
RD
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.
1b62f00d
RD
164 def OnCheckboxCheckbox(self, event):
165 self.anchoredPanel.SetConstraints(
8fa876ca 166 anchors.LayoutAnchors(self.anchoredPanel,
1b62f00d 167 self.leftCheckBox.GetValue(), self.topCheckBox.GetValue(),
8fa876ca
RD
168 self.rightCheckBox.GetValue(), self.bottomCheckBox.GetValue()
169 )
170 )
1b62f00d
RD
171
172 def OnOkButtonButton(self, event):
173 self.Close()
174
175#----------------------------------------------------------------------
176
177def runTest(frame, nb, log):
178 win = AnchorsDemoFrame(frame)
179 frame.otherWin = win
1e4a197e 180 win.Show(True)
1b62f00d
RD
181
182
183
184
185#----------------------------------------------------------------------
186
187
1b62f00d
RD
188overview = """<html><body>
189<h2>LayoutAnchors</h2>
190 A class that implements Delphi's Anchors with wxLayoutConstraints.
191<p>
192 Anchored sides maintain the distance from the edge of the
193 control to the same edge of the parent.
194 When neither side is selected, the control keeps the same
195 relative position to both sides.
196<p>
197 The current position and size of the control and it's parent
198 is used when setting up the constraints. To change the size or
199 position of an already anchored control, set the constraints to
200 None, reposition or resize and reapply the anchors.
201<p>
202 Examples:
203<p>
204 Let's anchor the right and bottom edge of a control and
205 resize it's parent.
206<p>
207<pre>
208 ctrl.SetConstraints(LayoutAnchors(ctrl, left=0, top=0, right=1, bottom=1))
209
210 +=========+ +===================+
211 | +-----+ | | |
212 | | * | -> | |
213 | +--*--+ | | +-----+ |
214 +---------+ | | * |
215 | +--*--+ |
216 +-------------------+
217 * = anchored edge
218</pre>
219<p>
220 When anchored on both sides the control will stretch horizontally.
221<p>
222<pre>
223 ctrl.SetConstraints(LayoutAnchors(ctrl, 1, 0, 1, 1))
224
225 +=========+ +===================+
226 | +-----+ | | |
227 | * * | -> | |
228 | +--*--+ | | +---------------+ |
229 +---------+ | * ctrl * |
230 | +-------*-------+ |
231 +-------------------+
232 * = anchored edge
233</pre>
234</html></body>
235"""
236
237
238
1fded56b
RD
239if __name__ == '__main__':
240 import sys,os
241 import run
8eca4fef 242 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
1b62f00d 243