Skip to content
wd edited this page Nov 13, 2020 · 2 revisions

GTK User Interface Builder Library - UIGTK

UIGTK is a C-language written library designed to simplify the creation of the graphical environment with the GTK toolkit from the GtkBuilder object.

To help understand the library, the following example interface will be used:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
	<object id="window" class="GtkWindow">
		<property name="title">-- LIBUIGTK --</property>
		<property name="visible">True</property>
		<signal name="destroy" handler="gtk_main_quit" />
		<child>
			<object class="GtkBox" id="box">
				<property name="visible">True</property>
				<property name="orientation">vertical</property>
				<property name="expand">True</property>
				<child>
					<object class="GtkButton" id="btn1">
						<property name="visible">True</property>
						<property name="label">Hello</property>
						<signal name="clicked" handler="hello" />
					</object>
					<packing>
						<property name="expand">True</property>
						<property name="fill">True</property>
					</packing>
				</child>
				<child>
					<object class="GtkButton" id="btn2">
						<property name="visible">True</property>
						<property name="label">Bye</property>
						<signal name="clicked" handler="bye" />
					</object>
					<packing>
						<property name="expand">True</property>
						<property name="fill">True</property>
					</packing>
				</child>
			</object>
		</child>
	</object>
</interface>

This results in the following appearance:

Interface image

The interface file name will be set to "example.ui".

Interface construction

The construction is divided into three phases:

  • Initialize the library indicated an interface file (.ui);
  • Connect the interface signals to their respective handlers; and
  • Start the main loop.

Interface initialization

To initialize the interface, the uigtk_init() function must be used, whose characteristics are described below:

int uigtk_init(char *file);

Parameters

Name Description
file Interface file path

Returns

0 if a known error has occurred and 1 for successful calls.

Connecting signals

To connect the signals to your handlers, you must use the uigtk_hadler() function, whose characteristics are described below:

#define uigtk_handler(handler)

Parameters

Name Description
handler Handler function

Returns

0 if a known error has occurred and 1 for successful calls.

Alternative procedure

If you need to assign a function name that does not match the handler attribute, the uigtk_callback () function can be used with the following prototype:

int uigtk_callback (char *name, void (*handler)())
Name Description
name Handler attribute value
handler Handler function

Start the main loop

To start the main loop, use the uigtk_main() function, whose characteristics are described below:

int uigtk_main()

Returns

0 if a known error has occurred and 1 for successful calls.

At the end of the looping, a new call to the uigtk_init function will be required.

Example

Considering that the name of the file containing the source code is called "example.c" and that it is in the same directory as the interface file "example.ui", the following sequence of commands will be necessary to build the interface.

Initially it is necessary to include the library header in our source code. Considering that both are in the same directory:

#include "libuigtk.h"

As noted in the interface file, there are two defined handlers whose construction needs to be carried out:

<signal name="clicked" handler="hello" />
...
<signal name="clicked" handler="bye" />

The prototypes of the functions would look like this:

void hello(GtkWidget *widget, gpointer data);
void bye(GtkWidget *widget, gpointer data);

To start the construction, just call the function to initialize the library indicating the interface file:

uigtk_init("example.ui");

After initialization, the manipulators must be connected, according to the signals defined in the interface:

uigtk_handler(gtk_main_quit);
uigtk_handler(hello);
uigtk_handler(bye);

Finally, the main loop must be initialized:

uigtk_main();

Grouping all the information, we have:

#include "libuigtk.h"

void hello(GtkWidget *widget, gpointer data);

void bye(GtkWidget *widget, gpointer data);

void main(int argc, char *argv[]) {

	/* Interface initialization */
	uigtk_init("example.ui");

	/* Connecting signals */
	uigtk_handler(gtk_main_quit);
	uigtk_handler(hello);
	uigtk_handler(bye);

	/* Start the main loop */	
	uigtk_main();

}

Don't forget to define the actions of the manipulators.

GCC Compilation

To compile and run the application, the following commands were executed:

gcc `pkg-config --cflags gtk+-3.0` -c libuigtk.c `pkg-config --libs gtk+-3.0`;
gcc `pkg-config --cflags gtk+-3.0` -c example.c  `pkg-config --libs gtk+-3.0`;
gcc `pkg-config --cflags gtk+-3.0` -o example example.o libuigtk.o `pkg-config --libs gtk+-3.0`;
./example;

Tools

The library also has some auxiliary functions in case you need more freedom when designing the interface.

GTK Builder

If you need to access the pointer to the GTK Builder created by the library, the uigtk_builder() function must be used, whose characteristics are described below:

GtkBuilder *uigtk_builder (void)

Returns

Returns the pointer to GTK Builder or NULL if any known errors occur. If you want to link the value to a variable, alternatively, you can use the macro below:

#define uigtk_set_builder(var)

Where var is intended to define the name of the variable to receive the pointer.

GTK Object

If you need to access the pointer to any interface object ( from its "ID", the uigtk_object() function must be used, whose characteristics are described below:

GObject *uigtk_object (char *id)

Parameters

Name Description
id Object ID value

Returns

Returns the pointer to object or NULL if any known errors occur. If you want to link the value to a variable, alternatively, you can use the macro below:

#define uigtk_set_object(var, id)

Where var is intended to define the name of the variable to receive the pointer.

Dialog

If you need to access the pointer to any interface object ( from its "ID", the uigtk_dialog() function must be used, whose characteristics are described below:

int uigtk_dialog (int type, char *title, char *text)

Parameters

Name Description
type Dialog type
title Box title
text Textual content

Types

Interface image

Interface image

Interface image

Interface image

Interface image

Returns

Returns an integer value depending on the answer:

  • -1 if the option "no" is chosen;
  • 0 if the box has been closed; and
  • 1 if "yes" or "ok" is chosen.

Additional Notes

Handbook

Source Code

Package

  • libgtk-3-dev package or equivalent.

Compilation (GCC)

To compile the source code it is necessary to use the pkg-config --cflags gtk+-3.0 and pkg-config --libs gtk+-3.0 flags.

gcc `pkg-config --cflags gtk+-3.0` -c libuigtk.c  `pkg-config --libs gtk+-3.0`

Versions

v1.1.0 (2020-05-18)

  • Improved function returns; and
  • Improved terminal messages.

v1.0.0 (2020-02-22)

  • Initial release.

Authors