]> git.saurik.com Git - wxWidgets.git/blame - utils/wxPython/demo/Main.py
Created and wrapped wxPyControlPoint
[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')
e166644c 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())
e166644c
RD
171 if y > numLines:
172 self.log.ShowPosition(self.log.XYToPosition(x, y-numLines))
173 ##self.log.ShowPosition(self.log.GetLastPosition())
64be6958 174 self.log.SetInsertionPointEnd()
cf694132
RD
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()
e91a9dfc 208 self.window = None
cf694132
RD
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
e91a9dfc
RD
220 self.window = module.runTest(self, self.nb, self)
221 if self.window:
222 self.nb.AddPage(self.window, 'Demo')
cf694132
RD
223 self.nb.SetSelection(2)
224 self.nb.ResizeChildren();
225
226 else:
227 self.ovr.Clear()
228 self.txt.Clear()
e91a9dfc 229 self.window = None
cf694132
RD
230
231
232 #---------------------------------------------
233 # Get the Demo files
234 def GetDemoFile(self, filename):
235 self.txt.Clear()
bb0054cd
RD
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())
8bf5d46e 240 except IOError:
cf694132
RD
241 self.txt.WriteText("Cannot open %s file." % filename)
242
bb0054cd 243
cf694132
RD
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):
ec3e670f
RD
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)
e166644c 269 from About import MyAboutBox
ec3e670f 270 about = MyAboutBox(self)
cf694132
RD
271 about.ShowModal()
272 about.Destroy()
273
274
275 #---------------------------------------------
276 def OnCloseWindow(self, event):
277 self.dying = true
e91a9dfc 278 self.window = None
26197023 279 self.mainmenu = None
cf694132
RD
280 self.Destroy()
281
282 #---------------------------------------------
283 def OnIdle(self, event):
284 if self.otherWin:
285 self.otherWin.Raise()
e91a9dfc 286 self.window = self.otherWin
cf694132
RD
287 self.otherWin = None
288
ec3e670f
RD
289 #---------------------------------------------
290 def OnDemoMenu(self, event):
291 print event.GetId(), self.mainmenu.GetLabel(event.GetId())
292 try:
293 selectedDemo = self.treeMap[self.mainmenu.GetLabel(event.GetId())]
294 except:
295 selectedDemo = None
296 if selectedDemo:
297 self.tree.SelectItem(selectedDemo)
298 self.tree.EnsureVisible(selectedDemo)
299
300
301
cf694132
RD
302#---------------------------------------------------------------------------
303#---------------------------------------------------------------------------
304
305class MyApp(wxApp):
306 def OnInit(self):
307 wxImage_AddHandler(wxJPEGHandler())
308 wxImage_AddHandler(wxPNGHandler())
309 wxImage_AddHandler(wxGIFHandler())
310 frame = wxPythonDemo(NULL, -1, "wxPython: (A Demonstration)")
311 frame.Show(true)
312 self.SetTopWindow(frame)
313 return true
314
315#---------------------------------------------------------------------------
316
317def main():
318 app = MyApp(0)
319 app.MainLoop()
320
321
322#---------------------------------------------------------------------------
323
324
325
326overview = """\
327Python
328------------
329
330Python is an interpreted, interactive, object-oriented programming language often compared to Tcl, Perl, Scheme, or Java.
331
332Python 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.
333
334wxWindows
335--------------------
336
337wxWindows 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.
338
339wxWindows 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.
340
341wxPython
342----------------
343
344wxPython 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.
345
346The 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.
347
348There 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.
349
350wxPython 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.
351"""
352
353
354
355
356
357
358
359#----------------------------------------------------------------------------
360#----------------------------------------------------------------------------
361
362if __name__ == '__main__':
363 main()
364
365#----------------------------------------------------------------------------
366
367
368
369
370
371
372