Skip to content

Instantly share code, notes, and snippets.

@bert
Created February 3, 2011 06:29
Show Gist options
  • Save bert/809126 to your computer and use it in GitHub Desktop.
Save bert/809126 to your computer and use it in GitHub Desktop.
Embedding applications via XEmbed
From: [email protected] [mailto:[email protected]] On Behalf Of Andy Spencer
Sent: Friday, January 21, 2011 5:24 AM
To: Michael Treibton
Cc: [email protected]
Subject: Re: Embedding applications via XEmbed
On 2011-01-20 20:52, Michael Treibton wrote:
> I've read about GtkSocket and GtkPlug, but the examples for the
> official documentation don't make it clear to me how to go about this.
>
> Can someone outline more fully how this would work?
Most likely you want to do something like this:
1. Create a GtkSocket as part of your application
2. Get the GtkSocket's window ID
3. Pass the window ID to the application you want to embed
Gtk and the other application should take care of the rest. A
common way to pass the window id is with a flag when you
spawn the other program.
For example `xterm -into <id>' or `mplayer -wid <id>'.
> If someone could provide an example of how this is handled, i'd much
> appreciate it.
I've attached a small example I wrote a while back that
embeds a gVim window inside an GTK+ application. You should
be able to substitute any other XEmbed application for gVim.
You can just print out the window id and the spawn the other
application manually if you want.
#include <gtk/gtk.h>
#define EXPR "'GoHello, World!<Esc><C-O>'"
void send_hello(GtkButton *btn, gint id)
{
gchar *command = g_strdup_printf(
"gvim --servername %d --remote-send " EXPR, id);
g_spawn_command_line_async(command, NULL);
}
gint main(gint argc, gchar **argv)
{
gtk_init(&argc, &argv);
/* Create window */
GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
GtkWidget *sock = gtk_socket_new();
GtkWidget *btn = gtk_button_new_with_label("Hello, World!");
g_signal_connect(sock, "plug-removed", gtk_main_quit, NULL);
g_signal_connect(win, "delete-event", gtk_main_quit, NULL);
gtk_widget_set_size_request(sock, 200, 200);
gtk_box_pack_start(GTK_BOX(vbox), sock, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), btn, FALSE, TRUE, 0);
gtk_container_add(GTK_CONTAINER(win), vbox);
gtk_widget_show_all(win);
/* Embed vim */
GdkNativeWindow id = gtk_socket_get_id(GTK_SOCKET(sock));
gchar *command = g_strdup_printf(
"gvim --servername %d --socketid %d", id, id);
g_spawn_command_line_async(command, NULL);
g_signal_connect(btn, "clicked", G_CALLBACK(send_hello), (gpointer)id);
/* Run */
gtk_main();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment