0d07716f |
1 | ;; Common Lisp bindings for GTK+ v2.0 |
ca0a8e30 |
2 | ;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no> |
0d07716f |
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 | |
22a2e918 |
18 | ;; $Id: gobject.lisp,v 1.24 2004/12/21 00:04:48 espen Exp $ |
0d07716f |
19 | |
20 | (in-package "GLIB") |
21 | |
22 | |
0c87a95a |
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) |
64bce834 |
85 | (if (typep (first direct-slotds) 'direct-property-slot-definition) |
0c87a95a |
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))) |
64bce834 |
100 | (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd)) |
0c87a95a |
101 | (setf |
64bce834 |
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))))))) |
0c87a95a |
112 | |
64bce834 |
113 | (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd)) |
0c87a95a |
114 | (setf |
64bce834 |
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 | |
0c87a95a |
126 | (call-next-method)) |
127 | |
128 | |
129 | ;;;; Super class for all classes in the GObject type hierarchy |
130 | |
0d07716f |
131 | (eval-when (:compile-toplevel :load-toplevel :execute) |
c9819f3e |
132 | (defclass gobject (ginstance) |
0d07716f |
133 | () |
0c87a95a |
134 | (:metaclass gobject-class) |
6baf860c |
135 | (:alien-name "GObject"))) |
136 | |
64bce834 |
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 | |
ca0a8e30 |
154 | (defmethod initialize-instance ((object gobject) &rest initargs) |
22a2e918 |
155 | (unless (slot-boundp object 'location) |
156 | ;; Extract initargs which we should pass directly to the GObeject |
157 | ;; constructor |
158 | (let* ((slotds (class-slots (class-of object))) |
159 | (args (when initargs |
160 | (loop |
161 | as (key value . rest) = initargs then rest |
162 | as slotd = (find-if |
163 | #'(lambda (slotd) |
164 | (member key (slot-definition-initargs slotd))) |
165 | slotds) |
166 | when (and (typep slotd 'effective-property-slot-definition) |
167 | (slot-value slotd 'construct)) |
168 | collect (progn |
169 | (remf initargs key) |
170 | (list |
171 | (slot-definition-pname slotd) |
172 | (slot-definition-type slotd) |
173 | value)) |
174 | while rest)))) |
175 | (if args |
176 | (let* ((string-size (size-of 'string)) |
177 | (string-writer (writer-function 'string)) |
178 | (string-destroy (destroy-function 'string)) |
179 | (params (allocate-memory |
180 | (* (length args) (+ string-size +gvalue-size+))))) |
6baf860c |
181 | (loop |
22a2e918 |
182 | for (pname type value) in args |
6baf860c |
183 | as tmp = params then (sap+ tmp (+ string-size +gvalue-size+)) |
22a2e918 |
184 | do (funcall string-writer pname tmp) |
185 | (gvalue-init (sap+ tmp string-size) type value)) |
186 | (unwind-protect |
187 | (setf |
188 | (slot-value object 'location) |
189 | (%gobject-newv (type-number-of object) (length args) params)) |
190 | (loop |
191 | repeat (length args) |
192 | as tmp = params then (sap+ tmp (+ string-size +gvalue-size+)) |
193 | do (funcall string-destroy tmp) |
194 | (gvalue-unset (sap+ tmp string-size))) |
195 | (deallocate-memory params))) |
7bde5a67 |
196 | (setf |
197 | (slot-value object 'location) |
22a2e918 |
198 | (%gobject-new (type-number-of object)))))) |
7bde5a67 |
199 | |
935a783c |
200 | (apply #'call-next-method object initargs)) |
201 | |
202 | |
29933e83 |
203 | (defmethod instance-finalizer ((instance gobject)) |
204 | (let ((location (proxy-location instance))) |
205 | #'(lambda () |
206 | (remove-cached-instance location) |
29933e83 |
207 | (%object-unref location)))) |
208 | |
6baf860c |
209 | |
3a935dfa |
210 | (defbinding (%gobject-new "g_object_new") () pointer |
b75a7b77 |
211 | (type type-number) |
212 | (nil null)) |
0d07716f |
213 | |
6baf860c |
214 | (defbinding (%gobject-newv "g_object_newv") () pointer |
935a783c |
215 | (type type-number) |
216 | (n-parameters unsigned-int) |
6baf860c |
217 | (params pointer)) |
9db52165 |
218 | |
219 | |
3a935dfa |
220 | |
383653f5 |
221 | ;;;; Property stuff |
0d07716f |
222 | |
c2cd6b3a |
223 | (defbinding %object-set-property () nil |
c9819f3e |
224 | (object gobject) |
225 | (name string) |
226 | (value gvalue)) |
a905f5d9 |
227 | |
c2cd6b3a |
228 | (defbinding %object-get-property () nil |
c9819f3e |
229 | (object gobject) |
230 | (name string) |
b75a7b77 |
231 | (value gvalue)) |
a905f5d9 |
232 | |
c2cd6b3a |
233 | (defbinding %object-notify () nil |
c9819f3e |
234 | (object gobject) |
235 | (name string)) |
a905f5d9 |
236 | |
c2cd6b3a |
237 | (defbinding object-freeze-notify () nil |
b75a7b77 |
238 | (object gobject)) |
a905f5d9 |
239 | |
c2cd6b3a |
240 | (defbinding object-thaw-notify () nil |
b75a7b77 |
241 | (object gobject)) |
a905f5d9 |
242 | |
c2cd6b3a |
243 | (defbinding %object-set-qdata-full () nil |
a905f5d9 |
244 | (object gobject) |
245 | (id quark) |
246 | (data unsigned-long) |
247 | (destroy-marshal pointer)) |
248 | |
b75a7b77 |
249 | |
250 | ;;;; User data |
251 | |
a905f5d9 |
252 | (defun (setf object-data) (data object key &key (test #'eq)) |
253 | (%object-set-qdata-full |
254 | object (quark-from-object key :test test) |
a5c3a597 |
255 | (register-user-data data) (callback %destroy-user-data)) |
a905f5d9 |
256 | data) |
257 | |
c2cd6b3a |
258 | (defbinding %object-get-qdata () unsigned-long |
a905f5d9 |
259 | (object gobject) |
260 | (id quark)) |
261 | |
262 | (defun object-data (object key &key (test #'eq)) |
263 | (find-user-data |
264 | (%object-get-qdata object (quark-from-object key :test test)))) |
265 | |
266 | |
3a935dfa |
267 | ;;;; |
268 | |
383653f5 |
269 | (defbinding %object-class-list-properties () pointer |
3a935dfa |
270 | (class pointer) |
271 | (n-properties unsigned-int :out)) |
272 | |
b7833c4c |
273 | |
274 | (defun %map-params (params length type inherited-p) |
275 | (if inherited-p |
6baf860c |
276 | (map-c-vector 'list #'identity params 'param length) |
b7833c4c |
277 | (let ((properties ())) |
6baf860c |
278 | (map-c-vector 'list |
b7833c4c |
279 | #'(lambda (param) |
280 | (when (eql (param-owner-type param) type) |
281 | (push param properties))) |
282 | params 'param length) |
283 | (nreverse properties)))) |
284 | |
285 | (defun query-object-class-properties (type &optional inherited-p) |
286 | (let* ((type-number (find-type-number type)) |
287 | (class (type-class-ref type-number))) |
288 | (unwind-protect |
289 | (multiple-value-bind (array length) |
290 | (%object-class-list-properties class) |
291 | (unwind-protect |
292 | (%map-params array length type-number inherited-p) |
293 | (deallocate-memory array))) |
294 | ; (type-class-unref type-number) |
295 | ))) |
3a935dfa |
296 | |
297 | |
298 | (defun default-slot-name (name) |
299 | (intern (substitute #\- #\_ (string-upcase (string-upcase name))))) |
300 | |
301 | (defun default-slot-accessor (class-name slot-name type) |
302 | (intern |
303 | (format |
304 | nil "~A-~A~A" class-name slot-name |
935a783c |
305 | (if (eq type 'boolean) "-P" "")))) |
3a935dfa |
306 | |
383653f5 |
307 | |
64bce834 |
308 | (defun slot-definition-from-property (class property &optional args) |
b7833c4c |
309 | (with-slots (name flags value-type documentation) property |
310 | (let* ((slot-name (default-slot-name name)) |
64bce834 |
311 | (slot-type (or (getf args :type) (type-from-number value-type) value-type)) |
b7833c4c |
312 | (accessor (default-slot-accessor class slot-name slot-type))) |
313 | |
314 | `(,slot-name |
315 | :allocation :property :pname ,name |
64bce834 |
316 | |
317 | ;; temporary hack |
318 | ,@(cond |
319 | ((find :unbound args) (list :unbound (getf args :unbound))) |
320 | ((type-is-p slot-type 'gobject) (list :unbound nil))) |
b7833c4c |
321 | |
322 | ;; accessors |
323 | ,@(cond |
324 | ((and |
325 | (member :writable flags) (member :readable flags) |
326 | (not (member :construct-only flags))) |
327 | (list :accessor accessor)) |
328 | ((and (member :writable flags) (not (member :construct-only flags))) |
329 | (list :writer `(setf ,accessor))) |
330 | ((member :readable flags) |
331 | (list :reader accessor))) |
332 | |
333 | ;; readable/writable/construct |
334 | ,@(when (or (not (member :writable flags)) |
335 | (member :construct-only flags)) |
336 | '(:writable nil)) |
337 | ,@(when (not (member :readable flags)) |
338 | '(:readable nil)) |
339 | ,@(when (or (member :construct flags) |
340 | (member :construct-only flags)) |
341 | '(:construct t)) |
342 | |
343 | ;; initargs |
344 | ,@(when (or (member :construct flags) |
345 | (member :construct-only flags) |
346 | (member :writable flags)) |
347 | (list :initarg (intern (string slot-name) "KEYWORD"))) |
348 | |
349 | :type ,slot-type |
350 | :documentation ,documentation)))) |
351 | |
352 | |
353 | (defun slot-definitions (class properties slots) |
354 | (loop |
b7833c4c |
355 | for property in properties |
64bce834 |
356 | as slot = (or |
357 | (find (param-name property) slots |
358 | :key #'(lambda (slot) (getf (rest slot) :pname)) |
359 | :test #'string=) |
360 | (find (param-name property) slots |
361 | :key #'first :test #'string-equal)) |
362 | do (cond |
363 | ((not slot) |
364 | (push (slot-definition-from-property class property) slots)) |
365 | ((getf (rest slot) :merge) |
366 | (setf |
367 | (rest slot) |
368 | (rest (slot-definition-from-property class property (rest slot))))))) |
b7833c4c |
369 | (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots)) |
370 | |
371 | |
372 | (defun expand-gobject-type (type &optional options (metaclass 'gobject-class)) |
373 | (let ((supers (cons (supertype type) (implements type))) |
374 | (class (type-from-number type)) |
375 | (slots (getf options :slots))) |
376 | `(defclass ,class ,supers |
377 | ,(slot-definitions class (query-object-class-properties type) slots) |
378 | (:metaclass ,metaclass) |
379 | (:alien-name ,(find-type-name type))))) |
383653f5 |
380 | |
3a935dfa |
381 | |
b7833c4c |
382 | (register-derivable-type 'gobject "GObject" 'expand-gobject-type) |