summaryrefslogtreecommitdiff
path: root/gtk
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2004-10-22 22:50:32 +0000
committerMatthias Clasen <matthiasc@src.gnome.org>2004-10-22 22:50:32 +0000
commita811584d01706df3651a93a3b0c2c9f86249ca52 (patch)
tree224c8567dc2065e9f428f8c307db07963690a92f /gtk
parent9d0a283f341aaf37a568579715b6c92fa518fd89 (diff)
downloadgtk+-a811584d01706df3651a93a3b0c2c9f86249ca52.tar.gz
Use the new file DND api.
2004-10-22 Matthias Clasen <mclasen@redhat.com> * gtk/gtkfilechooserbutton.c (gtk_file_chooser_button_drag_data_received) (gtk_file_chooser_button_init): Use the new file DND api. * gtk/gtkdnd.h: * gtk/gtkdnd.c (gtk_drag_dest_add_uri_targets) (gtk_drag_source_add_uri_targets): * gtk/gtkselection.h: * gtk/gtkselection.c (gtk_target_list_add_uri_targets): (gtk_selection_data_set_uris): (gtk_selection_data_get_uris): New functions to allow handling file DND "without knowing jack shit". * configure.in: Bump glib requirement to 2.5.4. * gtk/gtkfilechooserdefault.c (shortcuts_drop_uris): Use g_uri_list_extract_uris().
Diffstat (limited to 'gtk')
-rw-r--r--gtk/gtkdnd.c60
-rw-r--r--gtk/gtkdnd.h6
-rw-r--r--gtk/gtkfilechooserbutton.c15
-rw-r--r--gtk/gtkfilechooserdefault.c61
-rw-r--r--gtk/gtkselection.c118
-rw-r--r--gtk/gtkselection.h9
6 files changed, 198 insertions, 71 deletions
diff --git a/gtk/gtkdnd.c b/gtk/gtkdnd.c
index a2edb049ea..c59bd0f995 100644
--- a/gtk/gtkdnd.c
+++ b/gtk/gtkdnd.c
@@ -1079,6 +1079,33 @@ gtk_drag_dest_add_image_targets (GtkWidget *widget)
gtk_target_list_unref (target_list);
}
+/**
+ * gtk_drag_dest_add_uri_targets:
+ * @widget: a #GtkWidget that's a drag destination
+ *
+ * Add the URI targets supported by #GtkSelection to
+ * the target list of the drag destination. The targets
+ * are added with @info = 0. If you need another value,
+ * use gtk_target_list_add_uri_targets() and
+ * gtk_drag_dest_set_target_list().
+ *
+ * Since: 2.6
+ **/
+void
+gtk_drag_dest_add_uri_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_uri_targets (target_list, 0);
+ 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
@@ -2230,7 +2257,7 @@ gtk_drag_source_set_target_list (GtkWidget *widget,
* the target list of the drag source. The targets
* are added with @info = 0. If you need another value,
* use gtk_target_list_add_text_targets() and
- * gtk_drag_dest_set_target_list().
+ * gtk_drag_source_set_target_list().
*
* Since: 2.6
**/
@@ -2256,8 +2283,8 @@ gtk_drag_source_add_text_targets (GtkWidget *widget)
* Add the writable image targets supported by #GtkSelection to
* the target list of the drag source. The targets
* are added with @info = 0. If you need another value,
- * use gtk_target_list_add_text_targets() and
- * gtk_drag_dest_set_target_list().
+ * use gtk_target_list_add_image_targets() and
+ * gtk_drag_source_set_target_list().
*
* Since: 2.6
**/
@@ -2276,6 +2303,33 @@ gtk_drag_source_add_image_targets (GtkWidget *widget)
gtk_target_list_unref (target_list);
}
+/**
+ * gtk_drag_source_add_uri_targets:
+ * @widget: a #GtkWidget that's is a drag source
+ *
+ * Add the URI targets supported by #GtkSelection to
+ * the target list of the drag source. The targets
+ * are added with @info = 0. If you need another value,
+ * use gtk_target_list_add_uri_targets() and
+ * gtk_drag_source_set_target_list().
+ *
+ * Since: 2.6
+ **/
+void
+gtk_drag_source_add_uri_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_uri_targets (target_list, 0);
+ gtk_drag_source_set_target_list (widget, target_list);
+ gtk_target_list_unref (target_list);
+}
+
static void
gtk_drag_source_unset_icon (GtkDragSourceSite *site)
{
diff --git a/gtk/gtkdnd.h b/gtk/gtkdnd.h
index 1932340290..586980b061 100644
--- a/gtk/gtkdnd.h
+++ b/gtk/gtkdnd.h
@@ -85,8 +85,9 @@ GdkAtom gtk_drag_dest_find_target (GtkWidget *widget,
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_text_targets (GtkWidget *widget);
void gtk_drag_dest_add_image_targets (GtkWidget *widget);
+void gtk_drag_dest_add_uri_targets (GtkWidget *widget);
/* Source side */
@@ -101,8 +102,9 @@ void gtk_drag_source_unset (GtkWidget *widget);
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_text_targets (GtkWidget *widget);
void gtk_drag_source_add_image_targets (GtkWidget *widget);
+void gtk_drag_source_add_uri_targets (GtkWidget *widget);
void gtk_drag_source_set_icon (GtkWidget *widget,
GdkColormap *colormap,
diff --git a/gtk/gtkfilechooserbutton.c b/gtk/gtkfilechooserbutton.c
index 01f9d9dace..5ebdac05b5 100644
--- a/gtk/gtkfilechooserbutton.c
+++ b/gtk/gtkfilechooserbutton.c
@@ -58,7 +58,6 @@
#define DEFAULT_FILENAME N_("(None)")
#define DEFAULT_SPACING 0
-
/* ********************** *
* Private Enumerations *
* ********************** */
@@ -105,11 +104,6 @@ enum
TEXT_URI_LIST
};
-static const GtkTargetEntry drop_targets[] = {
- { "text/uri-list", 0, TEXT_URI_LIST }
-};
-
-
/* ********************* *
* Function Prototypes *
* ********************* */
@@ -291,9 +285,11 @@ gtk_file_chooser_button_init (GtkFileChooserButton *button)
gtk_drag_dest_unset (priv->entry);
gtk_drag_dest_set (GTK_WIDGET (button),
(GTK_DEST_DEFAULT_ALL),
- drop_targets, G_N_ELEMENTS (drop_targets),
+ NULL, 0,
GDK_ACTION_COPY);
gtk_drag_dest_add_text_targets (GTK_WIDGET (button));
+ gtk_target_list_add_uri_targets (gtk_drag_dest_get_target_list (GTK_WIDGET (button)),
+ TEXT_URI_LIST);
}
@@ -343,7 +339,8 @@ gtk_file_chooser_button_set_property (GObject *object,
case GTK_FILE_CHOOSER_PROP_ACTION:
g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
- _gtk_file_chooser_entry_set_action (priv->entry, g_value_get_enum (value));
+ _gtk_file_chooser_entry_set_action (priv->entry,
+ (GtkFileChooserAction)g_value_get_enum (value));
switch (g_value_get_enum (value))
{
@@ -491,7 +488,7 @@ gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
guint i;
gboolean selected;
- uris = g_strsplit (data->data, "\r\n", -1);
+ uris = gtk_selection_data_get_uris (data);
if (uris == NULL)
break;
diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c
index 8a4d53bc38..f6a9b5c85a 100644
--- a/gtk/gtkfilechooserdefault.c
+++ b/gtk/gtkfilechooserdefault.c
@@ -2478,70 +2478,23 @@ shortcuts_drag_drop_cb (GtkWidget *widget,
return TRUE;
}
-/* Converts raw selection data from text/uri-list to a list of strings. */
-static GSList *
-split_uris (const char *data)
-{
- GSList *uris;
- const char *p, *q;
-
- uris = NULL;
-
- p = data;
-
- /* We don't actually try to validate the URI according to RFC
- * 2396, or even check for allowed characters - we just ignore
- * comments and trim whitespace off the ends. We also
- * allow LF delimination as well as the specified CRLF.
- *
- * We do allow comments like specified in RFC 2483.
- */
- while (p)
- {
- if (*p != '#')
- {
- while (g_ascii_isspace (*p))
- p++;
-
- q = p;
- while (*q && (*q != '\n') && (*q != '\r'))
- q++;
-
- if (q > p)
- {
- q--;
- while (q > p && g_ascii_isspace (*q))
- q--;
-
- if (q > p)
- uris = g_slist_prepend (uris, g_strndup (p, q - p + 1));
- }
- }
- p = strchr (p, '\n');
- if (p)
- p++;
- }
-
- uris = g_slist_reverse (uris);
- return uris;
-}
-
/* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
static void
shortcuts_drop_uris (GtkFileChooserDefault *impl,
const char *data,
int position)
{
- GSList *uris, *l;
+ gchar **uris;
+ gint i;
- uris = split_uris (data);
+ uris = g_uri_list_extract_uris (data);
- for (l = uris; l; l = l->next)
+ for (i = 0; uris[i]; i++)
{
char *uri;
GtkFilePath *path;
- uri = l->data;
+ uri = uris[i];
path = gtk_file_system_uri_to_path (impl->file_system, uri);
if (path)
@@ -2560,11 +2513,9 @@ shortcuts_drop_uris (GtkFileChooserDefault *impl,
error_message (impl, msg);
g_free (msg);
}
-
- g_free (uri);
}
- g_slist_free (uris);
+ g_strfreev (uris);
}
/* Reorders the selected bookmark to the specified position */
diff --git a/gtk/gtkselection.c b/gtk/gtkselection.c
index b792a3cf93..7b3230843c 100644
--- a/gtk/gtkselection.c
+++ b/gtk/gtkselection.c
@@ -246,6 +246,7 @@ static GdkAtom ctext_atom;
static GdkAtom text_plain_atom;
static GdkAtom text_plain_utf8_atom;
static GdkAtom text_plain_locale_atom;
+static GdkAtom text_uri_list_atom;
static void
init_atoms (void)
@@ -264,6 +265,8 @@ init_atoms (void)
tmp = g_strdup_printf ("text/plain;charset=%s", charset);
text_plain_locale_atom = gdk_atom_intern (tmp, FALSE);
g_free (tmp);
+
+ text_uri_list_atom = gdk_atom_intern ("text/uri-list", FALSE);
}
}
@@ -340,6 +343,27 @@ gtk_target_list_add_image_targets (GtkTargetList *list,
g_slist_free (formats);
}
+/**
+ * gtk_target_list_add_uri_targets:
+ * @list: a #GtkTargetList
+ * @info: an ID that will be passed back to the application
+ *
+ * Adds the URI targets supported by #GtkSelection to
+ * the target list. All targets are added with the same @info.
+ *
+ * Since: 2.6
+ **/
+void
+gtk_target_list_add_uri_targets (GtkTargetList *list,
+ guint info)
+{
+ g_return_if_fail (list != NULL);
+
+ init_atoms ();
+
+ gtk_target_list_add (list, text_uri_list_atom, 0, info);
+}
+
void
gtk_target_list_add_table (GtkTargetList *list,
const GtkTargetEntry *targets,
@@ -1303,6 +1327,100 @@ gtk_selection_data_get_pixbuf (GtkSelectionData *selection_data)
return result;
}
+/**
+ * gtk_selection_data_set_uris:
+ * @selection_data: a #GtkSelectionData
+ * @uris: a %NULL-terminated array of strings hilding URIs
+ *
+ * Sets the contents of the selection from a list of URIs.
+ * The string 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_uris (GtkSelectionData *selection_data,
+ gchar **uris)
+{
+ init_atoms ();
+
+ if (selection_data->target == text_uri_list_atom)
+ {
+ GString *list;
+ gint i;
+ gchar *result;
+ gsize length;
+
+ list = g_string_new (0);
+ for (i = 0; uris[i]; i++)
+ {
+ g_string_append (list, uris[i]);
+ g_string_append (list, "\r\n");
+ }
+
+ result = g_convert (list->str, list->len,
+ "ASCII", "UTF-8",
+ NULL, &length, NULL);
+ g_string_free (list, TRUE);
+
+ if (result)
+ {
+ gtk_selection_data_set (selection_data,
+ text_uri_list_atom,
+ 8, (guchar *)result, length);
+
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+/**
+ * gtk_selection_data_get_uris:
+ * @selection_data: a #GtkSelectionData
+ *
+ * Gets the contents of the selection data as array of URIs.
+ *
+ * Return value: if the selection data contains a list of
+ * URIs, a newly allocated %NULL-terminated string array
+ * containing the URIs, otherwise %NULL. If the result is
+ * non-%NULL it must be freed with g_strfreev().
+ *
+ * Since: 2.6
+ **/
+gchar **
+gtk_selection_data_get_uris (GtkSelectionData *selection_data)
+{
+ gchar **result = 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,
+ selection_data->data,
+ selection_data->length,
+ &list);
+ if (count > 0)
+ result = g_uri_list_extract_uris (list[0]);
+
+ for (i = 1; i < count; i++)
+ g_free (list[i]);
+ g_free (list);
+ }
+
+ return result;
+}
+
/**
* gtk_selection_data_get_targets:
diff --git a/gtk/gtkselection.h b/gtk/gtkselection.h
index 12c341201d..5623b440e4 100644
--- a/gtk/gtkselection.h
+++ b/gtk/gtkselection.h
@@ -95,11 +95,13 @@ void gtk_target_list_add (GtkTargetList *list,
GdkAtom target,
guint flags,
guint info);
-void gtk_target_list_add_text_targets (GtkTargetList *list,
- guint info);
+void gtk_target_list_add_text_targets (GtkTargetList *list,
+ guint info);
void gtk_target_list_add_image_targets (GtkTargetList *list,
guint info,
gboolean writable);
+void gtk_target_list_add_uri_targets (GtkTargetList *list,
+ guint info);
void gtk_target_list_add_table (GtkTargetList *list,
const GtkTargetEntry *targets,
guint ntargets);
@@ -145,6 +147,9 @@ 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_set_uris (GtkSelectionData *selection_data,
+ gchar **uris);
+gchar **gtk_selection_data_get_uris (GtkSelectionData *selection_data);
gboolean gtk_selection_data_get_targets (GtkSelectionData *selection_data,
GdkAtom **targets,