chiark / gitweb /
Removed the need for dummy container-child classes.
[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
5bf6c64b 23;; $Id: gtkobject.lisp,v 1.47 2008-03-06 22:02:08 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)
8f12a0ff 32 (init-types-in-library gtk "libgtk-2.0")
f86d391e 33
34 (defclass %object (gobject)
560af5c5 35 ()
560af5c5 36 (:metaclass gobject-class)
dfa4f314 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
9adccb27 46(defmethod initialize-instance :around ((object %object) &rest initargs)
560af5c5 47 (declare (ignore initargs))
48 (call-next-method)
88d2b3c2 49 ;; Add a temorary reference which will be removed when the object is
50 ;; sinked
34fed32d 51 (funcall (reference-function '%object) (foreign-location object))
18eed2a0 52 (%object-sink object))
560af5c5 53
f86d391e 54(defbinding %object-sink () nil
55 (object %object))
560af5c5 56
73572c12 57;;;; Main loop and event handling
560af5c5 58
3e4f39ae 59(defparameter *reentrant-main-iteration* t)
60(defvar *running-main-iteration* nil)
61
73572c12 62(defbinding events-pending-p () boolean)
560af5c5 63
f86d391e 64(defbinding get-current-event () gdk:event)
aace61f5 65
f86d391e 66(defbinding main-do-event () nil
560af5c5 67 (event gdk:event))
68
f86d391e 69(defbinding main () nil)
560af5c5 70
f86d391e 71(defbinding main-level () int)
560af5c5 72
f86d391e 73(defbinding main-quit () nil)
560af5c5 74
f86d391e 75(defbinding main-iteration-do (&optional (blocking t)) boolean
560af5c5 76 (blocking boolean))
77
78(defun main-iterate-all (&rest args)
79 (declare (ignore args))
3e4f39ae 80 (unless (and (not *reentrant-main-iteration*) *running-main-iteration*)
81 (let ((*running-main-iteration* t))
82 (loop
83 while (events-pending-p)
84 do (main-iteration-do nil))))
34fed32d 85 #+clisp 0)
560af5c5 86
560af5c5 87
c3721a64 88(define-callback fd-source-callback-marshal nil
89 ((callback-id unsigned-int) (fd unsigned-int))
90 (glib::invoke-source-callback callback-id fd))
91
92(defbinding (input-add "gtk_input_add_full") (fd condition function) unsigned-int
93 (fd unsigned-int)
94 (condition gdk:input-condition)
95 (fd-source-callback-marshal callback)
96 (nil null)
97 ((register-callback-function function) unsigned-long)
98 (user-data-destroy-callback callback))
99
100
560af5c5 101;;;; Metaclass for child classes
f86d391e 102
103(defvar *container-to-child-class-mappings* (make-hash-table))
560af5c5 104
105(eval-when (:compile-toplevel :load-toplevel :execute)
34fed32d 106 (defclass container-child-class (virtual-slots-class)
1047e159 107 ())
560af5c5 108
f86d391e 109 (defclass direct-child-slot-definition (direct-virtual-slot-definition)
1047e159 110 ((pname :reader slot-definition-pname :initarg :pname)))
560af5c5 111
1047e159 112 (defclass effective-child-slot-definition (effective-virtual-slot-definition)
34fed32d 113 ((pname :reader slot-definition-pname :initarg :pname))))
560af5c5 114
115
34fed32d 116(defmethod shared-initialize ((class container-child-class) names &key container)
117 (declare (ignore names))
560af5c5 118 (call-next-method)
f86d391e 119 (setf
120 (gethash (find-class (first container)) *container-to-child-class-mappings*)
34fed32d 121 class)
122 #+clisp
123 (loop
124 for slotd in (class-direct-slots class)
125 when (typep slotd 'direct-child-slot-definition)
126 do (loop
127 for reader in (slot-definition-readers slotd)
128 do (add-reader-method class
129 (ensure-generic-function reader :lambda-list '(object))
130 (slot-definition-name slotd)))
131 (loop
132 for writer in (slot-definition-writers slotd)
133 do (add-writer-method class
134 (ensure-generic-function writer :lambda-list '(value object))
135 (slot-definition-name slotd)))))
136
137(defmethod direct-slot-definition-class ((class container-child-class) &rest initargs)
560af5c5 138 (case (getf initargs :allocation)
18eed2a0 139 (:property (find-class 'direct-child-slot-definition))
560af5c5 140 (t (call-next-method))))
141
34fed32d 142(defmethod effective-slot-definition-class ((class container-child-class) &rest initargs)
560af5c5 143 (case (getf initargs :allocation)
18eed2a0 144 (:property (find-class 'effective-child-slot-definition))
560af5c5 145 (t (call-next-method))))
560af5c5 146
34fed32d 147(defmethod compute-effective-slot-definition-initargs ((class container-child-class) direct-slotds)
b19bbc94 148 (if (eq (slot-definition-allocation (first direct-slotds)) :property)
1047e159 149 (nconc
150 (list :pname (most-specific-slot-value direct-slotds 'pname))
151 (call-next-method))
152 (call-next-method)))
153
2bd78f93 154(defmethod slot-readable-p ((slotd effective-child-slot-definition))
155 (declare (ignore slotd))
156 t)
157
584285fb 158(defmethod compute-slot-reader-function ((slotd effective-child-slot-definition) &optional signal-unbound-p)
159 (declare (ignore signal-unbound-p))
34fed32d 160 (let* ((type (slot-definition-type slotd))
161 (pname (slot-definition-pname slotd))
162 (reader (reader-function type :ref :get)))
163 #'(lambda (object)
164 (with-slots (parent child) object
165 (with-memory (gvalue +gvalue-size+)
166 (glib::%gvalue-init gvalue (find-type-number type))
167 (%container-child-get-property parent child pname gvalue)
168 (funcall reader gvalue +gvalue-value-offset+))))))
169
2bd78f93 170(defmethod slot-writable-p ((slotd effective-child-slot-definition))
171 (declare (ignore slotd))
172 t)
173
34fed32d 174(defmethod compute-slot-writer-function ((slotd effective-child-slot-definition))
175 (let* ((type (slot-definition-type slotd))
176 (pname (slot-definition-pname slotd))
177 (writer (writer-function type :temp t))
178 (destroy (destroy-function type :temp t)))
179 #'(lambda (value object)
180 (with-slots (parent child) object
181 (with-memory (gvalue +gvalue-size+)
182 (glib::%gvalue-init gvalue (find-type-number type))
183 (funcall writer value gvalue +gvalue-value-offset+)
184 (%container-child-set-property parent child pname gvalue)
185 (funcall destroy gvalue +gvalue-value-offset+))
186 value))))
187
188
b4f60db7 189(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 190 (add-method
191 generic-function
192 (make-instance 'standard-method
c289d084 193 :specializers (list (find-class 'widget))
194 :lambda-list '(widget)
fb370076 195 :documentation (or #?(sbcl>= 1 0 2)slot-documentation "automatically generated reader method")
c289d084 196 :function #'(lambda (args next-methods)
197 (declare (ignore next-methods))
198 (child-property-value (first args) slot-name)))))
560af5c5 199
b4f60db7 200(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 201 (add-method
202 generic-function
203 (make-instance 'standard-method
c289d084 204 :specializers (list (find-class t) (find-class 'widget))
205 :lambda-list '(value widget)
fb370076 206 :documentation (or #?(sbcl>= 1 0 2)slot-documentation "automatically generated reader method")
c289d084 207 :function #'(lambda (args next-methods)
208 (declare (ignore next-methods))
209 (destructuring-bind (value widget) args
210 (setf (child-property-value widget slot-name) value))))))
560af5c5 211
212
34fed32d 213(defmethod validate-superclass ((class container-child-class) (super standard-class))
18eed2a0 214 ;(subtypep (class-name super) 'container-child)
215 t)
560af5c5 216
217
eb69c5e7 218(defclass container-child (virtual-slots-object)
f86d391e 219 ((parent :initarg :parent :type container)
220 (child :initarg :child :type widget)))
221
222
223;;;;
224
18eed2a0 225(defbinding %container-class-list-child-properties () pointer
226 (class pointer)
227 (n-properties unsigned-int :out))
f86d391e 228
5bf6c64b 229(defun query-container-class-child-properties (type-number &optional owner-only-p)
18eed2a0 230 (let ((class (type-class-ref type-number)))
231 (multiple-value-bind (array length)
232 (%container-class-list-child-properties class)
233 (unwind-protect
5bf6c64b 234 (let ((properties (map-c-vector 'list #'identity array 'param length)))
235 (if owner-only-p
236 (delete-if
237 #'(lambda (property)
238 (not (eql (slot-value property 'glib::owner-type) type-number)))
239 properties)
240 properties))
18eed2a0 241 (deallocate-memory array)))))
f86d391e 242
243(defun default-container-child-name (container-class)
244 (intern (format nil "~A-CHILD" container-class)))
245
62f12808 246(defun expand-container-type (type forward-p options)
1047e159 247 (let* ((class (type-from-number type))
248 (super (supertype type))
249 (child-class (default-container-child-name class)))
62f12808 250 (if forward-p
251 (expand-gobject-type type t options)
252 `(progn
253 ,(expand-gobject-type type nil options)
5bf6c64b 254 ,(let ((child-properties (query-container-class-child-properties type t)))
255 (when child-properties
256 `(defclass ,child-class (,(default-container-child-name super))
257 ,(slot-definitions child-class child-properties nil)
258 (:metaclass container-child-class)
259 (:container ,class))))))))
62f12808 260
9b0041d5 261(defun container-child-class (container-class)
8f205b08 262 (gethash container-class *container-to-child-class-mappings*))
62f12808 263
7ecf52b3 264(defun container-dependencies (type options)
265 (delete-duplicates
266 (append
267 (gobject-dependencies type options)
268 (mapcar #'param-value-type (query-container-class-child-properties type)))))
269
270(register-derivable-type 'container "GtkContainer" 'expand-container-type 'container-dependencies)
ddd14ce8 271
272
273(defmacro define-callback-setter (name arg return-type &rest rest-args)
274 (let ((callback (gensym)))
275 (if arg
276 `(progn
277 (define-callback-marshal ,callback ,return-type
278 ,(cons arg rest-args))
279 (defbinding ,name () nil
280 ,arg
281 (,callback callback)
282 (function user-callback)
283 (user-data-destroy-callback callback)))
284 `(progn
285 (define-callback-marshal ,callback ,return-type ,rest-args)
286 (defbinding ,name () nil
287 (,callback callback)
288 (function user-callback)
289 (user-data-destroy-callback callback))))))
290