From e0ad07e0b4e7257fc95f8ce0f988b977c6b8e2b9 Mon Sep 17 00:00:00 2001 From: Owen Taylor Date: Thu, 21 Oct 2004 19:13:17 +0000 Subject: #155952 Thu Oct 21 15:01:09 2004 Owen Taylor #155952 * gtk/gtkentry.c: Fix checking the wrong position for the backspace_deletes_character PangoLogAttr. * gtk/gtktextbuffer.[ch] gtk/gtk.symbols (gtk_text_buffer_backspace): Move backspace logic from GtkTextView to here, check backspace_deletes_char. * gtk/gtktextview.c: Use gtk_text_buffer_backspace(). --- gtk/gtk.symbols | 1 + gtk/gtkentry.c | 2 +- gtk/gtktextbuffer.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++ gtk/gtktextbuffer.h | 16 +++++----- gtk/gtktextview.c | 37 ++--------------------- 5 files changed, 99 insertions(+), 42 deletions(-) (limited to 'gtk') diff --git a/gtk/gtk.symbols b/gtk/gtk.symbols index 8006ead2f..cc3f2ccfb 100644 --- a/gtk/gtk.symbols +++ b/gtk/gtk.symbols @@ -1855,6 +1855,7 @@ gtk_text_backward_delete gtk_text_buffer_add_selection_clipboard gtk_text_buffer_apply_tag gtk_text_buffer_apply_tag_by_name +gtk_text_buffer_backspace gtk_text_buffer_begin_user_action gtk_text_buffer_copy_clipboard gtk_text_buffer_create_child_anchor diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c index 34b957e4e..b586e7a53 100644 --- a/gtk/gtkentry.c +++ b/gtk/gtkentry.c @@ -2536,7 +2536,7 @@ gtk_entry_backspace (GtkEntry *entry) pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs); - if (log_attrs[prev_pos].backspace_deletes_character) + if (log_attrs[entry->current_pos].backspace_deletes_character) { gchar *cluster_text; gchar *normalized_text; diff --git a/gtk/gtktextbuffer.c b/gtk/gtktextbuffer.c index d19635d22..814cf8057 100644 --- a/gtk/gtktextbuffer.c +++ b/gtk/gtktextbuffer.c @@ -3382,6 +3382,91 @@ gtk_text_buffer_delete_selection (GtkTextBuffer *buffer, } } +/** + * gtk_text_buffer_backspace: + * @buffer: a #GtkTextBuffer + * @iter: a position in @buffer + * @interactive: whether the deletion is caused by user interaction + * @default_editable: whether the buffer is editable by default + * + * Performs the appropriate action as if the user hit the delete + * key with the cursor at the position specified by @iter. In the + * normal case a single character will be deleted, but when + * combining accents are involved, more than one character can + * be deleted, and when precomposed character and accent combinations, + * less than one character will be deleted. + * + * @iter must be at a cursor position. + * + * Return value: %TRUE if tbe buffer was modified + + * Since: 2.6 + **/ +gboolean +gtk_text_buffer_backspace (GtkTextBuffer *buffer, + GtkTextIter *iter, + gboolean interactive, + gboolean default_editable) +{ + gchar *cluster_text; + GtkTextIter start; + GtkTextIter end; + gboolean retval = FALSE; + const PangoLogAttr *attrs; + int offset; + gboolean backspace_deletes_character; + + g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE); + g_return_val_if_fail (iter != NULL, FALSE); + + start = *iter; + end = *iter; + + attrs = _gtk_text_buffer_get_line_log_attrs (buffer, &start, NULL); + offset = gtk_text_iter_get_line_offset (&start); + backspace_deletes_character = attrs[offset].backspace_deletes_character; + + gtk_text_iter_backward_cursor_position (&start); + + if (gtk_text_iter_equal (&start, &end)) + return FALSE; + + cluster_text = gtk_text_iter_get_text (&start, &end); + + if (interactive) + gtk_text_buffer_begin_user_action (buffer); + + if (gtk_text_buffer_delete_interactive (buffer, &start, &end, + default_editable)) + { + if (backspace_deletes_character) + { + gchar *normalized_text = g_utf8_normalize (cluster_text, + strlen (cluster_text), + G_NORMALIZE_NFD); + glong len = g_utf8_strlen (normalized_text, -1); + + if (len > 1) + gtk_text_buffer_insert_interactive (buffer, + &start, + normalized_text, + g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text, + default_editable); + + g_free (normalized_text); + } + + retval = TRUE; + } + + if (interactive) + gtk_text_buffer_end_user_action (buffer); + + g_free (cluster_text); + + return retval; +} + static void cut_or_copy (GtkTextBuffer *buffer, GtkClipboard *clipboard, diff --git a/gtk/gtktextbuffer.h b/gtk/gtktextbuffer.h index 6f4a4728c..6d9d3b877 100644 --- a/gtk/gtktextbuffer.h +++ b/gtk/gtktextbuffer.h @@ -197,14 +197,16 @@ void gtk_text_buffer_insert_with_tags_by_name (GtkTextBuffer *buffer, /* Delete from the buffer */ void gtk_text_buffer_delete (GtkTextBuffer *buffer, - GtkTextIter *start, - GtkTextIter *end); + GtkTextIter *start, + GtkTextIter *end); gboolean gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer, - GtkTextIter *start_iter, - GtkTextIter *end_iter, - gboolean default_editable); - - + GtkTextIter *start_iter, + GtkTextIter *end_iter, + gboolean default_editable); +gboolean gtk_text_buffer_backspace (GtkTextBuffer *buffer, + GtkTextIter *iter, + gboolean interactive, + gboolean default_editable); /* Obtain strings from the buffer */ gchar *gtk_text_buffer_get_text (GtkTextBuffer *buffer, diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c index a02e0afb7..7e503bd98 100644 --- a/gtk/gtktextview.c +++ b/gtk/gtktextview.c @@ -5147,8 +5147,6 @@ static void gtk_text_view_backspace (GtkTextView *text_view) { GtkTextIter insert; - GtkTextIter start; - GtkTextIter end; gtk_text_view_reset_im_context (text_view); @@ -5162,41 +5160,12 @@ gtk_text_view_backspace (GtkTextView *text_view) gtk_text_buffer_get_mark (get_buffer (text_view), "insert")); - start = insert; - end = insert; - - gtk_text_iter_backward_cursor_position (&end); - - if (!gtk_text_iter_equal (&start, &end)) + if (gtk_text_buffer_backspace (get_buffer (text_view), &insert, + TRUE, text_view->editable)) { - gchar *cluster_text = gtk_text_iter_get_text (&start, &end); - - gtk_text_buffer_begin_user_action (get_buffer (text_view)); - - if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end, - text_view->editable)) - { - gchar *normalized_text = g_utf8_normalize (cluster_text, - strlen (cluster_text), - G_NORMALIZE_NFD); - glong len = g_utf8_strlen (normalized_text, -1); - - if (len > 1) - gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), - normalized_text, - g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text, - text_view->editable); - - g_free (normalized_text); - } - - gtk_text_buffer_end_user_action (get_buffer (text_view)); - - g_free (cluster_text); - DV(g_print (G_STRLOC": scrolling onscreen\n")); gtk_text_view_scroll_mark_onscreen (text_view, - gtk_text_buffer_get_mark (get_buffer (text_view), "insert")); + gtk_text_buffer_get_mark (get_buffer (text_view), "insert")); } } -- cgit v1.2.1