##// END OF EJS Templates
put back null gui hook for osx...
MinRK -
Show More
@@ -1,577 +1,584 b''
1 # coding: utf-8
1 # coding: utf-8
2 """
2 """
3 Inputhook management for GUI event loop integration.
3 Inputhook management for GUI event loop integration.
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 try:
17 try:
18 import ctypes
18 import ctypes
19 except ImportError:
19 except ImportError:
20 ctypes = None
20 ctypes = None
21 except SystemError: # IronPython issue, 2/8/2014
21 except SystemError: # IronPython issue, 2/8/2014
22 ctypes = None
22 ctypes = None
23 import os
23 import os
24 import sys
24 import sys
25 from distutils.version import LooseVersion as V
25 from distutils.version import LooseVersion as V
26
26
27 from IPython.utils.warn import warn
27 from IPython.utils.warn import warn
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Constants
30 # Constants
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 # Constants for identifying the GUI toolkits.
33 # Constants for identifying the GUI toolkits.
34 GUI_WX = 'wx'
34 GUI_WX = 'wx'
35 GUI_QT = 'qt'
35 GUI_QT = 'qt'
36 GUI_QT4 = 'qt4'
36 GUI_QT4 = 'qt4'
37 GUI_GTK = 'gtk'
37 GUI_GTK = 'gtk'
38 GUI_TK = 'tk'
38 GUI_TK = 'tk'
39 GUI_OSX = 'osx'
39 GUI_OSX = 'osx'
40 GUI_GLUT = 'glut'
40 GUI_GLUT = 'glut'
41 GUI_PYGLET = 'pyglet'
41 GUI_PYGLET = 'pyglet'
42 GUI_GTK3 = 'gtk3'
42 GUI_GTK3 = 'gtk3'
43 GUI_NONE = 'none' # i.e. disable
43 GUI_NONE = 'none' # i.e. disable
44
44
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46 # Utilities
46 # Utilities
47 #-----------------------------------------------------------------------------
47 #-----------------------------------------------------------------------------
48
48
49 def _stdin_ready_posix():
49 def _stdin_ready_posix():
50 """Return True if there's something to read on stdin (posix version)."""
50 """Return True if there's something to read on stdin (posix version)."""
51 infds, outfds, erfds = select.select([sys.stdin],[],[],0)
51 infds, outfds, erfds = select.select([sys.stdin],[],[],0)
52 return bool(infds)
52 return bool(infds)
53
53
54 def _stdin_ready_nt():
54 def _stdin_ready_nt():
55 """Return True if there's something to read on stdin (nt version)."""
55 """Return True if there's something to read on stdin (nt version)."""
56 return msvcrt.kbhit()
56 return msvcrt.kbhit()
57
57
58 def _stdin_ready_other():
58 def _stdin_ready_other():
59 """Return True, assuming there's something to read on stdin."""
59 """Return True, assuming there's something to read on stdin."""
60 return True #
60 return True #
61
61
62
62
63 def _ignore_CTRL_C_posix():
63 def _ignore_CTRL_C_posix():
64 """Ignore CTRL+C (SIGINT)."""
64 """Ignore CTRL+C (SIGINT)."""
65 signal.signal(signal.SIGINT, signal.SIG_IGN)
65 signal.signal(signal.SIGINT, signal.SIG_IGN)
66
66
67 def _allow_CTRL_C_posix():
67 def _allow_CTRL_C_posix():
68 """Take CTRL+C into account (SIGINT)."""
68 """Take CTRL+C into account (SIGINT)."""
69 signal.signal(signal.SIGINT, signal.default_int_handler)
69 signal.signal(signal.SIGINT, signal.default_int_handler)
70
70
71 def _ignore_CTRL_C_other():
71 def _ignore_CTRL_C_other():
72 """Ignore CTRL+C (not implemented)."""
72 """Ignore CTRL+C (not implemented)."""
73 pass
73 pass
74
74
75 def _allow_CTRL_C_other():
75 def _allow_CTRL_C_other():
76 """Take CTRL+C into account (not implemented)."""
76 """Take CTRL+C into account (not implemented)."""
77 pass
77 pass
78
78
79 if os.name == 'posix':
79 if os.name == 'posix':
80 import select
80 import select
81 import signal
81 import signal
82 stdin_ready = _stdin_ready_posix
82 stdin_ready = _stdin_ready_posix
83 ignore_CTRL_C = _ignore_CTRL_C_posix
83 ignore_CTRL_C = _ignore_CTRL_C_posix
84 allow_CTRL_C = _allow_CTRL_C_posix
84 allow_CTRL_C = _allow_CTRL_C_posix
85 elif os.name == 'nt':
85 elif os.name == 'nt':
86 import msvcrt
86 import msvcrt
87 stdin_ready = _stdin_ready_nt
87 stdin_ready = _stdin_ready_nt
88 ignore_CTRL_C = _ignore_CTRL_C_other
88 ignore_CTRL_C = _ignore_CTRL_C_other
89 allow_CTRL_C = _allow_CTRL_C_other
89 allow_CTRL_C = _allow_CTRL_C_other
90 else:
90 else:
91 stdin_ready = _stdin_ready_other
91 stdin_ready = _stdin_ready_other
92 ignore_CTRL_C = _ignore_CTRL_C_other
92 ignore_CTRL_C = _ignore_CTRL_C_other
93 allow_CTRL_C = _allow_CTRL_C_other
93 allow_CTRL_C = _allow_CTRL_C_other
94
94
95
95
96 #-----------------------------------------------------------------------------
96 #-----------------------------------------------------------------------------
97 # Main InputHookManager class
97 # Main InputHookManager class
98 #-----------------------------------------------------------------------------
98 #-----------------------------------------------------------------------------
99
99
100
100
101 class InputHookManager(object):
101 class InputHookManager(object):
102 """Manage PyOS_InputHook for different GUI toolkits.
102 """Manage PyOS_InputHook for different GUI toolkits.
103
103
104 This class installs various hooks under ``PyOSInputHook`` to handle
104 This class installs various hooks under ``PyOSInputHook`` to handle
105 GUI event loop integration.
105 GUI event loop integration.
106 """
106 """
107
107
108 def __init__(self):
108 def __init__(self):
109 if ctypes is None:
109 if ctypes is None:
110 warn("IPython GUI event loop requires ctypes, %gui will not be available")
110 warn("IPython GUI event loop requires ctypes, %gui will not be available")
111 return
111 return
112 self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int)
112 self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int)
113 self.guihooks = {}
113 self.guihooks = {}
114 self.aliases = {}
114 self.aliases = {}
115 self.apps = {}
115 self.apps = {}
116 self._reset()
116 self._reset()
117
117
118 def _reset(self):
118 def _reset(self):
119 self._callback_pyfunctype = None
119 self._callback_pyfunctype = None
120 self._callback = None
120 self._callback = None
121 self._installed = False
121 self._installed = False
122 self._current_gui = None
122 self._current_gui = None
123
123
124 def get_pyos_inputhook(self):
124 def get_pyos_inputhook(self):
125 """Return the current PyOS_InputHook as a ctypes.c_void_p."""
125 """Return the current PyOS_InputHook as a ctypes.c_void_p."""
126 return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook")
126 return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook")
127
127
128 def get_pyos_inputhook_as_func(self):
128 def get_pyos_inputhook_as_func(self):
129 """Return the current PyOS_InputHook as a ctypes.PYFUNCYPE."""
129 """Return the current PyOS_InputHook as a ctypes.PYFUNCYPE."""
130 return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook")
130 return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook")
131
131
132 def set_inputhook(self, callback):
132 def set_inputhook(self, callback):
133 """Set PyOS_InputHook to callback and return the previous one."""
133 """Set PyOS_InputHook to callback and return the previous one."""
134 # On platforms with 'readline' support, it's all too likely to
134 # On platforms with 'readline' support, it's all too likely to
135 # have a KeyboardInterrupt signal delivered *even before* an
135 # have a KeyboardInterrupt signal delivered *even before* an
136 # initial ``try:`` clause in the callback can be executed, so
136 # initial ``try:`` clause in the callback can be executed, so
137 # we need to disable CTRL+C in this situation.
137 # we need to disable CTRL+C in this situation.
138 ignore_CTRL_C()
138 ignore_CTRL_C()
139 self._callback = callback
139 self._callback = callback
140 self._callback_pyfunctype = self.PYFUNC(callback)
140 self._callback_pyfunctype = self.PYFUNC(callback)
141 pyos_inputhook_ptr = self.get_pyos_inputhook()
141 pyos_inputhook_ptr = self.get_pyos_inputhook()
142 original = self.get_pyos_inputhook_as_func()
142 original = self.get_pyos_inputhook_as_func()
143 pyos_inputhook_ptr.value = \
143 pyos_inputhook_ptr.value = \
144 ctypes.cast(self._callback_pyfunctype, ctypes.c_void_p).value
144 ctypes.cast(self._callback_pyfunctype, ctypes.c_void_p).value
145 self._installed = True
145 self._installed = True
146 return original
146 return original
147
147
148 def clear_inputhook(self, app=None):
148 def clear_inputhook(self, app=None):
149 """Set PyOS_InputHook to NULL and return the previous one.
149 """Set PyOS_InputHook to NULL and return the previous one.
150
150
151 Parameters
151 Parameters
152 ----------
152 ----------
153 app : optional, ignored
153 app : optional, ignored
154 This parameter is allowed only so that clear_inputhook() can be
154 This parameter is allowed only so that clear_inputhook() can be
155 called with a similar interface as all the ``enable_*`` methods. But
155 called with a similar interface as all the ``enable_*`` methods. But
156 the actual value of the parameter is ignored. This uniform interface
156 the actual value of the parameter is ignored. This uniform interface
157 makes it easier to have user-level entry points in the main IPython
157 makes it easier to have user-level entry points in the main IPython
158 app like :meth:`enable_gui`."""
158 app like :meth:`enable_gui`."""
159 pyos_inputhook_ptr = self.get_pyos_inputhook()
159 pyos_inputhook_ptr = self.get_pyos_inputhook()
160 original = self.get_pyos_inputhook_as_func()
160 original = self.get_pyos_inputhook_as_func()
161 pyos_inputhook_ptr.value = ctypes.c_void_p(None).value
161 pyos_inputhook_ptr.value = ctypes.c_void_p(None).value
162 allow_CTRL_C()
162 allow_CTRL_C()
163 self._reset()
163 self._reset()
164 return original
164 return original
165
165
166 def clear_app_refs(self, gui=None):
166 def clear_app_refs(self, gui=None):
167 """Clear IPython's internal reference to an application instance.
167 """Clear IPython's internal reference to an application instance.
168
168
169 Whenever we create an app for a user on qt4 or wx, we hold a
169 Whenever we create an app for a user on qt4 or wx, we hold a
170 reference to the app. This is needed because in some cases bad things
170 reference to the app. This is needed because in some cases bad things
171 can happen if a user doesn't hold a reference themselves. This
171 can happen if a user doesn't hold a reference themselves. This
172 method is provided to clear the references we are holding.
172 method is provided to clear the references we are holding.
173
173
174 Parameters
174 Parameters
175 ----------
175 ----------
176 gui : None or str
176 gui : None or str
177 If None, clear all app references. If ('wx', 'qt4') clear
177 If None, clear all app references. If ('wx', 'qt4') clear
178 the app for that toolkit. References are not held for gtk or tk
178 the app for that toolkit. References are not held for gtk or tk
179 as those toolkits don't have the notion of an app.
179 as those toolkits don't have the notion of an app.
180 """
180 """
181 if gui is None:
181 if gui is None:
182 self.apps = {}
182 self.apps = {}
183 elif gui in self.apps:
183 elif gui in self.apps:
184 del self.apps[gui]
184 del self.apps[gui]
185
185
186 def register(self, toolkitname, *aliases):
186 def register(self, toolkitname, *aliases):
187 """Register a class to provide the event loop for a given GUI.
187 """Register a class to provide the event loop for a given GUI.
188
188
189 This is intended to be used as a class decorator. It should be passed
189 This is intended to be used as a class decorator. It should be passed
190 the names with which to register this GUI integration. The classes
190 the names with which to register this GUI integration. The classes
191 themselves should subclass :class:`InputHookBase`.
191 themselves should subclass :class:`InputHookBase`.
192
192
193 ::
193 ::
194
194
195 @inputhook_manager.register('qt')
195 @inputhook_manager.register('qt')
196 class QtInputHook(InputHookBase):
196 class QtInputHook(InputHookBase):
197 def enable(self, app=None):
197 def enable(self, app=None):
198 ...
198 ...
199 """
199 """
200 def decorator(cls):
200 def decorator(cls):
201 inst = cls(self)
201 inst = cls(self)
202 self.guihooks[toolkitname] = inst
202 self.guihooks[toolkitname] = inst
203 for a in aliases:
203 for a in aliases:
204 self.aliases[a] = toolkitname
204 self.aliases[a] = toolkitname
205 return cls
205 return cls
206 return decorator
206 return decorator
207
207
208 def current_gui(self):
208 def current_gui(self):
209 """Return a string indicating the currently active GUI or None."""
209 """Return a string indicating the currently active GUI or None."""
210 return self._current_gui
210 return self._current_gui
211
211
212 def enable_gui(self, gui=None, app=None):
212 def enable_gui(self, gui=None, app=None):
213 """Switch amongst GUI input hooks by name.
213 """Switch amongst GUI input hooks by name.
214
214
215 This is a higher level method than :meth:`set_inputhook` - it uses the
215 This is a higher level method than :meth:`set_inputhook` - it uses the
216 GUI name to look up a registered object which enables the input hook
216 GUI name to look up a registered object which enables the input hook
217 for that GUI.
217 for that GUI.
218
218
219 Parameters
219 Parameters
220 ----------
220 ----------
221 gui : optional, string or None
221 gui : optional, string or None
222 If None (or 'none'), clears input hook, otherwise it must be one
222 If None (or 'none'), clears input hook, otherwise it must be one
223 of the recognized GUI names (see ``GUI_*`` constants in module).
223 of the recognized GUI names (see ``GUI_*`` constants in module).
224
224
225 app : optional, existing application object.
225 app : optional, existing application object.
226 For toolkits that have the concept of a global app, you can supply an
226 For toolkits that have the concept of a global app, you can supply an
227 existing one. If not given, the toolkit will be probed for one, and if
227 existing one. If not given, the toolkit will be probed for one, and if
228 none is found, a new one will be created. Note that GTK does not have
228 none is found, a new one will be created. Note that GTK does not have
229 this concept, and passing an app if ``gui=="GTK"`` will raise an error.
229 this concept, and passing an app if ``gui=="GTK"`` will raise an error.
230
230
231 Returns
231 Returns
232 -------
232 -------
233 The output of the underlying gui switch routine, typically the actual
233 The output of the underlying gui switch routine, typically the actual
234 PyOS_InputHook wrapper object or the GUI toolkit app created, if there was
234 PyOS_InputHook wrapper object or the GUI toolkit app created, if there was
235 one.
235 one.
236 """
236 """
237 if gui in (None, GUI_NONE):
237 if gui in (None, GUI_NONE):
238 return self.disable_gui()
238 return self.disable_gui()
239
239
240 if gui in self.aliases:
240 if gui in self.aliases:
241 return self.enable_gui(self.aliases[gui], app)
241 return self.enable_gui(self.aliases[gui], app)
242
242
243 try:
243 try:
244 gui_hook = self.guihooks[gui]
244 gui_hook = self.guihooks[gui]
245 except KeyError:
245 except KeyError:
246 e = "Invalid GUI request {!r}, valid ones are: {}"
246 e = "Invalid GUI request {!r}, valid ones are: {}"
247 raise ValueError(e.format(gui, ', '.join(self.guihooks)))
247 raise ValueError(e.format(gui, ', '.join(self.guihooks)))
248 self._current_gui = gui
248 self._current_gui = gui
249
249
250 app = gui_hook.enable(app)
250 app = gui_hook.enable(app)
251 if app is not None:
251 if app is not None:
252 app._in_event_loop = True
252 app._in_event_loop = True
253 self.apps[gui] = app
253 self.apps[gui] = app
254 return app
254 return app
255
255
256 def disable_gui(self):
256 def disable_gui(self):
257 """Disable GUI event loop integration.
257 """Disable GUI event loop integration.
258
258
259 If an application was registered, this sets its ``_in_event_loop``
259 If an application was registered, this sets its ``_in_event_loop``
260 attribute to False. It then calls :meth:`clear_inputhook`.
260 attribute to False. It then calls :meth:`clear_inputhook`.
261 """
261 """
262 gui = self._current_gui
262 gui = self._current_gui
263 if gui in self.apps:
263 if gui in self.apps:
264 self.apps[gui]._in_event_loop = False
264 self.apps[gui]._in_event_loop = False
265 return self.clear_inputhook()
265 return self.clear_inputhook()
266
266
267 class InputHookBase(object):
267 class InputHookBase(object):
268 """Base class for input hooks for specific toolkits.
268 """Base class for input hooks for specific toolkits.
269
269
270 Subclasses should define an :meth:`enable` method with one argument, ``app``,
270 Subclasses should define an :meth:`enable` method with one argument, ``app``,
271 which will either be an instance of the toolkit's application class, or None.
271 which will either be an instance of the toolkit's application class, or None.
272 They may also define a :meth:`disable` method with no arguments.
272 They may also define a :meth:`disable` method with no arguments.
273 """
273 """
274 def __init__(self, manager):
274 def __init__(self, manager):
275 self.manager = manager
275 self.manager = manager
276
276
277 def disable(self):
277 def disable(self):
278 pass
278 pass
279
279
280 inputhook_manager = InputHookManager()
280 inputhook_manager = InputHookManager()
281
281
282 @inputhook_manager.register('osx')
283 class NullInputHook(InputHookBase):
284 """A null inputhook that doesn't need to do anything"""
285 def enable(self, app=None):
286 pass
287
282 @inputhook_manager.register('wx')
288 @inputhook_manager.register('wx')
283 class WxInputHook(InputHookBase):
289 class WxInputHook(InputHookBase):
284 def enable(self, app=None):
290 def enable(self, app=None):
285 """Enable event loop integration with wxPython.
291 """Enable event loop integration with wxPython.
286
292
287 Parameters
293 Parameters
288 ----------
294 ----------
289 app : WX Application, optional.
295 app : WX Application, optional.
290 Running application to use. If not given, we probe WX for an
296 Running application to use. If not given, we probe WX for an
291 existing application object, and create a new one if none is found.
297 existing application object, and create a new one if none is found.
292
298
293 Notes
299 Notes
294 -----
300 -----
295 This methods sets the ``PyOS_InputHook`` for wxPython, which allows
301 This methods sets the ``PyOS_InputHook`` for wxPython, which allows
296 the wxPython to integrate with terminal based applications like
302 the wxPython to integrate with terminal based applications like
297 IPython.
303 IPython.
298
304
299 If ``app`` is not given we probe for an existing one, and return it if
305 If ``app`` is not given we probe for an existing one, and return it if
300 found. If no existing app is found, we create an :class:`wx.App` as
306 found. If no existing app is found, we create an :class:`wx.App` as
301 follows::
307 follows::
302
308
303 import wx
309 import wx
304 app = wx.App(redirect=False, clearSigInt=False)
310 app = wx.App(redirect=False, clearSigInt=False)
305 """
311 """
306 import wx
312 import wx
307
313
308 wx_version = V(wx.__version__).version
314 wx_version = V(wx.__version__).version
309
315
310 if wx_version < [2, 8]:
316 if wx_version < [2, 8]:
311 raise ValueError("requires wxPython >= 2.8, but you have %s" % wx.__version__)
317 raise ValueError("requires wxPython >= 2.8, but you have %s" % wx.__version__)
312
318
313 from IPython.lib.inputhookwx import inputhook_wx
319 from IPython.lib.inputhookwx import inputhook_wx
314 from IPython.external.appnope import nope
320 from IPython.external.appnope import nope
315 self.manager.set_inputhook(inputhook_wx)
321 self.manager.set_inputhook(inputhook_wx)
316 nope()
322 nope()
317
323
318 import wx
324 import wx
319 if app is None:
325 if app is None:
320 app = wx.GetApp()
326 app = wx.GetApp()
321 if app is None:
327 if app is None:
322 app = wx.App(redirect=False, clearSigInt=False)
328 app = wx.App(redirect=False, clearSigInt=False)
323
329
324 return app
330 return app
325
331
326 def disable(self):
332 def disable(self):
327 """Disable event loop integration with wxPython.
333 """Disable event loop integration with wxPython.
328
334
329 This restores appnapp on OS X
335 This restores appnapp on OS X
330 """
336 """
331 from IPython.external.appnope import nap
337 from IPython.external.appnope import nap
332 nap()
338 nap()
333
339
334 @inputhook_manager.register('qt', 'qt4')
340 @inputhook_manager.register('qt', 'qt4')
335 class Qt4InputHook(InputHookBase):
341 class Qt4InputHook(InputHookBase):
336 def enable(self, app=None):
342 def enable(self, app=None):
337 """Enable event loop integration with PyQt4.
343 """Enable event loop integration with PyQt4.
338
344
339 Parameters
345 Parameters
340 ----------
346 ----------
341 app : Qt Application, optional.
347 app : Qt Application, optional.
342 Running application to use. If not given, we probe Qt for an
348 Running application to use. If not given, we probe Qt for an
343 existing application object, and create a new one if none is found.
349 existing application object, and create a new one if none is found.
344
350
345 Notes
351 Notes
346 -----
352 -----
347 This methods sets the PyOS_InputHook for PyQt4, which allows
353 This methods sets the PyOS_InputHook for PyQt4, which allows
348 the PyQt4 to integrate with terminal based applications like
354 the PyQt4 to integrate with terminal based applications like
349 IPython.
355 IPython.
350
356
351 If ``app`` is not given we probe for an existing one, and return it if
357 If ``app`` is not given we probe for an existing one, and return it if
352 found. If no existing app is found, we create an :class:`QApplication`
358 found. If no existing app is found, we create an :class:`QApplication`
353 as follows::
359 as follows::
354
360
355 from PyQt4 import QtCore
361 from PyQt4 import QtCore
356 app = QtGui.QApplication(sys.argv)
362 app = QtGui.QApplication(sys.argv)
357 """
363 """
358 from IPython.lib.inputhookqt4 import create_inputhook_qt4
364 from IPython.lib.inputhookqt4 import create_inputhook_qt4
359 from IPython.external.appnope import nope
365 from IPython.external.appnope import nope
360 app, inputhook_qt4 = create_inputhook_qt4(self, app)
366 app, inputhook_qt4 = create_inputhook_qt4(self, app)
361 self.manager.set_inputhook(inputhook_qt4)
367 self.manager.set_inputhook(inputhook_qt4)
362 nope()
368 nope()
363
369
364 return app
370 return app
365
371
366 def disable_qt4(self):
372 def disable_qt4(self):
367 """Disable event loop integration with PyQt4.
373 """Disable event loop integration with PyQt4.
368
374
369 This restores appnapp on OS X
375 This restores appnapp on OS X
370 """
376 """
371 from IPython.external.appnope import nap
377 from IPython.external.appnope import nap
372 nap()
378 nap()
373
379
374
380
375 @inputhook_manager.register('qt5')
381 @inputhook_manager.register('qt5')
376 class Qt5InputHook(Qt4InputHook):
382 class Qt5InputHook(Qt4InputHook):
377 def enable(self, app=None):
383 def enable(self, app=None):
378 os.environ['QT_API'] = 'pyqt5'
384 os.environ['QT_API'] = 'pyqt5'
379 return Qt4InputHook.enable(self, app)
385 return Qt4InputHook.enable(self, app)
380
386
381
387
382 @inputhook_manager.register('gtk')
388 @inputhook_manager.register('gtk')
383 class GtkInputHook(InputHookBase):
389 class GtkInputHook(InputHookBase):
384 def enable(self, app=None):
390 def enable(self, app=None):
385 """Enable event loop integration with PyGTK.
391 """Enable event loop integration with PyGTK.
386
392
387 Parameters
393 Parameters
388 ----------
394 ----------
389 app : ignored
395 app : ignored
390 Ignored, it's only a placeholder to keep the call signature of all
396 Ignored, it's only a placeholder to keep the call signature of all
391 gui activation methods consistent, which simplifies the logic of
397 gui activation methods consistent, which simplifies the logic of
392 supporting magics.
398 supporting magics.
393
399
394 Notes
400 Notes
395 -----
401 -----
396 This methods sets the PyOS_InputHook for PyGTK, which allows
402 This methods sets the PyOS_InputHook for PyGTK, which allows
397 the PyGTK to integrate with terminal based applications like
403 the PyGTK to integrate with terminal based applications like
398 IPython.
404 IPython.
399 """
405 """
400 import gtk
406 import gtk
401 try:
407 try:
402 gtk.set_interactive(True)
408 gtk.set_interactive(True)
403 except AttributeError:
409 except AttributeError:
404 # For older versions of gtk, use our own ctypes version
410 # For older versions of gtk, use our own ctypes version
405 from IPython.lib.inputhookgtk import inputhook_gtk
411 from IPython.lib.inputhookgtk import inputhook_gtk
406 self.manager.set_inputhook(inputhook_gtk)
412 self.manager.set_inputhook(inputhook_gtk)
407
413
408
414
409 @inputhook_manager.register('tk')
415 @inputhook_manager.register('tk')
410 class TkInputHook(InputHookBase):
416 class TkInputHook(InputHookBase):
411 def enable(self, app=None):
417 def enable(self, app=None):
412 """Enable event loop integration with Tk.
418 """Enable event loop integration with Tk.
413
419
414 Parameters
420 Parameters
415 ----------
421 ----------
416 app : toplevel :class:`Tkinter.Tk` widget, optional.
422 app : toplevel :class:`Tkinter.Tk` widget, optional.
417 Running toplevel widget to use. If not given, we probe Tk for an
423 Running toplevel widget to use. If not given, we probe Tk for an
418 existing one, and create a new one if none is found.
424 existing one, and create a new one if none is found.
419
425
420 Notes
426 Notes
421 -----
427 -----
422 If you have already created a :class:`Tkinter.Tk` object, the only
428 If you have already created a :class:`Tkinter.Tk` object, the only
423 thing done by this method is to register with the
429 thing done by this method is to register with the
424 :class:`InputHookManager`, since creating that object automatically
430 :class:`InputHookManager`, since creating that object automatically
425 sets ``PyOS_InputHook``.
431 sets ``PyOS_InputHook``.
426 """
432 """
427 if app is None:
433 if app is None:
428 try:
434 try:
429 from tkinter import Tk # Py 3
435 from tkinter import Tk # Py 3
430 except ImportError:
436 except ImportError:
431 from Tkinter import Tk # Py 2
437 from Tkinter import Tk # Py 2
432 app = Tk()
438 app = Tk()
433 app.withdraw()
439 app.withdraw()
434 self.manager.apps[GUI_TK] = app
440 self.manager.apps[GUI_TK] = app
435 return app
441 return app
436
442
437
443
438 @inputhook_manager.register('glut')
444 @inputhook_manager.register('glut')
439 class GlutInputHook(InputHookBase):
445 class GlutInputHook(InputHookBase):
440 def enable(self, app=None):
446 def enable(self, app=None):
441 """Enable event loop integration with GLUT.
447 """Enable event loop integration with GLUT.
442
448
443 Parameters
449 Parameters
444 ----------
450 ----------
445
451
446 app : ignored
452 app : ignored
447 Ignored, it's only a placeholder to keep the call signature of all
453 Ignored, it's only a placeholder to keep the call signature of all
448 gui activation methods consistent, which simplifies the logic of
454 gui activation methods consistent, which simplifies the logic of
449 supporting magics.
455 supporting magics.
450
456
451 Notes
457 Notes
452 -----
458 -----
453
459
454 This methods sets the PyOS_InputHook for GLUT, which allows the GLUT to
460 This methods sets the PyOS_InputHook for GLUT, which allows the GLUT to
455 integrate with terminal based applications like IPython. Due to GLUT
461 integrate with terminal based applications like IPython. Due to GLUT
456 limitations, it is currently not possible to start the event loop
462 limitations, it is currently not possible to start the event loop
457 without first creating a window. You should thus not create another
463 without first creating a window. You should thus not create another
458 window but use instead the created one. See 'gui-glut.py' in the
464 window but use instead the created one. See 'gui-glut.py' in the
459 docs/examples/lib directory.
465 docs/examples/lib directory.
460
466
461 The default screen mode is set to:
467 The default screen mode is set to:
462 glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH
468 glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH
463 """
469 """
464
470
465 import OpenGL.GLUT as glut
471 import OpenGL.GLUT as glut
466 from IPython.lib.inputhookglut import glut_display_mode, \
472 from IPython.lib.inputhookglut import glut_display_mode, \
467 glut_close, glut_display, \
473 glut_close, glut_display, \
468 glut_idle, inputhook_glut
474 glut_idle, inputhook_glut
469
475
470 if GUI_GLUT not in self.manager.apps:
476 if GUI_GLUT not in self.manager.apps:
471 glut.glutInit( sys.argv )
477 glut.glutInit( sys.argv )
472 glut.glutInitDisplayMode( glut_display_mode )
478 glut.glutInitDisplayMode( glut_display_mode )
473 # This is specific to freeglut
479 # This is specific to freeglut
474 if bool(glut.glutSetOption):
480 if bool(glut.glutSetOption):
475 glut.glutSetOption( glut.GLUT_ACTION_ON_WINDOW_CLOSE,
481 glut.glutSetOption( glut.GLUT_ACTION_ON_WINDOW_CLOSE,
476 glut.GLUT_ACTION_GLUTMAINLOOP_RETURNS )
482 glut.GLUT_ACTION_GLUTMAINLOOP_RETURNS )
477 glut.glutCreateWindow( sys.argv[0] )
483 glut.glutCreateWindow( sys.argv[0] )
478 glut.glutReshapeWindow( 1, 1 )
484 glut.glutReshapeWindow( 1, 1 )
479 glut.glutHideWindow( )
485 glut.glutHideWindow( )
480 glut.glutWMCloseFunc( glut_close )
486 glut.glutWMCloseFunc( glut_close )
481 glut.glutDisplayFunc( glut_display )
487 glut.glutDisplayFunc( glut_display )
482 glut.glutIdleFunc( glut_idle )
488 glut.glutIdleFunc( glut_idle )
483 else:
489 else:
484 glut.glutWMCloseFunc( glut_close )
490 glut.glutWMCloseFunc( glut_close )
485 glut.glutDisplayFunc( glut_display )
491 glut.glutDisplayFunc( glut_display )
486 glut.glutIdleFunc( glut_idle)
492 glut.glutIdleFunc( glut_idle)
487 self.manager.set_inputhook( inputhook_glut )
493 self.manager.set_inputhook( inputhook_glut )
488
494
489
495
490 def disable(self):
496 def disable(self):
491 """Disable event loop integration with glut.
497 """Disable event loop integration with glut.
492
498
493 This sets PyOS_InputHook to NULL and set the display function to a
499 This sets PyOS_InputHook to NULL and set the display function to a
494 dummy one and set the timer to a dummy timer that will be triggered
500 dummy one and set the timer to a dummy timer that will be triggered
495 very far in the future.
501 very far in the future.
496 """
502 """
497 import OpenGL.GLUT as glut
503 import OpenGL.GLUT as glut
498 from glut_support import glutMainLoopEvent
504 from glut_support import glutMainLoopEvent
499
505
500 glut.glutHideWindow() # This is an event to be processed below
506 glut.glutHideWindow() # This is an event to be processed below
501 glutMainLoopEvent()
507 glutMainLoopEvent()
502 super(GlutInputHook, self).disable()
508 super(GlutInputHook, self).disable()
503
509
504 @inputhook_manager.register('pyglet')
510 @inputhook_manager.register('pyglet')
505 class PygletInputHook(InputHookBase):
511 class PygletInputHook(InputHookBase):
506 def enable(self, app=None):
512 def enable(self, app=None):
507 """Enable event loop integration with pyglet.
513 """Enable event loop integration with pyglet.
508
514
509 Parameters
515 Parameters
510 ----------
516 ----------
511 app : ignored
517 app : ignored
512 Ignored, it's only a placeholder to keep the call signature of all
518 Ignored, it's only a placeholder to keep the call signature of all
513 gui activation methods consistent, which simplifies the logic of
519 gui activation methods consistent, which simplifies the logic of
514 supporting magics.
520 supporting magics.
515
521
516 Notes
522 Notes
517 -----
523 -----
518 This methods sets the ``PyOS_InputHook`` for pyglet, which allows
524 This methods sets the ``PyOS_InputHook`` for pyglet, which allows
519 pyglet to integrate with terminal based applications like
525 pyglet to integrate with terminal based applications like
520 IPython.
526 IPython.
521
527
522 """
528 """
523 from IPython.lib.inputhookpyglet import inputhook_pyglet
529 from IPython.lib.inputhookpyglet import inputhook_pyglet
524 self.manager.set_inputhook(inputhook_pyglet)
530 self.manager.set_inputhook(inputhook_pyglet)
525 return app
531 return app
526
532
527
533
528 @inputhook_manager.register('gtk3')
534 @inputhook_manager.register('gtk3')
529 class Gtk3InputHook(InputHookBase):
535 class Gtk3InputHook(InputHookBase):
530 def enable(self, app=None):
536 def enable(self, app=None):
531 """Enable event loop integration with Gtk3 (gir bindings).
537 """Enable event loop integration with Gtk3 (gir bindings).
532
538
533 Parameters
539 Parameters
534 ----------
540 ----------
535 app : ignored
541 app : ignored
536 Ignored, it's only a placeholder to keep the call signature of all
542 Ignored, it's only a placeholder to keep the call signature of all
537 gui activation methods consistent, which simplifies the logic of
543 gui activation methods consistent, which simplifies the logic of
538 supporting magics.
544 supporting magics.
539
545
540 Notes
546 Notes
541 -----
547 -----
542 This methods sets the PyOS_InputHook for Gtk3, which allows
548 This methods sets the PyOS_InputHook for Gtk3, which allows
543 the Gtk3 to integrate with terminal based applications like
549 the Gtk3 to integrate with terminal based applications like
544 IPython.
550 IPython.
545 """
551 """
546 from IPython.lib.inputhookgtk3 import inputhook_gtk3
552 from IPython.lib.inputhookgtk3 import inputhook_gtk3
547 self.manager.set_inputhook(inputhook_gtk3)
553 self.manager.set_inputhook(inputhook_gtk3)
548
554
549
555
550 clear_inputhook = inputhook_manager.clear_inputhook
556 clear_inputhook = inputhook_manager.clear_inputhook
551 set_inputhook = inputhook_manager.set_inputhook
557 set_inputhook = inputhook_manager.set_inputhook
552 current_gui = inputhook_manager.current_gui
558 current_gui = inputhook_manager.current_gui
553 clear_app_refs = inputhook_manager.clear_app_refs
559 clear_app_refs = inputhook_manager.clear_app_refs
554 enable_gui = inputhook_manager.enable_gui
560 enable_gui = inputhook_manager.enable_gui
555 disable_gui = inputhook_manager.disable_gui
561 disable_gui = inputhook_manager.disable_gui
556 register = inputhook_manager.register
562 register = inputhook_manager.register
557 guis = inputhook_manager.guihooks
563 guis = inputhook_manager.guihooks
558
564
559 # Deprecated methods: kept for backwards compatibility, do not use in new code
565 # Deprecated methods: kept for backwards compatibility, do not use in new code
560 def _make_deprecated_enable(name):
566 def _make_deprecated_enable(name):
561 def enable_toolkit(app=None):
567 def enable_toolkit(app=None):
562 warn("This function is deprecated - use enable_gui(%r) instead" % name)
568 warn("This function is deprecated - use enable_gui(%r) instead" % name)
563 inputhook_manager.enable_gui(name, app)
569 inputhook_manager.enable_gui(name, app)
564
570
571 enable_osx = _make_deprecated_enable('osx')
565 enable_wx = _make_deprecated_enable('wx')
572 enable_wx = _make_deprecated_enable('wx')
566 enable_qt4 = _make_deprecated_enable('qt4')
573 enable_qt4 = _make_deprecated_enable('qt4')
567 enable_gtk = _make_deprecated_enable('gtk')
574 enable_gtk = _make_deprecated_enable('gtk')
568 enable_tk = _make_deprecated_enable('tk')
575 enable_tk = _make_deprecated_enable('tk')
569 enable_glut = _make_deprecated_enable('glut')
576 enable_glut = _make_deprecated_enable('glut')
570 enable_pyglet = _make_deprecated_enable('pyglet')
577 enable_pyglet = _make_deprecated_enable('pyglet')
571 enable_gtk3 = _make_deprecated_enable('gtk3')
578 enable_gtk3 = _make_deprecated_enable('gtk3')
572
579
573 def _deprecated_disable():
580 def _deprecated_disable():
574 warn("This function is deprecated: use disable_gui() instead")
581 warn("This function is deprecated: use disable_gui() instead")
575 inputhook_manager.disable_gui()
582 inputhook_manager.disable_gui()
576 disable_wx = disable_qt4 = disable_gtk = disable_gtk3 = disable_glut = \
583 disable_wx = disable_qt4 = disable_gtk = disable_gtk3 = disable_glut = \
577 disable_pyglet = _deprecated_disable
584 disable_pyglet = disable_osx = _deprecated_disable
General Comments 0
You need to be logged in to leave comments. Login now