It is not difficult at all to programm a window. The creation of a window requires only four function calls that can be written into one single function definition:
import "gtk3" Graphics.UI.Gtk main :: IO () main = do initGUI window <- windowNew widgetShow window mainGUI
This is sufficient to create and to display a window. The window has no content at all, but it is decorated with a title bar and a frame. The title bar provides facilities to move, to resize, to minimize, to maximize and to close the window. Some window managers may offer additional actions.
Explanations and Notes
To use Gtk-3 in your program, you must import the module Graphics.UI.Gtk. It is good practise to write the import statement with the additional hint "gtk3". This hint helps the compiler to find the correct module when modules for both Gtk-2 and Gtk-3 are installed on your computer.
The entire program ist written as one single parameterless function of type IO (). That type enables the function to communicate with its environment, that is, with the operating system.
Before it can be used, Gtk must be prepared for usage. This is done with the call
initGUI
(When you use Gtk-3 with the programming language C, this function is called "gtk_init".)
Next, the function windowNew is called to create the window to be shown. A reference to the newly created window is stored in the variable window.
Next, the window is made visible with a call of function widgetShow. That function takes the variable window as it's sole argument.
To make the window work, you must call the function
mainGUI
This call enters a loop that processes all user input to the window.
(When you use Gtk-3 with the programming language C, this function is called "gtk_main".)
Archive file with this example: Example001.tar.gz. Be sure to read the file README.txt for further instructions. Included in that archive file is the makefile that is described below.
Compilation
To compile a program you can directly call the ghc compiler:
ghc -O2 -o Main Main.hs -XPackageImports
The compiler creates three files:
It is also possible to prepare a small makefile to simplify the compilation:
CC = ghc NAME = Main OPTIONS = -XPackageImports all: $(NAME).hs $(CC) -O2 -o $(NAME) $(NAME).hs $(OPTIONS) clean: rm -f $(NAME).hi rm -f $(NAME).o rm -f $(NAME)
The overall structure of this makefile follows established conventions:
Note that make targets are written with tabs. In the text shown above, a green bar indicates the presence of a tab. These tabs are in fact needed; the makefile will fail to work when you replace the tabs with a sequence of blanks! You can copy the text shown above into your preferred text editor. In principle the copy function can keep the tabs, but bear in mind that you have to configure your editor in such a way that it does not automatically expand tabs into sequences of blanks.
This tutorial provides makefiles for all examples.
To compile your program with that makefile, open a termial window in the foulder that contains your program and the makefile and type:
make
To start the compiled program, enter
./Main
into the terminal window you used to compile the program.
Regrettably, the code shown is not sufficient to terminate the program after the window was removed from the screen.
To see that the program continues to run after its window was closed, do the following: