From 909672309500c1322d73a08d7a8799cdcee2358c 2012-04-14 22:23:02 From: Thomas Kluyver Date: 2012-04-14 22:23:02 Subject: [PATCH] Merge pull request #1588 from thomir/gtk3-support Add Gtk3 event loop integration and example. --- diff --git a/IPython/core/magic.py b/IPython/core/magic.py index 83dfed2..6daf884 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -3420,6 +3420,7 @@ Defaulting color scheme to 'NoColor'""" %gui wx # enable wxPython event loop integration %gui qt4|qt # enable PyQt4 event loop integration %gui gtk # enable PyGTK event loop integration + %gui gtk3 # enable Gtk3 event loop integration %gui tk # enable Tk event loop integration %gui OSX # enable Cocoa event loop integration # (requires %matplotlib 1.1) diff --git a/IPython/lib/__init__.py b/IPython/lib/__init__.py index c631c01..482dcea 100644 --- a/IPython/lib/__init__.py +++ b/IPython/lib/__init__.py @@ -21,6 +21,7 @@ from IPython.lib.inputhook import ( enable_tk, disable_tk, enable_glut, disable_glut, enable_pyglet, disable_pyglet, + enable_gtk3, disable_gtk3, set_inputhook, clear_inputhook, current_gui ) diff --git a/IPython/lib/inputhook.py b/IPython/lib/inputhook.py index 54d4ae7..a90f1d6 100644 --- a/IPython/lib/inputhook.py +++ b/IPython/lib/inputhook.py @@ -36,6 +36,7 @@ GUI_TK = 'tk' GUI_OSX = 'osx' GUI_GLUT = 'glut' GUI_PYGLET = 'pyglet' +GUI_GTK3 = 'gtk3' GUI_NONE = 'none' # i.e. disable #----------------------------------------------------------------------------- @@ -421,6 +422,33 @@ class InputHookManager(object): """ self.clear_inputhook() + def enable_gtk3(self, app=None): + """Enable event loop integration with Gtk3 (gir bindings). + + Parameters + ---------- + app : ignored + Ignored, it's only a placeholder to keep the call signature of all + gui activation methods consistent, which simplifies the logic of + supporting magics. + + Notes + ----- + This methods sets the PyOS_InputHook for Gtk3, which allows + the Gtk3 to integrate with terminal based applications like + IPython. + """ + from IPython.lib.inputhookgtk3 import inputhook_gtk3 + self.set_inputhook(inputhook_gtk3) + self._current_gui = GUI_GTK + + def disable_gtk3(self): + """Disable event loop integration with PyGTK. + + This merely sets PyOS_InputHook to NULL. + """ + self.clear_inputhook() + def current_gui(self): """Return a string indicating the currently active GUI or None.""" return self._current_gui @@ -439,6 +467,8 @@ enable_glut = inputhook_manager.enable_glut disable_glut = inputhook_manager.disable_glut enable_pyglet = inputhook_manager.enable_pyglet disable_pyglet = inputhook_manager.disable_pyglet +enable_gtk3 = inputhook_manager.enable_gtk3 +disable_gtk3 = inputhook_manager.disable_gtk3 clear_inputhook = inputhook_manager.clear_inputhook set_inputhook = inputhook_manager.set_inputhook current_gui = inputhook_manager.current_gui @@ -480,6 +510,7 @@ def enable_gui(gui=None, app=None): GUI_QT4: enable_qt4, GUI_GLUT: enable_glut, GUI_PYGLET: enable_pyglet, + GUI_GTK3: enable_gtk3, } try: gui_hook = guis[gui] diff --git a/IPython/lib/inputhookgtk3.py b/IPython/lib/inputhookgtk3.py new file mode 100644 index 0000000..531f5ca --- /dev/null +++ b/IPython/lib/inputhookgtk3.py @@ -0,0 +1,34 @@ +# encoding: utf-8 +""" +Enable Gtk3 to be used interacive by IPython. + +Authors: Thomi Richards +""" +#----------------------------------------------------------------------------- +# Copyright (c) 2012, the IPython Development Team. +# +# Distributed under the terms of the Modified BSD License. +# +# The full license is in the file COPYING.txt, distributed with this software. +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +# Imports +#----------------------------------------------------------------------------- + +import sys +from gi.repository import Gtk, GLib + +#----------------------------------------------------------------------------- +# Code +#----------------------------------------------------------------------------- + +def _main_quit(*args, **kwargs): + Gtk.main_quit() + return False + + +def inputhook_gtk3(): + GLib.io_add_watch(sys.stdin, GLib.IO_IN, _main_quit) + Gtk.main() + return 0 diff --git a/docs/examples/lib/gui-gtk3.py b/docs/examples/lib/gui-gtk3.py new file mode 100644 index 0000000..2e27320 --- /dev/null +++ b/docs/examples/lib/gui-gtk3.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +"""Simple Gtk example to manually test event loop integration. + +This is meant to run tests manually in ipython as: + +In [1]: %gui gtk3 + +In [2]: %run gui-gtk3.py +""" + +from gi.repository import Gtk + + +def hello_world(wigdet, data=None): + print("Hello World") + +def delete_event(widget, event, data=None): + return False + +def destroy(widget, data=None): + Gtk.main_quit() + +window = Gtk.Window(Gtk.WindowType.TOPLEVEL) +window.connect("delete_event", delete_event) +window.connect("destroy", destroy) +button = Gtk.Button("Hello World") +button.connect("clicked", hello_world, None) + +window.add(button) +button.show() +window.show() + +try: + from IPython.lib.inputhook import enable_gtk3 + enable_gtk3() +except ImportError: + Gtk.main()