]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkselection.c
Include "config.h" instead of <config.h> Command used: find -name
[~andy/gtk] / gtk / gtkselection.c
index a453cb35e630f6d8a81c30c50897df6d84b3c1b0..56c14473fcb66d489e3148cf60b9b3c22ca8b912 100644 (file)
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
  */
 
-#include <config.h>
+#include "config.h"
 #include <stdarg.h>
 #include <string.h>
 #include "gdk.h"
 
 #include "gtkmain.h"
 #include "gtkselection.h"
+#include "gtktextbufferrichtext.h"
 #include "gtkintl.h"
 #include "gdk-pixbuf/gdk-pixbuf.h"
 
@@ -151,8 +152,8 @@ struct _GtkRetrievalInfo
 
 /* Local Functions */
 static void gtk_selection_init              (void);
-static gint gtk_selection_incr_timeout      (GtkIncrInfo      *info);
-static gint gtk_selection_retrieval_timeout (GtkRetrievalInfo *info);
+static gboolean gtk_selection_incr_timeout      (GtkIncrInfo      *info);
+static gboolean gtk_selection_retrieval_timeout (GtkRetrievalInfo *info);
 static void gtk_selection_retrieval_report  (GtkRetrievalInfo *info,
                                             GdkAtom           type,
                                             gint              format,
@@ -197,7 +198,7 @@ GtkTargetList *
 gtk_target_list_new (const GtkTargetEntry *targets,
                     guint                 ntargets)
 {
-  GtkTargetList *result = g_new (GtkTargetList, 1);
+  GtkTargetList *result = g_slice_new (GtkTargetList);
   result->list = NULL;
   result->ref_count = 1;
 
@@ -213,13 +214,16 @@ gtk_target_list_new (const GtkTargetEntry *targets,
  * 
  * Increases the reference count of a #GtkTargetList by one.
  *
+ * Return value: the passed in #GtkTargetList.
  **/
-void               
+GtkTargetList *
 gtk_target_list_ref (GtkTargetList *list)
 {
-  g_return_if_fail (list != NULL);
+  g_return_val_if_fail (list != NULL, NULL);
 
   list->ref_count++;
+
+  return list;
 }
 
 /**
@@ -242,13 +246,13 @@ gtk_target_list_unref (GtkTargetList *list)
       while (tmp_list)
        {
          GtkTargetPair *pair = tmp_list->data;
-         g_free (pair);
+         g_slice_free (GtkTargetPair, pair);
 
          tmp_list = tmp_list->next;
        }
       
       g_list_free (list->list);
-      g_free (list);
+      g_slice_free (GtkTargetList, list);
     }
 }
 
@@ -271,7 +275,7 @@ gtk_target_list_add (GtkTargetList *list,
 
   g_return_if_fail (list != NULL);
   
-  pair = g_new (GtkTargetPair, 1);
+  pair = g_slice_new (GtkTargetPair);
   pair->target = target;
   pair->flags = flags;
   pair->info = info;
@@ -334,10 +338,50 @@ gtk_target_list_add_text_targets (GtkTargetList *list,
   gtk_target_list_add (list, text_atom, 0, info);  
   gtk_target_list_add (list, GDK_TARGET_STRING, 0, info);  
   gtk_target_list_add (list, text_plain_utf8_atom, 0, info);  
-  gtk_target_list_add (list, text_plain_locale_atom, 0, info);  
+  if (!g_get_charset (NULL))
+    gtk_target_list_add (list, text_plain_locale_atom, 0, info);  
   gtk_target_list_add (list, text_plain_atom, 0, info);  
 }
 
+/**
+ * gtk_target_list_add_rich_text_targets:
+ * @list: a #GtkTargetList
+ * @info: an ID that will be passed back to the application
+ * @deserializable: if %TRUE, then deserializable rich text formats
+ *                  will be added, serializable formats otherwise.
+ * @buffer: a #GtkTextBuffer.
+ *
+ * Appends the rich text targets registered with
+ * gtk_text_buffer_register_serialize_format() or
+ * gtk_text_buffer_register_deserialize_format() to the target list. All
+ * targets are added with the same @info.
+ *
+ * Since: 2.10
+ **/
+void
+gtk_target_list_add_rich_text_targets (GtkTargetList  *list,
+                                       guint           info,
+                                       gboolean        deserializable,
+                                       GtkTextBuffer  *buffer)
+{
+  GdkAtom *atoms;
+  gint     n_atoms;
+  gint     i;
+
+  g_return_if_fail (list != NULL);
+  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
+
+  if (deserializable)
+    atoms = gtk_text_buffer_get_deserialize_formats (buffer, &n_atoms);
+  else
+    atoms = gtk_text_buffer_get_serialize_formats (buffer, &n_atoms);
+
+  for (i = 0; i < n_atoms; i++)
+    gtk_target_list_add (list, atoms[i], 0, info);
+
+  g_free (atoms);
+}
+
 /**
  * gtk_target_list_add_image_targets:
  * @list: a #GtkTargetList
@@ -440,7 +484,7 @@ gtk_target_list_add_table (GtkTargetList        *list,
 
   for (i=ntargets-1; i >= 0; i--)
     {
-      GtkTargetPair *pair = g_new (GtkTargetPair, 1);
+      GtkTargetPair *pair = g_slice_new (GtkTargetPair);
       pair->target = gdk_atom_intern (targets[i].target, FALSE);
       pair->flags = targets[i].flags;
       pair->info = targets[i].info;
@@ -471,7 +515,7 @@ gtk_target_list_remove (GtkTargetList *list,
       
       if (pair->target == target)
        {
-         g_free (pair);
+         g_slice_free (GtkTargetPair, pair);
 
          list->list = g_list_remove_link (list->list, tmp_list);
          g_list_free_1 (tmp_list);
@@ -487,10 +531,11 @@ gtk_target_list_remove (GtkTargetList *list,
  * gtk_target_list_find:
  * @list: a #GtkTargetList
  * @target: an interned atom representing the target to search for
- * @info: a pointer to the location to store application info for target
- * 
+ * @info: a pointer to the location to store application info for target,
+ *        or %NULL
+ *
  * Looks up a given target in a #GtkTargetList.
- * 
+ *
  * Return value: %TRUE if the target was found, otherwise %FALSE
  **/
 gboolean
@@ -498,22 +543,95 @@ gtk_target_list_find (GtkTargetList *list,
                      GdkAtom        target,
                      guint         *info)
 {
-  GList *tmp_list = list->list;
+  GList *tmp_list;
+
+  g_return_val_if_fail (list != NULL, FALSE);
+
+  tmp_list = list->list;
   while (tmp_list)
     {
       GtkTargetPair *pair = tmp_list->data;
 
       if (pair->target == target)
        {
-         *info = pair->info;
+          if (info)
+            *info = pair->info;
+
          return TRUE;
        }
+
       tmp_list = tmp_list->next;
     }
 
   return FALSE;
 }
 
+/**
+ * gtk_target_table_new_from_list:
+ * @list: a #GtkTargetList
+ * @n_targets: return location for the number ot targets in the table
+ *
+ * This function creates an #GtkTargetEntry array that contains the
+ * same targets as the passed %list. The returned table is newly
+ * allocated and should be freed using gtk_target_table_free() when no
+ * longer needed.
+ *
+ * Return value: the new table.
+ *
+ * Since: 2.10
+ **/
+GtkTargetEntry *
+gtk_target_table_new_from_list (GtkTargetList *list,
+                                gint          *n_targets)
+{
+  GtkTargetEntry *targets;
+  GList          *tmp_list;
+  gint            i;
+
+  g_return_val_if_fail (list != NULL, NULL);
+  g_return_val_if_fail (n_targets != NULL, NULL);
+
+  *n_targets = g_list_length (list->list);
+  targets = g_new0 (GtkTargetEntry, *n_targets);
+
+  for (i = 0, tmp_list = list->list;
+       i < *n_targets;
+       i++, tmp_list = g_list_next (tmp_list))
+    {
+      GtkTargetPair *pair = tmp_list->data;
+
+      targets[i].target = gdk_atom_name (pair->target);
+      targets[i].flags  = pair->flags;
+      targets[i].info   = pair->info;
+    }
+
+  return targets;
+}
+
+/**
+ * gtk_target_table_free:
+ * @targets: a #GtkTargetEntry array
+ * @n_targets: the number of entries in the array
+ *
+ * This function frees a target table as returned by
+ * gtk_target_table_new_from_list()
+ *
+ * Since: 2.10
+ **/
+void
+gtk_target_table_free (GtkTargetEntry *targets,
+                       gint            n_targets)
+{
+  gint i;
+
+  g_return_if_fail (targets == NULL || n_targets > 0);
+
+  for (i = 0; i < n_targets; i++)
+    g_free (targets[i].target);
+
+  g_free (targets);
+}
+
 /**
  * gtk_selection_owner_set_for_display:
  * @display: the #Gdkdisplay where the selection is set 
@@ -573,14 +691,14 @@ gtk_selection_owner_set_for_display (GdkDisplay   *display,
              current_selections = g_list_remove_link (current_selections,
                                                       tmp_list);
              g_list_free (tmp_list);
-             g_free (selection_info);
+             g_slice_free (GtkSelectionInfo, selection_info);
            }
        }
       else
        {
          if (selection_info == NULL)
            {
-             selection_info = g_new (GtkSelectionInfo, 1);
+             selection_info = g_slice_new (GtkSelectionInfo);
              selection_info->selection = selection;
              selection_info->widget = widget;
              selection_info->time = time;
@@ -678,7 +796,7 @@ gtk_selection_target_list_get (GtkWidget    *widget,
       tmp_list = tmp_list->next;
     }
 
-  sellist = g_new (GtkSelectionTargetList, 1);
+  sellist = g_slice_new (GtkSelectionTargetList);
   sellist->selection = selection;
   sellist->list = gtk_target_list_new (NULL, 0);
 
@@ -704,7 +822,7 @@ gtk_selection_target_list_remove (GtkWidget    *widget)
 
       gtk_target_list_unref (sellist->list);
 
-      g_free (sellist);
+      g_slice_free (GtkSelectionTargetList, sellist);
       tmp_list = tmp_list->next;
     }
 
@@ -741,7 +859,7 @@ gtk_selection_clear_targets (GtkWidget *widget,
        {
          lists = g_list_delete_link (lists, tmp_list);
          gtk_target_list_unref (sellist->list);
-         g_free (sellist);
+         g_slice_free (GtkSelectionTargetList, sellist);
 
          break;
        }
@@ -834,7 +952,9 @@ gtk_selection_remove_all (GtkWidget *widget)
   GList *tmp_list;
   GList *next;
   GtkSelectionInfo *selection_info;
-  
+
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+
   /* Remove pending requests/incrs for this widget */
   
   tmp_list = current_retrievals;
@@ -868,7 +988,7 @@ gtk_selection_remove_all (GtkWidget *widget)
          current_selections = g_list_remove_link (current_selections,
                                                   tmp_list);
          g_list_free (tmp_list);
-         g_free (selection_info);
+         g_slice_free (GtkSelectionInfo, selection_info);
        }
       
       tmp_list = next;
@@ -929,7 +1049,7 @@ gtk_selection_convert (GtkWidget *widget,
       tmp_list = tmp_list->next;
     }
   
-  info = g_new (GtkRetrievalInfo, 1);
+  info = g_slice_new (GtkRetrievalInfo);
   
   info->widget = widget;
   info->selection = selection;
@@ -971,8 +1091,10 @@ gtk_selection_convert (GtkWidget *widget,
                                          time_);
          
          g_free (selection_data.data);
+          selection_data.data = NULL;
+          selection_data.length = -1;
          
-         g_free (info);
+         g_slice_free (GtkRetrievalInfo, info);
          return TRUE;
        }
     }
