##// END OF EJS Templates
Work on inputhook....
Work on inputhook. * We now hold references to qt4 and wx apps that are created. * For wx and qt4 we now first try to get an existing app if it exists. * We now hijack the mainloops for all gui toolkits. * Longer poll time for wx.

File last commit:

r2208:2d9d4aa9
r2208:2d9d4aa9
Show More
inputhook.py
286 lines | 9.2 KiB | text/x-python | PythonLexer
Brian Granger
First draft of full inputhook management.
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
Work on the user focused GUI event loop interface....
r2195 import sys
Brian Granger
First draft of full inputhook management.
r2066
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
Brian Granger
Work on inputhook....
r2208 def _dummy_mainloop(*args, **kw):
pass
Brian Granger
First draft of full inputhook management.
r2066
class InputHookManager(object):
Brian Granger
General work on inputhook and the docs....
r2197 """Manage PyOS_InputHook for different GUI toolkits.
This class installs various hooks under ``PyOSInputHook`` to handle
GUI event loop integration.
"""
Brian Granger
First draft of full inputhook management.
r2066
def __init__(self):
self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int)
Brian Granger
Work on inputhook....
r2208 self._apps = {}
Brian Granger
First draft of full inputhook management.
r2066 self._reset()
def _reset(self):
self._callback_pyfunctype = None
self._callback = None
self._installed = False
Brian Granger
General work on inputhook and the docs....
r2197 self._current_gui = None
Brian Granger
First draft of full inputhook management.
r2066
Brian Granger
Work on inputhook....
r2208 def _hijack_wx(self):
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
core.PyApp_MainLoop = _dummy_mainloop
return orig_mainloop
def _hijack_qt4(self):
from PyQt4 import QtGui, QtCore
orig_mainloop = QtGui.qApp.exec_
QtGui.qApp.exec_ = _dummy_mainloop
QtGui.QApplication.exec_ = _dummy_mainloop
QtCore.QCoreApplication.exec_ = _dummy_mainloop
return orig_mainloop
def _hijack_gtk(self):
import gtk
orig_mainloop = gtk.main
gtk.mainloop = _dummy_mainloop
gtk.main = _dummy_mainloop
return orig_mainloop
def _hijack_tk(self):
import Tkinter
Tkinter.Misc.mainloop = _dummy_mainloop
Tkinter.mainloop = _dummy_mainloop
Brian Granger
First draft of full inputhook management.
r2066 def get_pyos_inputhook(self):
Brian Granger
More testing and docstrings added for inputhook.py
r2069 """Return the current PyOS_InputHook as a ctypes.c_void_p.
"""
Brian Granger
First draft of full inputhook management.
r2066 return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook")
def get_pyos_inputhook_as_func(self):
Brian Granger
More testing and docstrings added for inputhook.py
r2069 """Return the current PyOS_InputHook as a ctypes.PYFUNCYPE.
"""
Brian Granger
First draft of full inputhook management.
r2066 return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook")
Brian Granger
More testing and docstrings added for inputhook.py
r2069 def set_inputhook(self, callback):
Brian Granger
First draft of full inputhook management.
r2066 """Set PyOS_InputHook to callback and return the previous one.
"""
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
More testing and docstrings added for inputhook.py
r2069 """Set PyOS_InputHook to NULL and return the previous one.
"""
Brian Granger
First draft of full inputhook management.
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
Work on inputhook....
r2208 def clear_app_refs(self, gui=None):
"""Clear IPython's internal reference to an application instance.
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
Work on the user focused GUI event loop interface....
r2195 def enable_wx(self, app=False):
Brian Granger
More testing and docstrings added for inputhook.py
r2069 """Enable event loop integration with wxPython.
Brian Granger
General work on inputhook and the docs....
r2197
Parameters
----------
app : bool
Create a running application object or not.
Notes
-----
Brian Granger
More testing and docstrings added for inputhook.py
r2069 This methods sets the PyOS_InputHook for wxPython, which allows
the wxPython to integrate with terminal based applications like
IPython.
Once this has been called, you can use wx interactively by doing::
>>> import wx
>>> app = wx.App(redirect=False, clearSigInt=False)
Both options this constructor are important for things to work
properly in an interactive context.
But, *don't start the event loop*. That is handled automatically by
PyOS_InputHook.
"""
Brian Granger
Fixed import statements for inputhook.
r2068 from IPython.lib.inputhookwx import inputhook_wx
Brian Granger
First draft of full inputhook management.
r2066 self.set_inputhook(inputhook_wx)
Brian Granger
General work on inputhook and the docs....
r2197 self._current_gui = 'wx'
Brian Granger
Work on inputhook....
r2208 self._hijack_wx()
Brian Granger
Work on the user focused GUI event loop interface....
r2195 if app:
import wx
Brian Granger
Work on inputhook....
r2208 app = wx.GetApp()
if app is None:
app = wx.App(redirect=False, clearSigInt=False)
self._apps['wx'] = app
Brian Granger
Work on the user focused GUI event loop interface....
r2195 return app
Brian Granger
First draft of full inputhook management.
r2066
def disable_wx(self):
Brian Granger
More testing and docstrings added for inputhook.py
r2069 """Disable event loop integration with wxPython.
This merely sets PyOS_InputHook to NULL.
"""
Brian Granger
First draft of full inputhook management.
r2066 self.clear_inputhook()
Brian Granger
Work on the user focused GUI event loop interface....
r2195 def enable_qt4(self, app=False):
Brian Granger
More testing and docstrings added for inputhook.py
r2069 """Enable event loop integration with PyQt4.
Brian Granger
General work on inputhook and the docs....
r2197 Parameters
----------
app : bool
Create a running application object or not.
Notes
-----
Brian Granger
More testing and docstrings added for inputhook.py
r2069 This methods sets the PyOS_InputHook for wxPython, which allows
the PyQt4 to integrate with terminal based applications like
IPython.
Once this has been called, you can simply create a QApplication and
use it. But, *don't start the event loop*. That is handled
automatically by PyOS_InputHook.
"""
Brian Granger
First draft of full inputhook management.
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
General work on inputhook and the docs....
r2197 self._current_gui = 'qt4'
Brian Granger
Work on inputhook....
r2208 self._hijack_qt4()
Brian Granger
Work on the user focused GUI event loop interface....
r2195 if app:
from PyQt4 import QtGui
Brian Granger
Work on inputhook....
r2208 app = QtGui.QApplication.instance()
if app is None:
app = QtGui.QApplication(sys.argv)
self._apps['qt4'] = app
Brian Granger
Work on the user focused GUI event loop interface....
r2195 return app
Brian Granger
First draft of full inputhook management.
r2066
def disable_qt4(self):
Brian Granger
More testing and docstrings added for inputhook.py
r2069 """Disable event loop integration with PyQt4.
This merely sets PyOS_InputHook to NULL.
"""
Brian Granger
First draft of full inputhook management.
r2066 self.clear_inputhook()
Brian Granger
Work on the user focused GUI event loop interface....
r2195 def enable_gtk(self, app=False):
Brian Granger
More testing and docstrings added for inputhook.py
r2069 """Enable event loop integration with PyGTK.
Brian Granger
General work on inputhook and the docs....
r2197
Parameters
----------
app : bool
Create a running application object or not.
Notes
-----
Brian Granger
More testing and docstrings added for inputhook.py
r2069 This methods sets the PyOS_InputHook for PyGTK, which allows
the PyGTK to integrate with terminal based applications like
IPython.
Once this has been called, you can simple create PyGTK objects and
use them. But, *don't start the event loop*. That is handled
automatically by PyOS_InputHook.
"""
Brian Granger
First draft of full inputhook management.
r2066 import gtk
try:
gtk.set_interactive(True)
Brian Granger
General work on inputhook and the docs....
r2197 self._current_gui = 'gtk'
Brian Granger
First draft of full inputhook management.
r2066 except AttributeError:
# For older versions of gtk, use our own ctypes version
Brian Granger
Fixed import statements for inputhook.
r2068 from IPython.lib.inputhookgtk import inputhook_gtk
Brian Granger
Fixed two bugs in inputhook.
r2207 self.set_inputhook(inputhook_gtk)
self._current_gui = 'gtk'
Brian Granger
Work on inputhook....
r2208 self._hijack_gtk()
Brian Granger
First draft of full inputhook management.
r2066
def disable_gtk(self):
Brian Granger
More testing and docstrings added for inputhook.py
r2069 """Disable event loop integration with PyGTK.
This merely sets PyOS_InputHook to NULL.
"""
Brian Granger
First draft of full inputhook management.
r2066 self.clear_inputhook()
Brian Granger
Work on the user focused GUI event loop interface....
r2195 def enable_tk(self, app=False):
Brian Granger
General work on inputhook and the docs....
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``.
"""
self._current_gui = 'tk'
Brian Granger
Work on inputhook....
r2208 self._hijack_tk()
Brian Granger
First draft of full inputhook management.
r2066
def disable_tk(self):
Brian Granger
More testing and docstrings added for inputhook.py
r2069 """Disable event loop integration with Tkinter.
This merely sets PyOS_InputHook to NULL.
"""
Brian Granger
First draft of full inputhook management.
r2066 self.clear_inputhook()
Brian Granger
General work on inputhook and the docs....
r2197 def current_gui(self):
"""Return a string indicating the currently active GUI or None."""
return self._current_gui
Brian Granger
First draft of full inputhook management.
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
Fixed import statements for inputhook.
r2068 enable_tk = inputhook_manager.enable_tk
Brian Granger
More testing and docstrings added for inputhook.py
r2069 disable_tk = inputhook_manager.disable_tk
clear_inputhook = inputhook_manager.clear_inputhook
Brian Granger
General work on inputhook and the docs....
r2197 set_inputhook = inputhook_manager.set_inputhook
Brian Granger
Work on inputhook....
r2208 current_gui = inputhook_manager.current_gui
clear_app_refs = inputhook_manager.clear_app_refs