diff --git a/legacy/elm_code/src/lib/elm_code_text.c b/legacy/elm_code/src/lib/elm_code_text.c index 34397fd396..a9e86be57a 100644 --- a/legacy/elm_code/src/lib/elm_code_text.c +++ b/legacy/elm_code/src/lib/elm_code_text.c @@ -74,6 +74,21 @@ elm_code_line_text_contains(Elm_Code_Line *line, const char *search) return elm_code_line_text_strpos(line, search, 0) != ELM_CODE_TEXT_NOT_FOUND; } +EAPI char * +elm_code_line_text_substr(Elm_Code_Line *line, unsigned int position, int length) +{ + const char *content; + + if (!line || length < 1) + return strdup(""); + + if (position + length > line->length) + length = line->length - position; + + content = elm_code_line_text_get(line, NULL); + return strndup(content + position, length); +} + static void _elm_code_line_tokens_move_right(Elm_Code_Line *line, int position, int move) { diff --git a/legacy/elm_code/src/lib/elm_code_text.h b/legacy/elm_code/src/lib/elm_code_text.h index a051706734..3df2ff6774 100644 --- a/legacy/elm_code/src/lib/elm_code_text.h +++ b/legacy/elm_code/src/lib/elm_code_text.h @@ -30,6 +30,8 @@ EAPI int elm_code_line_text_strpos(Elm_Code_Line *line, const char *search, int EAPI Eina_Bool elm_code_line_text_contains(Elm_Code_Line *line, const char *search); +EAPI char *elm_code_line_text_substr(Elm_Code_Line *line, unsigned int position, 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); diff --git a/legacy/elm_code/src/lib/elm_code_widget_selection.c b/legacy/elm_code/src/lib/elm_code_widget_selection.c index 3a50c394c7..671ccd383b 100644 --- a/legacy/elm_code/src/lib/elm_code_widget_selection.c +++ b/legacy/elm_code/src/lib/elm_code_widget_selection.c @@ -102,15 +102,24 @@ elm_code_widget_selection_clear(Evas_Object *widget) eo_do(widget, eo_event_callback_call(ELM_CODE_WIDGET_EVENT_SELECTION_CLEARED, widget)); } -EAPI const char * +EAPI char * elm_code_widget_selection_text_get(Evas_Object *widget) { Elm_Code_Widget_Data *pd; + Elm_Code_Line *line; pd = eo_data_scope_get(widget, ELM_CODE_WIDGET_CLASS); if (!pd->selection) - return ""; + return strdup(""); - return "TODO"; + if (pd->selection->start_line == pd->selection->end_line) + { + line = elm_code_file_line_get(pd->code->file, pd->selection->start_line); + + return elm_code_line_text_substr(line, pd->selection->start_col - 1, + pd->selection->end_col - pd->selection->start_col + 1); + } + + return strdup("TODO"); } diff --git a/legacy/elm_code/src/lib/elm_code_widget_selection.h b/legacy/elm_code/src/lib/elm_code_widget_selection.h index 73e18227a1..0484414934 100644 --- a/legacy/elm_code/src/lib/elm_code_widget_selection.h +++ b/legacy/elm_code/src/lib/elm_code_widget_selection.h @@ -21,7 +21,7 @@ EAPI void elm_code_widget_selection_end(Evas_Object *widget, unsigned int line, EAPI void elm_code_widget_selection_clear(Evas_Object *widget); -EAPI const char *elm_code_widget_selection_text_get(Evas_Object *widget); +EAPI char *elm_code_widget_selection_text_get(Evas_Object *widget); /** * @} diff --git a/legacy/elm_code/src/tests/Makefile.am b/legacy/elm_code/src/tests/Makefile.am index 99ec87f35f..131aa1e448 100644 --- a/legacy/elm_code/src/tests/Makefile.am +++ b/legacy/elm_code/src/tests/Makefile.am @@ -12,6 +12,7 @@ elm_code_test_basic.c \ elm_code_test_parse.c \ elm_code_test_text.c \ elm_code_test_widget.c \ +elm_code_test_widget_selection.c \ elm_code_suite.c elm_code_suite_CPPFLAGS = -I$(top_builddir)/elm_code/src/lib/ \ diff --git a/legacy/elm_code/src/tests/elm_code_suite.c b/legacy/elm_code/src/tests/elm_code_suite.c index ec0f93c2bb..d45976869f 100644 --- a/legacy/elm_code/src/tests/elm_code_suite.c +++ b/legacy/elm_code/src/tests/elm_code_suite.c @@ -19,6 +19,7 @@ static const struct { { "text", elm_code_test_text }, { "basic", elm_code_test_basic }, { "widget", elm_code_test_widget }, + { "widget_selection", elm_code_test_widget_selection }, }; START_TEST(elm_code_initialization) diff --git a/legacy/elm_code/src/tests/elm_code_suite.h b/legacy/elm_code/src/tests/elm_code_suite.h index cd98285efa..d03e11b565 100644 --- a/legacy/elm_code/src/tests/elm_code_suite.h +++ b/legacy/elm_code/src/tests/elm_code_suite.h @@ -11,5 +11,6 @@ void elm_code_test_basic(TCase *tc); void elm_code_test_parse(TCase *tc); void elm_code_test_text(TCase *tc); void elm_code_test_widget(TCase *tc); +void elm_code_test_widget_selection(TCase *tc); #endif /* _EDLM_CODE_SUITE_H */ diff --git a/legacy/elm_code/src/tests/elm_code_test_widget_selection.c b/legacy/elm_code/src/tests/elm_code_test_widget_selection.c new file mode 100644 index 0000000000..512cb3c114 --- /dev/null +++ b/legacy/elm_code/src/tests/elm_code_test_widget_selection.c @@ -0,0 +1,73 @@ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "elm_code_suite.h" + +#include "elm_code_widget_selection.h" + +START_TEST (elm_code_test_widget_selection_set) +{ + Elm_Code *code; + Elm_Code_File *file; + Elm_Code_Widget *widget; + Evas_Object *win; + + elm_init(1, NULL); + code = elm_code_create(); + file = elm_code_file_new(code); + elm_code_file_line_append(file, "test", 4, NULL); + + win = elm_win_add(NULL, "entry", ELM_WIN_BASIC); + widget = eo_add(ELM_CODE_WIDGET_CLASS, win, + elm_code_widget_code_set(code)); + + elm_code_widget_selection_start(widget, 1, 2); + elm_code_widget_selection_end(widget, 1, 3); + elm_code_widget_selection_clear(widget); + + elm_code_free(code); + elm_shutdown(); +} +END_TEST + +START_TEST (elm_code_test_widget_selection_text_get) +{ + Elm_Code *code; + Elm_Code_File *file; + Elm_Code_Widget *widget; + Evas_Object *win; + char *selection; + + elm_init(1, NULL); + code = elm_code_create(); + file = elm_code_file_new(code); + elm_code_file_line_append(file, "test", 4, NULL); + + win = elm_win_add(NULL, "entry", ELM_WIN_BASIC); + widget = eo_add(ELM_CODE_WIDGET_CLASS, win, + elm_code_widget_code_set(code)); + + ck_assert_str_eq("", elm_code_widget_selection_text_get(widget)); + + elm_code_widget_selection_start(widget, 1, 2); + elm_code_widget_selection_end(widget, 1, 3); + + selection = elm_code_widget_selection_text_get(widget); + ck_assert_str_eq("es", selection); + free(selection); + + elm_code_widget_selection_clear(widget); + ck_assert_str_eq("", elm_code_widget_selection_text_get(widget)); + + 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); +} +