From: Jeremy O'D. <jer...@gm...> - 2012-02-28 10:37:36
|
Hi, On 28 February 2012 03:28, Alexander McPhail < has...@gm...> wrote: > Hi, > > Using GTK, the following works > > <code> > > -- | create a new 'Figure' plot > -- click on the window to save > plotNew :: FigureHandle -> IO DrawingArea > plotNew f = do > canvas <- drawingAreaNew > > set canvas [maybeFigure := (Just f)] > > return canvas > > > ----------------------------------------------------------------------------- > > -- | the figure attribute > figure :: Attr DrawingArea FigureState > figure = newAttr getFigure setFigure > where getFigure o = do > Just f <- get o maybeFigure > readMVar f > setFigure o f = set o [maybeFigure :~> (\(Just h) -> do > modifyMVar_ > h (\_ -> return f) > return $ > Just h)] > > > ----------------------------------------------------------------------------- > > maybeFigure :: Attr DrawingArea (Maybe FigureHandle) > maybeFigure = unsafePerformIO $ objectCreateAttribute > {-# NOINLINE maybeFigure #-} > > > ----------------------------------------------------------------------------- > > </code> > > I am attempting to port this code to WxHaskell but can not see how to > create a new attribute for the `FigureHandle`. > > This is the code I have so far > > type FigureHandle = MVar FigureState > > > ----------------------------------------------------------------------------- > -- | create a new 'Figure' plot > -- click on the window to save > plot :: Frame () -> FigureHandle -> IO (Panel ()) > plot fr f = do > p <- panel fr [figure := f] > set p [on paint := paintFigure p] > return p > > paintFigure :: Panel () -> DC a -> Rect -> IO () > > -- | the figure attribute > figure :: Attr (Panel a) FigureHandle > figure = newAttr "figure" getFigure setFigure > where getFigure o = do > readMVar figureHandle > setFigure o f = do > modifyMVar_ figureHandle (\_ -> return f) > You may find this entry from my blog helpful: http://wewantarock.wordpress.com/2010/01/11/custom-controls-in-wxhaskell-part-3/ I think you cannot create an attribute for FigureHandle. Attributes in wxHaskell are typed on the control to which they apply, for example (taken from the blog) if you have a DiffViewer control, then to create a diffFiles attribute which sets the files to be compared has the type signature below > diffFiles :: Attr (DiffViewer a) (FilePath, FilePath) > diffFiles = newAttr "diffFiles" dvGetFiles dvSetFiles > where > dvGetFiles win = {- Code follows... -} > dvSetFiles win (txt1, txt2) = {- Code follows -} You can just as easily create a new attribute for an existing control. In this case, I think that what you need is much closer to the code you have for GTK than what you have shown for wxHaskell. Hope this helps - I don't have a Gtk2Hs install to let me check they types of your working code, I'm afraid, so there is a bit of guesswork on my side. Best regards Jeremy |