elm_code selection: Support selection deletion

In the widget we always delete the selection if typing begins.
If delete or backspace was pressed don't execute the keystroke after.
This commit is contained in:
Andy Williams 2015-03-28 15:17:08 +00:00
parent f1571f296e
commit f330c48bce
4 changed files with 207 additions and 2 deletions

View File

@ -771,6 +771,20 @@ _elm_code_widget_cursor_move_right(Elm_Code_Widget *widget)
_elm_code_widget_cursor_move(widget, pd, pd->cursor_col+1, pd->cursor_line, EINA_TRUE);
}
static Eina_Bool
_elm_code_widget_delete_selection(Elm_Code_Widget *widget)
{
Elm_Code_Widget_Data *pd;
pd = eo_data_scope_get(widget, ELM_CODE_WIDGET_CLASS);
if (!pd->selection)
return EINA_FALSE;
elm_code_widget_selection_delete(widget);
return EINA_TRUE;
}
static void
_elm_code_widget_text_at_cursor_insert(Elm_Code_Widget *widget, const char *text, int length)
{
@ -778,6 +792,7 @@ _elm_code_widget_text_at_cursor_insert(Elm_Code_Widget *widget, const char *text
Elm_Code_Line *line;
unsigned int row, col;
_elm_code_widget_delete_selection(widget);
eo_do(widget,
code = elm_code_widget_code_get(),
elm_code_widget_cursor_position_get(&col, &row));
@ -798,6 +813,7 @@ _elm_code_widget_newline(Elm_Code_Widget *widget)
unsigned int row, col, length;
char *content;
_elm_code_widget_delete_selection(widget);
eo_do(widget,
code = elm_code_widget_code_get(),
elm_code_widget_cursor_position_get(&col, &row));
@ -870,6 +886,9 @@ _elm_code_widget_backspace(Elm_Code_Widget *widget)
Elm_Code_Line *line;
unsigned int row, col;
if (_elm_code_widget_delete_selection(widget))
return;
eo_do(widget,
code = elm_code_widget_code_get(),
elm_code_widget_cursor_position_get(&col, &row));
@ -900,6 +919,9 @@ _elm_code_widget_delete(Elm_Code_Widget *widget)
Elm_Code_Line *line;
unsigned int row, col;
if (_elm_code_widget_delete_selection(widget))
return;
eo_do(widget,
code = elm_code_widget_code_get(),
elm_code_widget_cursor_position_get(&col, &row));

View File

@ -102,6 +102,77 @@ elm_code_widget_selection_clear(Evas_Object *widget)
eo_do(widget, eo_event_callback_call(ELM_CODE_WIDGET_EVENT_SELECTION_CLEARED, widget));
}
static void
_elm_code_widget_selection_delete_single(Elm_Code_Widget_Data *pd)
{
Elm_Code_Line *line;
const char *old;
unsigned int old_length, length;
char *content;
if (pd->selection->end_col <= pd->selection->start_col)
return;
line = elm_code_file_line_get(pd->code->file, pd->selection->start_line);
old = elm_code_line_text_get(line, &old_length);
length = line->length - (pd->selection->end_col - pd->selection->start_col);
content = malloc(sizeof(char) * (length + 1));
snprintf(content, pd->selection->start_col, old);
snprintf(content + pd->selection->start_col - 1, old_length - pd->selection->end_col + 1,
old + pd->selection->end_col);
elm_code_line_text_set(line, content, length);
free(content);
}
static void
_elm_code_widget_selection_delete_multi(Elm_Code_Widget_Data *pd)
{
Elm_Code_Line *line;
const char *first, *last;
unsigned int last_length, length, i;
char *content;
if (pd->selection->end_line <= pd->selection->start_line)
return;
line = elm_code_file_line_get(pd->code->file, pd->selection->start_line);
first = elm_code_line_text_get(line, NULL);
line = elm_code_file_line_get(pd->code->file, pd->selection->end_line);
last = elm_code_line_text_get(line, &last_length);
length = pd->selection->start_col + last_length - pd->selection->end_col + 1;
content = malloc(sizeof(char) * (length + 1));
snprintf(content, pd->selection->start_col, first);
snprintf(content + pd->selection->start_col - 1, last_length - pd->selection->end_col + 1,
last + pd->selection->end_col);
for (i = line->number; i > pd->selection->start_line; i--)
elm_code_file_line_remove(pd->code->file, i);
line = elm_code_file_line_get(pd->code->file, pd->selection->start_line);
elm_code_line_text_set(line, content, length);
free(content);
}
EAPI void
elm_code_widget_selection_delete(Evas_Object *widget)
{
Elm_Code_Widget_Data *pd;
pd = eo_data_scope_get(widget, ELM_CODE_WIDGET_CLASS);
if (!pd->selection)
return;
if (pd->selection->start_line == pd->selection->end_line)
_elm_code_widget_selection_delete_single(pd);
else
_elm_code_widget_selection_delete_multi(pd);
free(pd->selection);
pd->selection = NULL;
eo_do(widget, eo_event_callback_call(ELM_CODE_WIDGET_EVENT_SELECTION_CLEARED, widget));
}
static char *
_elm_code_widget_selection_text_single_get(Elm_Code_Widget_Data *pd)
{
@ -178,6 +249,4 @@ elm_code_widget_selection_text_get(Evas_Object *widget)
return _elm_code_widget_selection_text_single_get(pd);
else
return _elm_code_widget_selection_text_multi_get(pd);
return strdup("TODO");
}

View File

@ -21,6 +21,8 @@ EAPI void elm_code_widget_selection_end(Evas_Object *widget, unsigned int line,
EAPI void elm_code_widget_selection_clear(Evas_Object *widget);
EAPI void elm_code_widget_selection_delete(Evas_Object *widget);
EAPI char *elm_code_widget_selection_text_get(Evas_Object *widget);
/**

View File

@ -126,11 +126,123 @@ START_TEST (elm_code_test_widget_selection_text_get_multiline)
}
END_TEST
START_TEST (elm_code_test_widget_selection_delete)
{
Elm_Code *code;
Elm_Code_File *file;
Elm_Code_Line *line;
Elm_Code_Widget *widget;
Evas_Object *win;
const char *text;
elm_init(1, NULL);
code = elm_code_create();
file = elm_code_file_new(code);
elm_code_file_line_append(file, "text", 4, NULL);
win = elm_win_add(NULL, "code", ELM_WIN_BASIC);
widget = eo_add(ELM_CODE_WIDGET_CLASS, win,
elm_code_widget_code_set(code));
line = elm_code_file_line_get(file, 1);
text = elm_code_line_text_get(line, NULL);
ck_assert_str_eq("text", text);
elm_code_widget_selection_start(widget, 1, 2);
elm_code_widget_selection_end(widget, 1, 3);
elm_code_widget_selection_delete(widget);
line = elm_code_file_line_get(file, 1);
text = elm_code_line_text_get(line, NULL);
ck_assert_str_eq("tt", text);
elm_code_free(code);
elm_shutdown();
}
END_TEST
START_TEST (elm_code_test_widget_selection_delete_twoline)
{
Elm_Code *code;
Elm_Code_File *file;
Elm_Code_Line *line;
Elm_Code_Widget *widget;
Evas_Object *win;
const char *text;
elm_init(1, NULL);
code = elm_code_create();
file = elm_code_file_new(code);
elm_code_file_line_append(file, "text", 4, NULL);
elm_code_file_line_append(file, "TEXT", 4, NULL);
win = elm_win_add(NULL, "code", ELM_WIN_BASIC);
widget = eo_add(ELM_CODE_WIDGET_CLASS, win,
elm_code_widget_code_set(code));
line = elm_code_file_line_get(file, 1);
text = elm_code_line_text_get(line, NULL);
ck_assert_str_eq("text", text);
ck_assert_int_eq(2, elm_code_file_lines_get(file));
elm_code_widget_selection_start(widget, 1, 3);
elm_code_widget_selection_end(widget, 2, 2);
elm_code_widget_selection_delete(widget);
line = elm_code_file_line_get(file, 1);
text = elm_code_line_text_get(line, NULL);
ck_assert_str_eq("teXT", text);
ck_assert_int_eq(1, elm_code_file_lines_get(file));
elm_code_free(code);
elm_shutdown();
}
END_TEST
START_TEST (elm_code_test_widget_selection_delete_multiline)
{
Elm_Code *code;
Elm_Code_File *file;
Elm_Code_Line *line;
Elm_Code_Widget *widget;
Evas_Object *win;
const char *text;
elm_init(1, NULL);
code = elm_code_create();
file = elm_code_file_new(code);
elm_code_file_line_append(file, "text", 4, NULL);
elm_code_file_line_append(file, "remove", 6, NULL);
elm_code_file_line_append(file, "TEXT", 4, NULL);
win = elm_win_add(NULL, "code", ELM_WIN_BASIC);
widget = eo_add(ELM_CODE_WIDGET_CLASS, win,
elm_code_widget_code_set(code));
line = elm_code_file_line_get(file, 1);
text = elm_code_line_text_get(line, NULL);
ck_assert_str_eq("text", text);
ck_assert_int_eq(3, elm_code_file_lines_get(file));
elm_code_widget_selection_start(widget, 1, 3);
elm_code_widget_selection_end(widget, 3, 2);
elm_code_widget_selection_delete(widget);
line = elm_code_file_line_get(file, 1);
text = elm_code_line_text_get(line, NULL);
ck_assert_str_eq("teXT", text);
ck_assert_int_eq(1, elm_code_file_lines_get(file));
elm_code_free(code);
elm_shutdown();
}
END_TEST
void elm_code_test_widget_selection(TCase *tc)
{
tcase_add_test(tc, elm_code_test_widget_selection_set);
tcase_add_test(tc, elm_code_test_widget_selection_text_get);
tcase_add_test(tc, elm_code_test_widget_selection_text_get_twoline);
tcase_add_test(tc, elm_code_test_widget_selection_text_get_multiline);
tcase_add_test(tc, elm_code_test_widget_selection_delete);
tcase_add_test(tc, elm_code_test_widget_selection_delete_twoline);
tcase_add_test(tc, elm_code_test_widget_selection_delete_multiline);
}