chiark / gitweb /
Add a boundp-function slot, which is required by virtual slot getter.
[clg] / gtk / gtkobject.lisp
CommitLineData
112ac1d3 1;; Common Lisp bindings for GTK+ v2.x
2;; Copyright 1999-2005 Espen S. Johnsen <espen@users.sf.net>
560af5c5 3;;
112ac1d3 4;; Permission is hereby granted, free of charge, to any person obtaining
5;; a copy of this software and associated documentation files (the
6;; "Software"), to deal in the Software without restriction, including
7;; without limitation the rights to use, copy, modify, merge, publish,
8;; distribute, sublicense, and/or sell copies of the Software, and to
9;; permit persons to whom the Software is furnished to do so, subject to
10;; the following conditions:
560af5c5 11;;
112ac1d3 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
560af5c5 14;;
112ac1d3 15;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
560af5c5 22
2ce62a29 23;; $Id: gtkobject.lisp,v 1.49 2008-10-09 18:45:01 espen Exp $
560af5c5 24
25
26(in-package "GTK")
27
8bf63d9f 28
560af5c5 29;;;; Superclass for the gtk class hierarchy
30
31(eval-when (:compile-toplevel :load-toplevel :execute)
44ad7044 32 (init-types-in-library gtk "libgtk-2.0"))
f86d391e 33
44ad7044 34(defclass %object (initially-unowned)
35 ()
36 (:metaclass gobject-class)
37 (:gtype |gtk_object_get_type|))
560af5c5 38
39
9adccb27 40(defmethod initialize-instance ((object %object) &rest initargs &key signal)
41 (declare (ignore signal))
560af5c5 42 (call-next-method)
18eed2a0 43 (dolist (signal-definition (get-all initargs :signal))
44 (apply #'signal-connect object signal-definition)))
560af5c5 45
44ad7044 46#?-(pkg-exists-p "glib-2.0" :atleast-version "2.10.0")
47(progn
48 (defmethod initialize-instance :around ((object %object) &rest initargs)
49 (declare (ignore initargs))
50 (call-next-method)
51 ;; Add a temorary reference which will be removed when the object is
52 ;; sinked
53 (funcall (reference-function '%object) (foreign-location object))
54 (%object-sink object))
55
56 (defbinding %object-sink () nil
57 (object %object)))
560af5c5 58
73572c12 59;;;; Main loop and event handling
560af5c5 60
3e4f39ae 61(defparameter *reentrant-main-iteration* t)
62(defvar *running-main-iteration* nil)
63
73572c12 64(defbinding events-pending-p () boolean)
560af5c5 65
f86d391e 66(defbinding get-current-event () gdk:event)
aace61f5 67
f86d391e 68(defbinding main-do-event () nil
560af5c5 69 (event gdk:event))
70
f86d391e 71(defbinding main () nil)
560af5c5 72
f86d391e 73(defbinding main-level () int)
560af5c5 74
f86d391e 75(defbinding main-quit () nil)
560af5c5 76
f86d391e 77(defbinding main-iteration-do (&optional (blocking t)) boolean
560af5c5 78 (blocking boolean))
79
80(defun main-iterate-all (&rest args)
81 (declare (ignore args))
3e4f39ae 82 (unless (and (not *reentrant-main-iteration*) *running-main-iteration*)
83 (let ((*running-main-iteration* t))
84 (loop
85 while (events-pending-p)
86 do (main-iteration-do nil))))
34fed32d 87 #+clisp 0)
560af5c5 88
560af5c5 89
c3721a64 90(define-callback fd-source-callback-marshal nil
91 ((callback-id unsigned-int) (fd unsigned-int))
92 (glib::invoke-source-callback callback-id fd))
93
94(defbinding (input-add "gtk_input_add_full") (fd condition function) unsigned-int
95 (fd unsigned-int)
96 (condition gdk:input-condition)
97 (fd-source-callback-marshal callback)
98 (nil null)
99 ((register-callback-function function) unsigned-long)
100 (user-data-destroy-callback callback))
101
102
560af5c5 103;;;; Metaclass for child classes
f86d391e 104
105(defvar *container-to-child-class-mappings* (make-hash-table))
560af5c5 106
107(eval-when (:compile-toplevel :load-toplevel :execute)
34fed32d 108 (defclass container-child-class (virtual-slots-class)
1047e159 109 ())
560af5c5 110
f86d391e 111 (defclass direct-child-slot-definition (direct-virtual-slot-definition)
1047e159 112 ((pname :reader slot-definition-pname :initarg :pname)))
560af5c5 113
1047e159 114 (defclass effective-child-slot-definition (effective-virtual-slot-definition)
34fed32d 115 ((pname :reader slot-definition-pname :initarg :pname))))
560af5c5 116
117
34fed32d 118(defmethod shared-initialize ((class container-child-class) names &key container)
119 (declare (ignore names))
560af5c5 120 (call-next-method)
f86d391e 121 (setf
122 (gethash (find-class (first container)) *container-to-child-class-mappings*)
34fed32d 123 class)
124 #+clisp
125 (loop
126 for slotd in (class-direct-slots class)
127 when (typep slotd 'direct-child-slot-definition)
128 do (loop
129 for reader in (slot-definition-readers slotd)
130 do (add-reader-method class
131 (ensure-generic-function reader :lambda-list '(object))
132 (slot-definition-name slotd)))
133 (loop
134 for writer in (slot-definition-writers slotd)
135 do (add-writer-method class
136 (ensure-generic-function writer :lambda-list '(value object))
137 (slot-definition-name slotd)))))
138
139(defmethod direct-slot-definition-class ((class container-child-class) &rest initargs)
560af5c5 140 (case (getf initargs :allocation)
18eed2a0 141 (:property (find-class 'direct-child-slot-definition))
560af5c5 142 (t (call-next-method))))
143
34fed32d 144(defmethod effective-slot-definition-class ((class container-child-class) &rest initargs)
560af5c5 145 (case (getf initargs :allocation)
18eed2a0 146 (:property (find-class 'effective-child-slot-definition))
560af5c5 147 (t (call-next-method))))
560af5c5 148
34fed32d 149(defmethod compute-effective-slot-definition-initargs ((class container-child-class) direct-slotds)
b19bbc94 150 (if (eq (slot-definition-allocation (first direct-slotds)) :property)
1047e159 151 (nconc
152 (list :pname (most-specific-slot-value direct-slotds 'pname))
153 (call-next-method))
154 (call-next-method)))
155
2bd78f93 156(defmethod slot-readable-p ((slotd effective-child-slot-definition))
157 (declare (ignore slotd))
158 t)
159
584285fb 160(defmethod compute-slot-reader-function ((slotd effective-child-slot-definition) &optional signal-unbound-p)
161 (declare (ignore signal-unbound-p))
34fed32d 162 (let* ((type (slot-definition-type slotd))
163 (pname (slot-definition-pname slotd))
164 (reader (reader-function type :ref :get)))
165 #'(lambda (object)
166 (with-slots (parent child) object
167 (with-memory (gvalue +gvalue-size+)
168 (glib::%gvalue-init gvalue (find-type-number type))
169 (%container-child-get-property parent child pname gvalue)
170 (funcall reader gvalue +gvalue-value-offset+))))))
171
2bd78f93 172(defmethod slot-writable-p ((slotd effective-child-slot-definition))
173 (declare (ignore slotd))
174 t)
175
34fed32d 176(defmethod compute-slot-writer-function ((slotd effective-child-slot-definition))
177 (let* ((type (slot-definition-type slotd))
178 (pname (slot-definition-pname slotd))
179 (writer (writer-function type :temp t))
180 (destroy (destroy-function type :temp t)))
181 #'(lambda (value object)
182 (with-slots (parent child) object
183 (with-memory (gvalue +gvalue-size+)
184 (glib::%gvalue-init gvalue (find-type-number type))
185 (funcall writer value gvalue +gvalue-value-offset+)
186 (%container-child-set-property parent child pname gvalue)
187 (funcall destroy gvalue +gvalue-value-offset+))
188 value))))
189
190
b4f60db7 191(defmethod add-reader-method ((class container-child-class) generic-function slot-name #?(sbcl>= 1 0 2)slot-documentation #?(sbcl>= 1 0 14)source-location)
560af5c5 192 (add-method
193 generic-function
194 (make-instance 'standard-method
c289d084 195 :specializers (list (find-class 'widget))
196 :lambda-list '(widget)
fb370076 197 :documentation (or #?(sbcl>= 1 0 2)slot-documentation "automatically generated reader method")
c289d084 198 :function #'(lambda (args next-methods)
199 (declare (ignore next-methods))
200 (child-property-value (first args) slot-name)))))
560af5c5 201
b4f60db7 202(defmethod add-writer-method ((class container-child-class) generic-function slot-name #?(sbcl>= 1 0 2)slot-documentation #?(sbcl>= 1 0 14)source-location)
560af5c5 203 (add-method
204 generic-function
205 (make-instance 'standard-method
c289d084 206 :specializers (list (find-class t) (find-class 'widget))
207 :lambda-list '(value widget)
fb370076 208 :documentation (or #?(sbcl>= 1 0 2)slot-documentation "automatically generated reader method")
c289d084 209 :function #'(lambda (args next-methods)
210 (declare (ignore next-methods))
211 (destructuring-bind (value widget) args
212 (setf (child-property-value widget slot-name) value))))))
560af5c5 213
214
34fed32d 215(defmethod validate-superclass ((class container-child-class) (super standard-class))
18eed2a0 216 ;(subtypep (class-name super) 'container-child)
217 t)
560af5c5 218
219
eb69c5e7 220(defclass container-child (virtual-slots-object)
f86d391e 221 ((parent :initarg :parent :type container)
222 (child :initarg :child :type widget)))
223
224
225;;;;
226
18eed2a0 227(defbinding %container-class-list-child-properties () pointer
228 (class pointer)
229 (n-properties unsigned-int :out))
f86d391e 230
5bf6c64b 231(defun query-container-class-child-properties (type-number &optional owner-only-p)
18eed2a0 232 (let ((class (type-class-ref type-number)))
233 (multiple-value-bind (array length)
234 (%container-class-list-child-properties class)
235 (unwind-protect
5bf6c64b 236 (let ((properties (map-c-vector 'list #'identity array 'param length)))
237 (if owner-only-p
238 (delete-if
239 #'(lambda (property)
240 (not (eql (slot-value property 'glib::owner-type) type-number)))
241 properties)
242 properties))
18eed2a0 243 (deallocate-memory array)))))
f86d391e 244
245(defun default-container-child-name (container-class)
246 (intern (format nil "~A-CHILD" container-class)))
247
62f12808 248(defun expand-container-type (type forward-p options)
1047e159 249 (let* ((class (type-from-number type))
250 (super (supertype type))
251 (child-class (default-container-child-name class)))
62f12808 252 (if forward-p
253 (expand-gobject-type type t options)
254 `(progn
255 ,(expand-gobject-type type nil options)
5bf6c64b 256 ,(let ((child-properties (query-container-class-child-properties type t)))
257 (when child-properties
258 `(defclass ,child-class (,(default-container-child-name super))
259 ,(slot-definitions child-class child-properties nil)
260 (:metaclass container-child-class)
261 (:container ,class))))))))
62f12808 262
7ecf52b3 263(defun container-dependencies (type options)
264 (delete-duplicates
265 (append
266 (gobject-dependencies type options)
267 (mapcar #'param-value-type (query-container-class-child-properties type)))))
268
269(register-derivable-type 'container "GtkContainer" 'expand-container-type 'container-dependencies)
ddd14ce8 270
271
272(defmacro define-callback-setter (name arg return-type &rest rest-args)
273 (let ((callback (gensym)))
274 (if arg
275 `(progn
276 (define-callback-marshal ,callback ,return-type
277 ,(cons arg rest-args))
278 (defbinding ,name () nil
279 ,arg
280 (,callback callback)
281 (function user-callback)
282 (user-data-destroy-callback callback)))
283 `(progn
284 (define-callback-marshal ,callback ,return-type ,rest-args)
285 (defbinding ,name () nil
286 (,callback callback)
287 (function user-callback)
288 (user-data-destroy-callback callback))))))
289