Added code for automatic type definitions
[clg] / glib / gobject.lisp
... / ...
CommitLineData
1;; Common Lisp bindings for GTK+ v2.0
2;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
3;;
4;; This library is free software; you can redistribute it and/or
5;; modify it under the terms of the GNU Lesser General Public
6;; License as published by the Free Software Foundation; either
7;; version 2 of the License, or (at your option) any later version.
8;;
9;; This library is distributed in the hope that it will be useful,
10;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12;; Lesser General Public License for more details.
13;;
14;; You should have received a copy of the GNU Lesser General Public
15;; License along with this library; if not, write to the Free Software
16;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18;; $Id: gobject.lisp,v 1.7 2001/05/11 16:08:08 espen Exp $
19
20(in-package "GLIB")
21
22
23(eval-when (:compile-toplevel :load-toplevel :execute)
24 (defclass gobject (ginstance)
25 ()
26 (:metaclass ginstance-class)
27 (:alien-name "GObject")
28 (:ref "g_object_ref")
29 (:unref "g_object_unref")))
30
31(defmethod initialize-instance ((object gobject) &rest initargs)
32 (declare (ignore initargs))
33 (setf
34 (slot-value object 'location)
35 (%gobject-new (type-number-of object)))
36 (call-next-method))
37
38(defbinding (%gobject-new "g_object_new") () pointer
39 (type type-number)
40 (nil null))
41
42
43
44;;;; Parameter stuff
45
46(defbinding %object-set-property () nil
47 (object gobject)
48 (name string)
49 (value gvalue))
50
51(defbinding %object-get-property () nil
52 (object gobject)
53 (name string)
54 (value gvalue))
55
56(defbinding %object-notify () nil
57 (object gobject)
58 (name string))
59
60(defbinding object-freeze-notify () nil
61 (object gobject))
62
63(defbinding object-thaw-notify () nil
64 (object gobject))
65
66(defbinding %object-set-qdata-full () nil
67 (object gobject)
68 (id quark)
69 (data unsigned-long)
70 (destroy-marshal pointer))
71
72
73;;;; User data
74
75(defun (setf object-data) (data object key &key (test #'eq))
76 (%object-set-qdata-full
77 object (quark-from-object key :test test)
78 (register-user-data data) *destroy-notify*)
79 data)
80
81(defbinding %object-get-qdata () unsigned-long
82 (object gobject)
83 (id quark))
84
85(defun object-data (object key &key (test #'eq))
86 (find-user-data
87 (%object-get-qdata object (quark-from-object key :test test))))
88
89
90
91;;;; Metaclass used for subclasses of gobject
92
93(eval-when (:compile-toplevel :load-toplevel :execute)
94 (defclass gobject-class (ginstance-class))
95
96 (defclass direct-gobject-slot-definition (direct-virtual-slot-definition)
97 ((param :reader slot-definition-param)))
98
99 (defclass effective-gobject-slot-definition
100 (effective-virtual-slot-definition)))
101
102
103; (defbinding object-class-install-param () nil
104; (class pointer)
105; (id unsigned-int)
106; (parameter parameter))
107
108; (defbinding object-class-find-param-spec () parameter
109; (class pointer)
110; (name string))
111
112
113(defmethod initialize-instance :after ((slotd direct-gobject-slot-definition)
114 &rest initargs &key param)
115 (declare (ignore initargs))
116 (when param
117 (setf
118 (slot-value slotd 'param)
119 (signal-name-to-string (slot-definition-name slotd)))))
120
121(defmethod direct-slot-definition-class ((class gobject-class) initargs)
122 (case (getf initargs :allocation)
123 (:param (find-class 'direct-gobject-slot-definition))
124 (t (call-next-method))))
125
126(defmethod effective-slot-definition-class ((class gobject-class) initargs)
127 (case (getf initargs :allocation)
128 (:param (find-class 'effective-gobject-slot-definition))
129 (t (call-next-method))))
130
131(defmethod compute-virtual-slot-accessors
132 ((class gobject-class) (slotd effective-gobject-slot-definition)
133 direct-slotds)
134 (with-slots (type) slotd
135 (let ((param-name (slot-definition-param (first direct-slotds)))
136 (type-number (find-type-number type))
137 (getter (intern-reader-function type))
138 (setter (intern-writer-function type))
139 (destroy (intern-destroy-function type)))
140 (list
141 #'(lambda (object)
142 (with-gc-disabled
143 (let ((gvalue (gvalue-new type-number)))
144 (%object-get-property object param-name gvalue)
145 (prog1
146 (funcall getter gvalue +gvalue-value-offset+)
147 (gvalue-free gvalue t)))))
148 #'(lambda (value object)
149 (with-gc-disabled
150 (let ((gvalue (gvalue-new type-number)))
151 (funcall setter value gvalue +gvalue-value-offset+)
152 (%object-set-property object param-name gvalue)
153 (funcall destroy gvalue +gvalue-value-offset+)
154 (gvalue-free gvalue nil)
155 value)))))))
156
157(defmethod validate-superclass ((class gobject-class)
158 (super pcl::standard-class))
159 (subtypep (class-name super) 'gobject))
160
161
162
163;;;;
164
165(defbinding %object-class-properties () pointer
166 (class pointer)
167 (n-properties unsigned-int :out))
168
169(defun query-object-class-properties (type)
170 (let ((class (type-class-ref type)))
171 (multiple-value-bind (array length)
172 (%object-class-properties class)
173 (map-c-array 'list #'identity array 'param length))))
174
175(defun query-object-class-dependencies (class)
176 (delete-duplicates
177 (reduce
178 #'nconc
179 (mapcar
180 #'(lambda (param)
181 ;; A gobject does not depend on it's supertypes due to forward
182 ;; referenced superclasses
183 (delete-if
184 #'(lambda (type)
185 (type-is-p class type))
186 (type-hierarchy (param-type param))))
187 (query-object-class-properties class)))))
188
189
190(defun default-slot-name (name)
191 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
192
193(defun default-slot-accessor (class-name slot-name type)
194 (intern
195 (format
196 nil "~A-~A~A" class-name slot-name
197 (if (eq 'boolean type) "-p" ""))))
198
199(defun expand-gobject-type (type-number &optional slots)
200 (let* ((super (supertype type-number))
201 (class (type-from-number type-number))
202 (expanded-slots
203 (mapcar
204 #'(lambda (param)
205 (with-slots (name flags type documentation) param
206 (let* ((slot-name (default-slot-name name))
207 (slot-type (type-from-number type))
208 (accessor
209 (default-slot-accessor class slot-name slot-type)))
210 `(,slot-name
211 :allocation :param
212 :param ,name
213 ,@(when (member :writable flags)
214 (list :writer `(setf ,accessor)))
215 ,@(when (member :readable flags)
216 (list :reader accessor))
217 ,@(when (member :construct flags)
218 (list :initarg (intern (string slot-name) "KEYWORD")))
219 :type ,slot-type
220 ,@(when documentation
221 (list :documentation documentation))))))
222 (query-object-class-properties type-number))))
223
224 `(defclass ,class (,super)
225 ,expanded-slots
226 (:metaclass gobject-class)
227 (:alien-name ,(find-type-name type-number)))))
228
229(register-derivable-type
230 'gobject "GObject"
231 :query 'query-object-class-dependencies
232 :expand 'expand-gobject-type)
233