]>
Commit | Line | Data |
---|---|---|
1 | #!/bin/env python | |
2 | #---------------------------------------------------------------------------- | |
3 | # Name: Main.py | |
4 | # Purpose: Testing lots of stuff, controls, window types, etc. | |
5 | # | |
6 | # Author: Robin Dunn | |
7 | # | |
8 | # Created: A long time ago, in a galaxy far, far away... | |
9 | # RCS-ID: $Id$ | |
10 | # Copyright: (c) 1999 by Total Control Software | |
11 | # Licence: wxWindows license | |
12 | #---------------------------------------------------------------------------- | |
13 | ||
14 | import sys, os, time | |
15 | ||
16 | import wx # This module uses the new wx namespace | |
17 | import wx.html | |
18 | ||
19 | import images | |
20 | ||
21 | ||
22 | #--------------------------------------------------------------------------- | |
23 | ||
24 | ||
25 | _treeList = [ | |
26 | # new stuff | |
27 | ('Recent Additions', [ | |
28 | 'wxScrolledPanel', | |
29 | 'ShapedWindow', | |
30 | 'NewNamespace', | |
31 | 'PopupMenu', | |
32 | 'AnalogClockWindow', | |
33 | 'MaskedEditControls', | |
34 | 'wxTreeListCtrl', | |
35 | 'wxGrid_MegaExample', | |
36 | ]), | |
37 | ||
38 | # managed windows == things with a (optional) caption you can close | |
39 | ('Base Frames and Dialogs', [ | |
40 | 'wxDialog', | |
41 | 'wxFrame', | |
42 | 'wxMDIWindows', | |
43 | 'wxMiniFrame', | |
44 | 'wxWizard', | |
45 | ]), | |
46 | ||
47 | # the common dialogs | |
48 | ('Common Dialogs', [ | |
49 | 'wxColourDialog', | |
50 | 'wxDirDialog', | |
51 | 'wxFileDialog', | |
52 | 'wxFileDialog_Save', | |
53 | 'wxFindReplaceDialog', | |
54 | 'wxFontDialog', | |
55 | 'wxMessageDialog', | |
56 | 'wxPageSetupDialog', | |
57 | 'wxPrintDialog', | |
58 | 'wxProgressDialog', | |
59 | 'wxSingleChoiceDialog', | |
60 | 'wxTextEntryDialog', | |
61 | ]), | |
62 | ||
63 | # dialogs from libraries | |
64 | ('More Dialogs', [ | |
65 | 'ErrorDialogs', | |
66 | 'ImageBrowser', | |
67 | 'wxMultipleChoiceDialog', | |
68 | 'wxScrolledMessageDialog', | |
69 | ]), | |
70 | ||
71 | # core controls | |
72 | ('Core Windows/Controls', [ | |
73 | 'PopupMenu', | |
74 | 'wxButton', | |
75 | 'wxCheckBox', | |
76 | 'wxCheckListBox', | |
77 | 'wxChoice', | |
78 | 'wxComboBox', | |
79 | 'wxGauge', | |
80 | 'wxGenericDirCtrl', | |
81 | 'wxGrid', | |
82 | 'wxGrid_MegaExample', | |
83 | 'wxListBox', | |
84 | 'wxListCtrl', | |
85 | 'wxListCtrl_virtual', | |
86 | 'wxMenu', | |
87 | 'wxNotebook', | |
88 | 'wxPopupWindow', | |
89 | 'wxRadioBox', | |
90 | 'wxRadioButton', | |
91 | 'wxSashWindow', | |
92 | 'wxScrolledWindow', | |
93 | 'wxSlider', | |
94 | 'wxSpinButton', | |
95 | 'wxSpinCtrl', | |
96 | 'wxSplitterWindow', | |
97 | 'wxStaticBitmap', | |
98 | 'wxStaticText', | |
99 | 'wxStatusBar', | |
100 | 'wxTextCtrl', | |
101 | 'wxToggleButton', | |
102 | 'wxToolBar', | |
103 | 'wxTreeCtrl', | |
104 | 'wxValidator', | |
105 | ]), | |
106 | ||
107 | # controls coming from other librairies | |
108 | ('More Windows/Controls', [ | |
109 | #'wxFloatBar', deprecated | |
110 | #'wxMVCTree', deprecated | |
111 | #'wxRightTextCtrl', deprecated as we have wxTE_RIGHT now. | |
112 | 'AnalogClockWindow', | |
113 | 'ColourSelect', | |
114 | 'ContextHelp', | |
115 | 'FancyText', | |
116 | 'FileBrowseButton', | |
117 | 'GenericButtons', | |
118 | 'MaskedEditControls', | |
119 | 'PyShell', | |
120 | 'PyCrust', | |
121 | 'SplitTree', | |
122 | 'TablePrint', | |
123 | 'Throbber', | |
124 | 'wxCalendar', | |
125 | 'wxCalendarCtrl', | |
126 | 'wxPyColourChooser', | |
127 | 'wxDynamicSashWindow', | |
128 | 'wxEditableListBox', | |
129 | 'wxEditor', | |
130 | 'wxHtmlWindow', | |
131 | 'wxIEHtmlWin', | |
132 | 'wxIntCtrl', | |
133 | 'wxLEDNumberCtrl', | |
134 | 'wxMimeTypesManager', | |
135 | 'wxMultiSash', | |
136 | 'wxPopupControl', | |
137 | 'wxStyledTextCtrl_1', | |
138 | 'wxStyledTextCtrl_2', | |
139 | 'wxTimeCtrl', | |
140 | 'wxTreeListCtrl', | |
141 | ]), | |
142 | ||
143 | # How to lay out the controls in a frame/dialog | |
144 | ('Window Layout', [ | |
145 | 'LayoutAnchors', | |
146 | 'Layoutf', | |
147 | 'RowColSizer', | |
148 | 'Sizers', | |
149 | 'wxLayoutConstraints', | |
150 | 'wxScrolledPanel', | |
151 | 'wxXmlResource', | |
152 | 'wxXmlResourceHandler', | |
153 | ]), | |
154 | ||
155 | # ditto | |
156 | ('Process and Events', [ | |
157 | 'EventManager', | |
158 | 'infoframe', | |
159 | 'OOR', | |
160 | 'PythonEvents', | |
161 | 'Threads', | |
162 | 'wxKeyEvents', | |
163 | 'wxProcess', | |
164 | 'wxTimer', | |
165 | ]), | |
166 | ||
167 | # Clipboard and DnD | |
168 | ('Clipboard and DnD', [ | |
169 | 'CustomDragAndDrop', | |
170 | 'DragAndDrop', | |
171 | 'URLDragAndDrop', | |
172 | ]), | |
173 | ||
174 | # Images | |
175 | ('Using Images', [ | |
176 | 'Throbber', | |
177 | 'wxArtProvider', | |
178 | 'wxDragImage', | |
179 | 'wxImage', | |
180 | 'wxImageFromStream', | |
181 | 'wxMask', | |
182 | ]), | |
183 | ||
184 | # Other stuff | |
185 | ('Miscellaneous', [ | |
186 | 'ColourDB', | |
187 | 'DialogUnits', | |
188 | 'DrawXXXList', | |
189 | 'FontEnumerator', | |
190 | 'PrintFramework', | |
191 | 'Throbber', | |
192 | 'Unicode', | |
193 | 'wxFileHistory', | |
194 | 'wxJoystick', | |
195 | 'wxOGL', | |
196 | 'wxWave', | |
197 | ]), | |
198 | ||
199 | # need libs not coming with the demo | |
200 | ('Objects using an external library', [ | |
201 | 'ActiveXWrapper_Acrobat', | |
202 | 'ActiveXWrapper_IE', | |
203 | 'wxGLCanvas', | |
204 | 'wxPlotCanvas', | |
205 | ]), | |
206 | ||
207 | ||
208 | ('Check out the samples dir too', [ | |
209 | ]), | |
210 | ||
211 | ] | |
212 | ||
213 | ||
214 | ||
215 | #--------------------------------------------------------------------------- | |
216 | ||
217 | class MyLog(wx.PyLog): | |
218 | def __init__(self, textCtrl, logTime=0): | |
219 | wx.PyLog.__init__(self) | |
220 | self.tc = textCtrl | |
221 | self.logTime = logTime | |
222 | ||
223 | def DoLogString(self, message, timeStamp): | |
224 | if self.logTime: | |
225 | message = time.strftime("%X", time.localtime(timeStamp)) + \ | |
226 | ": " + message | |
227 | if self.tc: | |
228 | self.tc.AppendText(message + '\n') | |
229 | ||
230 | ||
231 | class MyTP(wx.PyTipProvider): | |
232 | def GetTip(self): | |
233 | return "This is my tip" | |
234 | ||
235 | #--------------------------------------------------------------------------- | |
236 | # A class to be used to display source code in the demo. Try using the | |
237 | # wxSTC in the wxStyledTextCtrl_2 sample first, fall back to wxTextCtrl | |
238 | # if there is an error, such as the stc module not being present. | |
239 | ||
240 | try: | |
241 | ##raise ImportError | |
242 | from wx import stc | |
243 | from wxStyledTextCtrl_2 import PythonSTC | |
244 | class DemoCodeViewer(PythonSTC): | |
245 | def __init__(self, parent, ID): | |
246 | PythonSTC.__init__(self, parent, ID) | |
247 | self.SetEdgeMode(stc.STC_EDGE_NONE) | |
248 | self.SetSelBackground(True, wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHT)) | |
249 | self.SetSelForeground(True, wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)) | |
250 | ||
251 | # Some methods to make it compatible with how the wxTextCtrl is used | |
252 | def SetValue(self, value): | |
253 | self.SetReadOnly(False) | |
254 | self.SetText(value) | |
255 | self.SetReadOnly(True) | |
256 | ||
257 | def Clear(self): | |
258 | self.ClearAll() | |
259 | ||
260 | def SetInsertionPoint(self, pos): | |
261 | self.SetCurrentPos(pos) | |
262 | ||
263 | def ShowPosition(self, pos): | |
264 | self.GotoPos(pos) | |
265 | ||
266 | def GetLastPosition(self): | |
267 | return self.GetLength() | |
268 | ||
269 | def GetRange(self, start, end): | |
270 | return self.GetTextRange(start, end) | |
271 | ||
272 | def GetSelection(self): | |
273 | return self.GetAnchor(), self.GetCurrentPos() | |
274 | ||
275 | def SetSelection(self, start, end): | |
276 | self.SetSelectionStart(start) | |
277 | self.SetSelectionEnd(end) | |
278 | ||
279 | ||
280 | except ImportError: | |
281 | class DemoCodeViewer(wx.TextCtrl): | |
282 | def __init__(self, parent, ID): | |
283 | wx.TextCtrl.__init__(self, parent, ID, style = | |
284 | wx.TE_MULTILINE | wx.TE_READONLY | | |
285 | wx.HSCROLL | wx.TE_RICH2 | wx.TE_NOHIDESEL) | |
286 | ||
287 | ||
288 | #--------------------------------------------------------------------------- | |
289 | ||
290 | def opj(path): | |
291 | """Convert paths to the platform-specific separator""" | |
292 | return apply(os.path.join, tuple(path.split('/'))) | |
293 | ||
294 | ||
295 | #--------------------------------------------------------------------------- | |
296 | ||
297 | class wxPythonDemo(wx.Frame): | |
298 | overviewText = "wxPython Overview" | |
299 | ||
300 | def __init__(self, parent, id, title): | |
301 | wx.Frame.__init__(self, parent, -1, title, size = (800, 600), | |
302 | style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE) | |
303 | ||
304 | self.cwd = os.getcwd() | |
305 | self.curOverview = "" | |
306 | self.window = None | |
307 | ||
308 | icon = images.getMondrianIcon() | |
309 | self.SetIcon(icon) | |
310 | ||
311 | if wx.Platform == '__WXMSW__': | |
312 | # setup a taskbar icon, and catch some events from it | |
313 | self.tbicon = wx.TaskBarIcon() | |
314 | self.tbicon.SetIcon(icon, "wxPython Demo") | |
315 | wx.EVT_TASKBAR_LEFT_DCLICK(self.tbicon, self.OnTaskBarActivate) | |
316 | wx.EVT_TASKBAR_RIGHT_UP(self.tbicon, self.OnTaskBarMenu) | |
317 | wx.EVT_MENU(self.tbicon, self.TBMENU_RESTORE, self.OnTaskBarActivate) | |
318 | wx.EVT_MENU(self.tbicon, self.TBMENU_CLOSE, self.OnTaskBarClose) | |
319 | ||
320 | wx.CallAfter(self.ShowTip) | |
321 | ||
322 | self.otherWin = None | |
323 | wx.EVT_IDLE(self, self.OnIdle) | |
324 | wx.EVT_CLOSE(self, self.OnCloseWindow) | |
325 | wx.EVT_ICONIZE(self, self.OnIconfiy) | |
326 | wx.EVT_MAXIMIZE(self, self.OnMaximize) | |
327 | ||
328 | self.Centre(wx.BOTH) | |
329 | self.CreateStatusBar(1, wx.ST_SIZEGRIP) | |
330 | ||
331 | splitter = wx.SplitterWindow(self, -1, style=wx.NO_3D|wx.SP_3D) | |
332 | splitter2 = wx.SplitterWindow(splitter, -1, style=wx.NO_3D|wx.SP_3D) | |
333 | ||
334 | def EmptyHandler(evt): pass | |
335 | wx.EVT_ERASE_BACKGROUND(splitter, EmptyHandler) | |
336 | wx.EVT_ERASE_BACKGROUND(splitter2, EmptyHandler) | |
337 | ||
338 | # Prevent TreeCtrl from displaying all items after destruction when True | |
339 | self.dying = False | |
340 | ||
341 | # Make a File menu | |
342 | self.mainmenu = wx.MenuBar() | |
343 | menu = wx.Menu() | |
344 | exitID = wx.NewId() | |
345 | menu.Append(exitID, 'E&xit\tAlt-X', 'Get the heck outta here!') | |
346 | wx.EVT_MENU(self, exitID, self.OnFileExit) | |
347 | wx.App_SetMacExitMenuItemId(exitID) | |
348 | self.mainmenu.Append(menu, '&File') | |
349 | ||
350 | # Make a Demo menu | |
351 | menu = wx.Menu() | |
352 | for item in _treeList: | |
353 | submenu = wx.Menu() | |
354 | for childItem in item[1]: | |
355 | mID = wx.NewId() | |
356 | submenu.Append(mID, childItem) | |
357 | wx.EVT_MENU(self, mID, self.OnDemoMenu) | |
358 | menu.AppendMenu(wx.NewId(), item[0], submenu) | |
359 | self.mainmenu.Append(menu, '&Demo') | |
360 | ||
361 | ||
362 | # Make a Help menu | |
363 | helpID = wx.NewId() | |
364 | findID = wx.NewId() | |
365 | findnextID = wx.NewId() | |
366 | menu = wx.Menu() | |
367 | menu.Append(findID, '&Find\tCtrl-F', 'Find in the Demo Code') | |
368 | menu.Append(findnextID, 'Find &Next\tF3', 'Find Next') | |
369 | menu.AppendSeparator() | |
370 | menu.Append(helpID, '&About\tCtrl-H', 'wxPython RULES!!!') | |
371 | wx.App_SetMacAboutMenuItemId(helpID) | |
372 | wx.EVT_MENU(self, helpID, self.OnHelpAbout) | |
373 | wx.EVT_MENU(self, findID, self.OnHelpFind) | |
374 | wx.EVT_MENU(self, findnextID, self.OnFindNext) | |
375 | wx.EVT_COMMAND_FIND(self, -1, self.OnFind) | |
376 | wx.EVT_COMMAND_FIND_NEXT(self, -1, self.OnFind) | |
377 | wx.EVT_COMMAND_FIND_CLOSE(self, -1 , self.OnFindClose) | |
378 | self.mainmenu.Append(menu, '&Help') | |
379 | self.SetMenuBar(self.mainmenu) | |
380 | ||
381 | self.finddata = wx.FindReplaceData() | |
382 | ||
383 | if 0: | |
384 | # This is another way to set Accelerators, in addition to | |
385 | # using the '\t<key>' syntax in the menu items. | |
386 | aTable = wx.AcceleratorTable([(wx.ACCEL_ALT, ord('X'), exitID), | |
387 | (wx.ACCEL_CTRL, ord('H'), helpID), | |
388 | (wx.ACCEL_CTRL, ord('F'), findID), | |
389 | (wx.ACCEL_NORMAL, WXK_F3, findnextID) | |
390 | ]) | |
391 | self.SetAcceleratorTable(aTable) | |
392 | ||
393 | ||
394 | # Create a TreeCtrl | |
395 | tID = wx.NewId() | |
396 | self.treeMap = {} | |
397 | self.tree = wx.TreeCtrl(splitter, tID, | |
398 | style=wx.TR_HAS_BUTTONS | | |
399 | wx.TR_HAS_VARIABLE_ROW_HEIGHT | |
400 | ) | |
401 | ||
402 | root = self.tree.AddRoot("wxPython Overview") | |
403 | firstChild = None | |
404 | for item in _treeList: | |
405 | child = self.tree.AppendItem(root, item[0]) | |
406 | if not firstChild: firstChild = child | |
407 | for childItem in item[1]: | |
408 | theDemo = self.tree.AppendItem(child, childItem) | |
409 | self.treeMap[childItem] = theDemo | |
410 | ||
411 | self.tree.Expand(root) | |
412 | self.tree.Expand(firstChild) | |
413 | wx.EVT_TREE_ITEM_EXPANDED (self.tree, tID, self.OnItemExpanded) | |
414 | wx.EVT_TREE_ITEM_COLLAPSED (self.tree, tID, self.OnItemCollapsed) | |
415 | wx.EVT_TREE_SEL_CHANGED (self.tree, tID, self.OnSelChanged) | |
416 | wx.EVT_LEFT_DOWN (self.tree, self.OnTreeLeftDown) | |
417 | ||
418 | # Create a Notebook | |
419 | self.nb = wx.Notebook(splitter2, -1, style=wx.CLIP_CHILDREN) | |
420 | ||
421 | # Set up a wx.html.HtmlWindow on the Overview Notebook page | |
422 | # we put it in a panel first because there seems to be a | |
423 | # refresh bug of some sort (wxGTK) when it is directly in | |
424 | # the notebook... | |
425 | if 0: # the old way | |
426 | self.ovr = wx.html.HtmlWindow(self.nb, -1, size=(400, 400)) | |
427 | self.nb.AddPage(self.ovr, self.overviewText) | |
428 | ||
429 | else: # hopefully I can remove this hacky code soon, see SF bug #216861 | |
430 | panel = wx.Panel(self.nb, -1, style=wx.CLIP_CHILDREN) | |
431 | self.ovr = wx.html.HtmlWindow(panel, -1, size=(400, 400)) | |
432 | self.nb.AddPage(panel, self.overviewText) | |
433 | ||
434 | def OnOvrSize(evt, ovr=self.ovr): | |
435 | ovr.SetSize(evt.GetSize()) | |
436 | ||
437 | wx.EVT_SIZE(panel, OnOvrSize) | |
438 | wx.EVT_ERASE_BACKGROUND(panel, EmptyHandler) | |
439 | ||
440 | ||
441 | self.SetOverview(self.overviewText, overview) | |
442 | ||
443 | ||
444 | # Set up a notebook page for viewing the source code of each sample | |
445 | self.txt = DemoCodeViewer(self.nb, -1) | |
446 | self.nb.AddPage(self.txt, "Demo Code") | |
447 | self.GetDemoFile('Main.py') | |
448 | ||
449 | ||
450 | # Set up a log on the View Log Notebook page | |
451 | self.log = wx.TextCtrl(splitter2, -1, | |
452 | style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL) | |
453 | ||
454 | # Set the wxWindows log target to be this textctrl | |
455 | #wx.Log_SetActiveTarget(wx.LogTextCtrl(self.log)) | |
456 | ||
457 | # But instead of the above we want to show how to use our own wx.Log class | |
458 | wx.Log_SetActiveTarget(MyLog(self.log)) | |
459 | ||
460 | # for serious debugging | |
461 | #wx.Log_SetActiveTarget(wx.LogStderr()) | |
462 | #wx.Log_SetTraceMask(wx.TraceMessages) | |
463 | ||
464 | self.Show(True) | |
465 | ||
466 | ||
467 | # add the windows to the splitter and split it. | |
468 | splitter2.SplitHorizontally(self.nb, self.log, 450) | |
469 | splitter.SplitVertically(self.tree, splitter2, 180) | |
470 | ||
471 | splitter.SetMinimumPaneSize(20) | |
472 | splitter2.SetMinimumPaneSize(20) | |
473 | ||
474 | ||
475 | ||
476 | # select initial items | |
477 | self.nb.SetSelection(0) | |
478 | self.tree.SelectItem(root) | |
479 | ||
480 | if len(sys.argv) == 2: | |
481 | try: | |
482 | selectedDemo = self.treeMap[sys.argv[1]] | |
483 | except: | |
484 | selectedDemo = None | |
485 | if selectedDemo: | |
486 | self.tree.SelectItem(selectedDemo) | |
487 | self.tree.EnsureVisible(selectedDemo) | |
488 | ||
489 | ||
490 | wx.LogMessage('window handle: %s' % self.GetHandle()) | |
491 | ||
492 | ||
493 | #--------------------------------------------- | |
494 | def WriteText(self, text): | |
495 | if text[-1:] == '\n': | |
496 | text = text[:-1] | |
497 | wx.LogMessage(text) | |
498 | ||
499 | ||
500 | def write(self, txt): | |
501 | self.WriteText(txt) | |
502 | ||
503 | #--------------------------------------------- | |
504 | def OnItemExpanded(self, event): | |
505 | item = event.GetItem() | |
506 | wx.LogMessage("OnItemExpanded: %s" % self.tree.GetItemText(item)) | |
507 | event.Skip() | |
508 | ||
509 | #--------------------------------------------- | |
510 | def OnItemCollapsed(self, event): | |
511 | item = event.GetItem() | |
512 | wx.LogMessage("OnItemCollapsed: %s" % self.tree.GetItemText(item)) | |
513 | event.Skip() | |
514 | ||
515 | #--------------------------------------------- | |
516 | def OnTreeLeftDown(self, event): | |
517 | pt = event.GetPosition(); | |
518 | item, flags = self.tree.HitTest(pt) | |
519 | if item == self.tree.GetSelection(): | |
520 | self.SetOverview(self.tree.GetItemText(item)+" Overview", self.curOverview) | |
521 | event.Skip() | |
522 | ||
523 | #--------------------------------------------- | |
524 | def OnSelChanged(self, event): | |
525 | if self.dying: | |
526 | return | |
527 | ||
528 | item = event.GetItem() | |
529 | itemText = self.tree.GetItemText(item) | |
530 | self.RunDemo(itemText) | |
531 | ||
532 | ||
533 | #--------------------------------------------- | |
534 | def RunDemo(self, itemText): | |
535 | os.chdir(self.cwd) | |
536 | if self.nb.GetPageCount() == 3: | |
537 | if self.nb.GetSelection() == 2: | |
538 | self.nb.SetSelection(0) | |
539 | # inform the window that it's time to quit if it cares | |
540 | if self.window is not None: | |
541 | if hasattr(self.window, "ShutdownDemo"): | |
542 | self.window.ShutdownDemo() | |
543 | wx.SafeYield() # in case the page has pending events | |
544 | self.nb.DeletePage(2) | |
545 | ||
546 | if itemText == self.overviewText: | |
547 | self.GetDemoFile('Main.py') | |
548 | self.SetOverview(self.overviewText, overview) | |
549 | self.nb.Refresh(); | |
550 | self.window = None | |
551 | ||
552 | else: | |
553 | if os.path.exists(itemText + '.py'): | |
554 | wx.BeginBusyCursor() | |
555 | wx.LogMessage("Running demo %s.py..." % itemText) | |
556 | try: | |
557 | self.GetDemoFile(itemText + '.py') | |
558 | module = __import__(itemText, globals()) | |
559 | self.SetOverview(itemText + " Overview", module.overview) | |
560 | finally: | |
561 | wx.EndBusyCursor() | |
562 | self.tree.Refresh() | |
563 | ||
564 | # in case runTest is modal, make sure things look right... | |
565 | self.nb.Refresh(); | |
566 | wx.SafeYield() | |
567 | ||
568 | self.window = module.runTest(self, self.nb, self) ### | |
569 | if self.window is not None: | |
570 | self.nb.AddPage(self.window, 'Demo') | |
571 | self.nb.SetSelection(2) | |
572 | self.nb.Refresh() # without this wxMac has troubles showing the just added page | |
573 | ||
574 | else: | |
575 | self.ovr.SetPage("") | |
576 | self.txt.Clear() | |
577 | self.window = None | |
578 | ||
579 | ||
580 | ||
581 | #--------------------------------------------- | |
582 | # Get the Demo files | |
583 | def GetDemoFile(self, filename): | |
584 | self.txt.Clear() | |
585 | try: | |
586 | self.txt.SetValue(open(filename).read()) | |
587 | except IOError: | |
588 | self.txt.SetValue("Cannot open %s file." % filename) | |
589 | ||
590 | self.txt.SetInsertionPoint(0) | |
591 | self.txt.ShowPosition(0) | |
592 | ||
593 | #--------------------------------------------- | |
594 | def SetOverview(self, name, text): | |
595 | self.curOverview = text | |
596 | lead = text[:6] | |
597 | if lead != '<html>' and lead != '<HTML>': | |
598 | text = '<br>'.join(text.split('\n')) | |
599 | self.ovr.SetPage(text) | |
600 | self.nb.SetPageText(0, name) | |
601 | ||
602 | #--------------------------------------------- | |
603 | # Menu methods | |
604 | def OnFileExit(self, *event): | |
605 | self.Close() | |
606 | ||
607 | def OnHelpAbout(self, event): | |
608 | from About import MyAboutBox | |
609 | about = MyAboutBox(self) | |
610 | about.ShowModal() | |
611 | about.Destroy() | |
612 | ||
613 | def OnHelpFind(self, event): | |
614 | self.nb.SetSelection(1) | |
615 | self.finddlg = wx.FindReplaceDialog(self, self.finddata, "Find", | |
616 | wx.FR_NOUPDOWN | | |
617 | wx.FR_NOMATCHCASE | | |
618 | wx.FR_NOWHOLEWORD) | |
619 | self.finddlg.Show(True) | |
620 | ||
621 | def OnFind(self, event): | |
622 | self.nb.SetSelection(1) | |
623 | end = self.txt.GetLastPosition() | |
624 | textstring = self.txt.GetRange(0, end).lower() | |
625 | start = self.txt.GetSelection()[1] | |
626 | findstring = self.finddata.GetFindString().lower() | |
627 | loc = textstring.find(findstring, start) | |
628 | if loc == -1 and start != 0: | |
629 | # string not found, start at beginning | |
630 | start = 0 | |
631 | loc = textstring.find(findstring, start) | |
632 | if loc == -1: | |
633 | dlg = wx.MessageDialog(self, 'Find String Not Found', | |
634 | 'Find String Not Found in Demo File', | |
635 | wx.OK | wx.ICON_INFORMATION) | |
636 | dlg.ShowModal() | |
637 | dlg.Destroy() | |
638 | if self.finddlg: | |
639 | if loc == -1: | |
640 | self.finddlg.SetFocus() | |
641 | return | |
642 | else: | |
643 | self.finddlg.Destroy() | |
644 | self.txt.ShowPosition(loc) | |
645 | self.txt.SetSelection(loc, loc + len(findstring)) | |
646 | ||
647 | ||
648 | ||
649 | def OnFindNext(self, event): | |
650 | if self.finddata.GetFindString(): | |
651 | self.OnFind(event) | |
652 | else: | |
653 | self.OnHelpFind(event) | |
654 | ||
655 | def OnFindClose(self, event): | |
656 | event.GetDialog().Destroy() | |
657 | ||
658 | ||
659 | #--------------------------------------------- | |
660 | def OnCloseWindow(self, event): | |
661 | self.dying = True | |
662 | self.window = None | |
663 | self.mainmenu = None | |
664 | if hasattr(self, "tbicon"): | |
665 | del self.tbicon | |
666 | self.Destroy() | |
667 | ||
668 | ||
669 | #--------------------------------------------- | |
670 | def OnIdle(self, event): | |
671 | if self.otherWin: | |
672 | self.otherWin.Raise() | |
673 | self.window = self.otherWin | |
674 | self.otherWin = None | |
675 | ||
676 | ||
677 | #--------------------------------------------- | |
678 | def ShowTip(self): | |
679 | try: | |
680 | showTipText = open(opj("data/showTips")).read() | |
681 | showTip, index = eval(showTipText) | |
682 | except IOError: | |
683 | showTip, index = (1, 0) | |
684 | if showTip: | |
685 | tp = wx.CreateFileTipProvider(opj("data/tips.txt"), index) | |
686 | ##tp = MyTP(0) | |
687 | showTip = wx.ShowTip(self, tp) | |
688 | index = tp.GetCurrentTip() | |
689 | open(opj("data/showTips"), "w").write(str( (showTip, index) )) | |
690 | ||
691 | ||
692 | #--------------------------------------------- | |
693 | def OnDemoMenu(self, event): | |
694 | try: | |
695 | selectedDemo = self.treeMap[self.mainmenu.GetLabel(event.GetId())] | |
696 | except: | |
697 | selectedDemo = None | |
698 | if selectedDemo: | |
699 | self.tree.SelectItem(selectedDemo) | |
700 | self.tree.EnsureVisible(selectedDemo) | |
701 | ||
702 | ||
703 | #--------------------------------------------- | |
704 | def OnTaskBarActivate(self, evt): | |
705 | if self.IsIconized(): | |
706 | self.Iconize(False) | |
707 | if not self.IsShown(): | |
708 | self.Show(True) | |
709 | self.Raise() | |
710 | ||
711 | #--------------------------------------------- | |
712 | ||
713 | TBMENU_RESTORE = 1000 | |
714 | TBMENU_CLOSE = 1001 | |
715 | ||
716 | def OnTaskBarMenu(self, evt): | |
717 | menu = wx.Menu() | |
718 | menu.Append(self.TBMENU_RESTORE, "Restore wxPython Demo") | |
719 | menu.Append(self.TBMENU_CLOSE, "Close") | |
720 | self.tbicon.PopupMenu(menu) | |
721 | menu.Destroy() | |
722 | ||
723 | #--------------------------------------------- | |
724 | def OnTaskBarClose(self, evt): | |
725 | self.Close() | |
726 | ||
727 | # because of the way wx.TaskBarIcon.PopupMenu is implemented we have to | |
728 | # prod the main idle handler a bit to get the window to actually close | |
729 | wx.GetApp().ProcessIdle() | |
730 | ||
731 | ||
732 | #--------------------------------------------- | |
733 | def OnIconfiy(self, evt): | |
734 | wx.LogMessage("OnIconfiy") | |
735 | evt.Skip() | |
736 | ||
737 | #--------------------------------------------- | |
738 | def OnMaximize(self, evt): | |
739 | wx.LogMessage("OnMaximize") | |
740 | evt.Skip() | |
741 | ||
742 | ||
743 | ||
744 | ||
745 | #--------------------------------------------------------------------------- | |
746 | #--------------------------------------------------------------------------- | |
747 | ||
748 | class MySplashScreen(wx.SplashScreen): | |
749 | def __init__(self): | |
750 | bmp = wx.Image(opj("bitmaps/splash.gif")).ConvertToBitmap() | |
751 | wx.SplashScreen.__init__(self, bmp, | |
752 | wx.SPLASH_CENTRE_ON_SCREEN|wx.SPLASH_TIMEOUT, | |
753 | 4000, None, -1, | |
754 | style = wx.SIMPLE_BORDER|wx.FRAME_NO_TASKBAR|wx.STAY_ON_TOP) | |
755 | wx.EVT_CLOSE(self, self.OnClose) | |
756 | ||
757 | def OnClose(self, evt): | |
758 | frame = wxPythonDemo(None, -1, "wxPython: (A Demonstration)") | |
759 | frame.Show() | |
760 | evt.Skip() # Make sure the default handler runs too... | |
761 | ||
762 | ||
763 | class MyApp(wx.App): | |
764 | def OnInit(self): | |
765 | """ | |
766 | Create and show the splash screen. It will then create and show | |
767 | the main frame when it is time to do so. | |
768 | """ | |
769 | ||
770 | #import locale | |
771 | #self.locale = wx.Locale(wx.LANGUAGE_FRENCH) | |
772 | #locale.setlocale(locale.LC_ALL, 'fr') | |
773 | ||
774 | wx.InitAllImageHandlers() | |
775 | splash = MySplashScreen() | |
776 | splash.Show() | |
777 | return True | |
778 | ||
779 | ||
780 | ||
781 | #--------------------------------------------------------------------------- | |
782 | ||
783 | def main(): | |
784 | try: | |
785 | demoPath = os.path.dirname(__file__) | |
786 | os.chdir(demoPath) | |
787 | except: | |
788 | pass | |
789 | app = MyApp(wx.Platform == "__WXMAC__") | |
790 | app.MainLoop() | |
791 | ||
792 | ||
793 | #--------------------------------------------------------------------------- | |
794 | ||
795 | ||
796 | ||
797 | overview = """<html><body> | |
798 | <h2>wxPython</h2> | |
799 | ||
800 | <p> wxPython is a <b>GUI toolkit</b> for the <a | |
801 | href="http://www.python.org/">Python</a> programming language. It | |
802 | allows Python programmers to create programs with a robust, highly | |
803 | functional graphical user interface, simply and easily. It is | |
804 | implemented as a Python extension module (native code) that wraps the | |
805 | popular <a href="http://wxwindows.org/front.htm">wxWindows</a> cross | |
806 | platform GUI library, which is written in C++. | |
807 | ||
808 | <p> Like Python and wxWindows, wxPython is <b>Open Source</b> which | |
809 | means that it is free for anyone to use and the source code is | |
810 | available for anyone to look at and modify. Or anyone can contribute | |
811 | fixes or enhnacments to the project. | |
812 | ||
813 | <p> wxPython is a <b>cross-platform</b> toolkit. This means that the | |
814 | same program will run on multiple platforms without modification. | |
815 | Currently supported platforms are 32-bit Microsoft Windows, most Unix | |
816 | or unix-like systems, and Macintosh OS X. Since the language is | |
817 | Python, wxPython programs are <b>simple, easy</b> to write and easy to | |
818 | understand. | |
819 | ||
820 | <p> <b>This demo</b> is not only a collection of test cases for | |
821 | wxPython, but is also designed to help you learn about and how to use | |
822 | wxPython. Each sample is listed in the tree control on the left. | |
823 | When a sample is selected in the tree then a module is loaded and run | |
824 | (usually in a tab of this notebook,) and the source code of the module | |
825 | is loaded in another tab for you to browse and learn from. | |
826 | ||
827 | """ | |
828 | ||
829 | ||
830 | #---------------------------------------------------------------------------- | |
831 | #---------------------------------------------------------------------------- | |
832 | ||
833 | if __name__ == '__main__': | |
834 | main() | |
835 | ||
836 | #---------------------------------------------------------------------------- | |
837 | ||
838 | ||
839 | ||
840 | ||
841 | ||
842 | ||
843 |