]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/run.py
update from herbert breunung
[wxWidgets.git] / wxPython / demo / run.py
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 Enter...")
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.Log_SetActiveTarget(wx.LogStderr())
52
53 self.SetAssertMode(assertMode)
54
55 frame = wx.Frame(None, -1, "RunDemo: " + self.name, pos=(50,50), size=(200,100),
56 style=wx.DEFAULT_FRAME_STYLE)
57 frame.CreateStatusBar()
58
59 menuBar = wx.MenuBar()
60 menu = wx.Menu()
61 item = menu.Append(-1, "E&xit\tAlt-X", "Exit demo")
62 self.Bind(wx.EVT_MENU, self.OnButton, item)
63 menuBar.Append(menu, "&File")
64
65 ns = {}
66 ns['wx'] = wx
67 ns['app'] = self
68 ns['module'] = self.demoModule
69 ns['frame'] = frame
70
71 frame.SetMenuBar(menuBar)
72 frame.Show(True)
73 frame.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
74
75 win = self.demoModule.runTest(frame, frame, Log())
76
77 # a window will be returned if the demo does not create
78 # its own top-level window
79 if win:
80 # so set the frame to a good size for showing stuff
81 frame.SetSize((640, 480))
82 win.SetFocus()
83 self.window = win
84 ns['win'] = win
85 frect = frame.GetRect()
86
87 else:
88 # otherwise the demo made its own frame, so just put a
89 # button in this one
90 if hasattr(frame, 'otherWin'):
91 ns['win'] = frame.otherWin
92 frect = frame.otherWin.GetRect()
93 p = wx.Panel(frame, -1)
94 b = wx.Button(p, -1, " Exit ", (10,10))
95 wx.CallAfter(frame.SetClientSize, (200, 100))
96 frame.Bind(wx.EVT_BUTTON, self.OnButton, b)
97 else:
98 # It was probably a dialog or something that is already
99 # gone, so we're done.
100 frame.Destroy()
101 return True
102
103 self.SetTopWindow(frame)
104 self.frame = frame
105 #wx.Log_SetActiveTarget(wx.LogStderr())
106 #wx.Log_SetTraceMask(wx.TraceMessages)
107
108 if self.useShell:
109 # Make a PyShell window, and position it below our test window
110 from wx import py
111 shell = py.shell.ShellFrame(None, locals=ns)
112 frect.OffsetXY(0, frect.height)
113 frect.height = 400
114 shell.SetRect(frect)
115 shell.Show()
116
117 # Hook the close event of the test window so that we close
118 # the shell at the same time
119 def CloseShell(evt):
120 if shell:
121 shell.Close()
122 evt.Skip()
123 frame.Bind(wx.EVT_CLOSE, CloseShell)
124
125 return True
126
127
128 def OnButton(self, evt):
129 self.frame.Close(True)
130
131
132 def OnCloseFrame(self, evt):
133 if hasattr(self, "window") and hasattr(self.window, "ShutdownDemo"):
134 self.window.ShutdownDemo()
135 evt.Skip()
136
137
138 #----------------------------------------------------------------------------
139
140
141 def main(argv):
142 useShell = False
143 for x in range(len(sys.argv)):
144 if sys.argv[x] in ['--shell', '-shell', '-s']:
145 useShell = True
146 del sys.argv[x]
147 break
148
149 if len(argv) < 2:
150 print "Please specify a demo module name on the command-line"
151 raise SystemExit
152
153 name, ext = os.path.splitext(argv[1])
154 module = __import__(name)
155
156
157 app = RunDemoApp(name, module, useShell)
158 app.MainLoop()
159
160
161
162 if __name__ == "__main__":
163 main(sys.argv)
164
165