Problem with EventBox enter/leave_notify_event and Buttons



Hello!!

I have a problem with enter_notify_event and leave_notify_event. Let me
explain:

I have an VBox with 5 EventBox widgets, and inside each EventBox I have a
HBox with one Label and 2 Buttons. I connect the signals enter_notify_event
and leave_notify_event of each of the EventBox widgets to a callback and
everything seems to work well: If you move the mouse pointer vertically
between EventBox widgets you can see the events --I print a message from
those mentioned callbacks, and I also modify the Labels making their text
bold on enter_notify_event and normal on leave_notify_event.

But If you move the mouse pointer horizontally --within the same EventBox--
you'll also get enter/leave_notify_events, and this shouldn't happen since
the mouse pointer is always inside the EventBox. More precisely, you'll get
a leave_notify_event when moving the pointer to a button (although the
button is a child of the EventBox) and the corresponding enter_notify_event
when going back from the button's area to another area also inside the
EventBox.

I can solve this by setting above-child propriety of the EventBox to TRUE,
but this way I can't click the buttons :S

Below you can see an example written in Vala --if for some reason you don't
like Vala, I could rewrite it in C... I did it in Vala because it is
quicker.

How could I solve this situation?


private const int CONTROL_SPACING = 8;
private const int FRAME_BORDER = 6;

public class TestWidget : Gtk.HBox {
    public Gtk.Button edit_button;
    public Gtk.Button delete_button;
    public Gtk.Label label;

    private Pango.AttrList attrs_bold;
    private Pango.AttrList attrs_normal;

    public TestWidget (string name) {
        homogeneous = true;
        spacing = CONTROL_SPACING;

        attrs_bold = new Pango.AttrList();
        attrs_bold.insert(Pango.attr_weight_new(Pango.Weight.BOLD));
        attrs_normal = new Pango.AttrList();
        attrs_normal.insert(Pango.attr_weight_new(Pango.Weight.NORMAL));

        edit_button = new Gtk.Button.from_stock(Gtk.Stock.EDIT);
        delete_button = new Gtk.Button.from_stock(Gtk.Stock.DELETE);

        label = new Gtk.Label(name);
        label.set_alignment(0f, 0.5f);

        add(label);
        add(edit_button);
        add(delete_button);
    }

    public bool on_enter_notify_event() {
        label.set_attributes(attrs_bold);

        stdout.printf("enter_notify_event\n");

        return true;
    }

    public bool on_leave_notify_event() {
        label.set_attributes(attrs_normal);

        stdout.printf("leave_notify_event\n");

        return true;
    }
}

public class TestWindow : Gtk.Window {
    private Gtk.VBox widgets_layout = null;
    private Gtk.VBox layout = null;

    public TestWindow() {

        widgets_layout = new Gtk.VBox(false, CONTROL_SPACING);
        widgets_layout.set_border_width(FRAME_BORDER);

        layout = new Gtk.VBox(false, CONTROL_SPACING);
        layout.pack_start(widgets_layout, false);

        add(layout);
    }

    public void add_widget(TestWidget widget) {

        widget.edit_button.clicked.connect(edit_working);
        widget.delete_button.clicked.connect(delete_working);

        Gtk.EventBox event_box = new Gtk.EventBox();
//~         event_box.set_above_child(true);

        event_box.add_events(Gdk.EventMask.ENTER_NOTIFY_MASK |
Gdk.EventMask.LEAVE_NOTIFY_MASK);

        event_box.add(widget);
        event_box.enter_notify_event.connect(widget.on_enter_notify_event);
        event_box.leave_notify_event.connect(widget.on_leave_notify_event);

        widgets_layout.pack_start(event_box, false);
        widgets_layout.show_all();
    }

    public void edit_working() {
        stdout.printf("Edit button is working\n");
    }

    public void delete_working() {
        stdout.printf("Delete button is working\n");
    }
}

public class Test : Object {
    public static int main(string[] args) {
        Gtk.init(ref args);

        TestWindow window = new TestWindow();
        window.title = "enter/leave_notify_event test";
        window.destroy.connect(Gtk.main_quit);

        window.add_widget(new TestWidget("Test 01"));
        window.add_widget(new TestWidget("Test 02"));
        window.add_widget(new TestWidget("Test 03"));
        window.add_widget(new TestWidget("Test 04"));
        window.add_widget(new TestWidget("Test 05"));

        window.show_all();

        Gtk.main();

        return 0;
    }
}

You can compile it using
    valac ./test.vala --pkg=gtk+-2.0
or
    valac ./test.vala --pkg=gtk+-3.0

Thanks a lot for your time.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]