]> git.saurik.com Git - wxWidgets.git/blame - utils/wxPython/demo/Main.py
ifdef'd out wxEmptyString. This is defined in string.cpp.
[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
2f90df85 39 ('Window Layout', ['wxLayoutConstraints', 'Sizers', 'OldSizers']),
cf694132 40
2f90df85
RD
41 ('Miscellaneous', ['wxTimer', 'wxValidator', 'wxGLCanvas', 'DialogUnits',
42 'wxImage', 'PrintFramework', 'wxOGL']),
cf694132 43
2f90df85 44 ('wxPython Library', ['OldSizers', '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):
2f90df85
RD
55 wxFrame.__init__(self, parent, -1, title, size = (725, 550))
56
cf694132
RD
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()
2f90df85
RD
75 exitID = wxNewId()
76 menu.Append(exitID, 'E&xit\tCtrl-X', 'Get the heck outta here!')
77 EVT_MENU(self, exitID, self.OnFileExit)
cf694132
RD
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
2f90df85 93 helpID = wxNewId()
cf694132 94 menu = wxMenu()
2f90df85
RD
95 menu.Append(helpID, '&About\tCtrl-H', 'wxPython RULES!!!')
96 EVT_MENU(self, helpID, self.OnHelpAbout)
cf694132
RD
97 self.mainmenu.Append(menu, '&Help')
98 self.SetMenuBar(self.mainmenu)
99
2f90df85
RD
100 # set the menu accellerator table...
101 aTable = wxAcceleratorTable([(wxACCEL_CTRL, ord('X'), exitID),
102 (wxACCEL_CTRL, ord('H'), helpID)])
103 self.SetAcceleratorTable(aTable)
104
bb0054cd 105
cf694132 106 # Create a TreeCtrl
ec3e670f
RD
107 tID = wxNewId()
108 self.treeMap = {}
cf694132
RD
109 self.tree = wxTreeCtrl(splitter, tID)
110 root = self.tree.AddRoot("Overview")
111 for item in _treeList:
112 child = self.tree.AppendItem(root, item[0])
113 for childItem in item[1]:
bb0054cd 114 theDemo = self.tree.AppendItem(child, childItem)
ec3e670f 115 self.treeMap[childItem] = theDemo
bb0054cd 116
cf694132
RD
117 self.tree.Expand(root)
118 EVT_TREE_ITEM_EXPANDED (self.tree, tID, self.OnItemExpanded)
119 EVT_TREE_ITEM_COLLAPSED (self.tree, tID, self.OnItemCollapsed)
120 EVT_TREE_SEL_CHANGED (self.tree, tID, self.OnSelChanged)
2f90df85 121 ###EVT_TREE_SEL_CHANGING (self.tree, tID, self.OnSelChanging)
cf694132 122
cf694132
RD
123 # Create a Notebook
124 self.nb = wxNotebook(splitter2, -1)
125
126 # Set up a TextCtrl on the Overview Notebook page
efc5f224 127 self.ovr = wxTextCtrl(self.nb, -1, style = wxTE_MULTILINE|wxTE_READONLY)
cf694132
RD
128 self.nb.AddPage(self.ovr, "Overview")
129
130
131 # Set up a TextCtrl on the Demo Code Notebook page
efc5f224
RD
132 self.txt = wxTextCtrl(self.nb, -1,
133 style = wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
bb0054cd 134 self.txt.SetFont(wxFont(9, wxMODERN, wxNORMAL, wxNORMAL, false))
cf694132
RD
135 self.nb.AddPage(self.txt, "Demo Code")
136
137
cf694132 138 # Set up a log on the View Log Notebook page
efc5f224
RD
139 self.log = wxTextCtrl(splitter2, -1,
140 style = wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
cf694132 141 (w, self.charHeight) = self.log.GetTextExtent('X')
e166644c 142 self.WriteText('wxPython Demo Log:\n')
cf694132
RD
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
c127177f 155 #sys.stdout = self
cf694132 156
bb0054cd
RD
157 # select initial items
158 self.nb.SetSelection(0)
159 self.tree.SelectItem(root)
ec3e670f
RD
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
bb0054cd 170
cf694132
RD
171 #---------------------------------------------
172 def WriteText(self, text):
173 self.log.WriteText(text)
53920141
RD
174 w, h = self.log.GetClientSizeTuple()
175 numLines = h/self.charHeight
176 x, y = self.log.PositionToXY(self.log.GetLastPosition())
e166644c
RD
177 if y > numLines:
178 self.log.ShowPosition(self.log.XYToPosition(x, y-numLines))
179 ##self.log.ShowPosition(self.log.GetLastPosition())
64be6958 180 self.log.SetInsertionPointEnd()
cf694132
RD
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
2f90df85 199 ###print 'OnSelChanged entry'
cf694132
RD
200
201 if self.nb.GetPageCount() == 3:
202 if self.nb.GetSelection() == 2:
203 self.nb.SetSelection(0)
204 self.nb.DeletePage(2)
205
206 item = event.GetItem()
207 itemText = self.tree.GetItemText(item)
208
209 if itemText == 'Overview':
210 self.GetDemoFile('Main.py')
211 self.SetOverview('Overview', overview)
212 #self.nb.ResizeChildren();
213 self.nb.Refresh();
214 #wxYield()
e91a9dfc 215 self.window = None
cf694132
RD
216
217 else:
218 if os.path.exists(itemText + '.py'):
219 self.GetDemoFile(itemText + '.py')
220 module = __import__(itemText, globals())
221 self.SetOverview(itemText, module.overview)
222
223 # in case runTest is modal, make sure things look right...
224 self.nb.Refresh();
225 wxYield()
226
e91a9dfc
RD
227 self.window = module.runTest(self, self.nb, self)
228 if self.window:
229 self.nb.AddPage(self.window, 'Demo')
cf694132
RD
230 self.nb.SetSelection(2)
231 self.nb.ResizeChildren();
232
233 else:
234 self.ovr.Clear()
235 self.txt.Clear()
e91a9dfc 236 self.window = None
cf694132 237
2f90df85
RD
238 ###print 'OnSelChanged exit: ', itemText
239
240 ###def OnSelChanging(self, event):
241 ### print 'OnSelChanging'
242
cf694132
RD
243
244 #---------------------------------------------
245 # Get the Demo files
246 def GetDemoFile(self, filename):
247 self.txt.Clear()
bb0054cd
RD
248 #if not self.txt.LoadFile(filename):
249 # self.txt.WriteText("Cannot open %s file." % filename)
250 try:
251 self.txt.SetValue(open(filename).read())
8bf5d46e 252 except IOError:
cf694132
RD
253 self.txt.WriteText("Cannot open %s file." % filename)
254
bb0054cd 255
cf694132
RD
256 self.txt.SetInsertionPoint(0)
257 self.txt.ShowPosition(0)
258
259 #---------------------------------------------
260 def SetOverview(self, name, text):
261 self.ovr.Clear()
262 self.ovr.WriteText(text)
263 self.nb.SetPageText(0, name)
264 self.ovr.SetInsertionPoint(0)
265 self.ovr.ShowPosition(0)
266
267 #---------------------------------------------
268 # Menu methods
269 def OnFileExit(self, event):
270 self.Close()
271
272
273 def OnHelpAbout(self, event):
ec3e670f
RD
274 #about = wxMessageDialog(self,
275 # "wxPython is a Python extension module that\n"
276 # "encapsulates the wxWindows GUI classes.\n\n"
277 # "This demo shows off some of the capabilities\n"
278 # "of wxPython.\n\n"
279 # " Developed by Robin Dunn",
280 # "About wxPython", wxOK)
e166644c 281 from About import MyAboutBox
ec3e670f 282 about = MyAboutBox(self)
cf694132
RD
283 about.ShowModal()
284 about.Destroy()
285
286
287 #---------------------------------------------
288 def OnCloseWindow(self, event):
289 self.dying = true
e91a9dfc 290 self.window = None
26197023 291 self.mainmenu = None
cf694132
RD
292 self.Destroy()
293
294 #---------------------------------------------
295 def OnIdle(self, event):
296 if self.otherWin:
297 self.otherWin.Raise()
e91a9dfc 298 self.window = self.otherWin
cf694132
RD
299 self.otherWin = None
300
ec3e670f
RD
301 #---------------------------------------------
302 def OnDemoMenu(self, event):
ec3e670f
RD
303 try:
304 selectedDemo = self.treeMap[self.mainmenu.GetLabel(event.GetId())]
305 except:
306 selectedDemo = None
307 if selectedDemo:
2f90df85 308 ###print "---- start ----"
ec3e670f
RD
309 self.tree.SelectItem(selectedDemo)
310 self.tree.EnsureVisible(selectedDemo)
2f90df85 311 ###print "---- end ----"
ec3e670f
RD
312
313
cf694132
RD
314#---------------------------------------------------------------------------
315#---------------------------------------------------------------------------
316
317class MyApp(wxApp):
318 def OnInit(self):
319 wxImage_AddHandler(wxJPEGHandler())
320 wxImage_AddHandler(wxPNGHandler())
321 wxImage_AddHandler(wxGIFHandler())
322 frame = wxPythonDemo(NULL, -1, "wxPython: (A Demonstration)")
323 frame.Show(true)
324 self.SetTopWindow(frame)
325 return true
326
327#---------------------------------------------------------------------------
328
329def main():
330 app = MyApp(0)
331 app.MainLoop()
332
333
334#---------------------------------------------------------------------------
335
336
337
338overview = """\
339Python
340------------
341
342Python is an interpreted, interactive, object-oriented programming language often compared to Tcl, Perl, Scheme, or Java.
343
344Python 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.
345
346wxWindows
347--------------------
348
349wxWindows 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.
350
351wxWindows 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.
352
353wxPython
354----------------
355
356wxPython 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.
357
358The 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.
359
360There 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.
361
362wxPython 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.
363"""
364
365
366
367
368
369
370
371#----------------------------------------------------------------------------
372#----------------------------------------------------------------------------
373
374if __name__ == '__main__':
375 main()
376
377#----------------------------------------------------------------------------
378
379
380
381
382
383
384