GTK graphics

From ScriptBasic

With Scriptbasic it is possible to create Graphical User Interfaces (GUI's) using The Gimp Toolkit (GTK). Though intended for usage with Unix, it is possible to use these graphical possibilities with Windows as well.

This article will focus on using Scriptbasic together with the GTK-server.

Table of contents

Installing the GTK-server

Before programming a GUI, the GTK-server needs to be installed. The software can be downloaded freely from the GTK-server website ( For Linux, precompiled packages are available there also. For other types of Unix, the sources must be downloaded and compiled. Make sure that GTK ( is installed for your Linux or Unix version.

For Windows, precompiled binaries can be downloaded, but also the Windows-port for GTK needs to be installed. The GTK-environment for Windows can be downloaded from the GTK-server website ( as well. It is strongly recommended to use the GTK 2.x versions.

Conceptual overview of the GTK-server

The GTK-server is an external binary, which is able to communicate with an interpreted program. The interpreted program sends original GTK functions to the GTK-server, which will invoke this function in the GTK libraries. The GTK-server will return a result to the interpreted program.

So the communication is always synchronous: the client program will send a command, after which the GTK-server responds with a result. This result is always returned as a string.

The actual communication between the GTK-server and Scriptbasic takes place using TCP sockets or pipes. For TCP, the standard network functionality as implemented in Scriptbasic can be used. For the pipes, which behave like a file, the standard opening and closing of files can be used.

How does the GTK-server know what properties a GTK function has? For this it will read a configuration file (, in which the GTK functions are described. The standard installation of the GTK-server already provides a configuration file, but the programmer is free to make changes if needed.

The default configuration file does not cover all available GTK functions. If additional functions must be used, the programmer needs to add them himself manually.

During startup of the GTK-server, the configuration file must be available. The search order for the configuration file in Unix is as follows: first search the directory of the Scriptbasic program, if not available here try to read the environment variable "GTK_SERVER_CONFIG". If this environment variable is not available, try to find the config file in the home directory of the user, if not found here search the "/etc" directory, and finally search the "/usr/local/etc" directory.

The search order for Windows is as follows: first search the directory of the Scriptbasic program, if not available here try to read the environment variable "GTK_SERVER_CONFIG", if not available here then search the directory where the GTK-server binary itself resides.

So per individual Scriptbasic program a different GTK-server configfile can be used. If the configfile cannot be found, the GTK-server exits with an error message.

Programming with the GTK-server

How does a general program flow looks like? Which steps need to be taken to use the GTK-server? This will be illustrated with a sample program.

REM -------------------------------------------



PRINT #1, st & nl
LINE INPUT #1, tmp

GTK = tmp


REM -------------------------------------------

pid = SYSTEM("gtk-server tcp=localhost:50000")
OPEN "localhost:50000" FOR SOCKET AS 1

GTK("gtk_init NULL NULL")
win = GTK("gtk_window_new 0")
GTK("gtk_window_set_title " & win & " \"This is a title\"")
table = GTK("gtk_table_new 20 20 1")
GTK("gtk_container_add " & win & " " & table)
label = GTK("gtk_label_new \"Hello world\"")
GTK("gtk_table_attach_defaults " & table & " " & label & " 1 19 3 7")
button = GTK("gtk_button_new_with_label Exit")
GTK("gtk_table_attach_defaults " & table & " " & button & " 10 18 13 17")
GTK("gtk_widget_show_all " & win)

   event = GTK("gtk_server_callback WAIT")
UNTIL (event = button OR event = win)

PRINT #1, "gtk_exit 0"

As is visible from above, the program starts with executing the GTK-server. The arguments to the GTK-server explain that the used communication interface is TCP. Then the IP address and portnumber is mentioned. For most programs a portnumber higher than 1024 must be used, since lower portnumbers require administrator privileges.

The SLEEP is needed to let the GTK-server initialize itself and to setup the TCP socket.

Then the socket to the GTK-server is opened by the Scriptbasic program. On this socket the communication will take place. The communication function 'GTK' will write to it and read from it.

The next stage is defining the actual Graphical User Interface (GUI). The Scriptbasic program sends original GTK functions to the GTK-server, which in it's turn will realize these functions in the GTK libraries. It will return a result string to the Scriptbasic program. If a function did not succeed, it will return "-1". Otherwise it will return "ok". If a function creates a widget, like a button, or a window, it will return a number as identifier.

For a detailed description on GTK functions, please consult the documentation at the Gimp ToolKit website.

The third stage is the mainloop. In the mainloop the GTK-server will wait for an event on the widgets. If an event occurs, the identifier of the widget which caused the event is returned. In the example above, the mainloop will run until an event happens on the button or on the window.

The final stage is exiting the GTK-server. The GTK function 'gtk_exit' is sent directly, and this will exit the GTK-server. Therefore, it is not needed to read an answer from the TCP socket. After the exit, the socket is closed and the program ends.

Distributing a standalone package

When you are ready with developing your Scriptbasic program, it is possible to distribute it as a complete standalone package. In this way, your end users do not have to be bothered with the tedious install procedures for the GTK-environment and the GTK-server.

So, how to create such a distribution? The basic principle for this is to put all the needed binary's and libraries in the same directory. Summarized the following files need to be distributed:

  • The GTK-server
  • The GTK-server configfile
  • The GTK libraries (DLL's)
  • The Scriptbasic binary
  • The Scriptbasic program itself

For Windows, it is recommended to change the setting LOG_FILE in the configfile. The Windows version of the GTK-server always will create a logfile, to redirect the warnings generated by the GTK libraries. The warnings of GTK in a Win32 enviroment mostly concern fonts which cannot be found, and they explain that GTK will use some other font. If these warnings are not redirected, then a separate DOS box will popup and the warnings will be displayed in there. Since this is an ugly situation, the warnings will be redirected to the logfile. Therefore, put the LOG_FILE setting to your current directory (e.g. '.').

These warnings about fonts do not occur in Linux.

The example Base64 converter written in Scriptbasic also has been put into a standalone distribution (Win32).

  • For a GTK 1.x demonstration package, download here ( (2003799 bytes)
  • For a GTK 2.x demonstration package, download here ( (2941484 bytes)

External links

The GTK-server website (

The Gimp Toolkit website (