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