#include <glib.h>
#include <gdk/gdk.h>

GMainLoop *mainloop;
GdkWindow *win1;

static void event_func(GdkEvent *ev, gpointer data) {
    switch (ev->type) {
        case GDK_DELETE:
            g_main_loop_quit(mainloop);
            break;
        case GDK_EXPOSE: {
            GdkWindow *w = gdk_event_get_window(ev);
            gdk_window_begin_paint_region(w, ((GdkEventExpose *)ev)->region);
            cairo_t *cr = gdk_cairo_create(w);

            cairo_set_source_rgb(cr, 1, 1, 1);
            cairo_set_line_width(cr, 1);

            cairo_rectangle(cr, 0, 0, gdk_window_get_width(w), gdk_window_get_height(w));
            cairo_fill(cr);
            cairo_destroy(cr);
            gdk_window_end_paint(w);
        }
        break;
        default:
            break;
    }
}

int main(int argc, char **argv) {
    gdk_init(&argc, &argv);

    GdkWindowAttr attr;
    attr.title = argv[0];
    attr.event_mask = GDK_EXPOSURE_MASK;
    attr.window_type = GDK_WINDOW_TOPLEVEL;
    attr.wclass = GDK_INPUT_OUTPUT;
    attr.width = 400;
    attr.height = 300;
    
    // Setting these fields causes GDK to create a window of incorrect size.
    attr.x = 500;
    attr.y = 500;
    win1 = gdk_window_new(NULL, &attr, GDK_WA_X|GDK_WA_Y);
    
    gdk_event_handler_set(event_func, NULL, NULL);
    gdk_window_show(win1);

    mainloop = g_main_loop_new(g_main_context_default(), FALSE);
    g_main_loop_run(mainloop);

    gdk_window_destroy(win1);

    return 0;
}