gtkhs 中 Widget 类的自定义实现

发布于 2024-11-17 08:56:16 字数 269 浏览 5 评论 0原文

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

假设我想要一个小部件来显示和运行这样的 Lua 代码。

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

在 Haskell 水平上可能吗?

Gtk2hs has various widget datatypes that implements Widget class. Is it possible to write custom datatype which does the same?

Say I want to have widget for displaying and running Lua code like this.

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

Is it possible on Haskell level?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎言笑 2024-11-24 08:56:16

不可能在 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 #-}

It is not possible to create new widget 'classes' with Haskell in gtk.

What you can do is give custom attributes to an existing widget type. For example, in the package plot-gtk a custom data field (System.Glib.GObject) is added to a drawingArea widget:

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 #-}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文