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