This example explains how the widget size is queried to create drawings that fit into the widget.
Archive file with this example: Cairo-Example002.tar.gz. Be sure to read the file README.txt for further instructions.
The widget size is read in function drawEventHandler. This function uses the liftIO facility which is provided by the module Control.Monad.IO.Class.
import "gtk3" Graphics.UI.Gtk import Graphics.Rendering.Cairo import Control.Monad.IO.Class -- for liftIO -- This function draws a diagonal line from the top left corner to -- to the bottom right corner of the drawing area. -- The function arguments are: -- w width of the drawing area -- h height of the drawing area -- strokewidth the width of the stroke to be drawn drawMethod :: Double -> Double -> Double -> Render () drawMethod w h strokeWidth = do setSourceRGB 0.8 0.1 0.1 setLineWidth strokeWidth setLineCap LineCapRound setAntialias AntialiasNone moveTo 20 20 lineTo (w - 20) (h - 20) strokePreserve setSourceRGB 1.0 1.0 1.0 setLineWidth 1.0 stroke -- This event handler for the draw signal queries the size of -- the drawing area and calls the paint function "drawMethod" drawEventHandler :: DrawingArea -> Render () drawEventHandler drawingArea = do w <- liftIO $ widgetGetAllocatedWidth drawingArea h <- liftIO $ widgetGetAllocatedHeight drawingArea drawMethod (fromIntegral w) (fromIntegral h) 11.0 -- action to be performed after the window was closed destroyEventHandler :: IO () destroyEventHandler = do mainQuit main :: IO () main = do initGUI -- all needed widgets are created and configured window <- windowNew paintArea <- drawingAreaNew -- the properties of the main window are set set window [windowDefaultWidth := 250, windowDefaultHeight := 250, windowWindowPosition := WinPosCenter, windowTitle := "DrawingArea Demo", containerChild := paintArea] widgetModifyBg paintArea StateNormal (Color 65535 65535 65535) -- all needed callback functions are provided on paintArea draw (drawEventHandler paintArea) on window objectDestroy destroyEventHandler -- all widgets are made visible widgetShow paintArea widgetShow window -- the event processing is started mainGUI