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