]>
Commit | Line | Data |
---|---|---|
f6bcfd97 | 1 | #!/usr/bin/env python |
4c9993c3 RD |
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 | ||
1fded56b | 20 | import wx # This module uses the new wx namespace |
c38400d0 RD |
21 | import sys, os |
22 | ||
23 | # stuff for debugging | |
8dc6dcde | 24 | print "wx.VERSION_STRING = ", wx.VERSION_STRING |
c38400d0 RD |
25 | print "pid:", os.getpid() |
26 | ##raw_input("Press a key...") | |
4c9993c3 | 27 | |
d14a1e28 RD |
28 | assertMode = wx.PYAPP_ASSERT_DIALOG |
29 | ##assertMode = wx.PYAPP_ASSERT_EXCEPTION | |
30 | ||
4c9993c3 RD |
31 | |
32 | #---------------------------------------------------------------------------- | |
33 | ||
34 | class Log: | |
35 | def WriteText(self, text): | |
1e4a197e RD |
36 | if text[-1:] == '\n': |
37 | text = text[:-1] | |
1fded56b | 38 | wx.LogMessage(text) |
4c9993c3 RD |
39 | write = WriteText |
40 | ||
41 | ||
1fded56b | 42 | class RunDemoApp(wx.App): |
8eca4fef | 43 | def __init__(self, name, module, useShell): |
4c9993c3 RD |
44 | self.name = name |
45 | self.demoModule = module | |
8eca4fef | 46 | self.useShell = useShell |
1fded56b | 47 | wx.App.__init__(self, 0) |
0af45411 | 48 | |
4c9993c3 RD |
49 | |
50 | def OnInit(self): | |
1fded56b RD |
51 | wx.InitAllImageHandlers() |
52 | wx.Log_SetActiveTarget(wx.LogStderr()) | |
1e4a197e | 53 | |
d14a1e28 | 54 | self.SetAssertMode(assertMode) |
1fded56b | 55 | |
fd3f2efe | 56 | frame = wx.Frame(None, -1, "RunDemo: " + self.name, pos=(50,50), size=(200,100), |
1fded56b | 57 | style=wx.NO_FULL_REPAINT_ON_RESIZE|wx.DEFAULT_FRAME_STYLE) |
4c9993c3 | 58 | frame.CreateStatusBar() |
7f8f0b88 | 59 | |
1fded56b RD |
60 | menuBar = wx.MenuBar() |
61 | menu = wx.Menu() | |
7f8f0b88 RD |
62 | item = menu.Append(-1, "E&xit\tAlt-X", "Exit demo") |
63 | self.Bind(wx.EVT_MENU, self.OnButton, item) | |
8082483b | 64 | menuBar.Append(menu, "&File") |
7f8f0b88 | 65 | |
8eca4fef | 66 | ns = {} |
31e36465 | 67 | ns['wx'] = wx |
8eca4fef RD |
68 | ns['app'] = self |
69 | ns['module'] = self.demoModule | |
70 | ns['frame'] = frame | |
71 | ||
8082483b | 72 | frame.SetMenuBar(menuBar) |
1e4a197e | 73 | frame.Show(True) |
7f8f0b88 | 74 | frame.Bind(wx.EVT_CLOSE, self.OnCloseFrame) |
1e4a197e | 75 | |
4c9993c3 RD |
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 | |
f6bcfd97 BP |
82 | frame.SetSize((640, 480)) |
83 | win.SetFocus() | |
1e4a197e | 84 | self.window = win |
8eca4fef RD |
85 | ns['win'] = win |
86 | frect = frame.GetRect() | |
4c9993c3 RD |
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'): | |
8eca4fef RD |
92 | ns['win'] = frame.otherWin |
93 | frect = frame.otherWin.GetRect() | |
d14a1e28 RD |
94 | p = wx.Panel(frame, -1) |
95 | b = wx.Button(p, -1, " Exit ", (10,10)) | |
96 | p.Fit() | |
71ed12a0 RD |
97 | #frame.SetClientSize(p.GetSize()) |
98 | frame.SetSize((200, 100)) | |
7f8f0b88 | 99 | frame.Bind(wx.EVT_BUTTON, self.OnButton, b) |
4c9993c3 RD |
100 | else: |
101 | # It was probably a dialog or something that is already | |
102 | # gone, so we're done. | |
103 | frame.Destroy() | |
1e4a197e | 104 | return True |
4c9993c3 RD |
105 | |
106 | self.SetTopWindow(frame) | |
107 | self.frame = frame | |
1fded56b RD |
108 | #wx.Log_SetActiveTarget(wx.LogStderr()) |
109 | #wx.Log_SetTraceMask(wx.TraceMessages) | |
8eca4fef RD |
110 | |
111 | if self.useShell: | |
112 | # Make a PyShell window, and position it below our test window | |
113 | from wx import py | |
114 | shell = py.shell.ShellFrame(None, locals=ns) | |
115 | frect.OffsetXY(0, frect.height) | |
116 | frect.height = 400 | |
117 | shell.SetRect(frect) | |
118 | shell.Show() | |
119 | ||
120 | # Hook the close event of the test window so that we close | |
121 | # the shell at the same time | |
122 | def CloseShell(evt): | |
71ed12a0 RD |
123 | if shell: |
124 | shell.Close() | |
8eca4fef RD |
125 | evt.Skip() |
126 | frame.Bind(wx.EVT_CLOSE, CloseShell) | |
127 | ||
1e4a197e | 128 | return True |
4c9993c3 RD |
129 | |
130 | ||
131 | def OnButton(self, evt): | |
1e4a197e RD |
132 | self.frame.Close(True) |
133 | ||
134 | ||
135 | def OnCloseFrame(self, evt): | |
136 | if hasattr(self, "window") and hasattr(self.window, "ShutdownDemo"): | |
137 | self.window.ShutdownDemo() | |
138 | evt.Skip() | |
139 | ||
4c9993c3 RD |
140 | |
141 | #---------------------------------------------------------------------------- | |
142 | ||
143 | ||
7ff49f0c | 144 | def main(argv): |
8eca4fef RD |
145 | useShell = False |
146 | for x in range(len(sys.argv)): | |
147 | if sys.argv[x] in ['--shell', '-shell', '-s']: | |
148 | useShell = True | |
149 | del sys.argv[x] | |
150 | break | |
151 | ||
3979290c | 152 | if len(argv) < 2: |
4c9993c3 RD |
153 | print "Please specify a demo module name on the command-line" |
154 | raise SystemExit | |
155 | ||
887dfc14 | 156 | name, ext = os.path.splitext(argv[1]) |
4c9993c3 RD |
157 | module = __import__(name) |
158 | ||
159 | ||
8eca4fef | 160 | app = RunDemoApp(name, module, useShell) |
4c9993c3 RD |
161 | app.MainLoop() |
162 | ||
163 | ||
164 | ||
165 | if __name__ == "__main__": | |
7ff49f0c | 166 | main(sys.argv) |
4c9993c3 RD |
167 | |
168 |