GTK DSL
A declarative domain-specific language for building GTK4 user interfaces from plain text files. Write layout descriptions in .gdsl files — the engine parses them at runtime and constructs native GTK widget trees automatically.
Why a DSL?
GTK's C API requires ~40 lines to create a labeled slider inside a box. The DSL reduces it to 3 lines. No XML, no Glade, no runtime interpreter — just a simple text grammar compiled alongside your C code.
Build & Run
cd LinuxUtilities
./build_gtk_dsl_demo.sh # compiles gtk_dsl_demo.c
./build/bin/gtk_dsl_demo dsl/workbench.gdsl # load a .gdsl layout
# Optional install
./build_gtk_dsl_demo.sh --install
~/Programs/bin/gtk_dsl_demo dsl/workbench.gdsl
File Format
A .gdsl file is a series of widget declarations, one per line. Each widget has a type, optional parent reference, and key=value properties.
# Example: workbench.gdsl
# ─────────────────────────
# Root container — a vertical box
box id=root orientation=vertical spacing=8
# Header label
label parent=root text="GTK DSL Workbench" css="font-size:18px;font-weight:bold"
# Horizontal toolbar
box id=toolbar parent=root orientation=horizontal spacing=4
button parent=toolbar label="Open" action=open_file
button parent=toolbar label="Save" action=save_file
button parent=toolbar label="Refresh" action=reload_dsl
# Settings panel
grid id=settings parent=root columns=2 row-spacing=6 col-spacing=12
label parent=settings text="Brightness"
scale parent=settings min=0 max=100 value=75 action=set_brightness
label parent=settings text="Night Mode"
switch parent=settings active=true action=toggle_night
Widget Types
| Type | GTK Widget | Key Properties |
|---|---|---|
box |
GtkBox | orientation (vertical/horizontal), spacing |
grid |
GtkGrid | columns, row-spacing, col-spacing |
label |
GtkLabel | text, css |
button |
GtkButton | label, action |
switch |
GtkSwitch | active (true/false), action |
scale |
GtkScale | min, max, value, step, action |
entry |
GtkEntry | placeholder, text, action |
image |
GtkImage | file, width, height |
Common Properties
| Property | Applies To | Description |
|---|---|---|
id | All | Unique widget name (used as parent reference) |
parent | All | ID of the containing widget |
css | All | Inline CSS string applied via GtkCssProvider |
action | Button, Switch, Scale, Entry | Callback name registered in C code |
tooltip | All | Hover tooltip text |
visible | All | true (default) or false |
sensitive | All | Enable/disable widget interaction |
Architecture
┌──── .gdsl file ─────┐ ┌────── Parser ──────┐ ┌────── GTK Tree ──────┐
│ │ │ │ │ │
│ box id=root ... │────▶│ tokenize line │────▶│ GtkBox (root) │
│ label parent=root │ │ extract type │ │ ├─ GtkLabel │
│ button parent=root │ │ parse key=value │ │ ├─ GtkButton │
│ scale parent=root │ │ resolve parent │ │ └─ GtkScale │
│ │ │ create widget │ │ │
└─────────────────────┘ └────────────────────┘ └──────────────────────┘
│
▼
┌────── Actions ──────┐
│ │
│ C callback map │
│ g_signal_connect │
│ │
└─────────────────────┘
Registering Custom Actions
Actions in .gdsl files map to C functions registered in the action table:
// In gtk_dsl_demo.c
static void on_set_brightness(GtkRange *range, gpointer data) {
int value = (int)gtk_range_get_value(range);
printf("Brightness: %d%%\n", value);
}
// Register before loading .gdsl
dsl_register_action("set_brightness", G_CALLBACK(on_set_brightness));
Tips
- Comments — lines starting with
#are ignored - Whitespace — use spaces to separate properties (not commas)
- Quotes — required for values containing spaces:
text="Hello World" - Hot reload — the "Refresh" button re-parses the
.gdslwithout restarting - Debugging — set
GTK_DEBUG=interactiveto inspect the generated widget tree