chiark / gitweb /
Fix to avoid having to rely on internal _get_type functions
[clg] / gtk / gtkobject.lisp
CommitLineData
560af5c5 1;; Common Lisp bindings for GTK+ v2.0
56460319 2;; Copyright (C) 1999-2001 Espen S. Johnsen <espen@users.sourceforge.org>
560af5c5 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
735a29da 18;; $Id: gtkobject.lisp,v 1.25 2005-03-11 10:58:41 espen Exp $
560af5c5 19
20
21(in-package "GTK")
22
8bf63d9f 23
560af5c5 24;;;; Misc utils
25
f86d391e 26; (defun name-to-string (name)
27; (substitute #\_ #\- (string-downcase (string name))))
560af5c5 28
f86d391e 29; (defun string-to-name (name &optional (package "KEYWORD"))
30; (intern (substitute #\- #\_ (string-upcase name)) package))
560af5c5 31
32
560af5c5 33
34;;;; Superclass for the gtk class hierarchy
35
36(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 37 (init-types-in-library
38 #.(concatenate 'string (pkg-config:pkg-variable "gtk+-2.0" "libdir")
735a29da 39 "/libgtk-x11-2.0.so") :prefix "gtk_")
f86d391e 40
41 (defclass %object (gobject)
560af5c5 42 ()
560af5c5 43 (:metaclass gobject-class)
dfa4f314 44 (:gtype |gtk_object_get_type|)))
560af5c5 45
46
9adccb27 47(defmethod initialize-instance ((object %object) &rest initargs &key signal)
48 (declare (ignore signal))
560af5c5 49 (call-next-method)
9adccb27 50 (reference-foreign (class-of object) (proxy-location object))
18eed2a0 51 (dolist (signal-definition (get-all initargs :signal))
52 (apply #'signal-connect object signal-definition)))
560af5c5 53
9adccb27 54(defmethod initialize-instance :around ((object %object) &rest initargs)
560af5c5 55 (declare (ignore initargs))
56 (call-next-method)
18eed2a0 57 (%object-sink object))
560af5c5 58
f86d391e 59(defbinding %object-sink () nil
60 (object %object))
560af5c5 61
73572c12 62;;;; Main loop and event handling
560af5c5 63
64(declaim (inline events-pending-p main-iteration))
65
73572c12 66(defbinding events-pending-p () boolean)
560af5c5 67
f86d391e 68(defbinding get-current-event () gdk:event)
aace61f5 69
f86d391e 70(defbinding main-do-event () nil
560af5c5 71 (event gdk:event))
72
f86d391e 73(defbinding main () nil)
560af5c5 74
f86d391e 75(defbinding main-level () int)
560af5c5 76
f86d391e 77(defbinding main-quit () nil)
560af5c5 78
f86d391e 79(defbinding main-iteration-do (&optional (blocking t)) boolean
560af5c5 80 (blocking boolean))
81
82(defun main-iterate-all (&rest args)
83 (declare (ignore args))
73572c12 84 (loop
85 while (events-pending-p)
86 do (main-iteration-do nil)))
560af5c5 87
560af5c5 88
560af5c5 89;;;; Metaclass for child classes
f86d391e 90
91(defvar *container-to-child-class-mappings* (make-hash-table))
560af5c5 92
93(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 94 (defclass child-class (virtual-slots-class)
1047e159 95 ())
560af5c5 96
f86d391e 97 (defclass direct-child-slot-definition (direct-virtual-slot-definition)
1047e159 98 ((pname :reader slot-definition-pname :initarg :pname)))
560af5c5 99
1047e159 100 (defclass effective-child-slot-definition (effective-virtual-slot-definition)
101 ((pname :reader slot-definition-pname :initarg :pname)))
560af5c5 102
103
1047e159 104(defmethod shared-initialize ((class child-class) names &key container)
560af5c5 105 (call-next-method)
f86d391e 106 (setf
107 (gethash (find-class (first container)) *container-to-child-class-mappings*)
108 class))
560af5c5 109
1047e159 110(defmethod direct-slot-definition-class ((class child-class) &rest initargs)
560af5c5 111 (case (getf initargs :allocation)
18eed2a0 112 (:property (find-class 'direct-child-slot-definition))
560af5c5 113 (t (call-next-method))))
114
1047e159 115(defmethod effective-slot-definition-class ((class child-class) &rest initargs)
560af5c5 116 (case (getf initargs :allocation)
18eed2a0 117 (:property (find-class 'effective-child-slot-definition))
560af5c5 118 (t (call-next-method))))
560af5c5 119
1047e159 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
b030f2d9 127(progn
73572c12 128 #+cmu(declaim (optimize (inhibit-warnings 3)))
129 #+sbcl(declaim (muffle-conditions compiler-note))
b030f2d9 130 (defun %container-child-get-property (parent child pname gvalue))
131 (defun %container-child-set-property (parent child pname gvalue)))
132
133
1047e159 134(defmethod initialize-internal-slot-functions ((slotd effective-child-slot-definition))
dfa4f314 135 (let ((type (slot-definition-type slotd))
136 (pname (slot-definition-pname slotd)))
d24bc5ad 137 (setf
138 (slot-value slotd 'getter)
139 #'(lambda (object)
140 (with-slots (parent child) object
dfa4f314 141 (let ((gvalue (gvalue-new type)))
d24bc5ad 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))))))
1047e159 146
d24bc5ad 147 (setf
148 (slot-value slotd 'setter)
149 #'(lambda (value object)
150 (with-slots (parent child) object
dfa4f314 151 (let ((gvalue (gvalue-new type)))
d24bc5ad 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
1047e159 157 (call-next-method)))
560af5c5 158
159
73572c12 160(defmethod add-reader-method ((class child-class) generic-function slot-name)
560af5c5 161 (add-method
162 generic-function
163 (make-instance 'standard-method
c289d084 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)))))
560af5c5 169
73572c12 170(defmethod add-writer-method
560af5c5 171 ((class child-class) generic-function slot-name)
172 (add-method
173 generic-function
174 (make-instance 'standard-method
c289d084 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))))))
560af5c5 181
182
73572c12 183(defmethod validate-superclass ((class child-class) (super standard-class))
18eed2a0 184 ;(subtypep (class-name super) 'container-child)
185 t)
560af5c5 186
187
f86d391e 188(defclass container-child ()
189 ((parent :initarg :parent :type container)
190 (child :initarg :child :type widget)))
191
192
193;;;;
194
18eed2a0 195(defbinding %container-class-list-child-properties () pointer
196 (class pointer)
197 (n-properties unsigned-int :out))
f86d391e 198
18eed2a0 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
9adccb27 204 (map-c-vector 'list #'identity array 'param length)
18eed2a0 205 (deallocate-memory array)))))
f86d391e 206
207(defun default-container-child-name (container-class)
208 (intern (format nil "~A-CHILD" container-class)))
209
62f12808 210(defun expand-container-type (type forward-p options)
1047e159 211 (let* ((class (type-from-number type))
212 (super (supertype type))
213 (child-class (default-container-child-name class)))
62f12808 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)