< Previous | Next >
October 24, 2008 1:29 AM CDT by psilord in category Unfinished Junk

Vim All Up In Your Grill

Have you ever wanted to embed gvim into a GTK+ 2.0 application that you've been writing? Wouldn't it be so nice if it just showed up in your container widget and made comfortable cooing noises as it acted like the perfect editor for you while your strew all of your widgets and crap around it like so many failed dreams in your life?

Well, today is your lucky day! For reasons I will not elaborate upon, except to say I am not allowed in Utah anymore, I've found out how to do this.

Of course, the first place one would think to look for this knowledge is the GTK+ 2.0 tutorial. There is a lot of information there, so how about we go look at the one page we need, which apparently is on GTK Plugs and Sockets.

Oops. I guess that tutorial page isn't done yet. Sadness 10000. Maybe vim can shed some knowledge in their free time destroying help pages.

Stumbling around in vim's help like a drunkard looking for a place to urinate, I find two very helpful pages on how to do this.

By very helpful, I mean "WTF?!?". That helps me about as much as a McDonalds menu written in Linear A. However, I now know how to get gvim to do something interesting with a magical incantation, but who's picking up the phone on the other end? What does the phone even look like?

Huzzah! I find a dirty wad of intellectual gum that I can chew on for a while. The information to which that page leads is technically correct, but simultaneously hard to use--like an implicit surface equation. Armed with this knowledge, however, let's see if I can do something.

After sufficient screwage with the interface, I wrote a piece of trash which accomplishes the task. There were previous more buggy revisions of this code sitting in this very entry, along with other text. But, thank god for the internet easily enabling revisioninst history. If it works for major onilne news publications, it'll work for me.

Compile this program in the usual GTK+ 2.0 manner:

/* Written by Peter Keller <psilord@cs.wisc.edu> Oct 2008 */
/* This code is a piece of trash but is under the BSD license. */

#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <gtk/gtksocket.h>

void plug_add_cb(GtkSocket *socket_, gpointer user_data)
{
    g_print("Added a plug!\n");
}

gboolean plug_removed_cb(GtkSocket *socket_, gpointer user_data)
{
    g_print("Removed a plug!\n");

    /* be able to reuse this socket */
    return TRUE;
}

int main(int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *gtksock;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request (GTK_WIDGET (window), 200, 200);
    gtk_window_set_title (GTK_WINDOW (window), "Embedded vim test");
    g_signal_connect (G_OBJECT (window), "delete_event",
        G_CALLBACK(gtk_main_quit), NULL);
    g_signal_connect (G_OBJECT (window), "destroy",
        G_CALLBACK(gtk_main_quit), NULL);

    gtksock = gtk_socket_new();
    gtk_widget_set_size_request (GTK_WIDGET(gtksock), 200, 200);

    g_signal_connect(GTK_OBJECT(gtksock), "plug_added",
        G_CALLBACK(plug_add_cb), NULL);

    g_signal_connect(GTK_OBJECT(gtksock), "plug_removed",
        G_CALLBACK(plug_removed_cb), NULL);

    gtk_container_add(GTK_CONTAINER(window), gtksock);

    gtk_widget_show(gtksock);
    gtk_widget_show(window);

    g_print("The ID of the socket window is 0x%x\n"
            "Run the command 'gvim --socketid 0x%x'\n",
        gtk_socket_get_id(GTK_SOCKET(gtksock)),
        gtk_socket_get_id(GTK_SOCKET(gtksock)));

    gtk_main();

    return EXIT_SUCCESS;
}

And follow the directions when you run it. If it works, you'll see the gvim window open up in the idle window.

Somewhere, a dog barks.

End of Line.

< Previous | Next >