f5376351785447fd37379af2fdbd59d57b7f71fc
[clg] / gtk / gtkobject.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 1999-2005 Espen S. Johnsen <espen@users.sf.net>
3 ;;
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:
11 ;;
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
14 ;;
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.
22
23 ;; $Id: gtkobject.lisp,v 1.42 2007-06-06 10:43:54 espen Exp $
24
25
26 (in-package "GTK")
27
28
29 ;;;; Superclass for the gtk class hierarchy
30
31 (eval-when (:compile-toplevel :load-toplevel :execute)
32 (init-types-in-library gtk "libgtk-2.0")
33
34 (defclass %object (gobject)
35 ()
36 (:metaclass gobject-class)
37 (:gtype |gtk_object_get_type|)))
38
39
40 (defmethod initialize-instance ((object %object) &rest initargs &key signal)
41 (declare (ignore signal))
42 (call-next-method)
43 (dolist (signal-definition (get-all initargs :signal))
44 (apply #'signal-connect object signal-definition)))
45
46 (defmethod initialize-instance :around ((object %object) &rest initargs)
47 (declare (ignore initargs))
48 (call-next-method)
49 ;; Add a temorary reference which will be removed when the object is
50 ;; sinked
51 (funcall (reference-function '%object) (foreign-location object))
52 (%object-sink object))
53
54 (defbinding %object-sink () nil
55 (object %object))
56
57 ;;;; Main loop and event handling
58
59 (defbinding events-pending-p () boolean)
60
61 (defbinding get-current-event () gdk:event)
62
63 (defbinding main-do-event () nil
64 (event gdk:event))
65
66 (defbinding main () nil)
67
68 (defbinding main-level () int)
69
70 (defbinding main-quit () nil)
71
72 (defbinding main-iteration-do (&optional (blocking t)) boolean
73 (blocking boolean))
74
75 (defun main-iterate-all (&rest args)
76 (declare (ignore args))
77 (loop
78 while (events-pending-p)
79 do (main-iteration-do nil))
80 #+clisp 0)
81
82
83 ;;;; Metaclass for child classes
84
85 (defvar *container-to-child-class-mappings* (make-hash-table))
86
87 (eval-when (:compile-toplevel :load-toplevel :execute)
88 (defclass container-child-class (virtual-slots-class)
89 ())
90
91 (defclass direct-child-slot-definition (direct-virtual-slot-definition)
92 ((pname :reader slot-definition-pname :initarg :pname)))
93
94 (defclass effective-child-slot-definition (effective-virtual-slot-definition)
95 ((pname :reader slot-definition-pname :initarg :pname))))
96
97
98 (defmethod shared-initialize ((class container-child-class) names &key container)
99 (declare (ignore names))
100 (call-next-method)
101 (setf
102 (gethash (find-class (first container)) *container-to-child-class-mappings*)
103 class)
104 #+clisp
105 (loop
106 for slotd in (class-direct-slots class)
107 when (typep slotd 'direct-child-slot-definition)
108 do (loop
109 for reader in (slot-definition-readers slotd)
110 do (add-reader-method class
111 (ensure-generic-function reader :lambda-list '(object))
112 (slot-definition-name slotd)))
113 (loop
114 for writer in (slot-definition-writers slotd)
115 do (add-writer-method class
116 (ensure-generic-function writer :lambda-list '(value object))
117 (slot-definition-name slotd)))))
118
119 (defmethod direct-slot-definition-class ((class container-child-class) &rest initargs)
120 (case (getf initargs :allocation)
121 (:property (find-class 'direct-child-slot-definition))
122 (t (call-next-method))))
123
124 (defmethod effective-slot-definition-class ((class container-child-class) &rest initargs)
125 (case (getf initargs :allocation)
126 (:property (find-class 'effective-child-slot-definition))
127 (t (call-next-method))))
128
129 (defmethod compute-effective-slot-definition-initargs ((class container-child-class) direct-slotds)
130 (if (eq (slot-definition-allocation (first direct-slotds)) :property)
131 (nconc
132 (list :pname (most-specific-slot-value direct-slotds 'pname))
133 (call-next-method))
134 (call-next-method)))
135
136 (defmethod slot-readable-p ((slotd effective-child-slot-definition))
137 (declare (ignore slotd))
138 t)
139
140 (defmethod compute-slot-reader-function ((slotd effective-child-slot-definition) &optional signal-unbound-p)
141 (declare (ignore signal-unbound-p))
142 (let* ((type (slot-definition-type slotd))
143 (pname (slot-definition-pname slotd))
144 (reader (reader-function type :ref :get)))
145 #'(lambda (object)
146 (with-slots (parent child) object
147 (with-memory (gvalue +gvalue-size+)
148 (glib::%gvalue-init gvalue (find-type-number type))
149 (%container-child-get-property parent child pname gvalue)
150 (funcall reader gvalue +gvalue-value-offset+))))))
151
152 (defmethod slot-writable-p ((slotd effective-child-slot-definition))
153 (declare (ignore slotd))
154 t)
155
156 (defmethod compute-slot-writer-function ((slotd effective-child-slot-definition))
157 (let* ((type (slot-definition-type slotd))
158 (pname (slot-definition-pname slotd))
159 (writer (writer-function type :temp t))
160 (destroy (destroy-function type :temp t)))
161 #'(lambda (value object)
162 (with-slots (parent child) object
163 (with-memory (gvalue +gvalue-size+)
164 (glib::%gvalue-init gvalue (find-type-number type))
165 (funcall writer value gvalue +gvalue-value-offset+)
166 (%container-child-set-property parent child pname gvalue)
167 (funcall destroy gvalue +gvalue-value-offset+))
168 value))))
169
170
171 (defmethod add-reader-method ((class container-child-class) generic-function slot-name #?(sbcl>= 1 0 2)slot-documentation)
172 (add-method
173 generic-function
174 (make-instance 'standard-method
175 :specializers (list (find-class 'widget))
176 :lambda-list '(widget)
177 :documentation (or #?(sbcl>= 1 0 2)slot-documentation "automatically generated reader method")
178 :function #'(lambda (args next-methods)
179 (declare (ignore next-methods))
180 (child-property-value (first args) slot-name)))))
181
182 (defmethod add-writer-method ((class container-child-class) generic-function slot-name #?(sbcl>= 1 0 2)slot-documentation)
183 (add-method
184 generic-function
185 (make-instance 'standard-method
186 :specializers (list (find-class t) (find-class 'widget))
187 :lambda-list '(value widget)
188 :documentation (or #?(sbcl>= 1 0 2)slot-documentation "automatically generated reader method")
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))))))
193
194
195 (defmethod validate-superclass ((class container-child-class) (super standard-class))
196 ;(subtypep (class-name super) 'container-child)
197 t)
198
199
200 (defclass container-child (virtual-slots-object)
201 ((parent :initarg :parent :type container)
202 (child :initarg :child :type widget)))
203
204
205 ;;;;
206
207 (defbinding %container-class-list-child-properties () pointer
208 (class pointer)
209 (n-properties unsigned-int :out))
210
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
216 (map-c-vector 'list #'identity array 'param length)
217 (deallocate-memory array)))))
218
219 (defun default-container-child-name (container-class)
220 (intern (format nil "~A-CHILD" container-class)))
221
222 (defun expand-container-type (type forward-p options)
223 (let* ((class (type-from-number type))
224 (super (supertype type))
225 (child-class (default-container-child-name class)))
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)
233 (:metaclass container-child-class)
234 (:container ,class))))))
235
236 (defun container-child-class (container-class)
237 (gethash container-class *container-to-child-class-mappings*))
238
239 (defun container-dependencies (type options)
240 (delete-duplicates
241 (append
242 (gobject-dependencies type options)
243 (mapcar #'param-value-type (query-container-class-child-properties type)))))
244
245 (register-derivable-type 'container "GtkContainer" 'expand-container-type 'container-dependencies)