Documentation

API Reference
Tutorials
Examples
Help and Frequently Asked Questions
Hello World in Haskell with Gtk2Hs

Note: the new cabal packages as of yet do not contain any demos. You can download our demo programs from the repository.

API Reference

Online
glib package, version 0.12.3 (current)
gio package, version 0.12.3 (current)
cairo package, version 0.12.3 (current)
pango package, version 0.12.3 (current)
gtk package, version 0.12.3 (current)
version 0.11.0
version 0.9.13
version 0.9.12
Downloadable
glib package, version 0.11.0
gio package, version 0.11.0
cairo package, version 0.11.0
pango package, version 0.11.0
gtk package, version 0.11.0
version 0.10.0
version 0.9.13
version 0.9.12

The API reference can also be built from the source distribution.

You can now search the online API documentation using Hoogle by adding “+gtk” to your query (or “+cairo”, etc.). Hoogle allows you to search by name and by type.

When our documentation is not enough you can refer to the documentation for the underlying C library.

Tutorials

Glade Tutorial
step-by-step introduction to using the Glade interface designer with Gtk2Hs to quickly build working and visually-attractive GUIs

We are interested in expanding the range of tutorials. We would very much like to encourage people to help with this. See the development page for details of how to get the sources and send in changes (either via darcs or just via email).

Examples

There is an introductory article by Kenneth Hoste (and corresponding sources) that appeared in the first issue of The Monad.Reader. The article demonstrates various Gtk2Hs features (including the use of Glade) through the development of a version of the “memory” card game.

There are also several small demo programs to look over in the demo/ directory in the source distribution.

Help and Frequently Asked Questions

There is a small collection of FAQs.

There is a mailing list for general discussion: gtk2hs-users@lists.sourceforge.net.

Note that you have to join the mailing list before you can post.

For anything not covered in the FAQs, feel free to ask on the mailing list, though you may like to search or browse the mailing list archives to see if the issue has come up before.

If you use IRC, you may be able to find someone to ask on #haskell on irc.freenode.net.

Hello World in Haskell with Gtk2Hs

To get a feel of how a simple Gtk program written in Haskell looks like, consider the “Hello World” example from the demo/ directory, that comes with Gtk2Hs:

import Graphics.UI.Gtk
main :: IO ()
main = do
initGUI
window < - windowNew
button < - buttonNew
set window [ containerBorderWidth := 10,
containerChild := button ]
set button [ buttonLabel := "Hello World" ]
onClicked button (putStrLn “Hello World”)
onDestroy window mainQuit
widgetShowAll window
mainGUI

Build this using:

ghc --make Hello.hs -o hello

All operations on the GUI are executed in the I/O monad which is not very functional, but easy to understand. It should be possible to guess from the function names what this code does. There are also some special bits:

  • The UI elements of Gtk+ (so-called widgets) form a big tree of objects that inherit from each other. In this example, window inherits from a class called container wich in turn inherits from a class called widget. The function onDestroy takes a widget as argument, in this case we use the more derived window.
  • Widgets have attributes that can be set with a list notation. In the example we set the containerBorderWidth attribute of the object window. Note that containerBorderWidth is an attribute of the container class from which window is derived. It is possible to mix different attributes, e.g. one could set the attribute windowTitle to, say, “Hello World”.
  • The onClicked and onDestroy functions are used to attach IO actions to so-called signals. A signal is raised when the user undertakes a given action. As shown for onClicked, it is possible to attach several actions to the same signal. To commuincate with such an IO action, MVars or IORefs can be used.
  • Gtk+ determines the layout of UI elements by nesting elements into each other. The size of a window, for example, is determined by the size of the widget that is added to it. In our example, it is determined by the size of the button, which, in turn, is determined by the size of the label “Hello World”.