gtkhs中Widget类的自定义实现

时间:2011-06-28 19:15:29

标签: haskell gtk gtk2hs

Gtk2hs有各种实现Widget类的widget数据类型。是否可以编写自定义数据类型?

假设我想要显示和运行Lua代码的小部件。

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

是否可以在Haskell级别上使用?

1 个答案:

答案 0 :(得分:4)

无法在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 #-}