/*
 * gcc -o plug plug.c -O0 -g `pkg-config --cflags --libs gtk+-3.0`
 */

#include <stdlib.h>
#include <gtk/gtk.h>
#include <gtk/gtkx.h>
#include <gdk/gdkx.h>
#include <cairo/cairo.h>

FILE* flog = NULL;
GtkWidget *plug;
GtkWidget *canvas;

gboolean on_draw(GtkWidget *w, GdkEvent *ev, gpointer data)
{
    cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(w));

    cairo_set_source_rgb(cr, 0, 0, 1);
    cairo_paint(cr);

    cairo_set_source_rgb(cr, 1, 0, 0);
    cairo_rectangle(cr, 0, 0, 
                    gtk_widget_get_allocated_width(w),
                    gtk_widget_get_allocated_height(w));
    cairo_stroke(cr);

    cairo_destroy(cr);
    return TRUE;
}

gboolean on_plug_configure(GtkWidget *w, GdkEventConfigure *ev, gpointer data)
{
    GdkWindow *win = gtk_widget_get_window(plug);
    GdkRectangle r;
    gdk_window_get_frame_extents(win, &r);

    fprintf(flog, "on_plug_configure:   %d %d allocated: %d %d  frame_extents: %d %d %d %d\n",
            gtk_widget_get_allocated_width(w),
            gtk_widget_get_allocated_height(w),
            gdk_window_get_width(win),
            gdk_window_get_height(win),
            r.x, r.y, r.width, r.height);

    return FALSE;
}

int main(int argc, char **argv)
{
    #if 0
    flog = fopen( "/tmp/plug.txt", "w" );
    setbuf(flog, NULL);
    #else
    flog = stderr;
    #endif

    gtk_init(&argc, &argv);

    plug = gtk_plug_new(0);
    canvas = gtk_drawing_area_new();
    gtk_container_add(GTK_CONTAINER(plug), canvas);
    g_signal_connect(canvas, "draw", (GCallback)on_draw, NULL);
    g_signal_connect(plug, "configure-event", (GCallback)on_plug_configure, NULL);
    gtk_widget_show_all(plug);

    g_print("%ld\n", GDK_WINDOW_XID(gtk_widget_get_window (plug)));

    gtk_main();
    return 0;
}

