]> git.saurik.com Git - wxWidgets.git/blame - wxPython/src/_app_ex.py
Don't run SWIG when we're doing a clean
[wxWidgets.git] / wxPython / src / _app_ex.py
CommitLineData
d14a1e28
RD
1
2#----------------------------------------------------------------------
3
4class PyOnDemandOutputWindow:
5 def __init__(self, title = "wxPython: stdout/stderr"):
6 self.frame = None
7 self.title = title
8 self.parent = None
9
10 def SetParent(self, parent):
11 self.parent = parent
12
13 def OnCloseWindow(self, event):
14 if self.frame != None:
15 self.frame.Destroy()
16 self.frame = None
17 self.text = None
18
19 # These methods provide the file-like output behaviour.
20 def write(self, str):
21 if not wx.Thread_IsMain():
22 # Aquire the GUI mutex before making GUI calls. Mutex is released
23 # when locker is deleted at the end of this function.
24 locker = wx.MutexGuiLocker()
25
26 if not self.frame:
27 self.frame = wx.Frame(self.parent, -1, self.title,
28 style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
29 self.text = wxTextCtrl(self.frame, -1, "",
30 style = wx.TE_MULTILINE | wx.TE_READONLY)
31 self.frame.SetSize((450, 300))
32 self.frame.Show(True)
33 EVT_CLOSE(self.frame, self.OnCloseWindow)
34 self.text.AppendText(str)
35
36 def close(self):
37 if self.frame != None:
38 if not wx.Thread_IsMain():
39 locker = wx.MutexGuiLocker()
40 self.frame.Close()
41
42
43#----------------------------------------------------------------------
44# The main application class. Derive from this and implement an OnInit
45# method that creates a frame and then calls self.SetTopWindow(frame)
46
47_defRedirect = (wx.Platform == '__WXMSW__' or wx.Platform == '__WXMAC__')
48
49class App(wx.PyApp):
50 outputWindowClass = PyOnDemandOutputWindow
51
52 def __init__(self, redirect=_defRedirect, filename=None, useBestVisual=False):
53 wx.PyApp.__init__(self)
54
55 if wx.Platform == "__WXMAC__":
56 try:
57 import MacOS
58 if not MacOS.WMAvailable():
59 print """\
60This program needs access to the screen. Please run with 'pythonw',
61not 'python', and only when you are logged in on the main display of
62your Mac."""
63 _sys.exit(1)
64 except:
65 pass
66
67 # This has to be done before OnInit
68 self.SetUseBestVisual(useBestVisual)
69
70 # Set the default handler for SIGINT. This fixes a problem
71 # where if Ctrl-C is pressed in the console that started this
72 # app then it will not appear to do anything, (not even send
73 # KeyboardInterrupt???) but will later segfault on exit. By
74 # setting the default handler then the app will exit, as
75 # expected (depending on platform.)
76 try:
77 import signal
78 signal.signal(signal.SIGINT, signal.SIG_DFL)
79 except:
80 pass
81
82 # Save and redirect the stdio to a window?
83 self.stdioWin = None
84 self.saveStdio = (_sys.stdout, _sys.stderr)
85 if redirect:
86 self.RedirectStdio(filename)
87
88 # This finishes the initialization of wxWindows and then calls
89 # the OnInit that should be present in the derived class
90 self._BootstrapApp()
91
92
93 def __del__(self):
94 try:
95 self.RestoreStdio() # Just in case the MainLoop was overridden
96 except:
97 pass
98
99
100 def SetTopWindow(self, frame):
101 if self.stdioWin:
102 self.stdioWin.SetParent(frame)
103 wx.PyApp.SetTopWindow(self, frame)
104
105
106 def MainLoop(self):
107 wx.PyApp.MainLoop(self)
108 self.RestoreStdio()
109
110
111 def RedirectStdio(self, filename):
112 if filename:
113 _sys.stdout = _sys.stderr = open(filename, 'a')
114 else:
115 self.stdioWin = self.outputWindowClass()
116 _sys.stdout = _sys.stderr = self.stdioWin
117
118
119 def RestoreStdio(self):
120 _sys.stdout, _sys.stderr = self.saveStdio
121
122
123
124# change from wxPyApp_ to wxApp_
125App_GetMacSupportPCMenuShortcuts = _core.PyApp_GetMacSupportPCMenuShortcuts
126App_GetMacAboutMenuItemId = _core.PyApp_GetMacAboutMenuItemId
127App_GetMacPreferencesMenuItemId = _core.PyApp_GetMacPreferencesMenuItemId
128App_GetMacExitMenuItemId = _core.PyApp_GetMacExitMenuItemId
129App_GetMacHelpMenuTitleName = _core.PyApp_GetMacHelpMenuTitleName
130App_SetMacSupportPCMenuShortcuts = _core.PyApp_SetMacSupportPCMenuShortcuts
131App_SetMacAboutMenuItemId = _core.PyApp_SetMacAboutMenuItemId
132App_SetMacPreferencesMenuItemId = _core.PyApp_SetMacPreferencesMenuItemId
133App_SetMacExitMenuItemId = _core.PyApp_SetMacExitMenuItemId
134App_SetMacHelpMenuTitleName = _core.PyApp_SetMacHelpMenuTitleName
135App_GetComCtl32Version = _core.PyApp_GetComCtl32Version
136
137#----------------------------------------------------------------------------
138
139class PySimpleApp(wx.App):
140 def __init__(self, redirect=False, filename=None):
141 wx.App.__init__(self, redirect, filename)
142 def OnInit(self):
143 wx.InitAllImageHandlers()
144 return True
145
146
147class PyWidgetTester(wx.App):
148 def __init__(self, size = (250, 100)):
149 self.size = size
150 wx.App.__init__(self, 0)
151
152 def OnInit(self):
153 self.frame = wxFrame(None, -1, "Widget Tester", pos=(0,0), size=self.size)
154 self.SetTopWindow(self.frame)
155 return True
156
157 def SetWidget(self, widgetClass, *args):
158 w = widgetClass(self.frame, *args)
159 self.frame.Show(True)
160
161#----------------------------------------------------------------------------
162# DO NOT hold any other references to this object. This is how we
163# know when to cleanup system resources that wxWin is holding. When
164# the sys module is unloaded, the refcount on sys.__wxPythonCleanup
165# goes to zero and it calls the wxApp_CleanUp function.
166
167class __wxPyCleanup:
168 def __init__(self):
169 self.cleanup = _core.App_CleanUp
170 def __del__(self):
171 self.cleanup()
172
173_sys.__wxPythonCleanup = __wxPyCleanup()
174
175## # another possible solution, but it gets called too early...
176## if sys.version[0] == '2':
177## import atexit
178## atexit.register(_core.wxApp_CleanUp)
179## else:
180## sys.exitfunc = _core.wxApp_CleanUp
181
182
183#----------------------------------------------------------------------------