]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/GridBagSizer.py
SetStringSelection and etc. fixes
[wxWidgets.git] / wxPython / demo / GridBagSizer.py
CommitLineData
d14a1e28
RD
1
2import wx # This module uses the new wx namespace
3
4#----------------------------------------------------------------------
5gbsDescription = """\
95bfd958 6The wx.GridBagSizer is similar to the wx.FlexGridSizer except the items are explicitly positioned
d14a1e28
RD
7in a virtual cell of the layout grid, and column or row spanning is allowed. For example, this
8static text is positioned at (0,0) and it spans 7 columns.
9"""
10
11
12class TestFrame(wx.Frame):
13 def __init__(self):
95bfd958 14 wx.Frame.__init__(self, None, -1, "wx.GridBagSizer")
d14a1e28 15 p = wx.Panel(self, -1)
bf08dd12
RD
16 p.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
17
18 gbs = self.gbs = wx.GridBagSizer(5, 5)
d14a1e28
RD
19
20 gbs.Add( wx.StaticText(p, -1, gbsDescription),
21 (0,0), (1,7), wx.ALIGN_CENTER | wx.ALL, 5)
22
d14a1e28
RD
23 gbs.Add( wx.TextCtrl(p, -1, "pos(1,0)"), (1,0) )
24 gbs.Add( wx.TextCtrl(p, -1, "pos(1,1)"), (1,1) )
25 gbs.Add( wx.TextCtrl(p, -1, "pos(2,0)"), (2,0) )
26 gbs.Add( wx.TextCtrl(p, -1, "pos(2,1)"), (2,1) )
27
28 gbs.Add( wx.TextCtrl(p, -1, "pos(3,2), span(1,2)\nthis row and col are growable", style=wx.TE_MULTILINE),
29 (3,2), (1,2), flag=wx.EXPAND )
30
31 gbs.Add( wx.TextCtrl(p, -1, "pos(4,3), span(3,1)", style=wx.TE_MULTILINE),
32 (4,3), (3,1), wx.EXPAND)
33
34 gbs.Add( wx.TextCtrl(p, -1, "pos(5,4)"), (5,4), flag=wx.EXPAND )
35 gbs.Add( wx.TextCtrl(p, -1, "pos(6,5)"), (6,5), flag=wx.EXPAND )
36 gbs.Add( wx.TextCtrl(p, -1, "pos(7,6)"), (7,6) )
37
38 moveBtn1 = wx.Button(p, -1, "Move this to (3,6)")
39 moveBtn2 = wx.Button(p, -1, "Move this to (3,6)");
40 gbs.Add( moveBtn1, (10,2) )
41 gbs.Add( moveBtn2, (10,3) )
42
43 hideBtn = wx.Button(p, -1, "Hide this item -->")
44 gbs.Add(hideBtn, (12, 3))
45
46 hideTxt = wx.TextCtrl(p, -1, "pos(12,4), size(150, -1)", size = (150,-1))
47 gbs.Add( hideTxt, (12,4) )
48
49 showBtn = wx.Button(p, -1, "<-- Show it again")
50 gbs.Add(showBtn, (12, 5))
51 showBtn.Disable()
52 self.hideBtn = hideBtn
53 self.showBtn = showBtn
54 self.hideTxt = hideTxt
55
56 self.Bind(wx.EVT_BUTTON, self.OnHideButton, hideBtn)
57 self.Bind(wx.EVT_BUTTON, self.OnShowButton, showBtn)
58 self.Bind(wx.EVT_BUTTON, self.OnMoveButton, moveBtn1)
59 self.Bind(wx.EVT_BUTTON, self.OnMoveButton, moveBtn2)
60
61 # Add a spacer at the end to ensure some extra space at the bottom
bf08dd12 62 gbs.Add((10,10), (14,7))
d14a1e28
RD
63
64 gbs.AddGrowableRow(3)
65 gbs.AddGrowableCol(2)
66
67 p.SetSizerAndFit(gbs)
68 self.SetClientSize(p.GetSize())
69
70
71 def OnHideButton(self, evt):
72 self.gbs.Hide(self.hideTxt)
73 self.hideBtn.Disable()
74 self.showBtn.Enable()
75 self.gbs.Layout()
76
77
78 def OnShowButton(self, evt):
79 self.gbs.Show(self.hideTxt)
80 self.hideBtn.Enable()
81 self.showBtn.Disable()
82 self.gbs.Layout()
83
84
85 def OnMoveButton(self, evt):
86 btn = evt.GetEventObject()
87 curPos = self.gbs.GetItemPosition(btn)
88
89 # if it's already at the "other" spot then move it back
90 if curPos == (3,6):
91 self.gbs.SetItemPosition(btn, self.lastPos)
92 btn.SetLabel("Move this to (3,6)")
93 else:
94 if self.gbs.CheckForIntersection( (3,6), (1,1) ):
95 wx.MessageBox("""\
95bfd958 96wx.GridBagSizer will not allow items to be in the same cell as
d14a1e28
RD
97another item, so this operation will fail. You will also get an assert
98when compiled in debug mode.""",
99 "Warning", wx.OK | wx.ICON_INFORMATION)
100
101 try:
102 if self.gbs.SetItemPosition(btn, (3,6)):
103 self.lastPos = curPos
104 btn.SetLabel("Move it back")
105 except wx.PyAssertionError:
106 pass
107
108 self.gbs.Layout()
109
bf08dd12
RD
110
111 def OnLeftDown(self, evt):
112 pt = evt.GetPosition()
113 item = self.gbs.FindItemAtPoint(pt)
114 if item is None:
115 print "no item at", `pt`
116 else:
117 print "item found: ", `item.GetPos()`, "--", `item.GetSpan()`
118
d14a1e28
RD
119
120#----------------------------------------------------------------------
121
122def runTest(frame, nb, log):
123 win = TestFrame()
124 frame.otherWin = win
125 win.Show(True)
126
127
128#----------------------------------------------------------------------
129
130
131
132overview = """<html><body>
95bfd958 133<h2><center>wx.GridBagSizer</center></h2>
d14a1e28 134
95bfd958 135The wx.GridBagSizer is more or less a port of the the RowColSizer (that
d14a1e28
RD
136has been in the wxPython.lib package for quite some time) to C++. It
137allows items to be placed at specific layout grid cells, and items can
138span across more than one row or column.
139</body></html>
140"""
141
142
143
144if __name__ == '__main__':
145 import sys,os
146 import run
8eca4fef 147 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
d14a1e28 148