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 LICENSE, distributed as part of this software. 8 # ----------------------------------------------------------------------------- 9 10 # ----------------------------------------------------------------------------- 11 # Imports 12 # ----------------------------------------------------------------------------- 13 # stdlib 14 import sys 15 import warnings 16 17 # Third-party 18 import gi 19 20 gi.require_version("Gdk", "3.0") 21 gi.require_version("Gtk", "3.0") 22 from gi.repository import GObject, Gtk # noqa: E402 23 24 warnings.warn( 25 "The Gtk3 event loop for ipykernel is deprecated", category=DeprecationWarning, stacklevel=2 26 ) 27 28 # ----------------------------------------------------------------------------- 29 # Classes and functions 30 # ----------------------------------------------------------------------------- 31 32 33 class GTKEmbed: 34 """A class to embed a kernel into the GTK main event loop.""" 35 36 def __init__(self, kernel): 37 """Initialize the embed.""" 38 self.kernel = kernel 39 # These two will later store the real gtk functions when we hijack them 40 self.gtk_main = None 41 self.gtk_main_quit = None 42 43 def start(self): 44 """Starts the GTK main event loop and sets our kernel startup routine.""" 45 # Register our function to initiate the kernel and start gtk 46 GObject.idle_add(self._wire_kernel) 47 Gtk.main() 48 49 def _wire_kernel(self): 50 """Initializes the kernel inside GTK. 51 52 This is meant to run only once at startup, so it does its job and 53 returns False to ensure it doesn't get run again by GTK. 54 """ 55 self.gtk_main, self.gtk_main_quit = self._hijack_gtk() 56 GObject.timeout_add(int(1000 * self.kernel._poll_interval), self.iterate_kernel) 57 return False 58 59 def iterate_kernel(self): 60 """Run one iteration of the kernel and return True. 61 62 GTK timer functions must return True to be called again, so we make the 63 call to :meth:`do_one_iteration` and then return True for GTK. 64 """ 65 self.kernel.do_one_iteration() 66 return True 67 68 def stop(self): 69 """Stop the embed.""" 70 # FIXME: this one isn't getting called because we have no reliable 71 # kernel shutdown. We need to fix that: once the kernel has a 72 # shutdown mechanism, it can call this. 73 if self.gtk_main_quit: 74 self.gtk_main_quit() 75 sys.exit() 76 77 def _hijack_gtk(self): 78 """Hijack a few key functions in GTK for IPython integration. 79 80 Modifies pyGTK's main and main_quit with a dummy so user code does not 81 block IPython. This allows us to use %run to run arbitrary pygtk 82 scripts from a long-lived IPython session, and when they attempt to 83 start or stop 84 85 Returns 86 ------- 87 The original functions that have been hijacked: 88 - Gtk.main 89 - Gtk.main_quit 90 """ 91 92 def dummy(*args, **kw): 93 """No-op.""" 94 95 # save and trap main and main_quit from gtk 96 orig_main, Gtk.main = Gtk.main, dummy 97 orig_main_quit, Gtk.main_quit = Gtk.main_quit, dummy 98 return orig_main, orig_main_quit 99