From dc9cffec5d316c4c19a062f732abdf479359d314 Mon Sep 17 00:00:00 2001 From: MiSimon Date: Sat, 2 Feb 2019 00:19:18 +0100 Subject: [PATCH 1/5] Position cursor at the position at which lv_ta was clicked --- lv_objx/lv_ta.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/lv_objx/lv_ta.c b/lv_objx/lv_ta.c index 69c12ce03..521ca61ad 100644 --- a/lv_objx/lv_ta.c +++ b/lv_objx/lv_ta.c @@ -53,6 +53,7 @@ static void pwd_char_hider(lv_obj_t * ta); static bool char_is_accepted(lv_obj_t * ta, uint32_t c); static void get_cursor_style(lv_obj_t * ta, lv_style_t * style_res); static void refr_cursor_area(lv_obj_t * ta); +static lv_res_t label_signal_wrapper(lv_obj_t * ta, lv_signal_t sign, void * param); /********************** * STATIC VARIABLES @@ -61,6 +62,7 @@ static lv_design_func_t ancestor_design; static lv_design_func_t scrl_design; static lv_signal_func_t ancestor_signal; static lv_signal_func_t scrl_signal; +static lv_signal_func_t label_signal_original; /********************** * MACROS @@ -153,6 +155,11 @@ lv_obj_t * lv_ta_create(lv_obj_t * par, const lv_obj_t * copy) lv_obj_refresh_style(new_ta); } + /*Wrap the labels signal function and make it clickable*/ + if(label_signal_original) label_signal_original = lv_obj_get_signal_func(ext->label); + lv_obj_set_signal_func(ext->label, label_signal_wrapper); + lv_obj_set_click(ext->label, true); + #if USE_LV_ANIMATION /*Create a cursor blinker animation*/ lv_anim_t a; @@ -1090,7 +1097,36 @@ static lv_res_t lv_ta_signal(lv_obj_t * ta, lv_signal_t sign, void * param) lv_ta_set_cursor_type(ta, cur_type & (~LV_CURSOR_HIDDEN)); } #endif + } else if(sign == LV_SIGNAL_PRESSED) { + lv_indev_t * indev = (lv_indev_t *)param; + lv_area_t label_coords; + uint16_t index_of_char_at_position; + + lv_obj_get_coords(ext->label, &label_coords); + + lv_point_t relative_position = { + indev->proc.act_point.x - label_coords.x1, + indev->proc.act_point.y - label_coords.y1 + }; + + lv_coord_t label_width = lv_obj_get_width(ext->label); + + /*Check if the click happend on the left side of the area ouside the label*/ + if (relative_position.x < 0) { + index_of_char_at_position = 0; + } + /*Check if the click happend on the right side of the area ouside the label*/ + else if (relative_position.x >= label_width) { + index_of_char_at_position = LV_TA_CURSOR_LAST; + } + else { + index_of_char_at_position = lv_label_get_letter_on(ext->label, &relative_position); + + } + + lv_ta_set_cursor_pos(ta, index_of_char_at_position); } + return res; } @@ -1345,4 +1381,27 @@ static void refr_cursor_area(lv_obj_t * ta) lv_inv_area(&area_tmp); } +static lv_res_t label_signal_wrapper(lv_obj_t * label, lv_signal_t sign, void * param) +{ + if(sign == LV_SIGNAL_PRESSED) { + lv_obj_t * parent; + + parent = lv_obj_get_parent(label); + /*Get the parent (ta) of the parent (scrl part)*/ + parent = parent ? lv_obj_get_parent(parent) : NULL; + + /*Forward the pressed event to the parent, which is the ta*/ + if(parent) { + lv_signal_func_t parent_signal = lv_obj_get_signal_func(parent); + + if(parent_signal) { + if(parent_signal(parent, sign, param) != LV_RES_OK) return LV_RES_INV; + } + } + } + + if(label_signal_original) return label_signal_original(label, sign, param); + + return LV_RES_OK; +} #endif From 4e61a2da338818914aa3b3c544c9a483a1702b41 Mon Sep 17 00:00:00 2001 From: MiSimon Date: Sat, 2 Feb 2019 00:21:50 +0100 Subject: [PATCH 2/5] Fixed missing NULL check --- lv_objx/lv_ta.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lv_objx/lv_ta.c b/lv_objx/lv_ta.c index 521ca61ad..4518bfd6c 100644 --- a/lv_objx/lv_ta.c +++ b/lv_objx/lv_ta.c @@ -156,7 +156,7 @@ lv_obj_t * lv_ta_create(lv_obj_t * par, const lv_obj_t * copy) } /*Wrap the labels signal function and make it clickable*/ - if(label_signal_original) label_signal_original = lv_obj_get_signal_func(ext->label); + if(label_signal_original == NULL) label_signal_original = lv_obj_get_signal_func(ext->label); lv_obj_set_signal_func(ext->label, label_signal_wrapper); lv_obj_set_click(ext->label, true); From 1e6dc74c5a76bafe541490cd75ec3f0d88eaea1c Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Sat, 2 Feb 2019 05:00:59 +0100 Subject: [PATCH 3/5] lv_ta click simplification --- lv_objx/lv_ta.c | 85 ++++++++++++++++--------------------------------- 1 file changed, 28 insertions(+), 57 deletions(-) diff --git a/lv_objx/lv_ta.c b/lv_objx/lv_ta.c index 4518bfd6c..3b5510cac 100644 --- a/lv_objx/lv_ta.c +++ b/lv_objx/lv_ta.c @@ -53,7 +53,6 @@ static void pwd_char_hider(lv_obj_t * ta); static bool char_is_accepted(lv_obj_t * ta, uint32_t c); static void get_cursor_style(lv_obj_t * ta, lv_style_t * style_res); static void refr_cursor_area(lv_obj_t * ta); -static lv_res_t label_signal_wrapper(lv_obj_t * ta, lv_signal_t sign, void * param); /********************** * STATIC VARIABLES @@ -62,7 +61,6 @@ static lv_design_func_t ancestor_design; static lv_design_func_t scrl_design; static lv_signal_func_t ancestor_signal; static lv_signal_func_t scrl_signal; -static lv_signal_func_t label_signal_original; /********************** * MACROS @@ -155,11 +153,6 @@ lv_obj_t * lv_ta_create(lv_obj_t * par, const lv_obj_t * copy) lv_obj_refresh_style(new_ta); } - /*Wrap the labels signal function and make it clickable*/ - if(label_signal_original == NULL) label_signal_original = lv_obj_get_signal_func(ext->label); - lv_obj_set_signal_func(ext->label, label_signal_wrapper); - lv_obj_set_click(ext->label, true); - #if USE_LV_ANIMATION /*Create a cursor blinker animation*/ lv_anim_t a; @@ -1097,7 +1090,34 @@ static lv_res_t lv_ta_signal(lv_obj_t * ta, lv_signal_t sign, void * param) lv_ta_set_cursor_type(ta, cur_type & (~LV_CURSOR_HIDDEN)); } #endif - } else if(sign == LV_SIGNAL_PRESSED) { + } + return res; +} + +/** + * Signal function of the scrollable part of the text area + * @param scrl pointer to scrollable part of a text area object + * @param sign a signal type from lv_signal_t enum + * @param param pointer to a signal specific variable + * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted + */ +static lv_res_t lv_ta_scrollable_signal(lv_obj_t * scrl, lv_signal_t sign, void * param) +{ + lv_res_t res; + + /* Include the ancient signal function */ + res = scrl_signal(scrl, sign, param); + if(res != LV_RES_OK) return res; + + lv_obj_t * ta = lv_obj_get_parent(scrl); + lv_ta_ext_t * ext = lv_obj_get_ext_attr(ta); + if(sign == LV_SIGNAL_REFR_EXT_SIZE) { + /*Set ext. size because the cursor might be out of this object*/ + lv_style_t * style_label = lv_obj_get_style(ext->label); + lv_coord_t font_h = lv_font_get_height(style_label->text.font); + scrl->ext_size = LV_MATH_MAX(scrl->ext_size, style_label->text.line_space + font_h); + } + else if(sign == LV_SIGNAL_PRESSED) { lv_indev_t * indev = (lv_indev_t *)param; lv_area_t label_coords; uint16_t index_of_char_at_position; @@ -1127,32 +1147,6 @@ static lv_res_t lv_ta_signal(lv_obj_t * ta, lv_signal_t sign, void * param) lv_ta_set_cursor_pos(ta, index_of_char_at_position); } - return res; -} - -/** - * Signal function of the scrollable part of the text area - * @param scrl pointer to scrollable part of a text area object - * @param sign a signal type from lv_signal_t enum - * @param param pointer to a signal specific variable - * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted - */ -static lv_res_t lv_ta_scrollable_signal(lv_obj_t * scrl, lv_signal_t sign, void * param) -{ - lv_res_t res; - - /* Include the ancient signal function */ - res = scrl_signal(scrl, sign, param); - if(res != LV_RES_OK) return res; - - if(sign == LV_SIGNAL_REFR_EXT_SIZE) { - /*Set ext. size because the cursor might be out of this object*/ - lv_obj_t * ta = lv_obj_get_parent(scrl); - lv_ta_ext_t * ext = lv_obj_get_ext_attr(ta); - lv_style_t * style_label = lv_obj_get_style(ext->label); - lv_coord_t font_h = lv_font_get_height(style_label->text.font); - scrl->ext_size = LV_MATH_MAX(scrl->ext_size, style_label->text.line_space + font_h); - } return res; } @@ -1381,27 +1375,4 @@ static void refr_cursor_area(lv_obj_t * ta) lv_inv_area(&area_tmp); } -static lv_res_t label_signal_wrapper(lv_obj_t * label, lv_signal_t sign, void * param) -{ - if(sign == LV_SIGNAL_PRESSED) { - lv_obj_t * parent; - - parent = lv_obj_get_parent(label); - /*Get the parent (ta) of the parent (scrl part)*/ - parent = parent ? lv_obj_get_parent(parent) : NULL; - - /*Forward the pressed event to the parent, which is the ta*/ - if(parent) { - lv_signal_func_t parent_signal = lv_obj_get_signal_func(parent); - - if(parent_signal) { - if(parent_signal(parent, sign, param) != LV_RES_OK) return LV_RES_INV; - } - } - } - - if(label_signal_original) return label_signal_original(label, sign, param); - - return LV_RES_OK; -} #endif From cfe0c14e56a7fd369d8703fa3155afa79a7f2851 Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Sat, 2 Feb 2019 05:11:18 +0100 Subject: [PATCH 4/5] ta click: add to lv_ta_signal too --- lv_objx/lv_ta.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/lv_objx/lv_ta.c b/lv_objx/lv_ta.c index 3b5510cac..1b322c6bb 100644 --- a/lv_objx/lv_ta.c +++ b/lv_objx/lv_ta.c @@ -1091,6 +1091,30 @@ static lv_res_t lv_ta_signal(lv_obj_t * ta, lv_signal_t sign, void * param) } #endif } + else if(sign == LV_SIGNAL_PRESSED) { + lv_indev_t * indev = (lv_indev_t *)param; + lv_area_t label_coords; + uint16_t index_of_char_at_position; + + lv_obj_get_coords(ext->label, &label_coords); + + lv_point_t relative_position; + relative_position.x = indev->proc.act_point.x - label_coords.x1; + relative_position.y = indev->proc.act_point.y - label_coords.y1; + + lv_coord_t label_width = lv_obj_get_width(ext->label); + + /*Check if the click happened on the left side of the area outside the label*/ + if (relative_position.x < label_width / 2) { + index_of_char_at_position = 0; + } + /*Check if the click happened on the right side of the area outside the label*/ + else if (relative_position.x >= label_width / 2) { + index_of_char_at_position = LV_TA_CURSOR_LAST; + } + + lv_ta_set_cursor_pos(ta, index_of_char_at_position); + } return res; } @@ -1124,18 +1148,18 @@ static lv_res_t lv_ta_scrollable_signal(lv_obj_t * scrl, lv_signal_t sign, void lv_obj_get_coords(ext->label, &label_coords); - lv_point_t relative_position = { - indev->proc.act_point.x - label_coords.x1, - indev->proc.act_point.y - label_coords.y1 - }; + lv_point_t relative_position; + relative_position.x = indev->proc.act_point.x - label_coords.x1; + relative_position.y = indev->proc.act_point.y - label_coords.y1; + lv_coord_t label_width = lv_obj_get_width(ext->label); - /*Check if the click happend on the left side of the area ouside the label*/ + /*Check if the click happened on the left side of the area outside the label*/ if (relative_position.x < 0) { index_of_char_at_position = 0; } - /*Check if the click happend on the right side of the area ouside the label*/ + /*Check if the click happened on the right side of the area outside the label*/ else if (relative_position.x >= label_width) { index_of_char_at_position = LV_TA_CURSOR_LAST; } From 43b20179ed4c678e5bb7e705dfa4893051fd0195 Mon Sep 17 00:00:00 2001 From: MiSimon Date: Sat, 2 Feb 2019 22:59:28 +0100 Subject: [PATCH 5/5] removed code duplication --- lv_objx/lv_ta.c | 82 ++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 49 deletions(-) diff --git a/lv_objx/lv_ta.c b/lv_objx/lv_ta.c index 1b322c6bb..9c1fd3abc 100644 --- a/lv_objx/lv_ta.c +++ b/lv_objx/lv_ta.c @@ -53,6 +53,7 @@ static void pwd_char_hider(lv_obj_t * ta); static bool char_is_accepted(lv_obj_t * ta, uint32_t c); static void get_cursor_style(lv_obj_t * ta, lv_style_t * style_res); static void refr_cursor_area(lv_obj_t * ta); +static void update_cursor_position_on_click(lv_obj_t * ta, lv_indev_t * click_source); /********************** * STATIC VARIABLES @@ -1092,28 +1093,7 @@ static lv_res_t lv_ta_signal(lv_obj_t * ta, lv_signal_t sign, void * param) #endif } else if(sign == LV_SIGNAL_PRESSED) { - lv_indev_t * indev = (lv_indev_t *)param; - lv_area_t label_coords; - uint16_t index_of_char_at_position; - - lv_obj_get_coords(ext->label, &label_coords); - - lv_point_t relative_position; - relative_position.x = indev->proc.act_point.x - label_coords.x1; - relative_position.y = indev->proc.act_point.y - label_coords.y1; - - lv_coord_t label_width = lv_obj_get_width(ext->label); - - /*Check if the click happened on the left side of the area outside the label*/ - if (relative_position.x < label_width / 2) { - index_of_char_at_position = 0; - } - /*Check if the click happened on the right side of the area outside the label*/ - else if (relative_position.x >= label_width / 2) { - index_of_char_at_position = LV_TA_CURSOR_LAST; - } - - lv_ta_set_cursor_pos(ta, index_of_char_at_position); + update_cursor_position_on_click(ta, (lv_indev_t *) param); } return res; } @@ -1142,33 +1122,7 @@ static lv_res_t lv_ta_scrollable_signal(lv_obj_t * scrl, lv_signal_t sign, void scrl->ext_size = LV_MATH_MAX(scrl->ext_size, style_label->text.line_space + font_h); } else if(sign == LV_SIGNAL_PRESSED) { - lv_indev_t * indev = (lv_indev_t *)param; - lv_area_t label_coords; - uint16_t index_of_char_at_position; - - lv_obj_get_coords(ext->label, &label_coords); - - lv_point_t relative_position; - relative_position.x = indev->proc.act_point.x - label_coords.x1; - relative_position.y = indev->proc.act_point.y - label_coords.y1; - - - lv_coord_t label_width = lv_obj_get_width(ext->label); - - /*Check if the click happened on the left side of the area outside the label*/ - if (relative_position.x < 0) { - index_of_char_at_position = 0; - } - /*Check if the click happened on the right side of the area outside the label*/ - else if (relative_position.x >= label_width) { - index_of_char_at_position = LV_TA_CURSOR_LAST; - } - else { - index_of_char_at_position = lv_label_get_letter_on(ext->label, &relative_position); - - } - - lv_ta_set_cursor_pos(ta, index_of_char_at_position); + update_cursor_position_on_click(ta, (lv_indev_t *)param); } @@ -1399,4 +1353,34 @@ static void refr_cursor_area(lv_obj_t * ta) lv_inv_area(&area_tmp); } +static void update_cursor_position_on_click(lv_obj_t * ta, lv_indev_t * click_source) +{ + lv_ta_ext_t * ext = lv_obj_get_ext_attr(ta); + + lv_area_t label_coords; + uint16_t index_of_char_at_position; + + lv_obj_get_coords(ext->label, &label_coords); + + lv_point_t relative_position; + relative_position.x = click_source->proc.act_point.x - label_coords.x1; + relative_position.y = click_source->proc.act_point.y - label_coords.y1; + + lv_coord_t label_width = lv_obj_get_width(ext->label); + + /*Check if the click happened on the left side of the area outside the label*/ + if (relative_position.x < 0) { + index_of_char_at_position = 0; + } + /*Check if the click happened on the right side of the area outside the label*/ + else if (relative_position.x >= label_width) { + index_of_char_at_position = LV_TA_CURSOR_LAST; + } + else { + index_of_char_at_position = lv_label_get_letter_on(ext->label, &relative_position); + } + + lv_ta_set_cursor_pos(ta, index_of_char_at_position); +} + #endif