##// END OF EJS Templates
Adding more documentation of inputhook.py
Brian Granger -
Show More
@@ -45,6 +45,7 b' class InputHookManager(object):'
45 self._current_gui = None
45 self._current_gui = None
46
46
47 def _hijack_wx(self):
47 def _hijack_wx(self):
48 """Hijack the wx mainloop so a user calling it won't cause badness."""
48 import wx
49 import wx
49 if hasattr(wx, '_core_'): core = getattr(wx, '_core_')
50 if hasattr(wx, '_core_'): core = getattr(wx, '_core_')
50 elif hasattr(wx, '_core'): core = getattr(wx, '_core')
51 elif hasattr(wx, '_core'): core = getattr(wx, '_core')
@@ -54,6 +55,7 b' class InputHookManager(object):'
54 return orig_mainloop
55 return orig_mainloop
55
56
56 def _hijack_qt4(self):
57 def _hijack_qt4(self):
58 """Hijack the qt4 mainloop so a user calling it won't cause badness."""
57 from PyQt4 import QtGui, QtCore
59 from PyQt4 import QtGui, QtCore
58 orig_mainloop = QtGui.qApp.exec_
60 orig_mainloop = QtGui.qApp.exec_
59 QtGui.qApp.exec_ = _dummy_mainloop
61 QtGui.qApp.exec_ = _dummy_mainloop
@@ -62,6 +64,7 b' class InputHookManager(object):'
62 return orig_mainloop
64 return orig_mainloop
63
65
64 def _hijack_gtk(self):
66 def _hijack_gtk(self):
67 """Hijack the gtk mainloop so a user calling it won't cause badness."""
65 import gtk
68 import gtk
66 orig_mainloop = gtk.main
69 orig_mainloop = gtk.main
67 gtk.mainloop = _dummy_mainloop
70 gtk.mainloop = _dummy_mainloop
@@ -69,23 +72,21 b' class InputHookManager(object):'
69 return orig_mainloop
72 return orig_mainloop
70
73
71 def _hijack_tk(self):
74 def _hijack_tk(self):
75 """Hijack the tk mainloop so a user calling it won't cause badness."""
72 import Tkinter
76 import Tkinter
73 Tkinter.Misc.mainloop = _dummy_mainloop
77 Tkinter.Misc.mainloop = _dummy_mainloop
74 Tkinter.mainloop = _dummy_mainloop
78 Tkinter.mainloop = _dummy_mainloop
75
79
76 def get_pyos_inputhook(self):
80 def get_pyos_inputhook(self):
77 """Return the current PyOS_InputHook as a ctypes.c_void_p.
81 """Return the current PyOS_InputHook as a ctypes.c_void_p."""
78 """
79 return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook")
82 return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook")
80
83
81 def get_pyos_inputhook_as_func(self):
84 def get_pyos_inputhook_as_func(self):
82 """Return the current PyOS_InputHook as a ctypes.PYFUNCYPE.
85 """Return the current PyOS_InputHook as a ctypes.PYFUNCYPE."""
83 """
84 return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook")
86 return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook")
85
87
86 def set_inputhook(self, callback):
88 def set_inputhook(self, callback):
87 """Set PyOS_InputHook to callback and return the previous one.
89 """Set PyOS_InputHook to callback and return the previous one."""
88 """
89 self._callback = callback
90 self._callback = callback
90 self._callback_pyfunctype = self.PYFUNC(callback)
91 self._callback_pyfunctype = self.PYFUNC(callback)
91 pyos_inputhook_ptr = self.get_pyos_inputhook()
92 pyos_inputhook_ptr = self.get_pyos_inputhook()
@@ -96,8 +97,7 b' class InputHookManager(object):'
96 return original
97 return original
97
98
98 def clear_inputhook(self):
99 def clear_inputhook(self):
99 """Set PyOS_InputHook to NULL and return the previous one.
100 """Set PyOS_InputHook to NULL and return the previous one."""
100 """
101 pyos_inputhook_ptr = self.get_pyos_inputhook()
101 pyos_inputhook_ptr = self.get_pyos_inputhook()
102 original = self.get_pyos_inputhook_as_func()
102 original = self.get_pyos_inputhook_as_func()
103 pyos_inputhook_ptr.value = ctypes.c_void_p(None).value
103 pyos_inputhook_ptr.value = ctypes.c_void_p(None).value
@@ -107,6 +107,11 b' class InputHookManager(object):'
107 def clear_app_refs(self, gui=None):
107 def clear_app_refs(self, gui=None):
108 """Clear IPython's internal reference to an application instance.
108 """Clear IPython's internal reference to an application instance.
109
109
110 Whenever we create an app for a user on qt4 or wx, we hold a
111 reference to the app. This is needed because in some cases bad things
112 can happen if a user doesn't hold a reference themselves. This
113 method is provided to clear the references we are holding.
114
110 Parameters
115 Parameters
111 ----------
116 ----------
112 gui : None or str
117 gui : None or str
@@ -129,20 +134,20 b' class InputHookManager(object):'
129
134
130 Notes
135 Notes
131 -----
136 -----
132 This methods sets the PyOS_InputHook for wxPython, which allows
137 This methods sets the ``PyOS_InputHook`` for wxPython, which allows
133 the wxPython to integrate with terminal based applications like
138 the wxPython to integrate with terminal based applications like
134 IPython.
139 IPython.
135
140
136 Once this has been called, you can use wx interactively by doing::
141 If ``app`` is True, we create an :class:`wx.App` as follows::
137
142
138 >>> import wx
143 import wx
139 >>> app = wx.App(redirect=False, clearSigInt=False)
144 app = wx.App(redirect=False, clearSigInt=False)
140
145
141 Both options this constructor are important for things to work
146 Both options this constructor are important for things to work
142 properly in an interactive context.
147 properly in an interactive context.
143
148
144 But, *don't start the event loop*. That is handled automatically by
149 But, we first check to see if an application has already been
145 PyOS_InputHook.
150 created. If so, we simply return that instance.
146 """
151 """
147 from IPython.lib.inputhookwx import inputhook_wx
152 from IPython.lib.inputhookwx import inputhook_wx
148 self.set_inputhook(inputhook_wx)
153 self.set_inputhook(inputhook_wx)
@@ -158,7 +163,7 b' class InputHookManager(object):'
158
163
159 def disable_wx(self):
164 def disable_wx(self):
160 """Disable event loop integration with wxPython.
165 """Disable event loop integration with wxPython.
161
166
162 This merely sets PyOS_InputHook to NULL.
167 This merely sets PyOS_InputHook to NULL.
163 """
168 """
164 self.clear_inputhook()
169 self.clear_inputhook()
@@ -173,13 +178,17 b' class InputHookManager(object):'
173
178
174 Notes
179 Notes
175 -----
180 -----
176 This methods sets the PyOS_InputHook for wxPython, which allows
181 This methods sets the PyOS_InputHook for PyQt4, which allows
177 the PyQt4 to integrate with terminal based applications like
182 the PyQt4 to integrate with terminal based applications like
178 IPython.
183 IPython.
179
184
180 Once this has been called, you can simply create a QApplication and
185 If ``app`` is True, we create an :class:`QApplication` as follows::
181 use it. But, *don't start the event loop*. That is handled
186
182 automatically by PyOS_InputHook.
187 from PyQt4 import QtCore
188 app = QtGui.QApplication(sys.argv)
189
190 But, we first check to see if an application has already been
191 created. If so, we simply return that instance.
183 """
192 """
184 from PyQt4 import QtCore
193 from PyQt4 import QtCore
185 # PyQt4 has had this since 4.3.1. In version 4.2, PyOS_InputHook
194 # PyQt4 has had this since 4.3.1. In version 4.2, PyOS_InputHook
@@ -202,7 +211,7 b' class InputHookManager(object):'
202
211
203 def disable_qt4(self):
212 def disable_qt4(self):
204 """Disable event loop integration with PyQt4.
213 """Disable event loop integration with PyQt4.
205
214
206 This merely sets PyOS_InputHook to NULL.
215 This merely sets PyOS_InputHook to NULL.
207 """
216 """
208 self.clear_inputhook()
217 self.clear_inputhook()
@@ -213,17 +222,14 b' class InputHookManager(object):'
213 Parameters
222 Parameters
214 ----------
223 ----------
215 app : bool
224 app : bool
216 Create a running application object or not.
225 Create a running application object or not. Because gtk does't
226 have an app class, this does nothing.
217
227
218 Notes
228 Notes
219 -----
229 -----
220 This methods sets the PyOS_InputHook for PyGTK, which allows
230 This methods sets the PyOS_InputHook for PyGTK, which allows
221 the PyGTK to integrate with terminal based applications like
231 the PyGTK to integrate with terminal based applications like
222 IPython.
232 IPython.
223
224 Once this has been called, you can simple create PyGTK objects and
225 use them. But, *don't start the event loop*. That is handled
226 automatically by PyOS_InputHook.
227 """
233 """
228 import gtk
234 import gtk
229 try:
235 try:
General Comments 0
You need to be logged in to leave comments. Login now