隐藏GtkEventBox与光标更改 [英] Invisble GtkEventBox vs cursor change

查看:225
本文介绍了隐藏GtkEventBox与光标更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,有时我需要在进程正在进行时禁用大部分按钮和事件框(当然除了取消按钮除外)。每个事件框都包含一个可以点击的标签。为了让用户理解这些标签是可点击的,我在文本上加了下划线,并且在悬停在这些标签上时使光标发生了变化。



问题是,当我禁用一个事件框(使其不敏感),你可以看到一个相当丑陋的神器:


因此,我搜索并找到了这个函数: gtk_event_box_set_visible_window 。注意:我正在使用(我必须,不幸)Gtk 2.22,但他们只是从他们的网站上删除了文档。无论如何,这个函数的文本是一样的。



根据这个函数,你可以使事件框创建一个 GDK_INPUT_ONLY 窗口。如果我这样做,那么禁用事件框不会让它变得丑陋。



但是,由于事件框现在没有可输出窗口,因此

  gdk_window_set_cursor(event_box->窗口,光标); 

可以改变整个窗口的光标,而不仅仅是事件框。

我可以在窗口中看到没有可见窗口和光标变化之间的矛盾,但是我的问题是如何,否则我可以将光标切换到事件框,但在事件框被禁用时看不到可见的工件? 我试过了不同的方法,比如将事件框的背景改为透明等,但所有这些方法都很复杂。



我发现的最简单的解决方案如下: p>

  static GdkCursor * _normal_cursor = NULL; 
static GdkCursor * _hand_cursor = NULL;
$ b / * in main * /
_normal_cursor = gdk_window_get_cursor(widgets_to_remember-> window-> window);
_hand_cursor = gdk_cursor_new(GDK_HAND2);

/ *创建事件框* /
gtk_event_box_set_visible_window(GTK_EVENT_BOX(event_box),FALSE);
gtk_widget_set_events(event_box,GDK_BUTTON_PRESS_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);

_fix_event_box(event_box,window);
/ *主要其余部分* /

static gboolean _set_hand(GtkWidget * w,GdkEventCrossing * e,gpointer data)
{
gdk_window_set_cursor(w-> window ,_hand_cursor);
返回TRUE;


static gboolean _set_normal(gtkWidget * w,GdkEventCrossing * e,gpointer data)
{
gdk_window_set_cursor(w-> window,_normal_cursor);
返回TRUE;


static void _fix_event_box(GtkWidget * eb,GtkWidget * window)
{
g_signal_connect_swapped(eb,enter_notify_event,G_CALLBACK(_set_hand),window);
g_signal_connect_swapped(eb,leave_notify_event,G_CALLBACK(_set_normal),window);
}

这基本上做的是将事件框设置为不可见,然后将其 enter-notify-event leave-notify-event 信号处理程序在鼠标进入或离开时更改窗口光标窗口。


In my application, sometimes I need to disable most of the buttons and event boxes while a process is taking place (except the "cancel" button of course). Each event box contains a label which can be clicked. To make the user understand that these labels are clickable, I have underlined the texts and have made the cursor change when hovered over those labels.

The problem is that when I disable an event box (make it insensitive), you can see a rather ugly artifact:

So, I searched and found this function: gtk_event_box_set_visible_window. Note: I'm using (I have to, unfortunately) Gtk 2.22, but they just removed the documentation from their website. Anyway, the text of this function is the same.

According to this function, you can make the event box create a GDK_INPUT_ONLY window. If I do so, then disabling the event box doesn't make it ugly anymore.

However, since the event box now doesn't have an outputable window, the

gdk_window_set_cursor(event_box->window, cursor);

makes the cursor change for the whole window instead of just the event box.

I can somewhat see the contradiction between no visible window and cursor change over window, but my question is how, otherwise, can I have the cursor change over the event box, but don't see a visible artifact when the event box is disabled?

解决方案

I tried different methods, such as changing the background of the event box to transparent etc, but all of them were quite complicated.

The simplest solution I found was the following:

static GdkCursor *_normal_cursor = NULL;
static GdkCursor *_hand_cursor = NULL;

/* in main */
    _normal_cursor = gdk_window_get_cursor(widgets_to_remember->window->window);
    _hand_cursor = gdk_cursor_new(GDK_HAND2);

    /* create the event box */
    gtk_event_box_set_visible_window(GTK_EVENT_BOX(event_box), FALSE);
    gtk_widget_set_events(event_box, GDK_BUTTON_PRESS_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);

    _fix_event_box(event_box, window);
/* rest of main */

static gboolean _set_hand(GtkWidget *w, GdkEventCrossing *e, gpointer data)
{
    gdk_window_set_cursor(w->window, _hand_cursor);
    return TRUE;
}

static gboolean _set_normal(GtkWidget *w, GdkEventCrossing *e, gpointer data)
{
    gdk_window_set_cursor(w->window, _normal_cursor);
    return TRUE;
}

static void _fix_event_box(GtkWidget *eb, GtkWidget *window)
{
    g_signal_connect_swapped(eb, "enter_notify_event", G_CALLBACK(_set_hand), window);
    g_signal_connect_swapped(eb, "leave_notify_event", G_CALLBACK(_set_normal), window);
}

What this basically does is set the event box invisible, and then set its enter-notify-event and leave-notify-event signal handlers to change the window cursor when the mouse enters or leaves their window.

这篇关于隐藏GtkEventBox与光标更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