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