1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 1999-2000 Espen S. Johnsen <espejohn@online.no>
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.
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.
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
18 ;; $Id: gtkobject.lisp,v 1.1 2000-08-14 16:44:54 espen Exp $
25 (defun name-to-string (name)
26 (substitute #\_ #\- (string-downcase (string name))))
28 (defun string-to-name (name &optional (package "KEYWORD"))
29 (intern (substitute #\- #\_ (string-upcase name)) package))
34 (deftype arg () 'pointer)
36 (defconstant +arg-type-offset+ 0)
37 (defconstant +arg-name-offset+ 4)
38 (defconstant +arg-value-offset+ 8)
39 (defconstant +arg-size+ 16)
41 (define-foreign arg-new () arg
44 (define-foreign %arg-free () nil
46 (free-contents boolean))
48 (defun arg-free (arg free-contents &optional alien)
50 (alien (%arg-free arg free-contents))
52 (unless (null-pointer-p arg)
55 (get-destroy-function (type-from-number (arg-type arg)))
56 arg +arg-value-offset+))
57 (deallocate-memory arg)))))
59 (define-foreign %arg-reset () nil
63 (funcall (get-reader-function '(static string)) arg +arg-name-offset+))
65 (defun (setf arg-name) (name arg)
66 (funcall (get-writer-function '(static string)) name arg +arg-name-offset+)
70 (system:sap-ref-32 arg +arg-type-offset+))
72 (defun (setf arg-type) (type arg)
73 (setf (system:sap-ref-32 arg +arg-type-offset+) type))
75 (defun arg-value (arg &optional (type (type-from-number (arg-type arg))))
76 (funcall (get-reader-function type) arg +arg-value-offset+))
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+)
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)
90 (defun arg-array-ref (arg0 index)
91 (system:sap+ arg0 (* index +arg-size+)))
94 ;;;; Superclass for the gtk class hierarchy
96 (eval-when (:compile-toplevel :load-toplevel :execute)
97 (defclass object (gobject)
101 ; :accessor object-flags
102 ; :type object-flags))
103 (:metaclass gobject-class)
104 (:alien-name "GtkObject")))
107 (defmethod shared-initialize ((object object) names &rest initargs &key signals)
108 (declare (ignore initargs names))
110 (dolist (signal signals)
111 (apply #'signal-connect object signal)))
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))
121 (defmethod from-alien-initialzie-instance ((object object) &rest initargs)
122 (declare (ignore initargs))
124 (object-sink object))
127 (define-foreign object-default-construct () nil
130 (define-foreign object-sink () nil
133 (define-foreign ("gtk_object_getv" object-get-arg) () nil
138 (define-foreign ("gtk_object_setv" object-set-arg) () nil
143 (defun object-arg (object name)
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))))
151 (arg-free arg t))))))
153 (defun (setf object-arg) (value object name)
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))))
160 (setf (arg-value arg type) value)
161 (object-set-arg object arg)
166 ;;;; Callback and user data mechanism
168 (declaim (fixnum *user-data-count*))
170 (defvar *user-data* (make-hash-table))
171 (defvar *user-data-count* 0)
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*)))
178 (gethash *user-data-count* *user-data*)
179 (cons object destroy-function))
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)))
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))
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))
203 (callback-function (find-user-data callback-id)))
206 (push (arg-value (arg-array-ref arg-array (- nargs n 1))) args))
208 (labels ((invoke-callback ()
211 (let ((return-value (apply callback-function args)))
213 (setf (return-arg-value return-arg) return-value))))
215 (continue nil :report "Return from callback function"
219 "Enter return value of type ~S: "
221 (force-output *query-io*)
223 (return-arg-value return-arg)
224 (eval (read *query-io*)))))
225 (re-invoke nil :report "Re-invoke callback function"
226 (invoke-callback)))))
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*))
238 (defvar *callback-marshal* (system:foreign-symbol-address "callback_marshal"))
239 (defvar *destroy-marshal* (system:foreign-symbol-address "destroy_marshal"))
241 (defun after-gc-hook ()
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))))
248 (pushnew 'after-gc-hook ext:*after-gc-hooks*)
255 (declaim (inline events-pending-p main-iteration))
257 (define-foreign ("gtk_events_pending" events-pending-p) () boolean)
259 (define-foreign main-do-event () nil
262 (define-foreign main () nil)
264 (define-foreign main-level () int)
266 (define-foreign main-quit () nil)
269 ("gtk_main_iteration_do" main-iteration) (&optional (blocking t)) boolean
272 (defun main-iterate-all (&rest args)
273 (declare (ignore args))
274 (when (events-pending-p)
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)
287 (define-foreign %signal-emit-stop () nil
289 (signal-id unsigned-int))
291 (define-foreign %signal-emit-stop-by-name (object signal) nil
293 ((name-to-string signal) string))
295 (defun signal-emit-stop (object signal)
297 (%signal-emit-stop object signal)
298 (%signal-emit-stop-by-name object signal)))
300 (define-foreign %signal-connect-full
301 (object signal function after) unsigned-int
303 ((name-to-string signal) string)
305 (*callback-marshal* pointer)
306 ((register-callback-function function) unsigned-long)
307 (*destroy-marshal* pointer)
311 (defun signal-connect (object signal function
312 &key after ((:object callback-object)))
313 (let* ((callback-object (if (eq callback-object t)
318 #'(lambda (&rest args) (apply function callback-object args))
320 (%signal-connect-full object signal callback-function after)))
322 (define-foreign signal-disconnect () nil
324 (handler unsigned-int))
326 (define-foreign signal-handler-block () nil
328 (handler unsigned-int))
330 (define-foreign signal-handler-unblock () nil
332 (handler unsigned-int))
335 ;;;; Metaclass used for subclasses of object
337 (eval-when (:compile-toplevel :load-toplevel :execute)
338 (defclass object-class (gobject-class))
340 (defclass direct-object-slot-definition (direct-virtual-slot-definition))
342 (defclass effective-object-slot-definition
343 (effective-virtual-slot-definition)))
346 (defmethod initialize-instance :after ((slotd direct-object-slot-definition)
348 (declare (ignore initargs))
349 (unless (slot-boundp slotd 'location)
350 (with-slots (pcl::name location pcl::class) slotd
354 (alien-type-name (class-name pcl::class))
355 (name-to-string pcl::name))))))
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))))
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))))
370 (defmethod compute-virtual-slot-location
371 ((class object-class) (slotd effective-object-slot-definition)
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)))
382 (let ((arg (arg-new type-number)))
383 (setf (arg-name arg) location)
384 (object-get-arg object arg)
386 (funcall reader arg +arg-value-offset+)
387 (arg-free arg t t)))))
388 #'(lambda (value object)
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+)
399 (defmethod validate-superclass ((class object-class)
400 (super pcl::standard-class))
401 (subtypep (class-name super) 'object))
404 ;;;; Metaclasses used for widgets and containers
406 (eval-when (:compile-toplevel :load-toplevel :execute)
407 (defclass widget-class (object-class))
409 (defclass container-class (widget-class)
413 (defvar *child-to-container-class-mappings* (make-hash-table))
415 (defmethod shared-initialize ((class container-class) names
416 &rest initargs &key name child-class)
417 (declare (ignore initargs))
419 (with-slots ((child-class-slot child-class)) class
424 (intern (format nil "~A-CHILD" (or name (class-name class)))))
425 (gethash child-class-slot *child-to-container-class-mappings*)
429 (defmethod validate-superclass ((class widget-class)
430 (super pcl::standard-class))
431 (subtypep (class-name super) 'widget))
433 (defmethod validate-superclass ((class container-class)
434 (super pcl::standard-class))
435 (subtypep (class-name super) 'container))
439 ;;;; Metaclass for child classes
441 (eval-when (:compile-toplevel :load-toplevel :execute)
442 (defclass child-class (virtual-class))
444 (defclass direct-child-slot-definition (direct-virtual-slot-definition))
446 (defclass effective-child-slot-definition
447 (effective-virtual-slot-definition)))
450 (defmethod initialize-instance ((slotd direct-child-slot-definition)
452 (declare (ignore initargs))
454 (unless (slot-boundp slotd 'location)
455 (with-slots (pcl::name location pcl::class) slotd
460 (gethash (class-name pcl::class) *child-to-container-class-mappings*))
461 (name-to-string pcl::name))))))
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))))
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))))
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)))
486 (with-slots (parent child) object
488 (let ((arg (arg-new type-number)))
489 (setf (arg-name arg) location)
490 (container-child-get-arg parent child arg)
492 (funcall reader arg +arg-value-offset+)
493 (arg-free arg t t))))))
494 #'(lambda (value object)
495 (with-slots (parent child) object
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+)
506 (defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
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)))))
516 (defmethod pcl::add-writer-method
517 ((class child-class) generic-function slot-name)
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
527 (child-slot-value widget slot-name)
531 (defmethod validate-superclass ((class child-class) (super pcl::standard-class))
532 (subtypep (class-name super) 'container-child))