elm_code: add copy/paste/cut undo

Summary: The patch for supporting copy,paste and cut undo

Test Plan:
1. elementary_test - Code Editor
2. Check that undo work correctly when copy,paste and cut

Reviewers: ajwillia.ms

Reviewed By: ajwillia.ms

Subscribers: cedric, jpeg

Differential Revision: https://phab.enlightenment.org/D4636
This commit is contained in:
YeongJong Lee 2017-02-01 09:08:14 +00:00 committed by Andy Williams
parent 0f915ab497
commit 251469e2fe
3 changed files with 33 additions and 7 deletions

View File

@ -1221,9 +1221,7 @@ _elm_code_widget_change_create(unsigned int start_col, unsigned int start_line,
info->end_col = end_col;
info->end_line = end_line;
info->content = malloc((length + 1) * sizeof(char));
strncpy(info->content, text, length);
info->content[length] = '\0';
info->content = strndup(text, length);
info->length = length;
return info;
@ -1251,7 +1249,7 @@ _elm_code_widget_change_selection_add(Evas_Object *widget)
change = _elm_code_widget_change_create(selection->start_col,
selection->start_line,
selection->end_col,
selection->end_col + 1,
selection->end_line,
selection_text,
strlen(selection_text),

View File

@ -68,4 +68,6 @@ EAPI Elm_Code_Widget_Selection_Data *elm_code_widget_selection_normalized_get(Ev
void _elm_code_widget_undo_change_add(Evas_Object *widget, Elm_Code_Widget_Change_Info *info);
void _elm_code_widget_change_selection_add(Evas_Object *widget);
#endif

View File

@ -356,14 +356,17 @@ elm_code_widget_selection_cut(Evas_Object *widget)
{
char *text;
if (elm_code_widget_selection_is_empty(widget))
return;
text = elm_code_widget_selection_text_get(widget);
elm_cnp_selection_set(widget, ELM_SEL_TYPE_CLIPBOARD, ELM_SEL_FORMAT_TEXT, text, strlen(text));
elm_cnp_selection_loss_callback_set(widget, ELM_SEL_TYPE_CLIPBOARD, _selection_loss_cb, widget);
free(text);
_elm_code_widget_change_selection_add(widget);
elm_code_widget_selection_delete(widget);
// TODO construct and pass a change object for cut and paste
efl_event_callback_legacy_call(widget, ELM_OBJ_CODE_WIDGET_EVENT_CHANGED_USER, NULL);
}
@ -372,6 +375,9 @@ elm_code_widget_selection_copy(Evas_Object *widget)
{
char *text;
if (elm_code_widget_selection_is_empty(widget))
return;
text = elm_code_widget_selection_text_get(widget);
elm_cnp_selection_set(widget, ELM_SEL_TYPE_CLIPBOARD, ELM_SEL_FORMAT_TEXT, text, strlen(text));
elm_cnp_selection_loss_callback_set(widget, ELM_SEL_TYPE_CLIPBOARD, _selection_loss_cb, widget);
@ -429,8 +435,10 @@ static Eina_Bool
_selection_paste_cb(void *data, Evas_Object *obj EINA_UNUSED, Elm_Selection_Data *ev)
{
Elm_Code *code;
Elm_Code_Line *line;
Elm_Code_Widget *widget;
unsigned int row, col;
Elm_Code_Widget_Change_Info *change;
unsigned int row, col, end_row, end_col, position;
widget = (Elm_Code_Widget *)data;
@ -447,7 +455,24 @@ _selection_paste_cb(void *data, Evas_Object *obj EINA_UNUSED, Elm_Selection_Data
else
_selection_paste_multi(widget, code, col, row, ev->data, ev->len - 1);
// TODO construct and pass a change object for cut and paste
elm_obj_code_widget_cursor_position_get(widget, &end_row, &end_col);
line = elm_code_file_line_get(code->file, end_row);
position = elm_code_widget_line_text_position_for_column_get(widget, line, end_col);
change = calloc(1, sizeof(Elm_Code_Widget_Change_Info));
change->insert = EINA_TRUE;
change->start_col = col;
change->start_line = row;
change->end_col = position;
change->end_line = end_row;
change->content = strndup(ev->data, ev->len);
change->length = ev->len;
_elm_code_widget_undo_change_add(widget, change);
free((char *)change->content);
free(change);
efl_event_callback_legacy_call(widget, ELM_OBJ_CODE_WIDGET_EVENT_CHANGED_USER, NULL);
return EINA_TRUE;
}
@ -455,6 +480,7 @@ _selection_paste_cb(void *data, Evas_Object *obj EINA_UNUSED, Elm_Selection_Data
EAPI void
elm_code_widget_selection_paste(Evas_Object *widget)
{
_elm_code_widget_change_selection_add(widget);
elm_code_widget_selection_delete(widget);
elm_cnp_selection_get(widget, ELM_SEL_TYPE_CLIPBOARD, ELM_SEL_FORMAT_TEXT, _selection_paste_cb, widget);