Personal tools

Gtk2Hs/Tutorials/Intro

From HaskellWiki

< Gtk2Hs | Tutorials
Revision as of 20:06, 1 December 2010 by Treblacy (Talk | contribs)

Jump to: navigation, search

Note: this page is under construction. Feel free to help out!

Contents

1 Introduction

As with any tutorial, certain assumptions are inherent in the code and instructions presented below. In this case the primary assumptions are:

  • You are reasonably familiar with Haskell and in particular, writing code using the IO Monad.
  • You have successfully installed Gtk2Hs.
  • You believe using Glade to design your user interface is a good idea.

Bon chance, mes amis!

2 "Hello World" with Gtk2Hs

Start with the most basic working hello world app. Then we compile it...

Here's the most basic Gtk2Hs prog:

import Graphics.UI.Gtk

main = do
  initGUI
  window <- windowNew
  widgetShow window
  mainGUI

2.1 Compiling "Hello World"

How to compile it with GHC on linux/mac/win32

> ghc --make Hello.hs -o hello

2.2 Stepping through "Hello World"

initGUI must be called once before calling any other Gtk2Hs functions. Therefore it is usually called near the beginning of main.

window <- windowNew creates a top-level window and uses the identifier window to refer to it and pass it around later. Top-level windows are usually used as application windows, for example browser windows, terminal windows, and editor windows are top-level windows. Creation brings the window into internal existence but still not full existence: for one, the window is not shown, and most internal allocations required for showing are not done yet. Full existence will happen at the next line…

(Meanwhile, between the above and below is a customary place to change settings of the window and add stuff to it, although you could also do them later.)

widgetShow window completes the necessary internal allocations and shows the window. (
widgetShow
is very general and can show other things too — Widgets, which are covered in a later section.)

mainGUI is a loop, and at this point it looks like an infinite loop to you — you find that this program seems to hang. The next section will present a normal way to exit the loop. Meanwhile, what is the significance of this loop?

The loop is required to process input, for example mouse movements and clicks, key presses and releases, windows getting moved or resized, losing focus, gaining focus, requests for redraws. The loop receives all this input and takes corresponding default actions; it can also call functions you write (in addition or instead). More on this in the next section.

3 Signals

The concept of event driven GUI programming. What GObject signals are (without necessarily mentioning the term GObject yet)

4 Widget types

The way that the class heiriaharcy is represented in Haskell using type classes. Breif mention of up casting & downcasting. We'll come back to downcasting in the glade section.

5 Attributes

Show the "set object [ attr := value ]" style syntax. Reccomend it over the more verbose "objectSetAttr object value" style.

6 Using Glade

We want to encourage people to use Glade to do their widget layout rather than doing it all in code. We can explain manual widget layout as a more advanced topic next.

Redo the hello world program using Glade.

7 Widget layout

Doing widget layout in code rather than with Glade.

8 Basic concepts

Brief explanation of basic and general concepts. With references to the API functions whenever possible. (probably in form of examples?).

 - signal
 - event
 - window
 - widget
 - container
 - box
 - layout
 - button
 - label

The idea is to get the user with as general and useful concepts as possible so it makes it easier for him to surf the API since the beginning and write simple code.