chiark / gitweb /
2a11bce7b3fa6ba47e95e7a1d3a1189516162981
[clg] / glib / gobject.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000-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: gobject.lisp,v 1.10 2002-01-20 14:09:52 espen Exp $
19
20 (in-package "GLIB")
21
22
23 (eval-when (:compile-toplevel :load-toplevel :execute)
24   (defclass gobject (ginstance)
25     ()
26     (:metaclass ginstance-class)
27     (:alien-name "GObject")
28     (:ref "g_object_ref")
29     (:unref "g_object_unref")))
30
31 (defmethod initialize-instance ((object gobject) &rest initargs)
32   (declare (ignore initargs))
33   (setf 
34    (slot-value object 'location)
35    (%gobject-new (type-number-of object)))
36 ;  (funcall (proxy-class-copy (class-of object)) nil (proxy-location object))
37   (call-next-method)
38 ;  (funcall (proxy-class-free (class-of object)) nil (proxy-location object))
39   )
40
41 (defbinding (%gobject-new "g_object_new") () pointer
42   (type type-number)
43   (nil null))
44
45
46
47 ;;;; Property stuff
48
49 (defbinding %object-set-property () nil
50   (object gobject)
51   (name string)
52   (value gvalue))
53
54 (defbinding %object-get-property () nil
55   (object gobject)
56   (name string)
57   (value gvalue))
58
59 (defbinding %object-notify () nil
60   (object gobject)
61   (name string))
62
63 (defbinding object-freeze-notify () nil
64   (object gobject))
65
66 (defbinding object-thaw-notify () nil
67   (object gobject))
68
69 (defbinding %object-set-qdata-full () nil
70   (object gobject)
71   (id quark)
72   (data unsigned-long)
73   (destroy-marshal pointer))
74
75
76 ;;;; User data
77
78 (defun (setf object-data) (data object key &key (test #'eq))
79   (%object-set-qdata-full
80    object (quark-from-object key :test test)
81    (register-user-data data) *destroy-notify*)
82   data)
83
84 (defbinding %object-get-qdata () unsigned-long
85   (object gobject)               
86   (id quark))
87
88 (defun object-data (object key &key (test #'eq))
89   (find-user-data
90    (%object-get-qdata object (quark-from-object key :test test))))
91
92
93
94 ;;;; Metaclass used for subclasses of gobject
95
96 (eval-when (:compile-toplevel :load-toplevel :execute)
97   (defclass gobject-class (ginstance-class))
98
99   (defclass direct-gobject-slot-definition (direct-virtual-slot-definition)
100     ((pname :reader slot-definition-pname)))
101
102   (defclass effective-gobject-slot-definition
103     (effective-virtual-slot-definition)))
104
105   
106
107 ; (defbinding object-class-install-param () nil
108 ;   (class pointer)
109 ;   (id unsigned-int)
110 ;   (parameter parameter))
111
112 ; (defbinding object-class-find-param-spec () parameter
113 ;   (class pointer)
114 ;   (name string))
115
116
117 (defmethod initialize-instance :after ((slotd direct-gobject-slot-definition)
118                                        &rest initargs &key pname)
119   (declare (ignore initargs))
120   (when pname
121     (setf
122      (slot-value slotd 'pname)
123      (signal-name-to-string (slot-definition-name slotd)))))
124
125 (defmethod direct-slot-definition-class ((class gobject-class) initargs)
126   (case (getf initargs :allocation)
127     (:property (find-class 'direct-gobject-slot-definition))
128     (t (call-next-method))))
129
130 (defmethod effective-slot-definition-class ((class gobject-class) initargs)
131   (case (getf initargs :allocation)
132     (:property (find-class 'effective-gobject-slot-definition))
133     (t (call-next-method))))
134
135 (defmethod compute-virtual-slot-accessors
136     ((class gobject-class) (slotd effective-gobject-slot-definition)
137      direct-slotds)
138   (with-slots (type) slotd
139     (let ((pname (slot-definition-pname (first direct-slotds)))
140           (type-number (find-type-number type)))
141       (list
142        #'(lambda (object)
143            (with-gc-disabled
144              (let ((gvalue (gvalue-new type-number)))
145                (%object-get-property object pname gvalue)
146                (unwind-protect
147                    (funcall
148                     (intern-reader-function type) gvalue +gvalue-value-offset+)
149                  (gvalue-free gvalue t)))))
150        #'(lambda (value object)
151            (with-gc-disabled
152              (let ((gvalue (gvalue-new type-number)))
153                (funcall
154                 (intern-writer-function type)
155                 value gvalue +gvalue-value-offset+)
156                (%object-set-property object pname gvalue)
157                (funcall
158                 (intern-destroy-function type)
159                 gvalue +gvalue-value-offset+)
160                (gvalue-free gvalue nil)
161                value)))))))
162
163 (defmethod validate-superclass ((class gobject-class)
164                                 (super pcl::standard-class))
165 ;  (subtypep (class-name super) 'gobject)
166   t)
167
168
169
170 ;;;;
171
172 (defbinding %object-class-list-properties () pointer
173   (class pointer)
174   (n-properties unsigned-int :out))
175
176 (defun query-object-class-properties (type-number &optional
177                                       inherited-properties)
178   (let ((class (type-class-ref type-number)))
179     (multiple-value-bind (array length)
180         (%object-class-list-properties class)
181       (unwind-protect
182           (let ((all-properties
183                  (map-c-array 'list #'identity array 'param length)))
184             (if (not inherited-properties)
185                 (delete-if
186                  #'(lambda (param)
187                      (not (eql type-number (param-owner-type param))))
188                  all-properties)
189               all-properties))
190         (deallocate-memory array)))))
191
192
193 (defun default-slot-name (name)
194   (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
195
196 (defun default-slot-accessor (class-name slot-name type)
197   (intern
198    (format
199     nil "~A-~A~A" class-name slot-name
200     (if (eq 'boolean type) "-P" ""))))
201
202 (defun expand-gobject-type (type-number &optional options
203                             (metaclass 'gobject-class))
204   (let* ((supers (cons (supertype type-number) (implements type-number)))
205          (class  (type-from-number type-number))
206          (override-slots (getf options :slots))
207          (expanded-slots
208           (mapcar
209            #'(lambda (param)
210                (with-slots (name flags value-type documentation) param
211                  (let* ((slot-name (default-slot-name name))
212                         (slot-type (type-from-number value-type #|t|#))
213                         (accessor
214                          (default-slot-accessor class slot-name slot-type)))
215                    `(,slot-name
216                      :allocation :property
217                      :pname ,name
218                      ,@(cond
219                         ((and
220                           (member :writable flags)
221                           (member :readable flags))
222                          (list :accessor accessor))
223                         ((member :writable flags)
224                          (list :writer `(setf ,accessor)))
225                         ((member :readable flags)
226                          (list :reader accessor)))
227                      ,@(when (or
228                               (member :construct flags)
229                               (member :writable flags))
230                          (list :initarg (intern (string slot-name) "KEYWORD")))
231                      :type ,slot-type
232                      ,@(when documentation
233                          (list :documentation documentation))))))
234            (query-object-class-properties type-number))))
235
236     (dolist (slot-def override-slots)
237       (let ((name (car slot-def))
238             (pname (getf (cdr slot-def) :pname)))
239         (setq
240          expanded-slots
241          (delete-if
242           #'(lambda (expanded-slot-def)
243               (or
244                (eq name (car expanded-slot-def))
245                (and
246                 pname
247                 (string= pname (getf (cdr expanded-slot-def) :pname)))))
248           expanded-slots))
249
250         (unless (getf (cdr slot-def) :ignore)
251           (push slot-def expanded-slots))))
252     
253     `(progn
254        (defclass ,class ,supers
255          ,expanded-slots
256          (:metaclass ,metaclass)
257          (:alien-name ,(find-type-name type-number))))))
258
259
260 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)
261