#include <gtk/gtk.h>

#include <stdio.h>
#include <string.h>

static void on_button_clicked(GtkButton *button, gpointer user_data);

static void builder_connect_func(
        GtkBuilder *builder,
        GObject *object,
        const gchar *signal_name,
        const gchar *handler_name,
        GObject *connect_object,
        GConnectFlags flags,
        gpointer user_data
        )
{
    GCallback handler;

    if (0 == strcmp(handler_name, "on_button_clicked")) {
        handler = G_CALLBACK(on_button_clicked);
    } else if (0 == strcmp(handler_name, "on_delete_event")) {
        handler = G_CALLBACK(gtk_main_quit);
    } else {
        printf("Unknown handler name %s\n", handler_name);
        return;
    }

    g_signal_connect_object(object, signal_name, handler, connect_object, flags);
}

static void on_button_clicked(GtkButton *button, gpointer user_data)
{
    printf("Label text: %s\n", gtk_label_get_text(GTK_LABEL(user_data)));
}

int main(int argc, char **argv)
{
    GtkBuilder *builder;
    GtkWidget  *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new_from_file("user_data.glade");
    gtk_builder_connect_signals_full(builder, builder_connect_func, NULL);

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));

    gtk_widget_show_all(window);

    gtk_main();
    return 0;
}
