chiark / gitweb /
Added dependency to the gtk system and a couple of bug fixes
[clg] / gtk / gtkobject.lisp
CommitLineData
0d07716f 1;; Common Lisp bindings for GTK+ v2.0
8b69b878 2;; Copyright (C) 1999-2001 Espen S. Johnsen <espen@users.sourceforge.org>
0d07716f 3;;
4;; This library is free software; you can redistribute it and/or
5;; modify it under the terms of the GNU Lesser General Public
6;; License as published by the Free Software Foundation; either
7;; version 2 of the License, or (at your option) any later version.
8;;
9;; This library is distributed in the hope that it will be useful,
10;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12;; Lesser General Public License for more details.
13;;
14;; You should have received a copy of the GNU Lesser General Public
15;; License along with this library; if not, write to the Free Software
16;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
33766b28 18;; $Id: gtkobject.lisp,v 1.26 2005/03/11 16:48:15 espen Exp $
0d07716f 19
20
21(in-package "GTK")
22
aa1ae3a5 23
0d07716f 24;;;; Misc utils
25
6d27e76c 26; (defun name-to-string (name)
27; (substitute #\_ #\- (string-downcase (string name))))
0d07716f 28
6d27e76c 29; (defun string-to-name (name &optional (package "KEYWORD"))
30; (intern (substitute #\- #\_ (string-upcase name)) package))
0d07716f 31
32
0d07716f 33
34;;;; Superclass for the gtk class hierarchy
35
36(eval-when (:compile-toplevel :load-toplevel :execute)
6baf860c 37 (init-types-in-library
38 #.(concatenate 'string (pkg-config:pkg-variable "gtk+-2.0" "libdir")
33766b28 39 "/libgtk-x11-2.0.so"))
6d27e76c 40
41 (defclass %object (gobject)
0d07716f 42 ()
0d07716f 43 (:metaclass gobject-class)
dcb31db6 44 (:gtype |gtk_object_get_type|)))
0d07716f 45
46
6baf860c 47(defmethod initialize-instance ((object %object) &rest initargs &key signal)
48 (declare (ignore signal))
0d07716f 49 (call-next-method)
6baf860c 50 (reference-foreign (class-of object) (proxy-location object))
fb449127 51 (dolist (signal-definition (get-all initargs :signal))
52 (apply #'signal-connect object signal-definition)))
0d07716f 53
6baf860c 54(defmethod initialize-instance :around ((object %object) &rest initargs)
0d07716f 55 (declare (ignore initargs))
56 (call-next-method)
fb449127 57 (%object-sink object))
0d07716f 58
6d27e76c 59(defbinding %object-sink () nil
60 (object %object))
0d07716f 61
3d36c5d6 62;;;; Main loop and event handling
0d07716f 63
64(declaim (inline events-pending-p main-iteration))
65
3d36c5d6 66(defbinding events-pending-p () boolean)
0d07716f 67
6d27e76c 68(defbinding get-current-event () gdk:event)
13148f19 69
6d27e76c 70(defbinding main-do-event () nil
0d07716f 71 (event gdk:event))
72
6d27e76c 73(defbinding main () nil)
0d07716f 74
6d27e76c 75(defbinding main-level () int)
0d07716f 76
6d27e76c 77(defbinding main-quit () nil)
0d07716f 78
6d27e76c 79(defbinding main-iteration-do (&optional (blocking t)) boolean
0d07716f 80 (blocking boolean))
81
82(defun main-iterate-all (&rest args)
83 (declare (ignore args))
3d36c5d6 84 (loop
85 while (events-pending-p)
86 do (main-iteration-do nil)))
0d07716f 87
0d07716f 88
0d07716f 89;;;; Metaclass for child classes
6d27e76c 90
91(defvar *container-to-child-class-mappings* (make-hash-table))
0d07716f 92
93(eval-when (:compile-toplevel :load-toplevel :execute)
6baf860c 94 (defclass child-class (virtual-slots-class)
eb4f580c 95 ())
0d07716f 96
6d27e76c 97 (defclass direct-child-slot-definition (direct-virtual-slot-definition)
eb4f580c 98 ((pname :reader slot-definition-pname :initarg :pname)))
0d07716f 99
eb4f580c 100 (defclass effective-child-slot-definition (effective-virtual-slot-definition)
101 ((pname :reader slot-definition-pname :initarg :pname)))
0d07716f 102
103
eb4f580c 104(defmethod shared-initialize ((class child-class) names &key container)
0d07716f 105 (call-next-method)
6d27e76c 106 (setf
107 (gethash (find-class (first container)) *container-to-child-class-mappings*)
108 class))
0d07716f 109
eb4f580c 110(defmethod direct-slot-definition-class ((class child-class) &rest initargs)
0d07716f 111 (case (getf initargs :allocation)
fb449127 112 (:property (find-class 'direct-child-slot-definition))
0d07716f 113 (t (call-next-method))))
114
eb4f580c 115(defmethod effective-slot-definition-class ((class child-class) &rest initargs)
0d07716f 116 (case (getf initargs :allocation)
fb449127 117 (:property (find-class 'effective-child-slot-definition))
0d07716f 118 (t (call-next-method))))
0d07716f 119
eb4f580c 120(defmethod compute-effective-slot-definition-initargs ((class child-class) direct-slotds)
121 (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
122 (nconc
123 (list :pname (most-specific-slot-value direct-slotds 'pname))
124 (call-next-method))
125 (call-next-method)))
126
201c9293 127(progn
3d36c5d6 128 #+cmu(declaim (optimize (inhibit-warnings 3)))
129 #+sbcl(declaim (muffle-conditions compiler-note))
201c9293 130 (defun %container-child-get-property (parent child pname gvalue))
131 (defun %container-child-set-property (parent child pname gvalue)))
132
133
eb4f580c 134(defmethod initialize-internal-slot-functions ((slotd effective-child-slot-definition))
dcb31db6 135 (let ((type (slot-definition-type slotd))
136 (pname (slot-definition-pname slotd)))
0493e5f0 137 (setf
138 (slot-value slotd 'getter)
139 #'(lambda (object)
140 (with-slots (parent child) object
dcb31db6 141 (let ((gvalue (gvalue-new type)))
0493e5f0 142 (%container-child-get-property parent child pname gvalue)
143 (unwind-protect
144 (funcall (reader-function type) gvalue +gvalue-value-offset+)
145 (gvalue-free gvalue t))))))
eb4f580c 146
0493e5f0 147 (setf
148 (slot-value slotd 'setter)
149 #'(lambda (value object)
150 (with-slots (parent child) object
dcb31db6 151 (let ((gvalue (gvalue-new type)))
0493e5f0 152 (funcall (writer-function type) value gvalue +gvalue-value-offset+)
153 (%container-child-set-property parent child pname gvalue)
154 (gvalue-free gvalue t)
155 value)))))
156
eb4f580c 157 (call-next-method)))
0d07716f 158
159
3d36c5d6 160(defmethod add-reader-method ((class child-class) generic-function slot-name)
0d07716f 161 (add-method
162 generic-function
163 (make-instance 'standard-method
c66e7b94 164 :specializers (list (find-class 'widget))
165 :lambda-list '(widget)
166 :function #'(lambda (args next-methods)
167 (declare (ignore next-methods))
168 (child-property-value (first args) slot-name)))))
0d07716f 169
3d36c5d6 170(defmethod add-writer-method
0d07716f 171 ((class child-class) generic-function slot-name)
172 (add-method
173 generic-function
174 (make-instance 'standard-method
c66e7b94 175 :specializers (list (find-class t) (find-class 'widget))
176 :lambda-list '(value widget)
177 :function #'(lambda (args next-methods)
178 (declare (ignore next-methods))
179 (destructuring-bind (value widget) args
180 (setf (child-property-value widget slot-name) value))))))
0d07716f 181
182
3d36c5d6 183(defmethod validate-superclass ((class child-class) (super standard-class))
fb449127 184 ;(subtypep (class-name super) 'container-child)
185 t)
0d07716f 186
187
6d27e76c 188(defclass container-child ()
189 ((parent :initarg :parent :type container)
190 (child :initarg :child :type widget)))
191
192
193;;;;
194
fb449127 195(defbinding %container-class-list-child-properties () pointer
196 (class pointer)
197 (n-properties unsigned-int :out))
6d27e76c 198
fb449127 199(defun query-container-class-child-properties (type-number)
200 (let ((class (type-class-ref type-number)))
201 (multiple-value-bind (array length)
202 (%container-class-list-child-properties class)
203 (unwind-protect
6baf860c 204 (map-c-vector 'list #'identity array 'param length)
fb449127 205 (deallocate-memory array)))))
6d27e76c 206
207(defun default-container-child-name (container-class)
208 (intern (format nil "~A-CHILD" container-class)))
209
e9934f39 210(defun expand-container-type (type forward-p options)
eb4f580c 211 (let* ((class (type-from-number type))
212 (super (supertype type))
213 (child-class (default-container-child-name class)))
e9934f39 214 (if forward-p
215 (expand-gobject-type type t options)
216 `(progn
217 ,(expand-gobject-type type nil options)
218 (defclass ,child-class (,(default-container-child-name super))
219 ,(slot-definitions child-class
220 (query-container-class-child-properties type) nil)
221 (:metaclass child-class)
222 (:container ,class))))))
223
224
225(register-derivable-type 'container "GtkContainer" 'expand-container-type 'gobject-dependencies)