From baad7cddcabe9efba27535205ff6053a82840602 Mon Sep 17 00:00:00 2001 From: Alexandru Guduleasa Date: Fri, 10 Feb 2012 16:57:41 +0200 Subject: [PATCH] Gedit word select doesn't include space Bugzilla bug 634956 When dragging a word the space is not dragged with it. This patch checks the source and destination of the drag. If both source and destination have a space near them it will try to extend the select to a space near the source to preserve the tokens. Add function gedit_view_drag_data_text_recived to gedit-view.c to handle the drag and drop of text. --- gedit/gedit-view.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 110 insertions(+), 2 deletions(-) diff --git a/gedit/gedit-view.c b/gedit/gedit-view.c index 228ae1d..22b94a9 100644 --- a/gedit/gedit-view.c +++ b/gedit/gedit-view.c @@ -65,7 +65,8 @@ typedef enum enum { TARGET_URI_LIST = 100, - TARGET_TAB + TARGET_TAB, + TARGET_TEXT }; struct _GeditViewPrivate @@ -211,6 +212,7 @@ gedit_view_init (GeditView *view) if (tl != NULL) { gtk_target_list_add_uri_targets (tl, TARGET_URI_LIST); + gtk_target_list_add_text_targets(tl, TARGET_TEXT); gtk_target_list_add (tl, gdk_atom_intern_static_string ("GTK_NOTEBOOK_TAB"), GTK_TARGET_SAME_APP, @@ -447,6 +449,94 @@ get_notebook_from_view (GtkWidget *view) } static void +gedit_view_drag_data_text_recived (GtkWidget *widget, + GdkDragContext *context, + gint x, + gint y, + GtkSelectionData *selection_data, + guint info, + guint timestamp) +{ + + gint xb, yb; + gchar * data; + GtkWidget * view; + GtkTextBuffer *buffer; + GtkTextIter start, end, crt; + + view = gtk_drag_get_source_widget (context); + + + if (!GTK_IS_WIDGET (view)) + return; + + if (!GEDIT_IS_VIEW(view)) + return; + + buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); + if (buffer == NULL) + return; + + if (!gtk_text_buffer_get_selection_bounds(buffer, &start, &end)) + return; + + /* destination */ + gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), + GTK_TEXT_WINDOW_WIDGET, + x, y, + &xb, &yb); + gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW (view), &crt, xb, yb); + /* check for space */ + int dest_space = 0; + if (g_unichar_isspace (gtk_text_iter_get_char (&crt))) + dest_space += 1; + else if (gtk_text_iter_is_end (&crt)) + dest_space += 1; + gtk_text_iter_backward_char(&crt); + if (g_unichar_isspace (gtk_text_iter_get_char (&crt))) + dest_space += 10; + else if (gtk_text_iter_is_start (&crt)) + dest_space += 10; + if (dest_space == 0) + return; + if (dest_space == 11) + return; + + + /* source */ + int src_space = 0; + crt = start; + gtk_text_iter_backward_char(&crt); + if(g_unichar_isspace (gtk_text_iter_get_char (&end))) + src_space += 1; + if(g_unichar_isspace (gtk_text_iter_get_char (&crt))) + src_space += 10; + if(src_space == 0) + return; + + /* select with space */ + if (dest_space == 10) { + if (src_space == 10) + return; + else + gtk_text_iter_forward_char(&end); + } + else { + if (src_space == 1) + return; + else + gtk_text_iter_backward_char(&start); + } + + data = gtk_text_buffer_get_slice (buffer, &start, &end, TRUE); + gtk_text_buffer_select_range (buffer, &start, &end); + + gtk_selection_data_set_text(selection_data, + data, + strlen(data)); +} + +static void gedit_view_drag_data_received (GtkWidget *widget, GdkDragContext *context, gint x, @@ -455,6 +545,7 @@ gedit_view_drag_data_received (GtkWidget *widget, guint info, guint timestamp) { + /* If this is an URL emit DROP_URIS, otherwise chain up the signal */ if (info == TARGET_URI_LIST) { @@ -500,6 +591,22 @@ gedit_view_drag_data_received (GtkWidget *widget, gtk_drag_finish (context, TRUE, TRUE, timestamp); } + else if (info == TARGET_TEXT) { + + gedit_view_drag_data_text_recived (widget, + context, + x, y, + selection_data, + info, + timestamp); + + GTK_WIDGET_CLASS (gedit_view_parent_class)->drag_data_received (widget, + context, + x, y, + selection_data, + info, + timestamp); + } else { GTK_WIDGET_CLASS (gedit_view_parent_class)->drag_data_received (widget, @@ -956,7 +1063,8 @@ gedit_view_delete_selection (GeditView *view) buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); g_return_if_fail (buffer != NULL); - + + /* FIXME: what is default editability of a buffer? */ gtk_text_buffer_delete_selection (buffer, TRUE, -- 1.7.5.4