Merge remote-tracking branch 'andy128k/master'
[cl-gtk2.git] / glib / gobject.meta.lisp
blobfd245605c30c93c942d286973a41ae6686a12e65
1 (in-package :gobject)
3 (defclass gobject-class (standard-class)
4 ((g-type-name :initform nil
5 :accessor gobject-class-g-type-name)
6 (direct-g-type-name :initform nil
7 :initarg :g-type-name
8 :accessor gobject-class-direct-g-type-name)
9 (g-type-initializer :initform nil
10 :initarg :g-type-initializer
11 :reader gobject-class-g-type-initializer)
12 (interface-p :initform nil
13 :initarg :g-interface-p
14 :reader gobject-class-interface-p))
15 (:documentation "Metaclass for GObject-based classes."))
17 (defun initialize-gobject-class-g-type (class)
18 (if (gobject-class-g-type-initializer class)
19 (let* ((initializer-fn-ptr (foreign-symbol-pointer (gobject-class-g-type-initializer class)))
20 (type (when initializer-fn-ptr
21 (foreign-funcall-pointer initializer-fn-ptr nil
22 g-type-designator))))
23 (if (null initializer-fn-ptr)
24 (warn "Type initializer for class '~A' (GType '~A') is invalid: foreign symbol '~A'"
25 (gobject-class-direct-g-type-name class) (class-name class) (gobject-class-g-type-initializer class))
26 (progn
27 (when (eq (gtype +g-type-invalid+) type)
28 (warn "Declared GType name '~A' for class '~A' is invalid ('~A' returned 0)"
29 (gobject-class-direct-g-type-name class) (class-name class)
30 (gobject-class-g-type-initializer class)))
31 (unless (eq (gtype (gobject-class-direct-g-type-name class)) type)
32 (warn "Declared GType name '~A' for class '~A' does not match actual GType name '~A'"
33 (gobject-class-direct-g-type-name class)
34 (class-name class)
35 (gtype-name type))))))
36 (when (zerop (gtype-id (gtype (gobject-class-direct-g-type-name class))))
37 (warn "Declared GType name '~A' for class '~A' is invalid (g_type_name returned 0)"
38 (gobject-class-direct-g-type-name class) (class-name class)))))
40 (defun filter-from-initargs (initargs removed-key)
41 (loop
42 for (key value) on initargs by #'cddr
43 unless (eq key removed-key)
44 collect key and collect value))
46 (defun initargs-have-base-in-superclass (initargs base-class)
47 (let ((d-s (getf initargs :direct-superclasses)))
48 (loop
49 for class in d-s
50 thereis (subtypep class base-class))))
52 (defun compute-new-initargs-for-metaclass (initargs base-class)
53 (if (initargs-have-base-in-superclass initargs base-class)
54 initargs
55 (append (filter-from-initargs initargs :direct-superclasses)
56 (list :direct-superclasses
57 (append (getf initargs :direct-superclasses)
58 (list (find-class base-class)))))))
60 (defmethod initialize-instance :around ((class gobject-class) &rest initargs)
61 (apply #'call-next-method class (compute-new-initargs-for-metaclass initargs 'g-object)))
63 (defmethod reinitialize-instance :around ((class gobject-class) &rest initargs &key (direct-superclasses nil d-s-p) &allow-other-keys)
64 (declare (ignore direct-superclasses))
65 (if d-s-p
66 (apply #'call-next-method class (compute-new-initargs-for-metaclass initargs 'g-object))
67 (call-next-method)))
69 (defmethod initialize-instance :after ((object gobject-class) &key &allow-other-keys)
70 (when (gobject-class-direct-g-type-name object)
71 (register-object-type (gobject-class-direct-g-type-name object) (class-name object))
72 (at-init (object) (initialize-gobject-class-g-type object))))
74 (defmethod finalize-inheritance :after ((class gobject-class))
75 (iter (for superclass in (class-direct-superclasses class))
76 (unless (class-finalized-p superclass) (finalize-inheritance superclass)))
77 (setf (gobject-class-g-type-name class)
78 (or (gobject-class-direct-g-type-name class)
79 (let ((gobject-superclass (iter (for superclass in (class-direct-superclasses class))
80 (finding superclass such-that (typep superclass 'gobject-class)))))
81 (assert gobject-superclass)
82 (gobject-class-g-type-name gobject-superclass)))))
84 (defclass gobject-direct-slot-definition (standard-direct-slot-definition)
85 ((g-property-type :initform nil
86 :initarg :g-property-type
87 :reader gobject-direct-slot-definition-g-property-type)))
89 (defclass gobject-effective-slot-definition (standard-effective-slot-definition)
90 ((g-property-type :initform nil
91 :initarg :g-property-type
92 :accessor gobject-effective-slot-definition-g-property-type)))
94 (defclass gobject-property-direct-slot-definition (gobject-direct-slot-definition)
95 ((g-property-name :initform nil
96 :initarg :g-property-name
97 :reader gobject-property-direct-slot-definition-g-property-name)))
99 (defclass gobject-property-effective-slot-definition (gobject-effective-slot-definition)
100 ((g-property-name :initform nil
101 :initarg :g-property-name
102 :accessor gobject-property-effective-slot-definition-g-property-name)))
104 (defclass gobject-fn-direct-slot-definition (gobject-direct-slot-definition)
105 ((g-getter-name :initform nil
106 :initarg :g-getter
107 :reader gobject-fn-direct-slot-definition-g-getter-name)
108 (g-setter-name :initform nil
109 :initarg :g-setter
110 :reader gobject-fn-direct-slot-definition-g-setter-name)))
112 (defclass gobject-fn-effective-slot-definition (gobject-effective-slot-definition)
113 ((g-getter-name :initform nil
114 :initarg :g-getter
115 :accessor gobject-fn-effective-slot-definition-g-getter-name)
116 (g-setter-name :initform nil
117 :initarg :g-setter
118 :accessor gobject-fn-effective-slot-definition-g-setter-name)
119 (g-getter-fn :initform nil
120 :accessor gobject-fn-effective-slot-definition-g-getter-fn)
121 (g-setter-fn :initform nil
122 :accessor gobject-fn-effective-slot-definition-g-setter-fn)))
124 (defmethod validate-superclass ((class gobject-class) (superclass standard-class))
127 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs &key allocation &allow-other-keys)
128 (declare (ignore initargs))
129 (case allocation
130 (:gobject-property (find-class 'gobject-property-direct-slot-definition))
131 (:gobject-fn (find-class 'gobject-fn-direct-slot-definition))
132 (otherwise (call-next-method))))
134 (defvar *e-s-d* nil)
136 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
137 (declare (ignore initargs))
138 (or *e-s-d* (call-next-method)))
140 (defmethod compute-effective-slot-definition ((class gobject-class) name direct-slots)
141 (let ((effective-slot (let ((*e-s-d* (loop
142 for slot in direct-slots
143 when (typep slot 'gobject-direct-slot-definition)
144 return (etypecase slot
145 (gobject-property-direct-slot-definition (find-class 'gobject-property-effective-slot-definition))
146 (gobject-fn-direct-slot-definition (find-class 'gobject-fn-effective-slot-definition))))))
147 (call-next-method))))
148 (when (typep effective-slot 'gobject-effective-slot-definition)
149 (let ((allocation (loop
150 for direct-slot in direct-slots
151 when (slot-definition-allocation direct-slot)
152 return (slot-definition-allocation direct-slot)))
153 (property-name (loop
154 for direct-slot in direct-slots
155 when (and (typep direct-slot 'gobject-property-direct-slot-definition) (gobject-property-direct-slot-definition-g-property-name direct-slot))
156 return (gobject-property-direct-slot-definition-g-property-name direct-slot)))
157 (property-type (loop
158 for direct-slot in direct-slots
159 when (gobject-direct-slot-definition-g-property-type direct-slot)
160 return (gobject-direct-slot-definition-g-property-type direct-slot)))
161 (property-getter (loop
162 for direct-slot in direct-slots
163 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-getter-name direct-slot))
164 return (gobject-fn-direct-slot-definition-g-getter-name direct-slot)))
165 (property-setter (loop
166 for direct-slot in direct-slots
167 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-setter-name direct-slot))
168 return (gobject-fn-direct-slot-definition-g-setter-name direct-slot))))
169 (setf (gobject-effective-slot-definition-g-property-type effective-slot)
170 (gobject-effective-slot-definition-g-property-type effective-slot))
171 (ecase allocation
172 (:gobject-property (assert property-name nil "G-PROPERTY-NAME for slot ~A on class ~A must be specified" name (class-name class))
173 (setf (gobject-property-effective-slot-definition-g-property-name effective-slot)
174 property-name))
175 (:gobject-fn (assert (or property-getter property-setter) nil "At least one of G-PROPERTY-GETTER or G-PROPERTY-SETTER for slot ~A on class ~A must be specified"
176 name (class-name class))
177 (when (or (and property-getter (stringp property-getter))
178 (and property-setter (stringp property-setter)))
179 (assert property-type nil "G-PROPERTY-TYPE for slot ~A on class ~A must be specified because at least one of accessor is specified as a foreign function" name (class-name class)))
181 (setf (gobject-fn-effective-slot-definition-g-getter-name effective-slot) property-getter
182 (gobject-fn-effective-slot-definition-g-setter-name effective-slot) property-setter
183 (gobject-fn-effective-slot-definition-g-getter-fn effective-slot)
184 (and property-getter
185 (if (stringp property-getter)
186 (compile nil (if (foreign-symbol-pointer property-getter)
187 `(lambda (object)
188 (foreign-funcall ,property-getter
189 g-object object
190 ,property-type))
191 `(lambda (object)
192 (declare (ignore object))
193 (error "Property getter ~A is not available" ,property-getter))
195 property-getter))
196 (gobject-fn-effective-slot-definition-g-setter-fn effective-slot)
197 (and property-setter
198 (if (stringp property-setter)
199 (compile nil (if (foreign-symbol-pointer property-setter)
200 `(lambda (object new-value)
201 (foreign-funcall ,property-setter
202 g-object object
203 ,property-type new-value
204 :void))
205 `(lambda (object)
206 (declare (ignore object))
207 (error "Property setter ~A is not avaiable" ,property-setter))))
208 property-setter)))))))
209 effective-slot))
211 (defun create-gobject-from-class-and-initargs (class initargs)
212 (when (gobject-class-interface-p class)
213 (error "Trying to create instance of GInterface '~A' (class '~A')" (gobject-class-g-type-name class) (class-name class)))
214 (let (arg-names arg-values arg-types nc-setters nc-arg-values)
215 (declare (dynamic-extent arg-names arg-values arg-types nc-setters nc-arg-values))
216 (loop
217 for (arg-name arg-value) on initargs by #'cddr
218 for slot = (find arg-name (class-slots class) :key 'slot-definition-initargs :test 'member)
219 when (and slot (typep slot 'gobject-effective-slot-definition))
220 do (typecase slot
221 (gobject-property-effective-slot-definition
222 (push (gobject-property-effective-slot-definition-g-property-name slot) arg-names)
223 (push arg-value arg-values)
224 (push (gobject-effective-slot-definition-g-property-type slot) arg-types))
225 (gobject-fn-effective-slot-definition
226 (push (gobject-fn-effective-slot-definition-g-setter-fn slot) nc-setters)
227 (push arg-value nc-arg-values))))
228 (let ((object (g-object-call-constructor (gobject-class-g-type-name class) arg-names arg-values arg-types)))
229 (loop
230 for fn in nc-setters
231 for value in nc-arg-values
232 do (funcall fn object value))
233 object)))
235 (defun filter-initargs-by-class (class initargs)
236 (iter (with slots = (class-slots class))
237 (for (arg-name arg-value) on initargs by #'cddr)
238 (for slot = (find arg-name slots :key #'slot-definition-initargs :test 'member))
239 (unless (and slot (typep slot 'gobject-effective-slot-definition))
240 (nconcing (list arg-name arg-value)))))
242 (defmethod initialize-instance ((instance g-object) &rest initargs &key &allow-other-keys)
243 (let ((filtered-initargs (filter-initargs-by-class (class-of instance) initargs)))
244 (apply #'call-next-method instance filtered-initargs)))
246 (defmethod make-instance ((class gobject-class) &rest initargs &key pointer)
247 (log-for :subclass "(make-instance ~A ~{~A~^ ~})~%" class initargs)
248 (ensure-finalized class)
249 (let ((*currently-making-object-p* t))
250 (if pointer
251 (progn
252 (assert (= (length initargs) 2) nil "POINTER can not be combined with other initargs (~A)" initargs)
253 (call-next-method))
254 (let* ((default-initargs (iter (for (arg value) in (class-default-initargs class))
255 (nconcing (list arg value))))
256 (effective-initargs (append initargs default-initargs))
257 (pointer (create-gobject-from-class-and-initargs class effective-initargs)))
258 (apply #'call-next-method class :pointer pointer effective-initargs)))))
260 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
261 (handler-case
262 (and (slot-boundp object 'pointer)
263 (pointer object)
264 (progn (g-object-property-type (pointer object) (gobject-property-effective-slot-definition-g-property-name slot) :assert-readable t) t))
265 (property-unreadable-error () nil)))
267 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
268 (g-object-call-get-property (pointer object)
269 (gobject-property-effective-slot-definition-g-property-name slot)
270 (gobject-effective-slot-definition-g-property-type slot)))
272 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-property-effective-slot-definition))
273 (g-object-call-set-property (pointer object)
274 (gobject-property-effective-slot-definition-g-property-name slot)
275 new-value
276 (gobject-effective-slot-definition-g-property-type slot))
277 new-value)
279 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
280 (and (slot-boundp object 'pointer)
281 (pointer object)
282 (not (null (gobject-fn-effective-slot-definition-g-getter-fn slot)))))
284 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
285 (let ((fn (gobject-fn-effective-slot-definition-g-getter-fn slot)))
286 (funcall fn object)))
288 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-fn-effective-slot-definition))
289 (funcall (gobject-fn-effective-slot-definition-g-setter-fn slot) object new-value)
290 new-value)
292 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
293 (slot-boundp object 'pointer))
295 (defmethod slot-makunbound-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
296 (declare (ignore object))
297 nil)