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 | |
7391caeb |
18 | ;; $Id: gobject.lisp,v 1.12 2002-04-02 14:57:19 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 | |
82747bbd |
31 | (defmethod initialize-instance ((object gobject) &rest initargs) |
32 | (declare (ignore initargs)) |
de8516ca |
33 | (setf (slot-value object 'location) (%gobject-new (type-number-of object))) |
34 | (call-next-method)) |
a9044181 |
35 | |
d4b21b08 |
36 | (defbinding (%gobject-new "g_object_new") () pointer |
a9044181 |
37 | (type type-number) |
38 | (nil null)) |
560af5c5 |
39 | |
40 | |
de8516ca |
41 | (defbinding %object-ref (type location) pointer |
42 | (location pointer)) |
43 | |
44 | (defbinding %object-unref (type location) nil |
45 | (location pointer)) |
46 | |
47 | |
48 | (defun object-ref (object) |
49 | (%object-ref nil (proxy-location object))) |
50 | |
51 | (defun object-unref (object) |
52 | (%object-unref nil (proxy-location object))) |
53 | |
54 | |
d4b21b08 |
55 | |
323d4265 |
56 | ;;;; Property stuff |
560af5c5 |
57 | |
26b133ed |
58 | (defbinding %object-set-property () nil |
c8c48a4c |
59 | (object gobject) |
60 | (name string) |
61 | (value gvalue)) |
86d9d6ab |
62 | |
26b133ed |
63 | (defbinding %object-get-property () nil |
c8c48a4c |
64 | (object gobject) |
65 | (name string) |
a9044181 |
66 | (value gvalue)) |
86d9d6ab |
67 | |
26b133ed |
68 | (defbinding %object-notify () nil |
c8c48a4c |
69 | (object gobject) |
70 | (name string)) |
86d9d6ab |
71 | |
26b133ed |
72 | (defbinding object-freeze-notify () nil |
a9044181 |
73 | (object gobject)) |
86d9d6ab |
74 | |
26b133ed |
75 | (defbinding object-thaw-notify () nil |
a9044181 |
76 | (object gobject)) |
86d9d6ab |
77 | |
26b133ed |
78 | (defbinding %object-set-qdata-full () nil |
86d9d6ab |
79 | (object gobject) |
80 | (id quark) |
81 | (data unsigned-long) |
82 | (destroy-marshal pointer)) |
83 | |
a9044181 |
84 | |
85 | ;;;; User data |
86 | |
86d9d6ab |
87 | (defun (setf object-data) (data object key &key (test #'eq)) |
88 | (%object-set-qdata-full |
89 | object (quark-from-object key :test test) |
c8c48a4c |
90 | (register-user-data data) *destroy-notify*) |
86d9d6ab |
91 | data) |
92 | |
26b133ed |
93 | (defbinding %object-get-qdata () unsigned-long |
86d9d6ab |
94 | (object gobject) |
95 | (id quark)) |
96 | |
97 | (defun object-data (object key &key (test #'eq)) |
98 | (find-user-data |
99 | (%object-get-qdata object (quark-from-object key :test test)))) |
100 | |
101 | |
560af5c5 |
102 | |
a9044181 |
103 | ;;;; Metaclass used for subclasses of gobject |
104 | |
105 | (eval-when (:compile-toplevel :load-toplevel :execute) |
106 | (defclass gobject-class (ginstance-class)) |
c8c48a4c |
107 | |
d4b21b08 |
108 | (defclass direct-gobject-slot-definition (direct-virtual-slot-definition) |
323d4265 |
109 | ((pname :reader slot-definition-pname))) |
c8c48a4c |
110 | |
a9044181 |
111 | (defclass effective-gobject-slot-definition |
112 | (effective-virtual-slot-definition))) |
c8c48a4c |
113 | |
4de90d10 |
114 | |
560af5c5 |
115 | |
26b133ed |
116 | ; (defbinding object-class-install-param () nil |
560af5c5 |
117 | ; (class pointer) |
118 | ; (id unsigned-int) |
119 | ; (parameter parameter)) |
120 | |
26b133ed |
121 | ; (defbinding object-class-find-param-spec () parameter |
560af5c5 |
122 | ; (class pointer) |
123 | ; (name string)) |
124 | |
de8516ca |
125 | (defun signal-name-to-string (name) |
126 | (substitute #\_ #\- (string-downcase (string name)))) |
a9044181 |
127 | |
128 | (defmethod initialize-instance :after ((slotd direct-gobject-slot-definition) |
323d4265 |
129 | &rest initargs &key pname) |
a9044181 |
130 | (declare (ignore initargs)) |
323d4265 |
131 | (when pname |
d4b21b08 |
132 | (setf |
323d4265 |
133 | (slot-value slotd 'pname) |
d4b21b08 |
134 | (signal-name-to-string (slot-definition-name slotd))))) |
a9044181 |
135 | |
136 | (defmethod direct-slot-definition-class ((class gobject-class) initargs) |
137 | (case (getf initargs :allocation) |
323d4265 |
138 | (:property (find-class 'direct-gobject-slot-definition)) |
a9044181 |
139 | (t (call-next-method)))) |
140 | |
141 | (defmethod effective-slot-definition-class ((class gobject-class) initargs) |
142 | (case (getf initargs :allocation) |
323d4265 |
143 | (:property (find-class 'effective-gobject-slot-definition)) |
a9044181 |
144 | (t (call-next-method)))) |
145 | |
d4b21b08 |
146 | (defmethod compute-virtual-slot-accessors |
a9044181 |
147 | ((class gobject-class) (slotd effective-gobject-slot-definition) |
148 | direct-slotds) |
149 | (with-slots (type) slotd |
323d4265 |
150 | (let ((pname (slot-definition-pname (first direct-slotds))) |
4de90d10 |
151 | (type-number (find-type-number type))) |
a9044181 |
152 | (list |
153 | #'(lambda (object) |
154 | (with-gc-disabled |
155 | (let ((gvalue (gvalue-new type-number))) |
323d4265 |
156 | (%object-get-property object pname gvalue) |
157 | (unwind-protect |
4de90d10 |
158 | (funcall |
7391caeb |
159 | (intern-reader-function (type-from-number type-number)) gvalue +gvalue-value-offset+) ; temporary workaround for wrong topological sorting of types |
a9044181 |
160 | (gvalue-free gvalue t))))) |
161 | #'(lambda (value object) |
162 | (with-gc-disabled |
163 | (let ((gvalue (gvalue-new type-number))) |
4de90d10 |
164 | (funcall |
7391caeb |
165 | (intern-writer-function (type-from-number type-number)) ; temporary |
4de90d10 |
166 | value gvalue +gvalue-value-offset+) |
323d4265 |
167 | (%object-set-property object pname gvalue) |
4de90d10 |
168 | (funcall |
7391caeb |
169 | (intern-destroy-function (type-from-number type-number)) ; temporary |
4de90d10 |
170 | gvalue +gvalue-value-offset+) |
a9044181 |
171 | (gvalue-free gvalue nil) |
172 | value))))))) |
173 | |
a9044181 |
174 | (defmethod validate-superclass ((class gobject-class) |
175 | (super pcl::standard-class)) |
4de90d10 |
176 | ; (subtypep (class-name super) 'gobject) |
177 | t) |
d4b21b08 |
178 | |
179 | |
180 | |
181 | ;;;; |
182 | |
323d4265 |
183 | (defbinding %object-class-list-properties () pointer |
d4b21b08 |
184 | (class pointer) |
185 | (n-properties unsigned-int :out)) |
186 | |
323d4265 |
187 | (defun query-object-class-properties (type-number &optional |
188 | inherited-properties) |
4de90d10 |
189 | (let ((class (type-class-ref type-number))) |
d4b21b08 |
190 | (multiple-value-bind (array length) |
323d4265 |
191 | (%object-class-list-properties class) |
192 | (unwind-protect |
193 | (let ((all-properties |
194 | (map-c-array 'list #'identity array 'param length))) |
195 | (if (not inherited-properties) |
196 | (delete-if |
197 | #'(lambda (param) |
198 | (not (eql type-number (param-owner-type param)))) |
199 | all-properties) |
200 | all-properties)) |
201 | (deallocate-memory array))))) |
d4b21b08 |
202 | |
203 | |
204 | (defun default-slot-name (name) |
205 | (intern (substitute #\- #\_ (string-upcase (string-upcase name))))) |
206 | |
207 | (defun default-slot-accessor (class-name slot-name type) |
208 | (intern |
209 | (format |
210 | nil "~A-~A~A" class-name slot-name |
4de90d10 |
211 | (if (eq 'boolean type) "-P" "")))) |
d4b21b08 |
212 | |
323d4265 |
213 | (defun expand-gobject-type (type-number &optional options |
4de90d10 |
214 | (metaclass 'gobject-class)) |
337933d8 |
215 | (let* ((supers (cons (supertype type-number) (implements type-number))) |
d4b21b08 |
216 | (class (type-from-number type-number)) |
323d4265 |
217 | (override-slots (getf options :slots)) |
d4b21b08 |
218 | (expanded-slots |
219 | (mapcar |
220 | #'(lambda (param) |
323d4265 |
221 | (with-slots (name flags value-type documentation) param |
d4b21b08 |
222 | (let* ((slot-name (default-slot-name name)) |
7391caeb |
223 | (slot-type value-type) ;(type-from-number value-type t)) |
d4b21b08 |
224 | (accessor |
7391caeb |
225 | (default-slot-accessor class slot-name (type-from-number slot-type)))) ; temporary workaround for wrong topological sorting of types |
d4b21b08 |
226 | `(,slot-name |
323d4265 |
227 | :allocation :property |
228 | :pname ,name |
4de90d10 |
229 | ,@(cond |
230 | ((and |
231 | (member :writable flags) |
232 | (member :readable flags)) |
233 | (list :accessor accessor)) |
234 | ((member :writable flags) |
d4b21b08 |
235 | (list :writer `(setf ,accessor))) |
4de90d10 |
236 | ((member :readable flags) |
237 | (list :reader accessor))) |
238 | ,@(when (or |
239 | (member :construct flags) |
240 | (member :writable flags)) |
d4b21b08 |
241 | (list :initarg (intern (string slot-name) "KEYWORD"))) |
242 | :type ,slot-type |
243 | ,@(when documentation |
244 | (list :documentation documentation)))))) |
245 | (query-object-class-properties type-number)))) |
246 | |
323d4265 |
247 | (dolist (slot-def override-slots) |
248 | (let ((name (car slot-def)) |
249 | (pname (getf (cdr slot-def) :pname))) |
250 | (setq |
251 | expanded-slots |
252 | (delete-if |
253 | #'(lambda (expanded-slot-def) |
254 | (or |
255 | (eq name (car expanded-slot-def)) |
256 | (and |
257 | pname |
258 | (string= pname (getf (cdr expanded-slot-def) :pname))))) |
259 | expanded-slots)) |
260 | |
261 | (unless (getf (cdr slot-def) :ignore) |
262 | (push slot-def expanded-slots)))) |
d4b21b08 |
263 | |
323d4265 |
264 | `(progn |
337933d8 |
265 | (defclass ,class ,supers |
323d4265 |
266 | ,expanded-slots |
267 | (:metaclass ,metaclass) |
268 | (:alien-name ,(find-type-name type-number)))))) |
269 | |
270 | |
271 | (register-derivable-type 'gobject "GObject" 'expand-gobject-type) |
d4b21b08 |
272 | |