chiark / gitweb /
Added GET-ALL and PLIST-REMOVE to manipulate plists
[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.8 2001-05-29 15:50:31 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   (call-next-method))
37
38 (defbinding (%gobject-new "g_object_new") () pointer
39   (type type-number)
40   (nil null))
41
42
43
44 ;;;; Parameter stuff
45
46 (defbinding %object-set-property () nil
47   (object gobject)
48   (name string)
49   (value gvalue))
50
51 (defbinding %object-get-property () nil
52   (object gobject)
53   (name string)
54   (value gvalue))
55
56 (defbinding %object-notify () nil
57   (object gobject)
58   (name string))
59
60 (defbinding object-freeze-notify () nil
61   (object gobject))
62
63 (defbinding object-thaw-notify () nil
64   (object gobject))
65
66 (defbinding %object-set-qdata-full () nil
67   (object gobject)
68   (id quark)
69   (data unsigned-long)
70   (destroy-marshal pointer))
71
72
73 ;;;; User data
74
75 (defun (setf object-data) (data object key &key (test #'eq))
76   (%object-set-qdata-full
77    object (quark-from-object key :test test)
78    (register-user-data data) *destroy-notify*)
79   data)
80
81 (defbinding %object-get-qdata () unsigned-long
82   (object gobject)               
83   (id quark))
84
85 (defun object-data (object key &key (test #'eq))
86   (find-user-data
87    (%object-get-qdata object (quark-from-object key :test test))))
88
89
90
91 ;;;; Metaclass used for subclasses of gobject
92
93 (eval-when (:compile-toplevel :load-toplevel :execute)
94   (defclass gobject-class (ginstance-class))
95
96   (defclass direct-gobject-slot-definition (direct-virtual-slot-definition)
97     ((param :reader slot-definition-param)))
98
99   (defclass effective-gobject-slot-definition
100     (effective-virtual-slot-definition)))
101
102   
103
104 ; (defbinding object-class-install-param () nil
105 ;   (class pointer)
106 ;   (id unsigned-int)
107 ;   (parameter parameter))
108
109 ; (defbinding object-class-find-param-spec () parameter
110 ;   (class pointer)
111 ;   (name string))
112
113
114 (defmethod initialize-instance :after ((slotd direct-gobject-slot-definition)
115                                        &rest initargs &key param)
116   (declare (ignore initargs))
117   (when param
118     (setf
119      (slot-value slotd 'param)
120      (signal-name-to-string (slot-definition-name slotd)))))
121
122 (defmethod direct-slot-definition-class ((class gobject-class) initargs)
123   (case (getf initargs :allocation)
124     (:param (find-class 'direct-gobject-slot-definition))
125     (t (call-next-method))))
126
127 (defmethod effective-slot-definition-class ((class gobject-class) initargs)
128   (case (getf initargs :allocation)
129     (:param (find-class 'effective-gobject-slot-definition))
130     (t (call-next-method))))
131
132 (defmethod compute-virtual-slot-accessors
133     ((class gobject-class) (slotd effective-gobject-slot-definition)
134      direct-slotds)
135   (with-slots (type) slotd
136     (let ((param-name (slot-definition-param (first direct-slotds)))
137           (type-number (find-type-number type)))
138       (list
139        #'(lambda (object)
140            (with-gc-disabled
141              (let ((gvalue (gvalue-new type-number)))
142                (%object-get-property object param-name gvalue)
143                (prog1
144                    (funcall
145                     (intern-reader-function type) gvalue +gvalue-value-offset+)
146                  (gvalue-free gvalue t)))))
147        #'(lambda (value object)
148            (with-gc-disabled
149              (let ((gvalue (gvalue-new type-number)))
150                (funcall
151                 (intern-writer-function type)
152                 value gvalue +gvalue-value-offset+)
153                (%object-set-property object param-name gvalue)
154                (funcall
155                 (intern-destroy-function type)
156                 gvalue +gvalue-value-offset+)
157                (gvalue-free gvalue nil)
158                value)))))))
159
160 (defmethod validate-superclass ((class gobject-class)
161                                 (super pcl::standard-class))
162 ;  (subtypep (class-name super) 'gobject)
163   t)
164
165
166
167 ;;;;
168
169 (defbinding %object-class-properties () pointer
170   (class pointer)
171   (n-properties unsigned-int :out))
172
173 (defun query-object-class-properties (type-number)
174   (let ((class (type-class-ref type-number)))
175     (multiple-value-bind (array length)
176         (%object-class-properties class)
177       (map-c-array 'list #'identity array 'param length))))
178
179 (defun query-object-type-dependencies (type-number)
180   (delete-duplicates
181    (reduce
182     #'nconc
183     (mapcar
184      #'(lambda (param)
185          ;; A gobject does not depend on it's supertypes due to forward
186          ;; referenced superclasses
187          (delete-if
188           #'(lambda (type)
189               (type-is-p type-number type))
190           (type-hierarchy (param-type param))))
191      (query-object-class-properties type-number)))))
192
193
194 (defun default-slot-name (name)
195   (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
196
197 (defun default-slot-accessor (class-name slot-name type)
198   (intern
199    (format
200     nil "~A-~A~A" class-name slot-name
201     (if (eq 'boolean type) "-P" ""))))
202
203 (defun expand-gobject-type (type-number &optional slots
204                             (metaclass 'gobject-class))
205   (let* ((super (supertype type-number))
206          (class  (type-from-number type-number))
207          (expanded-slots
208           (mapcar
209            #'(lambda (param)
210                (with-slots (name flags type documentation) param
211                  (let* ((slot-name (default-slot-name name))
212                         (slot-type (type-from-number type #|t|#))
213                         (accessor
214                          (default-slot-accessor class slot-name slot-type)))
215                    `(,slot-name
216                      :allocation :param
217                      :param ,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     `(defclass ,class (,super)
237        ,expanded-slots
238        (:metaclass ,metaclass)
239        (:alien-name ,(find-type-name type-number)))))
240     
241 (register-derivable-type
242  'gobject "GObject"
243  :query 'query-object-type-dependencies
244  :expand 'expand-gobject-type)
245