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