]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env python | |
2 | #---------------------------------------------------------------------------- | |
3 | # Name: run.py | |
4 | # Purpose: Simple framework for running individual demos | |
5 | # | |
6 | # Author: Robin Dunn | |
7 | # | |
8 | # Created: 6-March-2000 | |
9 | # RCS-ID: $Id$ | |
10 | # Copyright: (c) 2000 by Total Control Software | |
11 | # Licence: wxWindows license | |
12 | #---------------------------------------------------------------------------- | |
13 | ||
14 | """ | |
15 | This program will load and run one of the individual demos in this | |
16 | directory within its own frame window. Just specify the module name | |
17 | on the command line. | |
18 | """ | |
19 | ||
20 | import wx # This module uses the new wx namespace | |
21 | import sys, os | |
22 | ||
23 | # stuff for debugging | |
24 | print "wx.VERSION_STRING = ", wx.VERSION_STRING | |
25 | print "pid:", os.getpid() | |
26 | ##raw_input("Press a key...") | |
27 | ||
28 | assertMode = wx.PYAPP_ASSERT_DIALOG | |
29 | ##assertMode = wx.PYAPP_ASSERT_EXCEPTION | |
30 | ||
31 | ||
32 | #---------------------------------------------------------------------------- | |
33 | ||
34 | class Log: | |
35 | def WriteText(self, text): | |
36 | if text[-1:] == '\n': | |
37 | text = text[:-1] | |
38 | wx.LogMessage(text) | |
39 | write = WriteText | |
40 | ||
41 | ||
42 | class RunDemoApp(wx.App): | |
43 | def __init__(self, name, module, useShell): | |
44 | self.name = name | |
45 | self.demoModule = module | |
46 | self.useShell = useShell | |
47 | wx.App.__init__(self, 0) | |
48 | ||
49 | ||
50 | def OnInit(self): | |
51 | wx.InitAllImageHandlers() | |
52 | wx.Log_SetActiveTarget(wx.LogStderr()) | |
53 | ||
54 | self.SetAssertMode(assertMode) | |
55 | ||
56 | frame = wx.Frame(None, -1, "RunDemo: " + self.name, pos=(50,50), size=(200,100), | |
57 | style=wx.NO_FULL_REPAINT_ON_RESIZE|wx.DEFAULT_FRAME_STYLE) | |
58 | frame.CreateStatusBar() | |
59 | ||
60 | menuBar = wx.MenuBar() | |
61 | menu = wx.Menu() | |
62 | item = menu.Append(-1, "E&xit\tAlt-X", "Exit demo") | |
63 | self.Bind(wx.EVT_MENU, self.OnButton, item) | |
64 | menuBar.Append(menu, "&File") | |
65 | ||
66 | ns = {} | |
67 | ns['wx'] = wx | |
68 | ns['app'] = self | |
69 | ns['module'] = self.demoModule | |
70 | ns['frame'] = frame | |
71 | ||
72 | frame.SetMenuBar(menuBar) | |
73 | frame.Show(True) | |
74 | frame.Bind(wx.EVT_CLOSE, self.OnCloseFrame) | |
75 | ||
76 | win = self.demoModule.runTest(frame, frame, Log()) | |
77 | ||
78 | # a window will be returned if the demo does not create | |
79 | # its own top-level window | |
80 | if win: | |
81 | # so set the frame to a good size for showing stuff | |
82 | frame.SetSize((640, 480)) | |
83 | win.SetFocus() | |
84 | self.window = win | |
85 | ns['win'] = win | |
86 | frect = frame.GetRect() | |
87 | ||
88 | else: | |
89 | # otherwise the demo made its own frame, so just put a | |
90 | # button in this one | |
91 | if hasattr(frame, 'otherWin'): | |
92 | ns['win'] = frame.otherWin | |
93 | frect = frame.otherWin.GetRect() | |
94 | p = wx.Panel(frame, -1) | |
95 | b = wx.Button(p, -1, " Exit ", (10,10)) | |
96 | wx.CallAfter(frame.SetClientSize, (200, 100)) | |
97 | frame.Bind(wx.EVT_BUTTON, self.OnButton, b) | |
98 | else: | |
99 | # It was probably a dialog or something that is already | |
100 | # gone, so we're done. | |
101 | frame.Destroy() | |
102 | return True | |
103 | ||
104 | self.SetTopWindow(frame) | |
105 | self.frame = frame | |
106 | #wx.Log_SetActiveTarget(wx.LogStderr()) | |
107 | #wx.Log_SetTraceMask(wx.TraceMessages) | |
108 | ||
109 | if self.useShell: | |
110 | # Make a PyShell window, and position it below our test window | |
111 | from wx import py | |
112 | shell = py.shell.ShellFrame(None, locals=ns) | |
113 | frect.OffsetXY(0, frect.height) | |
114 | frect.height = 400 | |
115 | shell.SetRect(frect) | |
116 | shell.Show() | |
117 | ||
118 | # Hook the close event of the test window so that we close | |
119 | # the shell at the same time | |
120 | def CloseShell(evt): | |
121 | if shell: | |
122 | shell.Close() | |
123 | evt.Skip() | |
124 | frame.Bind(wx.EVT_CLOSE, CloseShell) | |
125 | ||
126 | return True | |
127 | ||
128 | ||
129 | def OnButton(self, evt): | |
130 | self.frame.Close(True) | |
131 | ||
132 | ||
133 | def OnCloseFrame(self, evt): | |
134 | if hasattr(self, "window") and hasattr(self.window, "ShutdownDemo"): | |
135 | self.window.ShutdownDemo() | |
136 | evt.Skip() | |
137 | ||
138 | ||
139 | #---------------------------------------------------------------------------- | |
140 | ||
141 | ||
142 | def main(argv): | |
143 | useShell = False | |
144 | for x in range(len(sys.argv)): | |
145 | if sys.argv[x] in ['--shell', '-shell', '-s']: | |
146 | useShell = True | |
147 | del sys.argv[x] | |
148 | break | |
149 | ||
150 | if len(argv) < 2: | |
151 | print "Please specify a demo module name on the command-line" | |
152 | raise SystemExit | |
153 | ||
154 | name, ext = os.path.splitext(argv[1]) | |
155 | module = __import__(name) | |
156 | ||
157 | ||
158 | app = RunDemoApp(name, module, useShell) | |
159 | app.MainLoop() | |
160 | ||
161 | ||
162 | ||
163 | if __name__ == "__main__": | |
164 | main(sys.argv) | |
165 | ||
166 |