λgtk

It's GTK in a Lispy way

λgtk is a cross-platform Lisp interface to the complete GTK+2 family of shared libraries. λgtk currently runs in the following Lisp environments:

and is released under the Lisp Lesser General Public License (LLGPL).

If you are familiar with Lisp function call syntax and the GTK API then programming with λgtk is straightforward. See examples.lisp for some demonstrations translated from the GTK tutorial.

Design

Because GTK is so large and complex λgtk generates its Lisp FFI interfaces from a GTK definition file (gtk.ffi, output by Gary Byers' ffigen program) and an API control file (gtk.api) that you can edit and customize in order to generate more or less inclusive interfaces to GTK. λgtk is organized into two layers: a common file (lambda-gtk-common.lisp) and an implementation file for each target FFI (lambda-gtk-openmcl.lisp, lambda-gtk-cmusbcl.lisp, and so on). With the exception of callbacks (which are required by GTK itself) λgtk uses only a modest set of features from its host environment and is readily portable to other Lisps, including reasonably featured Schemes.

Features

λgtk provides Lisp programmers with the following set of features:

Lacunæ

The project provides a complete FFI to GTK in several different Lisps but is missing some obvious functionality. Here is a list of some missing features tagged by implementation difficulty:

Please contact me if you would like to help out!

Installation

  1. Download the λgtk sources and restore it to a directory on your machine.

  2. If you are using OpenMCL, download the OpenMCL GTK Interface and untar it inside OpenMCL's ccl/darwin-headers/ folder:

    $ cd /path/to/ccl/darwin-headers 
    $ tar -zxf openmcl-gtk-interface.tar.gz
    
  3. If you are using SBCL, install Thomas Burdick's Alien Function package and save a new sbcl.core image:

    $ tar -zxf sbcl-af-2004-10-22.tgz
    $ cd sbcl-af
    $ sbcl --load "system"
    * (sb-ext:save-lisp-and-die "/tmp/sbcl.core")
    $ cd /usr/local/lib/sbcl
    $ mv sbcl.core sbcl.core.orig
    $ mv /tmp/sbcl.core .
  4. Generate a GTK interface file: cd to the lambda-gtk installation directory, load the appropriate implementation file and call the lambda-gtk function. For example, in sbcl it might look like:
    $ cd /path/to/lambda-gtk
    $ sbcl
    * (load "lambda-gtk-cmusbcl")
    * (lambda-gtk "gtkffi-cmusbcl.lisp")
    * (quit)
    
  5. Compile and load the generated FFI into Lisp. Compiling a full interface will probably take between two and twelve minutes, depending on your Lisp implementation and processor speed. Once the interface is loaded you can start programming in GTK. The examples.lisp file is a fine place to start:
    $ sbcl
    * (load (compile-file "gtkffi-cmusbcl"))
    * (load "examples")
    * (hello-world)
    

    Examples range from the canonical "Hello World" to "Scribble Simple", the most complex demo in the GTK tutorial.

Documentation

Reading the GTK API reference is the best way to learn how to use λgtk: other than the simple symbol translations described in this document and the handful of glue functions shown below λgtk does not add any additional syntax or features of its own to GTK. The generation process is equally simple, there is only one entry point, the lambda-gtk function itself:

(lambda-gtk file . lib-packaging)

Outputs a Lisp/GTK FFI into file. If lib-packaging is true (the default) then GTK symbols are striped of their C library prefixes and placed in Lisp package named after the libraries: GTK, GDK, G, ATK and PANGO. If lib-packaging is false then only the GTK package is created and the Lisp symbols preserve their GTK library prefix. (It is assumed in this case that the user will use the FFI symbols without any package prefixing whatsoever.) Regardless of how lib-packaging is set, Lisp names always substitute the Lisp hyphen "-" in place of the C underbar "_".

The following table shows the effect of lib-packaging on a few representative symbols:

C name lib-packaging true lib-packaging false
gtk:+true+ +gtk-true+
gtk_main gtk:main gtk-main
GdkRectangle.x gdk:Rectangle.x GdkRectangle.x
In addition to exporting the symbols that GTK itself defines, λgtk also provides a handful of constants and functions for working with pointers and foreign objects in a portable way. This "glue code" consists of the following list of symbols: gtk:+true+, gtk:+false+, gtk:init-ensure, g:nullptr, g:nullptr?, g:callback,gtk:define-signal-handler, gtk:struct-alloc, gtk:struct-free, gtk:cstring->string, gtk:string->cstring, gtk:cstring-free.

Downloads

CVS

You can browse the CVS repository or download the current development tree via anonymous cvs, as described here

Mailing Lists

Pretty Pictures

If you use λgtk to develop a GUI interface in Lisp please send a pretty picture!

Here is mine:

[Stable links only, please!]

Acknowledgments

I would never have attempted this interface without the FFI tools that Gary Byers provides in his OpenMCL distribution. The callback mechanism for SBCL is written Thomas Burdick. Several of the GTK examples are adapted from Mario Mommer's l-gtk distribution.

Contact

Rick Taube
Associate Professor, Composition/Theory
School of Music, University of Illinois
Urbana, IL 61801 USA
Net: taube@uiuc.edu
Fax: 1 217 244-4585
Vox: 1 217 244-2684


Valid XHTML 1.0!