Handling events in custom widgets

View: New views
5 Messages — Rating Filter:   Alert me  

Handling events in custom widgets

by Teodosiy Kirilov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm trying to create a custom widget, which needs to also handle
keyboard and mouse input.

Basically I'm doing the same as the example in chapter 25.2 from the
book, but I have 3 instances of my widget - e.g
| Gtk::Paned* pane1 = new Gtk::HPaned;
| Gtk::Paned* pane2 = new Gtk::VPaned;
| Gtk::Widget* w1 = new MyWidget();
| Gtk::Widget* w2 = new MyWidget();
| Gtk::Widget* w3 = new MyWidget();
| add(m_VBox);
| m_VBox.pack_start(*Gtk::manage(pane1), Gtk::PACK_EXPAND_WIDGET);
| pane1->pack1(*Gtk::manage(w1), Gtk::EXPAND);
| pane1->pack2(*Gtk::manage(pane2), Gtk::EXPAND);
| pane2->pack1(*Gtk::manage(w2), Gtk::EXPAND);
| pane2->pack2(*Gtk::manage(w3), Gtk::EXPAND);

So what I try to do is handle keyboard and mouse input in the widget
under the mouse cursor - just like other widgets (e.g Gtk::Entry)
The problem is the event handlers in the MyWidget-Class (e.g
on_key_press_event) are not triggered at all. If I set the
CAN_FOCUS-flag of MyWidget, the w1 handles the keyboard input. I've
tried other flags, but with no success.
Setting event mask of the Gdk::Window
appropriately(m_refGdkWindow->set_events(...)) didn't help also.

What do I need to do in order to get my widget signaled when keyboard
and mouse events occur?

Thanks in advance
_______________________________________________
gtkmm-list mailing list
gtkmm-list@...
http://mail.gnome.org/mailman/listinfo/gtkmm-list

Parent Message unknown Re: Handling events in custom widgets

by Clive Hobson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

----- Original Message ----

> From: Teodosiy Kirilov <tdik123@...>
> To: gtkmm-list@...
> Sent: Friday, 20 June, 2008 9:32:43 PM
> Subject: Handling events in custom widgets
>
> I'm trying to create a custom widget, which needs to also handle
> keyboard and mouse input.
>
> Basically I'm doing the same as the example in chapter 25.2 from the
> book, but I have 3 instances of my widget - e.g
> | Gtk::Paned* pane1 = new Gtk::HPaned;
> | Gtk::Paned* pane2 = new Gtk::VPaned;
> | Gtk::Widget* w1 = new MyWidget();
> | Gtk::Widget* w2 = new MyWidget();
> | Gtk::Widget* w3 = new MyWidget();
> | add(m_VBox);
> | m_VBox.pack_start(*Gtk::manage(pane1), Gtk::PACK_EXPAND_WIDGET);
> | pane1->pack1(*Gtk::manage(w1), Gtk::EXPAND);
> | pane1->pack2(*Gtk::manage(pane2), Gtk::EXPAND);
> | pane2->pack1(*Gtk::manage(w2), Gtk::EXPAND);
> | pane2->pack2(*Gtk::manage(w3), Gtk::EXPAND);
>
> So what I try to do is handle keyboard and mouse input in the widget
> under the mouse cursor - just like other widgets (e.g Gtk::Entry)
> The problem is the event handlers in the MyWidget-Class (e.g
> on_key_press_event) are not triggered at all. If I set the
> CAN_FOCUS-flag of MyWidget, the w1 handles the keyboard input. I've
> tried other flags, but with no success.
> Setting event mask of the Gdk::Window
> appropriately(m_refGdkWindow->set_events(...)) didn't help also.
>
> What do I need to do in order to get my widget signaled when keyboard
> and mouse events occur?
[snip]

What is the parent widget you are deriving from?

Not all widgets have Gdk/X windows for receiving events, see
http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/chapter-widgets-without-xwindows.html
Which talks about Gtk::EventBox that you can use to get the events.

