gtk3embed.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. # Imports
  11. #-----------------------------------------------------------------------------
  12. # stdlib
  13. import sys
  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
  19. #-----------------------------------------------------------------------------
  20. # Classes and functions
  21. #-----------------------------------------------------------------------------
  22. class GTKEmbed(object):
  23. """A class to embed a kernel into the GTK main event loop.
  24. """
  25. def __init__(self, kernel):
  26. self.kernel = kernel
  27. # These two will later store the real gtk functions when we hijack them
  28. self.gtk_main = None
  29. self.gtk_main_quit = None
  30. def start(self):
  31. """Starts the GTK main event loop and sets our kernel startup routine.
  32. """
  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),
  43. self.iterate_kernel)
  44. return False
  45. def iterate_kernel(self):
  46. """Run one iteration of the kernel and return True.
  47. GTK timer functions must return True to be called again, so we make the
  48. call to :meth:`do_one_iteration` and then return True for GTK.
  49. """
  50. self.kernel.do_one_iteration()
  51. return True
  52. def stop(self):
  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. self.gtk_main_quit()
  57. sys.exit()
  58. def _hijack_gtk(self):
  59. """Hijack a few key functions in GTK for IPython integration.
  60. Modifies pyGTK's main and main_quit with a dummy so user code does not
  61. block IPython. This allows us to use %run to run arbitrary pygtk
  62. scripts from a long-lived IPython session, and when they attempt to
  63. start or stop
  64. Returns
  65. -------
  66. The original functions that have been hijacked:
  67. - Gtk.main
  68. - Gtk.main_quit
  69. """
  70. def dummy(*args, **kw):
  71. pass
  72. # save and trap main and main_quit from gtk
  73. orig_main, Gtk.main = Gtk.main, dummy
  74. orig_main_quit, Gtk.main_quit = Gtk.main_quit, dummy
  75. return orig_main, orig_main_quit