This example demonstrates building a minimal editor with hs-fltk. > import Graphics.UI.FLTK > import System.Exit > import Data.IORef We define a datatype to hold the state of the editor. It remembers the toplevel window, the text input widget, whether the buffer is modifier and the filepath associated with it. > data NS = NS Window Input Bool (Maybe FilePath) Quitting is done with exitWith. > quit _ = exitWith ExitSuccess Make the editor ready for a new file resetting the buffer. > new state _ = do (NS m i _ fp) <- readIORef state > writeIORef state (NS m i False fp) > set i [value := ""] Save a file if it is modified. Asks for the filename if it is not defined. > save state _ = readIORef state >>= s > where s (NS _ _ False _) = return () > s (NS m i st Nothing) = do fp <- fileChooser "Save as" "Text (*.txt)" "" > if null fp then return () else s (NS m i st (Just fp)) > s (NS m i _ (Just fp)) = do get i value >>= writeFile fp > writeIORef state (NS m i False (Just fp)) Open a new file. > open state _ = do fp <- fileChooser "Open" "Text (*.txt)" "" > if null fp > then return () > else do (NS m i _ _) <- readIORef state > set i [value ::= readFile fp] > writeIORef state $ NS m i False $ Just fp Create the main window and the menus. > main = do w <- newWindow 0 0 640 480 [ label := "notepad" ] > m <- newMenuBar 0 0 640 30 [] > i <- newMultiInput 0 30 640 450 [ when := wChanged ] > state <- newIORef (NS w i False Nothing) > set i [action := \_ -> modifyIORef state (\(NS w i _ f) -> NS w i True f)] > addL m [("&File/&New", new state), > ("&File/&Open",open state), > ("&File/&Save",save state), > ("&File/&Quit", \x -> save state x >> quit x), > ("&Help/&About",\_ -> message "notepad version 0.1")] > run w