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.4 2000-08-16 22:16:23 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-function 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-number (arg-type arg))
159 (type (type-from-number type-number)))
161 (setf (arg-type arg) type-number)
162 (setf (arg-value arg type) value)
163 (object-set-arg object arg)
168 ;;;; Callback and user data mechanism
170 (declaim (fixnum *user-data-count*))
172 (defvar *user-data* (make-hash-table))
173 (defvar *user-data-count* 0)
175 (defun register-user-data (object &optional destroy-function)
176 (check-type destroy-function (or null symbol function))
177 ; (incf *user-data-count*)
178 (setq *user-data-count* (the fixnum (1+ *user-data-count*)))
180 (gethash *user-data-count* *user-data*)
181 (cons object destroy-function))
185 (defun find-user-data (id)
186 (check-type id fixnum)
187 (multiple-value-bind (user-data p) (gethash id *user-data*)
188 (values (car user-data) p)))
191 (defun register-callback-function (function)
192 (check-type function (or null symbol function))
193 ; We treat callbacks just as ordinary user data
194 (register-user-data function))
197 (defun callback-trampoline (callback-id nargs arg-array)
198 (declare (fixnum callback-id nargs))
199 (let* ((return-arg (unless (null-pointer-p arg-array)
200 (arg-array-ref arg-array nargs)))
201 (return-type (if return-arg
202 (type-from-number (arg-type return-arg))
205 (callback-function (find-user-data callback-id)))
208 (push (arg-value (arg-array-ref arg-array (- nargs n 1))) args))
210 (labels ((invoke-callback ()
213 (let ((return-value (apply callback-function args)))
215 (setf (return-arg-value return-arg) return-value))))
217 (continue nil :report "Return from callback function"
221 "Enter return value of type ~S: "
223 (force-output *query-io*)
225 (return-arg-value return-arg)
226 (eval (read *query-io*)))))
227 (re-invoke nil :report "Re-invoke callback function"
228 (invoke-callback)))))
232 (defun destroy-user-data (id)
233 (check-type id fixnum)
234 (let ((user-data (gethash id *user-data*)))
235 (when (cdr user-data)
236 (funcall (cdr user-data) (car user-data))))
237 (remhash id *user-data*))
240 (defvar *callback-marshal* (system:foreign-symbol-address "callback_marshal"))
241 (defvar *destroy-marshal* (system:foreign-symbol-address "destroy_marshal"))
243 (defun after-gc-hook ()
245 (extern-alien "callback_trampoline" system-area-pointer)
246 (make-pointer (kernel:get-lisp-obj-address #'callback-trampoline))
247 (extern-alien "destroy_user_data" system-area-pointer)
248 (make-pointer (kernel:get-lisp-obj-address #'destroy-user-data))))
250 (pushnew 'after-gc-hook ext:*after-gc-hooks*)
255 ;;;; Main loop, timeouts and idle functions
257 (declaim (inline events-pending-p main-iteration))
259 (define-foreign ("gtk_events_pending" events-pending-p) () boolean)
261 (define-foreign get-current-event () gdk:event)
263 (define-foreign main-do-event () nil
266 (define-foreign main () nil)
268 (define-foreign main-level () int)
270 (define-foreign main-quit () nil)
273 ("gtk_main_iteration_do" main-iteration) (&optional (blocking t)) boolean
276 (defun main-iterate-all (&rest args)
277 (declare (ignore args))
278 (when (events-pending-p)
282 (define-foreign ("gtk_timeout_add_full" timeout-add)
283 (interval function) unsigned-int
284 (interval (unsigned 32))
286 (*callback-marshal* pointer)
287 ((register-callback-function function) unsigned-long)
288 (*destroy-marshal* pointer))
290 (define-foreign timeout-remove () nil
291 (timeout-handler-id unsigned-int))
293 (define-foreign ("gtk_idle_add_full" idle-add)
294 (function &optional (priority 200)) unsigned-int
297 (*callback-marshal* pointer)
298 ((register-callback-function function) unsigned-long)
299 (*destroy-marshal* pointer))
301 (define-foreign idle-remove () nil
302 (idle-handler-id unsigned-int))
305 (system:add-fd-handler (gdk:event-poll-fd) :input #'main-iterate-all)
306 (setq lisp::*periodic-polling-function* #'main-iterate-all)
307 (setq lisp::*max-event-to-sec* 0)
308 (setq lisp::*max-event-to-usec* 1000)
314 (define-foreign %signal-emit-stop () nil
316 (signal-id unsigned-int))
318 (define-foreign %signal-emit-stop-by-name (object signal) nil
320 ((name-to-string signal) string))
322 (defun signal-emit-stop (object signal)
324 (%signal-emit-stop object signal)
325 (%signal-emit-stop-by-name object signal)))
327 (define-foreign %signal-connect-full
328 (object signal function after) unsigned-int
330 ((name-to-string signal) string)
332 (*callback-marshal* pointer)
333 ((register-callback-function function) unsigned-long)
334 (*destroy-marshal* pointer)
338 (defun signal-connect (object signal function
339 &key after ((:object callback-object)))
340 (let* ((callback-object (if (eq callback-object t)
345 #'(lambda (&rest args) (apply function callback-object args))
347 (%signal-connect-full object signal callback-function after)))
349 (define-foreign signal-disconnect () nil
351 (handler unsigned-int))
353 (define-foreign signal-handler-block () nil
355 (handler unsigned-int))
357 (define-foreign signal-handler-unblock () nil
359 (handler unsigned-int))
362 ;;;; Metaclass used for subclasses of object
364 (eval-when (:compile-toplevel :load-toplevel :execute)
365 (defclass object-class (gobject-class))
367 (defclass direct-object-slot-definition (direct-virtual-slot-definition))
369 (defclass effective-object-slot-definition
370 (effective-virtual-slot-definition)))
373 (defmethod initialize-instance :after ((slotd direct-object-slot-definition)
375 (declare (ignore initargs))
376 (unless (slot-boundp slotd 'location)
377 (with-slots (pcl::name location pcl::class) slotd
381 (alien-type-name (class-name pcl::class))
382 (name-to-string pcl::name))))))
385 (defmethod direct-slot-definition-class ((class object-class) initargs)
386 (case (getf initargs :allocation)
387 (:arg (find-class 'direct-object-slot-definition))
388 (t (call-next-method))))
391 (defmethod effective-slot-definition-class ((class object-class) initargs)
392 (case (getf initargs :allocation)
393 (:arg (find-class 'effective-object-slot-definition))
394 (t (call-next-method))))
397 (defmethod compute-virtual-slot-location
398 ((class object-class) (slotd effective-object-slot-definition)
400 (with-slots (type) slotd
401 (let ((location (slot-definition-location (first direct-slotds)))
402 (type-number (find-type-number type))
403 (reader (get-reader-function type))
404 (writer (get-writer-function type))
405 (destroy (get-destroy-function type)))
409 (let ((arg (arg-new type-number)))
410 (setf (arg-name arg) location)
411 (object-get-arg object arg)
413 (funcall reader arg +arg-value-offset+)
414 (arg-free arg t t)))))
415 #'(lambda (value object)
417 (let ((arg (arg-new type-number)))
418 (setf (arg-name arg) location)
419 (funcall writer value arg +arg-value-offset+)
420 (object-set-arg object arg)
421 (funcall destroy arg +arg-value-offset+)
426 (defmethod validate-superclass ((class object-class)
427 (super pcl::standard-class))
428 (subtypep (class-name super) 'object))
431 ;;;; Metaclasses used for widgets and containers
433 (eval-when (:compile-toplevel :load-toplevel :execute)
434 (defclass widget-class (object-class))
436 (defclass container-class (widget-class)
440 (defvar *child-to-container-class-mappings* (make-hash-table))
442 (defmethod shared-initialize ((class container-class) names
443 &rest initargs &key name child-class)
444 (declare (ignore initargs))
446 (with-slots ((child-class-slot child-class)) class
451 (intern (format nil "~A-CHILD" (or name (class-name class)))))
452 (gethash child-class-slot *child-to-container-class-mappings*)
456 (defmethod validate-superclass ((class widget-class)
457 (super pcl::standard-class))
458 (subtypep (class-name super) 'widget))
460 (defmethod validate-superclass ((class container-class)
461 (super pcl::standard-class))
462 (subtypep (class-name super) 'container))
466 ;;;; Metaclass for child classes
468 (eval-when (:compile-toplevel :load-toplevel :execute)
469 (defclass child-class (virtual-class))
471 (defclass direct-child-slot-definition (direct-virtual-slot-definition))
473 (defclass effective-child-slot-definition
474 (effective-virtual-slot-definition)))
477 (defmethod initialize-instance ((slotd direct-child-slot-definition)
479 (declare (ignore initargs))
481 (unless (slot-boundp slotd 'location)
482 (with-slots (pcl::name location pcl::class) slotd
487 (gethash (class-name pcl::class) *child-to-container-class-mappings*))
488 (name-to-string pcl::name))))))
491 (defmethod direct-slot-definition-class ((class child-class) initargs)
492 (case (getf initargs :allocation)
493 (:arg (find-class 'direct-child-slot-definition))
494 (t (call-next-method))))
497 (defmethod effective-slot-definition-class ((class child-class) initargs)
498 (case (getf initargs :allocation)
499 (:arg (find-class 'effective-child-slot-definition))
500 (t (call-next-method))))
503 (defmethod compute-virtual-slot-location
504 ((class child-class) (slotd effective-child-slot-definition) direct-slotds)
505 (with-slots (type) slotd
506 (let ((location (slot-definition-location (first direct-slotds)))
507 (type-number (find-type-number type))
508 (reader (get-reader-function type))
509 (writer (get-writer-function type))
510 (destroy (get-destroy-function type)))
513 (with-slots (parent child) object
515 (let ((arg (arg-new type-number)))
516 (setf (arg-name arg) location)
517 (container-child-get-arg parent child arg)
519 (funcall reader arg +arg-value-offset+)
520 (arg-free arg t t))))))
521 #'(lambda (value object)
522 (with-slots (parent child) object
524 (let ((arg (arg-new type-number)))
525 (setf (arg-name arg) location)
526 (funcall writer value arg +arg-value-offset+)
527 (container-child-set-arg parent child arg)
528 (funcall destroy arg +arg-value-offset+)
533 (defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
536 (make-instance 'standard-method
537 :specializers (list (find-class 'widget))
538 :lambda-list '(widget)
539 :function #'(lambda (args next-methods)
540 (declare (ignore next-methods))
541 (child-slot-value (first args) slot-name)))))
543 (defmethod pcl::add-writer-method
544 ((class child-class) generic-function slot-name)
547 (make-instance 'standard-method
548 :specializers (list (find-class t) (find-class 'widget))
549 :lambda-list '(value widget)
550 :function #'(lambda (args next-methods)
551 (declare (ignore next-methods))
552 (destructuring-bind (value widget) args
554 (child-slot-value widget slot-name)
558 (defmethod validate-superclass ((class child-class) (super pcl::standard-class))
559 (subtypep (class-name super) 'container-child))