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