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