1;; guile-gnome
2;; Copyright (C) 2001 Martin Baulig <martin@gnome.org>
3;; Copyright (C) 2003,2004 Andy Wingo <wingo at pobox dot com>
4
5;; This program is free software; you can redistribute it and/or
6;; modify it under the terms of the GNU General Public License as
7;; published by the Free Software Foundation; either version 2 of
8;; the License, or (at your option) any later version.
9;;
10;; This program is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13;; GNU General Public License for more details.
14;;
15;; You should have received a copy of the GNU General Public License
16;; along with this program; if not, contact:
17;;
18;; Free Software Foundation           Voice:  +1-617-542-5942
19;; 59 Temple Place - Suite 330        Fax:    +1-617-542-2652
20;; Boston, MA  02111-1307,  USA       gnu@gnu.org
21
22;;; Commentary:
23;;
24;; This is the Guile wrapper of @code{libgobject}, an implementation of
25;; a runtime, dynamic type system for C. Besides providing an object
26;; system to C, @code{libgobject}'s main design goal was to increase the
27;; ease with which C code can be wrapped by interpreted languages, such
28;; as Guile or Perl.
29;;
30;; This module, @code{(gnome gobject)}, just re-exports procedures from
31;; other modules, so its documentation seems an opportune spot for a
32;; more tutorial-like introduction. So open up a Guile session and let's
33;; begin.
34;;
35;; First, if you haven't done it, load the appropriate version of
36;; Guile-GNOME:
37;;
38;; @lisp
39;; guile> (use-modules (gnome-2))
40;; @end lisp
41;;
42;; Import @code{(gnome gobject)} also:
43;;
44;; @lisp
45;; guile> (use-modules (gnome gobject))
46;; @end lisp
47;;
48;; @code{(gnome gobject)} is based heavily on GOOPS, Guile's object
49;; system, so go ahead and load up that too:
50;;
51;; @lisp
52;; guile> (use-modules (oop goops))
53;; @end lisp
54;;
55;; We will leave off the @code{guile>} prompt in the rest of this
56;; tutorial. When we want to show the value of an expression, we use
57;; @result{}:
58;;
59;; @lisp
60;; (+ 3 5)
61;; @result{} 8
62;; @end lisp
63;;
64;; @section Basic types
65;;
66;; When communicating with @code{libgobject}, most values need to be
67;; strictly-typed. There is a type class corresponding to each basic type
68;; in C: @code{<gchar>}, @code{<guchar>}, @code{<gboolean>},
69;; @code{<gint>}, @code{<guint>}, @code{<glong>}, @code{<gulong>},
70;; @code{<gint64>}, @code{<guint64>}, @code{<gfloat>}, @code{<gdouble>},
71;; and @code{<gchararray>}.
72;;
73;; You can make instances of these class with @code{make}:
74;;
75;; @lisp
76;; (make <gboolean> #:value #f)
77;; @result{} #<gvalue <gboolean> 40529040 #f>
78;;
79;; (make <guint> #:value 85)
80;; @result{} #<gvalue <guint> 4054f040 85>
81;;
82;; (make <gfloat> #:value 3.1415)
83;; @result{} #<gvalue <gfloat> 40556af0 3.1414999961853>
84;;
85;; (make <gchararray> #:value "Hello World!")
86;; @result{} #<gvalue <gchararray> 4055af90 Hello World!>
87;; @end lisp
88;;
89;; You can get the normal Scheme values back with @code{gvalue->scm}:
90;;
91;; @lisp
92;; (gvalue->scm (make <gchararray> #:value "Hello World!"))
93;; @result{} "Hello World!"
94;; @end lisp
95;;
96;; @section Enums and flags
97;;
98;; Enumerated values and bitflags are an essential part of many C APIs,
99;; and so they are specially wrapped in the GLib type system. You can
100;; create new enumerated types in Scheme by subclassing @code{<genum>}:
101;;
102;; @lisp
103;; (define-class <foo> (<genum>)
104;;   #:vtable '#((hello "Hello World" 1) (test "Test" 2)))
105;; @end lisp
106;;
107;; Instances are created with @code{make}, just like with the other
108;; types:
109;;
110;; @lisp
111;; (make <foo> #:value 'hello)
112;; (make <foo> #:value "Hello World")
113;; (make <foo> #:value 1)
114;;
115;; ;; These three all do the same thing
116;; @result{} #<gvalue <foo> 406275f8 (hello Hello World 1)>
117;; @end lisp
118;;
119;; If there is an already existing enum or flags class, you can get
120;; information about it:
121;;
122;; @lisp
123;; (genum-class->value-table <foo>)
124;; @result{} #((hello "Hello World" 1) (test "Test" 2))
125;; @end lisp
126;;
127;; Enums and flags have a special representation on the Scheme side. You
128;; can convert them to Scheme values as symbols, names, or as a numeric
129;; value.
130;;
131;; @lisp
132;; (define foo (make <foo> #:value 'hello))
133;; (genum->symbol foo)
134;; @result{} hello
135;; (genum->name foo)
136;; @result{} "Hello World"
137;; (genum->value foo)
138;; @result{} 1
139;; @end lisp
140;;
141;; @section GType
142;;
143;; All of the types that GLib knows about are available to Guile,
144;; regardless of which language defined them. GLib implements this via a
145;; type system, where every type has a name. So if you make a type
146;; called ``Foo'' in C, you can get to it in Scheme via
147;; @code{gtype-name->class}:
148;;
149;; @lisp
150;; ;; Retrieve the type for the foo enum we made earlier in the tutorial
151;; (define copy-of-<foo> (gtype-name->class "Foo"))
152;; (eq? <foo> copy-of-<foo>)
153;; @result{} #t
154;;
155;; (make copy-of-<foo> #:value 2)
156;; @result{} #<gvalue <foo> 40535e50 (test Test 2)>
157;; @end lisp
158;;
159;; @section GObject
160;;
161;; @code{<gobject>} (@code{GObject} in C) is the basic object type in
162;; @code{libgobject}. @code{(gnome gobject)} allows you to access
163;; existing GObject types, as well as to create new GObject types in
164;; Scheme.
165;;
166;; Before we start, let's pull in some generic functions that reduce the
167;; amount of typing we have to do:
168;;
169;; @lisp
170;; (use-modules (gnome gobject generics))
171;; @end lisp
172;;
173;; Let's assume we start with @code{<gtk-window>} from @code{(gnome
174;; gtk)}. The keyword arguments to @code{make} are interpreted as
175;; GObject properties to set:
176;;
177;; @lisp
178;; (define window (make <gtk-window>
179;;                  #:type 'toplevel #:title "Hello, World!"))
180;; @end lisp
181;;
182;; You can connect to signals on the new instance:
183;;
184;; @lisp
185;; (connect window 'delete-event
186;;          (lambda (window event)
187;;            ;; Returns #t to ignore this event
188;;            #t))
189;;
190;; ;; connect is a generic function implemented by
191;; ;; gtype-instance-signal-connect
192;; @end lisp
193;;
194;; And get and set properties...
195;;
196;; @lisp
197;; (get window 'title)
198;; @result{} "Hello, World!"
199;; (set window 'resizable #f)
200;;
201;; ;; get and set are also generics, implemented by gobject-get-property
202;; ;; and gobject-set-property
203;; @end lisp
204;;
205;; @section Deriving your own GObject types
206;;
207;; You can create new GObject types directly from Scheme, deriving either
208;; from a C object type or one you made in Scheme.
209;;
210;; @lisp
211;; ;; deriving from <gobject>
212;; (define-class <test> (<gobject>)
213;;   ;; a normal object slot
214;;   my-data
215;;
216;;   ;; an object slot exported as a gobject property
217;;   (pub-data #:gparam (list <gparam-long> #:name 'test))
218;;
219;;   ;; a signal with no arguments and no return value
220;;   #:gsignal '(frobate #f))
221;;
222;; ;; deriving from <test> -- also inherits properties and signals
223;; (define-class <hungry> (<test>))
224;; @end lisp
225;;
226;; Adding a signal automatically defines the default method:
227;;
228;; @lisp
229;; ;; This is the default handler for this signal.
230;; (define-method (test:frobate (object <test>))
231;;   (format #t "Frobating ~A\n" object))
232;;
233;; ;; We can override it for subclasses
234;; (define-method (test:frobate (object <hungry>))
235;;   (next-method) ;; chain up
236;;   (format #t "I'm hungry\n"))
237;;
238;; (emit (make <hungry>) 'frobate)
239;; ;; Try it!
240;; @end lisp
241;;
242;; You can override the @code{initialize}, @code{gobject:get-property},
243;; and @code{gobject:set-property} methods. For an extended example, see
244;; @code{tic-tac-toe.scm} in the @code{gtk/examples/gtk} directory of
245;; the distribution.
246;;
247;;; Code:
248
249(define-module (gnome gobject)
250  #:use-module (gnome gobject gtype)
251  #:use-module (gnome gobject gvalue)
252  #:use-module (gnome gobject gclosure)
253  #:use-module (gnome gobject gsignal)
254  #:use-module (gnome gobject gparameter)
255  #:use-module (gnome gobject gobject)
256  #:use-module (gnome gw support modules))
257
258(re-export-modules (gnome gobject gtype)
259                   (gnome gobject gvalue)
260                   (gnome gobject gclosure)
261                   (gnome gobject gsignal)
262                   (gnome gobject gparameter)
263                   (gnome gobject gobject))
264
265;(let* ((doc-dir (gobject-scheme-dir))
266;       (doc-file (in-vicinity doc-dir "guile-gnome-gobject-procedures.txt")))
267;  (set! documentation-files (append! documentation-files (list doc-file))))
268