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