560af5c5 |
1 | ;; Common Lisp bindings for GTK+ v2.0 |
82747bbd |
2 | ;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no> |
560af5c5 |
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 | |
a4b3ec58 |
18 | ;; $Id: gobject.lisp,v 1.14 2004-10-28 09:34:35 espen Exp $ |
560af5c5 |
19 | |
20 | (in-package "GLIB") |
21 | |
22 | |
23 | (eval-when (:compile-toplevel :load-toplevel :execute) |
c8c48a4c |
24 | (defclass gobject (ginstance) |
560af5c5 |
25 | () |
c8c48a4c |
26 | (:metaclass ginstance-class) |
82747bbd |
27 | (:alien-name "GObject") |
de8516ca |
28 | (:copy %object-ref) |
29 | (:free %object-unref))) |
560af5c5 |
30 | |
4d83a8a6 |
31 | |
82747bbd |
32 | (defmethod initialize-instance ((object gobject) &rest initargs) |
4d83a8a6 |
33 | (let ((slotds (class-slots (class-of object))) |
34 | (names (make-array 0 :adjustable t :fill-pointer t)) |
35 | (values (make-array 0 :adjustable t :fill-pointer t))) |
36 | |
37 | (loop |
38 | as tmp = initargs then (cddr tmp) while tmp |
39 | as key = (first tmp) |
40 | as value = (second tmp) |
41 | as slotd = (find-if |
42 | #'(lambda (slotd) |
43 | (member key (slot-definition-initargs slotd))) |
44 | slotds) |
45 | when (and (typep slotd 'effective-gobject-slot-definition) |
46 | (slot-value slotd 'construct)) |
47 | do (let ((type (find-type-number (slot-definition-type slotd)))) |
48 | (vector-push-extend (slot-definition-pname slotd) names) |
49 | (vector-push-extend (gvalue-new type value) values) |
50 | (remf initargs key))) |
51 | |
52 | (setf |
53 | (slot-value object 'location) |
54 | (if (zerop (length names)) |
55 | (%gobject-new (type-number-of object)) |
a4b3ec58 |
56 | (%gobject-newvv (type-number-of object) (length names) names values))) |
57 | |
58 | (mapc #'gvalue-free values)) |
59 | |
4d83a8a6 |
60 | (apply #'call-next-method object initargs)) |
61 | |
62 | |
a9044181 |
63 | |
d4b21b08 |
64 | (defbinding (%gobject-new "g_object_new") () pointer |
a9044181 |
65 | (type type-number) |
66 | (nil null)) |
560af5c5 |
67 | |
4d83a8a6 |
68 | (defbinding (%gobject-newvv "g_object_newvv") () pointer |
69 | (type type-number) |
70 | (n-parameters unsigned-int) |
71 | (names (vector string)) |
72 | (values (vector gvalue))) |
560af5c5 |
73 | |
de8516ca |
74 | |
4d83a8a6 |
75 | (defbinding %object-ref (type location) pointer |
de8516ca |
76 | (location pointer)) |
77 | |
4d83a8a6 |
78 | (defbinding %object-unref (type location) nil |
79 | (location pointer)) |
de8516ca |
80 | |
81 | (defun object-ref (object) |
82 | (%object-ref nil (proxy-location object))) |
83 | |
84 | (defun object-unref (object) |
85 | (%object-unref nil (proxy-location object))) |
86 | |
87 | |
d4b21b08 |
88 | |
323d4265 |
89 | ;;;; Property stuff |
560af5c5 |
90 | |
26b133ed |
91 | (defbinding %object-set-property () nil |
c8c48a4c |
92 | (object gobject) |
93 | (name string) |
94 | (value gvalue)) |
86d9d6ab |
95 | |
26b133ed |
96 | (defbinding %object-get-property () nil |
c8c48a4c |
97 | (object gobject) |
98 | (name string) |
a9044181 |
99 | (value gvalue)) |
86d9d6ab |
100 | |
26b133ed |
101 | (defbinding %object-notify () nil |
c8c48a4c |
102 | (object gobject) |
103 | (name string)) |
86d9d6ab |
104 | |
26b133ed |
105 | (defbinding object-freeze-notify () nil |
a9044181 |
106 | (object gobject)) |
86d9d6ab |
107 | |
26b133ed |
108 | (defbinding object-thaw-notify () nil |
a9044181 |
109 | (object gobject)) |
86d9d6ab |
110 | |
26b133ed |
111 | (defbinding %object-set-qdata-full () nil |
86d9d6ab |
112 | (object gobject) |
113 | (id quark) |
114 | (data unsigned-long) |
115 | (destroy-marshal pointer)) |
116 | |
a9044181 |
117 | |
118 | ;;;; User data |
119 | |
86d9d6ab |
120 | (defun (setf object-data) (data object key &key (test #'eq)) |
121 | (%object-set-qdata-full |
122 | object (quark-from-object key :test test) |
c8c48a4c |
123 | (register-user-data data) *destroy-notify*) |
86d9d6ab |
124 | data) |
125 | |
26b133ed |
126 | (defbinding %object-get-qdata () unsigned-long |
86d9d6ab |
127 | (object gobject) |
128 | (id quark)) |
129 | |
130 | (defun object-data (object key &key (test #'eq)) |
131 | (find-user-data |
132 | (%object-get-qdata object (quark-from-object key :test test)))) |
133 | |
134 | |
560af5c5 |
135 | |
a9044181 |
136 | ;;;; Metaclass used for subclasses of gobject |
137 | |
138 | (eval-when (:compile-toplevel :load-toplevel :execute) |
4d83a8a6 |
139 | (defclass gobject-class (ginstance-class) |
140 | ()) |
c8c48a4c |
141 | |
d4b21b08 |
142 | (defclass direct-gobject-slot-definition (direct-virtual-slot-definition) |
4d83a8a6 |
143 | ((pname :reader slot-definition-pname :initarg :pname) |
144 | (readable :initform t :reader slot-readable-p :initarg :readable) |
145 | (writable :initform t :reader slot-writable-p :initarg :writable) |
146 | (construct :initform nil :initarg :construct))) |
147 | |
148 | (defclass effective-gobject-slot-definition (effective-virtual-slot-definition) |
149 | ((pname :reader slot-definition-pname :initarg :pname) |
150 | (readable :reader slot-readable-p :initarg :readable) |
151 | (writable :reader slot-writable-p :initarg :writable) |
152 | (construct :initarg :construct)))) |
c8c48a4c |
153 | |
154 | |
560af5c5 |
155 | |
26b133ed |
156 | ; (defbinding object-class-install-param () nil |
560af5c5 |
157 | ; (class pointer) |
158 | ; (id unsigned-int) |
159 | ; (parameter parameter)) |
160 | |
26b133ed |
161 | ; (defbinding object-class-find-param-spec () parameter |
560af5c5 |
162 | ; (class pointer) |
163 | ; (name string)) |
164 | |
de8516ca |
165 | (defun signal-name-to-string (name) |
166 | (substitute #\_ #\- (string-downcase (string name)))) |
a9044181 |
167 | |
a9044181 |
168 | |
4d83a8a6 |
169 | (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs) |
a9044181 |
170 | (case (getf initargs :allocation) |
323d4265 |
171 | (:property (find-class 'direct-gobject-slot-definition)) |
a9044181 |
172 | (t (call-next-method)))) |
173 | |
4d83a8a6 |
174 | (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs) |
a9044181 |
175 | (case (getf initargs :allocation) |
323d4265 |
176 | (:property (find-class 'effective-gobject-slot-definition)) |
a9044181 |
177 | (t (call-next-method)))) |
178 | |
4d83a8a6 |
179 | (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds) |
180 | (if (eq (most-specific-slot-value direct-slotds 'allocation) :property) |
181 | (nconc |
182 | (list :pname (signal-name-to-string |
183 | (most-specific-slot-value direct-slotds 'pname)) |
184 | :readable (most-specific-slot-value direct-slotds 'readable) |
185 | :writable (most-specific-slot-value direct-slotds 'writable) |
186 | :construct (most-specific-slot-value direct-slotds 'construct)) |
187 | (call-next-method)) |
188 | (call-next-method))) |
189 | |
190 | |
191 | (defmethod initialize-internal-slot-functions ((slotd effective-gobject-slot-definition)) |
192 | (let* ((type (slot-definition-type slotd)) |
193 | (pname (slot-definition-pname slotd)) |
194 | (type-number (find-type-number type))) |
195 | (unless (slot-boundp slotd 'reader-function) |
196 | (setf |
197 | (slot-value slotd 'reader-function) |
198 | (if (slot-readable-p slotd) |
199 | #'(lambda (object) |
200 | (with-gc-disabled |
201 | (let ((gvalue (gvalue-new type-number))) |
202 | (%object-get-property object pname gvalue) |
203 | (unwind-protect |
204 | (funcall |
205 | (intern-reader-function (type-from-number type-number)) gvalue +gvalue-value-offset+) ; temporary workaround for wrong topological sorting of types |
206 | (gvalue-free gvalue t))))) |
207 | #'(lambda (value object) |
208 | (error "Slot is not readable: ~A" (slot-definition-name slotd)))))) |
209 | |
210 | (unless (slot-boundp slotd 'writer-function) |
211 | (setf |
212 | (slot-value slotd 'writer-function) |
213 | (if (slot-writable-p slotd) |
214 | #'(lambda (value object) |
215 | (with-gc-disabled |
216 | (let ((gvalue (gvalue-new type-number))) |
217 | (funcall |
218 | (intern-writer-function (type-from-number type-number)) ; temporary |
219 | value gvalue +gvalue-value-offset+) |
220 | (%object-set-property object pname gvalue) |
221 | (funcall |
222 | (intern-destroy-function (type-from-number type-number)) ; temporary |
223 | gvalue +gvalue-value-offset+) |
224 | (gvalue-free gvalue nil) |
225 | value))) |
226 | #'(lambda (value object) |
227 | (error "Slot is not writable: ~A" (slot-definition-name slotd)))))) |
228 | |
229 | (unless (slot-boundp slotd 'boundp-function) |
230 | (setf |
231 | (slot-value slotd 'boundp-function) |
a9044181 |
232 | #'(lambda (object) |
4d83a8a6 |
233 | (declare (ignore object)) |
234 | t)))) |
235 | (call-next-method)) |
236 | |
a9044181 |
237 | |
a9044181 |
238 | (defmethod validate-superclass ((class gobject-class) |
239 | (super pcl::standard-class)) |
4de90d10 |
240 | ; (subtypep (class-name super) 'gobject) |
241 | t) |
d4b21b08 |
242 | |
243 | |
244 | |
245 | ;;;; |
246 | |
323d4265 |
247 | (defbinding %object-class-list-properties () pointer |
d4b21b08 |
248 | (class pointer) |
249 | (n-properties unsigned-int :out)) |
250 | |
323d4265 |
251 | (defun query-object-class-properties (type-number &optional |
252 | inherited-properties) |
4de90d10 |
253 | (let ((class (type-class-ref type-number))) |
d4b21b08 |
254 | (multiple-value-bind (array length) |
323d4265 |
255 | (%object-class-list-properties class) |
256 | (unwind-protect |
257 | (let ((all-properties |
258 | (map-c-array 'list #'identity array 'param length))) |
259 | (if (not inherited-properties) |
260 | (delete-if |
261 | #'(lambda (param) |
262 | (not (eql type-number (param-owner-type param)))) |
263 | all-properties) |
264 | all-properties)) |
265 | (deallocate-memory array))))) |
d4b21b08 |
266 | |
267 | |
268 | (defun default-slot-name (name) |
269 | (intern (substitute #\- #\_ (string-upcase (string-upcase name))))) |
270 | |
271 | (defun default-slot-accessor (class-name slot-name type) |
272 | (intern |
273 | (format |
274 | nil "~A-~A~A" class-name slot-name |
4d83a8a6 |
275 | (if (eq type 'boolean) "-P" "")))) |
d4b21b08 |
276 | |
323d4265 |
277 | (defun expand-gobject-type (type-number &optional options |
4de90d10 |
278 | (metaclass 'gobject-class)) |
337933d8 |
279 | (let* ((supers (cons (supertype type-number) (implements type-number))) |
d4b21b08 |
280 | (class (type-from-number type-number)) |
4d83a8a6 |
281 | (manual-slots (getf options :slots)) |
d4b21b08 |
282 | (expanded-slots |
283 | (mapcar |
284 | #'(lambda (param) |
323d4265 |
285 | (with-slots (name flags value-type documentation) param |
d4b21b08 |
286 | (let* ((slot-name (default-slot-name name)) |
4d83a8a6 |
287 | ; (slot-type value-type) ;(type-from-number value-type t)) |
288 | (slot-type (or (type-from-number value-type) value-type)) |
d4b21b08 |
289 | (accessor |
4d83a8a6 |
290 | (default-slot-accessor class slot-name slot-type)));(type-from-number slot-type)))) ; temporary workaround for wrong topological sorting of types |
291 | |
d4b21b08 |
292 | `(,slot-name |
323d4265 |
293 | :allocation :property |
294 | :pname ,name |
4de90d10 |
295 | ,@(cond |
296 | ((and |
297 | (member :writable flags) |
4d83a8a6 |
298 | (member :readable flags) |
299 | (not (member :construct-only flags))) |
4de90d10 |
300 | (list :accessor accessor)) |
4d83a8a6 |
301 | ((and (member :writable flags) |
302 | (not (member :construct-only flags))) |
d4b21b08 |
303 | (list :writer `(setf ,accessor))) |
4de90d10 |
304 | ((member :readable flags) |
305 | (list :reader accessor))) |
4d83a8a6 |
306 | ,@(when (or |
307 | (not (member :writable flags)) |
308 | (member :construct-only flags)) |
309 | (list :writable nil)) |
310 | ,@(when (not (member :readable flags)) |
311 | (list :readable nil)) |
312 | ,@(when (or |
313 | (member :construct flags) |
314 | (member :construct-only flags)) |
315 | (list :construct t)) |
4de90d10 |
316 | ,@(when (or |
317 | (member :construct flags) |
4d83a8a6 |
318 | (member :construct-only flags) |
4de90d10 |
319 | (member :writable flags)) |
d4b21b08 |
320 | (list :initarg (intern (string slot-name) "KEYWORD"))) |
321 | :type ,slot-type |
322 | ,@(when documentation |
323 | (list :documentation documentation)))))) |
324 | (query-object-class-properties type-number)))) |
325 | |
4d83a8a6 |
326 | (dolist (slot-def (reverse manual-slots)) |
323d4265 |
327 | (let ((name (car slot-def)) |
328 | (pname (getf (cdr slot-def) :pname))) |
329 | (setq |
330 | expanded-slots |
331 | (delete-if |
332 | #'(lambda (expanded-slot-def) |
333 | (or |
334 | (eq name (car expanded-slot-def)) |
335 | (and |
336 | pname |
337 | (string= pname (getf (cdr expanded-slot-def) :pname))))) |
338 | expanded-slots)) |
339 | |
340 | (unless (getf (cdr slot-def) :ignore) |
341 | (push slot-def expanded-slots)))) |
d4b21b08 |
342 | |
323d4265 |
343 | `(progn |
337933d8 |
344 | (defclass ,class ,supers |
323d4265 |
345 | ,expanded-slots |
346 | (:metaclass ,metaclass) |
347 | (:alien-name ,(find-type-name type-number)))))) |
348 | |
349 | |
350 | (register-derivable-type 'gobject "GObject" 'expand-gobject-type) |
d4b21b08 |
351 | |