[PATCH v2 15/17] text: Add delete_surrounding_text to protocol

Jan Arne Petersen jpetersen at openismus.com
Sun Sep 9 14:08:44 PDT 2012


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

Add delete_surrounding_text event in the text_model interface and the
request in the input_method_context interface. Implement it in the
example editor client and in the example keyboard so that the backspace
key works with it.

Signed-off-by: Jan Arne Petersen <jpetersen at openismus.com>
---
 clients/editor.c          | 43 +++++++++++++++++++++++++++++++++++++++++++
 clients/keyboard.c        |  4 ++++
 protocol/input-method.xml |  4 ++++
 protocol/text.xml         |  4 ++++
 src/text-backend.c        | 12 ++++++++++++
 tests/test-text-client.c  |  9 +++++++++
 6 files changed, 76 insertions(+)

diff --git a/clients/editor.c b/clients/editor.c
index b6a1742..0ed217f 100644
--- a/clients/editor.c
+++ b/clients/editor.c
@@ -212,6 +212,8 @@ static void text_entry_insert_at_cursor(struct text_entry *entry, const char *te
 static void text_entry_set_preedit(struct text_entry *entry,
 				   const char *preedit_text,
 				   int preedit_cursor);
+static void text_entry_delete_text(struct text_entry *entry,
+				   uint32_t index, uint32_t length);
 
 static void
 text_model_commit_string(void *data,
@@ -250,6 +252,31 @@ text_model_preedit_string(void *data,
 }
 
 static void
+text_model_delete_surrounding_text(void *data,
+				   struct text_model *text_model,
+				   int32_t index,
+				   uint32_t length)
+{
+	struct text_entry *entry = data;
+	uint32_t cursor_index = index + entry->cursor;
+
+	if (cursor_index > strlen(entry->text)) {
+		fprintf(stderr, "Invalid cursor index %d\n", index);
+		return;
+	}
+
+	if (cursor_index + length > strlen(entry->text)) {
+		fprintf(stderr, "Invalid length %d\n", length);
+		return;
+	}
+
+	if (length == 0)
+		return;
+
+	text_entry_delete_text(entry, cursor_index, length);
+}
+
+static void
 text_model_preedit_styling(void *data,
 			   struct text_model *text_model)
 {
@@ -304,6 +331,7 @@ text_model_deactivated(void *data,
 static const struct text_model_listener text_model_listener = {
 	text_model_commit_string,
 	text_model_preedit_string,
+	text_model_delete_surrounding_text,
 	text_model_preedit_styling,
 	text_model_key,
 	text_model_selection_replacement,
@@ -516,6 +544,21 @@ text_entry_set_anchor_position(struct text_entry *entry,
 }
 
 static void
+text_entry_delete_text(struct text_entry *entry,
+		       uint32_t index, uint32_t length)
+{
+	if (entry->cursor > index)
+		entry->cursor -= length;
+
+	entry->text[index] = '\0';
+	strcat(entry->text, entry->text + index + length);
+
+	text_entry_update_layout(entry);
+
+	widget_schedule_redraw(entry->widget);
+}
+
+static void
 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
 {
 	cairo_text_extents_t extents;
diff --git a/clients/keyboard.c b/clients/keyboard.c
index 4bc7d24..588ef78 100644
--- a/clients/keyboard.c
+++ b/clients/keyboard.c
@@ -222,6 +222,10 @@ keyboard_handle_key(struct keyboard *keyboard, const struct key *key)
 							    strlen(keyboard->keyboard->preedit_string));
 			break;
 		case keytype_backspace:
+			if (strlen(keyboard->keyboard->preedit_string) == 0) {
+				input_method_context_delete_surrounding_text(keyboard->keyboard->context,
+									     -1, 1);
+			}
 			break;
 		case keytype_enter:
 			break;
diff --git a/protocol/input-method.xml b/protocol/input-method.xml
index 9baff62..10ca32a 100644
--- a/protocol/input-method.xml
+++ b/protocol/input-method.xml
@@ -49,6 +49,10 @@
       <arg name="text" type="string"/>
       <arg name="index" type="uint"/>
     </request>
+    <request name="delete_surrounding_text">
+      <arg name="index" type="int"/>
+      <arg name="length" type="uint"/>
+    </request>
     <event name="surrounding_text">
       <description summary="surrounding text event">
         The plain surrounding text around the input position. Cursor is the
diff --git a/protocol/text.xml b/protocol/text.xml
index b280a28..3d7d8f5 100644
--- a/protocol/text.xml
+++ b/protocol/text.xml
@@ -84,6 +84,10 @@
       <arg name="text" type="string"/>
       <arg name="index" type="uint"/>
     </event>
+    <event name="delete_surrounding_text">
+      <arg name="index" type="int"/>
+      <arg name="length" type="uint"/>
+    </event>
     <event name="preedit_styling"/>
     <event name="key"/>
     <event name="selection_replacement"/>
diff --git a/src/text-backend.c b/src/text-backend.c
index 4fb4d95..b2f9094 100644
--- a/src/text-backend.c
+++ b/src/text-backend.c
@@ -305,10 +305,22 @@ input_method_context_preedit_string(struct wl_client *client,
 	text_model_send_preedit_string(&context->model->resource, text, index);
 }
 
+static void
+input_method_context_delete_surrounding_text(struct wl_client *client,
+					     struct wl_resource *resource,
+					     int32_t index,
+					     uint32_t length)
+{
+	struct input_method_context *context = resource->data;
+
+	text_model_send_delete_surrounding_text(&context->model->resource, index, length);
+}
+
 static const struct input_method_context_interface input_method_context_implementation = {
 	input_method_context_destroy,
 	input_method_context_commit_string,
 	input_method_context_preedit_string,
+	input_method_context_delete_surrounding_text
 };
 
 static void
diff --git a/tests/test-text-client.c b/tests/test-text-client.c
index 86fefd8..3b390f6 100644
--- a/tests/test-text-client.c
+++ b/tests/test-text-client.c
@@ -60,6 +60,14 @@ text_model_preedit_string(void *data,
 }
 
 static void
+text_model_delete_surrounding_text(void *data,
+				   struct text_model *text_model,
+				   int32_t index,
+				   uint32_t length)
+{
+}
+
+static void
 text_model_preedit_styling(void *data,
 			   struct text_model *text_model)
 {
@@ -112,6 +120,7 @@ text_model_deactivated(void *data,
 static const struct text_model_listener text_model_listener = {
 	text_model_commit_string,
 	text_model_preedit_string,
+	text_model_delete_surrounding_text,
 	text_model_preedit_styling,
 	text_model_key,
 	text_model_selection_replacement,
-- 
1.7.11.4



More information about the wayland-devel mailing list