1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 1999-2001 Espen S. Johnsen <esj@stud.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.10 2001/10/21 23:18:11 espen Exp $
26 ; (defun name-to-string (name)
27 ; (substitute #\_ #\- (string-downcase (string name))))
29 ; (defun string-to-name (name &optional (package "KEYWORD"))
30 ; (intern (substitute #\- #\_ (string-upcase name)) package))
34 ;;;; Superclass for the gtk class hierarchy
36 (eval-when (:compile-toplevel :load-toplevel :execute)
37 (init-types-in-library
38 "/opt/gnome/lib/libgtk-x11-1.3.so"
39 :ignore ("gtk_window_get_type_hint"))
41 (defclass %object (gobject)
43 (:metaclass gobject-class)
44 (:alien-name "GtkObject")))
47 (defmethod shared-initialize ((object %object) names &rest initargs
49 (declare (ignore names))
51 (funcall (proxy-class-copy (class-of object)) nil (proxy-location object)) ; inc ref count before sinking
53 (dolist (signal-definition (get-all initargs :signal))
54 (apply #'signal-connect object signal-definition)))
56 (defmethod initialize-proxy ((object %object) &rest initargs)
57 (declare (ignore initargs))
59 (%object-sink object))
61 (defbinding %object-sink () nil
65 ;;;; Main loop, timeouts and idle functions
67 (declaim (inline events-pending-p main-iteration))
69 (defbinding (events-pending-p "gtk_events_pending") () boolean)
71 (defbinding get-current-event () gdk:event)
73 (defbinding main-do-event () nil
76 (defbinding main () nil)
78 (defbinding main-level () int)
80 (defbinding main-quit () nil)
82 (defbinding main-iteration-do (&optional (blocking t)) boolean
85 (defun main-iterate-all (&rest args)
86 (declare (ignore args))
87 (when (events-pending-p)
88 (main-iteration-do nil)
91 (system:add-fd-handler (gdk:connection-number) :input #'main-iterate-all)
92 (setq lisp::*periodic-polling-function* #'main-iterate-all)
93 (setq lisp::*max-event-to-sec* 0)
94 (setq lisp::*max-event-to-usec* 1000)
98 ;;;; Metaclass for child classes
100 (defvar *container-to-child-class-mappings* (make-hash-table))
102 (eval-when (:compile-toplevel :load-toplevel :execute)
103 (defclass child-class (virtual-slot-class))
105 (defclass direct-child-slot-definition (direct-virtual-slot-definition)
106 ((pname :reader slot-definition-pname)))
108 (defclass effective-child-slot-definition
109 (effective-virtual-slot-definition)))
112 (defmethod shared-initialize ((class child-class) names &rest initargs
114 (declare (ignore initargs))
117 (gethash (find-class (first container)) *container-to-child-class-mappings*)
120 (defmethod initialize-instance ((slotd direct-child-slot-definition)
121 &rest initargs &key pname)
122 (declare (ignore initargs))
125 (setf (slot-value slotd 'pname) pname)
127 (error "Need pname for slot with allocation :property")))
129 (defmethod direct-slot-definition-class ((class child-class) initargs)
130 (case (getf initargs :allocation)
131 (:property (find-class 'direct-child-slot-definition))
132 (t (call-next-method))))
134 (defmethod effective-slot-definition-class ((class child-class) initargs)
135 (case (getf initargs :allocation)
136 (:property (find-class 'effective-child-slot-definition))
137 (t (call-next-method))))
139 (defbinding %container-child-get-property () nil
140 (container container)
142 (property-name string)
145 (defbinding %container-child-set-property () nil
146 (container container)
148 (property-name string)
151 (defmethod compute-virtual-slot-accessors
152 ((class child-class) (slotd effective-child-slot-definition) direct-slotds)
153 (with-slots (type) slotd
154 (let ((pname (slot-definition-pname (first direct-slotds)))
155 (type-number (find-type-number type)))
158 (with-slots (parent child) object
160 (let ((gvalue (gvalue-new type-number)))
161 (%container-child-get-property parent child pname gvalue)
164 (intern-reader-function type)
165 gvalue +gvalue-value-offset+)
166 (gvalue-free gvalue t))))))
167 #'(lambda (value object)
168 (with-slots (parent child) object
170 (let ((gvalue (gvalue-new type-number)))
172 (intern-writer-function type)
173 value gvalue +gvalue-value-offset+)
174 (%container-child-set-property parent child pname gvalue)
176 (intern-destroy-function type)
177 gvalue +gvalue-value-offset+)
178 (gvalue-free gvalue nil)
182 (defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
185 (make-instance 'standard-method
186 :specializers (list (find-class 'widget))
187 :lambda-list '(widget)
188 :function #'(lambda (args next-methods)
189 (declare (ignore next-methods))
190 (child-slot-value (first args) slot-name)))))
192 (defmethod pcl::add-writer-method
193 ((class child-class) generic-function slot-name)
196 (make-instance 'standard-method
197 :specializers (list (find-class t) (find-class 'widget))
198 :lambda-list '(value widget)
199 :function #'(lambda (args next-methods)
200 (declare (ignore next-methods))
201 (destructuring-bind (value widget) args
203 (child-slot-value widget slot-name)
207 (defmethod validate-superclass ((class child-class) (super pcl::standard-class))
208 ;(subtypep (class-name super) 'container-child)
212 (defclass container-child ()
213 ((parent :initarg :parent :type container)
214 (child :initarg :child :type widget)))
219 (defbinding %container-class-list-child-properties () pointer
221 (n-properties unsigned-int :out))
223 (defun query-container-class-child-properties (type-number)
224 (let ((class (type-class-ref type-number)))
225 (multiple-value-bind (array length)
226 (%container-class-list-child-properties class)
228 (map-c-array 'list #'identity array 'param length)
229 (deallocate-memory array)))))
231 (defun default-container-child-name (container-class)
232 (intern (format nil "~A-CHILD" container-class)))
234 (defun expand-container-type (type-number &optional slots)
235 (let* ((class (type-from-number type-number))
236 (super (supertype type-number))
237 (child-class (default-container-child-name class))
238 (expanded-child-slots
241 (with-slots (name flags value-type documentation) param
242 (let* ((slot-name (default-slot-name name))
243 (slot-type (type-from-number value-type #|t|#))
245 (default-slot-accessor class slot-name slot-type)))
247 :allocation :property
251 (member :writable flags)
252 (member :readable flags))
253 (list :accessor accessor))
254 ((member :writable flags)
255 (list :writer `(setf ,accessor)))
256 ((member :readable flags)
257 (list :reader accessor)))
259 (member :construct flags)
260 (member :writable flags))
261 (list :initarg (intern (string slot-name) "KEYWORD")))
263 ,@(when documentation
264 (list :documentation documentation))))))
265 (query-container-class-child-properties type-number))))
267 ,(expand-gobject-type type-number slots)
268 (defclass ,child-class
269 (,(default-container-child-name super))
270 ,expanded-child-slots
271 (:metaclass child-class)
272 (:container ,class)))))
274 (register-derivable-type 'container "GtkContainer" 'expand-container-type)