| 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 | | wx.FULL_REPAINT_ON_RESIZE, |
| 45 | pos=(0, 0) |
| 46 | ) |
| 47 | |
| 48 | self.mainPanel.SetAutoLayout(True) |
| 49 | |
| 50 | self.okButton = wx.Button( |
| 51 | label='OK', id=ID_ANCHORSDEMOFRAMEOKBUTTON, |
| 52 | parent=self.mainPanel, name='okButton', |
| 53 | size=(72, 24), style=0, pos=(240, 128) |
| 54 | ) |
| 55 | |
| 56 | self.okButton.SetConstraints( |
| 57 | anchors.LayoutAnchors(self.okButton, False, False, True, True) |
| 58 | ) |
| 59 | |
| 60 | self.Bind( |
| 61 | wx.EVT_BUTTON, self.OnOkButtonButton, id=ID_ANCHORSDEMOFRAMEOKBUTTON |
| 62 | ) |
| 63 | |
| 64 | self.backgroundPanel = wx.Panel( |
| 65 | size=(304, 80), parent=self.mainPanel, |
| 66 | id=ID_ANCHORSDEMOFRAMEBACKGROUNDPANEL, |
| 67 | name='backgroundPanel', |
| 68 | style=wx.SIMPLE_BORDER | wx.CLIP_CHILDREN, |
| 69 | pos = (8, 40) |
| 70 | ) |
| 71 | |
| 72 | self.backgroundPanel.SetBackgroundColour(wx.Colour(255, 255, 255)) |
| 73 | self.backgroundPanel.SetConstraints( |
| 74 | anchors.LayoutAnchors(self.backgroundPanel, True, True, True, True) |
| 75 | ) |
| 76 | |
| 77 | self.anchoredPanel = wx.Panel( |
| 78 | size=(88, 48), id=ID_ANCHORSDEMOFRAMEANCHOREDPANEL, |
| 79 | parent=self.backgroundPanel, name='anchoredPanel', |
| 80 | style=wx.SIMPLE_BORDER, pos=(104, 16) |
| 81 | ) |
| 82 | |
| 83 | self.anchoredPanel.SetBackgroundColour(wx.Colour(0, 0, 222)) |
| 84 | self.anchoredPanel.SetConstraints( |
| 85 | anchors.LayoutAnchors(self.anchoredPanel, False, False, False, False) |
| 86 | ) |
| 87 | |
| 88 | self.leftCheckBox = wx.CheckBox( |
| 89 | label='Left', id=ID_ANCHORSDEMOFRAMELEFTCHECKBOX, |
| 90 | parent=self.mainPanel, name='leftCheckBox', |
| 91 | style=0, pos=(8, 8) |
| 92 | ) |
| 93 | |
| 94 | self.leftCheckBox.SetConstraints( |
| 95 | anchors.LayoutAnchors(self.leftCheckBox, False, True, False, False) |
| 96 | ) |
| 97 | |
| 98 | self.Bind( |
| 99 | wx.EVT_CHECKBOX, self.OnCheckboxCheckbox, source=self.leftCheckBox, |
| 100 | id=ID_ANCHORSDEMOFRAMELEFTCHECKBOX |
| 101 | ) |
| 102 | |
| 103 | self.topCheckBox = wx.CheckBox( |
| 104 | label='Top', id=ID_ANCHORSDEMOFRAMETOPCHECKBOX, |
| 105 | parent=self.mainPanel, name='topCheckBox', |
| 106 | style=0, pos=(88, 8) |
| 107 | ) |
| 108 | |
| 109 | self.topCheckBox.SetConstraints( |
| 110 | anchors.LayoutAnchors(self.topCheckBox, False, True, False, False) |
| 111 | ) |
| 112 | |
| 113 | self.Bind( |
| 114 | wx.EVT_CHECKBOX, self.OnCheckboxCheckbox, source=self.topCheckBox, |
| 115 | id=ID_ANCHORSDEMOFRAMETOPCHECKBOX |
| 116 | ) |
| 117 | |
| 118 | self.rightCheckBox = wx.CheckBox( |
| 119 | label='Right', id=ID_ANCHORSDEMOFRAMERIGHTCHECKBOX, |
| 120 | parent=self.mainPanel, name='rightCheckBox', |
| 121 | style=0, pos=(168, 8) |
| 122 | ) |
| 123 | |
| 124 | self.rightCheckBox.SetConstraints( |
| 125 | anchors.LayoutAnchors(self.rightCheckBox, False, True, False, False) |
| 126 | ) |
| 127 | |
| 128 | self.Bind( |
| 129 | wx.EVT_CHECKBOX, self.OnCheckboxCheckbox, source=self.rightCheckBox, |
| 130 | id=ID_ANCHORSDEMOFRAMERIGHTCHECKBOX |
| 131 | ) |
| 132 | |
| 133 | self.bottomCheckBox = wx.CheckBox( |
| 134 | label='Bottom', id=ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX, |
| 135 | parent=self.mainPanel, name='bottomCheckBox', |
| 136 | style=0, pos=(248, 8) |
| 137 | ) |
| 138 | |
| 139 | self.bottomCheckBox.SetConstraints( |
| 140 | anchors.LayoutAnchors(self.bottomCheckBox, False, True, False, False) |
| 141 | ) |
| 142 | |
| 143 | self.Bind( |
| 144 | wx.EVT_CHECKBOX, self.OnCheckboxCheckbox, source=self.bottomCheckBox, |
| 145 | id=ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX |
| 146 | ) |
| 147 | |
| 148 | self.helpStaticText = wx.StaticText( |
| 149 | label='Select anchor options above, then resize window to see the effect', |
| 150 | id=ID_ANCHORSDEMOFRAMEHELPSTATICTEXT, |
| 151 | parent=self.mainPanel, name='helpStaticText', |
| 152 | size=(224, 24), style=wx.ST_NO_AUTORESIZE, |
| 153 | pos=(8, 128) |
| 154 | ) |
| 155 | |
| 156 | self.helpStaticText.SetConstraints( |
| 157 | anchors.LayoutAnchors(self.helpStaticText, True, False, True, True) |
| 158 | ) |
| 159 | |
| 160 | def __init__(self, parent): |
| 161 | self._init_ctrls(parent) |
| 162 | |
| 163 | # Based on the values of the above checkboxes, we will adjust the layout constraints |
| 164 | # on the sample window whenever one of the checkboxes changes state. |
| 165 | def OnCheckboxCheckbox(self, event): |
| 166 | self.anchoredPanel.SetConstraints( |
| 167 | anchors.LayoutAnchors(self.anchoredPanel, |
| 168 | self.leftCheckBox.GetValue(), self.topCheckBox.GetValue(), |
| 169 | self.rightCheckBox.GetValue(), self.bottomCheckBox.GetValue() |
| 170 | ) |
| 171 | ) |
| 172 | |
| 173 | def OnOkButtonButton(self, event): |
| 174 | self.Close() |
| 175 | |
| 176 | #--------------------------------------------------------------------------- |
| 177 | |
| 178 | class TestPanel(wx.Panel): |
| 179 | def __init__(self, parent, log): |
| 180 | self.log = log |
| 181 | wx.Panel.__init__(self, parent, -1) |
| 182 | |
| 183 | b = wx.Button(self, -1, "Show the LayoutAnchors sample", (50,50)) |
| 184 | self.Bind(wx.EVT_BUTTON, self.OnButton, b) |
| 185 | |
| 186 | |
| 187 | def OnButton(self, evt): |
| 188 | win = AnchorsDemoFrame(self) |
| 189 | win.Show(True) |
| 190 | |
| 191 | |
| 192 | #--------------------------------------------------------------------------- |
| 193 | |
| 194 | |
| 195 | def runTest(frame, nb, log): |
| 196 | win = TestPanel(nb, log) |
| 197 | return win |
| 198 | |
| 199 | #---------------------------------------------------------------------- |
| 200 | |
| 201 | |
| 202 | overview = """<html><body> |
| 203 | <h2>LayoutAnchors</h2> |
| 204 | A class that implements Delphi's Anchors with wxLayoutConstraints. |
| 205 | <p> |
| 206 | Anchored sides maintain the distance from the edge of the |
| 207 | control to the same edge of the parent. |
| 208 | When neither side is selected, the control keeps the same |
| 209 | relative position to both sides. |
| 210 | <p> |
| 211 | The current position and size of the control and it's parent |
| 212 | is used when setting up the constraints. To change the size or |
| 213 | position of an already anchored control, set the constraints to |
| 214 | None, reposition or resize and reapply the anchors. |
| 215 | <p> |
| 216 | Examples: |
| 217 | <p> |
| 218 | Let's anchor the right and bottom edge of a control and |
| 219 | resize it's parent. |
| 220 | <p> |
| 221 | <pre> |
| 222 | ctrl.SetConstraints(LayoutAnchors(ctrl, left=0, top=0, right=1, bottom=1)) |
| 223 | |
| 224 | +=========+ +===================+ |
| 225 | | +-----+ | | | |
| 226 | | | * | -> | | |
| 227 | | +--*--+ | | +-----+ | |
| 228 | +---------+ | | * | |
| 229 | | +--*--+ | |
| 230 | +-------------------+ |
| 231 | * = anchored edge |
| 232 | </pre> |
| 233 | <p> |
| 234 | When anchored on both sides the control will stretch horizontally. |
| 235 | <p> |
| 236 | <pre> |
| 237 | ctrl.SetConstraints(LayoutAnchors(ctrl, 1, 0, 1, 1)) |
| 238 | |
| 239 | +=========+ +===================+ |
| 240 | | +-----+ | | | |
| 241 | | * * | -> | | |
| 242 | | +--*--+ | | +---------------+ | |
| 243 | +---------+ | * ctrl * | |
| 244 | | +-------*-------+ | |
| 245 | +-------------------+ |
| 246 | * = anchored edge |
| 247 | </pre> |
| 248 | </html></body> |
| 249 | """ |
| 250 | |
| 251 | |
| 252 | |
| 253 | if __name__ == '__main__': |
| 254 | import sys,os |
| 255 | import run |
| 256 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
| 257 | |