#include <gtk/gtk.h>
#include <string.h>

#define DISABLE_DEPRECATED

static GtkWidget *entry;

static gboolean
event_delete (GtkWidget *widget, GdkEvent *event)
{
    return FALSE;
}

static gboolean
event_destroy (GtkWidget *widget, GdkEvent *event)
{
    gtk_main_quit ();
    return FALSE;
}

static void
gui_get_text_entry (int length,
		    void(*changedfunc)(),
		    GtkWidget **widget)
{
    GtkWidget *thing;

    thing = gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(thing), length);

    g_signal_connect_after(GTK_EDITABLE(thing), "changed",
			     G_CALLBACK(changedfunc), NULL);

    *widget = thing;
}



static gchar *texts[] = {
    "Text one",
    "Text two",
    "It's me again!",
    ""
};

static void
set_text (GtkWidget* widget, guint number)
{
    gtk_entry_set_text(GTK_ENTRY(entry), texts[number]);
}

static void
entry_changed(void)
{
    const gchar* text = gtk_entry_get_text(GTK_ENTRY(entry));
    g_print("Length: %i, text: %s\n", strlen(text), text);
}

int
main (int argc, char* argv[])
{
    GtkWidget *vbox, *app, *thing;
    
    gtk_init(&argc, &argv);

    app = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(app), "GtkEntry attempt");
    gtk_window_set_policy(GTK_WINDOW(app), FALSE, FALSE, FALSE);

    g_signal_connect(app, "delete-event",
		     G_CALLBACK(event_delete), NULL);
    g_signal_connect(app, "destroy",
		     G_CALLBACK(event_destroy), NULL);

    vbox = gtk_vbox_new(FALSE, 0);
    
    gui_get_text_entry(30, entry_changed, &entry);
    gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
    
    thing = gtk_button_new_with_label("Set text one");
    g_signal_connect(thing, "clicked", G_CALLBACK(set_text), (gpointer)0);
    gtk_box_pack_start(GTK_BOX(vbox), thing, FALSE, FALSE, 0);

    thing = gtk_button_new_with_label("Set text two");
    g_signal_connect(thing, "clicked", G_CALLBACK(set_text), (gpointer)1);
    gtk_box_pack_start(GTK_BOX(vbox), thing, FALSE, FALSE, 0);

    thing = gtk_button_new_with_label("Set text three");
    g_signal_connect(thing, "clicked", G_CALLBACK(set_text), (gpointer)2);
    gtk_box_pack_start(GTK_BOX(vbox), thing, FALSE, FALSE, 0);

    thing = gtk_button_new_with_label("Clear");
    g_signal_connect(thing, "clicked", G_CALLBACK(set_text), (gpointer)3);
    gtk_box_pack_start(GTK_BOX(vbox), thing, FALSE, FALSE, 0);

    gtk_container_add(GTK_CONTAINER(app), vbox);

    gtk_widget_show_all(app);
    
    gtk_main();
    
    return 0;
}
