Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glib signals - How to check if a handler of an instance is already blocked?

Tags:

c

gtk

glib

gobject

After a handler of an instance has been blocked with g_signal_handler_block, is it possible to check if the handler is still being blocked or has been unblocked by g_signal_handler_unblock in the meantime, apart from storing the state in a boolean variable for example?

I hoped something like that would be possible

g_signal_handler_block (selection, handler_id_row_selected);
if (g_signal_handler_is_blocked (selection, handler_id_row_selected))
  g_print ("is still blocked");

But a "g_signal_handler_is_blocked" function does not exist. g_signal_handler_is_connected is not the right function to use, since the signal handler remains connected, thus the function returns TRUE.

I have tried g_signal_handler_find (), since there is G_SIGNAL_MATCH_UNBLOCKED as one of the match types, but it has not worked yet. Even though I have rewritten my code anyway, I still would like to know if it is possible, since i use the blocking/unblocking relatively often.

like image 682
GTK 1.2.6 fanboy Avatar asked Jan 21 '13 00:01

GTK 1.2.6 fanboy


1 Answers

g_signal_handler_find here is working as expected. Here is my test case:

#include <gtk/gtk.h>

gboolean
g_signal_handlers_is_blocked_by_func(gpointer instance, GFunc func, gpointer data)
{
    return g_signal_handler_find(instance,
                                 G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA | G_SIGNAL_MATCH_UNBLOCKED,
                                 0, 0, NULL, func, data) == 0;
}

static void
handler(void)
{
    g_print("handler called\n");
}

static void
switch_blocking(GtkWidget *button)
{
    GFunc func = (GFunc) handler;

    if (g_signal_handlers_is_blocked_by_func(button, func, NULL)) {
        g_signal_handlers_unblock_by_func(button, func, NULL);
        g_print("handler unblocked\n");
    } else {
        g_signal_handlers_block_by_func(button, func, NULL);
        g_print("handler blocked\n");
    }
}

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

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    button = gtk_button_new_with_label("Click me");

    g_signal_connect_after(button, "clicked", G_CALLBACK(switch_blocking), NULL);
    g_signal_connect(button, "clicked", G_CALLBACK(handler), NULL);

    gtk_container_add(GTK_CONTAINER(window), button);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}
like image 168
ntd Avatar answered Sep 21 '22 06:09

ntd