Update to match changes in gtype.lisp
[clg] / glib / gboxed.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 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: gboxed.lisp,v 1.6 2001-10-21 22:02:01 espen Exp $
19
20 (in-package "GLIB")
21
22
23 (eval-when (:compile-toplevel :load-toplevel :execute)
24 (defclass boxed (proxy)
25 ()
26 (:metaclass proxy-class)
27 (:copy %boxed-copy)
28 (:free %boxed-free)))
29
30 (defbinding %boxed-copy (type location) pointer
31 ((find-type-number type) type-number)
32 (location pointer))
33
34 (defbinding %boxed-free (type location) nil
35 ((find-type-number type) type-number)
36 (location pointer))
37
38
39 ;;;; Metaclass for boxed classes
40
41 (eval-when (:compile-toplevel :load-toplevel :execute)
42 (defclass boxed-class (proxy-class))
43
44
45 (defmethod shared-initialize ((class boxed-class) names
46 &rest initargs &key name alien-name)
47 (declare (ignore initargs names))
48 (call-next-method)
49
50 (let* ((class-name (or name (class-name class)))
51 (type-number
52 (find-type-number
53 (or (first alien-name) (default-alien-type-name class-name)))))
54 (register-type class-name type-number)))
55
56
57 (defmethod validate-superclass
58 ((class boxed-class) (super pcl::standard-class))
59 (subtypep (class-name super) 'boxed)))
60
61
62 ;;;;
63
64 (defun expand-boxed-type (type-number &optional slots)
65 `(defclass ,(type-from-number type-number) (boxed)
66 ,slots
67 (:metaclass boxed-class)
68 (:alien-name ,(find-type-name type-number))))
69
70 (register-derivable-type 'boxed "GBoxed" 'expand-boxed-type)
71
72 ;;;; Special boxed types
73
74 (defclass gstring (boxed)
75 ()
76 (:metaclass boxed-class)
77 (:alien-name "GString"))
78
79 (deftype-method translate-from-alien
80 gstring (type-spec location &optional weak-ref)
81 `(let ((location ,location))
82 (unless (null-pointer-p location)
83 (prog1
84 (c-call::%naturalize-c-string location)
85 ,(unless weak-ref
86 (unreference-alien type-spec location))))))
87
88 (deftype-method translate-to-alien
89 gstring (type-spec string &optional weak-ref)
90 (declare (ignore type-spec weak-ref))
91 `(let ((string ,string))
92 ;; Always copy strings to prevent seg fault due to GC
93 (funcall
94 ',(proxy-class-copy (find-class type-spec))
95 ',type-spec
96 (make-pointer (1+ (kernel:get-lisp-obj-address string))))))
97
98 (deftype-method cleanup-alien gstring (type-spec c-string &optional weak-ref)
99 (when weak-ref
100 (unreference-alien type-spec c-string)))
101