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