]> git.saurik.com Git - wxWidgets.git/blame - wxPython/src/_app_ex.py
Adding overview docs and a couple tweaks to the demo.
[wxWidgets.git] / wxPython / src / _app_ex.py
CommitLineData
d14a1e28
RD
1
2#----------------------------------------------------------------------
3
4class PyOnDemandOutputWindow:
6c3b4aae
RD
5 """
6 A class that can be used for redirecting Python's stdout and
7 stderr streams. It will do nothing until something is wrriten to
8 the stream at which point it will create a Frame with a text area
9 and write the text there.
10 """
d14a1e28
RD
11 def __init__(self, title = "wxPython: stdout/stderr"):
12 self.frame = None
13 self.title = title
488256e0
RD
14 self.pos = wx.DefaultPosition
15 self.size = (450, 300)
d14a1e28
RD
16 self.parent = None
17
18 def SetParent(self, parent):
6c3b4aae 19 """Set the window to be used as the popup Frame's parent."""
d14a1e28
RD
20 self.parent = parent
21
6c3b4aae
RD
22
23 def CreateOutputWindow(self, st):
488256e0
RD
24 self.frame = wx.Frame(self.parent, -1, self.title, self.pos, self.size,
25 style=wx.DEFAULT_FRAME_STYLE)
64e8a1f0 26 self.text = wx.TextCtrl(self.frame, -1, "",
488256e0 27 style=wx.TE_MULTILINE|wx.TE_READONLY)
4a7ea057 28 self.text.AppendText(st)
6c3b4aae 29 self.frame.Show(True)
7e8142e9 30 self.frame.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
6c3b4aae 31
d14a1e28 32
330af869
RD
33 def OnCloseWindow(self, event):
34 if self.frame is not None:
35 self.frame.Destroy()
36 self.frame = None
37 self.text = None
38
39
d14a1e28 40 # These methods provide the file-like output behaviour.
6c3b4aae
RD
41 def write(self, text):
42 """
43 Create the output window if needed and write the string to it.
44 If not called in the context of the gui thread then uses
45 CallAfter to do the work there.
46 """
47 if self.frame is None:
48 if not wx.Thread_IsMain():
49 wx.CallAfter(self.CreateOutputWindow, text)
50 else:
51 self.CreateOutputWindow(text)
52 else:
53 if not wx.Thread_IsMain():
54 wx.CallAfter(self.text.AppendText, text)
55 else:
56 self.text.AppendText(text)
57
d14a1e28
RD
58
59 def close(self):
6c3b4aae
RD
60 if self.frame is not None:
61 wx.CallAfter(self.frame.Close)
d14a1e28
RD
62
63
f454e362
RD
64 def flush(self):
65 pass
66
67
6c3b4aae 68
d14a1e28 69#----------------------------------------------------------------------
d14a1e28
RD
70
71_defRedirect = (wx.Platform == '__WXMSW__' or wx.Platform == '__WXMAC__')
4d9de110 72
d14a1e28 73class App(wx.PyApp):
6c3b4aae 74 """
dce2bd22
RD
75 The ``wx.App`` class represents the application and is used to:
76
77 * bootstrap the wxPython system and initialize the underlying
78 gui toolkit
79 * set and get application-wide properties
80 * implement the windowing system main message or event loop,
81 and to dispatch events to window instances
82 * etc.
83
84 Every application must have a ``wx.App`` instance, and all
85 creation of UI objects should be delayed until after the
d7403ad2
RD
86 ``wx.App`` object has been created in order to ensure that the gui
87 platform and wxWidgets have been fully initialized.
dce2bd22
RD
88
89 Normally you would derive from this class and implement an
90 ``OnInit`` method that creates a frame and then calls
91 ``self.SetTopWindow(frame)``.
92
d7403ad2 93 :see: `wx.PySimpleApp` for a simpler app class that can be used
d07d2bc9 94 directly.
6c3b4aae 95 """
dce2bd22 96
d14a1e28
RD
97 outputWindowClass = PyOnDemandOutputWindow
98
d7403ad2
RD
99 def __init__(self, redirect=_defRedirect, filename=None,
100 useBestVisual=False, clearSigInt=True):
dce2bd22
RD
101 """
102 Construct a ``wx.App`` object.
103
d7403ad2
RD
104 :param redirect: Should ``sys.stdout`` and ``sys.stderr`` be
105 redirected? Defaults to True on Windows and Mac, False
106 otherwise. If `filename` is None then output will be
107 redirected to a window that pops up as needed. (You can
108 control what kind of window is created for the output by
109 resetting the class variable ``outputWindowClass`` to a
110 class of your choosing.)
dce2bd22 111
d7403ad2
RD
112 :param filename: The name of a file to redirect output to, if
113 redirect is True.
dce2bd22
RD
114
115 :param useBestVisual: Should the app try to use the best
d7403ad2
RD
116 available visual provided by the system (only relevant on
117 systems that have more than one visual.) This parameter
118 must be used instead of calling `SetUseBestVisual` later
119 on because it must be set before the underlying GUI
120 toolkit is initialized.
121
122 :param clearSigInt: Should SIGINT be cleared? This allows the
123 app to terminate upon a Ctrl-C in the console like other
124 GUI apps will.
dce2bd22
RD
125
126 :note: You should override OnInit to do applicaition
127 initialization to ensure that the system, toolkit and
128 wxWidgets are fully initialized.
129 """
4d9de110 130
d14a1e28
RD
131 wx.PyApp.__init__(self)
132
4d9de110 133 # make sure we can create a GUI
a4314b7e 134 if not self.IsDisplayAvailable():
4d9de110
RD
135
136 if wx.Platform == "__WXMAC__":
137 msg = """This program needs access to the screen.
138Please run with 'pythonw', not 'python', and only when you are logged
139in on the main display of your Mac."""
140
141 elif wx.Platform == "__WXGTK__":
142 msg ="Unable to access the X Display, is $DISPLAY set properly?"
143
144 else:
145 msg = "Unable to create GUI"
146 # TODO: more description is needed for wxMSW...
d14a1e28 147
4d9de110
RD
148 raise SystemExit(msg)
149
d14a1e28
RD
150 # This has to be done before OnInit
151 self.SetUseBestVisual(useBestVisual)
152
153 # Set the default handler for SIGINT. This fixes a problem
154 # where if Ctrl-C is pressed in the console that started this
155 # app then it will not appear to do anything, (not even send
156 # KeyboardInterrupt???) but will later segfault on exit. By
157 # setting the default handler then the app will exit, as
158 # expected (depending on platform.)
d7403ad2
RD
159 if clearSigInt:
160 try:
161 import signal
162 signal.signal(signal.SIGINT, signal.SIG_DFL)
163 except:
164 pass
d14a1e28
RD
165
166 # Save and redirect the stdio to a window?
167 self.stdioWin = None
168 self.saveStdio = (_sys.stdout, _sys.stderr)
169 if redirect:
170 self.RedirectStdio(filename)
171
62038e59
RD
172 # Use Python's install prefix as the default
173 wx.StandardPaths.Get().SetInstallPrefix(_sys.prefix)
174
4b71c4ec
RD
175 # Until the new native control for wxMac is up to par, still use the generic one.
176 wx.SystemOptions.SetOptionInt("mac.listctrl.always_use_generic", 1)
177
d14a1e28
RD
178 # This finishes the initialization of wxWindows and then calls
179 # the OnInit that should be present in the derived class
180 self._BootstrapApp()
181
182
57ffa585
RD
183 def OnPreInit(self):
184 """
185 Things that must be done after _BootstrapApp has done its
186 thing, but would be nice if they were already done by the time
187 that OnInit is called.
188 """
189 wx.StockGDI._initStockObjects()
190
191
99269e6c
RD
192 def __del__(self, destroy=wx.PyApp.__del__):
193 self.RestoreStdio() # Just in case the MainLoop was overridden
194 destroy(self)
d14a1e28 195
69ac96fd 196 def Destroy(self):
4b5a79cf 197 self.this.own(False)
69ac96fd 198 wx.PyApp.Destroy(self)
d14a1e28
RD
199
200 def SetTopWindow(self, frame):
1e0c8722 201 """Set the \"main\" top level window"""
d14a1e28
RD
202 if self.stdioWin:
203 self.stdioWin.SetParent(frame)
204 wx.PyApp.SetTopWindow(self, frame)
205
206
207 def MainLoop(self):
1e0c8722 208 """Execute the main GUI event loop"""
d14a1e28
RD
209 wx.PyApp.MainLoop(self)
210 self.RestoreStdio()
211
212
330af869 213 def RedirectStdio(self, filename=None):
1e0c8722 214 """Redirect sys.stdout and sys.stderr to a file or a popup window."""
d14a1e28
RD
215 if filename:
216 _sys.stdout = _sys.stderr = open(filename, 'a')
217 else:
218 self.stdioWin = self.outputWindowClass()
219 _sys.stdout = _sys.stderr = self.stdioWin
220
221
222 def RestoreStdio(self):
99269e6c
RD
223 try:
224 _sys.stdout, _sys.stderr = self.saveStdio
225 except:
226 pass
d14a1e28
RD
227
228
488256e0
RD
229 def SetOutputWindowAttributes(self, title=None, pos=None, size=None):
230 """
231 Set the title, position and/or size of the output window if
d20c6820
RD
232 the stdio has been redirected. This should be called before
233 any output would cause the output window to be created.
488256e0
RD
234 """
235 if self.stdioWin:
236 if title is not None:
237 self.stdioWin.title = title
238 if pos is not None:
239 self.stdioWin.pos = pos
240 if size is not None:
241 self.stdioWin.size = size
242
243
244
d14a1e28 245
dce2bd22 246# change from wx.PyApp_XX to wx.App_XX
54f9ee45
RD
247App_GetMacSupportPCMenuShortcuts = _core_.PyApp_GetMacSupportPCMenuShortcuts
248App_GetMacAboutMenuItemId = _core_.PyApp_GetMacAboutMenuItemId
249App_GetMacPreferencesMenuItemId = _core_.PyApp_GetMacPreferencesMenuItemId
250App_GetMacExitMenuItemId = _core_.PyApp_GetMacExitMenuItemId
251App_GetMacHelpMenuTitleName = _core_.PyApp_GetMacHelpMenuTitleName
252App_SetMacSupportPCMenuShortcuts = _core_.PyApp_SetMacSupportPCMenuShortcuts
253App_SetMacAboutMenuItemId = _core_.PyApp_SetMacAboutMenuItemId
254App_SetMacPreferencesMenuItemId = _core_.PyApp_SetMacPreferencesMenuItemId
255App_SetMacExitMenuItemId = _core_.PyApp_SetMacExitMenuItemId
256App_SetMacHelpMenuTitleName = _core_.PyApp_SetMacHelpMenuTitleName
257App_GetComCtl32Version = _core_.PyApp_GetComCtl32Version
d14a1e28
RD
258
259#----------------------------------------------------------------------------
260
261class PySimpleApp(wx.App):
6c3b4aae
RD
262 """
263 A simple application class. You can just create one of these and
264 then then make your top level windows later, and not have to worry
dce2bd22
RD
265 about OnInit. For example::
266
267 app = wx.PySimpleApp()
268 frame = wx.Frame(None, title='Hello World')
269 frame.Show()
270 app.MainLoop()
271
272 :see: `wx.App`
273 """
6c3b4aae 274
d7403ad2
RD
275 def __init__(self, redirect=False, filename=None,
276 useBestVisual=False, clearSigInt=True):
dce2bd22
RD
277 """
278 :see: `wx.App.__init__`
279 """
d7403ad2 280 wx.App.__init__(self, redirect, filename, useBestVisual, clearSigInt)
6c3b4aae 281
d14a1e28 282 def OnInit(self):
d14a1e28
RD
283 return True
284
285
dce2bd22 286
6c3b4aae 287# Is anybody using this one?
d14a1e28
RD
288class PyWidgetTester(wx.App):
289 def __init__(self, size = (250, 100)):
290 self.size = size
291 wx.App.__init__(self, 0)
292
293 def OnInit(self):
64e8a1f0 294 self.frame = wx.Frame(None, -1, "Widget Tester", pos=(0,0), size=self.size)
d14a1e28
RD
295 self.SetTopWindow(self.frame)
296 return True
297
dce2bd22
RD
298 def SetWidget(self, widgetClass, *args, **kwargs):
299 w = widgetClass(self.frame, *args, **kwargs)
d14a1e28
RD
300 self.frame.Show(True)
301
302#----------------------------------------------------------------------------
303# DO NOT hold any other references to this object. This is how we
dce2bd22 304# know when to cleanup system resources that wxWidgets is holding. When
d14a1e28 305# the sys module is unloaded, the refcount on sys.__wxPythonCleanup
dce2bd22 306# goes to zero and it calls the wx.App_CleanUp function.
d14a1e28
RD
307
308class __wxPyCleanup:
309 def __init__(self):
54f9ee45 310 self.cleanup = _core_.App_CleanUp
d14a1e28
RD
311 def __del__(self):
312 self.cleanup()
313
314_sys.__wxPythonCleanup = __wxPyCleanup()
315
316## # another possible solution, but it gets called too early...
dce2bd22
RD
317## import atexit
318## atexit.register(_core_.wxApp_CleanUp)
d14a1e28
RD
319
320
321#----------------------------------------------------------------------------