zu www.bildungsgueter.de next page Table of Section Contents previous page

Correct Program Termination


Archive file with this example: Example002.tar.gz. Be sure to read the file README.txt for further instructions.

The second example adds the code that is needed to terminate the program after the window was closed. It turns out that one additional event handler is needed.

import "gtk3" Graphics.UI.Gtk

--  This function is to be called when the window is destroyed.
--  The function has the responsibility to terminate the program
--  in an orderly way. It does so in exiting the event loop.
destroyEventHandler :: IO ()
destroyEventHandler =
  do mainQuit


-- main program: create and display the window
main :: IO ()
main = do
    initGUI
    window <- windowNew
    set window [windowDefaultWidth := 250,
                windowDefaultHeight := 280]
    on window objectDestroy destroyEventHandler
    widgetShow window
    mainGUI

To terminate the program after its window was closed, you have to do two things:

The event handler for the objectDestroy event is required to be a parameterless function with result type

IO ()

It should call the function mainQuit to leave the event loop of Gtk.


The Use of the set Function

The example program demonstrates an additional feature: The set function which is used to specify certain attributes of the window.

The function set takes two arguments:

An attribute assignment is written as

The statement shown in the example sets the two attributes windowDefaultWidth and windowDefaultHeight. Both attributes together specify the initial size of the window.

You can also specify a minimal window size. To do that, you add two additional attributes:

set window [windowDefaultWidth := 250,    --  initial window size
            windowDefaultHeight := 280,
            widgetWidthRequest := 200,    --  minimal window size
            widgetHeightRequest := 220]

For these four attributes, the value -1 can be used to revoke a previously specified value.

For most example programs of this tutorial, both an initial and a minimal window size will be specified.

There are other window attributes you may wish to set:

as well as others, less frequently used ones.

Hints for those who want to know more:

The function set is defined in module System.Glib.Attributes.

Most Gtk elements have some attributes. These attributes can be found in the documentation. Bear in mind that a class inherits all attributes of all its base classes.


previous page Table of Section Contents next page