Debian Codegraph @

demo/graphic/Drawing.hs

raw
1 -- Example of an drawing graphics onto a canvas. Note that this example
2 -- uses the old-style Gdk drawing functions. New implementations should
3 -- use Cairo. See examples in that directory.
4 import Graphics.UI.Gtk
5 import Graphics.UI.Gtk.Gdk.EventM
6 import Graphics.UI.Gtk.Gdk.GC
7 import Control.Monad.Trans ( liftIO )
8
9 main = do
10 initGUI
11 dia <- dialogNew
12 dialogAddButton dia stockOk ResponseOk
13 contain <- dialogGetUpper dia
14 canvas <- drawingAreaNew
15 canvas `on` sizeRequest $ return (Requisition 40 40)
16 text <- canvas `widgetCreateLayout` "Hello World."
17 canvas `on` exposeEvent $ updateCanvas text
18 boxPackStartDefaults contain canvas
19 widgetShow canvas
20 dialogRun dia
21 return ()
22
23 updateCanvas :: PangoLayout -> EventM EExpose Bool
24 updateCanvas text = do
25 win <- eventWindow
26 liftIO $ do
27 (width,height) <- drawableGetSize win
28 gc <- gcNew win
29 gcSetValues gc $ newGCValues {
30 foreground = Color 65535 0 0,
31 capStyle = CapRound,
32 lineWidth = 20,
33 joinStyle = JoinRound
34 }
35 drawLines win gc [(30,30),(width-30,height-30),(width-30,30),(30,height-30)]
36 gcSetValues gc $ newGCValues {
37 foreground = Color 65535 65535 0,
38 lineWidth = 4
39 }
40 drawArc win gc False 0 0 width height (135*64) (90*64)
41
42 drawLayoutWithColors win gc 30 (height `div` 2) text
43 (Just (Color 0 0 0)) Nothing
44
45 return True