forked from enlightenment/edi
elm_code edit: support backspace and delete.
don't propogate backspace as other widgets use it to navigate adjust tokens left when we remove content and remove if 0 width token.
This commit is contained in:
parent
8f668b2deb
commit
afeab4b53b
|
@ -36,6 +36,25 @@ _elm_code_line_tokens_move_right(Elm_Code_Line *line, int position, int move)
|
|||
}
|
||||
}
|
||||
|
||||
static void
|
||||
_elm_code_line_tokens_move_left(Elm_Code_Line *line, int position, int move)
|
||||
{
|
||||
Eina_List *item, *next;
|
||||
Elm_Code_Token *token;
|
||||
|
||||
EINA_LIST_FOREACH_SAFE(line->tokens, item, next, token)
|
||||
{
|
||||
if (token->end >= position)
|
||||
token->end -= move;
|
||||
|
||||
if (token->start > position)
|
||||
token->start -= move;
|
||||
|
||||
if (token->end < token->start)
|
||||
line->tokens = eina_list_remove_list(line->tokens, item);
|
||||
}
|
||||
}
|
||||
|
||||
EAPI void
|
||||
elm_code_line_text_insert(Elm_Code_Line *line, unsigned int position, const char *string, int length)
|
||||
{
|
||||
|
@ -77,3 +96,43 @@ elm_code_line_text_insert(Elm_Code_Line *line, unsigned int position, const char
|
|||
file = line->file;
|
||||
elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, line);
|
||||
}
|
||||
|
||||
EAPI void
|
||||
elm_code_line_text_remove(Elm_Code_Line *line, unsigned int position, int length)
|
||||
{
|
||||
Elm_Code_File *file;
|
||||
char *removed;
|
||||
|
||||
if (!line)
|
||||
return;
|
||||
|
||||
removed = malloc(sizeof(char) * line->length - length + 1);
|
||||
if (position > 0)
|
||||
position--;
|
||||
if (position > line->length)
|
||||
position = line->length;
|
||||
|
||||
_elm_code_line_tokens_move_left(line, position + 1, length);
|
||||
|
||||
if (line->modified)
|
||||
{
|
||||
strncpy(removed, line->modified, position);
|
||||
strncpy(removed + position, line->modified + position + length, line->length - position - length);
|
||||
|
||||
free(line->modified);
|
||||
}
|
||||
else
|
||||
{
|
||||
strncpy(removed, line->content, position);
|
||||
strncpy(removed + position, line->content + position + length, line->length - position - length);
|
||||
}
|
||||
|
||||
line->modified = removed;
|
||||
line->length -= length;
|
||||
|
||||
// TODO update calculation
|
||||
line->unicode_length -= length;
|
||||
|
||||
file = line->file;
|
||||
elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, line);
|
||||
}
|
||||
|
|
|
@ -24,6 +24,8 @@ EAPI const char *elm_code_line_text_get(Elm_Code_Line *line, int *length);
|
|||
|
||||
EAPI void elm_code_line_text_insert(Elm_Code_Line *line, unsigned int position, const char *string, int length);
|
||||
|
||||
EAPI void elm_code_line_text_remove(Elm_Code_Line *line, unsigned int position, int length);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
|
|
@ -648,6 +648,45 @@ _elm_code_widget_newline(Elm_Code_Widget *widget)
|
|||
elm_code_widget_cursor_position_set(1, row + 1));
|
||||
}
|
||||
|
||||
static void
|
||||
_elm_code_widget_backspace(Elm_Code_Widget *widget)
|
||||
{
|
||||
Elm_Code *code;
|
||||
Elm_Code_Line *line;
|
||||
unsigned int row, col;
|
||||
|
||||
eo_do(widget,
|
||||
code = elm_code_widget_code_get(),
|
||||
elm_code_widget_cursor_position_get(&col, &row));
|
||||
|
||||
if (col <= 1)
|
||||
return;
|
||||
line = elm_code_file_line_get(code->file, row);
|
||||
|
||||
elm_code_line_text_remove(line, col - 1, 1);
|
||||
eo_do(widget,
|
||||
elm_code_widget_cursor_position_set(col - 1, row));
|
||||
}
|
||||
|
||||
static void
|
||||
_elm_code_widget_delete(Elm_Code_Widget *widget)
|
||||
{
|
||||
Elm_Code *code;
|
||||
Elm_Code_Line *line;
|
||||
unsigned int row, col;
|
||||
|
||||
eo_do(widget,
|
||||
code = elm_code_widget_code_get(),
|
||||
elm_code_widget_cursor_position_get(&col, &row));
|
||||
line = elm_code_file_line_get(code->file, row);
|
||||
if (col > line->unicode_length)
|
||||
return;
|
||||
|
||||
elm_code_line_text_remove(line, col, 1);
|
||||
eo_do(widget,
|
||||
elm_code_widget_cursor_position_set(col, row));
|
||||
}
|
||||
|
||||
static void
|
||||
_elm_code_widget_key_down_cb(void *data, Evas *evas EINA_UNUSED,
|
||||
Evas_Object *obj EINA_UNUSED, void *event_info)
|
||||
|
@ -676,6 +715,10 @@ _elm_code_widget_key_down_cb(void *data, Evas *evas EINA_UNUSED,
|
|||
|
||||
else if (!strcmp(ev->key, "KP_Enter") || !strcmp(ev->key, "Return"))
|
||||
_elm_code_widget_newline(widget);
|
||||
else if (!strcmp(ev->key, "BackSpace"))
|
||||
_elm_code_widget_backspace(widget);
|
||||
else if (!strcmp(ev->key, "Delete"))
|
||||
_elm_code_widget_delete(widget);
|
||||
|
||||
else if (ev->string && strlen(ev->string) == 1)
|
||||
_elm_code_widget_text_at_cursor_insert(widget, ev->string, 1);
|
||||
|
@ -713,6 +756,23 @@ _elm_code_widget_unfocused_event_cb(void *data, Evas_Object *obj,
|
|||
_elm_code_widget_fill(obj);
|
||||
}
|
||||
|
||||
EOLIAN static Eina_Bool
|
||||
_elm_code_widget_elm_widget_event(Eo *obj EINA_UNUSED, Elm_Code_Widget_Data *pd EINA_UNUSED,
|
||||
Evas_Object *src EINA_UNUSED, Evas_Callback_Type type, void *event_info)
|
||||
{
|
||||
Evas_Event_Key_Down *ev = event_info;
|
||||
|
||||
if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
|
||||
|
||||
if (!strcmp(ev->key, "BackSpace"))
|
||||
{
|
||||
ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
|
||||
return EINA_TRUE;
|
||||
}
|
||||
|
||||
return EINA_FALSE;
|
||||
}
|
||||
|
||||
EOLIAN static Eina_Bool
|
||||
_elm_code_widget_elm_widget_focus_next_manager_is(Eo *obj EINA_UNUSED,
|
||||
Elm_Code_Widget_Data *pd EINA_UNUSED)
|
||||
|
|
|
@ -173,6 +173,7 @@ class Elm_Code_Widget (Elm_Layout, Elm_Interface_Atspi_Text)
|
|||
Eo.Base.constructor;
|
||||
Eo.Base.finalize;
|
||||
Evas.Object_Smart.add;
|
||||
Elm_Widget.event;
|
||||
Elm_Widget.focus_next_manager_is;
|
||||
Elm_Widget.focus_direction_manager_is;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue