diff options
author | Matthias Clasen <mclasen@redhat.com> | 2004-10-21 04:53:23 +0000 |
---|---|---|
committer | Matthias Clasen <matthiasc@src.gnome.org> | 2004-10-21 04:53:23 +0000 |
commit | 968b192fecf7e52830d2355904ffc557908dea73 (patch) | |
tree | 157186eaf1b03577564931b5d2ac583bd9bcb44c /gtk/gtkselection.c | |
parent | bfb64fa04f98fc9f7eb5f4da39ab409934f1afd2 (diff) | |
download | gtk+-968b192fecf7e52830d2355904ffc557908dea73.tar.gz |
Add convenience api for image dnd (#150165):
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.
Diffstat (limited to 'gtk/gtkselection.c')
-rw-r--r-- | gtk/gtkselection.c | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/gtk/gtkselection.c b/gtk/gtkselection.c index e9fab86014..44f39e3b2b 100644 --- a/gtk/gtkselection.c +++ b/gtk/gtkselection.c @@ -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, @@ -1158,6 +1202,107 @@ gtk_selection_data_get_text (GtkSelectionData *selection_data) } /** + * 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 * @targets: location to store an array of targets. The result |