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