You rock, that worked perfectly.
From: Will Thompson <will@...>
To: Rouan van Dalen <rvdalen@...>
Cc: "gtk2hs-devel@..." <gtk2hs-devel@...>
Sent: Wednesday, 9 January 2013, 11:47
Subject: Re: [Gtk2hs-devel] problems getting the ancestor of an ancestor of a widget
On 09/01/13 09:31, Rouan van Dalen wrote:
> I have an Entry widget inside an EventBox (innerEventBox), and I have
> the innerEventBox inside another
> EventBox (outerEventBox).
> I need to traverse from the Entry widget to the outerEventBox so I can
> set the background color of the
> I have the following code to do this:
> get2ndAncestor :: Widget -> GType -> IO (Maybe Widget)
> get2ndAncestor widget gtype =
> do ancestor1 <- widgetGetAncestor widget gtype
> ancestor2 <- maybe (return Nothing) ((flip
> widgetGetAncestor) gtype) ancestor1
> return ancestor2
> When I use this code, it gets the 1st ancestor correctly, but the 2nd
> ancestor is the same as the 1st.
> The code above never returns the 2nd ancestor.
> The docs for widgetGetAncestor has a line that says:
> "Note that unlike widgetIsAncestor, widgetGetAncestor considers widget
> to be an ancestor of itself."
> I am not sure exactly what that line means, and if it is the cause of my
> problems. If so, how do I get the
> ancestor of the ancestor of a widget?
It is the cause of your problems. You have this hierarchy of widgets:
EventBox (a) -> EventBox (b) -> Entry (c)
'widgetGetAncestor b gTypeEventBox' returns Just b, because "widgetGetAncestor considers widget to be an ancestor of itself". It checks if b is an instance of gTypeEventBox, and since it is, returns it. When you call 'widgetGetAncestor c gTypeEventBox', it checks whether c is an EventBox, finds it is not, and recurses upwards.
If you know that the widget you want is exactly the grandparent of the widget you're starting from, you should be able to use widgetGetParent twice to get the EventBox you want.
It might be less fragile to keep hold of the EventBox you're looking for when you initially create your UI, so that you don't have to rely on the exact widget structure to find what you're looking for.