@@ -981,11 +1103,98 @@ gtk_selection_convert (GtkWidget *widget,
   
   current_retrievals = g_list_append (current_retrievals, info);
   gdk_selection_convert (widget->window, selection, target, time_);
-  g_timeout_add (1000, (GSourceFunc) gtk_selection_retrieval_timeout, info);
+  gdk_threads_add_timeout (1000,
+      (GSourceFunc) gtk_selection_retrieval_timeout, info);
   
   return TRUE;
 }
 
+/**
+ * gtk_selection_data_get_target:
+ * @selection_data: a pointer to a #GtkSelectionData structure.
+ *
+ * Retrieves the target of the selection.
+ *
+ * Since: 2.14
+ **/
+GdkAtom
+gtk_selection_data_get_target (GtkSelectionData *selection_data)
+{
+  g_return_val_if_fail (selection_data != NULL, 0);
+
+  return selection_data->target;
+}
+
+/**
+ * gtk_selection_data_get_data_type:
+ * @selection_data: a pointer to a #GtkSelectionData structure.
+ *
+ * Retrieves the data type of the selection.
+ *
+ * Since: 2.14
+ **/
+GdkAtom
+gtk_selection_data_get_data_type (GtkSelectionData *selection_data)
+{
+  g_return_val_if_fail (selection_data != NULL, 0);
+
+  return selection_data->type;
+}
+
+/**
+ * gtk_selection_data_get_format:
+ * @selection_data: a pointer to a #GtkSelectionData structure.
+ *
+ * Retrieves the format of the selection.
+ *
+ * Since: 2.14
+ **/
+gint
+gtk_selection_data_get_format (GtkSelectionData *selection_data)
+{
+  g_return_val_if_fail (selection_data != NULL, 0);
+
+  return selection_data->format;
+}
+
+/**
+ * gtk_selection_data_get_data:
+ * @selection_data: a pointer to a #GtkSelectionData structure.
+ * @length: an integer to be filled in, or %NULL
+ *
+ * Retrieves the raw data of the selection.
+ *
+ * If @length is not %NULL it is filled with the length of data.
+ *
+ * Since: 2.14
+ **/
+const guchar*
+gtk_selection_data_get_data (GtkSelectionData *selection_data,
+                             guint           *length)
+{
+  g_return_val_if_fail (selection_data != NULL, NULL);
+
+  if (length)
+      *length = selection_data->length;
+
+  return selection_data->data;
+}
+
+/**
+ * gtk_selection_data_get_display:
+ * @selection_data: a pointer to a #GtkSelectionData structure.
+ *
+ * Retrieves the display of the selection.
+ *
+ * Since: 2.14
+ **/
+GdkDisplay *
+gtk_selection_data_get_display (GtkSelectionData *selection_data)
+{
+  g_return_val_if_fail (selection_data != NULL, NULL);
+
+  return selection_data->display;
+}
 
 /**
  * gtk_selection_data_set:
@@ -1006,8 +1215,9 @@ gtk_selection_data_set (GtkSelectionData *selection_data,
                        const guchar     *data,
                        gint              length)
 {
-  if (selection_data->data)
-    g_free (selection_data->data);
+  g_return_if_fail (selection_data != NULL);
+
+  g_free (selection_data->data);
   
   selection_data->type = type;
   selection_data->format = format;
@@ -1025,7 +1235,7 @@ gtk_selection_data_set (GtkSelectionData *selection_data,
       if (length < 0)
        selection_data->data = NULL;
       else
-       selection_data->data = g_strdup("");
+       selection_data->data = (guchar *) g_strdup ("");
     }
   
   selection_data->length = length;
@@ -1044,7 +1254,7 @@ selection_set_string (GtkSelectionData *selection_data,
     {
       gtk_selection_data_set (selection_data,
                              GDK_SELECTION_TYPE_STRING,
-                             8, latin1, strlen (latin1));
+                             8, (guchar *) latin1, strlen (latin1));
       g_free (latin1);
       
       return TRUE;
@@ -1088,8 +1298,9 @@ normalize_to_crlf (const gchar *str,
 {
   GString *result = g_string_sized_new (len);
   const gchar *p = str;
+  const gchar *end = str + len;
 
-  while (1)
+  while (p < end)
     {
       if (*p == '\n')
        g_string_append_c (result, '\r');
@@ -1098,13 +1309,12 @@ normalize_to_crlf (const gchar *str,
        {
          g_string_append_c (result, *p);
          p++;
-         if (*p != '\n')
+         if (p == end || *p != '\n')
            g_string_append_c (result, '\n');
+         if (p == end)
+           break;
        }
 
-      if (*p == '\0')
-       break;
-
       g_string_append_c (result, *p);
       p++;
     }
@@ -1166,8 +1376,8 @@ selection_set_text_plain (GtkSelectionData *selection_data,
 
   if (!result)
     {
-      g_warning ("Error converting from UTF-8 to %s: %s",
-                charset, error->message);
+      g_warning ("Error converting from %s to %s: %s",
+                "UTF-8", charset, error->message);
       g_error_free (error);
       
       return FALSE;
@@ -1175,13 +1385,13 @@ selection_set_text_plain (GtkSelectionData *selection_data,
   
   gtk_selection_data_set (selection_data,
                          selection_data->target, 
-                         8, result, strlen (result));
+                         8, (guchar *) result, strlen (result));
   g_free (result);
   
   return TRUE;
 }
 
-static gchar *
+static guchar *
 selection_get_text_plain (GtkSelectionData *selection_data)
 {
   const gchar *charset = NULL;
@@ -1189,7 +1399,7 @@ selection_get_text_plain (GtkSelectionData *selection_data)
   gsize len;
   GError *error = NULL;
 
-  str = g_strdup (selection_data->data);
+  str = g_strdup ((const gchar *) selection_data->data);
   len = selection_data->length;
   
   if (selection_data->type == text_plain_atom)
@@ -1201,14 +1411,14 @@ selection_get_text_plain (GtkSelectionData *selection_data)
     {
       gchar *tmp = str;
       str = g_convert_with_fallback (tmp, len, 
-                                    charset, "UTF-8", 
+                                    "UTF-8", charset,
                                     NULL, NULL, &len, &error);
       g_free (tmp);
 
       if (!str)
        {
-         g_warning ("Error converting from %s to UTF-8: %s",
-                     charset, error->message);
+         g_warning ("Error converting from %s to %s: %s",
+                    charset, "UTF-8", error->message);
          g_error_free (error);
 
          return NULL;
@@ -1216,7 +1426,8 @@ selection_get_text_plain (GtkSelectionData *selection_data)
     }
   else if (!g_utf8_validate (str, -1, NULL))
     {
-      g_warning ("Error converting from text/plain;charset=utf-8 to UTF-8");
+      g_warning ("Error converting from %s to %s: %s",
+                "text/plain;charset=utf-8", "UTF-8", "invalid UTF-8");
       g_free (str);
 
       return NULL;
@@ -1225,7 +1436,7 @@ selection_get_text_plain (GtkSelectionData *selection_data)
   result = normalize_to_lf (str, len);
   g_free (str);
 
-  return result;
+  return (guchar *) result;
 }
 
 /**
@@ -1246,6 +1457,8 @@ gtk_selection_data_set_text (GtkSelectionData     *selection_data,
                             const gchar          *str,
                             gint                  len)
 {
+  g_return_val_if_fail (selection_data != NULL, FALSE);
+
   if (len < 0)
     len = strlen (str);
   
@@ -1296,6 +1509,8 @@ gtk_selection_data_get_text (GtkSelectionData *selection_data)
 {
   guchar *result = NULL;
 
+  g_return_val_if_fail (selection_data != NULL, NULL);
+
   init_atoms ();
   
   if (selection_data->length >= 0 &&
@@ -1312,7 +1527,7 @@ gtk_selection_data_get_text (GtkSelectionData *selection_data)
                                                               selection_data->length,
                                                               &list);
       if (count > 0)
-       result = list[0];
+       result = (guchar *) list[0];
 
       for (i = 1; i < count; i++)
        g_free (list[i]);
@@ -1354,6 +1569,9 @@ gtk_selection_data_set_pixbuf (GtkSelectionData *selection_data,
   gchar *str, *type;
   gsize len;
 
+  g_return_val_if_fail (selection_data != NULL, FALSE);
+  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
+
   formats = gdk_pixbuf_get_formats ();
 
   for (f = formats; f; f = f->next)
@@ -1412,6 +1630,8 @@ gtk_selection_data_get_pixbuf (GtkSelectionData *selection_data)
   GdkPixbufLoader *loader;
   GdkPixbuf *result = NULL;
 
+  g_return_val_if_fail (selection_data != NULL, NULL);
+
   if (selection_data->length > 0)
     {
       loader = gdk_pixbuf_loader_new ();
@@ -1450,6 +1670,9 @@ gboolean
 gtk_selection_data_set_uris (GtkSelectionData  *selection_data,
                             gchar            **uris)
 {
+  g_return_val_if_fail (selection_data != NULL, FALSE);
+  g_return_val_if_fail (uris != NULL, FALSE);
+
   init_atoms ();
 
   if (selection_data->target == text_uri_list_atom)
@@ -1504,13 +1727,14 @@ gtk_selection_data_get_uris (GtkSelectionData *selection_data)
 {
   gchar **result = NULL;
 
+  g_return_val_if_fail (selection_data != NULL, NULL);
+
   init_atoms ();
   
   if (selection_data->length >= 0 &&
       selection_data->type == text_uri_list_atom)
     {
       gchar **list;
-      gint i;
       gint count = gdk_text_property_to_utf8_list_for_display (selection_data->display,
                                                               utf8_atom,
                                                               selection_data->format, 
@@ -1547,6 +1771,8 @@ gtk_selection_data_get_targets (GtkSelectionData  *selection_data,
                                GdkAtom          **targets,
                                gint              *n_atoms)
 {
+  g_return_val_if_fail (selection_data != NULL, FALSE);
+
   if (selection_data->length >= 0 &&
       selection_data->format == 32 &&
       selection_data->type == GDK_SELECTION_TYPE_ATOM)
@@ -1584,13 +1810,18 @@ gtk_selection_data_get_targets (GtkSelectionData  *selection_data,
  **/
 gboolean 
 gtk_targets_include_text (GdkAtom *targets,
-                          gint     n_targets)
+                          gint     n_targets)
 {
   gint i;
   gboolean result = FALSE;
 
+  g_return_val_if_fail (targets != NULL || n_targets == 0, FALSE);
+
   /* Keep in sync with gtk_target_list_add_text_targets()
    */
+  init_atoms ();
   for (i = 0; i < n_targets; i++)
     {
       if (targets[i] == utf8_atom ||
@@ -1608,7 +1839,57 @@ gtk_targets_include_text (GdkAtom *targets,
   
   return result;
 }
-                                   
+
+/**
+ * gtk_targets_include_rich_text:
+ * @targets: an array of #GdkAtom<!-- -->s
+ * @n_targets: the length of @targets
+ * @buffer: a #GtkTextBuffer
+ *
+ * Determines if any of the targets in @targets can be used to
+ * provide rich text.
+ *
+ * Return value: %TRUE if @targets include a suitable target for rich text,
+ *               otherwise %FALSE.
+ *
+ * Since: 2.10
+ **/
+gboolean
+gtk_targets_include_rich_text (GdkAtom       *targets,
+                               gint           n_targets,
+                               GtkTextBuffer *buffer)
+{
+  GdkAtom *rich_targets;
+  gint n_rich_targets;
+  gint i, j;
+  gboolean result = FALSE;
+
+  g_return_val_if_fail (targets != NULL || n_targets == 0, FALSE);
+  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
+
+  init_atoms ();
+
+  rich_targets = gtk_text_buffer_get_deserialize_formats (buffer,
+                                                          &n_rich_targets);
+
+  for (i = 0; i < n_targets; i++)
+    {
+      for (j = 0; j < n_rich_targets; j++)
+        {
+          if (targets[i] == rich_targets[j])
+            {
+              result = TRUE;
+              goto done;
+            }
+        }
+    }
+
+ done:
+  g_free (rich_targets);
+
+  return result;
+}
+
 /**
  * gtk_selection_data_targets_include_text:
  * @selection_data: a #GtkSelectionData object
@@ -1627,6 +1908,8 @@ gtk_selection_data_targets_include_text (GtkSelectionData *selection_data)
   gint n_targets;
   gboolean result = FALSE;
 
+  g_return_val_if_fail (selection_data != NULL, FALSE);
+
   init_atoms ();
 
   if (gtk_selection_data_get_targets (selection_data, &targets, &n_targets))
@@ -1638,6 +1921,43 @@ gtk_selection_data_targets_include_text (GtkSelectionData *selection_data)
   return result;
 }
 
+/**
+ * gtk_selection_data_targets_include_rich_text:
+ * @selection_data: a #GtkSelectionData object
+ * @buffer: a #GtkTextBuffer
+ *
+ * Given a #GtkSelectionData object holding a list of targets,
+ * determines if any of the targets in @targets can be used to
+ * provide rich text.
+ *
+ * Return value: %TRUE if @selection_data holds a list of targets,
+ *               and a suitable target for rich text is included,
+ *               otherwise %FALSE.
+ *
+ * Since: 2.10
+ **/
+gboolean
+gtk_selection_data_targets_include_rich_text (GtkSelectionData *selection_data,
+                                              GtkTextBuffer    *buffer)
+{
+  GdkAtom *targets;
+  gint n_targets;
+  gboolean result = FALSE;
+
+  g_return_val_if_fail (selection_data != NULL, FALSE);
+  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
+
+  init_atoms ();
+
+  if (gtk_selection_data_get_targets (selection_data, &targets, &n_targets))
+    {
+      result = gtk_targets_include_rich_text (targets, n_targets, buffer);
+      g_free (targets);
+    }
+
+  return result;
+}
+
 /**
  * gtk_targets_include_image:
  * @targets: an array of #GdkAtom<!-- -->s
@@ -1663,6 +1983,8 @@ gtk_targets_include_image (GdkAtom *targets,
   gint i;
   gboolean result = FALSE;
 
+  g_return_val_if_fail (targets != NULL || n_targets == 0, FALSE);
+
   list = gtk_target_list_new (NULL, 0);
   gtk_target_list_add_image_targets (list, 0, writable);
   for (i = 0; i < n_targets && !result; i++)
@@ -1705,6 +2027,8 @@ gtk_selection_data_targets_include_image (GtkSelectionData *selection_data,
   gint n_targets;
   gboolean result = FALSE;
 
+  g_return_val_if_fail (selection_data != NULL, FALSE);
+
   init_atoms ();
 
   if (gtk_selection_data_get_targets (selection_data, &targets, &n_targets))
@@ -1736,8 +2060,13 @@ gtk_targets_include_uri (GdkAtom *targets,
   gint i;
   gboolean result = FALSE;
 
+  g_return_val_if_fail (targets != NULL || n_targets == 0, FALSE);
+
   /* Keep in sync with gtk_target_list_add_uri_targets()
    */
+
+  init_atoms ();
+
   for (i = 0; i < n_targets; i++)
     {
       if (targets[i] == text_uri_list_atom)
@@ -1759,7 +2088,7 @@ gtk_targets_include_uri (GdkAtom *targets,
  * provide a list or URIs.
  * 
  * Return value: %TRUE if @selection_data holds a list of targets,
- *   and a suitable target for text is included, otherwise %FALSE.
+ *   and a suitable target for URI lists is included, otherwise %FALSE.
  *
  * Since: 2.10
  **/
@@ -1770,6 +2099,8 @@ gtk_selection_data_targets_include_uri (GtkSelectionData *selection_data)
   gint n_targets;
   gboolean result = FALSE;
 
+  g_return_val_if_fail (selection_data != NULL, FALSE);
+
   init_atoms ();
 
   if (gtk_selection_data_get_targets (selection_data, &targets, &n_targets))
@@ -1813,7 +2144,7 @@ gtk_selection_init (void)
  * 
  * Since: 2.2
  *
- * Deprecated: Instead of calling this function, chain up from
+ * Deprecated: 2.4: Instead of calling this function, chain up from
  * your selection_clear_event handler. Calling this function
  * from any other context is illegal. 
  **/
@@ -1844,7 +2175,7 @@ gtk_selection_clear (GtkWidget         *widget,
     {
       current_selections = g_list_remove_link (current_selections, tmp_list);
       g_list_free (tmp_list);
-      g_free (selection_info);
+      g_slice_free (GtkSelectionInfo, selection_info);
     }
   
   return TRUE;
@@ -1892,7 +2223,7 @@ _gtk_selection_request (GtkWidget *widget,
   if (tmp_list == NULL)
     return FALSE;
   
-  info = g_new (GtkIncrInfo, 1);
+  info = g_slice_new (GtkIncrInfo);
 
   g_object_ref (widget);
   
@@ -1930,7 +2261,8 @@ _gtk_selection_request (GtkWidget *widget,
                                                 GDK_NONE, 
                                                 event->time);
          g_free (mult_atoms);
-         g_free (info);
+         g_slice_free (GtkIncrInfo, info);
+          gdk_error_trap_pop ();
          return TRUE;
        }
       gdk_error_trap_pop ();
@@ -1967,6 +2299,8 @@ _gtk_selection_request (GtkWidget *widget,
              info->conversions[i].target = ((GdkAtom *)mult_atoms)[2*i];
              info->conversions[i].property = ((GdkAtom *)mult_atoms)[2*i+1];
            }
+
+         g_free (mult_atoms);
        }
     }
   else                         /* only a single conversion */
@@ -2060,7 +2394,7 @@ _gtk_selection_request (GtkWidget *widget,
                             gdk_window_get_events (info->requestor) |
                             GDK_PROPERTY_CHANGE_MASK);
       current_incrs = g_list_append (current_incrs, info);
-      g_timeout_add (1000, (GSourceFunc) gtk_selection_incr_timeout, info);
+      gdk_threads_add_timeout (1000, (GSourceFunc) gtk_selection_incr_timeout, info);
     }
   
   /* If it was a MULTIPLE request, set the property to indicate which
@@ -2105,7 +2439,7 @@ _gtk_selection_request (GtkWidget *widget,
   if (info->num_incrs == 0)
     {
       g_free (info->conversions);
-      g_free (info);
+      g_slice_free (GtkIncrInfo, info);
     }
 
   g_object_unref (widget);
@@ -2249,8 +2583,6 @@ gtk_selection_incr_timeout (GtkIncrInfo *info)
   GList *tmp_list;
   gboolean retval;
 
-  GDK_THREADS_ENTER ();
-  
   /* Determine if retrieval has finished by checking if it still in
      list of pending retrievals */
   
@@ -2275,7 +2607,7 @@ gtk_selection_incr_timeout (GtkIncrInfo *info)
       /* FIXME: we should check if requestor window is still in use,
         and if not, remove it? */
       
-      g_free (info);
+      g_slice_free (GtkIncrInfo, info);
       
       retval =  FALSE;         /* remove timeout */
     }
@@ -2286,8 +2618,6 @@ gtk_selection_incr_timeout (GtkIncrInfo *info)
       retval = TRUE;           /* timeout will happen again */
     }
   
-  GDK_THREADS_LEAVE ();
-
   return retval;
 }
 
