1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 1999-2000 Espen S. Johnsen <esj@ostud.cs.uit.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.8 2001/01/28 14:23:38 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))
38 ;;; Argument stuff - to be removed soon
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-sink () nil
137 ;;;; Main loop, timeouts and idle functions
139 (declaim (inline events-pending-p main-iteration))
141 (define-foreign ("gtk_events_pending" events-pending-p) () boolean)
143 (define-foreign get-current-event () gdk:event)
145 (define-foreign main-do-event () nil
148 (define-foreign main () nil)
150 (define-foreign main-level () int)
152 (define-foreign main-quit () nil)
155 ("gtk_main_iteration_do" main-iteration) (&optional (blocking t)) boolean
158 (defun main-iterate-all (&rest args)
159 (declare (ignore args))
160 (when (events-pending-p)
164 ; (define-foreign ("gtk_timeout_add_full" timeout-add)
165 ; (interval function) unsigned-int
166 ; (interval (unsigned 32))
168 ; (*callback-marshal* pointer)
169 ; ((register-callback-function function) unsigned-long)
170 ; (*destroy-marshal* pointer))
172 ; (define-foreign timeout-remove () nil
173 ; (timeout-handler-id unsigned-int))
175 ; (define-foreign ("gtk_idle_add_full" idle-add)
176 ; (function &optional (priority 200)) unsigned-int
179 ; (*callback-marshal* pointer)
180 ; ((register-callback-function function) unsigned-long)
181 ; (*destroy-marshal* pointer))
183 ; (define-foreign idle-remove () nil
184 ; (idle-handler-id unsigned-int))
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)
194 ;;;; Metaclass used for subclasses of object
196 (eval-when (:compile-toplevel :load-toplevel :execute)
197 (defclass object-class (gobject-class)))
200 (defmethod validate-superclass ((class object-class)
201 (super pcl::standard-class))
202 (subtypep (class-name super) 'object))
205 ;;;; Metaclasses used for widgets and containers
207 (eval-when (:compile-toplevel :load-toplevel :execute)
208 (defclass widget-class (object-class))
210 (defclass container-class (widget-class)
214 (defvar *child-to-container-class-mappings* (make-hash-table))
216 (defmethod shared-initialize ((class container-class) names
217 &rest initargs &key name child-class)
218 (declare (ignore initargs))
220 (with-slots ((child-class-slot child-class)) class
225 (intern (format nil "~A-CHILD" (or name (class-name class)))))
226 (gethash child-class-slot *child-to-container-class-mappings*)
230 (defmethod validate-superclass ((class widget-class)
231 (super pcl::standard-class))
232 (subtypep (class-name super) 'widget))
234 (defmethod validate-superclass ((class container-class)
235 (super pcl::standard-class))
236 (subtypep (class-name super) 'container))
240 ;;;; Metaclass for child classes
242 (eval-when (:compile-toplevel :load-toplevel :execute)
243 (defclass child-class (virtual-class))
245 (defclass direct-child-slot-definition (direct-virtual-slot-definition))
247 (defclass effective-child-slot-definition
248 (effective-virtual-slot-definition)))
251 (defmethod initialize-instance ((slotd direct-child-slot-definition)
253 (declare (ignore initargs))
255 (unless (slot-boundp slotd 'location)
256 (with-slots (pcl::name location pcl::class) slotd
261 (gethash (class-name pcl::class) *child-to-container-class-mappings*))
262 (name-to-string pcl::name))))))
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))))
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))))
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)))
287 (with-slots (parent child) object
289 (let ((arg (arg-new type-number)))
290 (setf (arg-name arg) location)
291 (container-child-get-arg parent child arg)
293 (funcall reader arg +arg-value-offset+)
294 (arg-free arg t t))))))
295 #'(lambda (value object)
296 (with-slots (parent child) object
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+)
307 (defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
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)))))
317 (defmethod pcl::add-writer-method
318 ((class child-class) generic-function slot-name)
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
328 (child-slot-value widget slot-name)
332 (defmethod validate-superclass ((class child-class) (super pcl::standard-class))
333 (subtypep (class-name super) 'container-child))