6

在实例g_signal_handler_block的处理程序被 阻塞后,除了将状态存储在布尔变量中之外,是否可以检查处理程序是否仍然被阻塞或同时被 g_signal_handler_unblock 解除阻塞?

我希望这样的事情是可能的

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");

但是"g_signal_handler_is_blocked"函数不存在。g_signal_handler_is_connected不是使用正确的函数,因为信号处理程序保持连接,因此该函数返回 TRUE。

我试过了g_signal_handler_find (),因为有G_SIGNAL_MATCH_UNBLOCKED一种匹配类型,但它还没有奏效。即使我已经重写了我的代码,我仍然想知道它是否可能,因为我相对经常使用阻塞/解除阻塞。

4

1 回答 1

2

g_signal_handler_find这里按预期工作。这是我的测试用例:

#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;
}
于 2013-01-21T22:46:06.967 回答