]> git.saurik.com Git - wxWidgets.git/blame - utils/wxPython/demo/Main.py
Makefile for mingw/gcc-2.95
[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 *
16
17
18#---------------------------------------------------------------------------
19
20
21_treeList = [
22 ('Managed Windows', ['wxFrame', 'wxDialog', 'wxMiniFrame']),
23
24 ('Miscellaneous Windows', ['wxGrid', 'wxSashWindow',
25 'wxScrolledWindow', 'wxSplitterWindow',
ec3e670f
RD
26 'wxStatusBar', 'wxToolBar', 'wxNotebook',
27 'wxHtmlWindow']),
cf694132
RD
28
29 ('Common Dialogs', ['wxColourDialog', 'wxDirDialog', 'wxFileDialog',
30 'wxSingleChoiceDialog', 'wxTextEntryDialog',
31 'wxFontDialog', 'wxPageSetupDialog', 'wxPrintDialog',
bb0054cd 32 'wxMessageDialog', 'wxProgressDialog']),
cf694132
RD
33
34 ('Controls', ['wxButton', 'wxCheckBox', 'wxCheckListBox', 'wxChoice',
35 'wxComboBox', 'wxGauge', 'wxListBox', 'wxListCtrl', 'wxTextCtrl',
36 'wxTreeCtrl', 'wxSpinButton', 'wxStaticText', 'wxStaticBitmap',
37 'wxRadioBox', 'wxSlider']),
38
bb0054cd 39 ('Window Layout', ['wxLayoutConstraints', 'Sizers']),
cf694132 40
bb0054cd 41 ('Miscellaneous', ['wxTimer', 'wxGLCanvas', 'DialogUnits', 'wxImage',
e91a9dfc 42 'PrintFramework', 'wxOGL']),
cf694132 43
bb0054cd 44 ('wxPython Library', ['Sizers', 'Layoutf', 'wxScrolledMessageDialog',
cf694132
RD
45 'wxMultipleChoiceDialog', 'wxPlotCanvas']),
46
8bf5d46e 47 ('Cool Contribs', ['pyTree', 'hangman', 'SlashDot', 'XMLtreeview']),
cf694132
RD
48
49 ]
50
51#---------------------------------------------------------------------------
52
53class 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()
ec3e670f 75 mID = wxNewId()
cf694132
RD
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
ec3e670f
RD
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
cf694132 92 # Make a Help menu
ec3e670f 93 mID = wxNewId()
cf694132
RD
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
bb0054cd 100
cf694132 101 # Create a TreeCtrl
ec3e670f
RD
102 tID = wxNewId()
103 self.treeMap = {}
cf694132
RD
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]:
bb0054cd 109 theDemo = self.tree.AppendItem(child, childItem)
ec3e670f 110 self.treeMap[childItem] = theDemo
bb0054cd 111
cf694132
RD
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
cf694132
RD
117 # Create a Notebook
118 self.nb = wxNotebook(splitter2, -1)
119
120 # Set up a TextCtrl on the Overview Notebook page
efc5f224 121 self.ovr = wxTextCtrl(self.nb, -1, style = wxTE_MULTILINE|wxTE_READONLY)
cf694132
RD
122 self.nb.AddPage(self.ovr, "Overview")
123
124
125 # Set up a TextCtrl on the Demo Code Notebook page
efc5f224
RD
126 self.txt = wxTextCtrl(self.nb, -1,
127 style = wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
bb0054cd 128 self.txt.SetFont(wxFont(9, wxMODERN, wxNORMAL, wxNORMAL, false))
cf694132
RD
129 self.nb.AddPage(self.txt, "Demo Code")
130
131
cf694132 132 # Set up a log on the View Log Notebook page
efc5f224
RD
133 self.log = wxTextCtrl(splitter2, -1,
134 style = wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
cf694132 135 (w, self.charHeight) = self.log.GetTextExtent('X')
53920141 136 #self.WriteText('wxPython Demo Log:\n')
cf694132
RD
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
c127177f 149 #sys.stdout = self
cf694132 150
bb0054cd
RD
151 # select initial items
152 self.nb.SetSelection(0)
153 self.tree.SelectItem(root)
ec3e670f
RD
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
bb0054cd 164
cf694132
RD
165 #---------------------------------------------
166 def WriteText(self, text):
167 self.log.WriteText(text)
53920141
RD
168 w, h = self.log.GetClientSizeTuple()
169 numLines = h/self.charHeight
170 x, y = self.log.PositionToXY(self.log.GetLastPosition())
8bf5d46e 171 self.log.ShowPosition(self.log.XYToPosition(x, y-numLines))
efc5f224 172 ##self.log.ShowPosition(self.log.GetLastPosition())
64be6958 173 self.log.SetInsertionPointEnd()
cf694132
RD
174
175 def write(self, txt):
176 self.WriteText(txt)
177
178 #---------------------------------------------
179 def OnItemExpanded(self, event):
180 item = event.GetItem()
181 self.log.WriteText("OnItemExpanded: %s\n" % self.tree.GetItemText(item))
182
183 #---------------------------------------------
184 def OnItemCollapsed(self, event):
185 item = event.GetItem()
186 self.log.WriteText("OnItemCollapsed: %s\n" % self.tree.GetItemText(item))
187
188 #---------------------------------------------
189 def OnSelChanged(self, event):
190 if self.dying:
191 return
192
193 if self.nb.GetPageCount() == 3:
194 if self.nb.GetSelection() == 2:
195 self.nb.SetSelection(0)
196 self.nb.DeletePage(2)
197
198 item = event.GetItem()
199 itemText = self.tree.GetItemText(item)
200
201 if itemText == 'Overview':
202 self.GetDemoFile('Main.py')
203 self.SetOverview('Overview', overview)
204 #self.nb.ResizeChildren();
205 self.nb.Refresh();
206 #wxYield()
e91a9dfc 207 self.window = None
cf694132
RD
208
209 else:
210 if os.path.exists(itemText + '.py'):
211 self.GetDemoFile(itemText + '.py')
212 module = __import__(itemText, globals())
213 self.SetOverview(itemText, module.overview)
214
215 # in case runTest is modal, make sure things look right...
216 self.nb.Refresh();
217 wxYield()
218
e91a9dfc
RD
219 self.window = module.runTest(self, self.nb, self)
220 if self.window:
221 self.nb.AddPage(self.window, 'Demo')
cf694132
RD
222 self.nb.SetSelection(2)
223 self.nb.ResizeChildren();
224
225 else:
226 self.ovr.Clear()
227 self.txt.Clear()
e91a9dfc 228 self.window = None
cf694132
RD
229
230
231 #---------------------------------------------
232 # Get the Demo files
233 def GetDemoFile(self, filename):
234 self.txt.Clear()
bb0054cd
RD
235 #if not self.txt.LoadFile(filename):
236 # self.txt.WriteText("Cannot open %s file." % filename)
237 try:
238 self.txt.SetValue(open(filename).read())
8bf5d46e 239 except IOError:
cf694132
RD
240 self.txt.WriteText("Cannot open %s file." % filename)
241
bb0054cd 242
cf694132
RD
243 self.txt.SetInsertionPoint(0)
244 self.txt.ShowPosition(0)
245
246 #---------------------------------------------
247 def SetOverview(self, name, text):
248 self.ovr.Clear()
249 self.ovr.WriteText(text)
250 self.nb.SetPageText(0, name)
251 self.ovr.SetInsertionPoint(0)
252 self.ovr.ShowPosition(0)
253
254 #---------------------------------------------
255 # Menu methods
256 def OnFileExit(self, event):
257 self.Close()
258
259
260 def OnHelpAbout(self, event):
ec3e670f
RD
261 #about = wxMessageDialog(self,
262 # "wxPython is a Python extension module that\n"
263 # "encapsulates the wxWindows GUI classes.\n\n"
264 # "This demo shows off some of the capabilities\n"
265 # "of wxPython.\n\n"
266 # " Developed by Robin Dunn",
267 # "About wxPython", wxOK)
268 about = MyAboutBox(self)
cf694132
RD
269 about.ShowModal()
270 about.Destroy()
271
272
273 #---------------------------------------------
274 def OnCloseWindow(self, event):
275 self.dying = true
e91a9dfc 276 self.window = None
cf694132
RD
277 self.Destroy()
278
279 #---------------------------------------------
280 def OnIdle(self, event):
281 if self.otherWin:
282 self.otherWin.Raise()
e91a9dfc 283 self.window = self.otherWin
cf694132
RD
284 self.otherWin = None
285
ec3e670f
RD
286 #---------------------------------------------
287 def OnDemoMenu(self, event):
288 print event.GetId(), self.mainmenu.GetLabel(event.GetId())
289 try:
290 selectedDemo = self.treeMap[self.mainmenu.GetLabel(event.GetId())]
291 except:
292 selectedDemo = None
293 if selectedDemo:
294 self.tree.SelectItem(selectedDemo)
295 self.tree.EnsureVisible(selectedDemo)
296
297
298
299#---------------------------------------------------------------------------
300#---------------------------------------------------------------------------
301
302class MyAboutBox(wxDialog):
303 text = '''
304<html>
305<body bgcolor="#AC76DE">
306<center><table bgcolor="#458154" width="100%%" cellspacing="0" cellpadding="0" border="1">
307<tr>
308 <td align="center"><h1>wxPython %s</h1></td>
309</tr>
310</table>
311
312<p><b>wxPython</b> is a Python extension module that
313encapsulates the wxWindows GUI classes.</p>
314
315<p>This demo shows off some of the capabilities
316of <b>wxPython</b>. Select items from the menu or tree control,
317sit back and enjoy. Be sure to take a peek at the source code for each
318demo item so you can learn how to use the classes yourself.</p>
319
320<p><b>wxPython</b> is brought to you by <b>Robin Dunn</b> and<br>
321<b>Total Control Software</b>, copyright 1999.</p>
322
323<p><font size="-1">Please see <i>license.txt</i> for licensing information.</font></p>
324</center>
325</body>
326</html>
327'''
328 def __init__(self, parent):
329 from wxPython.html import *
330 wxDialog.__init__(self, parent, -1, 'About wxPython')
331 self.html = wxHtmlWindow(self, -1, wxPoint(5,5), wxSize(400, 350))
332 self.html.SetPage(self.text % wx.__version__)
333 wxButton(self, wxID_OK, 'OK', wxPoint(5, 365)).SetDefault()
334 self.Fit()
335
cf694132
RD
336
337#---------------------------------------------------------------------------
338#---------------------------------------------------------------------------
339
340class MyApp(wxApp):
341 def OnInit(self):
342 wxImage_AddHandler(wxJPEGHandler())
343 wxImage_AddHandler(wxPNGHandler())
344 wxImage_AddHandler(wxGIFHandler())
345 frame = wxPythonDemo(NULL, -1, "wxPython: (A Demonstration)")
346 frame.Show(true)
347 self.SetTopWindow(frame)
348 return true
349
350#---------------------------------------------------------------------------
351
352def main():
353 app = MyApp(0)
354 app.MainLoop()
355
356
357#---------------------------------------------------------------------------
358
359
360
361overview = """\
362Python
363------------
364
365Python is an interpreted, interactive, object-oriented programming language often compared to Tcl, Perl, Scheme, or Java.
366
367Python 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.
368
369wxWindows
370--------------------
371
372wxWindows 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.
373
374wxWindows 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.
375
376wxPython
377----------------
378
379wxPython 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.
380
381The 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.
382
383There 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.
384
385wxPython 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.
386"""
387
388
389
390
391
392
393
394#----------------------------------------------------------------------------
395#----------------------------------------------------------------------------
396
397if __name__ == '__main__':
398 main()
399
400#----------------------------------------------------------------------------
401
402
403
404
405
406
407