]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/run.py
Don't insert a newline when we get text
[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
21 import wx.lib.mixins.inspection
22 import sys, os
23
24 # stuff for debugging
25 print "wx.version:", wx.version()
26 print "pid:", os.getpid()
27 ##raw_input("Press Enter...")
28
29 assertMode = wx.PYAPP_ASSERT_DIALOG
30 ##assertMode = wx.PYAPP_ASSERT_EXCEPTION
31
32
33 #----------------------------------------------------------------------------
34
35 class Log:
36 def WriteText(self, text):
37 if text[-1:] == '\n':
38 text = text[:-1]
39 wx.LogMessage(text)
40 write = WriteText
41
42
43 class RunDemoApp(wx.App, wx.lib.mixins.inspection.InspectionMixin):
44 def __init__(self, name, module, useShell):
45 self.name = name
46 self.demoModule = module
47 self.useShell = useShell
48 wx.App.__init__(self, redirect=False)
49
50
51 def OnInit(self):
52 wx.Log_SetActiveTarget(wx.LogStderr())
53
54 self.SetAssertMode(assertMode)
55 self.Init() # InspectionMixin
56
57 frame = wx.Frame(None, -1, "RunDemo: " + self.name, pos=(50,50), size=(200,100),
58 style=wx.DEFAULT_FRAME_STYLE, name="run a sample")
59 frame.CreateStatusBar()
60
61 menuBar = wx.MenuBar()
62 menu = wx.Menu()
63 item = menu.Append(-1, "E&xit\tCtrl-Q", "Exit demo")
64 self.Bind(wx.EVT_MENU, self.OnExitApp, item)
65 menuBar.Append(menu, "&File")
66
67 ns = {}
68 ns['wx'] = wx
69 ns['app'] = self
70 ns['module'] = self.demoModule
71 ns['frame'] = frame
72
73 frame.SetMenuBar(menuBar)
74 frame.Show(True)
75 frame.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
76
77 win = self.demoModule.runTest(frame, frame, Log())
78
79 # a window will be returned if the demo does not create
80 # its own top-level window
81 if win:
82 # so set the frame to a good size for showing stuff
83 frame.SetSize((640, 480))
84 win.SetFocus()
85 self.window = win
86 ns['win'] = win
87 frect = frame.GetRect()
88
89 else:
90 # It was probably a dialog or something that is already
91 # gone, so we're done.
92 frame.Destroy()
93 return True
94
95 self.SetTopWindow(frame)
96 self.frame = frame
97 #wx.Log_SetActiveTarget(wx.LogStderr())
98 #wx.Log_SetTraceMask(wx.TraceMessages)
99
100 if self.useShell:
101 # Make a PyShell window, and position it below our test window
102 from wx import py
103 shell = py.shell.ShellFrame(None, locals=ns)
104 frect.OffsetXY(0, frect.height)
105 frect.height = 400
106 shell.SetRect(frect)
107 shell.Show()
108
109 # Hook the close event of the test window so that we close
110 # the shell at the same time
111 def CloseShell(evt):
112 if shell:
113 shell.Close()
114 evt.Skip()
115 frame.Bind(wx.EVT_CLOSE, CloseShell)
116
117 return True
118
119
120 def OnExitApp(self, evt):
121 self.frame.Close(True)
122
123
124 def OnCloseFrame(self, evt):
125 if hasattr(self, "window") and hasattr(self.window, "ShutdownDemo"):
126 self.window.ShutdownDemo()
127 evt.Skip()
128
129
130 #----------------------------------------------------------------------------
131
132
133 def main(argv):
134 useShell = False
135 for x in range(len(sys.argv)):
136 if sys.argv[x] in ['--shell', '-shell', '-s']:
137 useShell = True
138 del sys.argv[x]
139 break
140
141 if len(argv) < 2:
142 print "Please specify a demo module name on the command-line"
143 raise SystemExit
144
145 name, ext = os.path.splitext(argv[1])
146 module = __import__(name)
147
148
149 app = RunDemoApp(name, module, useShell)
150 app.MainLoop()
151
152
153
154 if __name__ == "__main__":
155 main(sys.argv)
156
157