Hi John, Hi list,
I'm not sure what format you want so I've left it essentially text, with a few HTML markups to make things clearer.
I can't get socket.steal to work reliably, so I haven't demonstrated that. If anyone on pygtk knows how I could reliably embed say an xterm I'd be most grateful. I've tried:


xterm &
xwininfo -name xterm
pasting the given number to
python socket.py 20971536

but the xterm disappears then reappears. On someone else's machine it worked, but I can't work out the difference between his set up and mine (perhaps window manager?)

njh
Plugs and Sockets

Sometimes it is useful to bring in an external application's interface
into your application.  One way of doing this is through the Plug and
Socket interface (another way is via <a href="bonobo">Bonobo</a>).

Plugs

Plugs are a clean way to allow your application to be embeded inside
another.  The main advantage to using a Plug is that you can connect
to the "embedded" signal.

There are a few functions associated with Plugs:

plug = gtk.Plug(socket_id)

Creates a new plug widget inside the Socket identified by
socket_id. If socket_id is 0L, the plug is left "unplugged" and can
later be plugged into Socket s by s.add_id().

plug.get_id ()

Gets the window ID of a GtkPlug widget, which can then be used to
embed this window inside another window, for instance with
gtk_socket_add_id().

Example

<pre>
#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk,sys

Wid = 0L
if len(sys.argv) == 2:
    Wid = long(sys.argv[1])

plug = gtk.Plug(Wid)
print "Plug ID=", plug.get_id()

def embed_event(widget):
    print "I (",widget,") have just been embedded!"

plug.connect("embedded", embed_event)

entry = gtk.Entry()
entry.set_text("hello")
def entry_point(widget):
    print "You've changed my text to '%s'" % widget.get_text()

entry.connect("changed", entry_point)
plug.connect("destroy", gtk.mainquit)

plug.add(entry)
plug.show_all()


gtk.mainloop()
</pre>


Sockets

Socket widgets are the surrounding widget to embed widgets
from another process into your GUI in a fashion that is transparent
to the user. One process creates a Socket widget and, passes the
that widget's window ID to the other process, which then creates a
GtkPlug with that window ID. Any widgets contained in the GtkPlug then
will appear inside the first applications window.

The socket's window ID is obtained by using socket.get_id(). Before
using this function, the socket must have been realized, and for
hence, have been added to its parent.

Note that if you pass the window ID of the socket to another process
that will create a plug in the socket, you must make sure that the
socket widget is not destroyed until that plug is created. Violating
this rule will cause unpredictable consequences, the most likely
consequence being that the plug will appear as a separate toplevel
window.

When GTK+ is notified that the embedded window has been destroyed,
then it will destroy the socket as well. You should always, therefore,
be prepared for your sockets to be destroyed at any time when the main
event loop is running.

The communication between a Socket and a Plug 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.

A socket can also be used to swallow arbitrary pre-existing top-level
windows using gtk_socket_steal(), though the integration when this is
done will not be as close as between a GtkPlug and a GtkSocket.

socket = gtk.Socket()

Create a new empty GtkSocket.

socket.steal(window_id)

gtk_socket_steal is deprecated and should not be used in newly-written
code.  It reparents a pre-existing toplevel window into a
GtkSocket. This is meant to embed clients that do not know about
embedding into a Socket, however doing so is inherently unreliable,
and using this function is not recommended.

The Socket must have already be added into a toplevel window before
you can make this call.

socket.add_id(window_id)

Adds an XEMBED client, such as a Plug, to the Socket. The client may
be in the same process or in a different process.

To embed a Plug in a Socket, you can either create the Plug with

plug = gtk.Plug(0L)

and then pass the number returned by plug.get_id() to

socket.add_id(),

or you can call window_id = socket.get_id() to get the window ID for
the socket, and then create the plug with gtk.Plug(window_id).

The Socket must have already be added into a toplevel window before
you can make this call.

socket.get_id()

Gets the window ID of a Socket widget, which can then be used to
create a client embedded inside the socket, for instance with
gtk.Plug().

The Socket must have already be added into a toplevel window before
you can make this call.


Example:

<pre>
#!/usr/bin/python

import string

import pygtk
pygtk.require('2.0')
import gtk,sys

window = gtk.Window()
window.show()

socket = gtk.Socket()
socket.show()
window.add(socket)

print "Socket ID=", socket.get_id()
window.connect("destroy", gtk.mainquit)

def plugged_event(widget):
    print "I (",widget,") have just had a plug inserted!"

socket.connect("plug-added", plugged_event, "inserted")

if len(sys.argv) == 2:
    socket.add_id(long(sys.argv[1]))

gtk.mainloop()
</pre>

To run the example you can either run plug.py first:

[EMAIL PROTECTED]:~/plug-n-socket$ python plug.py
Plug ID= 20971522

and copy the output ID to the first arg of socket.py

[EMAIL PROTECTED]:~/plug-n-socket$ python socket.py 20971522
Socket ID= 48234523
I ( <gtk.Plug object (GtkPlug) at 0x3008dd78> ) have just been embded!
I ( <gtk.Socket object (GtkSocket) at 0x3008ddf0> ) have just had a plug inserted!

Or you can run socket.py:

[EMAIL PROTECTED]:~/plug-n-socket$ python socket.py
Socket ID= 20971547

and then run plug, copying across the Wid:

[EMAIL PROTECTED]:~/plug-n-socket$ python plug.py 20971547
I ( <gtk.Socket object (GtkSocket) at 0x3008ddf0> ) have just had a plug inserted!
Plug ID= 48234498
_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to