inputhook.py
525 lines
| 17.5 KiB
| text/x-python
|
PythonLexer
Brian Granger
|
r2066 | #!/usr/bin/env python | ||
# encoding: utf-8 | ||||
""" | ||||
Inputhook management for GUI event loop integration. | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Copyright (C) 2008-2009 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
import ctypes | ||||
Brian Granger
|
r2195 | import sys | ||
Brian Granger
|
r2066 | |||
#----------------------------------------------------------------------------- | ||||
Brian Granger
|
r2214 | # Constants | ||
#----------------------------------------------------------------------------- | ||||
# Constants for identifying the GUI toolkits. | ||||
GUI_WX = 'wx' | ||||
GUI_QT4 = 'qt4' | ||||
GUI_GTK = 'gtk' | ||||
GUI_TK = 'tk' | ||||
#----------------------------------------------------------------------------- | ||||
# Utility classes | ||||
Brian Granger
|
r2066 | #----------------------------------------------------------------------------- | ||
Fernando Perez
|
r2213 | |||
Fernando Perez
|
r2212 | class _DummyMainloop(object): | ||
"""A special manager to hijack GUI mainloops that is mostly a no-op. | ||||
Brian Granger
|
r2214 | We are not using this class currently as it breaks GUI code that calls | ||
a mainloop function after the app has started to process pending events. | ||||
Fernando Perez
|
r2212 | """ | ||
def __init__(self, ml, ihm, gui_type): | ||||
self.ml = ml | ||||
self.ihm = ihm | ||||
self.gui_type = gui_type | ||||
Brian Granger
|
r2214 | |||
Fernando Perez
|
r2212 | def __call__(self, *args, **kw): | ||
if self.ihm.current_gui() == self.gui_type: | ||||
pass | ||||
else: | ||||
self.ml(*args, **kw) | ||||
Brian Granger
|
r2208 | |||
Brian Granger
|
r2066 | |||
Brian Granger
|
r2214 | #----------------------------------------------------------------------------- | ||
# Appstart and spin functions | ||||
#----------------------------------------------------------------------------- | ||||
def appstart_qt4(app): | ||||
"""Start the qt4 event loop in a way that plays with IPython. | ||||
When a qt4 app is run interactively in IPython, the event loop should | ||||
not be started. This function checks to see if IPython's qt4 integration | ||||
is activated and if so, it passes. If not, it will call the :meth:`exec_` | ||||
method of the main qt4 app. | ||||
This function should be used by users who want their qt4 scripts to work | ||||
both at the command line and in IPython. These users should put the | ||||
following logic at the bottom on their script, after they create a | ||||
:class:`QApplication` instance (called ``app`` here):: | ||||
try: | ||||
from IPython.lib.inputhook import appstart_qt4 | ||||
appstart_qt4(app) | ||||
except ImportError: | ||||
app.exec_() | ||||
""" | ||||
Brian Granger
|
r2210 | from PyQt4 import QtCore, QtGui | ||
Brian Granger
|
r2214 | assert isinstance(app, QtCore.QCoreApplication) | ||
if app is not None: | ||||
if current_gui() == GUI_QT4: | ||||
pass | ||||
else: | ||||
app.exec_() | ||||
def appstart_wx(app): | ||||
"""Start the wx event loop in a way that plays with IPython. | ||||
When a wx app is run interactively in IPython, the event loop should | ||||
not be started. This function checks to see if IPython's wx integration | ||||
is activated and if so, it passes. If not, it will call the | ||||
:meth:`MainLoop` method of the main qt4 app. | ||||
This function should be used by users who want their wx scripts to work | ||||
both at the command line and in IPython. These users should put the | ||||
following logic at the bottom on their script, after they create a | ||||
:class:`App` instance (called ``app`` here):: | ||||
try: | ||||
from IPython.lib.inputhook import appstart_wx | ||||
appstart_wx(app) | ||||
except ImportError: | ||||
app.MainLoop() | ||||
""" | ||||
import wx | ||||
assert isinstance(app, wx.App) | ||||
if app is not None: | ||||
if current_gui() == GUI_WX: | ||||
pass | ||||
else: | ||||
app.MainLoop() | ||||
Brian Granger
|
r2216 | def appstart_tk(app): | ||
"""Start the tk event loop in a way that plays with IPython. | ||||
When a tk app is run interactively in IPython, the event loop should | ||||
not be started. This function checks to see if IPython's tk integration | ||||
is activated and if so, it passes. If not, it will call the | ||||
:meth:`mainloop` method of the tk object passed to this method. | ||||
This function should be used by users who want their tk scripts to work | ||||
both at the command line and in IPython. These users should put the | ||||
following logic at the bottom on their script, after they create a | ||||
:class:`Tk` instance (called ``app`` here):: | ||||
try: | ||||
from IPython.lib.inputhook import appstart_tk | ||||
appstart_tk(app) | ||||
except ImportError: | ||||
app.mainloop() | ||||
""" | ||||
if app is not None: | ||||
if current_gui() == GUI_TK: | ||||
pass | ||||
else: | ||||
app.mainloop() | ||||
def appstart_gtk(): | ||||
"""Start the gtk event loop in a way that plays with IPython. | ||||
When a gtk app is run interactively in IPython, the event loop should | ||||
not be started. This function checks to see if IPython's gtk integration | ||||
is activated and if so, it passes. If not, it will call | ||||
:func:`gtk.main`. Unlike the other appstart implementations, this does | ||||
not take an ``app`` argument. | ||||
This function should be used by users who want their gtk scripts to work | ||||
both at the command line and in IPython. These users should put the | ||||
following logic at the bottom on their script:: | ||||
try: | ||||
from IPython.lib.inputhook import appstart_gtk | ||||
appstart_gtk() | ||||
except ImportError: | ||||
gtk.main() | ||||
""" | ||||
import gtk | ||||
if current_gui() == GUI_GTK: | ||||
pass | ||||
else: | ||||
gtk.main() | ||||
Brian Granger
|
r2214 | #----------------------------------------------------------------------------- | ||
# Main InputHookManager class | ||||
#----------------------------------------------------------------------------- | ||||
Brian Granger
|
r2210 | |||
Brian Granger
|
r2066 | class InputHookManager(object): | ||
Brian Granger
|
r2197 | """Manage PyOS_InputHook for different GUI toolkits. | ||
This class installs various hooks under ``PyOSInputHook`` to handle | ||||
GUI event loop integration. | ||||
""" | ||||
Brian Granger
|
r2066 | |||
def __init__(self): | ||||
self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int) | ||||
Brian Granger
|
r2208 | self._apps = {} | ||
Brian Granger
|
r2214 | self._spinner_dict = { | ||
GUI_QT4 : self._spin_qt4, | ||||
GUI_WX : self._spin_wx, | ||||
GUI_GTK : self._spin_gtk, | ||||
GUI_TK : self._spin_tk} | ||||
Brian Granger
|
r2066 | self._reset() | ||
def _reset(self): | ||||
self._callback_pyfunctype = None | ||||
self._callback = None | ||||
self._installed = False | ||||
Brian Granger
|
r2197 | self._current_gui = None | ||
Brian Granger
|
r2066 | |||
Brian Granger
|
r2208 | def _hijack_wx(self): | ||
Brian Granger
|
r2214 | """Hijack the wx mainloop so a user calling it won't cause badness. | ||
We are not currently using this as it breaks GUI code that calls a | ||||
mainloop at anytime but startup. | ||||
""" | ||||
Brian Granger
|
r2208 | import wx | ||
if hasattr(wx, '_core_'): core = getattr(wx, '_core_') | ||||
elif hasattr(wx, '_core'): core = getattr(wx, '_core') | ||||
else: raise AttributeError('Could not find wx core module') | ||||
orig_mainloop = core.PyApp_MainLoop | ||||
Fernando Perez
|
r2212 | core.PyApp_MainLoop = _DummyMainloop | ||
Brian Granger
|
r2208 | return orig_mainloop | ||
def _hijack_qt4(self): | ||||
Brian Granger
|
r2214 | """Hijack the qt4 mainloop so a user calling it won't cause badness. | ||
We are not currently using this as it breaks GUI code that calls a | ||||
mainloop at anytime but startup. | ||||
""" | ||||
Brian Granger
|
r2208 | from PyQt4 import QtGui, QtCore | ||
orig_mainloop = QtGui.qApp.exec_ | ||||
Brian Granger
|
r2214 | dumb_ml = _DummyMainloop(orig_mainloop, self, GUI_QT4) | ||
Fernando Perez
|
r2212 | QtGui.qApp.exec_ = dumb_ml | ||
QtGui.QApplication.exec_ = dumb_ml | ||||
QtCore.QCoreApplication.exec_ = dumb_ml | ||||
Brian Granger
|
r2208 | return orig_mainloop | ||
def _hijack_gtk(self): | ||||
Brian Granger
|
r2214 | """Hijack the gtk mainloop so a user calling it won't cause badness. | ||
We are not currently using this as it breaks GUI code that calls a | ||||
mainloop at anytime but startup. | ||||
""" | ||||
Brian Granger
|
r2208 | import gtk | ||
orig_mainloop = gtk.main | ||||
Brian Granger
|
r2214 | dumb_ml = _DummyMainloop(orig_mainloop, self, GUI_GTK) | ||
gtk.mainloop = dumb_ml | ||||
gtk.main = dumb_ml | ||||
Brian Granger
|
r2208 | return orig_mainloop | ||
def _hijack_tk(self): | ||||
Brian Granger
|
r2214 | """Hijack the tk mainloop so a user calling it won't cause badness. | ||
We are not currently using this as it breaks GUI code that calls a | ||||
mainloop at anytime but startup. | ||||
""" | ||||
Brian Granger
|
r2208 | import Tkinter | ||
Brian Granger
|
r2214 | orig_mainloop = gtk.main | ||
dumb_ml = _DummyMainloop(orig_mainloop, self, GUI_TK) | ||||
Tkinter.Misc.mainloop = dumb_ml | ||||
Tkinter.mainloop = dumb_ml | ||||
def _spin_qt4(self): | ||||
"""Process all pending events in the qt4 event loop. | ||||
This is for internal IPython use only and user code should not call this. | ||||
Instead, they should issue the raw GUI calls themselves. | ||||
""" | ||||
from PyQt4 import QtCore, QtGui | ||||
app = QtCore.QCoreApplication.instance() | ||||
if app is not None: | ||||
QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents) | ||||
def _spin_wx(self): | ||||
"""Process all pending events in the wx event loop. | ||||
This is for internal IPython use only and user code should not call this. | ||||
Instead, they should issue the raw GUI calls themselves. | ||||
""" | ||||
import wx | ||||
app = wx.GetApp() | ||||
if app is not None and wx.Thread_IsMain(): | ||||
evtloop = wx.EventLoop() | ||||
ea = wx.EventLoopActivator(evtloop) | ||||
while evtloop.Pending(): | ||||
evtloop.Dispatch() | ||||
app.ProcessIdle() | ||||
del ea | ||||
def _spin_gtk(self): | ||||
"""Process all pending events in the gtk event loop. | ||||
This is for internal IPython use only and user code should not call this. | ||||
Instead, they should issue the raw GUI calls themselves. | ||||
""" | ||||
Brian Granger
|
r2216 | import gtk | ||
gtk.gdk.threads_enter() | ||||
while gtk.events_pending(): | ||||
gtk.main_iteration(False) | ||||
gtk.gdk.flush() | ||||
gtk.gdk.threads_leave() | ||||
Brian Granger
|
r2214 | |||
def _spin_tk(self): | ||||
"""Process all pending events in the tk event loop. | ||||
This is for internal IPython use only and user code should not call this. | ||||
Instead, they should issue the raw GUI calls themselves. | ||||
""" | ||||
app = self._apps.get(GUI_TK) | ||||
if app is not None: | ||||
app.update() | ||||
def spin(self): | ||||
"""Process pending events in the current gui. | ||||
This method is just provided for IPython to use internally if needed | ||||
for things like testing. Third party projects should not call this | ||||
method, but instead should call the underlying GUI toolkit methods | ||||
that we are calling. | ||||
""" | ||||
spinner = self._spinner_dict.get(self._current_gui, lambda: None) | ||||
spinner() | ||||
Brian Granger
|
r2208 | |||
Brian Granger
|
r2066 | def get_pyos_inputhook(self): | ||
Brian Granger
|
r2209 | """Return the current PyOS_InputHook as a ctypes.c_void_p.""" | ||
Brian Granger
|
r2066 | return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook") | ||
def get_pyos_inputhook_as_func(self): | ||||
Brian Granger
|
r2209 | """Return the current PyOS_InputHook as a ctypes.PYFUNCYPE.""" | ||
Brian Granger
|
r2066 | return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook") | ||
Brian Granger
|
r2069 | def set_inputhook(self, callback): | ||
Brian Granger
|
r2209 | """Set PyOS_InputHook to callback and return the previous one.""" | ||
Brian Granger
|
r2066 | self._callback = callback | ||
self._callback_pyfunctype = self.PYFUNC(callback) | ||||
pyos_inputhook_ptr = self.get_pyos_inputhook() | ||||
original = self.get_pyos_inputhook_as_func() | ||||
pyos_inputhook_ptr.value = \ | ||||
ctypes.cast(self._callback_pyfunctype, ctypes.c_void_p).value | ||||
self._installed = True | ||||
return original | ||||
def clear_inputhook(self): | ||||
Brian Granger
|
r2209 | """Set PyOS_InputHook to NULL and return the previous one.""" | ||
Brian Granger
|
r2066 | pyos_inputhook_ptr = self.get_pyos_inputhook() | ||
original = self.get_pyos_inputhook_as_func() | ||||
pyos_inputhook_ptr.value = ctypes.c_void_p(None).value | ||||
self._reset() | ||||
return original | ||||
Brian Granger
|
r2208 | def clear_app_refs(self, gui=None): | ||
"""Clear IPython's internal reference to an application instance. | ||||
Brian Granger
|
r2209 | Whenever we create an app for a user on qt4 or wx, we hold a | ||
reference to the app. This is needed because in some cases bad things | ||||
can happen if a user doesn't hold a reference themselves. This | ||||
method is provided to clear the references we are holding. | ||||
Brian Granger
|
r2208 | Parameters | ||
---------- | ||||
gui : None or str | ||||
If None, clear all app references. If ('wx', 'qt4') clear | ||||
the app for that toolkit. References are not held for gtk or tk | ||||
as those toolkits don't have the notion of an app. | ||||
""" | ||||
if gui is None: | ||||
self._apps = {} | ||||
elif self._apps.has_key(gui): | ||||
del self._apps[gui] | ||||
Brian Granger
|
r2195 | def enable_wx(self, app=False): | ||
Brian Granger
|
r2069 | """Enable event loop integration with wxPython. | ||
Brian Granger
|
r2197 | |||
Parameters | ||||
---------- | ||||
app : bool | ||||
Create a running application object or not. | ||||
Notes | ||||
----- | ||||
Brian Granger
|
r2209 | This methods sets the ``PyOS_InputHook`` for wxPython, which allows | ||
Brian Granger
|
r2069 | the wxPython to integrate with terminal based applications like | ||
IPython. | ||||
Brian Granger
|
r2209 | |||
If ``app`` is True, we create an :class:`wx.App` as follows:: | ||||
import wx | ||||
app = wx.App(redirect=False, clearSigInt=False) | ||||
Brian Granger
|
r2069 | Both options this constructor are important for things to work | ||
properly in an interactive context. | ||||
Brian Granger
|
r2209 | |||
But, we first check to see if an application has already been | ||||
created. If so, we simply return that instance. | ||||
Brian Granger
|
r2069 | """ | ||
Brian Granger
|
r2068 | from IPython.lib.inputhookwx import inputhook_wx | ||
Brian Granger
|
r2066 | self.set_inputhook(inputhook_wx) | ||
Brian Granger
|
r2214 | self._current_gui = GUI_WX | ||
Brian Granger
|
r2195 | if app: | ||
import wx | ||||
Brian Granger
|
r2208 | app = wx.GetApp() | ||
if app is None: | ||||
app = wx.App(redirect=False, clearSigInt=False) | ||||
Brian Granger
|
r2214 | self._apps[GUI_WX] = app | ||
Brian Granger
|
r2195 | return app | ||
Brian Granger
|
r2066 | |||
def disable_wx(self): | ||||
Brian Granger
|
r2069 | """Disable event loop integration with wxPython. | ||
Brian Granger
|
r2209 | |||
Brian Granger
|
r2069 | This merely sets PyOS_InputHook to NULL. | ||
""" | ||||
Brian Granger
|
r2066 | self.clear_inputhook() | ||
Brian Granger
|
r2195 | def enable_qt4(self, app=False): | ||
Brian Granger
|
r2069 | """Enable event loop integration with PyQt4. | ||
Brian Granger
|
r2197 | Parameters | ||
---------- | ||||
app : bool | ||||
Create a running application object or not. | ||||
Notes | ||||
----- | ||||
Brian Granger
|
r2209 | This methods sets the PyOS_InputHook for PyQt4, which allows | ||
Brian Granger
|
r2069 | the PyQt4 to integrate with terminal based applications like | ||
IPython. | ||||
Brian Granger
|
r2209 | |||
If ``app`` is True, we create an :class:`QApplication` as follows:: | ||||
from PyQt4 import QtCore | ||||
app = QtGui.QApplication(sys.argv) | ||||
But, we first check to see if an application has already been | ||||
created. If so, we simply return that instance. | ||||
Brian Granger
|
r2069 | """ | ||
Brian Granger
|
r2066 | from PyQt4 import QtCore | ||
# PyQt4 has had this since 4.3.1. In version 4.2, PyOS_InputHook | ||||
# was set when QtCore was imported, but if it ever got removed, | ||||
# you couldn't reset it. For earlier versions we can | ||||
# probably implement a ctypes version. | ||||
try: | ||||
QtCore.pyqtRestoreInputHook() | ||||
except AttributeError: | ||||
pass | ||||
Brian Granger
|
r2214 | self._current_gui = GUI_QT4 | ||
Brian Granger
|
r2195 | if app: | ||
from PyQt4 import QtGui | ||||
Brian Granger
|
r2210 | app = QtCore.QCoreApplication.instance() | ||
Brian Granger
|
r2208 | if app is None: | ||
app = QtGui.QApplication(sys.argv) | ||||
Brian Granger
|
r2214 | self._apps[GUI_QT4] = app | ||
Brian Granger
|
r2195 | return app | ||
Brian Granger
|
r2066 | |||
def disable_qt4(self): | ||||
Brian Granger
|
r2069 | """Disable event loop integration with PyQt4. | ||
Brian Granger
|
r2209 | |||
Brian Granger
|
r2069 | This merely sets PyOS_InputHook to NULL. | ||
""" | ||||
Brian Granger
|
r2066 | self.clear_inputhook() | ||
Brian Granger
|
r2195 | def enable_gtk(self, app=False): | ||
Brian Granger
|
r2069 | """Enable event loop integration with PyGTK. | ||
Brian Granger
|
r2197 | |||
Parameters | ||||
---------- | ||||
app : bool | ||||
Brian Granger
|
r2209 | Create a running application object or not. Because gtk does't | ||
have an app class, this does nothing. | ||||
Brian Granger
|
r2197 | |||
Notes | ||||
----- | ||||
Brian Granger
|
r2069 | This methods sets the PyOS_InputHook for PyGTK, which allows | ||
the PyGTK to integrate with terminal based applications like | ||||
IPython. | ||||
""" | ||||
Brian Granger
|
r2066 | import gtk | ||
try: | ||||
gtk.set_interactive(True) | ||||
Brian Granger
|
r2214 | self._current_gui = GUI_GTK | ||
Brian Granger
|
r2066 | except AttributeError: | ||
# For older versions of gtk, use our own ctypes version | ||||
Brian Granger
|
r2068 | from IPython.lib.inputhookgtk import inputhook_gtk | ||
Brian Granger
|
r2207 | self.set_inputhook(inputhook_gtk) | ||
Brian Granger
|
r2214 | self._current_gui = GUI_GTK | ||
Brian Granger
|
r2066 | |||
def disable_gtk(self): | ||||
Brian Granger
|
r2069 | """Disable event loop integration with PyGTK. | ||
This merely sets PyOS_InputHook to NULL. | ||||
""" | ||||
Brian Granger
|
r2066 | self.clear_inputhook() | ||
Brian Granger
|
r2195 | def enable_tk(self, app=False): | ||
Brian Granger
|
r2197 | """Enable event loop integration with Tk. | ||
Parameters | ||||
---------- | ||||
app : bool | ||||
Create a running application object or not. | ||||
Notes | ||||
----- | ||||
Currently this is a no-op as creating a :class:`Tkinter.Tk` object | ||||
sets ``PyOS_InputHook``. | ||||
""" | ||||
Brian Granger
|
r2214 | self._current_gui = GUI_TK | ||
if app: | ||||
import Tkinter | ||||
app = Tkinter.Tk() | ||||
app.withdraw() | ||||
self._apps[GUI_TK] = app | ||||
return app | ||||
Brian Granger
|
r2066 | |||
def disable_tk(self): | ||||
Brian Granger
|
r2069 | """Disable event loop integration with Tkinter. | ||
This merely sets PyOS_InputHook to NULL. | ||||
""" | ||||
Brian Granger
|
r2066 | self.clear_inputhook() | ||
Brian Granger
|
r2197 | def current_gui(self): | ||
"""Return a string indicating the currently active GUI or None.""" | ||||
return self._current_gui | ||||
Brian Granger
|
r2066 | inputhook_manager = InputHookManager() | ||
enable_wx = inputhook_manager.enable_wx | ||||
disable_wx = inputhook_manager.disable_wx | ||||
enable_qt4 = inputhook_manager.enable_qt4 | ||||
disable_qt4 = inputhook_manager.disable_qt4 | ||||
enable_gtk = inputhook_manager.enable_gtk | ||||
disable_gtk = inputhook_manager.disable_gtk | ||||
Brian Granger
|
r2068 | enable_tk = inputhook_manager.enable_tk | ||
Brian Granger
|
r2069 | disable_tk = inputhook_manager.disable_tk | ||
clear_inputhook = inputhook_manager.clear_inputhook | ||||
Brian Granger
|
r2197 | set_inputhook = inputhook_manager.set_inputhook | ||
Brian Granger
|
r2208 | current_gui = inputhook_manager.current_gui | ||
clear_app_refs = inputhook_manager.clear_app_refs | ||||
Brian Granger
|
r2214 | spin = inputhook_manager.spin | ||