]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/Main.py
Cleaned up paint DC cache in ~wxPaintDC to avoid spurious memory warning
[wxWidgets.git] / utils / wxPython / demo / Main.py
1 #!/bin/env python
2 #----------------------------------------------------------------------------
3 # Name: Main.py
4 # Purpose: Testing lots of stuff, controls, window types, etc.
5 #
6 # Author: Robin Dunn & Gary Dumer
7 #
8 # Created:
9 # RCS-ID: $Id$
10 # Copyright: (c) 1999 by Total Control Software
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------------
13
14 import sys, os
15 from wxPython.wx import *
16
17
18 #---------------------------------------------------------------------------
19
20 _useSplitter = true
21 _useNestedSplitter = true
22
23 _treeList = [
24 ('Managed Windows', ['wxFrame', 'wxDialog', 'wxMiniFrame']),
25
26 ('Miscellaneous Windows', ['wxGrid', 'wxSashWindow',
27 'wxScrolledWindow', 'wxSplitterWindow',
28 'wxStatusBar', 'wxToolBar', 'wxNotebook',
29 'wxHtmlWindow']),
30
31 ('Common Dialogs', ['wxColourDialog', 'wxDirDialog', 'wxFileDialog',
32 'wxSingleChoiceDialog', 'wxTextEntryDialog',
33 'wxFontDialog', 'wxPageSetupDialog', 'wxPrintDialog',
34 'wxMessageDialog', 'wxProgressDialog']),
35
36 ('Controls', ['wxButton', 'wxCheckBox', 'wxCheckListBox', 'wxChoice',
37 'wxComboBox', 'wxGauge', 'wxListBox', 'wxListCtrl', 'wxTextCtrl',
38 'wxTreeCtrl', 'wxSpinButton', 'wxStaticText', 'wxStaticBitmap',
39 'wxRadioBox', 'wxSlider']),
40
41 ('Window Layout', ['wxLayoutConstraints', 'Sizers', 'OldSizers']),
42
43 ('Miscellaneous', ['wxTimer', 'wxValidator', 'wxGLCanvas', 'DialogUnits',
44 'wxImage', 'PrintFramework', 'wxOGL']),
45
46 ('wxPython Library', ['OldSizers', 'Layoutf', 'wxScrolledMessageDialog',
47 'wxMultipleChoiceDialog', 'wxPlotCanvas', 'wxFloatBar',
48 'PyShell']),
49
50 ('Cool Contribs', ['pyTree', 'hangman', 'SlashDot', 'XMLtreeview']),
51
52 ]
53
54 #---------------------------------------------------------------------------
55
56 class wxPythonDemo(wxFrame):
57 def __init__(self, parent, id, title):
58 wxFrame.__init__(self, parent, -1, title, size = (725, 550))
59
60 if wxPlatform == '__WXMSW__':
61 self.icon = wxIcon('bitmaps/mondrian.ico', wxBITMAP_TYPE_ICO)
62 self.SetIcon(self.icon)
63
64 self.otherWin = None
65 EVT_IDLE(self, self.OnIdle)
66
67 self.Centre(wxBOTH)
68 self.CreateStatusBar(1, wxST_SIZEGRIP)
69
70 if _useSplitter:
71 splitter = wxSplitterWindow(self, -1)
72 if _useNestedSplitter:
73 splitter2 = wxSplitterWindow(splitter, -1)
74 logParent = nbParent = splitter2
75 else:
76 nbParent = splitter
77 logParent = wxFrame(self, -1, "wxPython Demo: log window",
78 (0,0), (500, 150))
79 logParent.Show(true)
80 else:
81 nbParent = self
82 logParent = wxFrame(self, -1, "wxPython Demo: log window",
83 (0,0), (500, 150))
84 logParent.Show(true)
85
86
87
88 # Prevent TreeCtrl from displaying all items after destruction
89 self.dying = false
90
91 # Make a File menu
92 self.mainmenu = wxMenuBar()
93 menu = wxMenu()
94 exitID = wxNewId()
95 menu.Append(exitID, 'E&xit\tAlt-X', 'Get the heck outta here!')
96 EVT_MENU(self, exitID, self.OnFileExit)
97 self.mainmenu.Append(menu, '&File')
98
99 # Make a Demo menu
100 menu = wxMenu()
101 for item in _treeList:
102 submenu = wxMenu()
103 for childItem in item[1]:
104 mID = wxNewId()
105 submenu.Append(mID, childItem)
106 EVT_MENU(self, mID, self.OnDemoMenu)
107 menu.AppendMenu(wxNewId(), item[0], submenu)
108 self.mainmenu.Append(menu, '&Demo')
109
110
111 # Make a Help menu
112 helpID = wxNewId()
113 menu = wxMenu()
114 menu.Append(helpID, '&About\tCtrl-H', 'wxPython RULES!!!')
115 EVT_MENU(self, helpID, self.OnHelpAbout)
116 self.mainmenu.Append(menu, '&Help')
117 self.SetMenuBar(self.mainmenu)
118
119 # set the menu accellerator table...
120 aTable = wxAcceleratorTable([(wxACCEL_ALT, ord('X'), exitID),
121 (wxACCEL_CTRL, ord('H'), helpID)])
122 self.SetAcceleratorTable(aTable)
123
124
125 # Create a TreeCtrl
126 if _useSplitter:
127 tID = wxNewId()
128 self.treeMap = {}
129 self.tree = wxTreeCtrl(splitter, tID)
130 root = self.tree.AddRoot("Overview")
131 for item in _treeList:
132 child = self.tree.AppendItem(root, item[0])
133 for childItem in item[1]:
134 theDemo = self.tree.AppendItem(child, childItem)
135 self.treeMap[childItem] = theDemo
136
137 self.tree.Expand(root)
138 EVT_TREE_ITEM_EXPANDED (self.tree, tID, self.OnItemExpanded)
139 EVT_TREE_ITEM_COLLAPSED (self.tree, tID, self.OnItemCollapsed)
140 EVT_TREE_SEL_CHANGED (self.tree, tID, self.OnSelChanged)
141
142 # Create a Notebook
143 self.nb = wxNotebook(nbParent, -1)
144
145 # Set up a TextCtrl on the Overview Notebook page
146 self.ovr = wxTextCtrl(self.nb, -1, style = wxTE_MULTILINE|wxTE_READONLY)
147 self.nb.AddPage(self.ovr, "Overview")
148
149
150 # Set up a TextCtrl on the Demo Code Notebook page
151 self.txt = wxTextCtrl(self.nb, -1,
152 style = wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
153 self.txt.SetFont(wxFont(9, wxMODERN, wxNORMAL, wxNORMAL, false))
154 self.nb.AddPage(self.txt, "Demo Code")
155
156
157 # Set up a log on the View Log Notebook page
158 self.log = wxTextCtrl(logParent, -1,
159 style = wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
160 (w, self.charHeight) = self.log.GetTextExtent('X')
161 self.WriteText('wxPython Demo Log:\n')
162
163
164 # add the windows to the splitter and split it.
165 if _useSplitter:
166 if _useNestedSplitter:
167 splitter2.SplitHorizontally(self.nb, self.log)
168 splitter2.SetSashPosition(360, true)
169 splitter2.SetMinimumPaneSize(20)
170
171 splitter.SplitVertically(self.tree, splitter2)
172 else:
173 splitter.SplitVertically(self.tree, self.nb)
174
175 splitter.SetSashPosition(180, true)
176 splitter.SetMinimumPaneSize(20)
177
178
179 # make our log window be stdout
180 #sys.stdout = self
181
182 # select initial items
183 self.nb.SetSelection(0)
184 if _useSplitter:
185 self.tree.SelectItem(root)
186
187 if len(sys.argv) == 2:
188 try:
189 selectedDemo = self.treeMap[sys.argv[1]]
190 except:
191 selectedDemo = None
192 if selectedDemo and _useSplitter:
193 self.tree.SelectItem(selectedDemo)
194 self.tree.EnsureVisible(selectedDemo)
195
196
197 self.WriteText('window handle: %s\n' % self.GetHandle())
198
199
200 #---------------------------------------------
201 def WriteText(self, text):
202 self.log.WriteText(text)
203 w, h = self.log.GetClientSizeTuple()
204 numLines = h/self.charHeight
205 x, y = self.log.PositionToXY(self.log.GetLastPosition())
206 if y > numLines:
207 self.log.ShowPosition(self.log.XYToPosition(x, y-numLines))
208 ##self.log.ShowPosition(self.log.GetLastPosition())
209 self.log.SetInsertionPointEnd()
210
211 def write(self, txt):
212 self.WriteText(txt)
213
214 #---------------------------------------------
215 def OnItemExpanded(self, event):
216 item = event.GetItem()
217 self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item))
218
219 #---------------------------------------------
220 def OnItemCollapsed(self, event):
221 item = event.GetItem()
222 self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item))
223
224 #---------------------------------------------
225 def OnSelChanged(self, event):
226 if self.dying:
227 return
228
229 item = event.GetItem()
230 itemText = self.tree.GetItemText(item)
231 self.RunDemo(itemText)
232
233
234 #---------------------------------------------
235 def RunDemo(self, itemText):
236 if self.nb.GetPageCount() == 3:
237 if self.nb.GetSelection() == 2:
238 self.nb.SetSelection(0)
239 self.nb.DeletePage(2)
240
241 if itemText == 'Overview':
242 self.GetDemoFile('Main.py')
243 self.SetOverview('Overview', overview)
244 self.nb.Refresh();
245 self.window = None
246
247 else:
248 if os.path.exists(itemText + '.py'):
249 wxBeginBusyCursor()
250 self.GetDemoFile(itemText + '.py')
251 module = __import__(itemText, globals())
252 self.SetOverview(itemText, module.overview)
253 wxEndBusyCursor()
254
255 # in case runTest is modal, make sure things look right...
256 self.nb.Refresh();
257 wxYield()
258
259 self.window = module.runTest(self, self.nb, self)
260 if self.window:
261 self.nb.AddPage(self.window, 'Demo')
262 #self.nb.ResizeChildren()
263 self.nb.SetSelection(2)
264 #self.nb.ResizeChildren()
265 #if self.window.GetAutoLayout():
266 # self.window.Layout()
267
268 else:
269 self.ovr.Clear()
270 self.txt.Clear()
271 self.window = None
272
273
274
275 #---------------------------------------------
276 # Get the Demo files
277 def GetDemoFile(self, filename):
278 self.txt.Clear()
279 #if not self.txt.LoadFile(filename):
280 # self.txt.WriteText("Cannot open %s file." % filename)
281 try:
282 self.txt.SetValue(open(filename).read())
283 except IOError:
284 self.txt.WriteText("Cannot open %s file." % filename)
285
286
287 self.txt.SetInsertionPoint(0)
288 self.txt.ShowPosition(0)
289
290 #---------------------------------------------
291 def SetOverview(self, name, text):
292 self.ovr.Clear()
293 self.ovr.WriteText(text)
294 self.nb.SetPageText(0, name)
295 self.ovr.SetInsertionPoint(0)
296 self.ovr.ShowPosition(0)
297
298 #---------------------------------------------
299 # Menu methods
300 def OnFileExit(self, event):
301 self.Close()
302
303
304 def OnHelpAbout(self, event):
305 #about = wxMessageDialog(self,
306 # "wxPython is a Python extension module that\n"
307 # "encapsulates the wxWindows GUI classes.\n\n"
308 # "This demo shows off some of the capabilities\n"
309 # "of wxPython.\n\n"
310 # " Developed by Robin Dunn",
311 # "About wxPython", wxOK)
312 from About import MyAboutBox
313 about = MyAboutBox(self)
314 about.ShowModal()
315 about.Destroy()
316
317
318 #---------------------------------------------
319 def OnCloseWindow(self, event):
320 self.dying = true
321 self.window = None
322 self.mainmenu = None
323 self.Destroy()
324
325 #---------------------------------------------
326 def OnIdle(self, event):
327 if self.otherWin:
328 self.otherWin.Raise()
329 self.window = self.otherWin
330 self.otherWin = None
331
332 #---------------------------------------------
333 def OnDemoMenu(self, event):
334 if _useSplitter:
335 try:
336 selectedDemo = self.treeMap[self.mainmenu.GetLabel(event.GetId())]
337 except:
338 selectedDemo = None
339 if selectedDemo:
340 self.tree.SelectItem(selectedDemo)
341 self.tree.EnsureVisible(selectedDemo)
342 else:
343 self.RunDemo(self.mainmenu.GetLabel(event.GetId()))
344
345 #---------------------------------------------------------------------------
346 #---------------------------------------------------------------------------
347
348 class MyApp(wxApp):
349 def OnInit(self):
350 wxImage_AddHandler(wxJPEGHandler())
351 wxImage_AddHandler(wxPNGHandler())
352 wxImage_AddHandler(wxGIFHandler())
353 frame = wxPythonDemo(NULL, -1, "wxPython: (A Demonstration)")
354 frame.Show(true)
355 self.SetTopWindow(frame)
356 return true
357
358 #---------------------------------------------------------------------------
359
360 def main():
361 app = MyApp(0)
362 app.MainLoop()
363
364
365 #---------------------------------------------------------------------------
366
367
368
369 overview = """\
370 Python
371 ------------
372
373 Python is an interpreted, interactive, object-oriented programming language often compared to Tcl, Perl, Scheme, or Java.
374
375 Python combines remarkable power with very clear syntax. It has modules, classes, exceptions, very high level dynamic data types, and dynamic typing. There are interfaces to many system calls and libraries, and new built-in modules are easily written in C or C++. Python is also usable as an extension language for applications that need a programmable interface.
376
377 wxWindows
378 --------------------
379
380 wxWindows is a free C++ framework designed to make cross-platform programming child's play. Well, almost. wxWindows 2 supports Windows 3.1/95/98/NT, Unix with GTK/Motif/Lesstif, with a Mac version underway. Other ports are under consideration.
381
382 wxWindows is a set of libraries that allows C++ applications to compile and run on several different types of computers, with minimal source code changes. There is one library per supported GUI (such as Motif, or Windows). As well as providing a common API (Application Programming Interface) for GUI functionality, it provides functionality for accessing some commonly-used operating system facilities, such as copying or deleting files. wxWindows is a 'framework' in the sense that it provides a lot of built-in functionality, which the application can use or replace as required, thus saving a great deal of coding effort. Basic data structures such as strings, linked lists and hash tables are also supported.
383
384 wxPython
385 ----------------
386
387 wxPython is a Python extension module that encapsulates the wxWindows GUI classes. Currently it is only available for the Win32 and GTK ports of wxWindows, but as soon as the other ports are brought up to the same level as Win32 and GTK, it should be fairly trivial to enable wxPython to be used with the new GUI.
388
389 The wxPython extension module attempts to mirror the class heiarchy of wxWindows as closely as possible. This means that there is a wxFrame class in wxPython that looks, smells, tastes and acts almost the same as the wxFrame class in the C++ version. Unfortunately, because of differences in the languages, wxPython doesn't match wxWindows exactly, but the differences should be easy to absorb because they are natural to Python. For example, some methods that return multiple values via argument pointers in C++ will return a tuple of values in Python.
390
391 There is still much to be done for wxPython, many classes still need to be mirrored. Also, wxWindows is still somewhat of a moving target so it is a bit of an effort just keeping wxPython up to date. On the other hand, there are enough of the core classes completed that useful applications can be written.
392
393 wxPython is close enough to the C++ version that the majority of the wxPython documentation is actually just notes attached to the C++ documents that describe the places where wxPython is different. There is also a series of sample programs included, and a series of documentation pages that assist the programmer in getting started with wxPython.
394 """
395
396
397
398
399
400
401
402 #----------------------------------------------------------------------------
403 #----------------------------------------------------------------------------
404
405 if __name__ == '__main__':
406 main()
407
408 #----------------------------------------------------------------------------
409
410
411
412
413
414
415