2011-06-28 68 views
5

Gtk2hs具有各種實現Widget類的Widget數據類型。是否有可能編寫自定義數據類型,這是相同的?gtkhs中Widget類的自定義實現

說我想要顯示和運行這樣的Lua代碼的小部件。

data LuaWidget = LuaWidget { text :: TextView, package :: HBox } deriving Eq 
instance Widget LuaWidget where 
    .... 

Haskell級別可能嗎?

回答

3

在gtk中用Haskell創建新的窗口小部件「類」是不可能的。

你可以做的是給現有的控件類型提供自定義屬性。例如,在封裝plot-gtk自定義數據域(System.Glib.GObject)加入到一個drawingArea插件:

import System.Glib.GObject 
import Graphics.UI.Gtk 

-- | create a new 'Figure' plot 
plotNew :: FigureHandle -> IO DrawingArea 
plotNew f = do 
    canvas <- drawingAreaNew 

    set canvas [maybeFigure := (Just f)] 

    _ <- on canvas exposeEvent $ tryEvent $ liftIO $ do 
      s <- widgetGetSize canvas 
      drw <- widgetGetDrawWindow canvas 
      fig <- get canvas figure 
      renderWithDrawable drw (renderFigureState fig s) 

    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 #-}