From: Dmitry Kalyanov Date: Mon, 1 Jun 2009 20:09:23 +0000 (+0400) Subject: Updated tutorial X-Git-Url: http://repo.macrolet.net/gitweb/?a=commitdiff_plain;h=773d2a73ddfd56842675472852526883c79a571b;p=cl-gtk2.git Updated tutorial --- diff --git a/doc/hello_world.png b/doc/hello_world.png index 6d8c849..71f5631 100644 Binary files a/doc/hello_world.png and b/doc/hello_world.png differ diff --git a/doc/tutorial.xml b/doc/tutorial.xml index 160e20d..d28a0a5 100644 --- a/doc/tutorial.xml +++ b/doc/tutorial.xml @@ -47,40 +47,21 @@ "Hello world" example Let's start from a simple example. - Load the GTK system: (asdf:oos 'asdf:load-op :gtk) - Having loaded the GTK system, compile the following code: + Start Slime, type the following code in the REPL: -(defpackage :gtk-hello - (:use :cl :gtk :gobject :glib) - (:export :run)) +(asdf:oos 'asdf:load-op :gtk) -(in-package :gtk-hello) - -(defun run () - (let ((output *standard-output*)) - (with-main-loop - (let ((window (make-instance 'gtk-window - :type :toplevel - :window-position :center - :title "Hello world!" - :default-width 300 - :default-height 100)) - (button (make-instance 'button :label "Hello, world!")) - (counter 0)) - (g-signal-connect button "clicked" - (lambda (b) - (declare (ignore b)) - (format output "Hello, world!~%") - (setf (button-label button) - (format nil - "Hello, world! (clicked ~D times)" - (incf counter))))) - (container-add window button) - (widget-show window :all t))))) +(gtk:within-main-loop + (let ((window (make-instance 'gtk:gtk-window :title "Hello, world!"))) + (gtk:widget-show window))) - And now, run it:(gtk-hello:run) - This code should create a form with a button. When the button is clicked, this program prints a message to standard output and changes the text of the button. - If you have a window, then congratulations! Otherwise, contact the author for support. + The empty window with title "Hello, world!" should appear. + Let's analyze this example line-by-line. + (asdf:oos 'asdf:load-op :gtk) loads the GTK system into Lisp. + CL-GTK2 runs Gtk+ main loop in background thread (because Lisp development is interactive in its nature; if main loop would block the REPL thread, you would have to restart the Lisp image too often). Because all access to Gtk+ should come from Gtk+ thread, we should run the code in that thread. Macro gtk:within-main-loop does exactly that: it schedules the code to be tun in the Gtk+ thread. You should use this macro whenever you want evaluate the code from the REPL or when you start you application. + Next, we create the window with make-instance and set its title property to "Hello, world!". + When the window is created, it is not yet shown on the screen. To show it, we call (gtk:widget-show window). + After this code executes, you should get back to the REPL (rememer, Gtk+ runs in background thread) and the window should appear on the screen.
Example walk-through