Hello.
I'd like to have a GtkContainer and when the mouse cursor is over the container, I want to show certain child widgets of it and hide them when the mouse cursor is not over the container. To accomplish that, I'm using a GtkEventBox and just adding a GtkBox to it.
My code currently looks like that:
Code:
#include <gtk/gtk.h>
GtkWidget* button1;
static
void on_enter(GtkWidget *widget,
gpointer data)
{
g_print("enter\n");
gtk_widget_set_visible(button1, TRUE);
}
static
void on_leave(GtkWidget *widget,
gpointer data)
{
g_print("leave\n");
gtk_widget_set_visible(button1, FALSE);
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *button2;
GtkWidget *box;
GtkWidget *evt_box;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
button1 = gtk_button_new_with_label ("FOO");
button2 = gtk_button_new_with_label ("BAR");
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
evt_box = gtk_event_box_new();
gtk_widget_set_no_show_all(button1, TRUE);
gtk_box_pack_start(GTK_BOX(box), button1, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(box), button2, TRUE, TRUE, 0);
gtk_container_add(GTK_CONTAINER(evt_box), box);
g_signal_connect(evt_box, "enter-notify-event", G_CALLBACK(on_enter), NULL);
g_signal_connect(evt_box, "leave-notify-event", G_CALLBACK(on_leave), NULL);
gtk_container_add(GTK_CONTAINER(window), evt_box);
/* and the window */
gtk_widget_show_all (window);
gtk_window_resize(GTK_WINDOW(window), 400, 400);
gtk_main ();
return 0;
}
If you compile and run this example, you will notice the following behavior:
- If you move your cursor first in the left side of the big BAR button, you are now over the FOO button and can click it like normal. If you now move your cursor on the BAR button again(which is now smaller and on the right), the FOO button will disappear again(which shouldn't happen).
- Now the interesting part: If you move your cursor over the right half of the BAR button first, the FOO button will now appear and you can click the BAR button like normal. Howerver, once you move your cursor over the FOO button, the program is stuck in an infinite loop of enter/leave events.
Can someone explain to me why this happens and how I can still implement it the way I want?
Thanks in advance.