From HaskellWiki
Revision as of 04:19, 14 February 2006 by BrettGiles (talk | contribs) (Importing from old hawiki)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

What is it?

WxHaskell is a Haskell binding to WxWidgets (formerly known as WxWindows), which allows you to create graphical user interfaces using native widgets on Windows, Mac and *nix (gtk).




  1. Getting Started:
    • here are some InstallationTips
    • read carefully http://wxhaskell.sourceforge.net/quickstart.html - it contains more information than you think
    • we include below some orientation for the most essential wxhaskell concepts
    • see samples/wx/Minimal.hs
  2. Text:
    • retrieving the text of a TextCtrl: mytext <- get mytextEntry text
    • staticText misbehaves on resize: make sure that you are giving the widget enough space... for example, if you want a one-line staticText, you should
      • use hfill on the staticText
      • not use hfill (or hfloat...) or any other widgets in the same row
  3. wxHaskellLayout:
    • Don't bother looking at the the examples yet, the API is better for this
    • See the Haddock-generated documentation for Graphics.UI.WX.Layout
  4. Scroll Bars:
    • scrollbars are just windows. You create a scrollFrame, and any widgets you want inside the scrollbars, you make this scrollFrame their parent
    • see samples/wx/ImageViewer.hs
    • the following widgets (controls) already include scrollbars : listBox
  5. Mac OS X (Darwin)
    • for ghci usage download EnableGui.hs from wxhaskell.sourceforge.net/download/EnableGUI.hs and
      ghc -fffi -package wx EnableGUI.hs
      ghci -package wx HelloTest.hs EnableGui.hs
      -- followed by :m +EnableGUI and
      enableGUI >> main

Managing multiple windows

Scenario: you have a main window MAIN with some information. You want to create a secondary window PARAMS in which the user edits some configuration stuff. When the user closes PARAMS, the window MAIN should be updated to reflect the new settings.

You have some code which looks like

set paramsButton [ on command := do createParamsWindow
                                    updateMainWindow ]

Problem: MAIN is not updated... at least, not until you call PARAMS a second time.


* This is NOT a problem with Haskell laziness
* This is more an issue with running concurrent bits of code

Explanation: updateMainWindow tries to read the new configuration value, but it does not succeed, because no configuration values have changed. Why not? Simpler than it looks: all the createParamsWindow function does is creates a window with some widgets, assign some commands to said widgets, and return. There's nothing in the createParamsWindow code that says the function should only return when PARAMS has been closed.

Solution: Pass createParamsWindow a function which updates the MAIN window:

set paramsButton [ on command := do createParamsWindow updateMainWindow ]

When the user closes PARAMS, one of the final things it should do is call this update function

A Short Guide

It is helpful to a get a grip on three basic concepts: Widgets, Layout and Events.


Widgets are the basic components of any GUIs. They include buttons, radio boxes, frames and what not. There are essentially two kinds of widgets: windows and controls. Windows are widgets that contain other widgets. Note: what you might normally think of as a window, i.e. that big thing with the close, minimise, maximise buttons on your OS in wxhaskell is a special kind of window called a "frame".

We define windows by calling some function (for example, frame) with a list of attributes. Here is a example which creates a frame with the title bar "Hello!":

  = do f    <- frame    [text := "Hello!"]

The more interesting widgets are controls such a buttons and check boxes. We define controls almost the same way as windows, by calling some function (for example, button) with a window and a list of attributes:

  = do f    <- frame    [text := "Hello!"]
       quit <- button f [text := "Quit"]

The relevant documentation:

 * http://wxhaskell.sourceforge.net/doc/Graphics.UI.WX.Controls.html
 * http://wxhaskell.sourceforge.net/doc/Graphics.UI.WX.Window.html


Layout means telling wxhaskell how to arrange your widgets with respect to each other. It is something you would normally do after you have defined all your widgets.

We won't go into much detail here because the Haddock-generated documentation is very helpful here, but here is a trivial example of layout where all we do is stick the quit button in the frame. Notice how layout is just an attribute of the frame widget.

  = do f    <- frame    [text := "Hello!"]
       quit <- button f [text := "Quit"]
       set f [layout := widget quit]
 * http://wxhaskell.sourceforge.net/doc/Graphics.UI.WX.Layout.html

Scrolled windows

Widgets (for example, listboxes) will automatically create scrolled windows for you. You only need to deal with them if you are working with bitmaps.


How do you use XRC-Files with wxHaskell? Is it possible?

- I also very much would like to know this. Anyone?


Events are what joins your widgets to the rest of your code. Whenever a user manipulates a widget (e.g. presses a button), an event is triggered. If you set the appropriate attributes, you can cause these events to call some piece of Haskell code. In the following example, we set the "on command" attribute to close the frame f.

  = do f    <- frame    [text := "Hello!"]
       quit <- button f [text := "Quit", on command := close f]
       set f [layout := widget quit]
* http://wxhaskell.sourceforge.net/doc/Graphics.UI.WX.Events.html

Idle Event

Use wxHaskell Idle Event for automation.

See also

* Autoforms
* WxFruit