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