chiark / gitweb /
Updated for glib-1.3.4
[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.5 2001-04-29 20:17:27 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 (define-foreign ("g_object_new" %gobject-new) () gobject
39   (type type-number)
40   (nil null))
41
42
43 ;;;; Parameter stuff
44
45 (define-foreign %object-set-property () nil
46   (object gobject)
47   (name string)
48   (value gvalue))
49
50 (define-foreign %object-get-property () nil
51   (object gobject)
52   (name string)
53   (value gvalue))
54
55 (define-foreign %object-notify () nil
56   (object gobject)
57   (name string))
58
59 (define-foreign object-freeze-notify () nil
60   (object gobject))
61
62 (define-foreign object-thaw-notify () nil
63   (object gobject))
64
65 (define-foreign %object-set-qdata-full () nil
66   (object gobject)
67   (id quark)
68   (data unsigned-long)
69   (destroy-marshal pointer))
70
71
72 ;;;; User data
73
74 (defun (setf object-data) (data object key &key (test #'eq))
75   (%object-set-qdata-full
76    object (quark-from-object key :test test)
77    (register-user-data data) *destroy-notify*)
78   data)
79
80 (define-foreign %object-get-qdata () unsigned-long
81   (object gobject)               
82   (id quark))
83
84 (defun object-data (object key &key (test #'eq))
85   (find-user-data
86    (%object-get-qdata object (quark-from-object key :test test))))
87
88
89
90 ;;;; Metaclass used for subclasses of gobject
91
92 (eval-when (:compile-toplevel :load-toplevel :execute)
93   (defclass gobject-class (ginstance-class))
94
95   (defclass direct-gobject-slot-definition (direct-virtual-slot-definition))
96
97   (defclass effective-gobject-slot-definition
98     (effective-virtual-slot-definition)))
99
100
101 ; (define-foreign object-class-install-param () nil
102 ;   (class pointer)
103 ;   (id unsigned-int)
104 ;   (parameter parameter))
105
106 ; (define-foreign object-class-find-param-spec () parameter
107 ;   (class pointer)
108 ;   (name string))
109
110
111 (defmethod initialize-instance :after ((slotd direct-gobject-slot-definition)
112                                        &rest initargs &key)
113   (declare (ignore initargs))
114   (unless (slot-boundp slotd 'location)
115     ;; Find parameter name from slot name
116     (with-slots (pcl::name location) slotd
117       (setf location (signal-name-to-string pcl::name)))))
118
119 (defmethod direct-slot-definition-class ((class gobject-class) initargs)
120   (case (getf initargs :allocation)
121     (:param (find-class 'direct-gobject-slot-definition))
122     (t (call-next-method))))
123
124 (defmethod effective-slot-definition-class ((class gobject-class) initargs)
125   (case (getf initargs :allocation)
126     (:param (find-class 'effective-gobject-slot-definition))
127     (t (call-next-method))))
128
129 (defmethod compute-virtual-slot-location
130     ((class gobject-class) (slotd effective-gobject-slot-definition)
131      direct-slotds)
132   (with-slots (type) slotd
133     (let ((param-name (slot-definition-location (first direct-slotds)))
134           (type-number (find-type-number type))
135           (reader (intern-reader-function type))
136           (writer (intern-writer-function type))
137           (destroy (intern-destroy-function 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 reader gvalue +gvalue-value-offset+)
145                  (gvalue-free gvalue t)))))
146        #'(lambda (value object)
147            (with-gc-disabled
148              (let ((gvalue (gvalue-new type-number)))
149                (funcall writer value gvalue +gvalue-value-offset+)
150                (%object-set-property object param-name gvalue)
151                (funcall destroy gvalue +gvalue-value-offset+)
152                (gvalue-free gvalue nil)
153                value)))))))
154
155
156 (defmethod validate-superclass ((class gobject-class)
157                                 (super pcl::standard-class))
158   (subtypep (class-name super) 'gobject))
159
160
161 ;;;;
162
163 ; (defmacro defclass-by-query (name)
164 ;   (destructuring-bind (lisp-name alien-name) name
165