]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/Main.py
Added wrapper for wxListCtrl.SortItems. Added column sorting to the
[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 from wxPython.lib.splashscreen import SplashScreen
17
18 #---------------------------------------------------------------------------
19
20 _useSplitter = true
21 _useNestedSplitter = true
22
23 _treeList = [
24 ('New since last release', []),
25
26 ('Managed Windows', ['wxFrame', 'wxDialog', 'wxMiniFrame']),
27
28 ('Non-Managed Windows', ['wxGrid', 'wxSashWindow',
29 'wxScrolledWindow', 'wxSplitterWindow',
30 'wxStatusBar', 'wxNotebook',
31 'wxHtmlWindow']),
32
33 ('Common Dialogs', ['wxColourDialog', 'wxDirDialog', 'wxFileDialog',
34 'wxSingleChoiceDialog', 'wxTextEntryDialog',
35 'wxFontDialog', 'wxPageSetupDialog', 'wxPrintDialog',
36 'wxMessageDialog', 'wxProgressDialog']),
37
38 ('Controls', ['wxButton', 'wxCheckBox', 'wxCheckListBox', 'wxChoice',
39 'wxComboBox', 'wxGauge', 'wxListBox', 'wxListCtrl', 'wxTextCtrl',
40 'wxTreeCtrl', 'wxSpinButton', 'wxStaticText', 'wxStaticBitmap',
41 'wxRadioBox', 'wxSlider', 'wxToolBar', #'wxToggleButton'
42 ]),
43
44 ('Window Layout', ['wxLayoutConstraints', 'Sizers', 'OldSizers']),
45
46 ('Miscellaneous', [ 'DragAndDrop', 'CustomDragAndDrop', 'FontEnumerator',
47 'wxTimer', 'wxValidator', 'wxGLCanvas', 'DialogUnits',
48 'wxImage', 'wxMask', 'PrintFramework', 'wxOGL',
49 'PythonEvents', 'Threads']),
50
51 ('wxPython Library', ['Layoutf', 'wxScrolledMessageDialog',
52 'wxMultipleChoiceDialog', 'wxPlotCanvas', 'wxFloatBar',
53 'PyShell', 'wxCalendar', 'wxMVCTree', 'wxVTKRenderWindow',
54 'FileBrowseButton', 'GenericButtons', 'wxEditor']),
55
56 ('Cool Contribs', ['pyTree', 'hangman', 'SlashDot', 'XMLtreeview']),
57
58 ]
59
60 #---------------------------------------------------------------------------
61
62 class wxPythonDemo(wxFrame):
63 def __init__(self, parent, id, title):
64 wxFrame.__init__(self, parent, -1, title, size = (725, 550))
65
66 self.cwd = os.getcwd()
67
68 if wxPlatform == '__WXMSW__':
69 self.icon = wxIcon('bitmaps/mondrian.ico', wxBITMAP_TYPE_ICO)
70 self.SetIcon(self.icon)
71
72 self.otherWin = None
73 EVT_IDLE(self, self.OnIdle)
74
75 self.Centre(wxBOTH)
76 self.CreateStatusBar(1, wxST_SIZEGRIP)
77
78 if _useSplitter:
79 splitter = wxSplitterWindow(self, -1)
80 if _useNestedSplitter:
81 splitter2 = wxSplitterWindow(splitter, -1)
82 logParent = nbParent = splitter2
83 else:
84 nbParent = splitter
85 logParent = wxFrame(self, -1, "wxPython Demo: log window",
86 (0,0), (500, 150))
87 logParent.Show(true)
88 else:
89 nbParent = self
90 logParent = wxFrame(self, -1, "wxPython Demo: log window",
91 (0,0), (500, 150))
92 logParent.Show(true)
93
94
95
96 # Prevent TreeCtrl from displaying all items after destruction
97 self.dying = false
98
99 # Make a File menu
100 self.mainmenu = wxMenuBar()
101 menu = wxMenu()
102 exitID = wxNewId()
103 menu.Append(exitID, 'E&xit\tAlt-X', 'Get the heck outta here!')
104 EVT_MENU(self, exitID, self.OnFileExit)
105 self.mainmenu.Append(menu, '&File')
106
107 # Make a Demo menu
108 menu = wxMenu()
109 for item in _treeList:
110 submenu = wxMenu()
111 for childItem in item[1]:
112 mID = wxNewId()
113 submenu.Append(mID, childItem)
114 EVT_MENU(self, mID, self.OnDemoMenu)
115 menu.AppendMenu(wxNewId(), item[0], submenu)
116 self.mainmenu.Append(menu, '&Demo')
117
118
119 # Make a Help menu
120 helpID = wxNewId()
121 menu = wxMenu()
122 menu.Append(helpID, '&About\tCtrl-H', 'wxPython RULES!!!')
123 EVT_MENU(self, helpID, self.OnHelpAbout)
124 self.mainmenu.Append(menu, '&Help')
125 self.SetMenuBar(self.mainmenu)
126
127 # set the menu accellerator table...
128 aTable = wxAcceleratorTable([(wxACCEL_ALT, ord('X'), exitID),
129 (wxACCEL_CTRL, ord('H'), helpID)])
130 self.SetAcceleratorTable(aTable)
131
132
133 # Create a TreeCtrl
134 if _useSplitter:
135 tID = wxNewId()
136 self.treeMap = {}
137 self.tree = wxTreeCtrl(splitter, tID)
138 #self.tree.SetBackgroundColour(wxNamedColour("Pink"))
139 root = self.tree.AddRoot("Overview")
140 firstChild = None
141 for item in _treeList:
142 child = self.tree.AppendItem(root, item[0])
143 if not firstChild: firstChild = child
144 for childItem in item[1]:
145 theDemo = self.tree.AppendItem(child, childItem)
146 self.treeMap[childItem] = theDemo
147
148 self.tree.Expand(root)
149 self.tree.Expand(firstChild)
150 EVT_TREE_ITEM_EXPANDED (self.tree, tID, self.OnItemExpanded)
151 EVT_TREE_ITEM_COLLAPSED (self.tree, tID, self.OnItemCollapsed)
152 EVT_TREE_SEL_CHANGED (self.tree, tID, self.OnSelChanged)
153
154 # Create a Notebook
155 self.nb = wxNotebook(nbParent, -1)
156
157 # Set up a TextCtrl on the Overview Notebook page
158 self.ovr = wxTextCtrl(self.nb, -1, style = wxTE_MULTILINE|wxTE_READONLY)
159 self.nb.AddPage(self.ovr, "Overview")
160
161
162 # Set up a TextCtrl on the Demo Code Notebook page
163 self.txt = wxTextCtrl(self.nb, -1,
164 style = wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
165 self.txt.SetFont(wxFont(9, wxMODERN, wxNORMAL, wxNORMAL, false))
166 self.nb.AddPage(self.txt, "Demo Code")
167
168
169 # Set up a log on the View Log Notebook page
170 self.log = wxTextCtrl(logParent, -1,
171 style = wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
172 (w, self.charHeight) = self.log.GetTextExtent('X')
173 self.WriteText('wxPython Demo Log:\n')
174
175 self.Show(true)
176
177 # add the windows to the splitter and split it.
178 if _useSplitter:
179 if _useNestedSplitter:
180 splitter2.SplitHorizontally(self.nb, self.log)
181 splitter2.SetSashPosition(360, true)
182 splitter2.SetMinimumPaneSize(20)
183
184 splitter.SplitVertically(self.tree, splitter2)
185 else:
186 splitter.SplitVertically(self.tree, self.nb)
187
188 splitter.SetSashPosition(180, true)
189 splitter.SetMinimumPaneSize(20)
190
191
192 # make our log window be stdout
193 #sys.stdout = self
194
195 # select initial items
196 self.nb.SetSelection(0)
197 if _useSplitter:
198 self.tree.SelectItem(root)
199
200 if len(sys.argv) == 2:
201 try:
202 selectedDemo = self.treeMap[sys.argv[1]]
203 except:
204 selectedDemo = None
205 if selectedDemo and _useSplitter:
206 self.tree.SelectItem(selectedDemo)
207 self.tree.EnsureVisible(selectedDemo)
208
209
210 self.WriteText('window handle: %s\n' % self.GetHandle())
211
212
213 #---------------------------------------------
214 def WriteText(self, text):
215 self.log.WriteText(text)
216 w, h = self.log.GetClientSizeTuple()
217 numLines = h/self.charHeight
218 x, y = self.log.PositionToXY(self.log.GetLastPosition())
219 if y > numLines:
220 self.log.ShowPosition(self.log.XYToPosition(x, y-numLines))
221 ##self.log.ShowPosition(self.log.GetLastPosition())
222 self.log.SetInsertionPointEnd()
223
224 def write(self, txt):
225 self.WriteText(txt)
226
227 #---------------------------------------------
228 def OnItemExpanded(self, event):
229 item = event.GetItem()
230 self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item))
231
232 #---------------------------------------------
233 def OnItemCollapsed(self, event):
234 item = event.GetItem()
235 self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item))
236
237 #---------------------------------------------
238 def OnSelChanged(self, event):
239 if self.dying:
240 return
241
242 item = event.GetItem()
243 itemText = self.tree.GetItemText(item)
244 self.RunDemo(itemText)
245
246
247 #---------------------------------------------
248 def RunDemo(self, itemText):
249 os.chdir(self.cwd)
250 if self.nb.GetPageCount() == 3:
251 if self.nb.GetSelection() == 2:
252 self.nb.SetSelection(0)
253 self.nb.DeletePage(2)
254
255 if itemText == 'Overview':
256 self.GetDemoFile('Main.py')
257 self.SetOverview('Overview', overview)
258 self.nb.Refresh();
259 self.window = None
260
261 else:
262 if os.path.exists(itemText + '.py'):
263 wxBeginBusyCursor()
264 self.GetDemoFile(itemText + '.py')
265 module = __import__(itemText, globals())
266 self.SetOverview(itemText, module.overview)
267 wxEndBusyCursor()
268
269 # in case runTest is modal, make sure things look right...
270 self.nb.Refresh();
271 wxYield()
272
273 self.window = module.runTest(self, self.nb, self)
274 if self.window:
275 self.nb.AddPage(self.window, 'Demo')
276 wxYield()
277 self.nb.SetSelection(2)
278
279 else:
280 self.ovr.Clear()
281 self.txt.Clear()
282 self.window = None
283
284
285
286 #---------------------------------------------
287 # Get the Demo files
288 def GetDemoFile(self, filename):
289 self.txt.Clear()
290 #if not self.txt.LoadFile(filename):
291 # self.txt.WriteText("Cannot open %s file." % filename)
292 try:
293 self.txt.SetValue(open(filename).read())
294 except IOError:
295 self.txt.WriteText("Cannot open %s file." % filename)
296
297
298 self.txt.SetInsertionPoint(0)
299 self.txt.ShowPosition(0)
300
301 #---------------------------------------------
302 def SetOverview(self, name, text):
303 self.ovr.Clear()
304 self.ovr.WriteText(text)
305 self.nb.SetPageText(0, name)
306 self.ovr.SetInsertionPoint(0)
307 self.ovr.ShowPosition(0)
308
309 #---------------------------------------------
310 # Menu methods
311 def OnFileExit(self, event):
312 self.Close()
313
314
315 def OnHelpAbout(self, event):
316 #about = wxMessageDialog(self,
317 # "wxPython is a Python extension module that\n"
318 # "encapsulates the wxWindows GUI classes.\n\n"
319 # "This demo shows off some of the capabilities\n"
320 # "of wxPython.\n\n"
321 # " Developed by Robin Dunn",
322 # "About wxPython", wxOK)
323 from About import MyAboutBox
324 about = MyAboutBox(self)
325 about.ShowModal()
326 about.Destroy()
327
328
329 #---------------------------------------------
330 def OnCloseWindow(self, event):
331 self.dying = true
332 self.window = None
333 self.mainmenu = None
334 self.Destroy()
335
336 #---------------------------------------------
337 def OnIdle(self, event):
338 if self.otherWin:
339 self.otherWin.Raise()
340 self.window = self.otherWin
341 self.otherWin = None
342
343 #---------------------------------------------
344 def OnDemoMenu(self, event):
345 if _useSplitter:
346 try:
347 selectedDemo = self.treeMap[self.mainmenu.GetLabel(event.GetId())]
348 except:
349 selectedDemo = None
350 if selectedDemo:
351 self.tree.SelectItem(selectedDemo)
352 self.tree.EnsureVisible(selectedDemo)
353 else:
354 self.RunDemo(self.mainmenu.GetLabel(event.GetId()))
355
356 #---------------------------------------------------------------------------
357 #---------------------------------------------------------------------------
358
359 class MyApp(wxApp):
360 def OnInit(self):
361 wxImage_AddHandler(wxJPEGHandler())
362 wxImage_AddHandler(wxPNGHandler())
363 wxImage_AddHandler(wxGIFHandler())
364
365 self.splash = SplashScreen(None, bitmapfile='bitmaps/splash.gif',
366 duration=4000, callback=self.AfterSplash)
367 self.splash.Show(true)
368 wxYield()
369 return true
370
371 def AfterSplash(self):
372 self.splash.Close(true)
373 frame = wxPythonDemo(None, -1, "wxPython: (A Demonstration)")
374 frame.Show(true)
375 self.SetTopWindow(frame)
376 return true
377
378 #---------------------------------------------------------------------------
379
380 def main():
381 try:
382 demoPath = os.path.split(__file__)[0]
383 os.chdir(demoPath)
384 except:
385 pass
386 app = MyApp(0)
387 app.MainLoop()
388
389
390 #---------------------------------------------------------------------------
391
392
393
394 overview = """\
395 Python
396 ------------
397
398 Python is an interpreted, interactive, object-oriented programming language often compared to Tcl, Perl, Scheme, or Java.
399
400 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.
401
402 wxWindows
403 --------------------
404
405 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.
406
407 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.
408
409 wxPython
410 ----------------
411
412 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.
413
414 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.
415
416 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.
417
418 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.
419 """
420
421
422
423
424
425
426
427 #----------------------------------------------------------------------------
428 #----------------------------------------------------------------------------
429
430 if __name__ == '__main__':
431 main()
432
433 #----------------------------------------------------------------------------
434
435
436
437
438
439
440