chiark / gitweb /
Made iter argument to TREE-MODEL-ITER-N-CHILDREN optional
[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
9b0041d5 23;; $Id: gtkobject.lisp,v 1.39 2006-09-05 13:23:40 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)
9adccb27 32 (init-types-in-library
33 #.(concatenate 'string (pkg-config:pkg-variable "gtk+-2.0" "libdir")
8f205b08 34 "/libgtk-x11-2.0." asdf:*dso-extension*))
f86d391e 35
36 (defclass %object (gobject)
560af5c5 37 ()
560af5c5 38 (:metaclass gobject-class)
dfa4f314 39 (:gtype |gtk_object_get_type|)))
560af5c5 40
41
9adccb27 42(defmethod initialize-instance ((object %object) &rest initargs &key signal)
43 (declare (ignore signal))
560af5c5 44 (call-next-method)
18eed2a0 45 (dolist (signal-definition (get-all initargs :signal))
46 (apply #'signal-connect object signal-definition)))
560af5c5 47
9adccb27 48(defmethod initialize-instance :around ((object %object) &rest initargs)
560af5c5 49 (declare (ignore initargs))
50 (call-next-method)
88d2b3c2 51 ;; Add a temorary reference which will be removed when the object is
52 ;; sinked
34fed32d 53 (funcall (reference-function '%object) (foreign-location object))
18eed2a0 54 (%object-sink object))
560af5c5 55
f86d391e 56(defbinding %object-sink () nil
57 (object %object))
560af5c5 58
73572c12 59;;;; Main loop and event handling
560af5c5 60
73572c12 61(defbinding events-pending-p () boolean)
560af5c5 62
f86d391e 63(defbinding get-current-event () gdk:event)
aace61f5 64
f86d391e 65(defbinding main-do-event () nil
560af5c5 66 (event gdk:event))
67
f86d391e 68(defbinding main () nil)
560af5c5 69
f86d391e 70(defbinding main-level () int)
560af5c5 71
f86d391e 72(defbinding main-quit () nil)
560af5c5 73
f86d391e 74(defbinding main-iteration-do (&optional (blocking t)) boolean
560af5c5 75 (blocking boolean))
76
77(defun main-iterate-all (&rest args)
78 (declare (ignore args))
73572c12 79 (loop
80 while (events-pending-p)
34fed32d 81 do (main-iteration-do nil))
82 #+clisp 0)
560af5c5 83
560af5c5 84
560af5c5 85;;;; Metaclass for child classes
f86d391e 86
87(defvar *container-to-child-class-mappings* (make-hash-table))
560af5c5 88
89(eval-when (:compile-toplevel :load-toplevel :execute)
34fed32d 90 (defclass container-child-class (virtual-slots-class)
1047e159 91 ())
560af5c5 92
f86d391e 93 (defclass direct-child-slot-definition (direct-virtual-slot-definition)
1047e159 94 ((pname :reader slot-definition-pname :initarg :pname)))
560af5c5 95
1047e159 96 (defclass effective-child-slot-definition (effective-virtual-slot-definition)
34fed32d 97 ((pname :reader slot-definition-pname :initarg :pname))))
560af5c5 98
99
34fed32d 100(defmethod shared-initialize ((class container-child-class) names &key container)
101 (declare (ignore names))
560af5c5 102 (call-next-method)
f86d391e 103 (setf
104 (gethash (find-class (first container)) *container-to-child-class-mappings*)
34fed32d 105 class)
106 #+clisp
107 (loop
108 for slotd in (class-direct-slots class)
109 when (typep slotd 'direct-child-slot-definition)
110 do (loop
111 for reader in (slot-definition-readers slotd)
112 do (add-reader-method class
113 (ensure-generic-function reader :lambda-list '(object))
114 (slot-definition-name slotd)))
115 (loop
116 for writer in (slot-definition-writers slotd)
117 do (add-writer-method class
118 (ensure-generic-function writer :lambda-list '(value object))
119 (slot-definition-name slotd)))))
120
121(defmethod direct-slot-definition-class ((class container-child-class) &rest initargs)
560af5c5 122 (case (getf initargs :allocation)
18eed2a0 123 (:property (find-class 'direct-child-slot-definition))
560af5c5 124 (t (call-next-method))))
125
34fed32d 126(defmethod effective-slot-definition-class ((class container-child-class) &rest initargs)
560af5c5 127 (case (getf initargs :allocation)
18eed2a0 128 (:property (find-class 'effective-child-slot-definition))
560af5c5 129 (t (call-next-method))))
560af5c5 130
34fed32d 131(defmethod compute-effective-slot-definition-initargs ((class container-child-class) direct-slotds)
b19bbc94 132 (if (eq (slot-definition-allocation (first direct-slotds)) :property)
1047e159 133 (nconc
134 (list :pname (most-specific-slot-value direct-slotds 'pname))
135 (call-next-method))
136 (call-next-method)))
137
2bd78f93 138(defmethod slot-readable-p ((slotd effective-child-slot-definition))
139 (declare (ignore slotd))
140 t)
141
584285fb 142(defmethod compute-slot-reader-function ((slotd effective-child-slot-definition) &optional signal-unbound-p)
143 (declare (ignore signal-unbound-p))
34fed32d 144 (let* ((type (slot-definition-type slotd))
145 (pname (slot-definition-pname slotd))
146 (reader (reader-function type :ref :get)))
147 #'(lambda (object)
148 (with-slots (parent child) object
149 (with-memory (gvalue +gvalue-size+)
150 (glib::%gvalue-init gvalue (find-type-number type))
151 (%container-child-get-property parent child pname gvalue)
152 (funcall reader gvalue +gvalue-value-offset+))))))
153
2bd78f93 154(defmethod slot-writable-p ((slotd effective-child-slot-definition))
155 (declare (ignore slotd))
156 t)
157
34fed32d 158(defmethod compute-slot-writer-function ((slotd effective-child-slot-definition))
159 (let* ((type (slot-definition-type slotd))
160 (pname (slot-definition-pname slotd))
161 (writer (writer-function type :temp t))
162 (destroy (destroy-function type :temp t)))
163 #'(lambda (value object)
164 (with-slots (parent child) object
165 (with-memory (gvalue +gvalue-size+)
166 (glib::%gvalue-init gvalue (find-type-number type))
167 (funcall writer value gvalue +gvalue-value-offset+)
168 (%container-child-set-property parent child pname gvalue)
169 (funcall destroy gvalue +gvalue-value-offset+))
170 value))))
171
172
173(defmethod add-reader-method ((class container-child-class) generic-function slot-name)
560af5c5 174 (add-method
175 generic-function
176 (make-instance 'standard-method
c289d084 177 :specializers (list (find-class 'widget))
178 :lambda-list '(widget)
179 :function #'(lambda (args next-methods)
180 (declare (ignore next-methods))
181 (child-property-value (first args) slot-name)))))
560af5c5 182
34fed32d 183(defmethod add-writer-method ((class container-child-class) generic-function slot-name)
560af5c5 184 (add-method
185 generic-function
186 (make-instance 'standard-method
c289d084 187 :specializers (list (find-class t) (find-class 'widget))
188 :lambda-list '(value widget)
189 :function #'(lambda (args next-methods)
190 (declare (ignore next-methods))
191 (destructuring-bind (value widget) args
192 (setf (child-property-value widget slot-name) value))))))
560af5c5 193
194
34fed32d 195(defmethod validate-superclass ((class container-child-class) (super standard-class))
18eed2a0 196 ;(subtypep (class-name super) 'container-child)
197 t)
560af5c5 198
199
eb69c5e7 200(defclass container-child (virtual-slots-object)
f86d391e 201 ((parent :initarg :parent :type container)
202 (child :initarg :child :type widget)))
203
204
205;;;;
206
18eed2a0 207(defbinding %container-class-list-child-properties () pointer
208 (class pointer)
209 (n-properties unsigned-int :out))
f86d391e 210
18eed2a0 211(defun query-container-class-child-properties (type-number)
212 (let ((class (type-class-ref type-number)))
213 (multiple-value-bind (array length)
214 (%container-class-list-child-properties class)
215 (unwind-protect
9adccb27 216 (map-c-vector 'list #'identity array 'param length)
18eed2a0 217 (deallocate-memory array)))))
f86d391e 218
219(defun default-container-child-name (container-class)
220 (intern (format nil "~A-CHILD" container-class)))
221
62f12808 222(defun expand-container-type (type forward-p options)
1047e159 223 (let* ((class (type-from-number type))
224 (super (supertype type))
225 (child-class (default-container-child-name class)))
62f12808 226 (if forward-p
227 (expand-gobject-type type t options)
228 `(progn
229 ,(expand-gobject-type type nil options)
230 (defclass ,child-class (,(default-container-child-name super))
231 ,(slot-definitions child-class
232 (query-container-class-child-properties type) nil)
34fed32d 233 (:metaclass container-child-class)
62f12808 234 (:container ,class))))))
235
9b0041d5 236(defun container-child-class (container-class)
8f205b08 237 (gethash container-class *container-to-child-class-mappings*))
62f12808 238
239(register-derivable-type 'container "GtkContainer" 'expand-container-type 'gobject-dependencies)