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