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