560af5c5 |
1 | ;; Common Lisp bindings for GTK+ v2.0 |
c8c48a4c |
2 | ;; Copyright (C) 1999-2000 Espen S. Johnsen <esj@ostud.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 | |
968da4bc |
18 | ;; $Id: gtkobject.lisp,v 1.8 2001-01-28 14:23:38 espen Exp $ |
560af5c5 |
19 | |
20 | |
21 | (in-package "GTK") |
22 | |
8bf63d9f |
23 | |
24 | ;;;; Initializing |
25 | |
26 | (setf (alien-type-name 'pointer) "gpointer") |
27 | |
28 | |
560af5c5 |
29 | ;;;; Misc utils |
30 | |
31 | (defun name-to-string (name) |
32 | (substitute #\_ #\- (string-downcase (string name)))) |
33 | |
34 | (defun string-to-name (name &optional (package "KEYWORD")) |
35 | (intern (substitute #\- #\_ (string-upcase name)) package)) |
36 | |
37 | |
968da4bc |
38 | ;;; Argument stuff - to be removed soon |
560af5c5 |
39 | |
40 | (deftype arg () 'pointer) |
41 | |
42 | (defconstant +arg-type-offset+ 0) |
43 | (defconstant +arg-name-offset+ 4) |
44 | (defconstant +arg-value-offset+ 8) |
45 | (defconstant +arg-size+ 16) |
46 | |
47 | (define-foreign arg-new () arg |
48 | (type type-number)) |
49 | |
50 | (define-foreign %arg-free () nil |
51 | (arg arg) |
52 | (free-contents boolean)) |
53 | |
54 | (defun arg-free (arg free-contents &optional alien) |
55 | (cond |
56 | (alien (%arg-free arg free-contents)) |
57 | (t |
58 | (unless (null-pointer-p arg) |
59 | (when free-contents |
60 | (funcall |
61 | (get-destroy-function (type-from-number (arg-type arg))) |
62 | arg +arg-value-offset+)) |
63 | (deallocate-memory arg))))) |
64 | |
65 | (define-foreign %arg-reset () nil |
66 | (arg arg)) |
67 | |
68 | (defun arg-name (arg) |
69 | (funcall (get-reader-function '(static string)) arg +arg-name-offset+)) |
70 | |
71 | (defun (setf arg-name) (name arg) |
72 | (funcall (get-writer-function '(static string)) name arg +arg-name-offset+) |
73 | name) |
74 | |
75 | (defun arg-type (arg) |
76 | (system:sap-ref-32 arg +arg-type-offset+)) |
77 | |
78 | (defun (setf arg-type) (type arg) |
79 | (setf (system:sap-ref-32 arg +arg-type-offset+) type)) |
80 | |
81 | (defun arg-value (arg &optional (type (type-from-number (arg-type arg)))) |
82 | (funcall (get-reader-function type) arg +arg-value-offset+)) |
83 | |
84 | ;; One should never call this function on an arg whose value is already set |
85 | (defun (setf arg-value) |
86 | (value arg &optional (type (type-from-number (arg-type arg)))) |
a250ff35 |
87 | (funcall (get-writer-function type) value arg +arg-value-offset+) |
560af5c5 |
88 | value) |
89 | |
90 | (defun (setf return-arg-value) |
91 | (value arg &optional (type (type-from-number (arg-type arg)))) |
92 | ; this is probably causing a memory leak |
93 | (funcall (get-writer-function type) value (arg-value arg 'pointer) 0) |
94 | value) |
95 | |
96 | (defun arg-array-ref (arg0 index) |
97 | (system:sap+ arg0 (* index +arg-size+))) |
98 | |
99 | |
100 | ;;;; Superclass for the gtk class hierarchy |
101 | |
102 | (eval-when (:compile-toplevel :load-toplevel :execute) |
103 | (defclass object (gobject) |
104 | () |
105 | ; ((flags |
106 | ; :allocation :alien |
107 | ; :accessor object-flags |
108 | ; :type object-flags)) |
109 | (:metaclass gobject-class) |
110 | (:alien-name "GtkObject"))) |
111 | |
112 | |
113 | (defmethod shared-initialize ((object object) names &rest initargs &key signals) |
114 | (declare (ignore initargs names)) |
115 | (call-next-method) |
116 | (dolist (signal signals) |
117 | (apply #'signal-connect object signal))) |
118 | |
119 | |
120 | (defmethod initialize-instance :after ((object object) &rest initargs &key) |
121 | (declare (ignore initargs)) |
968da4bc |
122 | ; (object-default-construct object) |
560af5c5 |
123 | (reference-instance object) |
124 | (object-sink object)) |
125 | |
126 | |
127 | (defmethod from-alien-initialzie-instance ((object object) &rest initargs) |
128 | (declare (ignore initargs)) |
129 | (call-next-method) |
130 | (object-sink object)) |
131 | |
132 | |
560af5c5 |
133 | (define-foreign object-sink () nil |
134 | (object object)) |
135 | |
560af5c5 |
136 | |
aace61f5 |
137 | ;;;; Main loop, timeouts and idle functions |
560af5c5 |
138 | |
139 | (declaim (inline events-pending-p main-iteration)) |
140 | |
141 | (define-foreign ("gtk_events_pending" events-pending-p) () boolean) |
142 | |
aace61f5 |
143 | (define-foreign get-current-event () gdk:event) |
144 | |
560af5c5 |
145 | (define-foreign main-do-event () nil |
146 | (event gdk:event)) |
147 | |
148 | (define-foreign main () nil) |
149 | |
150 | (define-foreign main-level () int) |
151 | |
152 | (define-foreign main-quit () nil) |
153 | |
154 | (define-foreign |
155 | ("gtk_main_iteration_do" main-iteration) (&optional (blocking t)) boolean |
156 | (blocking boolean)) |
157 | |
158 | (defun main-iterate-all (&rest args) |
159 | (declare (ignore args)) |
160 | (when (events-pending-p) |
161 | (main-iteration nil) |
162 | (main-iterate-all))) |
163 | |
968da4bc |
164 | ; (define-foreign ("gtk_timeout_add_full" timeout-add) |
165 | ; (interval function) unsigned-int |
166 | ; (interval (unsigned 32)) |
167 | ; (0 unsigned-long) |
168 | ; (*callback-marshal* pointer) |
169 | ; ((register-callback-function function) unsigned-long) |
170 | ; (*destroy-marshal* pointer)) |
aace61f5 |
171 | |
968da4bc |
172 | ; (define-foreign timeout-remove () nil |
173 | ; (timeout-handler-id unsigned-int)) |
aace61f5 |
174 | |
968da4bc |
175 | ; (define-foreign ("gtk_idle_add_full" idle-add) |
176 | ; (function &optional (priority 200)) unsigned-int |
177 | ; (priority int) |
178 | ; (0 unsigned-long) |
179 | ; (*callback-marshal* pointer) |
180 | ; ((register-callback-function function) unsigned-long) |
181 | ; (*destroy-marshal* pointer)) |
aace61f5 |
182 | |
968da4bc |
183 | ; (define-foreign idle-remove () nil |
184 | ; (idle-handler-id unsigned-int)) |
aace61f5 |
185 | |
186 | |
560af5c5 |
187 | (system:add-fd-handler (gdk:event-poll-fd) :input #'main-iterate-all) |
188 | (setq lisp::*periodic-polling-function* #'main-iterate-all) |
189 | (setq lisp::*max-event-to-sec* 0) |
190 | (setq lisp::*max-event-to-usec* 1000) |
191 | |
192 | |
193 | |
560af5c5 |
194 | ;;;; Metaclass used for subclasses of object |
195 | |
196 | (eval-when (:compile-toplevel :load-toplevel :execute) |
968da4bc |
197 | (defclass object-class (gobject-class))) |
560af5c5 |
198 | |
199 | |
200 | (defmethod validate-superclass ((class object-class) |
201 | (super pcl::standard-class)) |
202 | (subtypep (class-name super) 'object)) |
968da4bc |
203 | |
560af5c5 |
204 | |
205 | ;;;; Metaclasses used for widgets and containers |
206 | |
207 | (eval-when (:compile-toplevel :load-toplevel :execute) |
208 | (defclass widget-class (object-class)) |
209 | |
210 | (defclass container-class (widget-class) |
211 | (child-class))) |
212 | |
213 | |
214 | (defvar *child-to-container-class-mappings* (make-hash-table)) |
215 | |
216 | (defmethod shared-initialize ((class container-class) names |
217 | &rest initargs &key name child-class) |
218 | (declare (ignore initargs)) |
219 | (call-next-method) |
220 | (with-slots ((child-class-slot child-class)) class |
221 | (setf |
222 | child-class-slot |
223 | (or |
224 | (first child-class) |
225 | (intern (format nil "~A-CHILD" (or name (class-name class))))) |
226 | (gethash child-class-slot *child-to-container-class-mappings*) |
227 | class))) |
228 | |
229 | |
230 | (defmethod validate-superclass ((class widget-class) |
231 | (super pcl::standard-class)) |
232 | (subtypep (class-name super) 'widget)) |
233 | |
234 | (defmethod validate-superclass ((class container-class) |
235 | (super pcl::standard-class)) |
236 | (subtypep (class-name super) 'container)) |
237 | |
238 | |
239 | |
240 | ;;;; Metaclass for child classes |
241 | |
242 | (eval-when (:compile-toplevel :load-toplevel :execute) |
243 | (defclass child-class (virtual-class)) |
244 | |
245 | (defclass direct-child-slot-definition (direct-virtual-slot-definition)) |
246 | |
247 | (defclass effective-child-slot-definition |
248 | (effective-virtual-slot-definition))) |
249 | |
250 | |
251 | (defmethod initialize-instance ((slotd direct-child-slot-definition) |
252 | &rest initargs &key) |
253 | (declare (ignore initargs)) |
254 | (call-next-method) |
255 | (unless (slot-boundp slotd 'location) |
256 | (with-slots (pcl::name location pcl::class) slotd |
257 | (setf |
258 | location |
259 | (format nil "~A::~A" |
260 | (alien-type-name |
261 | (gethash (class-name pcl::class) *child-to-container-class-mappings*)) |
262 | (name-to-string pcl::name)))))) |
263 | |
264 | |
265 | (defmethod direct-slot-definition-class ((class child-class) initargs) |
266 | (case (getf initargs :allocation) |
267 | (:arg (find-class 'direct-child-slot-definition)) |
268 | (t (call-next-method)))) |
269 | |
270 | |
271 | (defmethod effective-slot-definition-class ((class child-class) initargs) |
272 | (case (getf initargs :allocation) |
273 | (:arg (find-class 'effective-child-slot-definition)) |
274 | (t (call-next-method)))) |
275 | |
276 | |
277 | (defmethod compute-virtual-slot-location |
278 | ((class child-class) (slotd effective-child-slot-definition) direct-slotds) |
279 | (with-slots (type) slotd |
280 | (let ((location (slot-definition-location (first direct-slotds))) |
281 | (type-number (find-type-number type)) |
282 | (reader (get-reader-function type)) |
283 | (writer (get-writer-function type)) |
284 | (destroy (get-destroy-function type))) |
285 | (list |
286 | #'(lambda (object) |
287 | (with-slots (parent child) object |
288 | (with-gc-disabled |
289 | (let ((arg (arg-new type-number))) |
290 | (setf (arg-name arg) location) |
291 | (container-child-get-arg parent child arg) |
292 | (prog1 |
293 | (funcall reader arg +arg-value-offset+) |
294 | (arg-free arg t t)))))) |
295 | #'(lambda (value object) |
296 | (with-slots (parent child) object |
297 | (with-gc-disabled |
298 | (let ((arg (arg-new type-number))) |
299 | (setf (arg-name arg) location) |
300 | (funcall writer value arg +arg-value-offset+) |
301 | (container-child-set-arg parent child arg) |
302 | (funcall destroy arg +arg-value-offset+) |
303 | (arg-free arg nil) |
304 | value)))))))) |
305 | |
306 | |
307 | (defmethod pcl::add-reader-method ((class child-class) generic-function slot-name) |
308 | (add-method |
309 | generic-function |
310 | (make-instance 'standard-method |
311 | :specializers (list (find-class 'widget)) |
312 | :lambda-list '(widget) |
313 | :function #'(lambda (args next-methods) |
314 | (declare (ignore next-methods)) |
315 | (child-slot-value (first args) slot-name))))) |
316 | |
317 | (defmethod pcl::add-writer-method |
318 | ((class child-class) generic-function slot-name) |
319 | (add-method |
320 | generic-function |
321 | (make-instance 'standard-method |
322 | :specializers (list (find-class t) (find-class 'widget)) |
323 | :lambda-list '(value widget) |
324 | :function #'(lambda (args next-methods) |
325 | (declare (ignore next-methods)) |
326 | (destructuring-bind (value widget) args |
327 | (setf |
328 | (child-slot-value widget slot-name) |
329 | value)))))) |
330 | |
331 | |
332 | (defmethod validate-superclass ((class child-class) (super pcl::standard-class)) |
333 | (subtypep (class-name super) 'container-child)) |
334 | |
335 | |