##// END OF EJS Templates
Fixed import statements for inputhook.
Brian Granger -
Show More
@@ -0,0 +1,27 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 Extra capabilities for IPython
5 """
6
7 #-----------------------------------------------------------------------------
8 # Copyright (C) 2008-2009 The IPython Development Team
9 #
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
12 #-----------------------------------------------------------------------------
13
14 #-----------------------------------------------------------------------------
15 # Imports
16 #-----------------------------------------------------------------------------
17
18 from IPython.lib.inputhook import (
19 enable_wx, disable_wx,
20 enable_gtk, disable_gtk,
21 enable_qt4, disable_qt4,
22 enable_tk, disable_tk
23 )
24
25 #-----------------------------------------------------------------------------
26 # Code
27 #----------------------------------------------------------------------------- No newline at end of file
@@ -1,109 +1,111 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 Inputhook management for GUI event loop integration.
4 Inputhook management for GUI event loop integration.
5 """
5 """
6
6
7 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
8 # Copyright (C) 2008-2009 The IPython Development Team
8 # Copyright (C) 2008-2009 The IPython Development Team
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 import ctypes
18 import ctypes
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Code
21 # Code
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23
23
24
24
25 class InputHookManager(object):
25 class InputHookManager(object):
26
26
27 def __init__(self):
27 def __init__(self):
28 self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int)
28 self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int)
29 self._reset()
29 self._reset()
30
30
31 def _reset(self):
31 def _reset(self):
32 self._callback_pyfunctype = None
32 self._callback_pyfunctype = None
33 self._callback = None
33 self._callback = None
34 self._installed = False
34 self._installed = False
35
35
36 def get_pyos_inputhook(self):
36 def get_pyos_inputhook(self):
37 return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook")
37 return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook")
38
38
39 def get_pyos_inputhook_as_func(self):
39 def get_pyos_inputhook_as_func(self):
40 return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook")
40 return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook")
41
41
42 def set_inputhook(callback):
42 def set_inputhook(callback):
43 """Set PyOS_InputHook to callback and return the previous one.
43 """Set PyOS_InputHook to callback and return the previous one.
44 """
44 """
45 self._callback = callback
45 self._callback = callback
46 self._callback_pyfunctype = self.PYFUNC(callback)
46 self._callback_pyfunctype = self.PYFUNC(callback)
47 pyos_inputhook_ptr = self.get_pyos_inputhook()
47 pyos_inputhook_ptr = self.get_pyos_inputhook()
48 original = self.get_pyos_inputhook_as_func()
48 original = self.get_pyos_inputhook_as_func()
49 pyos_inputhook_ptr.value = \
49 pyos_inputhook_ptr.value = \
50 ctypes.cast(self._callback_pyfunctype, ctypes.c_void_p).value
50 ctypes.cast(self._callback_pyfunctype, ctypes.c_void_p).value
51 self._installed = True
51 self._installed = True
52 return original
52 return original
53
53
54 def clear_inputhook(self):
54 def clear_inputhook(self):
55 """Set PyOS_InputHook to NULL and return the previous one."""
55 """Set PyOS_InputHook to NULL and return the previous one."""
56 pyos_inputhook_ptr = self.get_pyos_inputhook()
56 pyos_inputhook_ptr = self.get_pyos_inputhook()
57 original = self.get_pyos_inputhook_as_func()
57 original = self.get_pyos_inputhook_as_func()
58 pyos_inputhook_ptr.value = ctypes.c_void_p(None).value
58 pyos_inputhook_ptr.value = ctypes.c_void_p(None).value
59 self._reset()
59 self._reset()
60 return original
60 return original
61
61
62 def enable_wx(self):
62 def enable_wx(self):
63 from IPython.lib.guiloop.inputhookwx import inputhook_wx
63 from IPython.lib.inputhookwx import inputhook_wx
64 self.set_inputhook(inputhook_wx)
64 self.set_inputhook(inputhook_wx)
65
65
66 def disable_wx(self):
66 def disable_wx(self):
67 self.clear_inputhook()
67 self.clear_inputhook()
68
68
69 def enable_qt4(self):
69 def enable_qt4(self):
70 from PyQt4 import QtCore
70 from PyQt4 import QtCore
71 # PyQt4 has had this since 4.3.1. In version 4.2, PyOS_InputHook
71 # PyQt4 has had this since 4.3.1. In version 4.2, PyOS_InputHook
72 # was set when QtCore was imported, but if it ever got removed,
72 # was set when QtCore was imported, but if it ever got removed,
73 # you couldn't reset it. For earlier versions we can
73 # you couldn't reset it. For earlier versions we can
74 # probably implement a ctypes version.
74 # probably implement a ctypes version.
75 try:
75 try:
76 QtCore.pyqtRestoreInputHook()
76 QtCore.pyqtRestoreInputHook()
77 except AttributeError:
77 except AttributeError:
78 pass
78 pass
79
79
80 def disable_qt4(self):
80 def disable_qt4(self):
81 self.clear_inputhook()
81 self.clear_inputhook()
82
82
83 def enable_gtk(self):
83 def enable_gtk(self):
84 import gtk
84 import gtk
85 try:
85 try:
86 gtk.set_interactive(True)
86 gtk.set_interactive(True)
87 except AttributeError:
87 except AttributeError:
88 # For older versions of gtk, use our own ctypes version
88 # For older versions of gtk, use our own ctypes version
89 from IPython.lib.guiloop.inputhookgtk import inputhook_gtk
89 from IPython.lib.inputhookgtk import inputhook_gtk
90 add_inputhook(inputhook_gtk)
90 add_inputhook(inputhook_gtk)
91
91
92 def disable_gtk(self):
92 def disable_gtk(self):
93 self.clear_inputhook()
93 self.clear_inputhook()
94
94
95 def enable_tk(self):
95 def enable_tk(self):
96 # Creating a Tkinter.Tk object sets PyOS_InputHook()
96 # Creating a Tkinter.Tk object sets PyOS_InputHook()
97 pass
97 pass
98
98
99 def disable_tk(self):
99 def disable_tk(self):
100 self.clear_inputhook()
100 self.clear_inputhook()
101
101
102 inputhook_manager = InputHookManager()
102 inputhook_manager = InputHookManager()
103
103
104 enable_wx = inputhook_manager.enable_wx
104 enable_wx = inputhook_manager.enable_wx
105 disable_wx = inputhook_manager.disable_wx
105 disable_wx = inputhook_manager.disable_wx
106 enable_qt4 = inputhook_manager.enable_qt4
106 enable_qt4 = inputhook_manager.enable_qt4
107 disable_qt4 = inputhook_manager.disable_qt4
107 disable_qt4 = inputhook_manager.disable_qt4
108 enable_gtk = inputhook_manager.enable_gtk
108 enable_gtk = inputhook_manager.enable_gtk
109 disable_gtk = inputhook_manager.disable_gtk
109 disable_gtk = inputhook_manager.disable_gtk
110 enable_tk = inputhook_manager.enable_tk
111 disable_tk = inputhook_manager.disable_tk No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now