b44caf77 |
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 | |
888d25fb |
18 | ;; $Id: gboxed.lisp,v 1.4 2001/05/29 15:46:17 espen Exp $ |
b44caf77 |
19 | |
20 | (in-package "GLIB") |
21 | |
22 | |
23 | (eval-when (:compile-toplevel :load-toplevel :execute) |
c1854b12 |
24 | (defclass boxed (proxy) |
b44caf77 |
25 | () |
3a935dfa |
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) |
b44caf77 |
32 | (location pointer)) |
33 | |
3a935dfa |
34 | (defbinding %boxed-free (type location) nil |
35 | ((find-type-number type) type-number) |
b44caf77 |
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 |
3a935dfa |
46 | &rest initargs &key name alien-name) |
b44caf77 |
47 | (declare (ignore initargs names)) |
48 | (call-next-method) |
49 | |
50 | (let* ((class-name (or name (class-name class))) |
51 | (type-number |
3a935dfa |
52 | (find-type-number |
53 | (or (first alien-name) (default-alien-type-name class-name))))) |
54 | (register-type class-name type-number))) |
b44caf77 |
55 | |
56 | |
57 | (defmethod validate-superclass |
58 | ((class boxed-class) (super pcl::standard-class)) |
59 | (subtypep (class-name super) 'boxed)) |
60 | |
61 | |
3a935dfa |
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)))) |
b44caf77 |
69 | |
3a935dfa |
70 | (register-derivable-type 'boxed "GBoxed" :expand 'expand-boxed-type) |
888d25fb |
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 | |