Make testProg produce readable (non-overlapping) output.
[matthijs/projects/fpprac.git] / FPPrac.hs
index 7e5a6914225519e0d3056b50344467794f405091..306f048001dca25e62b2864c98e2b01fda06af78 100644 (file)
--- a/FPPrac.hs
+++ b/FPPrac.hs
@@ -93,7 +93,7 @@ data TinaProgram = forall s. Main
 testProg = Main
        { initialState    = 1
        , initialRequests = [GfxText red (pt 0 0) "foo", GfxText blue (pt 100 100) "bar"]
-       , eventHandler    = \s e -> (s+1,[GfxText green (pt 50 50) $ show (s,e)])
+       , eventHandler    = \s e -> (s+1,[GfxText green (pt 50 (50 + 10 * s)) $ show (s,e)])
        , windowWidth     = 200
        , windowHeight    = 200
        }
@@ -162,22 +162,32 @@ runGUI w h (IS { .. }) = do
        buffer' <- Cairo.createImageSurface Cairo.FormatARGB32 w h
        buffer <- newIORef buffer'
 
+       -- Repack state
+       let state = IS { .. }
+       
        -- Register events
        Gtk.on window Gtk.exposeEvent $ onExpose buffer
        Gtk.on window Gtk.configureEvent $ onResize buffer
+       Gtk.on window Gtk.keyPressEvent $ onKeyDown state
 
-       -- Repack state
-       let state = IS { .. }
-       
        -- Process any initial requests
        processPostponed state
 
        -- Show the window and start the Gtk mainloop.
        Gtk.widgetShowAll window
        Gtk.mainGUI
-       
 
--- | Copy the given surface to the exposed window on an expose event.
+-- | Called when a key is pressed.
+onKeyDown :: IState -> EventM.EventM EventM.EKey Bool
+onKeyDown s = do
+       keyval <- EventM.eventKeyVal
+       case Gtk.keyToChar keyval of
+               Just c -> liftIO $ post s (KeyIn c)
+               Nothing -> return ()
+       return True -- No clue what this means
+
+-- | Called when (part of) the window should be redrawn. Copy the given surface
+--   to the exposed window on an expose event.
 onExpose :: IORef Cairo.Surface -> EventM.EventM EventM.EExpose Bool
 onExpose buffer = do
        current_buffer <- liftIO $ readIORef buffer