chiark / gitweb /
056d41f4c051f5610a9695a80af11945d6f5c222
[clg] / gtk / gtkobject.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 1999-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
3 ;;
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.
8 ;;
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.
13 ;;
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
17
18 ;; $Id: gtkobject.lisp,v 1.10 2001-10-21 23:18:11 espen Exp $
19
20
21 (in-package "GTK")
22
23
24 ;;;; Misc utils
25
26 ; (defun name-to-string (name)
27 ;   (substitute #\_ #\- (string-downcase (string name))))
28
29 ; (defun string-to-name (name &optional (package "KEYWORD"))
30 ;   (intern (substitute #\- #\_ (string-upcase name)) package))
31
32
33
34 ;;;; Superclass for the gtk class hierarchy
35
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"))
40
41   (defclass %object (gobject)
42     ()
43     (:metaclass gobject-class)
44     (:alien-name "GtkObject")))
45
46
47 (defmethod shared-initialize ((object %object) names &rest initargs
48                               &allow-other-keys)
49   (declare (ignore names))
50   (call-next-method)
51   (funcall (proxy-class-copy (class-of object)) nil (proxy-location object)) ; inc ref count before sinking
52   (%object-sink object)
53   (dolist (signal-definition (get-all initargs :signal))
54     (apply #'signal-connect object signal-definition)))
55
56 (defmethod initialize-proxy ((object %object) &rest initargs)
57   (declare (ignore initargs))
58   (call-next-method)
59   (%object-sink object))
60
61 (defbinding %object-sink () nil
62   (object %object))
63
64
65 ;;;; Main loop, timeouts and idle functions
66
67 (declaim (inline events-pending-p main-iteration))
68
69 (defbinding (events-pending-p "gtk_events_pending") () boolean)
70
71 (defbinding get-current-event () gdk:event)
72
73 (defbinding main-do-event () nil
74   (event gdk:event))
75
76 (defbinding main () nil)
77
78 (defbinding main-level () int)
79
80 (defbinding main-quit () nil)
81
82 (defbinding main-iteration-do (&optional (blocking t)) boolean
83   (blocking boolean))
84
85 (defun main-iterate-all (&rest args)
86   (declare (ignore args))
87   (when (events-pending-p)
88     (main-iteration-do nil)
89     (main-iterate-all)))
90
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)
95
96
97
98 ;;;; Metaclass for child classes
99  
100 (defvar *container-to-child-class-mappings* (make-hash-table))
101
102 (eval-when (:compile-toplevel :load-toplevel :execute)
103   (defclass child-class (virtual-slot-class))
104
105   (defclass direct-child-slot-definition (direct-virtual-slot-definition)
106     ((pname :reader slot-definition-pname)))
107
108   (defclass effective-child-slot-definition
109     (effective-virtual-slot-definition)))
110
111
112 (defmethod shared-initialize ((class child-class) names &rest initargs
113                               &key container)
114   (declare (ignore initargs))
115   (call-next-method)
116   (setf
117    (gethash (find-class (first container)) *container-to-child-class-mappings*)
118     class))
119
120 (defmethod initialize-instance  ((slotd direct-child-slot-definition)
121                                  &rest initargs &key pname)
122   (declare (ignore initargs))
123   (call-next-method)
124   (if pname
125       (setf (slot-value slotd 'pname) pname)
126     ; ???
127     (error "Need pname for slot with allocation :property")))
128
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))))
133
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))))
138
139 (defbinding %container-child-get-property () nil
140   (container container)
141   (child widget)
142   (property-name string)
143   (value gvalue))
144
145 (defbinding %container-child-set-property () nil
146   (container container)
147   (child widget)
148   (property-name string)
149   (value gvalue))
150   
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)))
156       (list
157        #'(lambda (object)
158            (with-slots (parent child) object       
159              (with-gc-disabled
160                (let ((gvalue (gvalue-new type-number)))
161                  (%container-child-get-property parent child pname gvalue)
162                  (unwind-protect
163                      (funcall
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       
169              (with-gc-disabled
170               (let ((gvalue (gvalue-new type-number)))
171                 (funcall
172                  (intern-writer-function type)
173                  value gvalue +gvalue-value-offset+)
174                 (%container-child-set-property parent child pname gvalue)
175                 (funcall
176                  (intern-destroy-function type)
177                  gvalue +gvalue-value-offset+)
178                 (gvalue-free gvalue nil)
179                 value))))))))
180
181
182 (defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
183   (add-method
184    generic-function
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)))))
191
192 (defmethod pcl::add-writer-method
193     ((class child-class) generic-function slot-name)
194   (add-method
195    generic-function
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
202                                   (setf
203                                    (child-slot-value widget slot-name)
204                                    value))))))
205
206
207 (defmethod validate-superclass ((class child-class) (super pcl::standard-class))
208   ;(subtypep (class-name super) 'container-child)
209   t)
210
211
212 (defclass container-child ()
213   ((parent :initarg :parent :type container)
214    (child :initarg :child :type widget)))
215
216
217 ;;;;
218
219 (defbinding %container-class-list-child-properties () pointer
220   (class pointer)
221   (n-properties unsigned-int :out))
222
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)
227       (unwind-protect
228           (map-c-array 'list #'identity array 'param length)
229         (deallocate-memory array)))))
230
231 (defun default-container-child-name (container-class)
232   (intern (format nil "~A-CHILD" container-class)))
233
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
239           (mapcar
240            #'(lambda (param)
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|#))
244                         (accessor
245                          (default-slot-accessor class slot-name slot-type)))
246                    `(,slot-name
247                      :allocation :property
248                      :pname ,name
249                      ,@(cond
250                         ((and
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)))
258                      ,@(when (or
259                               (member :construct flags)
260                               (member :writable flags))
261                          (list :initarg (intern (string slot-name) "KEYWORD")))
262                      :type ,slot-type
263                      ,@(when documentation
264                          (list :documentation documentation))))))
265            (query-container-class-child-properties type-number))))
266     `(progn
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)))))
273
274 (register-derivable-type 'container "GtkContainer" 'expand-container-type)