Alternatively, you can derive from a widget that already creates a Gdk/X window and use add_events()/set_events().  The tutorial section you mentioned (http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/sec-custom-widgets.html) has the code in on_realize() to create the window and set it for the widget.

Regards,
Clive



      Get the name you always wanted with the new y7mail email address.
www.yahoo7.com.au/mail
_______________________________________________
gtkmm-list mailing list
gtkmm-list@...
http://mail.gnome.org/mailman/listinfo/gtkmm-list

Parent Message unknown Re: Handling events in custom widgets

by Clive Hobson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

----- Original Message ----

> From: Teodosiy Kirilov <tdik123@...>
> To: gtkmm-list@...
> Sent: Friday, 20 June, 2008 9:32:43 PM
> Subject: Handling events in custom widgets
>
> I'm trying to create a custom widget, which needs to also handle
> keyboard and mouse input.
>
> Basically I'm doing the same as the example in chapter 25.2 from the
> book, but I have 3 instances of my widget - e.g
> | Gtk::Paned* pane1 = new Gtk::HPaned;
> | Gtk::Paned* pane2 = new Gtk::VPaned;
> | Gtk::Widget* w1 = new MyWidget();
> | Gtk::Widget* w2 = new MyWidget();
> | Gtk::Widget* w3 = new MyWidget();
> | add(m_VBox);
> | m_VBox.pack_start(*Gtk::manage(pane1), Gtk::PACK_EXPAND_WIDGET);
> | pane1->pack1(*Gtk::manage(w1), Gtk::EXPAND);
> | pane1->pack2(*Gtk::manage(pane2), Gtk::EXPAND);
> | pane2->pack1(*Gtk::manage(w2), Gtk::EXPAND);
> | pane2->pack2(*Gtk::manage(w3), Gtk::EXPAND);
>
> So what I try to do is handle keyboard and mouse input in the widget
> under the mouse cursor - just like other widgets (e.g Gtk::Entry)
> The problem is the event handlers in the MyWidget-Class (e.g
> on_key_press_event) are not triggered at all. If I set the
> CAN_FOCUS-flag of MyWidget, the w1 handles the keyboard input. I've
> tried other flags, but with no success.
> Setting event mask of the Gdk::Window
> appropriately(m_refGdkWindow->set_events(...)) didn't help also.
>
> What do I need to do in order to get my widget signaled when keyboard
> and mouse events occur?
[snip]

What is the parent widget you are deriving from?

Not all widgets have Gdk/X windows for receiving events, see
http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/chapter-widgets-without-xwindows.html
Which talks about Gtk::EventBox that you can use to get the events.

Alternatively, you can derive from a widget that already creates a Gdk/X window and use add_events()/set_events().  The tutorial section you mentioned (http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/sec-custom-widgets.html) has the code in on_realize() to create the window and set it for the widget.

Regards,
Clive



      Get the name you always wanted with the new y7mail email address.
www.yahoo7.com.au/mail
_______________________________________________
gtkmm-list mailing list
gtkmm-list@...
http://mail.gnome.org/mailman/listinfo/gtkmm-list

Re: Handling events in custom widgets

by Teodosiy Kirilov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Jun 23, 2008 at 2:31 PM, Clive Hobson wrote:
> ...
> Alternatively, you can derive from a widget that already creates a Gdk/X window and use add_events()/set_events(). The tutorial section you mentioned (http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/sec-custom-widgets.html) has the code in on_realize() to create the window and set it for the widget.

That's the first thing I did (inherited from DrawingArea). But then
for some reasons I decided  to inherit from Gtk::Widget and create the
underlying window myself.
I've created the window with the values from the tutorial example and
 m_refGdkWindow = Gdk::Window::create(get_parent_window(),
&attributes, GDK_WA_X | GDK_WA_Y);
 m_refGdkWindow->set_events(Gdk::EXPOSURE_MASK | Gdk::BUTTON_PRESS_MASK
                                  | Gdk::KEY_PRESS_MASK |
Gdk::ENTER_NOTIFY_MASK );
 unset_flags(Gtk::NO_WINDOW);
 set_window(m_refGdkWindow);

