Revision as of 09:44, 12 June 2010

Contents

1 A quick start with wxHaskell

This document is written to get you started quickly with writing wxHaskell applications. Further documentation can be found on the documentation page.

Note from the author: I have written this page to be in close correspondence with the yahu getting started page – first of all to make my job easier by reusing Koen Claessen's excellent example, but also since it makes an interesting comparison: we reuse many concepts of yahu, most notably properties and attributes, but as yahu is based on Tcl/TK, the applications are also typed and structured in a fundamentally different way.

Daan Leijen

2 Hello world in wxHaskell

Start your favorite editor and write the following program (that will show a frame with a single button that closes the frame when pressed).

You can read the MacOS X notes for more information on using wxHaskell on MacOS X, and how to use it from an interpreter prompt.

2.1 Types

A typical wxHaskell program imports the Graphics.UI.WX library. If you need to access specific wxWidgets functionality, you would also import the lower level Graphics.UI.WXCore library. The main function uses start to start our GUI. The function start initializes the GUI framework with the provided argument and starts the window event loop until the application quits or when all top-level windows are closed. The GUI itself is described with the following functions:

Actually, some of these functions have (even) more general types – you can use the :t command in GHCi to see them.

The types Frame () and Button () denote graphical objects. These objects can have properties. When an object is created we can supply an initial list of properties but we can also set them later using set. The type of properties for frames are Prop (Frame ()) and for buttons Prop (Button ()).

Properties are created by combining attributes with values. Examples of attributes are text and layout. An attribute of type Attr w a applies to objects of type w and values of type a. Values can be assigned to attributes using the (:=) operator. You can find out more about attributes in the haddock documentation for the modules WX.Attributes and WX.Classes.

Somewhat special attributes are events. An event of type Event w a can be transformed into an attribute Attr w a using on. The value of an event attribute is normally an IO action that is executed when the event happens. Find out more about events in the haddock documentation for WX.Events and the lower level WXCore.Events

Since wxHaskell is based on an object-oriented framework, we also encode inheritance. The extra type parameter of objects encodes the inheritance relationship. When the parameter of an object is unit (), it denotes an object of that exact class. When the parameter is a type variable a, it denotes any object that is instance of that class. For example, both the frame and button functions return precisely a frame or button and use a () type parameter. However, the text attribute applies to any kind of window, including frames and buttons, and has a Window a as its argument. We can now use the text attribute for example for both frames and buttons. In wxHaskell, this works since a Frame () is actually a type synonym for Window (CFrame ()) and can thus be passed where a Window a is expected. The same hold for a Button () that is a synonym for Control (CButton ()) that is again a synonym for Window (CControl (CButton ())).

2.2 Layout

The layout of a window is specified through the layout attribute. The layout of the current program is rather terse and we will spice it up by letting the button float in the center when the window is resized. This is also a good opportunity to add a small margin around the button.

set f [layout := margin 10 (floatCentre (widget quit))]

We can also add a text label above the button that is also centered. The argument of column specifies the amount of space between the items.

Unlike more functional GUI libraries, wxHaskell does not provide a model for state management and uses simple mutable variables to communicate state across different event handlers. (Note: this is a concious design decision – as functional GUI interfaces are still very much a research area, we want to provide a full fledged GUI library using simple IO first, than try to build good functional interfaces on top of that). The state of the bouncing balls demo is a list of balls. Each ball is represented as a list of all its future heights. At the start of the program the list is empty (varCreate []).

Next, we use fixedFrame to create a non-resizeable window frame. A panel is created to paint the balls on and its paint handler paints the current balls in the panel. (Note: a panel has nothing to do with a Java panel: it is a widget that is normally used to place controls in as it manages control navigation keys like tab).

To animate the balls, we install a timer that advances all the balls on each timer tick and causes the panel to repaint the balls. We also install event handlers that react on the user: a mouse click causes a new ball to drop, a right click opens another frame (!), a p-key pauses the balls, and +/- increase/decrease the speed of the balls. Note how the operator (:~) applies a function to an attribute value instead of assigning one. Thus, the expression (set t [enabled :~ not]) flips the enabled state of the timer.

Finally, we specify the layout of the frame, using minsize to specifiy the minimal size of the panel and thus the size of the frame as it is not resizeable.

A paint event handler gets two arguments: a device context (DC) to draw on and a rectangle that specifies the coordinates of the viewing area. We have supplied the first argument ourselves when setting the event handler, namely the mutable variable that holds the list of all balls.

As said, a single ball is represented as a list of all its future positions. When painting the current balls, we simple extract the head positions of all balls and draw them using drawBall. Drawing combinators like circle draw using the current pen, brush, and font of the device context. By default, a brush is transparent so we set it to a solid red brush through writing services before drawing the circles. Note that this is an optimization, we could have achieved the same effect by setting it for each circle individually: circle dc pt radius [brushKind := BrushSolid, brushColor := red]. You can read more about drawing in the documentation of the WX.Draw module.

3.2 Bouncing

The timer event handler uses nextBalls to advance all the balls to their next postion.

We prepend a new list of ball positions to the existing resume list using the varUpdate function and we repaint the panel. The new list of positions is calculated with the bouncing function that takes the position of the mouse pointer as its argument. This function uses the bounce function to calculate all future heights given an initial height and speed. Each time the ball touches the ground, it loses 2 units of speed.

Hopefully this sample inspires you to write more interesting GUI's. Don't forget to look at the samples provided with the wxHaskell documentation.