[Haskell-cafe] gtk2hs question - derive SettingsWindow from Window

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Thu Dec 18 12:12:05 EST 2008


On Fri, 2008-12-12 at 21:03 +0100, Cetin Sert wrote:
> Hi all,
> 
> For a network manager of sorts I'm working on, I want to derive a
> SettingsWindowClass from the WindowClass present in Gtk2Hs:
> 
> I want (the) instance(s) of the SettingsWindowClass to have a field to
> store connection settings:
> 
> 1) Is it safe to do it like this?

You've missed out the unsafe bit where you make SettingsWindow an
instance of class WindowClass. That bit will not work because the upcast
function is not a proper Haskell function just is just a pointer cast.
Currently the assumption is that each gtk2hs type is backed by a C type
held in a ForeignPtr. We should generalise that to allow subclasses
defined in Haskell.

> class WindowClass self ⇒ SettingsWindowClass self where
>   settingsWindowGetSettings :: self → IO [ConnectionSetting]
>   settingsWindowSetSettings :: self → [ConnectionSetting] → IO ()
> 
> newtype SettingsWindow = SettingsWindow (Window,[ConnectionSetting])

So you can use this newtype, you just can't make it an instance of
WindowClass.

> mkSettingsWindow = SettingsWindow
> unSettingsWindow (SettingsWindow o) = o
> 
> settingsWindowNew :: IO SettingsWindow
> settingsWindowNew = do
>   win ← windowNew
>   return $ mkSettingsWindow (win,[])
> 
> 2) Is this a common practice in gtk2hs usage?

Making records of widgets is pretty common.

> 3) And will GC properly free the memory allocated for the Window
> object? How is this ensured, do ForeignPtr's always call delete on the
> underlying C ptr when they get garbage collected?

Right, exactly.

Duncan



More information about the Haskell-Cafe mailing list