gtkembed.py 3.1 KB

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