ruby_object
ruby_object

Reputation: 1266

How do I cast Widget to Label in Haskell's GI-Gtk?

I have this sample code where I have a ListBox containing ListBoxRows, which in turn contain a Label. When I click on the ListBox, I get a ListBoxRow. So far so good. The problems start when I want to interact with the ListBoxRows children.

I have used this function to get the Label, which is the child of the ListBoxRow. https://hackage.haskell.org/package/gi-gtk-3.0.18/docs/GI-Gtk-Objects-Container.html#g:13

However, the returned type is Widget. How do I convert the type of the object? Function widgetGetName tells me it is a label, but the Haskell Type system insists it is a Widget, so I can not use label specific functions.

working code

  _ <- onListBoxRowSelected listbox2 (\(Just r) -> do
                                         cc <- containerGetChildren r
                                         mlabel <- castTo Label (head cc)
                                         case mlabel of
                                           Nothing -> putStrLn "Not a label!"
                                           Just label -> (labelGetText label) >>= putStrLn . unpack)

Thanks to Dan

Upvotes: 0

Views: 444

Answers (1)

Dan Robertson
Dan Robertson

Reputation: 4360

Try this:

cc <- containerGetChildren r
mlabel <- castTo Label (head cc)
case mlabel of
  Nothing -> putStrLn “Not a label!”
  Just label -> labelGetText label >>= putStrLn

Upvotes: 1

Related Questions