gtkembed.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 gobject
  16. import gtk
  17. warnings.warn(
  18. "The Gtk3 event loop for ipykernel is deprecated", category=DeprecationWarning, stacklevel=2
  19. )
  20. # -----------------------------------------------------------------------------
  21. # Classes and functions
  22. # -----------------------------------------------------------------------------
  23. class GTKEmbed:
  24. """A class to embed a kernel into the GTK main event loop."""
  25. def __init__(self, kernel):
  26. """Initialize the embed."""
  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. def start(self):
  32. """Starts the GTK main event loop and sets our kernel startup routine."""
  33. # Register our function to initiate the kernel and start gtk
  34. gobject.idle_add(self._wire_kernel)
  35. gtk.main()
  36. def _wire_kernel(self):
  37. """Initializes the kernel inside GTK.
  38. This is meant to run only once at startup, so it does its job and
  39. returns False to ensure it doesn't get run again by GTK.
  40. """
  41. self.gtk_main, self.gtk_main_quit = self._hijack_gtk()
  42. gobject.timeout_add(int(1000 * self.kernel._poll_interval), self.iterate_kernel)
  43. return False
  44. def iterate_kernel(self):
  45. """Run one iteration of the kernel and return True.
  46. GTK timer functions must return True to be called again, so we make the
  47. call to :meth:`do_one_iteration` and then return True for GTK.
  48. """
  49. self.kernel.do_one_iteration()
  50. return True
  51. def stop(self):
  52. """Stop the embed."""
  53. # FIXME: this one isn't getting called because we have no reliable
  54. # kernel shutdown. We need to fix that: once the kernel has a
  55. # shutdown mechanism, it can call this.
  56. if self.gtk_main_quit:
  57. self.gtk_main_quit()
  58. sys.exit()
  59. def _hijack_gtk(self):
  60. """Hijack a few key functions in GTK for IPython integration.
  61. Modifies pyGTK's main and main_quit with a dummy so user code does not
  62. block IPython. This allows us to use %run to run arbitrary pygtk
  63. scripts from a long-lived IPython session, and when they attempt to
  64. start or stop
  65. Returns
  66. -------
  67. The original functions that have been hijacked:
  68. - gtk.main
  69. - gtk.main_quit
  70. """
  71. def dummy(*args, **kw):
  72. """No-op."""
  73. # save and trap main and main_quit from gtk
  74. orig_main, gtk.main = gtk.main, dummy
  75. orig_main_quit, gtk.main_quit = gtk.main_quit, dummy
  76. return orig_main, orig_main_quit