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