Show More
@@ -0,0 +1,85 b'' | |||
|
1 | """GUI support for the IPython ZeroMQ kernel - GTK toolkit support. | |
|
2 | """ | |
|
3 | #----------------------------------------------------------------------------- | |
|
4 | # Copyright (C) 2010-2011 The IPython Development Team | |
|
5 | # | |
|
6 | # Distributed under the terms of the BSD License. The full license is in | |
|
7 | # the file COPYING.txt, distributed as part of this software. | |
|
8 | #----------------------------------------------------------------------------- | |
|
9 | ||
|
10 | #----------------------------------------------------------------------------- | |
|
11 | # Imports | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # stdlib | |
|
14 | import sys | |
|
15 | ||
|
16 | # Third-party | |
|
17 | from gi.repository import GObject, Gtk | |
|
18 | ||
|
19 | #----------------------------------------------------------------------------- | |
|
20 | # Classes and functions | |
|
21 | #----------------------------------------------------------------------------- | |
|
22 | ||
|
23 | class GTKEmbed(object): | |
|
24 | """A class to embed a kernel into the GTK main event loop. | |
|
25 | """ | |
|
26 | def __init__(self, kernel): | |
|
27 | self.kernel = kernel | |
|
28 | # These two will later store the real gtk functions when we hijack them | |
|
29 | self.gtk_main = None | |
|
30 | self.gtk_main_quit = None | |
|
31 | ||
|
32 | def start(self): | |
|
33 | """Starts the GTK main event loop and sets our kernel startup routine. | |
|
34 | """ | |
|
35 | # Register our function to initiate the kernel and start gtk | |
|
36 | GObject.idle_add(self._wire_kernel) | |
|
37 | Gtk.main() | |
|
38 | ||
|
39 | def _wire_kernel(self): | |
|
40 | """Initializes the kernel inside GTK. | |
|
41 | ||
|
42 | This is meant to run only once at startup, so it does its job and | |
|
43 | returns False to ensure it doesn't get run again by GTK. | |
|
44 | """ | |
|
45 | self.gtk_main, self.gtk_main_quit = self._hijack_gtk() | |
|
46 | GObject.timeout_add(int(1000*self.kernel._poll_interval), | |
|
47 | self.iterate_kernel) | |
|
48 | return False | |
|
49 | ||
|
50 | def iterate_kernel(self): | |
|
51 | """Run one iteration of the kernel and return True. | |
|
52 | ||
|
53 | GTK timer functions must return True to be called again, so we make the | |
|
54 | call to :meth:`do_one_iteration` and then return True for GTK. | |
|
55 | """ | |
|
56 | self.kernel.do_one_iteration() | |
|
57 | return True | |
|
58 | ||
|
59 | def stop(self): | |
|
60 | # FIXME: this one isn't getting called because we have no reliable | |
|
61 | # kernel shutdown. We need to fix that: once the kernel has a | |
|
62 | # shutdown mechanism, it can call this. | |
|
63 | self.gtk_main_quit() | |
|
64 | sys.exit() | |
|
65 | ||
|
66 | def _hijack_gtk(self): | |
|
67 | """Hijack a few key functions in GTK for IPython integration. | |
|
68 | ||
|
69 | Modifies pyGTK's main and main_quit with a dummy so user code does not | |
|
70 | block IPython. This allows us to use %run to run arbitrary pygtk | |
|
71 | scripts from a long-lived IPython session, and when they attempt to | |
|
72 | start or stop | |
|
73 | ||
|
74 | Returns | |
|
75 | ------- | |
|
76 | The original functions that have been hijacked: | |
|
77 | - Gtk.main | |
|
78 | - Gtk.main_quit | |
|
79 | """ | |
|
80 | def dummy(*args, **kw): | |
|
81 | pass | |
|
82 | # save and trap main and main_quit from gtk | |
|
83 | orig_main, Gtk.main = Gtk.main, dummy | |
|
84 | orig_main_quit, Gtk.main_quit = Gtk.main_quit, dummy | |
|
85 | return orig_main, orig_main_quit |
@@ -154,6 +154,14 b' def loop_gtk(kernel):' | |||
|
154 | 154 | gtk_kernel.start() |
|
155 | 155 | |
|
156 | 156 | |
|
157 | def loop_gtk3(kernel): | |
|
158 | """Start the kernel, coordinating with the GTK event loop""" | |
|
159 | from .gui.gtk3embed import GTKEmbed | |
|
160 | ||
|
161 | gtk_kernel = GTKEmbed(kernel) | |
|
162 | gtk_kernel.start() | |
|
163 | ||
|
164 | ||
|
157 | 165 | def loop_cocoa(kernel): |
|
158 | 166 | """Start the kernel, coordinating with the Cocoa CFRunLoop event loop |
|
159 | 167 | via the matplotlib MacOSX backend. |
@@ -233,6 +241,7 b' loop_map = {' | |||
|
233 | 241 | 'wx' : loop_wx, |
|
234 | 242 | 'tk' : loop_tk, |
|
235 | 243 | 'gtk': loop_gtk, |
|
244 | 'gtk3': loop_gtk3, | |
|
236 | 245 | None : None, |
|
237 | 246 | } |
|
238 | 247 |
General Comments 0
You need to be logged in to leave comments.
Login now