chiark / gitweb /
initial checkin
[clg] / gtk / gtkobject.lisp
CommitLineData
0d07716f 1;; Common Lisp bindings for GTK+ v2.0
2;; Copyright (C) 1999-2000 Espen S. Johnsen <espejohn@online.no>
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
ecb4e7dc 18;; $Id: gtkobject.lisp,v 1.1.1.1 2000/08/14 16:44:54 espen Exp $
0d07716f 19
20
21(in-package "GTK")
22
23;;;; Misc utils
24
25(defun name-to-string (name)
26 (substitute #\_ #\- (string-downcase (string name))))
27
28(defun string-to-name (name &optional (package "KEYWORD"))
29 (intern (substitute #\- #\_ (string-upcase name)) package))
30
31
32;;;; Argument stuff
33
34(deftype arg () 'pointer)
35
36(defconstant +arg-type-offset+ 0)
37(defconstant +arg-name-offset+ 4)
38(defconstant +arg-value-offset+ 8)
39(defconstant +arg-size+ 16)
40
41(define-foreign arg-new () arg
42 (type type-number))
43
44(define-foreign %arg-free () nil
45 (arg arg)
46 (free-contents boolean))
47
48(defun arg-free (arg free-contents &optional alien)
49 (cond
50 (alien (%arg-free arg free-contents))
51 (t
52 (unless (null-pointer-p arg)
53 (when free-contents
54 (funcall
55 (get-destroy-function (type-from-number (arg-type arg)))
56 arg +arg-value-offset+))
57 (deallocate-memory arg)))))
58
59(define-foreign %arg-reset () nil
60 (arg arg))
61
62(defun arg-name (arg)
63 (funcall (get-reader-function '(static string)) arg +arg-name-offset+))
64
65(defun (setf arg-name) (name arg)
66 (funcall (get-writer-function '(static string)) name arg +arg-name-offset+)
67 name)
68
69(defun arg-type (arg)
70 (system:sap-ref-32 arg +arg-type-offset+))
71
72(defun (setf arg-type) (type arg)
73 (setf (system:sap-ref-32 arg +arg-type-offset+) type))
74
75(defun arg-value (arg &optional (type (type-from-number (arg-type arg))))
76 (funcall (get-reader-function type) arg +arg-value-offset+))
77
78;; One should never call this function on an arg whose value is already set
79(defun (setf arg-value)
80 (value arg &optional (type (type-from-number (arg-type arg))))
81 (funcall (get-writer-funcation type) value arg +arg-value-offset+)
82 value)
83
84(defun (setf return-arg-value)
85 (value arg &optional (type (type-from-number (arg-type arg))))
86 ; this is probably causing a memory leak
87 (funcall (get-writer-function type) value (arg-value arg 'pointer) 0)
88 value)
89
90(defun arg-array-ref (arg0 index)
91 (system:sap+ arg0 (* index +arg-size+)))
92
93
94;;;; Superclass for the gtk class hierarchy
95
96(eval-when (:compile-toplevel :load-toplevel :execute)
97 (defclass object (gobject)
98 ()
99; ((flags
100; :allocation :alien
101; :accessor object-flags
102; :type object-flags))
103 (:metaclass gobject-class)
104 (:alien-name "GtkObject")))
105
106
107(defmethod shared-initialize ((object object) names &rest initargs &key signals)
108 (declare (ignore initargs names))
109 (call-next-method)
110 (dolist (signal signals)
111 (apply #'signal-connect object signal)))
112
113
114(defmethod initialize-instance :after ((object object) &rest initargs &key)
115 (declare (ignore initargs))
116 (object-default-construct object)
117 (reference-instance object)
118 (object-sink object))
119
120
121(defmethod from-alien-initialzie-instance ((object object) &rest initargs)
122 (declare (ignore initargs))
123 (call-next-method)
124 (object-sink object))
125
126
127(define-foreign object-default-construct () nil
128 (object object))
129
130(define-foreign object-sink () nil
131 (object object))
132
133(define-foreign ("gtk_object_getv" object-get-arg) () nil
134 (object object)
135 (1 unsigned-int)
136 (arg arg))
137
138(define-foreign ("gtk_object_setv" object-set-arg) () nil
139 (object object)
140 (1 unsigned-int)
141 (arg arg))
142
143(defun object-arg (object name)
144 (with-gc-disabled
145 (let ((arg (arg-new 0)))
146 (setf (arg-name arg) name)
147 (object-get-arg object arg)
148 (let ((type (type-from-number (arg-type arg))))
149 (prog1
150 (arg-value arg type)
151 (arg-free arg t))))))
152
153(defun (setf object-arg) (value object name)
154 (with-gc-disabled
155 (let ((arg (arg-new 0)))
156 (setf (arg-name arg) name)
157 (object-get-arg object arg)
158 (let ((type (type-from-number (arg-type arg))))
159 (%arg-reset arg)
160 (setf (arg-value arg type) value)
161 (object-set-arg object arg)
162 (arg-free arg t))))
163 value)
164
165
166;;;; Callback and user data mechanism
167
168(declaim (fixnum *user-data-count*))
169
170(defvar *user-data* (make-hash-table))
171(defvar *user-data-count* 0)
172
173(defun register-user-data (object &optional destroy-function)
174 (check-type destroy-function (or null symbol function))
175; (incf *user-data-count*)
176 (setq *user-data-count* (the fixnum (1+ *user-data-count*)))
177 (setf
178 (gethash *user-data-count* *user-data*)
179 (cons object destroy-function))
180 *user-data-count*)
181
182
183(defun find-user-data (id)
184 (check-type id fixnum)
185 (multiple-value-bind (user-data p) (gethash id *user-data*)
186 (values (car user-data) p)))
187
188
189(defun register-callback-function (function)
190 (check-type function (or null symbol function))
191 ; We treat callbacks just as ordinary user data
192 (register-user-data function))
193
194
195(defun callback-trampoline (callback-id nargs arg-array)
196 (declare (fixnum callback-id nargs))
197 (let* ((return-arg (unless (null-pointer-p arg-array)
198 (arg-array-ref arg-array nargs)))
199 (return-type (if return-arg
200 (type-from-number (arg-type return-arg))
201 nil))
202 (args nil)
203 (callback-function (find-user-data callback-id)))
204
205 (dotimes (n nargs)
206 (push (arg-value (arg-array-ref arg-array (- nargs n 1))) args))
207
208 (labels ((invoke-callback ()
209 (restart-case
210 (unwind-protect
211 (let ((return-value (apply callback-function args)))
212 (when return-type
213 (setf (return-arg-value return-arg) return-value))))
214
215 (continue nil :report "Return from callback function"
216 (when return-type
217 (format
218 *query-io*
219 "Enter return value of type ~S: "
220 return-type)
221 (force-output *query-io*)
222 (setf
223 (return-arg-value return-arg)
224 (eval (read *query-io*)))))
225 (re-invoke nil :report "Re-invoke callback function"
226 (invoke-callback)))))
227 (invoke-callback))))
228
229
230(defun destroy-user-data (id)
231 (check-type id fixnum)
232 (let ((user-data (gethash id *user-data*)))
233 (when (cdr user-data)
234 (funcall (cdr user-data) (car user-data))))
235 (remhash id *user-data*))
236
237
238(defvar *callback-marshal* (system:foreign-symbol-address "callback_marshal"))
239(defvar *destroy-marshal* (system:foreign-symbol-address "destroy_marshal"))
240
241(defun after-gc-hook ()
242 (setf
243 (extern-alien "callback_trampoline" system-area-pointer)
244 (make-pointer (kernel:get-lisp-obj-address #'callback-trampoline))
245 (extern-alien "destroy_user_data" system-area-pointer)
246 (make-pointer (kernel:get-lisp-obj-address #'destroy-user-data))))
247
248(pushnew 'after-gc-hook ext:*after-gc-hooks*)
249(after-gc-hook)
250
251
252
253;;;; Main loop
254
255(declaim (inline events-pending-p main-iteration))
256
257(define-foreign ("gtk_events_pending" events-pending-p) () boolean)
258
259(define-foreign main-do-event () nil
260 (event gdk:event))
261
262(define-foreign main () nil)
263
264(define-foreign main-level () int)
265
266(define-foreign main-quit () nil)
267
268(define-foreign
269 ("gtk_main_iteration_do" main-iteration) (&optional (blocking t)) boolean
270 (blocking boolean))
271
272(defun main-iterate-all (&rest args)
273 (declare (ignore args))
274 (when (events-pending-p)
275 (main-iteration nil)
276 (main-iterate-all)))
277
278(system:add-fd-handler (gdk:event-poll-fd) :input #'main-iterate-all)
279(setq lisp::*periodic-polling-function* #'main-iterate-all)
280(setq lisp::*max-event-to-sec* 0)
281(setq lisp::*max-event-to-usec* 1000)
282
283
284
285;;;; Signals
286
287(define-foreign %signal-emit-stop () nil
288 (object object)
289 (signal-id unsigned-int))
290
291(define-foreign %signal-emit-stop-by-name (object signal) nil
292 (object object)
293 ((name-to-string signal) string))
294
295(defun signal-emit-stop (object signal)
296 (if (numberp signal)
297 (%signal-emit-stop object signal)
298 (%signal-emit-stop-by-name object signal)))
299
300(define-foreign %signal-connect-full
301 (object signal function after) unsigned-int
302 (object object)
303 ((name-to-string signal) string)
304 (0 unsigned-long)
305 (*callback-marshal* pointer)
306 ((register-callback-function function) unsigned-long)
307 (*destroy-marshal* pointer)
308 (nil boolean)
309 (after boolean))
310
311(defun signal-connect (object signal function
312 &key after ((:object callback-object)))
313 (let* ((callback-object (if (eq callback-object t)
314 object
315 callback-object))
316 (callback-function
317 (if callback-object
318 #'(lambda (&rest args) (apply function callback-object args))
319 function)))
320 (%signal-connect-full object signal callback-function after)))
321
322(define-foreign signal-disconnect () nil
323 (object object)
324 (handler unsigned-int))
325
326(define-foreign signal-handler-block () nil
327 (object object)
328 (handler unsigned-int))
329
330(define-foreign signal-handler-unblock () nil
331 (object object)
332 (handler unsigned-int))
333
334
335;;;; Metaclass used for subclasses of object
336
337(eval-when (:compile-toplevel :load-toplevel :execute)
338 (defclass object-class (gobject-class))
339
340 (defclass direct-object-slot-definition (direct-virtual-slot-definition))
341
342 (defclass effective-object-slot-definition
343 (effective-virtual-slot-definition)))
344
345
346(defmethod initialize-instance :after ((slotd direct-object-slot-definition)
347 &rest initargs &key)
348 (declare (ignore initargs))
349 (unless (slot-boundp slotd 'location)
350 (with-slots (pcl::name location pcl::class) slotd
351 (setf
352 location
353 (format nil "~A::~A"
354 (alien-type-name (class-name pcl::class))
355 (name-to-string pcl::name))))))
356
357
358(defmethod direct-slot-definition-class ((class object-class) initargs)
359 (case (getf initargs :allocation)
360 (:arg (find-class 'direct-object-slot-definition))
361 (t (call-next-method))))
362
363
364(defmethod effective-slot-definition-class ((class object-class) initargs)
365 (case (getf initargs :allocation)
366 (:arg (find-class 'effective-object-slot-definition))
367 (t (call-next-method))))
368
369
370(defmethod compute-virtual-slot-location
371 ((class object-class) (slotd effective-object-slot-definition)
372 direct-slotds)
373 (with-slots (type) slotd
374 (let ((location (slot-definition-location (first direct-slotds)))
375 (type-number (find-type-number type))
376 (reader (get-reader-function type))
377 (writer (get-writer-function type))
378 (destroy (get-destroy-function type)))
379 (list
380 #'(lambda (object)
381 (with-gc-disabled
382 (let ((arg (arg-new type-number)))
383 (setf (arg-name arg) location)
384 (object-get-arg object arg)
385 (prog1
386 (funcall reader arg +arg-value-offset+)
387 (arg-free arg t t)))))
388 #'(lambda (value object)
389 (with-gc-disabled
390 (let ((arg (arg-new type-number)))
391 (setf (arg-name arg) location)
392 (funcall writer value arg +arg-value-offset+)
393 (object-set-arg object arg)
394 (funcall destroy arg +arg-value-offset+)
395 (arg-free arg nil)
396 value)))))))
397
398
399(defmethod validate-superclass ((class object-class)
400 (super pcl::standard-class))
401 (subtypep (class-name super) 'object))
402
403
404;;;; Metaclasses used for widgets and containers
405
406(eval-when (:compile-toplevel :load-toplevel :execute)
407 (defclass widget-class (object-class))
408
409 (defclass container-class (widget-class)
410 (child-class)))
411
412
413(defvar *child-to-container-class-mappings* (make-hash-table))
414
415(defmethod shared-initialize ((class container-class) names
416 &rest initargs &key name child-class)
417 (declare (ignore initargs))
418 (call-next-method)
419 (with-slots ((child-class-slot child-class)) class
420 (setf
421 child-class-slot
422 (or
423 (first child-class)
424 (intern (format nil "~A-CHILD" (or name (class-name class)))))
425 (gethash child-class-slot *child-to-container-class-mappings*)
426 class)))
427
428
429(defmethod validate-superclass ((class widget-class)
430 (super pcl::standard-class))
431 (subtypep (class-name super) 'widget))
432
433(defmethod validate-superclass ((class container-class)
434 (super pcl::standard-class))
435 (subtypep (class-name super) 'container))
436
437
438
439;;;; Metaclass for child classes
440
441(eval-when (:compile-toplevel :load-toplevel :execute)
442 (defclass child-class (virtual-class))
443
444 (defclass direct-child-slot-definition (direct-virtual-slot-definition))
445
446 (defclass effective-child-slot-definition
447 (effective-virtual-slot-definition)))
448
449
450(defmethod initialize-instance ((slotd direct-child-slot-definition)
451 &rest initargs &key)
452 (declare (ignore initargs))
453 (call-next-method)
454 (unless (slot-boundp slotd 'location)
455 (with-slots (pcl::name location pcl::class) slotd
456 (setf
457 location
458 (format nil "~A::~A"
459 (alien-type-name
460 (gethash (class-name pcl::class) *child-to-container-class-mappings*))
461 (name-to-string pcl::name))))))
462
463
464(defmethod direct-slot-definition-class ((class child-class) initargs)
465 (case (getf initargs :allocation)
466 (:arg (find-class 'direct-child-slot-definition))
467 (t (call-next-method))))
468
469
470(defmethod effective-slot-definition-class ((class child-class) initargs)
471 (case (getf initargs :allocation)
472 (:arg (find-class 'effective-child-slot-definition))
473 (t (call-next-method))))
474
475
476(defmethod compute-virtual-slot-location
477 ((class child-class) (slotd effective-child-slot-definition) direct-slotds)
478 (with-slots (type) slotd
479 (let ((location (slot-definition-location (first direct-slotds)))
480 (type-number (find-type-number type))
481 (reader (get-reader-function type))
482 (writer (get-writer-function type))
483 (destroy (get-destroy-function type)))
484 (list
485 #'(lambda (object)
486 (with-slots (parent child) object
487 (with-gc-disabled
488 (let ((arg (arg-new type-number)))
489 (setf (arg-name arg) location)
490 (container-child-get-arg parent child arg)
491 (prog1
492 (funcall reader arg +arg-value-offset+)
493 (arg-free arg t t))))))
494 #'(lambda (value object)
495 (with-slots (parent child) object
496 (with-gc-disabled
497 (let ((arg (arg-new type-number)))
498 (setf (arg-name arg) location)
499 (funcall writer value arg +arg-value-offset+)
500 (container-child-set-arg parent child arg)
501 (funcall destroy arg +arg-value-offset+)
502 (arg-free arg nil)
503 value))))))))
504
505
506(defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
507 (add-method
508 generic-function
509 (make-instance 'standard-method
510 :specializers (list (find-class 'widget))
511 :lambda-list '(widget)
512 :function #'(lambda (args next-methods)
513 (declare (ignore next-methods))
514 (child-slot-value (first args) slot-name)))))
515
516(defmethod pcl::add-writer-method
517 ((class child-class) generic-function slot-name)
518 (add-method
519 generic-function
520 (make-instance 'standard-method
521 :specializers (list (find-class t) (find-class 'widget))
522 :lambda-list '(value widget)
523 :function #'(lambda (args next-methods)
524 (declare (ignore next-methods))
525 (destructuring-bind (value widget) args
526 (setf
527 (child-slot-value widget slot-name)
528 value))))))
529
530
531(defmethod validate-superclass ((class child-class) (super pcl::standard-class))
532 (subtypep (class-name super) 'container-child))
533
534