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 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:
containerChild
The content of the window. We will have to assign a value to that attribute as soon as we furnish some window content.
windowTitle
The title shown in the title bar of the window.
windowType
The type of the window. The most usual window type is WindowTopLevel. The type WindowPopup is used mainly for dialog windows.
windowResizable
A logical value that indicates whether or not the window is resizable.
windowModal
A logical value that indicates whether or not the window is modal.
windowWindowPosition
This attribute specifies, how the window will be placed on the screen. Possible values of this attribute are:
WinPosNone
The position of the window is left unspecified.
WinPosCenter
The window is placed in the middle of the display.
WinPosMouse
The window placement depends from the position of the mouse. If possible, the center of the window coincides with the mouse position.
WinPosCenterAlways
The window is placed in the middle of the display. That centered placement is kept when the window is resized.
WinPosCenterOnParent
The window is placed centered in front of its parent window. This is a good placement for dialog windows.
windowDeletable
...
windowIcon
specifies the icon for the window
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.