如何将图库与GTK drawables一起使用?

时间:2012-08-09 14:12:11

标签: haskell gtk haskell-diagrams

我正在尝试学习如何使用diagrams library和Cairo / GTK后端绘制图表。我有一个空白的窗口出现,但我的绘图不会呈现。我做错了什么?

module Main where

import Control.Monad.Trans (liftIO)
import Graphics.UI.Gtk
import Diagrams.Backend.Cairo
import Diagrams.Backend.Cairo.Gtk
import Diagrams.Prelude

main :: IO ()
main = do
  initGUI
  window <- windowNew
  canvas <- drawingAreaNew
  canvas `on` sizeRequest $ return (Requisition 1000 1000)
  set window [ containerBorderWidth := 10,
               containerChild := canvas ]
  canvas `on` exposeEvent $ renderFigure
  onDestroy window mainQuit
  widgetShowAll window
  mainGUI


renderFigure :: EventM EExpose Bool
renderFigure = do
   win <- eventWindow
   liftIO $ renderToGtk win $ toGtkCoords figure
   -- liftIO $ defaultRender win figure
   return True


figure :: Diagram Cairo R2
figure =  unitCircle # scaleX 0.5 # rotateBy (1/6) # scale 50

我尝试使用“defaultRender”(如上所述),但这会产生类型错误:显然“win”不是正确的类型。

我已阅读图表的教程和用户手册,但它实际上并未告诉您如何使用Cairo后端。

更新:我有渲染的椭圆。 “renderFigure”函数现在说:

renderFigure :: DrawingArea -> EventM EExpose Bool
renderFigure canvas = do
   -- win <- eventWindow
   liftIO $ do
      -- diagramArea <- widgetGetDrawWindow canvas 
      -- renderToGtk diagramArea $ toGtkCoords figure
      defaultRender canvas figure
   return True

并且传入的DrawingArea是“canvas on exposeEvent ...”设置中引用的“canvas”。

但我仍然无法使用“renderToGtk”渲染固定大小的图表。

更新2:感谢Joachim Breitner的回答,我现在已经有了这样的最小GTK图表程序,为您的剪切和粘贴乐趣做好准备。

module Main where

import Control.Monad.Trans (liftIO)
import Graphics.UI.Gtk
import Diagrams.Backend.Cairo
import Diagrams.Backend.Cairo.Gtk
import Diagrams.Prelude

main :: IO ()
main = do
  initGUI
  window <- windowNew
  canvas <- drawingAreaNew
  canvas `on` sizeRequest $ return (Requisition 256 256)
  set window [ containerBorderWidth := 10,
               containerChild := canvas ]
  canvas `on` exposeEvent $ renderFigure
  onDestroy window mainQuit
  widgetShowAll window
  mainGUI


renderFigure :: EventM EExpose Bool
renderFigure = do
   win <- eventWindow
   liftIO $ renderToGtk win $ toGtkCoords figure
   return True


figure :: Diagram Cairo R2
figure =  unitCircle # scaleX 0.5 # rotateBy (1/6) # scale 50 # fc red  

2 个答案:

答案 0 :(得分:6)

您的初始代码确实有效。但renderToGtk不会缩放图像,因此它看起来相当小,太小而无法在该线宽处看到。但是试试

figure :: Diagram Cairo R2
figure =  unitCircle # scaleX 0.5 # rotateBy (1/6) # scale 50 # fc red

它会像魅力一样发挥作用!

答案 1 :(得分:2)

您可以注意到,在最新版本的图表中,您应该使用

import Diagrams.Backend.Gtk

而不是

import Diagrams.Backend.Cairo.Gtk

figure :: Diagram Cairo

而不是

figure :: Diagram Cairo R2

结果现在看起来像:

module Main where

import Control.Monad.Trans (liftIO)
import Graphics.UI.Gtk
import Diagrams.Backend.Cairo
import Diagrams.Backend.Gtk
import Diagrams.Prelude

main :: IO ()
main = do
  initGUI
  window <- windowNew
  canvas <- drawingAreaNew
  canvas `on` sizeRequest $ return (Requisition 256 256)
  Graphics.UI.Gtk.set window [ containerBorderWidth := 10,
               containerChild := canvas ]
  canvas `on` exposeEvent $ renderFigure
  onDestroy window mainQuit
  widgetShowAll window
  mainGUI


renderFigure :: EventM EExpose Bool
renderFigure = do
   win <- eventWindow
   liftIO $ renderToGtk win $ toGtkCoords figure
   return True


figure :: Diagram Cairo
figure =  unitCircle # scaleX 0.5 # rotateBy (1/6) # scale 50 # fc red