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 | |
9db52165 |
18 | ;; $Id: gobject.lisp,v 1.11 2002/03/24 12:50:30 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) |
ca0a8e30 |
27 | (:alien-name "GObject") |
9db52165 |
28 | (:copy %object-ref) |
29 | (:free %object-unref))) |
0d07716f |
30 | |
ca0a8e30 |
31 | (defmethod initialize-instance ((object gobject) &rest initargs) |
32 | (declare (ignore initargs)) |
9db52165 |
33 | (setf (slot-value object 'location) (%gobject-new (type-number-of object))) |
34 | (call-next-method)) |
b75a7b77 |
35 | |
3a935dfa |
36 | (defbinding (%gobject-new "g_object_new") () pointer |
b75a7b77 |
37 | (type type-number) |
38 | (nil null)) |
0d07716f |
39 | |
40 | |
9db52165 |
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 | |
3a935dfa |
55 | |
383653f5 |
56 | ;;;; Property stuff |
0d07716f |
57 | |
c2cd6b3a |
58 | (defbinding %object-set-property () nil |
c9819f3e |
59 | (object gobject) |
60 | (name string) |
61 | (value gvalue)) |
a905f5d9 |
62 | |
c2cd6b3a |
63 | (defbinding %object-get-property () nil |
c9819f3e |
64 | (object gobject) |
65 | (name string) |
b75a7b77 |
66 | (value gvalue)) |
a905f5d9 |
67 | |
c2cd6b3a |
68 | (defbinding %object-notify () nil |
c9819f3e |
69 | (object gobject) |
70 | (name string)) |
a905f5d9 |
71 | |
c2cd6b3a |
72 | (defbinding object-freeze-notify () nil |
b75a7b77 |
73 | (object gobject)) |
a905f5d9 |
74 | |
c2cd6b3a |
75 | (defbinding object-thaw-notify () nil |
b75a7b77 |
76 | (object gobject)) |
a905f5d9 |
77 | |
c2cd6b3a |
78 | (defbinding %object-set-qdata-full () nil |
a905f5d9 |
79 | (object gobject) |
80 | (id quark) |
81 | (data unsigned-long) |
82 | (destroy-marshal pointer)) |
83 | |
b75a7b77 |
84 | |
85 | ;;;; User data |
86 | |
a905f5d9 |
87 | (defun (setf object-data) (data object key &key (test #'eq)) |
88 | (%object-set-qdata-full |
89 | object (quark-from-object key :test test) |
c9819f3e |
90 | (register-user-data data) *destroy-notify*) |
a905f5d9 |
91 | data) |
92 | |
c2cd6b3a |
93 | (defbinding %object-get-qdata () unsigned-long |
a905f5d9 |
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 | |
0d07716f |
102 | |
b75a7b77 |
103 | ;;;; Metaclass used for subclasses of gobject |
104 | |
105 | (eval-when (:compile-toplevel :load-toplevel :execute) |
106 | (defclass gobject-class (ginstance-class)) |
c9819f3e |
107 | |
3a935dfa |
108 | (defclass direct-gobject-slot-definition (direct-virtual-slot-definition) |
383653f5 |
109 | ((pname :reader slot-definition-pname))) |
c9819f3e |
110 | |
b75a7b77 |
111 | (defclass effective-gobject-slot-definition |
112 | (effective-virtual-slot-definition))) |
c9819f3e |
113 | |
b011356b |
114 | |
0d07716f |
115 | |
c2cd6b3a |
116 | ; (defbinding object-class-install-param () nil |
0d07716f |
117 | ; (class pointer) |
118 | ; (id unsigned-int) |
119 | ; (parameter parameter)) |
120 | |
c2cd6b3a |
121 | ; (defbinding object-class-find-param-spec () parameter |
0d07716f |
122 | ; (class pointer) |
123 | ; (name string)) |
124 | |
9db52165 |
125 | (defun signal-name-to-string (name) |
126 | (substitute #\_ #\- (string-downcase (string name)))) |
b75a7b77 |
127 | |
128 | (defmethod initialize-instance :after ((slotd direct-gobject-slot-definition) |
383653f5 |
129 | &rest initargs &key pname) |
b75a7b77 |
130 | (declare (ignore initargs)) |
383653f5 |
131 | (when pname |
3a935dfa |
132 | (setf |
383653f5 |
133 | (slot-value slotd 'pname) |
3a935dfa |
134 | (signal-name-to-string (slot-definition-name slotd))))) |
b75a7b77 |
135 | |
136 | (defmethod direct-slot-definition-class ((class gobject-class) initargs) |
137 | (case (getf initargs :allocation) |
383653f5 |
138 | (:property (find-class 'direct-gobject-slot-definition)) |
b75a7b77 |
139 | (t (call-next-method)))) |
140 | |
141 | (defmethod effective-slot-definition-class ((class gobject-class) initargs) |
142 | (case (getf initargs :allocation) |
383653f5 |
143 | (:property (find-class 'effective-gobject-slot-definition)) |
b75a7b77 |
144 | (t (call-next-method)))) |
145 | |
3a935dfa |
146 | (defmethod compute-virtual-slot-accessors |
b75a7b77 |
147 | ((class gobject-class) (slotd effective-gobject-slot-definition) |
148 | direct-slotds) |
149 | (with-slots (type) slotd |
383653f5 |
150 | (let ((pname (slot-definition-pname (first direct-slotds))) |
b011356b |
151 | (type-number (find-type-number type))) |
b75a7b77 |
152 | (list |
153 | #'(lambda (object) |
154 | (with-gc-disabled |
155 | (let ((gvalue (gvalue-new type-number))) |
383653f5 |
156 | (%object-get-property object pname gvalue) |
157 | (unwind-protect |
b011356b |
158 | (funcall |
159 | (intern-reader-function type) gvalue +gvalue-value-offset+) |
b75a7b77 |
160 | (gvalue-free gvalue t))))) |
161 | #'(lambda (value object) |
162 | (with-gc-disabled |
163 | (let ((gvalue (gvalue-new type-number))) |
b011356b |
164 | (funcall |
165 | (intern-writer-function type) |
166 | value gvalue +gvalue-value-offset+) |
383653f5 |
167 | (%object-set-property object pname gvalue) |
b011356b |
168 | (funcall |
169 | (intern-destroy-function type) |
170 | gvalue +gvalue-value-offset+) |
b75a7b77 |
171 | (gvalue-free gvalue nil) |
172 | value))))))) |
173 | |
b75a7b77 |
174 | (defmethod validate-superclass ((class gobject-class) |
175 | (super pcl::standard-class)) |
b011356b |
176 | ; (subtypep (class-name super) 'gobject) |
177 | t) |
3a935dfa |
178 | |
179 | |
180 | |
181 | ;;;; |
182 | |
383653f5 |
183 | (defbinding %object-class-list-properties () pointer |
3a935dfa |
184 | (class pointer) |
185 | (n-properties unsigned-int :out)) |
186 | |
383653f5 |
187 | (defun query-object-class-properties (type-number &optional |
188 | inherited-properties) |
b011356b |
189 | (let ((class (type-class-ref type-number))) |
3a935dfa |
190 | (multiple-value-bind (array length) |
383653f5 |
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))))) |
3a935dfa |
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 |
b011356b |
211 | (if (eq 'boolean type) "-P" "")))) |
3a935dfa |
212 | |
383653f5 |
213 | (defun expand-gobject-type (type-number &optional options |
b011356b |
214 | (metaclass 'gobject-class)) |
7858d45e |
215 | (let* ((supers (cons (supertype type-number) (implements type-number))) |
3a935dfa |
216 | (class (type-from-number type-number)) |
383653f5 |
217 | (override-slots (getf options :slots)) |
3a935dfa |
218 | (expanded-slots |
219 | (mapcar |
220 | #'(lambda (param) |
383653f5 |
221 | (with-slots (name flags value-type documentation) param |
3a935dfa |
222 | (let* ((slot-name (default-slot-name name)) |
383653f5 |
223 | (slot-type (type-from-number value-type #|t|#)) |
3a935dfa |
224 | (accessor |
225 | (default-slot-accessor class slot-name slot-type))) |
226 | `(,slot-name |
383653f5 |
227 | :allocation :property |
228 | :pname ,name |
b011356b |
229 | ,@(cond |
230 | ((and |
231 | (member :writable flags) |
232 | (member :readable flags)) |
233 | (list :accessor accessor)) |
234 | ((member :writable flags) |
3a935dfa |
235 | (list :writer `(setf ,accessor))) |
b011356b |
236 | ((member :readable flags) |
237 | (list :reader accessor))) |
238 | ,@(when (or |
239 | (member :construct flags) |
240 | (member :writable flags)) |
3a935dfa |
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 | |
383653f5 |
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)))) |
3a935dfa |
263 | |
383653f5 |
264 | `(progn |
7858d45e |
265 | (defclass ,class ,supers |
383653f5 |
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) |
3a935dfa |
272 | |