Delight

Aug 2009, Thomas Leonard

Introduction

The Delight standard library includes support for creating graphical applications using GTK. A simple Hello World program:

import dlt.gtk: *

class Main:
	in Gtk gtk

	void main(string[] args):
		args = gtk.init_check(args)

		auto window = gtk.gtkWindow()
		auto label = gtk.gtkLabel("Hello world")

		window.add(label)
		window.show_all()

		gtk.main()

Note that you must link GTK programs with the -ldltgtk option.

GObject linkage

D provides two libraries for GTK. BCD provides low-level C-style bindings, which GtkD wraps GTK objects in D objects to provide a nicer API.

Delight, instead, adds a new external linkage type for GObject. For example:

extern (GObject):
	interface GtkWidget:
		void show()

	interface GtkWindow extends GtkWidget:
		void set_title(const(char) *title)

	GtkWindow gtk_window_new(int type)
	void gtk_init(int*? argc, char**? argv)
	void gtk_main()

class Main:
	void main():
		gtk_init(null, null)

		GtkWindow window = gtk_window_new(0)
		window.set_title("GObject demo")
		window.show()

		gtk_main()

No wrapping is done here. When you call window.show, Delight searches the GtkWindow and GtkWidget interfaces (at compile time) to find the show method. Due to the GObject linkage, this turns into a call to the C symbol gtk_widget_show.

Advantages:

  • No unnecessary casts or verbose function names (as with BCD).
  • No extra objects created (as with GtkD).

Disadvantages:

  • No automatic memory management. You must ref and unref these objects just as in a C program. Also, if GTK holds a pointer to a Delight object, the garbage collector won't see it and may collect the object anyway.
  • Strings must be C-style null-terminated arrays. D constant strings are OK, but you need to call toStringz(s) to add the null for other strings.

So, it may be worth wrapping the objects anyway. However, GtkD doesn't work with GDC currently.