[PATCH 3/4] editor: Delete selected text before adding new

Jan Arne Petersen jpetersen at openismus.com
Mon Sep 17 06:28:09 PDT 2012


From: Jan Arne Petersen <jpetersen at openismus.com>

When inserting new text, delete selected text first.
---
 clients/editor.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/clients/editor.c b/clients/editor.c
index 5ad1aa2..5c75ac1 100644
--- a/clients/editor.c
+++ b/clients/editor.c
@@ -214,6 +214,7 @@ static void text_entry_set_preedit(struct text_entry *entry,
 				   int preedit_cursor);
 static void text_entry_delete_text(struct text_entry *entry,
 				   uint32_t index, uint32_t length);
+static void text_entry_delete_selected_text(struct text_entry *entry);
 
 static void
 text_model_commit_string(void *data,
@@ -228,6 +229,7 @@ text_model_commit_string(void *data,
 		index = strlen(text);
 	}
 
+	text_entry_delete_selected_text(entry);
 	text_entry_insert_at_cursor(entry, text);
 
 	widget_schedule_redraw(entry->widget);
@@ -246,6 +248,7 @@ text_model_preedit_string(void *data,
 		index = strlen(text);
 	}
 
+	text_entry_delete_selected_text(entry);
 	text_entry_set_preedit(entry, text, index);
 
 	widget_schedule_redraw(entry->widget);
@@ -597,6 +600,20 @@ text_entry_delete_text(struct text_entry *entry,
 }
 
 static void
+text_entry_delete_selected_text(struct text_entry *entry)
+{
+	uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
+	uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
+
+	if (entry->anchor == entry->cursor)
+		return;
+
+	text_entry_delete_text(entry, start_index, end_index - start_index);
+
+	entry->anchor = entry->cursor;
+}
+
+static void
 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
 {
 	cairo_text_extents_t extents;
-- 
1.7.11.4



More information about the wayland-devel mailing list