summaryrefslogtreecommitdiff
path: root/gtk/gtkclipboard.c
diff options
context:
space:
mode:
authorMichael Natterer <mitch@imendio.com>2006-03-07 13:46:11 +0000
committerMichael Natterer <mitch@src.gnome.org>2006-03-07 13:46:11 +0000
commit6c1d990adc5eea803b4cb28956befb83e89468ee (patch)
treeca25230f26a27da2bde3f6cf1c3e9910c0e7487d /gtk/gtkclipboard.c
parent1f5c294851a009295c9d331110512e47324d09c0 (diff)
downloadgtk+-6c1d990adc5eea803b4cb28956befb83e89468ee.tar.gz
Add infrastructure for copy/paste and DND of rich text for GtkTextBuffer.
2006-03-07 Michael Natterer <mitch@imendio.com> Add infrastructure for copy/paste and DND of rich text for GtkTextBuffer. Fixes bug #324177. * gtk/gtktextbufferrichtext.[ch]: new files implementing a per-buffer registry of rich text formats. * gtk/gtk.h: #include gtktextbufferrichtext.h * gtk/gtktextbufferserialize.[ch]: new files implementing an internal serialization format that can handle all of a text buffer's tags and pixbufs. It's not useful for anything except tranfer between instances of GtkTextBuffer (Anders Carlsson). * gtk/Makefile.am: build the new files. * gtk/gtkclipboard.[ch]: added convenience APIs for rich text, just as they exist for plain text and pixbufs. * gtk/gtkselection.[ch]: added rich text convenience APIs here too. Return the target list from gtk_target_list_ref(). Register GtkTargetList as boxed type. Added gtk_target_table_new_from_list() and gtk_target_table_free(), which make converting between GtkTargetList and arrays of GtkTargetEntry considerably easier. * gtk/gtktextutil.[ch]: added _gtk_text_util_create_rich_drag_icon() which creates a fancy rich text icon (Matthias Clasen). * gtk/gtktextbuffer.[ch]: use all the new stuff above and implement copy and paste of rich text. Added APIs for getting the target lists used for copy and paste. Added public enum GtkTextBufferTargetInfo which contains the "info" IDs associated with the entries of the target lists. * gtk/gtktextview.c: use the new rich text APIs and GtkTextBuffer's new target list API to enable DND of rich text chunks. * gtk/gtk.symbols: export all the new symbols added. * tests/testtext.c: added rich text testing stuff.
Diffstat (limited to 'gtk/gtkclipboard.c')
-rw-r--r--gtk/gtkclipboard.c197
1 files changed, 196 insertions, 1 deletions
diff --git a/gtk/gtkclipboard.c b/gtk/gtkclipboard.c
index 25ae16664e..8eceb1dd8b 100644
--- a/gtk/gtkclipboard.c
+++ b/gtk/gtkclipboard.c
@@ -27,6 +27,7 @@
#include "gtkinvisible.h"
#include "gtkmain.h"
#include "gtkmarshalers.h"
+#include "gtktextbufferrichtext.h"
#include "gtkintl.h"
#include "gtkalias.h"
@@ -47,6 +48,7 @@ typedef struct _GtkClipboardClass GtkClipboardClass;
typedef struct _RequestContentsInfo RequestContentsInfo;
typedef struct _RequestTextInfo RequestTextInfo;
+typedef struct _RequestRichTextInfo RequestRichTextInfo;
typedef struct _RequestImageInfo RequestImageInfo;
typedef struct _RequestTargetsInfo RequestTargetsInfo;
@@ -97,6 +99,15 @@ struct _RequestTextInfo
gpointer user_data;
};
+struct _RequestRichTextInfo
+{
+ GtkClipboardRichTextReceivedFunc callback;
+ GdkAtom *atoms;
+ gint n_atoms;
+ gint current_atom;
+ gpointer user_data;
+};
+
struct _RequestImageInfo
{
GtkClipboardImageReceivedFunc callback;
@@ -927,7 +938,7 @@ request_text_received_func (GtkClipboard *clipboard,
RequestTextInfo *info = data;
gchar *result = NULL;
- result = gtk_selection_data_get_text (selection_data);
+ result = (gchar *) gtk_selection_data_get_text (selection_data);
if (!result)
{
@@ -992,6 +1003,80 @@ gtk_clipboard_request_text (GtkClipboard *clipboard,
info);
}
+static void
+request_rich_text_received_func (GtkClipboard *clipboard,
+ GtkSelectionData *selection_data,
+ gpointer data)
+{
+ RequestRichTextInfo *info = data;
+ guint8 *result = NULL;
+ gsize length = 0;
+
+ result = selection_data->data;
+ length = selection_data->length;
+
+ info->current_atom++;
+
+ if ((!result || length < 1) && (info->current_atom < info->n_atoms))
+ {
+ gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
+ request_rich_text_received_func,
+ info);
+ return;
+ }
+
+ info->callback (clipboard, selection_data->target, result, length,
+ info->user_data);
+ g_free (info->atoms);
+ g_free (info);
+}
+
+/**
+ * gtk_clipboard_request_rich_text:
+ * @clipboard: a #GtkClipboard
+ * @buffer: a #GtkTextBuffer
+ * @callback: a function to call when the text is received,
+ * or the retrieval fails. (It will always be called
+ * one way or the other.)
+ * @user_data: user data to pass to @callback.
+ *
+ * Requests the contents of the clipboard as rich text. When the rich
+ * text is later received, @callback will be called.
+ *
+ * The @text parameter to @callback will contain the resulting rich
+ * text if the request succeeded, or %NULL if it failed. The @length
+ * parameter will contain @text's length. This function can fail for
+ * various reasons, in particular if the clipboard was empty or if the
+ * contents of the clipboard could not be converted into rich text form.
+ *
+ * Since: 2.10
+ **/
+void
+gtk_clipboard_request_rich_text (GtkClipboard *clipboard,
+ GtkTextBuffer *buffer,
+ GtkClipboardRichTextReceivedFunc callback,
+ gpointer user_data)
+{
+ RequestRichTextInfo *info;
+
+ g_return_if_fail (clipboard != NULL);
+ g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
+ g_return_if_fail (callback != NULL);
+
+ info = g_new (RequestRichTextInfo, 1);
+ info->callback = callback;
+ info->atoms = NULL;
+ info->n_atoms = 0;
+ info->current_atom = 0;
+ info->user_data = user_data;
+
+ info->atoms = gtk_text_buffer_get_deserialize_formats (buffer, &info->n_atoms);
+
+ gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
+ request_rich_text_received_func,
+ info);
+}
+
static void
request_image_received_func (GtkClipboard *clipboard,
GtkSelectionData *selection_data,
@@ -1143,6 +1228,8 @@ typedef struct
{
GMainLoop *loop;
gpointer data;
+ GdkAtom format; /* used by rich text */
+ gsize length; /* used by rich text */
} WaitResults;
static void
@@ -1254,6 +1341,75 @@ gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
return results.data;
}
+static void
+clipboard_rich_text_received_func (GtkClipboard *clipboard,
+ GdkAtom format,
+ const guint8 *text,
+ gsize length,
+ gpointer data)
+{
+ WaitResults *results = data;
+
+ results->data = g_memdup (text, length);
+ results->format = format;
+ results->length = length;
+ g_main_loop_quit (results->loop);
+}
+
+/**
+ * gtk_clipboard_wait_for_rich_text:
+ * @clipboard: a #GtkClipboard
+ * @buffer: a #GtkTextBuffer
+ * @length: return location for the length of the returned data
+ *
+ * Requests the contents of the clipboard as rich text. This function
+ * waits for the data to be received using the main loop, so events,
+ * timeouts, etc, may be dispatched during the wait.
+ *
+ * Return value: a newly-allocated binary block of data which must
+ * be freed with g_free(), or %NULL if retrieving
+ * the selection data failed. (This could happen
+ * for various reasons, in particular if the
+ * clipboard was empty or if the contents of the
+ * clipboard could not be converted into text form.)
+ *
+ * Since: 2.10
+ **/
+guint8 *
+gtk_clipboard_wait_for_rich_text (GtkClipboard *clipboard,
+ GtkTextBuffer *buffer,
+ GdkAtom *format,
+ gsize *length)
+{
+ WaitResults results;
+
+ g_return_val_if_fail (clipboard != NULL, NULL);
+ g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
+ g_return_val_if_fail (format != NULL, NULL);
+ g_return_val_if_fail (length != NULL, NULL);
+
+ results.data = NULL;
+ results.loop = g_main_loop_new (NULL, TRUE);
+
+ gtk_clipboard_request_rich_text (clipboard, buffer,
+ clipboard_rich_text_received_func,
+ &results);
+
+ if (g_main_loop_is_running (results.loop))
+ {
+ GDK_THREADS_LEAVE ();
+ g_main_loop_run (results.loop);
+ GDK_THREADS_ENTER ();
+ }
+
+ g_main_loop_unref (results.loop);
+
+ *format = results.format;
+ *length = results.length;
+
+ return results.data;
+}
+
static void
clipboard_image_received_func (GtkClipboard *clipboard,
GdkPixbuf *pixbuf,
@@ -1362,6 +1518,45 @@ gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
}
/**
+ * gtk_clipboard_wait_is_rich_text_available:
+ * @clipboard: a #GtkClipboard
+ * @buffer: a #GtkTextBuffer
+ *
+ * Test to see if there is rich text available to be pasted
+ * This is done by requesting the TARGETS atom and checking
+ * if it contains any of the supported rich text targets. This function
+ * waits for the data to be received using the main loop, so events,
+ * timeouts, etc, may be dispatched during the wait.
+ *
+ * This function is a little faster than calling
+ * gtk_clipboard_wait_for_rich_text() since it doesn't need to retrieve
+ * the actual text.
+ *
+ * Return value: %TRUE is there is rich text available, %FALSE otherwise.
+ *
+ * Since: 2.10
+ **/
+gboolean
+gtk_clipboard_wait_is_rich_text_available (GtkClipboard *clipboard,
+ GtkTextBuffer *buffer)
+{
+ GtkSelectionData *data;
+ gboolean result = FALSE;
+
+ g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
+ g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
+
+ data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
+ if (data)
+ {
+ result = gtk_selection_data_targets_include_rich_text (data, buffer);
+ gtk_selection_data_free (data);
+ }
+
+ return result;
+}
+
+/**
* gtk_clipboard_wait_is_image_available:
* @clipboard: a #GtkClipboard
*