Add GtkTextBuffer serialization/deserialization binding
authorDmitry Kalyanov <Kalyanov.Dmitry@gmail.com>
Mon, 12 Oct 2009 20:34:26 +0000 (00:34 +0400)
committerDmitry Kalyanov <Kalyanov.Dmitry@gmail.com>
Mon, 12 Oct 2009 20:34:26 +0000 (00:34 +0400)
api.ods
bugs/issue-648ddb6ff7319e4137b4eec870d1506d8a6883e7.yaml
gtk/gtk.text.lisp

diff --git a/api.ods b/api.ods
index 605d63b..c7b010e 100644 (file)
Binary files a/api.ods and b/api.ods differ
index 3d303f4..c6144fa 100644 (file)
@@ -5,8 +5,8 @@ type: :task
 component: cl-gtk2
 release: "0.1"
 reporter: Kalyanov Dmitry <Kalyanov.Dmitry@gmail.com>
-status: :unstarted
-disposition: 
+status: :closed
+disposition: :fixed
 creation_time: 2009-10-01 19:42:13.954638 Z
 references: []
 
@@ -16,3 +16,7 @@ log_events:
   - Kalyanov Dmitry <Kalyanov.Dmitry@gmail.com>
   - created
   - ""
+- - 2009-10-12 20:31:04.894231 Z
+  - Kalyanov Dmitry <Kalyanov.Dmitry@gmail.com>
+  - closed with disposition fixed
+  - ""
index b8b53cb..73a4985 100644 (file)
 
 (export 'text-buffer-remove-selection-clipboard)
 
-;; enum                GtkTextBufferTargetInfo;
-;; gboolean            (*GtkTextBufferDeserializeFunc)     (GtkTextBuffer *register_buffer,
-;;                                                          GtkTextBuffer *content_buffer,
-;;                                                          GtkTextIter *iter,
-;;                                                          const guint8 *data,
-;;                                                          gsize length,
-;;                                                          gboolean create_tags,
-;;                                                          gpointer user_data,
-;;                                                          GError **error);
-;; gboolean            gtk_text_buffer_deserialize         (GtkTextBuffer *register_buffer,
-;;                                                          GtkTextBuffer *content_buffer,
-;;                                                          GdkAtom format,
-;;                                                          GtkTextIter *iter,
-;;                                                          const guint8 *data,
-;;                                                          gsize length,
-;;                                                          GError **error);
-;; gboolean            gtk_text_buffer_deserialize_get_can_create_tags
-;;                                                         (GtkTextBuffer *buffer,
-;;                                                          GdkAtom format);
-;; void                gtk_text_buffer_deserialize_set_can_create_tags
-;;                                                         (GtkTextBuffer *buffer,
-;;                                                          GdkAtom format,
-;;                                                          gboolean can_create_tags);
-;; GtkTargetList*      gtk_text_buffer_get_copy_target_list
-;;                                                         (GtkTextBuffer *buffer);
-;; GdkAtom*            gtk_text_buffer_get_deserialize_formats
-;;                                                         (GtkTextBuffer *buffer,
-;;                                                          gint *n_formats);
-;; GtkTargetList*      gtk_text_buffer_get_paste_target_list
-;;                                                         (GtkTextBuffer *buffer);
-;; GdkAtom*            gtk_text_buffer_get_serialize_formats
-;;                                                         (GtkTextBuffer *buffer,
-;;                                                          gint *n_formats);
-;; GdkAtom             gtk_text_buffer_register_deserialize_format
-;;                                                         (GtkTextBuffer *buffer,
-;;                                                          const gchar *mime_type,
-;;                                                          GtkTextBufferDeserializeFunc function,
-;;                                                          gpointer user_data,
-;;                                                          GDestroyNotify user_data_destroy);
-;; GdkAtom             gtk_text_buffer_register_deserialize_tagset
-;;                                                         (GtkTextBuffer *buffer,
-;;                                                          const gchar *tagset_name);
-;; GdkAtom             gtk_text_buffer_register_serialize_format
-;;                                                         (GtkTextBuffer *buffer,
-;;                                                          const gchar *mime_type,
-;;                                                          GtkTextBufferSerializeFunc function,
-;;                                                          gpointer user_data,
-;;                                                          GDestroyNotify user_data_destroy);
-;; GdkAtom             gtk_text_buffer_register_serialize_tagset
-;;                                                         (GtkTextBuffer *buffer,
-;;                                                          const gchar *tagset_name);
-;; guint8*             (*GtkTextBufferSerializeFunc)       (GtkTextBuffer *register_buffer,
-;;                                                          GtkTextBuffer *content_buffer,
-;;                                                          const GtkTextIter *start,
-;;                                                          const GtkTextIter *end,
-;;                                                          gsize *length,
-;;                                                          gpointer user_data);
-;; guint8*             gtk_text_buffer_serialize           (GtkTextBuffer *register_buffer,
-;;                                                          GtkTextBuffer *content_buffer,
-;;                                                          GdkAtom format,
-;;                                                          const GtkTextIter *start,
-;;                                                          const GtkTextIter *end,
-;;                                                          gsize *length);
-;; void                gtk_text_buffer_unregister_deserialize_format
-;;                                                         (GtkTextBuffer *buffer,
-;;                                                          GdkAtom format);
-;; void                gtk_text_buffer_unregister_serialize_format
-;;                                                         (GtkTextBuffer *buffer,
-;;                                                          GdkAtom format);
+(defcfun gtk-text-buffer-deserialize :boolean
+  (register-buffer (g-object text-buffer))
+  (content-buffer (g-object text-buffer))
+  (format gdk-atom-as-string)
+  (iter (g-boxed-foreign text-iter))
+  (data :pointer)
+  (length gsize)
+  (error :pointer))
+
+(defun text-buffer-deserialize (register-buffer content-buffer format iter data)
+  (let ((bytes (foreign-alloc :uint8 :count (length data))))
+    (iter (for i from 0 below (length data))
+          (setf (mem-aref bytes :uint8 i) (aref data i)))
+    (unwind-protect
+         (with-g-error (err)
+           (gtk-text-buffer-deserialize register-buffer content-buffer
+                                        format iter bytes (length data) err))
+      (foreign-free bytes))))
+
+(export 'text-buffer-deserialize)
+
+(defcfun (text-buffer-deserialize-can-create-tags "gtk_text_buffer_deserialize_get_can_create_tags") :boolean
+  (buffer (g-object text-buffer))
+  (format gdk-atom-as-string))
+
+(defcfun gtk-text-buffer-deserialize-set-can-create-tags :void
+  (buffer (g-object text-buffer))
+  (format gdk-atom-as-string)
+  (can-create-tags :boolean))
+
+(defun (setf text-buffer-deserialize-can-create-tags) (new-value buffer format)
+  (gtk-text-buffer-deserialize-set-can-create-tags buffer format new-value))
+
+(export 'text-buffer-deserialize-can-create-tags)
+
+(defcfun gtk-text-buffer-get-deserialize-formats (:pointer gdk-atom-as-string)
+  (text-buffer (g-object text-buffer))
+  (n-formats (:pointer :int)))
+
+(defun text-buffer-get-deserialize-formats (text-buffer)
+  (with-foreign-object (n-formats :int)
+    (let ((atoms-ptr (gtk-text-buffer-get-deserialize-formats text-buffer n-formats)))
+      (iter (for i from 0 below (mem-ref n-formats :int))
+            (for atom = (mem-aref atoms-ptr 'gdk-atom-as-string i))
+            (collect atom)))))
+
+(export 'text-buffer-get-deserialize-formats)
+
+(defcfun gtk-text-buffer-get-serialize-formats (:pointer gdk-atom-as-string)
+  (text-buffer (g-object text-buffer))
+  (n-formats (:pointer :int)))
+
+(defun text-buffer-get-serialize-formats (text-buffer)
+  (with-foreign-object (n-formats :int)
+    (let ((atoms-ptr (gtk-text-buffer-get-serialize-formats text-buffer n-formats)))
+      (iter (for i from 0 below (mem-ref n-formats :int))
+            (for atom = (mem-aref atoms-ptr 'gdk-atom-as-string i))
+            (collect atom)))))
+
+(export 'text-buffer-get-serialize-formats)
+
+(defcallback gtk-text-buffer-deserialize-cb :boolean
+    ((register-buffer (g-object text-buffer))
+     (content-buffer (g-object text-buffer))
+     (iter (g-boxed-foreign text-iter))
+     (data :pointer)
+     (length gsize)
+     (create-tags :boolean)
+     (user-data :pointer)
+     (error :pointer))
+  (with-catching-to-g-error (error)
+    (let ((fn (stable-pointer-value user-data)))
+      (restart-case
+          (let ((bytes (iter (with bytes = (make-array length :element-type '(unsigned-byte 8)))
+                             (for i from 0 below length)
+                             (setf (aref bytes i) (mem-ref data :uint8 i))
+                             (finally (return bytes)))))
+            (progn (funcall fn register-buffer content-buffer iter bytes create-tags) t))
+        (return-from-text-buffer-deserialize-cb ()
+          (error 'g-error-condition
+                 :domain "cl-gtk2"
+                 :code 0
+                 :message "'return-from-text-buffer-deserialize-cb' restart was called"))))))
+
+(defcfun gtk-text-buffer-register-deserialize-format gdk-atom-as-string
+  (buffer (g-object text-buffer))
+  (mime-type :string)
+  (function :pointer)
+  (user-data :pointer)
+  (destroy-notify :pointer))
+
+(defun text-buffer-register-deserialize-format (buffer mime-type function)
+  (gtk-text-buffer-register-deserialize-format buffer mime-type
+                                               (callback gtk-text-buffer-deserialize-cb)
+                                               (allocate-stable-pointer function)
+                                               (callback stable-pointer-free-destroy-notify-callback)))
+
+(export 'text-buffer-register-deserialize-format)
+
+(defcfun (text-buffer-register-deserialize-tagset "gtk_text_buffer_register_deserialize_tagset") gdk-atom-as-string
+  (buffer (g-object text-buffer))
+  (tagset-name :string))
+
+(export 'text-buffer-register-deserialize-tagset)
+
+(defcallback gtk-text-buffer-serialize-cb :pointer
+    ((register-buffer (g-object text-buffer))
+     (content-buffer (g-object text-buffer))
+     (start-iter (g-boxed-foreign text-iter))
+     (end-iter (g-boxed-foreign text-iter))
+     (length (:pointer gsize))
+     (user-data :pointer))
+  (let ((fn (stable-pointer-value user-data)))
+    (restart-case
+        (let* ((bytes (funcall fn register-buffer content-buffer start-iter end-iter))
+               (bytes-ptr (g-malloc (length bytes))))
+          (setf (mem-ref length 'gsize) (length bytes))
+          (iter (for i from 0 below (length bytes))
+                (setf (mem-aref bytes-ptr :uint8 i) (aref bytes i)))
+          bytes-ptr)
+      (return-from-text-buffer-serialize-cb () nil))))
+
+(defcfun gtk-text-buffer-register-serialize-format gdk-atom-as-string
+  (buffer (g-object text-buffer))
+  (mime-type :string)
+  (function :pointer)
+  (user-data :pointer)
+  (destroy-notify :pointer))
+
+(defun text-buffer-register-serialize-format (buffer mime-type function)
+  (gtk-text-buffer-register-serialize-format buffer mime-type
+                                             (callback gtk-text-buffer-serialize-cb)
+                                             (allocate-stable-pointer function)
+                                             (callback stable-pointer-free-destroy-notify-callback)))
+
+(export 'text-buffer-register-serialize-format)
+
+(defcfun (text-buffer-register-serialize-tagset "gtk_text_buffer_register_serialize_tagset") gdk-atom-as-string
+  (buffer (g-object text-buffer))
+  (tagset-name :string))
+
+(export 'text-buffer-register-serialize-tagset)
+
+(defcfun gtk-text-buffer-serialize :pointer
+  (register-buffer (g-object text-buffer))
+  (content-buffer (g-object text-buffer))
+  (format gdk-atom-as-string)
+  (start (g-boxed-foreign text-iter))
+  (end (g-boxed-foreign text-iter))
+  (length (:pointer gsize)))
+
+(defun text-buffer-serialize (register-buffer content-buffer format start end)
+  (with-foreign-object (length 'gsize)
+    (let ((bytes (gtk-text-buffer-serialize register-buffer content-buffer format start end length)))
+      (iter (for i from 0 to (mem-ref length 'gsize))
+            (for byte = (mem-aref bytes :uint8 i))
+            (collect byte result-type vector)
+            (finally (g-free bytes))))))
+
+(export 'text-buffer-serialize)
+
+(defcfun (text-buffer-unregister-deserialize-format "gtk_text_buffer_unregister_deserialize_format") :void
+  (buffer (g-object text-buffer))
+  (format gdk-atom-as-string))
+
+(export 'text-buffer-unregister-deserialize-format)
+
+(defcfun (text-buffer-unregister-serialize-format "gtk_text_buffer_unregister_serialize_format") :void
+  (buffer (g-object text-buffer))
+  (format gdk-atom-as-string))
+
+(export 'text-buffer-unregister-serialize-format)
 
 ;; text tag