5d1cab59a067c1789d1ad6ab34cc31ccd4e39c92
[clg] / gtk / gtkobject.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 1999-2001 Espen S. Johnsen <espen@users.sourceforge.org>
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
18 ;; $Id: gtkobject.lisp,v 1.14 2002-03-24 13:02:43 espen Exp $
19
20
21 (in-package "GTK")
22
23
24 ;;;; Misc utils
25
26 ; (defun name-to-string (name)
27 ; (substitute #\_ #\- (string-downcase (string name))))
28
29 ; (defun string-to-name (name &optional (package "KEYWORD"))
30 ; (intern (substitute #\- #\_ (string-upcase name)) package))
31
32
33
34 ;;;; Superclass for the gtk class hierarchy
35
36 (eval-when (:compile-toplevel :load-toplevel :execute)
37 (init-types-in-library "libgtk-x11-2.0.so"
38 :ignore ("gtk_window_get_type_hint"))
39
40 (defclass %object (gobject)
41 ()
42 (:metaclass gobject-class)
43 (:alien-name "GtkObject")))
44
45
46 (defmethod shared-initialize ((object %object) names &rest initargs
47 &allow-other-keys)
48 (declare (ignore names))
49 (call-next-method)
50 (object-ref object) ; inc ref count before sinking
51 (%object-sink object)
52 (dolist (signal-definition (get-all initargs :signal))
53 (apply #'signal-connect object signal-definition)))
54
55 (defmethod initialize-proxy ((object %object) &rest initargs)
56 (declare (ignore initargs))
57 (call-next-method)
58 (%object-sink object))
59
60 (defbinding %object-sink () nil
61 (object %object))
62
63
64 ;;;; Main loop, timeouts and idle functions
65
66 (declaim (inline events-pending-p main-iteration))
67
68 (defbinding (events-pending-p "gtk_events_pending") () boolean)
69
70 (defbinding get-current-event () gdk:event)
71
72 (defbinding main-do-event () nil
73 (event gdk:event))
74
75 (defbinding main () nil)
76
77 (defbinding main-level () int)
78
79 (defbinding main-quit () nil)
80
81 (defbinding main-iteration-do (&optional (blocking t)) boolean
82 (blocking boolean))
83
84 (defun main-iterate-all (&rest args)
85 (declare (ignore args))
86 (when (events-pending-p)
87 (main-iteration-do nil)
88 (main-iterate-all)))
89
90 (system:add-fd-handler (gdk:connection-number) :input #'main-iterate-all)
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
97 ;;;; Metaclass for child classes
98
99 (defvar *container-to-child-class-mappings* (make-hash-table))
100
101 (eval-when (:compile-toplevel :load-toplevel :execute)
102 (defclass child-class (virtual-slot-class))
103
104 (defclass direct-child-slot-definition (direct-virtual-slot-definition)
105 ((pname :reader slot-definition-pname)))
106
107 (defclass effective-child-slot-definition
108 (effective-virtual-slot-definition)))
109
110
111 (defmethod shared-initialize ((class child-class) names &rest initargs
112 &key container)
113 (declare (ignore initargs))
114 (call-next-method)
115 (setf
116 (gethash (find-class (first container)) *container-to-child-class-mappings*)
117 class))
118
119 (defmethod initialize-instance ((slotd direct-child-slot-definition)
120 &rest initargs &key pname)
121 (declare (ignore initargs))
122 (call-next-method)
123 (if pname
124 (setf (slot-value slotd 'pname) pname)
125 ; ???
126 (error "Need pname for slot with allocation :property")))
127
128 (defmethod direct-slot-definition-class ((class child-class) initargs)
129 (case (getf initargs :allocation)
130 (:property (find-class 'direct-child-slot-definition))
131 (t (call-next-method))))
132
133 (defmethod effective-slot-definition-class ((class child-class) initargs)
134 (case (getf initargs :allocation)
135 (:property (find-class 'effective-child-slot-definition))
136 (t (call-next-method))))
137
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
144 (defmethod compute-virtual-slot-accessors
145 ((class child-class) (slotd effective-child-slot-definition) direct-slotds)
146
147 (with-slots (type) slotd
148 (let ((pname (slot-definition-pname (first direct-slotds)))
149 (type-number (find-type-number type)))
150 (list
151 #'(lambda (object)
152 (with-slots (parent child) object
153 (with-gc-disabled
154 (let ((gvalue (gvalue-new type-number)))
155 (%container-child-get-property parent child pname gvalue)
156 (unwind-protect
157 (funcall
158 (intern-reader-function type)
159 gvalue +gvalue-value-offset+)
160 (gvalue-free gvalue t))))))
161 #'(lambda (value object)
162 (with-slots (parent child) object
163 (with-gc-disabled
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))))))))
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))
202 ;(subtypep (class-name super) 'container-child)
203 t)
204
205
206 (defclass container-child ()
207 ((parent :initarg :parent :type container)
208 (child :initarg :child :type widget)))
209
210
211 ;;;;
212
213 (defbinding %container-class-list-child-properties () pointer
214 (class pointer)
215 (n-properties unsigned-int :out))
216
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)))))
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))
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|#))
238 (accessor
239 (default-slot-accessor class slot-name slot-type)))
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)