]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/run.py
I think I found out how GTK+ want popup windows
[wxWidgets.git] / wxPython / demo / run.py
CommitLineData
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"""
15This program will load and run one of the individual demos in this
16directory within its own frame window. Just specify the module name
17on the command line.
18"""
19
1fded56b 20import wx # This module uses the new wx namespace
c38400d0
RD
21import sys, os
22
23# stuff for debugging
8dc6dcde 24print "wx.VERSION_STRING = ", wx.VERSION_STRING
c38400d0 25print "pid:", os.getpid()
74a57fcd 26##raw_input("Press Enter...")
4c9993c3 27
d14a1e28
RD
28assertMode = wx.PYAPP_ASSERT_DIALOG
29##assertMode = wx.PYAPP_ASSERT_EXCEPTION
30
4c9993c3
RD
31
32#----------------------------------------------------------------------------
33
34class 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 42class 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
4287ca9e 47 wx.App.__init__(self, redirect=False)
0af45411 48
4c9993c3
RD
49
50 def OnInit(self):
1fded56b 51 wx.Log_SetActiveTarget(wx.LogStderr())
1e4a197e 52
d14a1e28 53 self.SetAssertMode(assertMode)
1fded56b 54
fd3f2efe 55 frame = wx.Frame(None, -1, "RunDemo: " + self.name, pos=(50,50), size=(200,100),
e478ff9e 56 style=wx.DEFAULT_FRAME_STYLE)
4c9993c3 57 frame.CreateStatusBar()
7f8f0b88 58
1fded56b
RD
59 menuBar = wx.MenuBar()
60 menu = wx.Menu()
7f8f0b88 61 item = menu.Append(-1, "E&xit\tAlt-X", "Exit demo")
31a01a24 62 self.Bind(wx.EVT_MENU, self.OnExitApp, item)
8082483b 63 menuBar.Append(menu, "&File")
7f8f0b88 64
8eca4fef 65 ns = {}
31e36465 66 ns['wx'] = wx
8eca4fef
RD
67 ns['app'] = self
68 ns['module'] = self.demoModule
69 ns['frame'] = frame
70
8082483b 71 frame.SetMenuBar(menuBar)
1e4a197e 72 frame.Show(True)
7f8f0b88 73 frame.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
1e4a197e 74
4c9993c3
RD
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
f6bcfd97
BP
81 frame.SetSize((640, 480))
82 win.SetFocus()
1e4a197e 83 self.window = win
8eca4fef
RD
84 ns['win'] = win
85 frect = frame.GetRect()
4c9993c3
RD
86
87 else:
8fc680f4
RD
88 # It was probably a dialog or something that is already
89 # gone, so we're done.
90 frame.Destroy()
91 return True
4c9993c3
RD
92
93 self.SetTopWindow(frame)
94 self.frame = frame
1fded56b
RD
95 #wx.Log_SetActiveTarget(wx.LogStderr())
96 #wx.Log_SetTraceMask(wx.TraceMessages)
8eca4fef
RD
97
98 if self.useShell:
99 # Make a PyShell window, and position it below our test window
100 from wx import py
101 shell = py.shell.ShellFrame(None, locals=ns)
102 frect.OffsetXY(0, frect.height)
103 frect.height = 400
104 shell.SetRect(frect)
105 shell.Show()
106
107 # Hook the close event of the test window so that we close
108 # the shell at the same time
109 def CloseShell(evt):
71ed12a0
RD
110 if shell:
111 shell.Close()
8eca4fef
RD
112 evt.Skip()
113 frame.Bind(wx.EVT_CLOSE, CloseShell)
114
1e4a197e 115 return True
4c9993c3
RD
116
117
31a01a24 118 def OnExitApp(self, evt):
1e4a197e
RD
119 self.frame.Close(True)
120
121
122 def OnCloseFrame(self, evt):
123 if hasattr(self, "window") and hasattr(self.window, "ShutdownDemo"):
124 self.window.ShutdownDemo()
125 evt.Skip()
126
4c9993c3
RD
127
128#----------------------------------------------------------------------------
129
130
7ff49f0c 131def main(argv):
8eca4fef
RD
132 useShell = False
133 for x in range(len(sys.argv)):
134 if sys.argv[x] in ['--shell', '-shell', '-s']:
135 useShell = True
136 del sys.argv[x]
137 break
138
3979290c 139 if len(argv) < 2:
4c9993c3
RD
140 print "Please specify a demo module name on the command-line"
141 raise SystemExit
142
887dfc14 143 name, ext = os.path.splitext(argv[1])
4c9993c3
RD
144 module = __import__(name)
145
146
8eca4fef 147 app = RunDemoApp(name, module, useShell)
4c9993c3
RD
148 app.MainLoop()
149
150
151
152if __name__ == "__main__":
7ff49f0c 153 main(sys.argv)
4c9993c3
RD
154
155