Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a function when user stops entering text

I am writing a small GUI application using Haskell's gtk2hs library and am currently working with the multiline text boxes within it. I have a function which I want to run when the user makes changes to the text within the text box, but don't want them to have to click a button to activate it.

Furthermore, because it is a rather intrusive and processing intensive function (It draws graphics, loads files etc.), I would like it to fire not whenever a user makes any change (which could probably be done with the bufferChanged signal in text buffer I'm guessing?) but when they stop for a few seconds in between changes.

Basically I am wondering if there is something in gtk which is analogous to the way range widgets can have their update policy set to continuous or delayed, but for text boxes

like image 795
Craig Innes Avatar asked Jan 11 '13 12:01

Craig Innes


1 Answers

I don't know anything of the Haskell bindings but in plain C it is quite easy to implement by leveraging a timeout GSource.

#include <gtk/gtk.h>

static guint source_id = 0;

static gboolean do_stuff(gpointer user_data)
{
    g_print("doing stuff...\n");
    return FALSE;
}

static void postpone(void)
{
    if (source_id > 0)
        g_source_remove(source_id);
    source_id = g_timeout_add(1000, do_stuff, NULL);
}

int main(int argc, char **argv)
{
    GtkWidget *window, *text_view;
    GtkTextBuffer *text_buffer;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);

    text_view = gtk_text_view_new();
    gtk_container_add(GTK_CONTAINER(window), text_view);

    text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
    g_signal_connect(text_buffer, "changed", G_CALLBACK(postpone), NULL);

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

The issue of quitting the TextView before the timeout has elapsed is still open though.

like image 82
ntd Avatar answered Sep 23 '22 07:09

ntd