ab645e41389486914d6a247d5c85f9180cb51179
[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.29 2006-02-04 12:17:45 espen Exp $
24
25
26 (in-package "GTK")
27
28
29 ;;;; Misc utils
30
31 ; (defun name-to-string (name)
32 ; (substitute #\_ #\- (string-downcase (string name))))
33
34 ; (defun string-to-name (name &optional (package "KEYWORD"))
35 ; (intern (substitute #\- #\_ (string-upcase name)) package))
36
37
38
39 ;;;; Superclass for the gtk class hierarchy
40
41 (eval-when (:compile-toplevel :load-toplevel :execute)
42 (init-types-in-library
43 #.(concatenate 'string (pkg-config:pkg-variable "gtk+-2.0" "libdir")
44 "/libgtk-x11-2.0.so"))
45
46 (defclass %object (gobject)
47 ()
48 (:metaclass gobject-class)
49 (:gtype |gtk_object_get_type|)))
50
51
52 (defmethod initialize-instance ((object %object) &rest initargs &key signal)
53 (declare (ignore signal))
54 (call-next-method)
55 (dolist (signal-definition (get-all initargs :signal))
56 (apply #'signal-connect object signal-definition)))
57
58 (defmethod initialize-instance :around ((object %object) &rest initargs)
59 (declare (ignore initargs))
60 (call-next-method)
61 ;; Add a temorary reference which will be removed when the object is
62 ;; sinked
63 (reference-foreign (class-of object) (foreign-location object))
64 (%object-sink object))
65
66 (defbinding %object-sink () nil
67 (object %object))
68
69 ;;;; Main loop and event handling
70
71 (declaim (inline events-pending-p main-iteration))
72
73 (defbinding events-pending-p () boolean)
74
75 (defbinding get-current-event () gdk:event)
76
77 (defbinding main-do-event () nil
78 (event gdk:event))
79
80 (defbinding main () nil)
81
82 (defbinding main-level () int)
83
84 (defbinding main-quit () nil)
85
86 (defbinding main-iteration-do (&optional (blocking t)) boolean
87 (blocking boolean))
88
89 (defun main-iterate-all (&rest args)
90 (declare (ignore args))
91 (loop
92 while (events-pending-p)
93 do (main-iteration-do nil)))
94
95
96 ;;;; Metaclass for child classes
97
98 (defvar *container-to-child-class-mappings* (make-hash-table))
99
100 (eval-when (:compile-toplevel :load-toplevel :execute)
101 (defclass child-class (virtual-slots-class)
102 ())
103
104 (defclass direct-child-slot-definition (direct-virtual-slot-definition)
105 ((pname :reader slot-definition-pname :initarg :pname)))
106
107 (defclass effective-child-slot-definition (effective-virtual-slot-definition)
108 ((pname :reader slot-definition-pname :initarg :pname)))
109
110
111 (defmethod shared-initialize ((class child-class) names &key container)
112 (call-next-method)
113 (setf
114 (gethash (find-class (first container)) *container-to-child-class-mappings*)
115 class))
116
117 (defmethod direct-slot-definition-class ((class child-class) &rest initargs)
118 (case (getf initargs :allocation)
119 (:property (find-class 'direct-child-slot-definition))
120 (t (call-next-method))))
121
122 (defmethod effective-slot-definition-class ((class child-class) &rest initargs)
123 (case (getf initargs :allocation)
124 (:property (find-class 'effective-child-slot-definition))
125 (t (call-next-method))))
126
127 (defmethod compute-effective-slot-definition-initargs ((class child-class) direct-slotds)
128 (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
129 (nconc
130 (list :pname (most-specific-slot-value direct-slotds 'pname))
131 (call-next-method))
132 (call-next-method)))
133
134 (progn
135 #+cmu(declaim (optimize (inhibit-warnings 3)))
136 #+sbcl(declaim (muffle-conditions compiler-note))
137 (defun %container-child-get-property (parent child pname gvalue))
138 (defun %container-child-set-property (parent child pname gvalue)))
139
140
141 (defmethod initialize-internal-slot-functions ((slotd effective-child-slot-definition))
142 (let ((type (slot-definition-type slotd))
143 (pname (slot-definition-pname slotd)))
144 (setf
145 (slot-value slotd 'getter)
146 #'(lambda (object)
147 (with-slots (parent child) object
148 (let ((gvalue (gvalue-new type)))
149 (%container-child-get-property parent child pname gvalue)
150 (unwind-protect
151 (funcall (reader-function type) gvalue +gvalue-value-offset+)
152 (gvalue-free gvalue t))))))
153
154 (setf
155 (slot-value slotd 'setter)
156 #'(lambda (value object)
157 (with-slots (parent child) object
158 (let ((gvalue (gvalue-new type)))
159 (funcall (writer-function type) value gvalue +gvalue-value-offset+)
160 (%container-child-set-property parent child pname gvalue)
161 (gvalue-free gvalue t)
162 value)))))
163
164 (call-next-method)))
165
166
167 (defmethod add-reader-method ((class child-class) generic-function slot-name)
168 (add-method
169 generic-function
170 (make-instance 'standard-method
171 :specializers (list (find-class 'widget))
172 :lambda-list '(widget)
173 :function #'(lambda (args next-methods)
174 (declare (ignore next-methods))
175 (child-property-value (first args) slot-name)))))
176
177 (defmethod add-writer-method
178 ((class child-class) generic-function slot-name)
179 (add-method
180 generic-function
181 (make-instance 'standard-method
182 :specializers (list (find-class t) (find-class 'widget))
183 :lambda-list '(value widget)
184 :function #'(lambda (args next-methods)
185 (declare (ignore next-methods))
186 (destructuring-bind (value widget) args
187 (setf (child-property-value widget slot-name) value))))))
188
189
190 (defmethod validate-superclass ((class child-class) (super standard-class))
191 ;(subtypep (class-name super) 'container-child)
192 t)
193
194
195 (defclass container-child ()
196 ((parent :initarg :parent :type container)
197 (child :initarg :child :type widget)))
198
199
200 ;;;;
201
202 (defbinding %container-class-list-child-properties () pointer
203 (class pointer)
204 (n-properties unsigned-int :out))
205
206 (defun query-container-class-child-properties (type-number)
207 (let ((class (type-class-ref type-number)))
208 (multiple-value-bind (array length)
209 (%container-class-list-child-properties class)
210 (unwind-protect
211 (map-c-vector 'list #'identity array 'param length)
212 (deallocate-memory array)))))
213
214 (defun default-container-child-name (container-class)
215 (intern (format nil "~A-CHILD" container-class)))
216
217 (defun expand-container-type (type forward-p options)
218 (let* ((class (type-from-number type))
219 (super (supertype type))
220 (child-class (default-container-child-name class)))
221 (if forward-p
222 (expand-gobject-type type t options)
223 `(progn
224 ,(expand-gobject-type type nil options)
225 (defclass ,child-class (,(default-container-child-name super))
226 ,(slot-definitions child-class
227 (query-container-class-child-properties type) nil)
228 (:metaclass child-class)
229 (:container ,class))))))
230
231
232 (register-derivable-type 'container "GtkContainer" 'expand-container-type 'gobject-dependencies)