Graphical User Interfaces in Haskell: Koen Lindström Claessen
Graphical User Interfaces in Haskell: Koen Lindström Claessen
First a Demo
An editor for simple straight line drawings Line currently being drawn by dragging the mouse
Saving a Drawing
GUI Libraries
GUI programming is complex
Much graphics programming needed Many different elements needed (buttons, scroll bars, menus, entry fields)
Portability
Every platform provides a GUI library but they are all different!
The differences are more than just skin deep How do we write portable programs with GUIs?
New portable GUI library Windows GUI lib Windows Mac Linux Windows
Portable interface
wxWindows
Windows
GHC
wxHaskell only works with GHC
Glasgow Haskell compiler
Prog.hs Haskell main program ghc package wx make Prog -o prog prog Compiled code
A compiled program like any other Cannot call individual functions in an interpreter
gui :: IO () gui = do f <- frame [text := "Hello"] inp <- entry f [] out <- entry f [] but <- button f [ text := "Hello , on command := do s <- get inp text set out [text := "Hello "++s] ] set f [ layout := floatCentre $ column 5 [ label "What is your name? , widget inp , widget but , widget out ]]
GUI Concepts
A frame top level window
Panel
Panel
Panel
GUI Hierarchy
Every GUI element (except a frame) has a parent We specify the parent at creation
f <- frame [text := "Hello"] inp <- entry f [] Create two entry fields (with out <- entry f [] parent f) but <- button f [text := "Hello", on command := do s <- get inp text set out [text := "Hello "++s]] Create a button (with parent f)
The first parameter of a creation call is always the parent. Create a frame (no parent)
Attributes
GUI elements have attributes
Rather like the fields of a record, or components of a data structure
Types: Attr w a
An attribute of type a, for a widget of type w
Overloading
The same attribute name can be used with many different widget types
Attribute names are overloaded
f <- frame [text := "Hello"] but <- button f [text := "Hello]
Layout
The layout attribute specifies the appearance on the screen
f <- frame [text := "Hello"] set f [layout := floatCentre $ column 5 [label "What is your name?", widget inp, Place argument in the widget but, centre of the frame widget out]]
widget converts GUI elements to a layout for inclusion in a frame
Instructions
Controls have attributes which are instructions to follow if the user activates them These instructions are followed when
the button is pressed
but <- button f [text := "Hello", on command := do s <- get inp text set out [text := "Hello "++s]] on creates an instruction attribute
Modifying Attributes
Attributes can be read and modified
do s <- get inp text set out [text := "Hello "++s]]
Instructions to read the value of an attribute
Invisible Widgets
Some widgets are invisible They still have attributes that can be set and modified Examples:
Timer widget
Interval On command
Variable widget
Value
Design Principles
Separate the GUI from the functionality as much as possible
So that the GUI can later be replaced without changing the functionality
Examples
Hello.hs
Simple hello example
BouncingBalls.hs
A simple animation containing bouncing balls
Draw.hs
A simple drawing program
Mine.hs
A version of the game minesweeper
haskell.org
Libraries!
haskell.org/libraries/
Hierarchical Libraries
Lots of modules
Googling wxHaskell
Here it is!
Documentation
wxHaskell Documentation
Haddock documentation for wxHaskell General information on wxWidgets sometimes you have to read this
wxHaskell Haddock
Controls: Buttons, entry fields etc
Reading
http://wxhaskell.sourceforge.net/ wxHaskell: A Portable and Concise GUI Library for Haskell, Daan Leijen
Haskell Workshop, Salt Lake City, 2004 http://www.cs.uu.nl/~daan/download/papers/w xhaskell.pdf