also tried calling Gtk::Widget::add_events(), setting window class and
other things but with no success. So probably there is something
missing...

Anyway, when I'm inheriting from DrawingArea, everything works as
expected - the event-handlers like e.g on_motion_notify() are
triggered. I had to set the CAN_FOCUS flag and put a "grab_focus()" in
on_enter_notify_event in order to handle keyboard input within the
hovered widget, but I guess there is no easier way... I even got rid
of my earlyer problem by disabling the double buffering of the widget
(...).

But it's still interesting to know:
What do I need to do to convert something that inherits from
DrawingArea to something only inheriting from Widget, preserving all
functionality.


Regards
Teodosiy
_______________________________________________
gtkmm-list mailing list
gtkmm-list@...
http://mail.gnome.org/mailman/listinfo/gtkmm-list

Parent Message unknown Re: Handling events in custom widgets

by Clive Hobson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sorry for the delayed response...

> From: Teodosiy Kirilov <tdik123@...>
> Sent: Tuesday, 24 June, 2008 2:17:45 AM
> Subject: Re: Handling events in custom widgets
>
> On Mon, Jun 23, 2008 at 2:31 PM, Clive Hobson wrote:
> > ...
> > Alternatively, you can derive from a widget that already creates a Gdk/X
> window and use add_events()/set_events(). The tutorial section you mentioned
> (http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/sec-custom-widgets.html)
> has the code in on_realize() to create the window and set it for the widget.
>
> That's the first thing I did (inherited from DrawingArea). But then
> for some reasons I decided  to inherit from Gtk::Widget and create the
> underlying window myself.
> I've created the window with the values from the tutorial example and
> m_refGdkWindow = Gdk::Window::create(get_parent_window(),
> &attributes, GDK_WA_X | GDK_WA_Y);
> m_refGdkWindow->set_events(Gdk::EXPOSURE_MASK | Gdk::BUTTON_PRESS_MASK
>                                   | Gdk::KEY_PRESS_MASK |
> Gdk::ENTER_NOTIFY_MASK );
> unset_flags(Gtk::NO_WINDOW);
> set_window(m_refGdkWindow);
>
> also tried calling Gtk::Widget::add_events(), setting window class and
> other things but with no success. So probably there is something
> missing...

I ran into a similar problem (events didn't arrive) when inheriting from Gtk::Bin, but didn't investigate further because for other reasons I changed to using a child widget for drawing/events and had the child inherit from Gtk::DrawingArea.

>
> Anyway, when I'm inheriting from DrawingArea, everything works as
> expected - the event-handlers like e.g on_motion_notify() are
> triggered. I had to set the CAN_FOCUS flag and put a "grab_focus()" in
> on_enter_notify_event in order to handle keyboard input within the
> hovered widget, but I guess there is no easier way... I even got rid
> of my earlyer problem by disabling the double buffering of the widget
> (...).

[small aside]: It might annoy people who prefer click-to-focus if your widget takes keyboard focus every time the mouse enters.

>
> But it's still interesting to know:
> What do I need to do to convert something that inherits from
> DrawingArea to something only inheriting from Widget, preserving all
> functionality.
[snip]
Sorry, I can't help with an explanation.  However, I think that's the whole point of Gtk::DrawingArea - to be a Gtk::Widget with convenient access to events and drawing.

Regards,
Clive



      Get the name you always wanted with the new y7mail email address.
www.yahoo7.com.au/mail
_______________________________________________
gtkmm-list mailing list
gtkmm-list@...
http://mail.gnome.org/mailman/listinfo/gtkmm-list
LightInTheBox - Buy quality products at wholesale price