Re: Event Propagation problem



Hi Carlos,

Glib::SignalProxy1<...> with its connect() method is documented here:
http://developer.gnome.org/glibmm/unstable/classGlib_1_1SignalProxy1.html

When I look at your code I notice that my previous long reply was not long enough. I had forgotten the possibility to override default signal handlers, as described here:
http://developer.gnome.org/gtkmm-tutorial/stable/sec-overriding-default-signal-handlers.html.en

That's actually what you have done, when you have overridden the virtual functions Gtk::Widget::on_key_press_event() and on_key_release_event() in Toolbox. The calls to signal_key_[press|release]_event().connect() in Toolbox are unnecessary, and I recommend that you delete them. I think that your Toolbox::on_key_[press|release]_event() functions are called twice, when they return false. (Once because of the signal handlers you connect, then again (if false is returned) because they are part of the default signal handling.) No disaster follows, but it's unnecessary.

Kjell

2012-01-13 21:58, Carlos Lopez Gonzalez skrev:
Yeah! FInally it works!
My top level Gtk::Window is called ToolBox:  class Toolbox : public Gtk::Window so I added the signal connections as you mentioned, in the constructor:
    signal_key_press_event().connect(sigc::mem_fun(*this, &Toolbox::on_key_press_event), false);
    signal_key_release_event().connect(sigc::mem_fun(*this, &Toolbox::on_key_release_event), false);
The ToolBox only has one member instance of class Widget_Distance: public Gtk::SpinButton.
This is one of the widgets where I want to stop the event propagation.
So I added two members to the Widget_Distance to handle the press and release key events:
bool
Widget_Distance::on_key_press_event(GdkEventKey* event)
{
    return SpinButton::on_key_press_event(event);
}

bool
Widget_Distance::on_key_release_event(GdkEventKey* event)
{
    return SpinButton::on_key_release_event(event);
}

It calls the SpinButton event handlers, so when the SpinButton  doesn't handle the event (false) then it returns the same value.

Then I added the two other event handlers needed in the ToolBox:

bool
Toolbox::on_key_press_event(GdkEventKey* event)
{
    if(widget_defaults->get_widget_bline_width()->is_focus())
        return widget_defaults->get_widget_bline_width()->on_key_press_event(event);
    return Gtk::Window::on_key_press_event(event);
}

bool
Toolbox::on_key_release_event(GdkEventKey* event)
{
    if(widget_defaults->get_widget_bline_width()->is_focus())
        return widget_defaults->get_widget_bline_width()->on_key_release_event(event);
    return Gtk::Window::on_key_release_event(event);
}

Notice that I first lookup if the widget_bline_width (which is a instance of a Widget_Distance class) is focus. When it is focus it is being edited so I don't want to handle the key events if the widget already handled. So if Widget_Distance handler returns false then the event is propagated away and it is cached by the defined Accelerators. If the Widget_Distance handler returns true the key event doesn't need other handler and its propagation is stopped.
Now, if the Widget_Distance is not on focus, the Toolbox key event handler will as to the Gtk::Window for the handling occurring the same things.

Thank you very much for the explanations to you all.

One final question: the 'after' parameter in the connect member is documented? if so, where is it?
Greetings
Carlos




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