GTK

From Rixort Wiki
Jump to navigation Jump to search

General

  • Every widget in GTK is a GtkWidget.
  • GTK widgets use the GObject hierarchy, which allows for single inheritance. All GtkWidgets are ultimately GObjects.
  • GtkWidget is an abstract base class for all GTK widgets. Standard practice is to store all widgets as GtkWidgets.
  • gtk_widget_show() will only show the widget passed as the parameter. Invisible children will not be shown, and if the widget's parent is invisible then the widget will not be shown until its parent is.
  • gtk_widget_hide() only marks the parameter widget as hidden. Although all child widgets are hidden by implication, they are not set as such, and so showing the widget again will result in all of its children being shown.
  • gtk_widget_show_all() recursively draws the widget and all its children.
  • gtk_widget_hide_all() recursively hides and sets the hidden flag on the widget and all its children.
  • gtk_widget_set_sensitive sets a widget as active or inactive. Children take their sensitivity from their parents and any change to the parent will automatically affect the children.
  • gtk_window_set_icon_from_file sets the file icon, and will be automatically resized as required.
  • Top-level windows are placed by the window manager and so you should not assume any particular position.
  • After calling gtk_main(), control of the program cannot be regained until a callback function is initialised.

Console debugging

Data can be sent to the console via g_print.

Labels

  • Label text can be set when the label is created: GtkWidget* gtk_label_new(const gchar *str)
  • Label text cannot be selected by default. Enable it by calling: void gtk_label_set_selectable(GtkLabel *label, gboolean selectable). Error messages should always be selectable so that they can be copied and pasted into bug reports, search engines etc.

Containers

  • A widget can be added as the child of a container by calling: void gtk_container_add(GtkContainer *container, GtkWidget *child)
  • GtkWindow is derived from GtkBin, which can only contain one child. However, that child can be a container itself, which may in turn contain multiple children.

Signals and callbacks

Signals can be connected at any point within the application, including within callback functions. However, it is sensible to initialise any critical callbacks before calling gtk_main().

The underscore and dash characters are interchangeable in signal names, so "destroy_event" and "destroy-event" refer to the same signal.

g_signal_connect() returns a handler identifier which can be used with the g_signal_handler functions.

Signals are inherited from parent structures.

g_signal_connect requires the following parameters:

  1. gpointer object: The object to connect the signal to.
  2. const gchar *signal_name: The name of the signal to be connected.
  3. GCallBack handler: The name of the callback handler (a function). Must be cast with G_CALLBACK.
  4. gpointer data: Optional data, can be set to NULL.

Simple Makefile

The following Makefile is sufficient to compile GTK 3 applications using clang on Linux. This assumes that the GTK library has been registered with pkg-config, which will be the case if you have installed your distribution's package.

CC=clang
CFLAGS=-Wall -Wextra -Werror $(shell pkg-config gtk+-3.0 --cflags)
LDFLAGS=$(shell pkg-config gtk+-3.0 --libs)
SOURCES=helloworld.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=helloworld

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
  $(CC) $(LDFLAGS) $(OBJECTS) -o $@

%.o: %.c
   $(CC) $(CFLAGS) -c $< -o $@

clean:
  rm -rf *.o $(EXECUTABLE)

Articles