fix for clozure: do not use (non-standard) make-instance method for STRUCTURE-CLASSes
[cl-gtk2.git] / glib / gobject.meta.lisp
1 (in-package :gobject)
2
3 (defclass gobject-class (standard-class)
4   ((g-type-name :initform (error "G-TYPE-NAME must be specified")
5                 :initarg :g-type-name
6                 :reader gobject-class-g-type-name)
7    (g-type-initializer :initform nil
8                        :initarg :g-type-initializer
9                        :reader gobject-class-g-type-initializer)
10    (interface-p :initform nil
11                 :initarg :g-interface-p
12                 :reader gobject-class-interface-p))
13   (:documentation "Metaclass for GObject-based classes."))
14
15 (defun initialize-gobject-class-g-type (class)
16   (if (gobject-class-g-type-initializer class)
17       (let* ((initializer-fn-ptr (foreign-symbol-pointer (gobject-class-g-type-initializer class)))
18              (type (when initializer-fn-ptr
19                      (foreign-funcall-pointer initializer-fn-ptr nil
20                                               g-type))))
21         (if (null initializer-fn-ptr)
22             (warn "Type initializer for class '~A' (GType '~A') is invalid: foreign symbol '~A'"
23                   (gobject-class-g-type-name class) (class-name class) (gobject-class-g-type-initializer class))
24             (progn
25               (when (g-type= +g-type-invalid+ type)
26                 (warn "Declared GType name '~A' for class '~A' is invalid ('~A' returned 0)"
27                       (gobject-class-g-type-name class) (class-name class)
28                       (gobject-class-g-type-initializer class)))
29               (unless (g-type= (gobject-class-g-type-name class) type)
30                 (warn "Declared GType name '~A' for class '~A' does not match actual GType name '~A'"
31                       (gobject-class-g-type-name class)
32                       (class-name class)
33                       (g-type-name type))))))
34       (unless (g-type-from-name (gobject-class-g-type-name class))
35         (warn "Declared GType name '~A' for class '~A' is invalid (g_type_name returned 0)"
36               (gobject-class-g-type-name class) (class-name class)))))
37
38 (defmethod initialize-instance :after ((object gobject-class) &key &allow-other-keys)
39   (register-object-type (gobject-class-g-type-name object) (class-name object))
40   (at-init (object) (initialize-gobject-class-g-type object)))
41
42 (defclass gobject-direct-slot-definition (standard-direct-slot-definition)
43   ((g-property-type :initform nil
44                     :initarg :g-property-type
45                     :reader gobject-direct-slot-definition-g-property-type)))
46
47 (defclass gobject-effective-slot-definition (standard-effective-slot-definition)
48   ((g-property-type :initform nil
49                     :initarg :g-property-type
50                     :accessor gobject-effective-slot-definition-g-property-type)))
51
52 (defclass gobject-property-direct-slot-definition (gobject-direct-slot-definition)
53   ((g-property-name :initform nil
54                     :initarg :g-property-name
55                     :reader gobject-property-direct-slot-definition-g-property-name)))
56
57 (defclass gobject-property-effective-slot-definition (gobject-effective-slot-definition)
58   ((g-property-name :initform nil
59                     :initarg :g-property-name
60                     :accessor gobject-property-effective-slot-definition-g-property-name)))
61
62 (defclass gobject-fn-direct-slot-definition (gobject-direct-slot-definition)
63   ((g-getter-name :initform nil
64                   :initarg :g-getter
65                   :reader gobject-fn-direct-slot-definition-g-getter-name)
66    (g-setter-name :initform nil
67                   :initarg :g-setter
68                   :reader gobject-fn-direct-slot-definition-g-setter-name)))
69
70 (defclass gobject-fn-effective-slot-definition (gobject-effective-slot-definition)
71   ((g-getter-name :initform nil
72                   :initarg :g-getter
73                   :accessor gobject-fn-effective-slot-definition-g-getter-name)
74    (g-setter-name :initform nil
75                   :initarg :g-setter
76                   :accessor gobject-fn-effective-slot-definition-g-setter-name)
77    (g-getter-fn :initform nil
78                 :accessor gobject-fn-effective-slot-definition-g-getter-fn)
79    (g-setter-fn :initform nil
80                 :accessor gobject-fn-effective-slot-definition-g-setter-fn)))
81
82 (defmethod validate-superclass ((class gobject-class) (superclass standard-class))
83   t)
84
85 (defmethod validate-superclass ((class standard-class) (superclass gobject-class))
86   t)
87
88 (defmethod compute-class-precedence-list ((class gobject-class))
89   (let ((classes (call-next-method)))
90     (if (member (find-class 'g-object) classes)
91         classes
92         `(,class ,(find-class 'g-object) ,@(rest classes)))))
93
94 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
95   (declare (ignore initargs))
96   (case allocation
97     (:gobject-property 'gobject-property-direct-slot-definition)
98     (:gobject-fn 'gobject-fn-direct-slot-definition)
99     (otherwise (call-next-method))))
100
101 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
102   (declare (ignore initargs))
103   (case allocation
104     (:gobject-property 'gobject-property-effective-slot-definition)
105     (:gobject-fn 'gobject-fn-effective-slot-definition)
106     (otherwise (call-next-method))))
107
108 (defmethod compute-effective-slot-definition ((class gobject-class) name direct-slots)
109   (let ((effective-slot (call-next-method)))
110     (when (typep effective-slot 'gobject-effective-slot-definition)
111       (let ((allocation (loop
112                               for direct-slot in direct-slots
113                               when (slot-definition-allocation direct-slot)
114                               return (slot-definition-allocation direct-slot)))
115             (property-name (loop
116                               for direct-slot in direct-slots
117                               when (and (typep direct-slot 'gobject-property-direct-slot-definition) (gobject-property-direct-slot-definition-g-property-name direct-slot))
118                               return (gobject-property-direct-slot-definition-g-property-name direct-slot)))
119             (property-type (loop
120                               for direct-slot in direct-slots
121                               when (gobject-direct-slot-definition-g-property-type direct-slot)
122                               return (gobject-direct-slot-definition-g-property-type direct-slot)))
123             (property-getter (loop
124                               for direct-slot in direct-slots
125                               when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-getter-name direct-slot))
126                               return (gobject-fn-direct-slot-definition-g-getter-name direct-slot)))
127             (property-setter (loop
128                               for direct-slot in direct-slots
129                               when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-setter-name direct-slot))
130                               return (gobject-fn-direct-slot-definition-g-setter-name direct-slot))))
131         (setf (gobject-effective-slot-definition-g-property-type effective-slot)
132               (gobject-effective-slot-definition-g-property-type effective-slot))
133         (ecase allocation
134           (:gobject-property (assert property-name nil "G-PROPERTY-NAME for slot ~A on class ~A must be specified" name (class-name class))
135                              (setf (gobject-property-effective-slot-definition-g-property-name effective-slot)
136                                    property-name))
137           (: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"
138                                name (class-name class))
139                        (when (or (and property-getter (stringp property-getter))
140                                  (and property-setter (stringp property-setter)))
141                         (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)))
142                        
143                        (setf (gobject-fn-effective-slot-definition-g-getter-name effective-slot) property-getter
144                              (gobject-fn-effective-slot-definition-g-setter-name effective-slot) property-setter
145                              (gobject-fn-effective-slot-definition-g-getter-fn effective-slot)
146                              (and property-getter
147                                   (if (stringp property-getter)
148                                       (compile nil `(lambda (object)
149                                                       (foreign-funcall ,property-getter
150                                                                        g-object object
151                                                                        ,property-type)))
152                                       property-getter))
153                              (gobject-fn-effective-slot-definition-g-setter-fn effective-slot)
154                              (and property-setter
155                                   (if (stringp property-setter)
156                                       (compile nil `(lambda (object new-value)
157                                                       (foreign-funcall ,property-setter
158                                                                        g-object object
159                                                                        ,property-type new-value
160                                                                        :void)))
161                                       property-setter)))))))
162     effective-slot))
163
164 (defun create-gobject-from-class-and-initargs (class initargs)
165   (when (gobject-class-interface-p class)
166     (error "Trying to create instance of GInterface '~A' (class '~A')" (gobject-class-g-type-name class) (class-name class)))
167   (let (arg-names arg-values arg-types nc-setters nc-arg-values)
168     (declare (dynamic-extent arg-names arg-values arg-types nc-setters nc-arg-values))
169     (loop
170        for (arg-name arg-value) on initargs by #'cddr
171        for slot = (find arg-name (class-slots class) :key 'slot-definition-initargs :test 'member)
172        when (and slot (typep slot 'gobject-effective-slot-definition))
173        do (typecase slot
174             (gobject-property-effective-slot-definition
175              (push (gobject-property-effective-slot-definition-g-property-name slot) arg-names)
176              (push arg-value arg-values)
177              (push (gobject-effective-slot-definition-g-property-type slot) arg-types))
178             (gobject-fn-effective-slot-definition
179              (push (gobject-fn-effective-slot-definition-g-setter-fn slot) nc-setters)
180              (push arg-value nc-arg-values))))
181     (let ((object (g-object-call-constructor (gobject-class-g-type-name class) arg-names arg-values arg-types)))
182       (loop
183          for fn in nc-setters
184          for value in nc-arg-values
185          do (funcall fn object value))
186       object)))
187
188 (defun filter-initargs-by-class (class initargs)
189   (iter (with slots = (class-slots class))
190         (for (arg-name arg-value) on initargs by #'cddr)
191         (for slot = (find arg-name slots :key #'slot-definition-initargs :test 'member))
192         (unless (and slot (typep slot 'gobject-effective-slot-definition))
193           (nconcing (list arg-name arg-value)))))
194
195 (defmethod initialize-instance ((instance g-object) &rest initargs &key &allow-other-keys)
196   (let ((filtered-initargs (filter-initargs-by-class (class-of instance) initargs)))
197     (apply #'call-next-method instance filtered-initargs)))
198
199 (defmethod make-instance ((class gobject-class) &rest initargs &key pointer)
200   (if pointer
201       (progn
202         (assert (= (length initargs) 2) nil "POINTER can not be combined with other initargs (~A)" initargs)
203         (call-next-method))
204       (let ((pointer (create-gobject-from-class-and-initargs class initargs)))
205         (apply #'call-next-method class :pointer pointer initargs))))
206
207 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
208   (handler-case
209       (progn (g-object-property-type (pointer object) (gobject-property-effective-slot-definition-g-property-name slot) :assert-readable t) t)
210     (property-unreadable-error () nil)))
211
212 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
213   (g-object-call-get-property (pointer object)
214                               (gobject-property-effective-slot-definition-g-property-name slot)
215                               (gobject-effective-slot-definition-g-property-type slot)))
216
217 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-property-effective-slot-definition))
218   (g-object-call-set-property (pointer object)
219                               (gobject-property-effective-slot-definition-g-property-name slot)
220                               new-value
221                               (gobject-effective-slot-definition-g-property-type slot)))
222
223 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
224   (not (null (gobject-fn-effective-slot-definition-g-getter-fn slot))))
225
226 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
227   (let ((fn (gobject-fn-effective-slot-definition-g-getter-fn slot)))
228     (funcall fn object)))
229
230 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-fn-effective-slot-definition))
231   (funcall (gobject-fn-effective-slot-definition-g-setter-fn slot) object new-value))
232
233 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
234   t)
235
236 (defmethod slot-makunbound-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
237   nil)