Show More
@@ -1,27 +1,28 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | Extra capabilities for IPython |
|
4 | Extra capabilities for IPython | |
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 | from IPython.lib.inputhook import ( |
|
18 | from IPython.lib.inputhook import ( | |
19 | enable_wx, disable_wx, |
|
19 | enable_wx, disable_wx, | |
20 | enable_gtk, disable_gtk, |
|
20 | enable_gtk, disable_gtk, | |
21 | enable_qt4, disable_qt4, |
|
21 | enable_qt4, disable_qt4, | |
22 | enable_tk, disable_tk |
|
22 | enable_tk, disable_tk, | |
|
23 | set_inputhook, clear_inputhook | |||
23 | ) |
|
24 | ) | |
24 |
|
25 | |||
25 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
26 | # Code |
|
27 | # Code | |
27 | #----------------------------------------------------------------------------- No newline at end of file |
|
28 | #----------------------------------------------------------------------------- |
@@ -1,111 +1,172 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 | """Manage PyOS_InputHook for different GUI toolkits.""" | |||
26 |
|
27 | |||
27 | def __init__(self): |
|
28 | def __init__(self): | |
28 | self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int) |
|
29 | self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int) | |
29 | self._reset() |
|
30 | self._reset() | |
30 |
|
31 | |||
31 | def _reset(self): |
|
32 | def _reset(self): | |
32 | self._callback_pyfunctype = None |
|
33 | self._callback_pyfunctype = None | |
33 | self._callback = None |
|
34 | self._callback = None | |
34 | self._installed = False |
|
35 | self._installed = False | |
35 |
|
36 | |||
36 | def get_pyos_inputhook(self): |
|
37 | def get_pyos_inputhook(self): | |
|
38 | """Return the current PyOS_InputHook as a ctypes.c_void_p. | |||
|
39 | """ | |||
37 | return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook") |
|
40 | return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook") | |
38 |
|
41 | |||
39 | def get_pyos_inputhook_as_func(self): |
|
42 | def get_pyos_inputhook_as_func(self): | |
|
43 | """Return the current PyOS_InputHook as a ctypes.PYFUNCYPE. | |||
|
44 | """ | |||
40 | return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook") |
|
45 | return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook") | |
41 |
|
46 | |||
42 | def set_inputhook(callback): |
|
47 | def set_inputhook(self, callback): | |
43 | """Set PyOS_InputHook to callback and return the previous one. |
|
48 | """Set PyOS_InputHook to callback and return the previous one. | |
44 | """ |
|
49 | """ | |
45 | self._callback = callback |
|
50 | self._callback = callback | |
46 | self._callback_pyfunctype = self.PYFUNC(callback) |
|
51 | self._callback_pyfunctype = self.PYFUNC(callback) | |
47 | pyos_inputhook_ptr = self.get_pyos_inputhook() |
|
52 | pyos_inputhook_ptr = self.get_pyos_inputhook() | |
48 | original = self.get_pyos_inputhook_as_func() |
|
53 | original = self.get_pyos_inputhook_as_func() | |
49 | pyos_inputhook_ptr.value = \ |
|
54 | pyos_inputhook_ptr.value = \ | |
50 | ctypes.cast(self._callback_pyfunctype, ctypes.c_void_p).value |
|
55 | ctypes.cast(self._callback_pyfunctype, ctypes.c_void_p).value | |
51 | self._installed = True |
|
56 | self._installed = True | |
52 | return original |
|
57 | return original | |
53 |
|
58 | |||
54 | def clear_inputhook(self): |
|
59 | def clear_inputhook(self): | |
55 |
"""Set PyOS_InputHook to NULL and return the previous one. |
|
60 | """Set PyOS_InputHook to NULL and return the previous one. | |
|
61 | """ | |||
56 | pyos_inputhook_ptr = self.get_pyos_inputhook() |
|
62 | pyos_inputhook_ptr = self.get_pyos_inputhook() | |
57 | original = self.get_pyos_inputhook_as_func() |
|
63 | original = self.get_pyos_inputhook_as_func() | |
58 | pyos_inputhook_ptr.value = ctypes.c_void_p(None).value |
|
64 | pyos_inputhook_ptr.value = ctypes.c_void_p(None).value | |
59 | self._reset() |
|
65 | self._reset() | |
60 | return original |
|
66 | return original | |
61 |
|
67 | |||
62 | def enable_wx(self): |
|
68 | def enable_wx(self): | |
|
69 | """Enable event loop integration with wxPython. | |||
|
70 | ||||
|
71 | This methods sets the PyOS_InputHook for wxPython, which allows | |||
|
72 | the wxPython to integrate with terminal based applications like | |||
|
73 | IPython. | |||
|
74 | ||||
|
75 | Once this has been called, you can use wx interactively by doing:: | |||
|
76 | ||||
|
77 | >>> import wx | |||
|
78 | >>> app = wx.App(redirect=False, clearSigInt=False) | |||
|
79 | ||||
|
80 | Both options this constructor are important for things to work | |||
|
81 | properly in an interactive context. | |||
|
82 | ||||
|
83 | But, *don't start the event loop*. That is handled automatically by | |||
|
84 | PyOS_InputHook. | |||
|
85 | """ | |||
63 | from IPython.lib.inputhookwx import inputhook_wx |
|
86 | from IPython.lib.inputhookwx import inputhook_wx | |
64 | self.set_inputhook(inputhook_wx) |
|
87 | self.set_inputhook(inputhook_wx) | |
65 |
|
88 | |||
66 | def disable_wx(self): |
|
89 | def disable_wx(self): | |
|
90 | """Disable event loop integration with wxPython. | |||
|
91 | ||||
|
92 | This merely sets PyOS_InputHook to NULL. | |||
|
93 | """ | |||
67 | self.clear_inputhook() |
|
94 | self.clear_inputhook() | |
68 |
|
95 | |||
69 | def enable_qt4(self): |
|
96 | def enable_qt4(self): | |
|
97 | """Enable event loop integration with PyQt4. | |||
|
98 | ||||
|
99 | This methods sets the PyOS_InputHook for wxPython, which allows | |||
|
100 | the PyQt4 to integrate with terminal based applications like | |||
|
101 | IPython. | |||
|
102 | ||||
|
103 | Once this has been called, you can simply create a QApplication and | |||
|
104 | use it. But, *don't start the event loop*. That is handled | |||
|
105 | automatically by PyOS_InputHook. | |||
|
106 | """ | |||
70 | from PyQt4 import QtCore |
|
107 | from PyQt4 import QtCore | |
71 | # PyQt4 has had this since 4.3.1. In version 4.2, PyOS_InputHook |
|
108 | # 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, |
|
109 | # was set when QtCore was imported, but if it ever got removed, | |
73 | # you couldn't reset it. For earlier versions we can |
|
110 | # you couldn't reset it. For earlier versions we can | |
74 | # probably implement a ctypes version. |
|
111 | # probably implement a ctypes version. | |
75 | try: |
|
112 | try: | |
76 | QtCore.pyqtRestoreInputHook() |
|
113 | QtCore.pyqtRestoreInputHook() | |
77 | except AttributeError: |
|
114 | except AttributeError: | |
78 | pass |
|
115 | pass | |
79 |
|
116 | |||
80 | def disable_qt4(self): |
|
117 | def disable_qt4(self): | |
|
118 | """Disable event loop integration with PyQt4. | |||
|
119 | ||||
|
120 | This merely sets PyOS_InputHook to NULL. | |||
|
121 | """ | |||
81 | self.clear_inputhook() |
|
122 | self.clear_inputhook() | |
82 |
|
123 | |||
83 | def enable_gtk(self): |
|
124 | def enable_gtk(self): | |
|
125 | """Enable event loop integration with PyGTK. | |||
|
126 | ||||
|
127 | This methods sets the PyOS_InputHook for PyGTK, which allows | |||
|
128 | the PyGTK to integrate with terminal based applications like | |||
|
129 | IPython. | |||
|
130 | ||||
|
131 | Once this has been called, you can simple create PyGTK objects and | |||
|
132 | use them. But, *don't start the event loop*. That is handled | |||
|
133 | automatically by PyOS_InputHook. | |||
|
134 | """ | |||
84 | import gtk |
|
135 | import gtk | |
85 | try: |
|
136 | try: | |
86 | gtk.set_interactive(True) |
|
137 | gtk.set_interactive(True) | |
87 | except AttributeError: |
|
138 | except AttributeError: | |
88 | # For older versions of gtk, use our own ctypes version |
|
139 | # For older versions of gtk, use our own ctypes version | |
89 | from IPython.lib.inputhookgtk import inputhook_gtk |
|
140 | from IPython.lib.inputhookgtk import inputhook_gtk | |
90 | add_inputhook(inputhook_gtk) |
|
141 | add_inputhook(inputhook_gtk) | |
91 |
|
142 | |||
92 | def disable_gtk(self): |
|
143 | def disable_gtk(self): | |
|
144 | """Disable event loop integration with PyGTK. | |||
|
145 | ||||
|
146 | This merely sets PyOS_InputHook to NULL. | |||
|
147 | """ | |||
93 | self.clear_inputhook() |
|
148 | self.clear_inputhook() | |
94 |
|
149 | |||
95 | def enable_tk(self): |
|
150 | def enable_tk(self): | |
96 | # Creating a Tkinter.Tk object sets PyOS_InputHook() |
|
151 | # Creating a Tkinter.Tk object sets PyOS_InputHook() | |
97 | pass |
|
152 | pass | |
98 |
|
153 | |||
99 | def disable_tk(self): |
|
154 | def disable_tk(self): | |
|
155 | """Disable event loop integration with Tkinter. | |||
|
156 | ||||
|
157 | This merely sets PyOS_InputHook to NULL. | |||
|
158 | """ | |||
100 | self.clear_inputhook() |
|
159 | self.clear_inputhook() | |
101 |
|
160 | |||
102 | inputhook_manager = InputHookManager() |
|
161 | inputhook_manager = InputHookManager() | |
103 |
|
162 | |||
104 | enable_wx = inputhook_manager.enable_wx |
|
163 | enable_wx = inputhook_manager.enable_wx | |
105 | disable_wx = inputhook_manager.disable_wx |
|
164 | disable_wx = inputhook_manager.disable_wx | |
106 | enable_qt4 = inputhook_manager.enable_qt4 |
|
165 | enable_qt4 = inputhook_manager.enable_qt4 | |
107 | disable_qt4 = inputhook_manager.disable_qt4 |
|
166 | disable_qt4 = inputhook_manager.disable_qt4 | |
108 | enable_gtk = inputhook_manager.enable_gtk |
|
167 | enable_gtk = inputhook_manager.enable_gtk | |
109 | disable_gtk = inputhook_manager.disable_gtk |
|
168 | disable_gtk = inputhook_manager.disable_gtk | |
110 | enable_tk = inputhook_manager.enable_tk |
|
169 | enable_tk = inputhook_manager.enable_tk | |
111 | disable_tk = inputhook_manager.disable_tk No newline at end of file |
|
170 | disable_tk = inputhook_manager.disable_tk | |
|
171 | clear_inputhook = inputhook_manager.clear_inputhook | |||
|
172 | set_inputhook = inputhook_manager.set_inputhook No newline at end of file |
@@ -1,28 +1,36 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | Enable pygtk to be used interacive by setting PyOS_InputHook. |
|
4 | Enable pygtk to be used interacive by setting PyOS_InputHook. | |
5 |
|
5 | |||
6 | Authors: Brian Granger |
|
6 | Authors: Brian Granger | |
7 | """ |
|
7 | """ | |
8 |
|
8 | |||
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 | # Copyright (C) 2008-2009 The IPython Development Team |
|
10 | # Copyright (C) 2008-2009 The IPython Development Team | |
11 | # |
|
11 | # | |
12 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | # Distributed under the terms of the BSD License. The full license is in | |
13 | # the file COPYING, distributed as part of this software. |
|
13 | # the file COPYING, distributed as part of this software. | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
|
16 | #----------------------------------------------------------------------------- | |||
|
17 | # Imports | |||
|
18 | #----------------------------------------------------------------------------- | |||
|
19 | ||||
16 | import sys |
|
20 | import sys | |
17 | import gtk, gobject |
|
21 | import gtk, gobject | |
18 |
|
22 | |||
|
23 | #----------------------------------------------------------------------------- | |||
|
24 | # Code | |||
|
25 | #----------------------------------------------------------------------------- | |||
|
26 | ||||
19 |
|
27 | |||
20 | def _main_quit(*args, **kwargs): |
|
28 | def _main_quit(*args, **kwargs): | |
21 | gtk.main_quit() |
|
29 | gtk.main_quit() | |
22 | return False |
|
30 | return False | |
23 |
|
31 | |||
24 | def inputhook_gtk(): |
|
32 | def inputhook_gtk(): | |
25 | gobject.io_add_watch(sys.stdin, gobject.IO_IN, _main_quit) |
|
33 | gobject.io_add_watch(sys.stdin, gobject.IO_IN, _main_quit) | |
26 | gtk.main() |
|
34 | gtk.main() | |
27 | return 0 |
|
35 | return 0 | |
28 |
|
36 |
General Comments 0
You need to be logged in to leave comments.
Login now