chiark / gitweb /
Custom types are now re-registered when a saved image is loaded
[clg] / gtk / gtkobject.lisp
CommitLineData
55212af1 1;; Common Lisp bindings for GTK+ v2.x
2;; Copyright 1999-2005 Espen S. Johnsen <espen@users.sf.net>
0d07716f 3;;
55212af1 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:
0d07716f 11;;
55212af1 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
0d07716f 14;;
55212af1 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.
0d07716f 22
f0c6525f 23;; $Id: gtkobject.lisp,v 1.33 2006/02/28 16:34:37 espen Exp $
0d07716f 24
25
26(in-package "GTK")
27
aa1ae3a5 28
0d07716f 29;;;; Superclass for the gtk class hierarchy
30
31(eval-when (:compile-toplevel :load-toplevel :execute)
6baf860c 32 (init-types-in-library
33 #.(concatenate 'string (pkg-config:pkg-variable "gtk+-2.0" "libdir")
33766b28 34 "/libgtk-x11-2.0.so"))
6d27e76c 35
36 (defclass %object (gobject)
0d07716f 37 ()
0d07716f 38 (:metaclass gobject-class)
dcb31db6 39 (:gtype |gtk_object_get_type|)))
0d07716f 40
41
6baf860c 42(defmethod initialize-instance ((object %object) &rest initargs &key signal)
43 (declare (ignore signal))
0d07716f 44 (call-next-method)
fb449127 45 (dolist (signal-definition (get-all initargs :signal))
46 (apply #'signal-connect object signal-definition)))
0d07716f 47
6baf860c 48(defmethod initialize-instance :around ((object %object) &rest initargs)
0d07716f 49 (declare (ignore initargs))
50 (call-next-method)
bba7e2d9 51 ;; Add a temorary reference which will be removed when the object is
52 ;; sinked
8dcd91d7 53 (reference-foreign (class-of object) (foreign-location object))
fb449127 54 (%object-sink object))
0d07716f 55
6d27e76c 56(defbinding %object-sink () nil
57 (object %object))
0d07716f 58
3d36c5d6 59;;;; Main loop and event handling
0d07716f 60
61(declaim (inline events-pending-p main-iteration))
62
3d36c5d6 63(defbinding events-pending-p () boolean)
0d07716f 64
6d27e76c 65(defbinding get-current-event () gdk:event)
13148f19 66
6d27e76c 67(defbinding main-do-event () nil
0d07716f 68 (event gdk:event))
69
6d27e76c 70(defbinding main () nil)
0d07716f 71
6d27e76c 72(defbinding main-level () int)
0d07716f 73
6d27e76c 74(defbinding main-quit () nil)
0d07716f 75
6d27e76c 76(defbinding main-iteration-do (&optional (blocking t)) boolean
0d07716f 77 (blocking boolean))
78
79(defun main-iterate-all (&rest args)
80 (declare (ignore args))
3d36c5d6 81 (loop
82 while (events-pending-p)
83 do (main-iteration-do nil)))
0d07716f 84
0d07716f 85
0d07716f 86;;;; Metaclass for child classes
6d27e76c 87
88(defvar *container-to-child-class-mappings* (make-hash-table))
0d07716f 89
90(eval-when (:compile-toplevel :load-toplevel :execute)
6baf860c 91 (defclass child-class (virtual-slots-class)
eb4f580c 92 ())
0d07716f 93
6d27e76c 94 (defclass direct-child-slot-definition (direct-virtual-slot-definition)
eb4f580c 95 ((pname :reader slot-definition-pname :initarg :pname)))
0d07716f 96
eb4f580c 97 (defclass effective-child-slot-definition (effective-virtual-slot-definition)
98 ((pname :reader slot-definition-pname :initarg :pname)))
0d07716f 99
100
eb4f580c 101(defmethod shared-initialize ((class child-class) names &key container)
0d07716f 102 (call-next-method)
6d27e76c 103 (setf
104 (gethash (find-class (first container)) *container-to-child-class-mappings*)
105 class))
0d07716f 106
eb4f580c 107(defmethod direct-slot-definition-class ((class child-class) &rest initargs)
0d07716f 108 (case (getf initargs :allocation)
fb449127 109 (:property (find-class 'direct-child-slot-definition))
0d07716f 110 (t (call-next-method))))
111
eb4f580c 112(defmethod effective-slot-definition-class ((class child-class) &rest initargs)
0d07716f 113 (case (getf initargs :allocation)
fb449127 114 (:property (find-class 'effective-child-slot-definition))
0d07716f 115 (t (call-next-method))))
0d07716f 116
eb4f580c 117(defmethod compute-effective-slot-definition-initargs ((class child-class) direct-slotds)
c23cc486 118 (if (eq (slot-definition-allocation (first direct-slotds)) :property)
eb4f580c 119 (nconc
120 (list :pname (most-specific-slot-value direct-slotds 'pname))
121 (call-next-method))
122 (call-next-method)))
123
201c9293 124
eb4f580c 125(defmethod initialize-internal-slot-functions ((slotd effective-child-slot-definition))
dcb31db6 126 (let ((type (slot-definition-type slotd))
127 (pname (slot-definition-pname slotd)))
0493e5f0 128 (setf
129 (slot-value slotd 'getter)
130 #'(lambda (object)
131 (with-slots (parent child) object
dcb31db6 132 (let ((gvalue (gvalue-new type)))
0493e5f0 133 (%container-child-get-property parent child pname gvalue)
134 (unwind-protect
135 (funcall (reader-function type) gvalue +gvalue-value-offset+)
136 (gvalue-free gvalue t))))))
eb4f580c 137
0493e5f0 138 (setf
139 (slot-value slotd 'setter)
140 #'(lambda (value object)
141 (with-slots (parent child) object
dcb31db6 142 (let ((gvalue (gvalue-new type)))
0493e5f0 143 (funcall (writer-function type) value gvalue +gvalue-value-offset+)
144 (%container-child-set-property parent child pname gvalue)
145 (gvalue-free gvalue t)
146 value)))))
147
eb4f580c 148 (call-next-method)))
0d07716f 149
150
3d36c5d6 151(defmethod add-reader-method ((class child-class) generic-function slot-name)
0d07716f 152 (add-method
153 generic-function
154 (make-instance 'standard-method
c66e7b94 155 :specializers (list (find-class 'widget))
156 :lambda-list '(widget)
157 :function #'(lambda (args next-methods)
158 (declare (ignore next-methods))
159 (child-property-value (first args) slot-name)))))
0d07716f 160
3d36c5d6 161(defmethod add-writer-method
0d07716f 162 ((class child-class) generic-function slot-name)
163 (add-method
164 generic-function
165 (make-instance 'standard-method
c66e7b94 166 :specializers (list (find-class t) (find-class 'widget))
167 :lambda-list '(value widget)
168 :function #'(lambda (args next-methods)
169 (declare (ignore next-methods))
170 (destructuring-bind (value widget) args
171 (setf (child-property-value widget slot-name) value))))))
0d07716f 172
173
3d36c5d6 174(defmethod validate-superclass ((class child-class) (super standard-class))
fb449127 175 ;(subtypep (class-name super) 'container-child)
176 t)
0d07716f 177
178
6d27e76c 179(defclass container-child ()
180 ((parent :initarg :parent :type container)
181 (child :initarg :child :type widget)))
182
183
184;;;;
185
fb449127 186(defbinding %container-class-list-child-properties () pointer
187 (class pointer)
188 (n-properties unsigned-int :out))
6d27e76c 189
fb449127 190(defun query-container-class-child-properties (type-number)
191 (let ((class (type-class-ref type-number)))
192 (multiple-value-bind (array length)
193 (%container-class-list-child-properties class)
194 (unwind-protect
6baf860c 195 (map-c-vector 'list #'identity array 'param length)
fb449127 196 (deallocate-memory array)))))
6d27e76c 197
198(defun default-container-child-name (container-class)
199 (intern (format nil "~A-CHILD" container-class)))
200
e9934f39 201(defun expand-container-type (type forward-p options)
eb4f580c 202 (let* ((class (type-from-number type))
203 (super (supertype type))
204 (child-class (default-container-child-name class)))
e9934f39 205 (if forward-p
206 (expand-gobject-type type t options)
207 `(progn
208 ,(expand-gobject-type type nil options)
209 (defclass ,child-class (,(default-container-child-name super))
210 ,(slot-definitions child-class
211 (query-container-class-child-properties type) nil)
212 (:metaclass child-class)
213 (:container ,class))))))
214
215
216(register-derivable-type 'container "GtkContainer" 'expand-container-type 'gobject-dependencies)