Use gtk_drag_*_add_{text,uri}_targets instead of hardcoded target tables.

2008-02-15  Matthias Clasen  <mclasen@redhat.com>

        * gtk/gtkentry.c:
        * gtk/gtkfilesel.c:
        * gtk/gtklabel.c:
        * gtk/gtkpathbar.c: Use gtk_drag_*_add_{text,uri}_targets
        instead of hardcoded target tables.  (#516092, Christian Persch)



svn path=/trunk/; revision=19590
This commit is contained in:
Matthias Clasen 2008-02-16 02:31:33 +00:00 committed by Matthias Clasen
parent 3b3122ee06
commit 13277493eb
6 changed files with 91 additions and 162 deletions

View File

@ -1,3 +1,11 @@
2008-02-15 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkentry.c:
* gtk/gtkfilesel.c:
* gtk/gtklabel.c:
* gtk/gtkpathbar.c: Use gtk_drag_*_add_{text,uri}_targets
instead of hardcoded target tables. (#516092, Christian Persch)
2008-02-15 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkselection.c (gtk_target_list_add_text_targets): Don't

View File

@ -4276,35 +4276,25 @@ primary_clear_cb (GtkClipboard *clipboard,
static void
gtk_entry_update_primary_selection (GtkEntry *entry)
{
static GtkTargetEntry targets[] = {
{ "UTF8_STRING", 0, 0 },
{ "STRING", 0, 0 },
{ "TEXT", 0, 0 },
{ "COMPOUND_TEXT", 0, 0 },
{ "text/plain;charset=utf-8", 0, 0 },
{ NULL, 0, 0 },
{ "text/plain", 0, 0 }
};
GtkTargetList *list;
GtkTargetEntry *targets;
GtkClipboard *clipboard;
gint start, end;
if (targets[5].target == NULL)
{
const gchar *charset;
g_get_charset (&charset);
targets[5].target = g_strdup_printf ("text/plain;charset=%s", charset);
}
gint n_targets;
if (!GTK_WIDGET_REALIZED (entry))
return;
list = gtk_target_list_new (NULL, 0);
gtk_target_list_add_text_targets (list, 0);
targets = gtk_target_table_new_from_list (list, &n_targets);
clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
{
if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
if (!gtk_clipboard_set_with_owner (clipboard, targets, n_targets,
primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
primary_clear_cb (clipboard, entry);
}
@ -4313,6 +4303,9 @@ gtk_entry_update_primary_selection (GtkEntry *entry)
if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
gtk_clipboard_clear (clipboard);
}
gtk_target_table_free (targets, n_targets);
gtk_target_list_unref (list);
}
/* Public API

View File

@ -831,49 +831,6 @@ gtk_file_selection_init (GtkFileSelection *filesel)
gtk_widget_pop_composite_child ();
}
static gchar *
uri_list_extract_first_uri (const gchar* uri_list)
{
const gchar *p, *q;
g_return_val_if_fail (uri_list != NULL, NULL);
p = uri_list;
/* 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)
return g_strndup (p, q - p + 1);
}
}
p = strchr (p, '\n');
if (p)
p++;
}
return NULL;
}
static void
dnd_really_drop (GtkWidget *dialog, gint response_id, GtkFileSelection *fs)
{
@ -889,7 +846,6 @@ dnd_really_drop (GtkWidget *dialog, gint response_id, GtkFileSelection *fs)
gtk_widget_destroy (dialog);
}
static void
filenames_dropped (GtkWidget *widget,
GdkDragContext *context,
@ -899,22 +855,21 @@ filenames_dropped (GtkWidget *widget,
guint info,
guint time)
{
char *uri = NULL;
char **uris = NULL;
char *filename = NULL;
char *hostname;
const char *this_hostname;
GError *error = NULL;
if (!selection_data->data)
uris = gtk_selection_data_get_uris (selection_data);
if (!uris || !uris[0])
{
g_strfreev (uris);
return;
}
uri = uri_list_extract_first_uri ((char *)selection_data->data);
if (!uri)
return;
filename = g_filename_from_uri (uri, &hostname, &error);
g_free (uri);
filename = g_filename_from_uri (uris[0], &hostname, &error);
g_strfreev (uris);
if (!filename)
{
@ -963,16 +918,6 @@ filenames_dropped (GtkWidget *widget,
g_free (filename);
}
enum
{
TARGET_URILIST,
TARGET_UTF8_STRING,
TARGET_STRING,
TARGET_TEXT,
TARGET_COMPOUND_TEXT
};
static void
filenames_drag_get (GtkWidget *widget,
GdkDragContext *context,
@ -982,21 +927,24 @@ filenames_drag_get (GtkWidget *widget,
GtkFileSelection *filesel)
{
const gchar *file;
gchar *uri_list;
const char *hostname;
GError *error;
gchar *filename_utf8;
file = gtk_file_selection_get_filename (filesel);
if (!file)
return;
if (file)
{
if (info == TARGET_URILIST)
if (gtk_targets_include_uri (&selection_data->target, 1))
{
gchar *file_uri;
const char *hostname;
GError *error;
char *uris[2];
hostname = g_get_host_name ();
error = NULL;
uri_list = g_filename_to_uri (file, hostname, &error);
if (!uri_list)
file_uri = g_filename_to_uri (file, hostname, &error);
if (!file_uri)
{
g_warning ("Error getting filename: %s\n",
error->message);
@ -1004,42 +952,32 @@ filenames_drag_get (GtkWidget *widget,
return;
}
gtk_selection_data_set (selection_data,
selection_data->target, 8,
(void *)uri_list, strlen((char *)uri_list));
g_free (uri_list);
uris[0] = file_uri;
uris[1] = NULL;
gtk_selection_data_set_uris (selection_data, uris);
g_free (file_uri);
return;
}
else
{
gchar *filename_utf8 = g_filename_to_utf8 (file, -1, NULL, NULL, NULL);
g_assert (filename_utf8);
filename_utf8 = g_filename_to_utf8 (file, -1, NULL, NULL, NULL);
if (!filename_utf8)
return;
gtk_selection_data_set_text (selection_data, filename_utf8, -1);
g_free (filename_utf8);
}
}
}
static void
file_selection_setup_dnd (GtkFileSelection *filesel)
{
GtkWidget *eventbox;
static const GtkTargetEntry drop_types[] = {
{ "text/uri-list", 0, TARGET_URILIST}
};
static const gint n_drop_types = sizeof(drop_types)/sizeof(drop_types[0]);
static const GtkTargetEntry drag_types[] = {
{ "text/uri-list", 0, TARGET_URILIST},
{ "UTF8_STRING", 0, TARGET_UTF8_STRING },
{ "STRING", 0, 0 },
{ "TEXT", 0, 0 },
{ "COMPOUND_TEXT", 0, 0 }
};
static const gint n_drag_types = sizeof(drag_types)/sizeof(drag_types[0]);
gtk_drag_dest_set (GTK_WIDGET (filesel),
GTK_DEST_DEFAULT_ALL,
drop_types, n_drop_types,
NULL, 0,
GDK_ACTION_COPY);
gtk_drag_dest_add_uri_targets (GTK_WIDGET (filesel));
g_signal_connect (filesel, "drag_data_received",
G_CALLBACK (filenames_dropped), NULL);
@ -1047,8 +985,10 @@ file_selection_setup_dnd (GtkFileSelection *filesel)
eventbox = gtk_widget_get_parent (filesel->selection_text);
gtk_drag_source_set (eventbox,
GDK_BUTTON1_MASK,
drag_types, n_drag_types,
NULL, 0,
GDK_ACTION_COPY);
gtk_drag_source_add_uri_targets (eventbox);
gtk_drag_source_add_text_targets (eventbox);
g_signal_connect (eventbox, "drag_data_get",
G_CALLBACK (filenames_drag_get), filesel);

View File

@ -3413,13 +3413,6 @@ gtk_label_select_region_index (GtkLabel *label,
gint anchor_index,
gint end_index)
{
static const GtkTargetEntry targets[] = {
{ "STRING", 0, 0 },
{ "TEXT", 0, 0 },
{ "COMPOUND_TEXT", 0, 0 },
{ "UTF8_STRING", 0, 0 }
};
g_return_if_fail (GTK_IS_LABEL (label));
if (label->select_info)
@ -3438,12 +3431,22 @@ gtk_label_select_region_index (GtkLabel *label,
if (anchor_index != end_index)
{
GtkTargetList *list;
GtkTargetEntry *targets;
gint n_targets;
list = gtk_target_list_new (NULL, 0);
gtk_target_list_add_text_targets (list, 0);
targets = gtk_target_table_new_from_list (list, &n_targets);
gtk_clipboard_set_with_owner (clipboard,
targets,
G_N_ELEMENTS (targets),
targets, n_targets,
get_text_callback,
clear_text_callback,
G_OBJECT (label));
gtk_target_table_free (targets, n_targets);
gtk_target_list_unref (list);
}
else
{

View File

@ -1344,10 +1344,6 @@ make_directory_button (GtkPathBar *path_bar,
gboolean current_dir,
gboolean file_is_hidden)
{
const GtkTargetEntry targets[] = {
{ "text/uri-list", 0, 0 }
};
AtkObject *atk_obj;
GtkWidget *child = NULL;
GtkWidget *label_alignment = NULL;
@ -1414,9 +1410,9 @@ make_directory_button (GtkPathBar *path_bar,
gtk_drag_source_set (button_data->button,
GDK_BUTTON1_MASK,
targets,
G_N_ELEMENTS (targets),
NULL, 0,
GDK_ACTION_COPY);
gtk_drag_source_add_uri_targets (button_data->button);
g_signal_connect (button_data->button, "drag_data_get",
G_CALLBACK (button_drag_data_get_cb), button_data);

View File

@ -153,15 +153,6 @@ enum {
LOAD_FINISHED /* the model is fully loaded and inserted */
};
enum {
TEXT_URI_LIST
};
/* Target types for DnD from the file list */
static const GtkTargetEntry recent_list_source_targets[] = {
{ "text/uri-list", 0, TEXT_URI_LIST }
};
/* Icon size for if we can't get it from the theme */
#define FALLBACK_ICON_SIZE 48
#define FALLBACK_ITEM_LIMIT 20
@ -451,9 +442,9 @@ gtk_recent_chooser_default_constructor (GType type,
/* drag and drop */
gtk_drag_source_set (impl->recent_view,
GDK_BUTTON1_MASK,
recent_list_source_targets,
G_N_ELEMENTS (recent_list_source_targets),
NULL, 0,
GDK_ACTION_COPY);
gtk_drag_source_add_uri_targets (impl->recent_view);
impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
@ -1535,25 +1526,23 @@ recent_view_drag_data_get_cb (GtkWidget *widget,
gpointer data)
{
GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (data);
DragData *drag_data;
DragData drag_data;
gsize n_uris;
n_uris = gtk_tree_selection_count_selected_rows (impl->selection);
if (n_uris == 0)
return;
drag_data = g_new (DragData, 1);
drag_data->uri_list = g_new0 (gchar *, n_uris + 1);
drag_data->next_pos = 0;
drag_data.uri_list = g_new0 (gchar *, n_uris + 1);
drag_data.next_pos = 0;
gtk_tree_selection_selected_foreach (impl->selection,
append_uri_to_urilist,
drag_data);
&drag_data);
gtk_selection_data_set_uris (selection_data, drag_data->uri_list);
gtk_selection_data_set_uris (selection_data, drag_data.uri_list);
g_strfreev (drag_data->uri_list);
g_free (drag_data);
g_strfreev (drag_data.uri_list);
}
static gboolean