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