]>
Commit | Line | Data |
---|---|---|
d115ca5e RD |
1 | |
2 | ||
3 | import wx | |
4 | ||
5 | testItems = [ | |
6 | "", | |
7 | "BitmapButton", | |
8 | "Button", | |
9 | "CalendarCtrl", | |
10 | "CheckBox", | |
11 | "CheckListBox", | |
12 | "Choice", | |
13 | "ComboBox", | |
14 | "Gauge", | |
15 | "GenericDirCtrl", | |
16 | "ListBox", | |
17 | "ListCtrl", | |
18 | "RadioBox", | |
19 | "RadioButton", | |
20 | "ScrollBar", | |
21 | "Slider", | |
22 | "SpinButton", | |
23 | "SpinCtrl", | |
24 | "StaticBitmap", | |
25 | "StaticBox", | |
26 | "StaticLine", | |
27 | "StaticText", | |
28 | "TextCtrl", | |
29 | "ToggleButton", | |
30 | "TreeCtrl", | |
31 | "--------------", | |
32 | "Panel", | |
33 | "Panel With Border", | |
34 | "Panel With BG", | |
35 | "Panel With Controls", | |
36 | "Panel With RadioBox", | |
37 | "--------------", | |
38 | "GenericButton", | |
39 | ||
40 | ] | |
41 | ||
42 | import keyword | |
43 | testChoices = keyword.kwlist | |
44 | ||
45 | testChoices2 = "one two three four five six seven eight nine".split() | |
46 | ||
47 | ||
48 | ||
49 | ||
50 | class Frame(wx.Frame): | |
51 | def __init__(self): | |
52 | wx.Frame.__init__(self, None, title="Draw Widget Test") | |
53 | self.left = wx.Panel(self) | |
54 | self.right = DisplayPanel(self) | |
55 | self.widget = None | |
56 | ||
57 | sizer = wx.BoxSizer(wx.HORIZONTAL) | |
58 | sizer.Add(self.left, 1, wx.EXPAND) | |
59 | sizer.Add(self.right, 1, wx.EXPAND) | |
60 | self.SetSizer(sizer) | |
61 | ||
62 | menu = wx.Menu() | |
63 | menu.Append(wx.ID_EXIT, "E&xit\tAlt-X") | |
64 | self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT) | |
65 | mbar = wx.MenuBar() | |
66 | mbar.Append(menu, "&File") | |
67 | self.SetMenuBar(mbar) | |
68 | ||
69 | cb = wx.ComboBox(self.left, -1, pos=(20,20), | |
70 | choices=testItems, style=wx.CB_READONLY) | |
71 | self.Bind(wx.EVT_COMBOBOX, self.OnWidgetChosen, cb) | |
72 | ||
73 | ||
74 | def OnWidgetChosen(self, evt): | |
75 | item = evt.GetString() | |
76 | item = item.replace(" ", "_") | |
77 | func = getattr(self, "Test"+item, None) | |
78 | if func is not None: | |
79 | func(self.left) | |
80 | ||
81 | def OnExit(self, evt): | |
82 | self.Close() | |
83 | ||
84 | ||
85 | ||
86 | def DoWidget(self, widget): | |
87 | self.right.Empty() | |
88 | if self.widget is not None: | |
89 | self.widget.Destroy() | |
90 | self.widget = widget | |
91 | if widget is None: | |
92 | return | |
93 | ||
94 | visiblePos = (20,80) | |
95 | hiddenPos = (-1000,-1000) | |
96 | ||
97 | widget.SetPosition(visiblePos) | |
98 | widget.Update() | |
99 | self.GetBMP(0) # Uses just a DC.Blit, so it must be visible | |
100 | ||
101 | # the rest should work when the widget is not visible. | |
102 | widget.SetPosition(hiddenPos) | |
103 | widget.Update() | |
104 | self.Update() | |
105 | ||
106 | self.GetBMP(1) | |
107 | self.GetBMP(2) | |
108 | self.GetBMP(4) | |
109 | ||
110 | # make it visible again for the user to compare | |
111 | widget.SetPosition(visiblePos) | |
112 | ||
113 | ||
114 | def GetBMP(self, method): | |
115 | w = self.widget | |
116 | maskClr = wx.Colour(12, 34, 56) | |
117 | sz = w.GetSize() | |
118 | bmp = wx.EmptyBitmap(sz.width, sz.height) | |
119 | dc = wx.MemoryDC() | |
120 | dc.SelectObject(bmp) | |
121 | dc.SetBackground(wx.Brush(maskClr)) | |
122 | dc.Clear() | |
123 | ||
124 | if method == 0: | |
125 | wdc = wx.WindowDC(w) | |
126 | dc.Blit(0,0, sz.width, sz.height, wdc, 0, 0) | |
127 | else: | |
128 | wx.DrawWindowOnDC(w, dc, method) | |
129 | ||
130 | dc.SelectObject(wx.NullBitmap) | |
131 | bmp.SetMaskColour(maskClr) | |
132 | self.right.SetBMP(bmp, method) | |
133 | ||
134 | ||
135 | ||
136 | ||
137 | ||
138 | def Test(self, p): | |
139 | self.DoWidget(None) | |
140 | ||
141 | def TestBitmapButton(self, p): | |
142 | self.DoWidget(wx.BitmapButton(p, -1, | |
143 | wx.Bitmap("image.png"))) | |
144 | def TestButton(self, p): | |
145 | self.DoWidget(wx.Button(p, -1, "A button")) | |
146 | ||
147 | def TestCalendarCtrl(self, p): | |
148 | import wx.calendar | |
149 | w = wx.calendar.CalendarCtrl(p, style=wx.calendar.CAL_SEQUENTIAL_MONTH_SELECTION) | |
150 | self.DoWidget(w) | |
151 | ||
152 | def TestCheckBox(self, p): | |
153 | self.DoWidget(wx.CheckBox(p, -1, "checkbox")) | |
154 | ||
155 | def TestCheckListBox(self, p): | |
156 | w = wx.CheckListBox(p, -1, choices=testChoices) | |
157 | w.SetSelection(2) | |
158 | self.DoWidget(w) | |
159 | ||
160 | def TestChoice(self, p): | |
161 | w = wx.Choice(p, -1, choices=testChoices) | |
162 | w.SetSelection(2) | |
163 | self.DoWidget(w) | |
164 | ||
165 | def TestComboBox(self, p): | |
166 | w = wx.ComboBox(p, -1, choices=testChoices) | |
167 | w.SetSelection(2) | |
168 | self.DoWidget(w) | |
169 | ||
170 | def TestGauge(self, p): | |
171 | w = wx.Gauge(p, -1, 100, size=(150, -1)) | |
172 | w.SetValue(65) | |
173 | self.DoWidget(w) | |
174 | ||
175 | def TestGenericDirCtrl(self, p): | |
176 | w = wx.GenericDirCtrl(p, size=(150,200), style=wx.DIRCTRL_DIR_ONLY) | |
177 | self.DoWidget(w) | |
178 | ||
179 | def TestListBox(self, p): | |
180 | w = wx.ListBox(p, -1, choices=testChoices) | |
181 | w.SetSelection(2) | |
182 | self.DoWidget(w) | |
183 | ||
184 | def TestListCtrl(self, p): | |
185 | w = wx.ListCtrl(p, -1, size=(250, 100), style=wx.LC_REPORT) | |
186 | w.InsertColumn(0, "Col 1") | |
187 | w.InsertColumn(1, "Col 2") | |
188 | w.InsertColumn(2, "Col 3") | |
189 | for x in range(10): | |
190 | w.InsertStringItem(x, str(x)) | |
191 | w.SetStringItem(x, 1, str(x)) | |
192 | w.SetStringItem(x, 2, str(x)) | |
193 | self.DoWidget(w) | |
194 | ||
195 | def TestRadioBox(self, p): | |
196 | w = wx.RadioBox(p, -1, "RadioBox", | |
197 | choices=testChoices2, majorDimension=3) | |
198 | self.DoWidget(w) | |
199 | ||
200 | def TestRadioButton(self, p): | |
201 | self.DoWidget(wx.RadioButton(p, -1, "RadioButton")) | |
202 | ||
203 | def TestScrollBar(self, p): | |
204 | w = wx.ScrollBar(p, -1, size=(150,-1)) | |
205 | w.SetScrollbar(25, 5, 100, 10) | |
206 | self.DoWidget(w) | |
207 | ||
208 | def TestSlider(self, p): | |
209 | w = wx.Slider(p, -1, size=(150,-1)) | |
210 | self.DoWidget(w) | |
211 | ||
212 | def TestSpinButton(self, p): | |
213 | w = wx.SpinButton(p, -1) | |
214 | self.DoWidget(w) | |
215 | ||
216 | def TestSpinCtrl(self, p): | |
217 | w = wx.SpinCtrl(p, -1) | |
218 | self.DoWidget(w) | |
219 | ||
220 | def TestStaticBitmap(self, p): | |
221 | w = wx.StaticBitmap(p, -1, wx.Bitmap("image.png")) | |
222 | self.DoWidget(w) | |
223 | ||
224 | def TestStaticBox(self, p): | |
225 | w = wx.StaticBox(p, -1, "StaticBox", size=(150,75)) | |
226 | self.DoWidget(w) | |
227 | ||
228 | def TestStaticLine(self, p): | |
229 | w = wx.StaticLine(p, -1, size=(150,-1)) | |
230 | self.DoWidget(w) | |
231 | ||
232 | def TestStaticText(self, p): | |
233 | w = wx.StaticText(p, -1, "This is a wx.StaticText") | |
234 | self.DoWidget(w) | |
235 | ||
236 | def TestTextCtrl(self, p): | |
237 | self.DoWidget(wx.TextCtrl(p, -1, "This is a TextCtrl", size=(150,-1))) | |
238 | ||
239 | def TestToggleButton(self, p): | |
240 | w = wx.ToggleButton(p, -1, "Toggle Button") | |
241 | self.DoWidget(w) | |
242 | ||
243 | def TestTreeCtrl(self, p): | |
244 | w = wx.TreeCtrl(p, -1, size=(150,200)) | |
245 | root = w.AddRoot("The Root Item") | |
246 | for x in range(15): | |
247 | child = w.AppendItem(root, "Item %d" % x) | |
248 | w.Expand(root) | |
249 | self.DoWidget(w) | |
250 | ||
251 | def TestPanel(self, p): | |
252 | w = wx.Panel(p, size=(100,100)) | |
253 | self.DoWidget(w) | |
254 | ||
255 | def TestPanel_With_Border(self, p): | |
256 | w = wx.Panel(p, size=(100,100), style=wx.SUNKEN_BORDER) | |
257 | self.DoWidget(w) | |
258 | ||
259 | def TestPanel_With_BG(self, p): | |
260 | w = wx.Panel(p, size=(100,100), style=wx.SUNKEN_BORDER) | |
261 | w.SetBackgroundColour("pink") | |
262 | self.DoWidget(w) | |
263 | ||
264 | def TestPanel_With_Controls(self, p): | |
265 | w = wx.Panel(p, size=(100,100), style=wx.SUNKEN_BORDER) | |
266 | l1 = wx.StaticText(w, -1, "Name:") | |
267 | l2 = wx.StaticText(w, -1, "Email:") | |
268 | t1 = wx.TextCtrl(w, -1, "", size=(120,-1)) | |
269 | t2 = wx.TextCtrl(w, -1, "", size=(120,-1)) | |
270 | btn = wx.Button(w, wx.ID_OK) | |
271 | sizer = wx.BoxSizer(wx.VERTICAL) | |
272 | fgs = wx.FlexGridSizer(2,2,5,5) | |
273 | fgs.Add(l1) | |
274 | fgs.Add(t1) | |
275 | fgs.Add(l2) | |
276 | fgs.Add(t2) | |
277 | sizer.Add(fgs, 0, wx.ALL, 10) | |
278 | sizer.Add(btn, 0, wx.ALL, 10) | |
279 | w.SetSizerAndFit(sizer) | |
280 | self.DoWidget(w) | |
281 | ||
282 | def TestPanel_With_RadioBox(self, p): | |
283 | w = wx.Panel(p, size=(100,100), style=wx.SUNKEN_BORDER) | |
284 | wx.RadioBox(w, -1, "RadioBox", pos=(10,10), | |
285 | choices=testChoices2, majorDimension=3) | |
286 | w.Fit() | |
287 | self.DoWidget(w) | |
288 | ||
289 | def TestGenericButton(self, p): | |
290 | import wx.lib.buttons as b | |
291 | w = b.GenButton(p, -1, "Generic Button") | |
292 | w.SetFont(wx.Font(20, wx.SWISS, wx.NORMAL, wx.BOLD, False)) | |
293 | w.SetBezelWidth(5) | |
294 | w.SetBestSize() | |
295 | w.SetBackgroundColour("Navy") | |
296 | w.SetForegroundColour(wx.WHITE) | |
297 | self.DoWidget(w) | |
298 | ||
299 | def TestT(self, p): | |
300 | self.DoWidget(w) | |
301 | ||
302 | def TestT(self, p): | |
303 | self.DoWidget(w) | |
304 | ||
305 | def TestT(self, p): | |
306 | self.DoWidget(w) | |
307 | ||
308 | def TestT(self, p): | |
309 | self.DoWidget(w) | |
310 | ||
311 | ||
312 | ||
313 | ||
314 | ||
315 | ||
316 | ||
317 | class DisplayPanel(wx.Panel): | |
318 | def __init__(self, parent, ID=-1): | |
319 | wx.Panel.__init__(self, parent, ID) | |
320 | self.SetBackgroundColour("sky blue") | |
321 | self.Empty() | |
322 | self.Bind(wx.EVT_PAINT, self.OnPaint) | |
323 | self.Bind(wx.EVT_ERASE_BACKGROUND, self.OEB) | |
324 | ||
325 | def Empty(self): | |
326 | self.bmps = [None] * 5 | |
327 | self.Refresh() | |
328 | ||
329 | def SetBMP(self, bmp, method): | |
330 | self.bmps[method] = bmp | |
331 | self.Refresh() | |
332 | ||
333 | def OEB(self, evt): | |
334 | None | |
335 | ||
336 | def OnPaint(self, evt): | |
337 | dc = wx.BufferedPaintDC(self) | |
338 | dc.SetBackground(wx.Brush(self.GetBackgroundColour())) | |
339 | dc.Clear() | |
340 | y = 25 | |
341 | for idx, bmp in enumerate(self.bmps): | |
342 | if bmp is not None: | |
343 | dc.DrawText(str(idx), 15, y) | |
344 | dc.DrawBitmap(bmp, 30,y, True) | |
345 | y += bmp.GetHeight() + 15 | |
346 | ||
347 | ||
348 | ||
349 | app = wx.App(False) | |
350 | f = Frame() | |
351 | f.SetSize((600,600)) | |
352 | f.Show() | |
353 | app.MainLoop() |