]> Pileus Git - ~andy/gtk/commitdiff
Add convenience api for image dnd (#150165):
authorMatthias Clasen <mclasen@redhat.com>
Thu, 21 Oct 2004 04:53:23 +0000 (04:53 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Thu, 21 Oct 2004 04:53:23 +0000 (04:53 +0000)
2004-10-21  Matthias Clasen  <mclasen@redhat.com>

Add convenience api for image dnd (#150165):

* gtk/gtkselection.h:
* gtk/gtkselection.c (gtk_target_list_add_image_targets)
(gtk_selection_data_set_pixbuf, gtk_selection_data_get_pixbuf):
New functions to handle the image formats readable/writable
by gdk-pixbuf.

* gtk/gtkdnd.h:
* gtk/gtkdnd.c (gtk_drag_dest_add_image_targets)
(gtk_drag_source_add_text_targets): New functions to handle
the image formats readable/writable by gdk-pixbuf.

ChangeLog
ChangeLog.pre-2-10
ChangeLog.pre-2-6
ChangeLog.pre-2-8
gtk/gtkdnd.c
gtk/gtkdnd.h
gtk/gtkselection.c
gtk/gtkselection.h

index 2ebf9fe2885b24f33866845893e2da3ca72b5680..78291168984b7284e1f43be60643688cc4ff1625 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2004-10-21  Matthias Clasen  <mclasen@redhat.com>
+
+       Add convenience api for image dnd (#150165):
+       
+       * gtk/gtkselection.h: 
+       * gtk/gtkselection.c (gtk_target_list_add_image_targets) 
+       (gtk_selection_data_set_pixbuf, gtk_selection_data_get_pixbuf): 
+       New functions to handle the image formats readable/writable 
+       by gdk-pixbuf. 
+       
+       * gtk/gtkdnd.h: 
+       * gtk/gtkdnd.c (gtk_drag_dest_add_image_targets) 
+       (gtk_drag_source_add_text_targets): New functions to handle
+       the image formats readable/writable by gdk-pixbuf. 
+
 2004-10-20  Matthias Clasen  <mclasen@redhat.com>
 
        * gtk/gtktreemodelfilter.c (gtk_tree_model_filter_new): Make
index 2ebf9fe2885b24f33866845893e2da3ca72b5680..78291168984b7284e1f43be60643688cc4ff1625 100644 (file)
@@ -1,3 +1,18 @@
+2004-10-21  Matthias Clasen  <mclasen@redhat.com>
+
+       Add convenience api for image dnd (#150165):
+       
+       * gtk/gtkselection.h: 
+       * gtk/gtkselection.c (gtk_target_list_add_image_targets) 
+       (gtk_selection_data_set_pixbuf, gtk_selection_data_get_pixbuf): 
+       New functions to handle the image formats readable/writable 
+       by gdk-pixbuf. 
+       
+       * gtk/gtkdnd.h: 
+       * gtk/gtkdnd.c (gtk_drag_dest_add_image_targets) 
+       (gtk_drag_source_add_text_targets): New functions to handle
+       the image formats readable/writable by gdk-pixbuf. 
+
 2004-10-20  Matthias Clasen  <mclasen@redhat.com>
 
        * gtk/gtktreemodelfilter.c (gtk_tree_model_filter_new): Make
index 2ebf9fe2885b24f33866845893e2da3ca72b5680..78291168984b7284e1f43be60643688cc4ff1625 100644 (file)
@@ -1,3 +1,18 @@
+2004-10-21  Matthias Clasen  <mclasen@redhat.com>
+
+       Add convenience api for image dnd (#150165):
+       
+       * gtk/gtkselection.h: 
+       * gtk/gtkselection.c (gtk_target_list_add_image_targets) 
+       (gtk_selection_data_set_pixbuf, gtk_selection_data_get_pixbuf): 
+       New functions to handle the image formats readable/writable 
+       by gdk-pixbuf. 
+       
+       * gtk/gtkdnd.h: 
+       * gtk/gtkdnd.c (gtk_drag_dest_add_image_targets) 
+       (gtk_drag_source_add_text_targets): New functions to handle
+       the image formats readable/writable by gdk-pixbuf. 
+
 2004-10-20  Matthias Clasen  <mclasen@redhat.com>
 
        * gtk/gtktreemodelfilter.c (gtk_tree_model_filter_new): Make
index 2ebf9fe2885b24f33866845893e2da3ca72b5680..78291168984b7284e1f43be60643688cc4ff1625 100644 (file)
@@ -1,3 +1,18 @@
+2004-10-21  Matthias Clasen  <mclasen@redhat.com>
+
+       Add convenience api for image dnd (#150165):
+       
+       * gtk/gtkselection.h: 
+       * gtk/gtkselection.c (gtk_target_list_add_image_targets) 
+       (gtk_selection_data_set_pixbuf, gtk_selection_data_get_pixbuf): 
+       New functions to handle the image formats readable/writable 
+       by gdk-pixbuf. 
+       
+       * gtk/gtkdnd.h: 
+       * gtk/gtkdnd.c (gtk_drag_dest_add_image_targets) 
+       (gtk_drag_source_add_text_targets): New functions to handle
+       the image formats readable/writable by gdk-pixbuf. 
+
 2004-10-20  Matthias Clasen  <mclasen@redhat.com>
 
        * gtk/gtktreemodelfilter.c (gtk_tree_model_filter_new): Make
index 61061e3f7685d7919ec391e2e308a364a9eba1d0..60b88ceac02e073cbb3b3b2fbcf3ba22bd5d317c 100644 (file)
@@ -1049,6 +1049,30 @@ gtk_drag_dest_add_text_targets (GtkWidget *widget)
   gtk_target_list_unref (target_list);
 }
 
+/**
+ * gtk_drag_dest_add_image_targets:
+ * @widget: a #GtkWidget that's a drag destination
+ *
+ * Add the image targets supported by #GtkSelection to
+ * the target list of the drag destination.  
+ * 
+ * Since: 2.6
+ **/
+void
+gtk_drag_dest_add_image_targets (GtkWidget *widget)
+{
+  GtkTargetList *target_list;
+
+  target_list = gtk_drag_dest_get_target_list (widget);
+  if (target_list)
+    gtk_target_list_ref (target_list);
+  else
+    target_list = gtk_target_list_new (NULL, 0);
+  gtk_target_list_add_image_targets (target_list, FALSE);
+  gtk_drag_dest_set_target_list (widget, target_list);
+  gtk_target_list_unref (target_list);
+}
+
 /*************************************************************
  * _gtk_drag_dest_handle_event:
  *     Called from widget event handling code on Drag events
@@ -2216,6 +2240,30 @@ gtk_drag_source_add_text_targets (GtkWidget *widget)
   gtk_target_list_unref (target_list);
 }
 
+/**
+ * gtk_drag_source_add_image_targets:
+ * @widget: a #GtkWidget that's is a drag source
+ *
+ * Add the writable image targets supported by #GtkSelection to
+ * the target list of the drag source.  
+ * 
+ * Since: 2.6
+ **/
+void
+gtk_drag_source_add_image_targets (GtkWidget *widget)
+{
+  GtkTargetList *target_list;
+
+  target_list = gtk_drag_source_get_target_list (widget);
+  if (target_list)
+    gtk_target_list_ref (target_list);
+  else
+    target_list = gtk_target_list_new (NULL, 0);
+  gtk_target_list_add_image_targets (target_list, TRUE);
+  gtk_drag_source_set_target_list (widget, target_list);
+  gtk_target_list_unref (target_list);
+}
+
 static void
 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
 {
index bfcbb50c37e4d2773ec456445d62f4b7807c853f..1932340290ed3e1a1f5b56ef822a08ee341d6946 100644 (file)
@@ -86,6 +86,7 @@ GtkTargetList* gtk_drag_dest_get_target_list (GtkWidget      *widget);
 void           gtk_drag_dest_set_target_list (GtkWidget      *widget,
                                               GtkTargetList  *target_list);
 void           gtk_drag_dest_add_text_targets (GtkWidget     *widget);
+void           gtk_drag_dest_add_image_targets (GtkWidget    *widget);
 
 /* Source side */
 
@@ -101,6 +102,7 @@ GtkTargetList* gtk_drag_source_get_target_list (GtkWidget     *widget);
 void           gtk_drag_source_set_target_list (GtkWidget     *widget,
                                                 GtkTargetList *target_list);
 void           gtk_drag_source_add_text_targets (GtkWidget     *widget);
+void           gtk_drag_source_add_image_targets (GtkWidget    *widget);
 
 void gtk_drag_source_set_icon        (GtkWidget   *widget,
                                      GdkColormap *colormap,
index e9fab860148797b43e603b0d1bd7c1a7a3cea8db..44f39e3b2b12f6497ce6330220858c6f291c8022 100644 (file)
@@ -59,6 +59,7 @@
 #include "gtkalias.h"
 #include "gtkmain.h"
 #include "gtkselection.h"
+#include "gdk-pixbuf/gdk-pixbuf.h"
 
 #ifdef GDK_WINDOWING_X11
 #include "x11/gdkx.h"
@@ -294,6 +295,49 @@ gtk_target_list_add_text_targets (GtkTargetList *list)
   gtk_target_list_add (list, text_plain_atom, 0, 0);  
 }
 
+/**
+ * gtk_target_list_add_image_targets:
+ * @list: a #GtkTargetList
+ * @writable: whether to add only targets for which GTK+ knows
+ *   how to convert a pixbuf into the format
+ * 
+ * Adds the image targets supported by #GtkSelection to
+ * the target list. The targets are added with both flags
+ * and info being zero.
+ * 
+ * Since: 2.6
+ **/
+void 
+gtk_target_list_add_image_targets (GtkTargetList *list,
+                                  gboolean       writable)
+{
+  GSList *formats, *f;
+  gchar **mimes, **m;
+  GdkAtom atom;
+
+  g_return_if_fail (list != NULL);
+
+  formats = gdk_pixbuf_get_formats ();
+
+  for (f = formats; f; f = f->next)
+    {
+      GdkPixbufFormat *fmt = f->data;
+
+      if (writable && !gdk_pixbuf_format_is_writable (fmt))
+       continue;
+      
+      mimes = gdk_pixbuf_format_get_mime_types (fmt);
+      for (m = mimes; *m; m++)
+       {
+         atom = gdk_atom_intern (*m, FALSE);
+         gtk_target_list_add (list, atom, 0, 0);  
+       }
+      g_strfreev (mimes);
+    }
+
+  g_slist_free (formats);
+}
+
 void               
 gtk_target_list_add_table (GtkTargetList        *list,
                           const GtkTargetEntry *targets,
@@ -1157,6 +1201,107 @@ gtk_selection_data_get_text (GtkSelectionData *selection_data)
   return result;
 }
 
+/**
+ * gtk_selection_data_set_pixbuf:
+ * @selection_data: a #GtkSelectionData
+ * @pixbuf: a #GdkPixbuf
+ * 
+ * Sets the contents of the selection from a #GdkPixbuf
+ * The pixbuf is converted to the form determined by
+ * @selection_data->target.
+ * 
+ * Return value: %TRUE if the selection was successfully set,
+ *   otherwise %FALSE.
+ *
+ * Since: 2.6
+ **/
+gboolean
+gtk_selection_data_set_pixbuf (GtkSelectionData *selection_data,
+                              GdkPixbuf        *pixbuf)
+{
+  GSList *formats, *f;
+  gchar **mimes, **m;
+  GdkAtom atom;
+  gboolean result;
+  gchar *str, *type;
+  gsize len;
+
+  formats = gdk_pixbuf_get_formats ();
+
+  for (f = formats; f; f = f->next)
+    {
+      GdkPixbufFormat *fmt = f->data;
+
+      mimes = gdk_pixbuf_format_get_mime_types (fmt);
+      for (m = mimes; *m; m++)
+       {
+         atom = gdk_atom_intern (*m, FALSE);
+         if (selection_data->target == atom)
+           {
+             str = NULL;
+             type = gdk_pixbuf_format_get_name (fmt),
+             result = gdk_pixbuf_save_to_buffer (pixbuf,
+                                                 &str,
+                                                 &len,
+                                                 type,
+                                                 NULL);
+             if (result) 
+               gtk_selection_data_set (selection_data,
+                                       atom, 8, (guchar *)str, len);
+             g_free (type);
+             g_free (str);
+             g_strfreev (mimes);
+             g_slist_free (formats);
+             
+             return result;
+           }
+       }
+
+      g_strfreev (mimes);
+    }
+
+  g_slist_free (formats);
+  return FALSE;
+}
+
+/**
+ * gtk_selection_data_get_pixbuf:
+ * @selection_data: a #GtkSelectionData
+ * 
+ * Gets the contents of the selection data as a #GdkPixbuf.
+ * 
+ * Return value: if the selection data contained a recognized
+ *   image type and it could be converted to a #GdkPixbuf, a 
+ *   newly allocated pixbuf is returned, otherwise %NULL.
+ *   If the result is non-%NULL it must be freed with g_object_unref().
+ *
+ * Since: 2.6
+ **/
+GdkPixbuf *
+gtk_selection_data_get_pixbuf (GtkSelectionData *selection_data)
+{
+  GdkPixbufLoader *loader;
+  GdkPixbuf *result = NULL;
+
+  loader = gdk_pixbuf_loader_new ();
+  
+  if (gdk_pixbuf_loader_write (loader, 
+                              selection_data->data,
+                              selection_data->length,
+                              NULL))
+    result = gdk_pixbuf_loader_get_pixbuf (loader);
+  
+  if (result)
+    g_object_ref (result);
+
+  gdk_pixbuf_loader_close (loader, NULL);
+  g_object_unref (loader);
+  
+  return result;
+}
+
+
 /**
  * gtk_selection_data_get_targets:
  * @selection_data: a #GtkSelectionData object
index 9e398a8e6db211992533fc53838653368ad8faa2..02bbd8a0f09dc8419c4ad1bb5570373b7e240fd2 100644 (file)
@@ -96,6 +96,8 @@ void           gtk_target_list_add       (GtkTargetList  *list,
                                          guint           flags,
                                          guint           info);
 void           gtk_target_list_add_text_targets (GtkTargetList  *list);
+void           gtk_target_list_add_image_targets (GtkTargetList  *list,
+                                                 gboolean        writable);
 void           gtk_target_list_add_table (GtkTargetList        *list,
                                          const GtkTargetEntry *targets,
                                          guint                 ntargets);
@@ -138,6 +140,9 @@ gboolean gtk_selection_data_set_text (GtkSelectionData     *selection_data,
                                      const gchar          *str,
                                      gint                  len);
 guchar * gtk_selection_data_get_text (GtkSelectionData     *selection_data);
+gboolean gtk_selection_data_set_pixbuf (GtkSelectionData   *selection_data,
+                                       GdkPixbuf          *pixbuf);
+GdkPixbuf *gtk_selection_data_get_pixbuf (GtkSelectionData *selection_data);
 
 gboolean gtk_selection_data_get_targets          (GtkSelectionData  *selection_data,
                                                  GdkAtom          **targets,