From 06fd1566eaa01b236fbaabd2ce3536606890805e Mon Sep 17 00:00:00 2001 From: Daniel Hirt Date: Sun, 3 Jul 2016 13:57:22 +0000 Subject: [PATCH] Canvas text cursor: fix bug after code port Fixes T4005. Also, unified repeated code and added a test for multiple cursors. --- src/lib/evas/canvas/evas_object_textblock.c | 71 ++++++++++++++--------------- src/tests/evas/evas_test_textblock.c | 29 ++++++++++++ 2 files changed, 63 insertions(+), 37 deletions(-) diff --git a/src/lib/evas/canvas/evas_object_textblock.c b/src/lib/evas/canvas/evas_object_textblock.c index e0130b7..cd78e82 100644 --- a/src/lib/evas/canvas/evas_object_textblock.c +++ b/src/lib/evas/canvas/evas_object_textblock.c @@ -9682,6 +9682,31 @@ _evas_textblock_cursors_set_node(Efl_Canvas_Text_Data *o, } } +static inline void +_cursor_update_offset(Efl_Canvas_Text_Cursor_Data *cur, Efl_Canvas_Text_Data *o, + const Evas_Object_Textblock_Node_Text *n, size_t start, int offset) +{ + if ((n == cur->node) && + (cur->pos > start)) + { + if ((offset < 0) && (cur->pos <= (size_t) (-1 * offset))) + { + cur->pos = 0; + } + else + { + cur->pos += offset; + } + cur->changed = EINA_TRUE; + } + else if (!cur->node) + { + cur->node = o->text_nodes; + cur->pos = 0; + cur->changed = EINA_TRUE; + } +} + /** * @internal * Update the offset of all the cursors after cur. @@ -9697,50 +9722,22 @@ _evas_textblock_cursors_update_offset(const Efl_Canvas_Text_Cursor_Data *cur, size_t start, int offset) { Eina_List *l; - Efl_Canvas_Text_Cursor *data_obj; + Efl_Canvas_Text_Cursor *ocur_obj; + Efl_Canvas_Text_Cursor_Data *ocur; Efl_Canvas_Text_Data *o = eo_data_scope_get(cur->obj, MY_CLASS); - Efl_Canvas_Text_Cursor_Data *ocur = eo_data_scope_get(o->cursor, EFL_CANVAS_TEXT_CURSOR_CLASS); + ocur = eo_data_scope_get(o->cursor, EFL_CANVAS_TEXT_CURSOR_CLASS); if (cur != ocur) { - if ((n == cur->node) && - (cur->pos > start)) - { - if ((offset < 0) && (cur->pos <= (size_t) (-1 * offset))) - { - ocur->pos = 0; - } - else - { - ocur->pos += offset; - } - ocur->changed = EINA_TRUE; - } + _cursor_update_offset(ocur, o, n, start, offset); } - EINA_LIST_FOREACH(o->cursors, l, data_obj) + + EINA_LIST_FOREACH(o->cursors, l, ocur_obj) { - Efl_Canvas_Text_Cursor_Data *data = eo_data_scope_get(data_obj, EFL_CANVAS_TEXT_CURSOR_CLASS); - if (data != cur) + ocur = eo_data_scope_get(ocur_obj, EFL_CANVAS_TEXT_CURSOR_CLASS); + if (ocur != cur) { - if ((n == data->node) && - (data->pos > start)) - { - if ((offset < 0) && (data->pos <= (size_t) (-1 * offset))) - { - data->pos = 0; - } - else - { - data->pos += offset; - } - data->changed = EINA_TRUE; - } - else if (!data->node) - { - data->node = o->text_nodes; - data->pos = 0; - data->changed = EINA_TRUE; - } + _cursor_update_offset(ocur, o, n, start, offset); } } } diff --git a/src/tests/evas/evas_test_textblock.c b/src/tests/evas/evas_test_textblock.c index 9d9ee2e..381c938 100644 --- a/src/tests/evas/evas_test_textblock.c +++ b/src/tests/evas/evas_test_textblock.c @@ -943,6 +943,35 @@ START_TEST(evas_textblock_cursor) ck_assert_int_eq(pos, 44); } + { + /* Test multiple cursors: + * Deleting characters should "pull" all the cursors positioned *after* + * the deleted position, and "push" on insertion. + * Testing with one additional cursor will suffice. */ + int j, pos; + Evas_Textblock_Cursor *cur2; + + cur2 = evas_object_textblock_cursor_new(tb); + evas_object_textblock_text_markup_set(tb, "Hello world"); + evas_textblock_cursor_pos_set(cur2, 0); + evas_textblock_cursor_pos_set(cur, 5); + for (j = 5; j >= 0; j--) + { + pos = evas_textblock_cursor_pos_get(cur); + ck_assert_int_eq(pos, j); + evas_textblock_cursor_char_delete(cur2); + } + evas_object_textblock_text_markup_set(tb, "Hello world"); + evas_textblock_cursor_pos_set(cur2, 0); + evas_textblock_cursor_pos_set(cur, 5); + for (j = 5; j <= 10; j++) + { + pos = evas_textblock_cursor_pos_get(cur); + ck_assert_int_eq(pos, j); + evas_textblock_cursor_text_append(cur2, "a"); + } + evas_textblock_cursor_free(cur2); + } END_TB_TEST(); } END_TEST -- 2.7.4