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