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.6 2000-09-04 22:14:54 espen Exp $
26 (setf (alien-type-name 'pointer) "gpointer")
31 (defun name-to-string (name)
32 (substitute #\_ #\- (string-downcase (string name))))
34 (defun string-to-name (name &optional (package "KEYWORD"))
35 (intern (substitute #\- #\_ (string-upcase name)) package))
40 (deftype arg () 'pointer)
42 (defconstant +arg-type-offset+ 0)
43 (defconstant +arg-name-offset+ 4)
44 (defconstant +arg-value-offset+ 8)
45 (defconstant +arg-size+ 16)
47 (define-foreign arg-new () arg
50 (define-foreign %arg-free () nil
52 (free-contents boolean))
54 (defun arg-free (arg free-contents &optional alien)
56 (alien (%arg-free arg free-contents))
58 (unless (null-pointer-p arg)
61 (get-destroy-function (type-from-number (arg-type arg)))
62 arg +arg-value-offset+))
63 (deallocate-memory arg)))))
65 (define-foreign %arg-reset () nil
69 (funcall (get-reader-function '(static string)) arg +arg-name-offset+))
71 (defun (setf arg-name) (name arg)
72 (funcall (get-writer-function '(static string)) name arg +arg-name-offset+)
76 (system:sap-ref-32 arg +arg-type-offset+))
78 (defun (setf arg-type) (type arg)
79 (setf (system:sap-ref-32 arg +arg-type-offset+) type))
81 (defun arg-value (arg &optional (type (type-from-number (arg-type arg))))
82 (funcall (get-reader-function type) arg +arg-value-offset+))
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))))
87 (funcall (get-writer-function type) value arg +arg-value-offset+)
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)
96 (defun arg-array-ref (arg0 index)
97 (system:sap+ arg0 (* index +arg-size+)))
100 ;;;; Superclass for the gtk class hierarchy
102 (eval-when (:compile-toplevel :load-toplevel :execute)
103 (defclass object (gobject)
107 ; :accessor object-flags
108 ; :type object-flags))
109 (:metaclass gobject-class)
110 (:alien-name "GtkObject")))
113 (defmethod shared-initialize ((object object) names &rest initargs &key signals)
114 (declare (ignore initargs names))
116 (dolist (signal signals)
117 (apply #'signal-connect object signal)))
120 (defmethod initialize-instance :after ((object object) &rest initargs &key)
121 (declare (ignore initargs))
122 (object-default-construct object)
123 (reference-instance object)
124 (object-sink object))
127 (defmethod from-alien-initialzie-instance ((object object) &rest initargs)
128 (declare (ignore initargs))
130 (object-sink object))
133 (define-foreign object-default-construct () nil
136 (define-foreign object-sink () nil
139 (define-foreign ("gtk_object_getv" object-get-arg) () nil
144 (define-foreign ("gtk_object_setv" object-set-arg) () nil
149 (defun object-arg (object name)
151 (let ((arg (arg-new 0)))
152 (setf (arg-name arg) name)
153 (object-get-arg object arg)
154 (let ((type (type-from-number (arg-type arg))))
157 (arg-free arg t))))))
159 (defun (setf object-arg) (value object name)
161 (let ((arg (arg-new 0)))
162 (setf (arg-name arg) name)
163 (object-get-arg object arg)
164 (let* ((type-number (arg-type arg))
165 (type (type-from-number type-number)))
167 (setf (arg-type arg) type-number)
168 (setf (arg-value arg type) value)
169 (object-set-arg object arg)
174 ;;;; Callback mechanism
176 (defun register-callback-function (function)
177 (check-type function (or null symbol function))
178 ; We treat callbacks just as ordinary user data
179 (register-user-data function))
181 (defun callback-trampoline (callback-id nargs arg-array)
182 (declare (fixnum callback-id nargs))
183 (let* ((return-arg (unless (null-pointer-p arg-array)
184 (arg-array-ref arg-array nargs)))
185 (return-type (if return-arg
186 (type-from-number (arg-type return-arg))
189 (callback-function (find-user-data callback-id)))
192 (push (arg-value (arg-array-ref arg-array (- nargs n 1))) args))
194 (labels ((invoke-callback ()
197 (let ((return-value (apply callback-function args)))
199 (setf (return-arg-value return-arg) return-value))))
201 (continue nil :report "Return from callback function"
205 "Enter return value of type ~S: "
207 (force-output *query-io*)
209 (return-arg-value return-arg)
210 (eval (read *query-io*)))))
211 (re-invoke nil :report "Re-invoke callback function"
212 (invoke-callback)))))
215 (defvar *callback-marshal* (system:foreign-symbol-address "callback_marshal"))
216 (setq *destroy-marshal* (system:foreign-symbol-address "destroy_marshal"))
218 (defun after-gc-hook ()
220 (extern-alien "callback_trampoline" system-area-pointer)
221 (make-pointer (kernel:get-lisp-obj-address #'callback-trampoline))
222 (extern-alien "destroy_user_data" system-area-pointer)
223 (make-pointer (kernel:get-lisp-obj-address #'destroy-user-data))))
225 (pushnew 'after-gc-hook ext:*after-gc-hooks*)
230 ;;;; Main loop, timeouts and idle functions
232 (declaim (inline events-pending-p main-iteration))
234 (define-foreign ("gtk_events_pending" events-pending-p) () boolean)
236 (define-foreign get-current-event () gdk:event)
238 (define-foreign main-do-event () nil
241 (define-foreign main () nil)
243 (define-foreign main-level () int)
245 (define-foreign main-quit () nil)
248 ("gtk_main_iteration_do" main-iteration) (&optional (blocking t)) boolean
251 (defun main-iterate-all (&rest args)
252 (declare (ignore args))
253 (when (events-pending-p)
257 (define-foreign ("gtk_timeout_add_full" timeout-add)
258 (interval function) unsigned-int
259 (interval (unsigned 32))
261 (*callback-marshal* pointer)
262 ((register-callback-function function) unsigned-long)
263 (*destroy-marshal* pointer))
265 (define-foreign timeout-remove () nil
266 (timeout-handler-id unsigned-int))
268 (define-foreign ("gtk_idle_add_full" idle-add)
269 (function &optional (priority 200)) unsigned-int
272 (*callback-marshal* pointer)
273 ((register-callback-function function) unsigned-long)
274 (*destroy-marshal* pointer))
276 (define-foreign idle-remove () nil
277 (idle-handler-id unsigned-int))
280 (system:add-fd-handler (gdk:event-poll-fd) :input #'main-iterate-all)
281 (setq lisp::*periodic-polling-function* #'main-iterate-all)
282 (setq lisp::*max-event-to-sec* 0)
283 (setq lisp::*max-event-to-usec* 1000)
289 (define-foreign %signal-emit-stop () nil
291 (signal-id unsigned-int))
293 (define-foreign %signal-emit-stop-by-name (object signal) nil
295 ((name-to-string signal) string))
297 (defun signal-emit-stop (object signal)
299 (%signal-emit-stop object signal)
300 (%signal-emit-stop-by-name object signal)))
302 (define-foreign %signal-connect-full
303 (object signal function after) unsigned-int
305 ((name-to-string signal) string)
307 (*callback-marshal* pointer)
308 ((register-callback-function function) unsigned-long)
309 (*destroy-marshal* pointer)
313 (defun signal-connect (object signal function
314 &key after ((:object callback-object)))
315 (let* ((callback-object (if (eq callback-object t)
320 #'(lambda (&rest args) (apply function callback-object args))
322 (%signal-connect-full object signal callback-function after)))
324 (define-foreign signal-disconnect () nil
326 (handler unsigned-int))
328 (define-foreign signal-handler-block () nil
330 (handler unsigned-int))
332 (define-foreign signal-handler-unblock () nil
334 (handler unsigned-int))
337 ;;;; Metaclass used for subclasses of object
339 (eval-when (:compile-toplevel :load-toplevel :execute)
340 (defclass object-class (gobject-class))
342 (defclass direct-object-slot-definition (direct-virtual-slot-definition))
344 (defclass effective-object-slot-definition
345 (effective-virtual-slot-definition)))
348 (defmethod initialize-instance :after ((slotd direct-object-slot-definition)
350 (declare (ignore initargs))
351 (unless (slot-boundp slotd 'location)
352 (with-slots (pcl::name location pcl::class) slotd
356 (alien-type-name (class-name pcl::class))
357 (name-to-string pcl::name))))))
360 (defmethod direct-slot-definition-class ((class object-class) initargs)
361 (case (getf initargs :allocation)
362 (:arg (find-class 'direct-object-slot-definition))
363 (t (call-next-method))))
366 (defmethod effective-slot-definition-class ((class object-class) initargs)
367 (case (getf initargs :allocation)
368 (:arg (find-class 'effective-object-slot-definition))
369 (t (call-next-method))))
372 (defmethod compute-virtual-slot-location
373 ((class object-class) (slotd effective-object-slot-definition)
375 (with-slots (type) slotd
376 (let ((location (slot-definition-location (first direct-slotds)))
377 (type-number (find-type-number type))
378 (reader (get-reader-function type))
379 (writer (get-writer-function type))
380 (destroy (get-destroy-function type)))
384 (let ((arg (arg-new type-number)))
385 (setf (arg-name arg) location)
386 (object-get-arg object arg)
388 (funcall reader arg +arg-value-offset+)
389 (arg-free arg t t)))))
390 #'(lambda (value object)
392 (let ((arg (arg-new type-number)))
393 (setf (arg-name arg) location)
394 (funcall writer value arg +arg-value-offset+)
395 (object-set-arg object arg)
396 (funcall destroy arg +arg-value-offset+)
401 (defmethod validate-superclass ((class object-class)
402 (super pcl::standard-class))
403 (subtypep (class-name super) 'object))
406 ;;;; Metaclasses used for widgets and containers
408 (eval-when (:compile-toplevel :load-toplevel :execute)
409 (defclass widget-class (object-class))
411 (defclass container-class (widget-class)
415 (defvar *child-to-container-class-mappings* (make-hash-table))
417 (defmethod shared-initialize ((class container-class) names
418 &rest initargs &key name child-class)
419 (declare (ignore initargs))
421 (with-slots ((child-class-slot child-class)) class
426 (intern (format nil "~A-CHILD" (or name (class-name class)))))
427 (gethash child-class-slot *child-to-container-class-mappings*)
431 (defmethod validate-superclass ((class widget-class)
432 (super pcl::standard-class))
433 (subtypep (class-name super) 'widget))
435 (defmethod validate-superclass ((class container-class)
436 (super pcl::standard-class))
437 (subtypep (class-name super) 'container))
441 ;;;; Metaclass for child classes
443 (eval-when (:compile-toplevel :load-toplevel :execute)
444 (defclass child-class (virtual-class))
446 (defclass direct-child-slot-definition (direct-virtual-slot-definition))
448 (defclass effective-child-slot-definition
449 (effective-virtual-slot-definition)))
452 (defmethod initialize-instance ((slotd direct-child-slot-definition)
454 (declare (ignore initargs))
456 (unless (slot-boundp slotd 'location)
457 (with-slots (pcl::name location pcl::class) slotd
462 (gethash (class-name pcl::class) *child-to-container-class-mappings*))
463 (name-to-string pcl::name))))))
466 (defmethod direct-slot-definition-class ((class child-class) initargs)
467 (case (getf initargs :allocation)
468 (:arg (find-class 'direct-child-slot-definition))
469 (t (call-next-method))))
472 (defmethod effective-slot-definition-class ((class child-class) initargs)
473 (case (getf initargs :allocation)
474 (:arg (find-class 'effective-child-slot-definition))
475 (t (call-next-method))))
478 (defmethod compute-virtual-slot-location
479 ((class child-class) (slotd effective-child-slot-definition) direct-slotds)
480 (with-slots (type) slotd
481 (let ((location (slot-definition-location (first direct-slotds)))
482 (type-number (find-type-number type))
483 (reader (get-reader-function type))
484 (writer (get-writer-function type))
485 (destroy (get-destroy-function type)))
488 (with-slots (parent child) object
490 (let ((arg (arg-new type-number)))
491 (setf (arg-name arg) location)
492 (container-child-get-arg parent child arg)
494 (funcall reader arg +arg-value-offset+)
495 (arg-free arg t t))))))
496 #'(lambda (value object)
497 (with-slots (parent child) object
499 (let ((arg (arg-new type-number)))
500 (setf (arg-name arg) location)
501 (funcall writer value arg +arg-value-offset+)
502 (container-child-set-arg parent child arg)
503 (funcall destroy arg +arg-value-offset+)
508 (defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
511 (make-instance 'standard-method
512 :specializers (list (find-class 'widget))
513 :lambda-list '(widget)
514 :function #'(lambda (args next-methods)
515 (declare (ignore next-methods))
516 (child-slot-value (first args) slot-name)))))
518 (defmethod pcl::add-writer-method
519 ((class child-class) generic-function slot-name)
522 (make-instance 'standard-method
523 :specializers (list (find-class t) (find-class 'widget))
524 :lambda-list '(value widget)
525 :function #'(lambda (args next-methods)
526 (declare (ignore next-methods))
527 (destructuring-bind (value widget) args
529 (child-slot-value widget slot-name)
533 (defmethod validate-superclass ((class child-class) (super pcl::standard-class))
534 (subtypep (class-name super) 'container-child))