1 {-# LANGUAGE RecordWildCards, ExistentialQuantification #-}
6 Color, Rect(..), Point(..),
11 import qualified Graphics.UI.Gtk as Gtk
12 import Graphics.UI.Gtk (AttrOp(..)) -- For the := constructor
13 import qualified Graphics.Rendering.Cairo as Cairo
14 import qualified Graphics.UI.Gtk.Gdk.EventM as EventM
16 import Control.Monad.Trans -- for liftIO
18 import Control.Applicative
23 -- | A rectangle in two dimensional space
31 data Point = Point !Int !Int deriving (Show, Eq)
33 type Color = Gtk.Color
35 -- Create a Color from Red, Green and Blue values. The inputs should be
36 -- between 0 and 255 (inclusive).
37 rgb :: Int -> Int -> Int -> Color
38 rgb r g b = Gtk.Color (conv r) (conv g) (conv b)
39 where conv = fromInteger . toInteger . (*256)
41 -- | Some predefined colours
45 white = rgb 0xff 0xff 0xff
47 -- | Helper functions for creating a Point
48 point, pt :: Int -> Int -> Point
53 = GfxLines Color [Point] -- coloured line through a list of points
54 | GfxPolygon Color [Point] -- filled polygon of given colour
55 | GfxPicture FilePath Point -- shows a picture
56 | GfxText Color Point String -- coloured string on position Point
57 | GfxRectangle Color Rect -- filled rectangle of given colour
58 | GfxEllipse Color Rect -- ellipse within given rectangle
59 | GfxDisc Color Rect -- filled ellipse within given rectangle
60 | GfxClear -- clears the graphical window
62 | GfxFont String Int -- changes to fontname of given size
63 | WinPrompt String String String -- pops up a window with an edit field
64 -- | WinFilePrompt Bool
65 | WinMenu [(String,[String])] -- adds a menu list to the graphical window
66 | WinTitle String -- gives a title to the graphical window
67 | FRead String -- read file with a given name
68 | FWrite String String -- writes a text file with a given filename
69 | ReqQuit -- quits the graphical system
73 = KeyIn Char -- touched key with given character
74 | MouseDoubleClick Point -- mouse event on position Point
75 | MouseDragged Point -- ibid
76 | MouseDown Point -- ibid
77 | MouseUp Point -- ibid
78 | MenuItem String String -- selected item from WinMenu with a given name
79 | PromptResponse String String String -- response to WinPrompt request
80 | FileContents String String -- response to FRead request
83 type TinaStep s = s -> Response -> (s,[Request])
84 data TinaProgram = forall s. Main
86 , initialRequests :: [Request]
87 , eventHandler :: TinaStep s
94 , initialRequests = [GfxText red (pt 0 0) "foo", GfxText blue (pt 100 100) "bar"]
95 , eventHandler = \s e -> (s+1,[GfxText green (pt 50 50) $ show (s,e)])
100 data IState = forall s. IS
101 { {-sFrame :: Frame ()
103 , buffer :: MemoryDC ()
104 , -}postponed :: IORef [Request]
105 , usrState :: IORef s
106 , usrProg :: TinaStep s
109 processPostponed :: IState -> IO ()
110 processPostponed s@IS {..} = do
111 ps <- readIORef postponed
112 unless (null ps) $ do
113 writeIORef postponed (tail ps)
114 rs <- handle s (head ps)
115 mapM (stepUserProgram s) rs
118 post s r = stepUserProgram s r >> processPostponed s
120 stepUserProgram :: IState -> Response -> IO ()
121 stepUserProgram IS {..} r = do
122 state <- readIORef usrState
123 let (state',reqs) = usrProg state r
124 writeIORef usrState state'
125 readIORef postponed >>= writeIORef postponed . (++ reqs)
127 handle :: IState -> Request -> IO [Response]
128 handle s@IS {..} r = do
129 resps <- maybe (fail $ "No handler for request " ++ show r) id $
130 fmap (>> return []) (gfxHandler s r <|> winHandler s r <|> miscHandler s r)
133 runTina :: TinaProgram -> IO ()
134 runTina Main {..} = do
135 usrState <- newIORef initialState
136 postponed <- newIORef (GfxText (rgb 0 0 0) (pt 50 50) "foo" : GfxClear :initialRequests)
137 let state = IS { usrProg = eventHandler, .. }
138 runGUI {-(sz windowWidth windowHeight) -}state
140 runGUI :: {-Size ->-} IState -> IO ()
145 -- Create a window, which will make the mainloop terminated when
147 window <- Gtk.windowNew
148 Gtk.set window [ Gtk.containerBorderWidth := 10
149 , Gtk.windowTitle := "FP Practicum" ]
150 Gtk.onDestroy window Gtk.mainQuit
152 Gtk.on window Gtk.exposeEvent $ do
153 --(w,h) <- Gtk.eventWindowSize
154 dw <- EventM.eventWindow
156 Gtk.renderWithDrawable dw $ do
157 --translate (w/2) (h/2)
158 --scale (w/drawSide) (h/drawSide)
159 Cairo.arc 100 100 50 0 (2*pi)
163 -- Show the window and start the Gtk mainloop.
164 Gtk.widgetShowAll window
169 runGUI s IS {..} = do
171 [ text := "FP Practicum"
174 buffer <- memoryDCCreate
175 bitmapCreateEmpty s 24 >>= memoryDCSelectObject buffer
176 withBrushStyle (BrushStyle BrushSolid white) (dcSetBackground buffer)
179 [ fontFace := "Courier New"
181 , brushColor := rgb 0 0 0
182 , brushKind := BrushSolid
183 , penColor := rgb 0 0 0
184 , penKind := PenSolid
186 sPanel <- panel sFrame [ size := s ]
189 [ on paint := onPaint state
190 , on doubleClick := post state . MouseDoubleClick
191 , on click := post state . MouseDown
192 , on drag := post state . MouseDragged
193 , on unclick := post state . MouseUp
194 , on anyKey := transKey (post state . KeyIn)
197 [ on closing := sFrame `set` [ visible := False ] >> wxcAppExit
198 , on anyKey := transKey (post state . KeyIn)
199 , layout := widget sPanel
201 windowSetFocus sFrame
202 processPostponed state
203 onPaint :: IState -> DC a -> Rect -> IO ()
204 onPaint IS {..} dest va = do
205 dcBlit dest va buffer (Point 0 0) wxCOPY False >> return ()
207 transKey :: (Char -> IO ()) -> Key -> IO ()
208 transKey prod (KeyChar c) = prod c
209 transKey prod KeySpace = prod ' '
210 transKey prod KeyEscape = prod '\ESC'
211 transKey prod KeyReturn = prod '\n'
212 transKey prod KeyTab = prod '\t'
213 transKey _ _ = return ()
218 miscHandler s@IS {..} (FRead fn ) = Just $ readFile fn >>= post s . FileContents fn
219 miscHandler IS {..} (FWrite fn cnts) = Just $ writeFile fn cnts
220 miscHandler IS {..} ReqQuit = Just $ putStrLn "Quiting" >> wxcAppExit
222 miscHandler IS {..} _ = Nothing
225 winHandler s@IS {..} (WinPrompt st1 st2 st3) = Just $ textDialog sFrame st1 st2 st3 >>= post s . PromptResponse st1 st2
226 winHandler IS {..} (WinTitle st) = Just $ sFrame `set` [text := st]
227 winHandler s@IS {..} (WinMenu ms) = Just $ mkMenu >>= \ms' -> sFrame `set` [menuBar := ms']
231 p <- menuPane [ text := name ]
234 i <- menuItem p [ text := item ]
235 sFrame `set` [on (menu i) := post s (MenuItem name item)]
238 | (name,items) <- ms ]
240 winHandler _ _ = Nothing
243 gfxHandler IS {..} (GfxLines col ps) = Just $ polyline buffer ps [penColor := col] >> dirtyPts sPanel ps
244 gfxHandler IS {..} (GfxPolygon col ps) = Just $ polygon buffer ps [penColor := col, brushColor := col] >> dirtyPts sPanel ps
245 gfxHandler IS {..} (GfxText col xy st) = Just $ drawText buffer st xy [textColor := col] >> getTextExtent buffer st >>= dirtyRect' sPanel xy
246 gfxHandler IS {..} (GfxRectangle col rt) = Just $ drawRect buffer rt [penColor := col, brushColor := col] >> dirtyRect sPanel rt
247 gfxHandler IS {..} (GfxEllipse col rt) = Just $ ellipse buffer rt [penColor := col, brushKind := BrushTransparent] >> dirtyRect sPanel rt
248 gfxHandler IS {..} (GfxDisc col rt) = Just $ ellipse buffer rt [penColor := col, brushColor := col] >> dirtyRect sPanel rt
249 gfxHandler IS {..} (GfxFont st sz) = Just $ buffer `set` [ fontSize := sz, fontFace := st ]
250 gfxHandler IS {..} GfxClear = Just $ dcClear buffer >> windowRefresh sPanel False
251 gfxHandler IS {..} (GfxPicture fd pt) = Just $ bitmapCreateFromFile fd >>= \bm -> drawBitmap buffer bm pt False [] >> bitmapGetSize bm >>= dirtyRect' sPanel pt
253 gfxHandler _ _ = Nothing
256 dirtyPts :: Window a -> [Point] -> IO ()
257 dirtyPts dc ps = dirtyRect' dc (pt x y) Size {..}
263 sizeW = maximum xs - x
264 sizeH = maximum ys - y
266 dirtyRect' :: Window a -> Point -> Size -> IO ()
267 dirtyRect' dc Point {..} Size {..} = dirtyRect dc $ Rect pointX pointY sizeW sizeH
269 dirtyRect :: Window a -> Rect -> IO ()
270 dirtyRect dc rect = windowRefreshRect dc False (grow 2 rect)
272 rectanglify :: Point -> Size -> Rect
273 rectanglify Point {..} Size {..} = Rect pointX pointY sizeW sizeH
275 grow :: Int -> Rect -> Rect
276 grow n Rect {..} = Rect
277 { rectLeft = rectLeft - n
278 , rectTop = rectTop - n
279 , rectWidth = rectWidth + 2 * n
280 , rectHeight = rectHeight + 2 * n }