From time to time, it may be useful to be able to embed a widget from another application within your application. gtkmm allows you to do this with the Gtk::Socket and Gtk::Plug classes. It is not anticipated that very many applications will need this functionality, but in the rare case that you need to display a widget that is running in a completely different process, these classes can be very helpful.

The communication between a Socket and a Plug follows the XEmbed protocol. This protocol has also been implemented in other toolkits (e.g. Qt), which allows the same level of integration when embedding a Qt widget in GTK+ or vice versa.

The way that Sockets and Plugs work together is through their window ids. Both a Socket and a Plug have IDs that can be retrieved with their get_id() member functions. The use of these IDs will be explained below in Section 22.1.3 ― Connecting Plugs and Sockets.

22.1.1. Sockets

A Socket is a special kind of container widget that provides the ability to embed widgets from one process into another process in a way that is transparent to the user.

22.1.2. Plugs

A Plug is a special kind of Window that can be plugged into a Socket. Besides the normal properties and methods of Gtk::Window, a Plug provides a constructor that takes the ID of a Socket, which will automatically embed the Plug into the Socket that matches that ID.

Since a Plug is just a special type of Gtk::Window class, you can add containers or widgets to it like you would to any other window.

22.1.3. Connecting Plugs and Sockets

After a Socket or Plug object is realized, you can obtain its ID with its get_id() function. This ID can then be shared with other processes so that other processes know how to connect to each other.

There are two basic strategies that can be used:

  • Create a Socket object in one process and pass the ID of that Socket to another process so that it can create a Plug object by specifying the given Socket ID in its constructor. There is no way to assign a Plug to a particular Socket after creation, so you must pass the Socket ID to the Plug's constructor.
  • Create a Plug independantly from any particular Socket and pass the ID of the Plug to other processes that need to use it. The ID of the Plug can be associated with a particular Socket object using the Socket::add_id() function. This is the approach used in the example below.