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