import Graphics.UI.Gtk import Graphics.UI.Gtk.Gdk.Events -- import Graphics.Rendering.Cairo import Graphics.UI.Gtk.Gdk.GC import Control.Monad.Trans import Data.IORef main = do initGUI -- create main window win <- windowNew onDestroy win mainQuit -- create state variables linesRef <- newIORef [] widthRef <- newIORef 1 -- create the drawing area drawingArea <- drawingAreaNew let height = 200 let width = 400 onSizeRequest drawingArea $ return (Requisition width height) onButtonPress drawingArea $ \e -> do lines <- readIORef linesRef let lines' = (round (eventX e), round (eventY e)) : lines writeIORef linesRef lines' print lines' widgetQueueDraw drawingArea return True onExpose drawingArea $ \e -> do dw <- widgetGetDrawWindow drawingArea gc <- gcNew dw lines <- readIORef linesRef currentWidth <- readIORef widthRef gcSetValues gc $ newGCValues { lineWidth = currentWidth } drawLines dw gc lines --sequence_ (zipWith (drawLine dw gc) lines (tail lines)) return True entry <- entryNew btn <- buttonNew buttonSetLabel btn "Set Width" onClicked btn $ do txt <- entryGetText entry case reads txt of [(n,"")] -> do writeIORef widthRef n widgetQueueDraw drawingArea _ -> do dlg <- messageDialogNew (Just win) [] MessageError ButtonsOk "This is not an integer" dialogRun dlg widgetDestroy dlg -- describe layout of widgets let spacing = 10 vbox <- vBoxNew False spacing boxPackStart vbox drawingArea PackGrow 0 hbox <- hBoxNew False spacing boxPackStart hbox entry PackGrow 0 boxPackStart hbox btn PackNatural 0 boxPackStart vbox hbox PackNatural 0 containerAdd win vbox widgetShowAll win mainGUI {- GTK is written in C and available in many languages initialization of GTK main loop of GTK for reference use the documentation and the examples how to add widgets in containers Steps ----- 1. initGUI and mainGUI 2. windowNew & widgetShowAll 3. onDestroy win mainQuit 4. drawingAreaNew & containerAdd 5. onButtonPress drawingArea $ \e -> print (eventX e, eventY e) >> return True 6. modeling state -> IORef -}