Search code examples
pythonvimpysidepygobject

pyside embed vim


I know to embed vim in a Gtk application using sockets like the following snippet

from gi.repository import Gtk
import subprocess

win=Gtk.Window()
win.set_default_size(600,800)
win.connect('delete-event', Gtk.main_quit)
editor = Gtk.Socket()
win.add(editor)
editor.connect("plug-removed", Gtk.main_quit)
subprocess.Popen(["/usr/bin/gvim", \
        "--socketid", str(editor.get_id())])
win.show_all()
Gtk.main()

How does one do this in PySide? I could not find any reference to sockets in pyside.

UPDATE (using JimP's idea)

The following code embeds a gvim instance in a Pyside widget. However the gvim window does not seem to resize when to the full size of the parent window.

import sys
from PySide import QtGui
from PySide import QtCore

app = QtGui.QApplication(sys.argv)    
win = QtGui.QWidget()
win.resize(600, 800)

container = QtGui.QX11EmbedContainer(win)
container.show()
QtCore.QObject.connect(container, 
    QtCore.SIGNAL("clientClosed()"), 
    QtCore.QCoreApplication.instance().quit)
winId = container.winId()
process = QtCore.QProcess(container)
options = ["--socketid", str(winId)]
process.start("gvim", options)

win.show()    
sys.exit(app.exec_())

Solution

  • I think the key to getting this working would be translating GTK speak to QT speak. Google around your code, I see that Gtk.Socket says:

    The communication between a GtkSocket and a GtkPlug follows the XEmbed protocol. This protocol has also been implemented in other toolkits, e.g. Qt, allowing the same level of integration when embedding a Qt widget in GTK or vice versa.

    So then the question becomes what does QT call their XEmbed classes? Google around I found QX11EmbedContainer which says:

    It is possible for PySide.QtGui.QX11EmbedContainer to embed XEmbed widgets from toolkits other than Qt, such as GTK+. Arbitrary (non-XEmbed) X11 widgets can also be embedded, but the XEmbed-specific features such as window activation and focus handling are then lost.

    The GTK+ equivalent of PySide.QtGui.QX11EmbedContainer is GtkSocket. The corresponding KDE 3 widget is called QXEmbed.

    I'm not running PySide at the moment, but that page on QX11EmbedContainer contains some example C++ code that I think will get you where you need to be. You will need to translate the C++ to Python, but I don't that will be too hard.