Re: Handling events in custom widgets



----- Original Message ----
> From: Teodosiy Kirilov <tdik123 googlemail com>
> To: gtkmm-list gnome org
> 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


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