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