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> 2008-02-15 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkselection.c (gtk_target_list_add_text_targets): Don't * gtk/gtkselection.c (gtk_target_list_add_text_targets): Don't

View File

@ -4276,35 +4276,25 @@ primary_clear_cb (GtkClipboard *clipboard,
static void static void
gtk_entry_update_primary_selection (GtkEntry *entry) gtk_entry_update_primary_selection (GtkEntry *entry)
{ {
static GtkTargetEntry targets[] = { GtkTargetList *list;
{ "UTF8_STRING", 0, 0 }, GtkTargetEntry *targets;
{ "STRING", 0, 0 },
{ "TEXT", 0, 0 },
{ "COMPOUND_TEXT", 0, 0 },
{ "text/plain;charset=utf-8", 0, 0 },
{ NULL, 0, 0 },
{ "text/plain", 0, 0 }
};
GtkClipboard *clipboard; GtkClipboard *clipboard;
gint start, end; gint start, end;
gint n_targets;
if (targets[5].target == NULL)
{
const gchar *charset;
g_get_charset (&charset);
targets[5].target = g_strdup_printf ("text/plain;charset=%s", charset);
}
if (!GTK_WIDGET_REALIZED (entry)) if (!GTK_WIDGET_REALIZED (entry))
return; 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); clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end)) 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_get_cb, primary_clear_cb, G_OBJECT (entry)))
primary_clear_cb (clipboard, 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)) if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
gtk_clipboard_clear (clipboard); gtk_clipboard_clear (clipboard);
} }
gtk_target_table_free (targets, n_targets);
gtk_target_list_unref (list);
} }
/* Public API /* Public API
@ -5201,7 +5194,7 @@ popup_targets_received (GtkClipboard *clipboard,
gboolean show_input_method_menu; gboolean show_input_method_menu;
gboolean show_unicode_menu; gboolean show_unicode_menu;
clipboard_contains_text = gtk_selection_data_targets_include_text (data); clipboard_contains_text = gtk_selection_data_targets_include_text (data);
if (entry->popup_menu) if (entry->popup_menu)
gtk_widget_destroy (entry->popup_menu); gtk_widget_destroy (entry->popup_menu);

View File

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

View File

@ -3413,13 +3413,6 @@ gtk_label_select_region_index (GtkLabel *label,
gint anchor_index, gint anchor_index,
gint end_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)); g_return_if_fail (GTK_IS_LABEL (label));
if (label->select_info) if (label->select_info)
@ -3438,12 +3431,22 @@ gtk_label_select_region_index (GtkLabel *label,
if (anchor_index != end_index) 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, gtk_clipboard_set_with_owner (clipboard,
targets, targets, n_targets,
G_N_ELEMENTS (targets),
get_text_callback, get_text_callback,
clear_text_callback, clear_text_callback,
G_OBJECT (label)); G_OBJECT (label));
gtk_target_table_free (targets, n_targets);
gtk_target_list_unref (list);
} }
else else
{ {

View File

@ -1344,10 +1344,6 @@ make_directory_button (GtkPathBar *path_bar,
gboolean current_dir, gboolean current_dir,
gboolean file_is_hidden) gboolean file_is_hidden)
{ {
const GtkTargetEntry targets[] = {
{ "text/uri-list", 0, 0 }
};
AtkObject *atk_obj; AtkObject *atk_obj;
GtkWidget *child = NULL; GtkWidget *child = NULL;
GtkWidget *label_alignment = NULL; GtkWidget *label_alignment = NULL;
@ -1414,9 +1410,9 @@ make_directory_button (GtkPathBar *path_bar,
gtk_drag_source_set (button_data->button, gtk_drag_source_set (button_data->button,
GDK_BUTTON1_MASK, GDK_BUTTON1_MASK,
targets, NULL, 0,
G_N_ELEMENTS (targets),
GDK_ACTION_COPY); GDK_ACTION_COPY);
gtk_drag_source_add_uri_targets (button_data->button);
g_signal_connect (button_data->button, "drag_data_get", g_signal_connect (button_data->button, "drag_data_get",
G_CALLBACK (button_drag_data_get_cb), button_data); 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 */ 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 */ /* Icon size for if we can't get it from the theme */
#define FALLBACK_ICON_SIZE 48 #define FALLBACK_ICON_SIZE 48
#define FALLBACK_ITEM_LIMIT 20 #define FALLBACK_ITEM_LIMIT 20
@ -451,9 +442,9 @@ gtk_recent_chooser_default_constructor (GType type,
/* drag and drop */ /* drag and drop */
gtk_drag_source_set (impl->recent_view, gtk_drag_source_set (impl->recent_view,
GDK_BUTTON1_MASK, GDK_BUTTON1_MASK,
recent_list_source_targets, NULL, 0,
G_N_ELEMENTS (recent_list_source_targets),
GDK_ACTION_COPY); GDK_ACTION_COPY);
gtk_drag_source_add_uri_targets (impl->recent_view);
impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12); impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
@ -1535,25 +1526,23 @@ recent_view_drag_data_get_cb (GtkWidget *widget,
gpointer data) gpointer data)
{ {
GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (data); GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (data);
DragData *drag_data; DragData drag_data;
gsize n_uris; gsize n_uris;
n_uris = gtk_tree_selection_count_selected_rows (impl->selection); n_uris = gtk_tree_selection_count_selected_rows (impl->selection);
if (n_uris == 0) if (n_uris == 0)
return; return;
drag_data = g_new (DragData, 1); drag_data.uri_list = g_new0 (gchar *, n_uris + 1);
drag_data->uri_list = g_new0 (gchar *, n_uris + 1); drag_data.next_pos = 0;
drag_data->next_pos = 0;
gtk_tree_selection_selected_foreach (impl->selection, gtk_tree_selection_selected_foreach (impl->selection,
append_uri_to_urilist, 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_strfreev (drag_data.uri_list);
g_free (drag_data);
} }
static gboolean static gboolean