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