3 #+nil(define-g-boxed-class "GdkRectangle" rectangle ()
6 (width :int :initform 0)
7 (height :int :initform 0))
9 (define-foreign-type g-boxed-foreign-type ()
11 :accessor g-boxed-foreign-info
12 :initform (error "info must be specified"))
13 (free-from-foreign :initarg :free-from-foreign
15 :accessor g-boxed-foreign-free-from-foreign)
16 (free-to-foreign :initarg :free-to-foreign
18 :accessor g-boxed-foreign-free-to-foreign)
19 (for-callback :initarg :for-callback
21 :accessor g-boxed-foreign-for-callback))
22 (:actual-type :pointer))
24 (eval-when (:compile-toplevel :load-toplevel :execute)
25 (defstruct g-boxed-info
29 (eval-when (:compile-toplevel :load-toplevel :execute)
30 (defun get-g-boxed-foreign-info (name)
31 (get name 'g-boxed-foreign-info)))
33 (define-parse-method g-boxed-foreign (name &key free-from-foreign free-to-foreign for-callback)
34 (let ((info (get-g-boxed-foreign-info name)))
35 (assert info nil "Unknown foreign GBoxed type ~A" name)
36 (make-instance 'g-boxed-foreign-type
38 :free-from-foreign free-from-foreign
39 :free-to-foreign free-to-foreign
40 :for-callback for-callback)))
42 (eval-when (:load-toplevel :compile-toplevel :execute)
43 (defstruct (g-boxed-cstruct-wrapper-info (:include g-boxed-info))
47 (defmacro define-g-boxed-cstruct (name cstruct-name g-type-name &body slots)
50 ,@(iter (for (name type &key initarg) in slots)
51 (collect (list name initarg))))
52 (defcstruct ,cstruct-name
53 ,@(iter (for (name type &key initarg) in slots)
54 (collect `(,name ,type))))
55 (eval-when (:compile-toplevel :load-toplevel :execute)
56 (setf (get ',name 'g-boxed-foreign-info)
57 (make-g-boxed-cstruct-wrapper-info :name ',name
59 :cstruct ',cstruct-name
60 :slots ',(iter (for (name type &key initarg) in slots)
63 (define-g-boxed-cstruct gdk-rectangle gdk-rectangle-cstruct "GdkRectangle"
66 (width :int :initarg 0)
67 (height :int :initarg 0))
69 (defgeneric create-temporary-native (type proxy)
70 (:documentation "Creates a native structure (or passes a pointer to copy contained in PROXY)
71 that contains the same data that the PROXY contains and returns a pointer to it.
73 This call is always paired by call to FREE-TEMPORARY-NATIVE and calls may be nested."))
75 (defgeneric free-temporary-native (type proxy native-ptr)
76 (:documentation "Frees the native structure that was previously created
77 by CREATE-TEMPORARY-NATIVE for the same PROXY.
79 Also reads data from native structure pointer to by NATIVE-PTR
80 and sets the PROXY to contain the same data.
82 This call is always paired by call to CREATE-TEMPORARY-NATIVE and calls may be nested."))
84 (defgeneric create-proxy-for-native (type native-ptr)
85 (:documentation "Creates a proxy that is initialized by data contained in native
86 structured pointed to by NATIVE-PTR.
88 Created proxy should not be linked to NATIVE-PTR and should have
89 indefinite lifetime (until garbage collector collects it). Specifically,
90 if proxy need a pointer to native structure, it should make a copy of
93 If proxy requires finalization, finalizers should be added."))
95 (defgeneric create-reference-proxy (type native-ptr)
96 (:documentation "Creates a reference proxy for a native structure pointed to by NATIVE-PTR.
98 Reference proxy's lifetime is bound to duration of a callback. When the
99 callback returns the reference proxy is declared invalid and operations on it are errors.
101 This call is always paired by call to FREE-REFERENCE-PROXY and calls will not nest."))
103 (defgeneric free-reference-proxy (type proxy native-ptr)
104 (:documentation "Frees a reference proxy PROXY previously created by call to
105 CREATE-REFERENCE-PROXY. This call should ensure that all changes on PROXY are
106 reflected in native structure pointed to by NATIVE-PTR.
108 After a call to FREE-REFERENCE-PROXY, PROXY is declared invalid and using it is an error,
109 operations on it should signal erros.
111 This call is always paired by call to CREATE-REFERENCE-PROXY."))
113 (defmethod create-temporary-native ((type g-boxed-cstruct-wrapper-info) proxy)
114 (format t "create-temporary-native~%")
115 (let* ((native-structure-type (g-boxed-cstruct-wrapper-info-cstruct type))
116 (native-structure (foreign-alloc native-structure-type)))
117 (iter (for slot in (g-boxed-cstruct-wrapper-info-slots type))
118 (setf (foreign-slot-value native-structure native-structure-type slot)
119 (slot-value proxy slot)))
122 (defmethod free-temporary-native ((type g-boxed-cstruct-wrapper-info) proxy native-structure)
123 (format t "free-temporary-native~%")
124 (let ((native-structure-type (g-boxed-cstruct-wrapper-info-cstruct type)))
125 (iter (for slot in (g-boxed-cstruct-wrapper-info-slots type))
126 (setf (slot-value proxy slot)
127 (foreign-slot-value native-structure native-structure-type slot))))
128 (foreign-free native-structure))
130 (defmethod create-proxy-for-native ((type g-boxed-cstruct-wrapper-info) native-structure)
131 (format t "create-proxy-for-native~%")
132 (let* ((native-structure-type (g-boxed-cstruct-wrapper-info-cstruct type))
133 (proxy (make-instance (g-boxed-info-name type))))
134 (iter (for slot in (g-boxed-cstruct-wrapper-info-slots type))
135 (setf (slot-value proxy slot)
136 (foreign-slot-value native-structure native-structure-type slot)))
139 (defmethod create-reference-proxy ((type g-boxed-cstruct-wrapper-info) native-structure)
140 (format t "create-reference-proxy~%")
141 (create-proxy-for-native type native-structure))
143 (defmethod free-reference-proxy ((type g-boxed-cstruct-wrapper-info) proxy native-structure)
144 (format t "free-reference-proxy~%")
145 (let ((native-structure-type (g-boxed-cstruct-wrapper-info-cstruct type)))
146 (iter (for slot in (g-boxed-cstruct-wrapper-info-slots type))
147 (setf (foreign-slot-value native-structure native-structure-type slot)
148 (slot-value proxy slot)))))
150 (defmethod translate-to-foreign (proxy (type g-boxed-foreign-type))
152 (let* ((info (g-boxed-foreign-info type)))
153 (values (create-temporary-native info proxy) proxy))
156 (defmethod free-translated-object (native-structure (type g-boxed-foreign-type) proxy)
158 (free-temporary-native (g-boxed-foreign-info type) proxy native-structure)))
160 (defmethod translate-from-foreign (native-structure (type g-boxed-foreign-type))
161 (unless (null-pointer-p native-structure)
162 (let* ((info (g-boxed-foreign-info type)))
164 ((g-boxed-foreign-for-callback type)
165 (create-reference-proxy info native-structure))
166 ((or (g-boxed-foreign-free-to-foreign type)
167 (g-boxed-foreign-free-from-foreign type))
168 (error "Feature not yet handled"))
169 (t (create-proxy-for-native info native-structure))))))
171 (defmethod cleanup-translated-object-for-callback ((type g-boxed-foreign-type) proxy native-structure)
172 (unless (null-pointer-p native-structure)
173 (free-reference-proxy (g-boxed-foreign-info type) proxy native-structure)))
175 (defmethod has-callback-cleanup ((type g-boxed-foreign-type))
178 (defcallback incf-rectangle :void ((rectangle (g-boxed-foreign gdk-rectangle :for-callback t))
180 (incf (gdk-rectangle-x rectangle) delta)
181 (incf (gdk-rectangle-y rectangle) delta)
182 (incf (gdk-rectangle-width rectangle) delta)
183 (incf (gdk-rectangle-height rectangle) delta)
184 (format t "~A~%" rectangle))
186 (defun do-incf-rect (r &optional (delta 1))
187 (foreign-funcall-pointer (callback incf-rectangle) ()
188 (g-boxed-foreign gdk-rectangle) r
193 (eval-when (:compile-toplevel :load-toplevel :execute)
194 (defstruct (g-boxed-opaque-wrapper-info (:include g-boxed-info))
197 (defclass g-boxed-opaque ()
198 ((pointer :initarg :pointer
200 :accessor g-boxed-opaque-pointer)))
202 (defmethod create-temporary-native ((type g-boxed-opaque-wrapper-info) proxy)
203 (declare (ignore type))
204 (g-boxed-opaque-pointer proxy))
206 (defmethod free-temporary-native ((type g-boxed-opaque-wrapper-info) proxy native-structure)
207 (declare (ignore type proxy native-structure)))
209 (defmethod create-reference-proxy ((type g-boxed-opaque-wrapper-info) native-structure)
210 (make-instance (g-boxed-info-g-type type) :pointer native-structure))
212 (defmethod free-reference-proxy ((type g-boxed-opaque-wrapper-info) proxy native-structure)
213 (declare (ignore type native-structure))
214 (setf (g-boxed-opaque-pointer proxy) nil))
216 (defmethod create-proxy-for-native ((type g-boxed-opaque-wrapper-info) native-structure)
217 (let* ((g-type (g-boxed-info-g-type type))
218 (native-copy (g-boxed-copy g-type native-structure)))
219 (flet ((finalizer () (g-boxed-free g-type native-copy)))
220 (let ((proxy (make-instance (g-boxed-opaque-wrapper-info-g-type type) :pointer native-copy)))
221 (tg:finalize proxy #'finalizer)
224 (defmacro define-g-boxed-opaque (name g-type-name &key
225 (alloc (error "Alloc must be specified")))
226 (let ((native-copy (gensym "NATIVE-COPY-"))
227 (instance (gensym "INSTANCE-"))
228 (finalizer (gensym "FINALIZER-")))
229 `(progn (defclass ,name (g-boxed-opaque) ())
230 (defmethod initialize-instance :after ((,instance ,name) &key &allow-other-keys)
231 (unless (g-boxed-opaque-pointer ,instance)
232 (let ((,native-copy ,alloc))
233 (flet ((,finalizer () (g-boxed-free ,g-type-name ,native-copy)))
234 (setf (g-boxed-opaque-pointer ,instance) ,native-copy)
235 (finalize ,instance #',finalizer)))))
236 (eval-when (:compile-toplevel :load-toplevel :execute)
237 (setf (get ',name 'g-boxed-foreign-info)
238 (make-g-boxed-opaque-wrapper-info :name ',name
239 :g-type ,g-type-name))))))
241 (define-g-boxed-opaque gtk-tree-path "GtkTreePath"
242 :alloc (let* ((native-structure (gtk-tree-path-new))
243 (native-copy (g-boxed-copy "GtkTreePath" native-structure)))
244 (gtk-tree-path-free native-structure)
247 (defcfun gtk-tree-path-new :pointer)
249 (defcfun gtk-tree-path-free :void
250 (gtk-tree-path :pointer))
252 (defcfun gtk-tree-path-copy :pointer
253 (gtk-tree-path :pointer))
255 (defcfun (%gtk-tree-path-get-depth "gtk_tree_path_get_depth") :int
256 (path (g-boxed-foreign gtk-tree-path)))
258 (defcfun (%gtk-tree-path-get-indices "gtk_tree_path_get_indices") (:pointer :int)
259 (path (g-boxed-foreign gtk-tree-path)))
261 (defcfun gtk-tree-path-append-index :void
262 (path (g-boxed-foreign gtk-tree-path))
265 (defun tree-path-get-indices (path)
266 (let ((n (%gtk-tree-path-get-depth path))
267 (indices (%gtk-tree-path-get-indices path)))
270 collect (mem-aref indices :int i))))