@@ -2493,14 +2823,12 @@ _gtk_selection_property_notify (GtkWidget       *widget,
  *   results:
  *************************************************************/
 
-static gint
+static gboolean
 gtk_selection_retrieval_timeout (GtkRetrievalInfo *info)
 {
   GList *tmp_list;
   gboolean retval;
 
-  GDK_THREADS_ENTER ();
-  
   /* Determine if retrieval has finished by checking if it still in
      list of pending retrievals */
   
@@ -2523,7 +2851,7 @@ gtk_selection_retrieval_timeout (GtkRetrievalInfo *info)
        }
       
       g_free (info->buffer);
-      g_free (info);
+      g_slice_free (GtkRetrievalInfo, info);
       
       retval =  FALSE;         /* remove timeout */
     }
@@ -2534,8 +2862,6 @@ gtk_selection_retrieval_timeout (GtkRetrievalInfo *info)
       retval =  TRUE;          /* timeout will happen again */
     }
 
-  GDK_THREADS_LEAVE ();
-
   return retval;
 }
 
@@ -2713,7 +3039,7 @@ gtk_selection_data_copy (GtkSelectionData *data)
   
   g_return_val_if_fail (data != NULL, NULL);
   
-  new_data = g_new (GtkSelectionData, 1);
+  new_data = g_slice_new (GtkSelectionData);
   *new_data = *data;
 
   if (data->data)
@@ -2737,10 +3063,9 @@ gtk_selection_data_free (GtkSelectionData *data)
 {
   g_return_if_fail (data != NULL);
   
-  if (data->data)
-    g_free (data->data);
+  g_free (data->data);
   
-  g_free (data);
+  g_slice_free (GtkSelectionData, data);
 }
 
 GType
@@ -2756,6 +3081,19 @@ gtk_selection_data_get_type (void)
   return our_type;
 }
 
+GType
+gtk_target_list_get_type (void)
+{
+  static GType our_type = 0;
+
+  if (our_type == 0)
+    our_type = g_boxed_type_register_static (I_("GtkTargetList"),
+                                            (GBoxedCopyFunc) gtk_target_list_ref,
+                                            (GBoxedFreeFunc) gtk_target_list_unref);
+
+  return our_type;
+}
+
 static int 
 gtk_selection_bytes_per_item (gint format)
 {