chiark / gitweb /
Updated for gtk+-1.3.9
[clg] / gtk / gtkobject.lisp
CommitLineData
560af5c5 1;; Common Lisp bindings for GTK+ v2.0
f86d391e 2;; Copyright (C) 1999-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
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
18eed2a0 18;; $Id: gtkobject.lisp,v 1.10 2001-10-21 23:18:11 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)
18eed2a0 37 (init-types-in-library
38 "/opt/gnome/lib/libgtk-x11-1.3.so"
39 :ignore ("gtk_window_get_type_hint"))
f86d391e 40
41 (defclass %object (gobject)
560af5c5 42 ()
560af5c5 43 (:metaclass gobject-class)
44 (:alien-name "GtkObject")))
45
46
f86d391e 47(defmethod shared-initialize ((object %object) names &rest initargs
18eed2a0 48 &allow-other-keys)
49 (declare (ignore names))
560af5c5 50 (call-next-method)
18eed2a0 51 (funcall (proxy-class-copy (class-of object)) nil (proxy-location object)) ; inc ref count before sinking
f86d391e 52 (%object-sink object)
18eed2a0 53 (dolist (signal-definition (get-all initargs :signal))
54 (apply #'signal-connect object signal-definition)))
560af5c5 55
18eed2a0 56(defmethod initialize-proxy ((object %object) &rest initargs)
560af5c5 57 (declare (ignore initargs))
58 (call-next-method)
18eed2a0 59 (%object-sink object))
560af5c5 60
f86d391e 61(defbinding %object-sink () nil
62 (object %object))
560af5c5 63
560af5c5 64
aace61f5 65;;;; Main loop, timeouts and idle functions
560af5c5 66
67(declaim (inline events-pending-p main-iteration))
68
f86d391e 69(defbinding (events-pending-p "gtk_events_pending") () boolean)
560af5c5 70
f86d391e 71(defbinding get-current-event () gdk:event)
aace61f5 72
f86d391e 73(defbinding main-do-event () nil
560af5c5 74 (event gdk:event))
75
f86d391e 76(defbinding main () nil)
560af5c5 77
f86d391e 78(defbinding main-level () int)
560af5c5 79
f86d391e 80(defbinding main-quit () nil)
560af5c5 81
f86d391e 82(defbinding main-iteration-do (&optional (blocking t)) boolean
560af5c5 83 (blocking boolean))
84
85(defun main-iterate-all (&rest args)
86 (declare (ignore args))
87 (when (events-pending-p)
f86d391e 88 (main-iteration-do nil)
560af5c5 89 (main-iterate-all)))
90
18eed2a0 91(system:add-fd-handler (gdk:connection-number) :input #'main-iterate-all)
560af5c5 92(setq lisp::*periodic-polling-function* #'main-iterate-all)
93(setq lisp::*max-event-to-sec* 0)
94(setq lisp::*max-event-to-usec* 1000)
95
96
97
560af5c5 98;;;; Metaclass for child classes
f86d391e 99
100(defvar *container-to-child-class-mappings* (make-hash-table))
560af5c5 101
102(eval-when (:compile-toplevel :load-toplevel :execute)
f86d391e 103 (defclass child-class (virtual-slot-class))
560af5c5 104
f86d391e 105 (defclass direct-child-slot-definition (direct-virtual-slot-definition)
18eed2a0 106 ((pname :reader slot-definition-pname)))
560af5c5 107
108 (defclass effective-child-slot-definition
109 (effective-virtual-slot-definition)))
110
111
f86d391e 112(defmethod shared-initialize ((class child-class) names &rest initargs
113 &key container)
560af5c5 114 (declare (ignore initargs))
115 (call-next-method)
f86d391e 116 (setf
117 (gethash (find-class (first container)) *container-to-child-class-mappings*)
118 class))
560af5c5 119
f86d391e 120(defmethod initialize-instance ((slotd direct-child-slot-definition)
18eed2a0 121 &rest initargs &key pname)
f86d391e 122 (declare (ignore initargs))
123 (call-next-method)
18eed2a0 124 (if pname
125 (setf (slot-value slotd 'pname) pname)
126 ; ???
127 (error "Need pname for slot with allocation :property")))
560af5c5 128
129(defmethod direct-slot-definition-class ((class child-class) initargs)
130 (case (getf initargs :allocation)
18eed2a0 131 (:property (find-class 'direct-child-slot-definition))
560af5c5 132 (t (call-next-method))))
133
560af5c5 134(defmethod effective-slot-definition-class ((class child-class) initargs)
135 (case (getf initargs :allocation)
18eed2a0 136 (:property (find-class 'effective-child-slot-definition))
560af5c5 137 (t (call-next-method))))
560af5c5 138
18eed2a0 139(defbinding %container-child-get-property () nil
140 (container container)
141 (child widget)
142 (property-name string)
143 (value gvalue))
144
145(defbinding %container-child-set-property () nil
146 (container container)
147 (child widget)
148 (property-name string)
149 (value gvalue))
150
151(defmethod compute-virtual-slot-accessors
560af5c5 152 ((class child-class) (slotd effective-child-slot-definition) direct-slotds)
153 (with-slots (type) slotd
18eed2a0 154 (let ((pname (slot-definition-pname (first direct-slotds)))
155 (type-number (find-type-number type)))
560af5c5 156 (list
157 #'(lambda (object)
158 (with-slots (parent child) object
159 (with-gc-disabled
18eed2a0 160 (let ((gvalue (gvalue-new type-number)))
161 (%container-child-get-property parent child pname gvalue)
162 (unwind-protect
f86d391e 163 (funcall
164 (intern-reader-function type)
18eed2a0 165 gvalue +gvalue-value-offset+)
166 (gvalue-free gvalue t))))))
560af5c5 167 #'(lambda (value object)
168 (with-slots (parent child) object
169 (with-gc-disabled
18eed2a0 170 (let ((gvalue (gvalue-new type-number)))
171 (funcall
172 (intern-writer-function type)
173 value gvalue +gvalue-value-offset+)
174 (%container-child-set-property parent child pname gvalue)
175 (funcall
176 (intern-destroy-function type)
177 gvalue +gvalue-value-offset+)
178 (gvalue-free gvalue nil)
179 value))))))))
560af5c5 180
181
182(defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
183 (add-method
184 generic-function
185 (make-instance 'standard-method
186 :specializers (list (find-class 'widget))
187 :lambda-list '(widget)
188 :function #'(lambda (args next-methods)
189 (declare (ignore next-methods))
190 (child-slot-value (first args) slot-name)))))
191
192(defmethod pcl::add-writer-method
193 ((class child-class) generic-function slot-name)
194 (add-method
195 generic-function
196 (make-instance 'standard-method
197 :specializers (list (find-class t) (find-class 'widget))
198 :lambda-list '(value widget)
199 :function #'(lambda (args next-methods)
200 (declare (ignore next-methods))
201 (destructuring-bind (value widget) args
202 (setf
203 (child-slot-value widget slot-name)
204 value))))))
205
206
207(defmethod validate-superclass ((class child-class) (super pcl::standard-class))
18eed2a0 208 ;(subtypep (class-name super) 'container-child)
209 t)
560af5c5 210
211
f86d391e 212(defclass container-child ()
213 ((parent :initarg :parent :type container)
214 (child :initarg :child :type widget)))
215
216
217;;;;
218
18eed2a0 219(defbinding %container-class-list-child-properties () pointer
220 (class pointer)
221 (n-properties unsigned-int :out))
f86d391e 222
18eed2a0 223(defun query-container-class-child-properties (type-number)
224 (let ((class (type-class-ref type-number)))
225 (multiple-value-bind (array length)
226 (%container-class-list-child-properties class)
227 (unwind-protect
228 (map-c-array 'list #'identity array 'param length)
229 (deallocate-memory array)))))
f86d391e 230
231(defun default-container-child-name (container-class)
232 (intern (format nil "~A-CHILD" container-class)))
233
234(defun expand-container-type (type-number &optional slots)
235 (let* ((class (type-from-number type-number))
236 (super (supertype type-number))
237 (child-class (default-container-child-name class))
18eed2a0 238 (expanded-child-slots
239 (mapcar
240 #'(lambda (param)
241 (with-slots (name flags value-type documentation) param
242 (let* ((slot-name (default-slot-name name))
243 (slot-type (type-from-number value-type #|t|#))
244 (accessor
245 (default-slot-accessor class slot-name slot-type)))
246 `(,slot-name
247 :allocation :property
248 :pname ,name
249 ,@(cond
250 ((and
251 (member :writable flags)
252 (member :readable flags))
253 (list :accessor accessor))
254 ((member :writable flags)
255 (list :writer `(setf ,accessor)))
256 ((member :readable flags)
257 (list :reader accessor)))
258 ,@(when (or
259 (member :construct flags)
260 (member :writable flags))
261 (list :initarg (intern (string slot-name) "KEYWORD")))
262 :type ,slot-type
263 ,@(when documentation
264 (list :documentation documentation))))))
265 (query-container-class-child-properties type-number))))
266 `(progn
267 ,(expand-gobject-type type-number slots)
268 (defclass ,child-class
269 (,(default-container-child-name super))
270 ,expanded-child-slots
271 (:metaclass child-class)
272 (:container ,class)))))
273
274(register-derivable-type 'container "GtkContainer" 'expand-container-type)