Skip to main content

GTK Basics

This page provides a review of the GTK tablet is intended for use by application developers writing applications that utilize tablets on the Linux desktop.

At the time of this writing GTK 4.21.1 was the latest version. This document describes Tablet input in GTK3 (see the sample code).

Introduction

GTK is a free open-source widget toolkit for creating graphical user interfaces (GUIs) targeted at Linux and specifically GNOME.

Compatibility

GTK is a cross-platform toolkit with solid OS compatibility across Linux, Windows, and macOS, though support varies. Linux and BSD offer the most complete integration, especially with Wayland-based desktops, while Windows support is mature enough for many applications. macOS support exists, though it is less complete.

GTK 3 supports a wide range of backends including Wayland, X11, macOS Quartz, Windows Win32, and even the HTML5-based Broadway backend. GTK 4 narrows the list to modern, actively maintained backends: primarily Wayland, X11, macOS Quartz, and Win32.

GTK provides tablet compatibility through its input abstraction layer, GDK, which integrates pressure-sensitive stylus input, tilt data, erasers, proximity events, and multi-tool support across both Wayland and X11. On modern Wayland systems, GTK receives tablet events through the zwp_tablet_v2 protocol, giving applications access to smooth pressure curves and consistent device identifiers, while pad features—such as rings, strips, and hardware buttons—are also exposed via dedicated GDK interfaces. X11 remains fully supported.

Understanding the Toolkit

Developing a Tablet Drawing Application in GTK 3

When developing a tablet-focused drawing application in GTK 3, you must account for touch input, redraw efficiency, and screen rotation. The following outlines the key steps.

  1. Create and Place the Drawing Area Create the widget: Create the drawing area using gtk_drawing_area_new(). Add it to a container such as GtkBox or GtkGrid.
  2. Enable Input Signals Request input events: using gtk_widget_set_events() or gtk_widget_add_events() Use gtk_widget_add_events() to enable pointer events without overwriting existing masks:
  3. Connect Input signals Connect drawing callbacks: using for example: g_signal_connect (da, "draw", G_CALLBACK (scribble_draw), NULL).
  4. Implement Drawing and Efficient Redrawing Draw via Cairo: In the "draw" signal, use Cairo functions on the provided cairo_t *cr to render your backing surface or your stroke data. see draw_brush in the sample code
  5. Handle Rotation and Resize Tablets rotate and resize frequently. Connect to "configure-event" to detect size changes: check whether width/height changed and recreate your off-screen Cairo surface (back buffer) with the new dimensions. This ensures strokes render correctly and prevents mismatches between buffer and widget size.

Developing a Tablet Drawing Application in GTK 4

If developing in GTK 4 consider that there are numerous differences including that GTK 4 uses event controllers instead of event masks.