2001-02-16 03:19:50 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
import wx
|
|
|
|
import wx.lib.anchors as anchors
|
2001-02-16 03:19:50 -05:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Nifty little trick here; apply wx.NewId() to generate a series of
|
|
|
|
# IDs used later on in the app.
|
|
|
|
|
|
|
|
[ ID_ANCHORSDEMOFRAMEANCHOREDPANEL,
|
|
|
|
ID_ANCHORSDEMOFRAMEHELPSTATICTEXT,
|
|
|
|
ID_ANCHORSDEMOFRAMEMAINPANEL,
|
|
|
|
ID_ANCHORSDEMOFRAMEBACKGROUNDPANEL,
|
|
|
|
ID_ANCHORSDEMOFRAMERIGHTCHECKBOX,
|
|
|
|
ID_ANCHORSDEMOFRAMEOKBUTTON,
|
|
|
|
ID_ANCHORSDEMOFRAMETOPCHECKBOX,
|
|
|
|
ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX,
|
|
|
|
ID_ANCHORSDEMOFRAME,
|
|
|
|
ID_ANCHORSDEMOFRAMELEFTCHECKBOX,
|
|
|
|
] = map(lambda _init_ctrls: wx.NewId(), range(10))
|
|
|
|
|
|
|
|
# A small note here: while only certain parts of this frame are actually demonstrating
|
|
|
|
# the capabilities of the LayoutAnchors feature, all the controls are within the same
|
|
|
|
# frame; therefore, all controls and windows within the frame must use LayoutAnchors.
|
|
|
|
# You can't mix LayoutAnchors and sizers.
|
|
|
|
class AnchorsDemoFrame(wx.Frame):
|
2001-02-16 03:19:50 -05:00
|
|
|
def _init_utils(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _init_ctrls(self, prnt):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Frame.__init__(
|
|
|
|
self, size=(328, 187), id=ID_ANCHORSDEMOFRAME,
|
|
|
|
title='LayoutAnchors Demonstration', parent=prnt,
|
|
|
|
name='AnchorsDemoFrame',
|
|
|
|
style = wx.DEFAULT_FRAME_STYLE | wx.CLIP_CHILDREN, pos=(261, 123)
|
|
|
|
)
|
2001-02-16 03:19:50 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self._init_utils()
|
2001-02-16 03:19:50 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.mainPanel = wx.Panel(
|
|
|
|
size=(320, 160), parent=self,
|
|
|
|
id=ID_ANCHORSDEMOFRAMEMAINPANEL, name='panel1',
|
|
|
|
style=wx.TAB_TRAVERSAL | wx.CLIP_CHILDREN,
|
|
|
|
pos=(0, 0)
|
|
|
|
)
|
2001-02-16 03:19:50 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.mainPanel.SetAutoLayout(True)
|
2001-02-16 03:19:50 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.okButton = wx.Button(
|
|
|
|
label='OK', id=ID_ANCHORSDEMOFRAMEOKBUTTON,
|
|
|
|
parent=self.mainPanel, name='okButton',
|
|
|
|
size=(72, 24), style=0, pos=(240, 128)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.okButton.SetConstraints(
|
|
|
|
anchors.LayoutAnchors(self.okButton, False, False, True, True)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.Bind(
|
|
|
|
wx.EVT_BUTTON, self.OnOkButtonButton, id=ID_ANCHORSDEMOFRAMEOKBUTTON
|
|
|
|
)
|
|
|
|
|
|
|
|
self.backgroundPanel = wx.Panel(
|
|
|
|
size=(304, 80), parent=self.mainPanel,
|
|
|
|
id=ID_ANCHORSDEMOFRAMEBACKGROUNDPANEL,
|
|
|
|
name='backgroundPanel',
|
|
|
|
style=wx.SIMPLE_BORDER | wx.CLIP_CHILDREN,
|
|
|
|
pos = (8, 40)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.backgroundPanel.SetBackgroundColour(wx.Colour(255, 255, 255))
|
|
|
|
self.backgroundPanel.SetConstraints(
|
|
|
|
anchors.LayoutAnchors(self.backgroundPanel, True, True, True, True)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.anchoredPanel = wx.Panel(
|
|
|
|
size=(88, 48), id=ID_ANCHORSDEMOFRAMEANCHOREDPANEL,
|
|
|
|
parent=self.backgroundPanel, name='anchoredPanel',
|
|
|
|
style=wx.SIMPLE_BORDER, pos=(104, 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.anchoredPanel.SetBackgroundColour(wx.Colour(0, 0, 222))
|
|
|
|
self.anchoredPanel.SetConstraints(
|
|
|
|
anchors.LayoutAnchors(self.anchoredPanel, False, False, False, False)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.leftCheckBox = wx.CheckBox(
|
|
|
|
label='Left', id=ID_ANCHORSDEMOFRAMELEFTCHECKBOX,
|
|
|
|
parent=self.mainPanel, name='leftCheckBox',
|
|
|
|
size=(40, 16), style=0, pos=(8, 8)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.leftCheckBox.SetConstraints(
|
|
|
|
anchors.LayoutAnchors(self.leftCheckBox, False, True, False, False)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.Bind(
|
|
|
|
wx.EVT_CHECKBOX, self.OnCheckboxCheckbox, source=self.leftCheckBox,
|
|
|
|
id=ID_ANCHORSDEMOFRAMELEFTCHECKBOX
|
|
|
|
)
|
|
|
|
|
|
|
|
self.topCheckBox = wx.CheckBox(
|
|
|
|
label='Top', id=ID_ANCHORSDEMOFRAMETOPCHECKBOX,
|
|
|
|
parent=self.mainPanel, name='topCheckBox',
|
|
|
|
size=(40, 16), style=0, pos=(88, 8)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.topCheckBox.SetConstraints(
|
|
|
|
anchors.LayoutAnchors(self.topCheckBox, False, True, False, False)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.Bind(
|
|
|
|
wx.EVT_CHECKBOX, self.OnCheckboxCheckbox, source=self.topCheckBox,
|
|
|
|
id=ID_ANCHORSDEMOFRAMETOPCHECKBOX
|
|
|
|
)
|
|
|
|
|
|
|
|
self.rightCheckBox = wx.CheckBox(
|
|
|
|
label='Right', id=ID_ANCHORSDEMOFRAMERIGHTCHECKBOX,
|
|
|
|
parent=self.mainPanel, name='rightCheckBox',
|
|
|
|
size=(48, 16), style=0, pos=(168, 8)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.rightCheckBox.SetConstraints(
|
|
|
|
anchors.LayoutAnchors(self.rightCheckBox, False, True, False, False)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.Bind(
|
|
|
|
wx.EVT_CHECKBOX, self.OnCheckboxCheckbox, source=self.rightCheckBox,
|
|
|
|
id=ID_ANCHORSDEMOFRAMERIGHTCHECKBOX
|
|
|
|
)
|
|
|
|
|
|
|
|
self.bottomCheckBox = wx.CheckBox(
|
|
|
|
label='Bottom', id=ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX,
|
|
|
|
parent=self.mainPanel, name='bottomCheckBox',
|
|
|
|
size=(56, 16), style=0, pos=(248, 8)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.bottomCheckBox.SetConstraints(
|
|
|
|
anchors.LayoutAnchors(self.bottomCheckBox, False, True, False, False)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.Bind(
|
|
|
|
wx.EVT_CHECKBOX, self.OnCheckboxCheckbox, source=self.bottomCheckBox,
|
|
|
|
id=ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX
|
|
|
|
)
|
|
|
|
|
|
|
|
self.helpStaticText = wx.StaticText(
|
|
|
|
label='Select anchor options above, then resize window to see the effect',
|
|
|
|
id=ID_ANCHORSDEMOFRAMEHELPSTATICTEXT,
|
|
|
|
parent=self.mainPanel, name='helpStaticText',
|
|
|
|
size=(224, 24), style=wx.ST_NO_AUTORESIZE,
|
|
|
|
pos=(8, 128)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.helpStaticText.SetConstraints(
|
|
|
|
anchors.LayoutAnchors(self.helpStaticText, True, False, True, True)
|
|
|
|
)
|
2001-02-16 03:19:50 -05:00
|
|
|
|
|
|
|
def __init__(self, parent):
|
|
|
|
self._init_ctrls(parent)
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Based on the values of the above checkboxes, we will adjust the layout constraints
|
|
|
|
# on the sample window whenever one of the checkboxes changes state.
|
2001-02-16 03:19:50 -05:00
|
|
|
def OnCheckboxCheckbox(self, event):
|
|
|
|
self.anchoredPanel.SetConstraints(
|
2003-12-08 20:23:28 -05:00
|
|
|
anchors.LayoutAnchors(self.anchoredPanel,
|
2001-02-16 03:19:50 -05:00
|
|
|
self.leftCheckBox.GetValue(), self.topCheckBox.GetValue(),
|
2003-12-08 20:23:28 -05:00
|
|
|
self.rightCheckBox.GetValue(), self.bottomCheckBox.GetValue()
|
|
|
|
)
|
|
|
|
)
|
2001-02-16 03:19:50 -05:00
|
|
|
|
|
|
|
def OnOkButtonButton(self, event):
|
|
|
|
self.Close()
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
|
|
|
win = AnchorsDemoFrame(frame)
|
|
|
|
frame.otherWin = win
|
2003-03-25 01:35:27 -05:00
|
|
|
win.Show(True)
|
2001-02-16 03:19:50 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
overview = """<html><body>
|
|
|
|
<h2>LayoutAnchors</h2>
|
|
|
|
A class that implements Delphi's Anchors with wxLayoutConstraints.
|
|
|
|
<p>
|
|
|
|
Anchored sides maintain the distance from the edge of the
|
|
|
|
control to the same edge of the parent.
|
|
|
|
When neither side is selected, the control keeps the same
|
|
|
|
relative position to both sides.
|
|
|
|
<p>
|
|
|
|
The current position and size of the control and it's parent
|
|
|
|
is used when setting up the constraints. To change the size or
|
|
|
|
position of an already anchored control, set the constraints to
|
|
|
|
None, reposition or resize and reapply the anchors.
|
|
|
|
<p>
|
|
|
|
Examples:
|
|
|
|
<p>
|
|
|
|
Let's anchor the right and bottom edge of a control and
|
|
|
|
resize it's parent.
|
|
|
|
<p>
|
|
|
|
<pre>
|
|
|
|
ctrl.SetConstraints(LayoutAnchors(ctrl, left=0, top=0, right=1, bottom=1))
|
|
|
|
|
|
|
|
+=========+ +===================+
|
|
|
|
| +-----+ | | |
|
|
|
|
| | * | -> | |
|
|
|
|
| +--*--+ | | +-----+ |
|
|
|
|
+---------+ | | * |
|
|
|
|
| +--*--+ |
|
|
|
|
+-------------------+
|
|
|
|
* = anchored edge
|
|
|
|
</pre>
|
|
|
|
<p>
|
|
|
|
When anchored on both sides the control will stretch horizontally.
|
|
|
|
<p>
|
|
|
|
<pre>
|
|
|
|
ctrl.SetConstraints(LayoutAnchors(ctrl, 1, 0, 1, 1))
|
|
|
|
|
|
|
|
+=========+ +===================+
|
|
|
|
| +-----+ | | |
|
|
|
|
| * * | -> | |
|
|
|
|
| +--*--+ | | +---------------+ |
|
|
|
|
+---------+ | * ctrl * |
|
|
|
|
| +-------*-------+ |
|
|
|
|
+-------------------+
|
|
|
|
* = anchored edge
|
|
|
|
</pre>
|
|
|
|
</html></body>
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-07-02 19:13:10 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
2004-03-04 19:06:33 -05:00
|
|
|
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|
2001-02-16 03:19:50 -05:00
|
|
|
|