Abzetdin Adamov's IT Blog

IT is about doing more with less!

Archive for December, 2010

GTK2HS Installation under Windows and Haskell/GUI Programming with GTK2HS.

Posted by Abzetdin Adamov on December 11, 2010

Haskell WinGHCI Interface

Haskell WinGHCI Interface

Recently I tried to make necessary settings to do GUI programming in Haskell under Windows. It turned out, that it’s not so easy as may seem. There are a lot of contradictory tutorials on the Web. Even, official download address of GTK2HS (www.haskell.org/gtk2hs/download) annonced by many tutorials is not existing now. So the official instructions for building gtk2hs on Windows have out of date information and leave out some important issues, so here’s how I got it all working on Windows XP, I believe steps are same for Windows 7. Now step by step we will go though installation process:

  1. Haskell Platform
    Install latest release from http://hackage.haskell.org/platform/ (I installed version 2010.2.0.0 ) 

  2. GTK+
    Download the GTK+ all-in-one bundle 2.16 version (2.22 has some problems) all-in-one bundle from here http://www.gtk.org/download-windows.html
    Extract to folder you would like (for example d:\gtk)
    Add \bin (d:\gtk\bin) to PATH (environmental) variable.
    Run CMD and check command

    > pkg-config –cflags gtk+-2.0
    If you will get some paths instead of failure message, things are going well

  3. MinGW
    You don’t need to install MinGW separately, it already exists within your Haskell Platform. Only you should do – add MinGW \bin to to PATH (environmental) variable (I’ve installed Haskell Platform to D:\Haskell Platform\2010.2.0.0, so my path is D:\Haskell Platform\2010.2.0.0\mingw\bin).

  4. GTK2HS
    Now you should have everything you need to install gtk2hs from Hackage:

    cabal update
    cabal install gtk2hs-buildtools
    cabal install gtk

Depend on you Internet download speed, computer perfomance and releases of packages we talked above, each step will take some time. Whole process may vary from 20 to 40 minutes.

Congratulation! Now you can make sure that GTK2HS wors properly by running the following advanced “Hello World” code:

import Graphics.UI.Gtk

main :: IO ()
main =
    do initGUI
       win <- windowNew
       windowSetTitle win "Adamov's Example"
       win `onDestroy` mainQuit

       ent <- entryNew
       btn <- buttonNew

       col <- vBoxNew False 5
       containerAdd col ent
       containerAdd col btn

       buttonSetLabel btn "Click to Print"
       entrySetText ent "Hello World"

       btn `onClicked` do s <- entryGetText ent
                          print s

       containerAdd win col
       widgetShowAll win
       mainGUI
Haskell/GUI Programming with GTK2HS. Advanced "Hello World"

Haskell/GUI Programming with GTK2HS. Advanced "Hello World"

Advertisements

Posted in Programming and Development | Tagged: | 2 Comments »