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