gtk3embed.py 3.4 KB

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