diff --git a/lv_conf_template.h b/lv_conf_template.h index 6fb68df88..fe16371be 100644 --- a/lv_conf_template.h +++ b/lv_conf_template.h @@ -354,7 +354,7 @@ typedef void * lv_obj_user_data_t; /*Hor, or ver. scroll speed [px/sec] in 'LV_LABEL_LONG_ROLL/ROLL_CIRC' mode*/ # define LV_LABEL_DEF_SCROLL_SPEED 25 # define LV_LABEL_WAIT_CHAR_COUNT 3 /* Waiting period at beginning/end of animation cycle */ -# define LV_LABEL_TEXT_SELECTION 1 +# define LV_LABEL_TEXT_SEL 1 /*Enable selecting text of the label */ #endif /*LED (dependencies: -)*/ diff --git a/src/lv_objx/lv_label.c b/src/lv_objx/lv_label.c index d65cdcd1a..5cdad6147 100644 --- a/src/lv_objx/lv_label.c +++ b/src/lv_objx/lv_label.c @@ -93,7 +93,7 @@ lv_obj_t * lv_label_create(lv_obj_t * par, const lv_obj_t * copy) ext->anim_speed = LV_LABEL_DEF_SCROLL_SPEED; ext->offset.x = 0; ext->offset.y = 0; -#if LV_LABEL_TEXT_SELECTION +#if LV_LABEL_TEXT_SEL ext->txt_sel_start = LV_LABEL_TEXT_SEL_OFF; ext->txt_sel_end = LV_LABEL_TEXT_SEL_OFF; #endif @@ -354,7 +354,7 @@ void lv_label_set_anim_speed(lv_obj_t * label, uint16_t anim_speed) } void lv_label_set_text_sel_start( lv_obj_t * label, uint16_t index ) { -#if LV_LABEL_TEXT_SELECTION +#if LV_LABEL_TEXT_SEL lv_label_ext_t * ext = lv_obj_get_ext_attr(label); ext->txt_sel_start = index; lv_obj_invalidate(label); @@ -363,7 +363,7 @@ void lv_label_set_text_sel_start( lv_obj_t * label, uint16_t index ) { void lv_label_set_text_sel_end( lv_obj_t * label, uint16_t index ) { -#if LV_LABEL_TEXT_SELECTION +#if LV_LABEL_TEXT_SEL lv_label_ext_t * ext = lv_obj_get_ext_attr(label); ext->txt_sel_end = index; lv_obj_invalidate(label); @@ -594,8 +594,8 @@ uint16_t lv_label_get_letter_on(const lv_obj_t * label, lv_point_t * pos) * @param label pointer to a label object. * @return selection start index. `LV_LABEL_TXT_SEL_OFF` if nothing is selected. */ -int32_t lv_label_get_txt_sel_start( const lv_obj_t * label ) { -#if LV_LABEL_TEXT_SELECTION +uint16_t lv_label_get_text_sel_start( const lv_obj_t * label ) { +#if LV_LABEL_TEXT_SEL lv_label_ext_t * ext = lv_obj_get_ext_attr(label); return ext->txt_sel_start; @@ -609,11 +609,12 @@ int32_t lv_label_get_txt_sel_start( const lv_obj_t * label ) { * @param label pointer to a label object. * @return selection end index. `LV_LABEL_TXT_SEL_OFF` if nothing is selected. */ -int32_t lv_label_get_tsxt_sel_end( const lv_obj_t * label ) { -#if LV_LABEL_TEXT_SELECTION +uint16_t lv_label_get_text_sel_end( const lv_obj_t * label ) { +#if LV_LABEL_TEXT_SEL lv_label_ext_t * ext = lv_obj_get_ext_attr(label); return ext->txt_sel_end; #else + (void) label; /*Unused*/ return LV_LABEL_TEXT_SEL_OFF; #endif } @@ -824,7 +825,7 @@ static bool lv_label_design(lv_obj_t * label, const lv_area_t * mask, lv_design_ } lv_draw_label(&coords, mask, style, opa_scale, ext->text, flag, &ext->offset, - lv_label_get_txt_sel_start(label), lv_label_get_tsxt_sel_end(label)); + lv_label_get_text_sel_start(label), lv_label_get_text_sel_end(label)); if(ext->long_mode == LV_LABEL_LONG_ROLL_CIRC) { lv_point_t size; @@ -839,7 +840,7 @@ static bool lv_label_design(lv_obj_t * label, const lv_area_t * mask, lv_design_ ofs.y = ext->offset.y; lv_draw_label(&coords, mask, style, opa_scale, ext->text, flag, &ofs, - lv_label_get_txt_sel_start(label), lv_label_get_tsxt_sel_end(label)); + lv_label_get_text_sel_start(label), lv_label_get_text_sel_end(label)); } /*Draw the text again below the original to make an circular effect */ @@ -847,7 +848,7 @@ static bool lv_label_design(lv_obj_t * label, const lv_area_t * mask, lv_design_ ofs.x = ext->offset.x; ofs.y = ext->offset.y + size.y + lv_font_get_height(style->text.font); lv_draw_label(&coords, mask, style, opa_scale, ext->text, flag, &ofs, - lv_label_get_txt_sel_start(label), lv_label_get_tsxt_sel_end(label)); + lv_label_get_text_sel_start(label), lv_label_get_text_sel_end(label)); } } } diff --git a/src/lv_objx/lv_label.h b/src/lv_objx/lv_label.h index 285df0f6c..cd2e25a9e 100644 --- a/src/lv_objx/lv_label.h +++ b/src/lv_objx/lv_label.h @@ -71,7 +71,7 @@ typedef struct uint16_t anim_speed; /*Speed of scroll and roll animation in px/sec unit*/ lv_point_t offset; /*Text draw position offset*/ -#if LV_LABEL_TEXT_SELECTION +#if LV_LABEL_TEXT_SEL uint16_t txt_sel_start; /*Left-most selection character*/ uint16_t txt_sel_end; /*Right-most selection character*/ #endif @@ -273,14 +273,14 @@ static inline const lv_style_t * lv_label_get_style(const lv_obj_t * label) * @param label pointer to a label object. * @return selection start index. `LV_LABEL_TXT_SEL_OFF` if nothing is selected. */ -int32_t lv_label_get_text_sel_start( const lv_obj_t * label ); +uint16_t lv_label_get_text_sel_start( const lv_obj_t * label ); /** * @brief Get the selection end index. * @param label pointer to a label object. * @return selection end index. `LV_LABEL_TXT_SEL_OFF` if nothing is selected. */ -int32_t lv_label_get_tsxt_sel_end( const lv_obj_t * label ); +uint16_t lv_label_get_text_sel_end( const lv_obj_t * label ); /*===================== diff --git a/src/lv_objx/lv_ta.c b/src/lv_objx/lv_ta.c index 7a9b76213..b6a912ba0 100644 --- a/src/lv_objx/lv_ta.c +++ b/src/lv_objx/lv_ta.c @@ -795,10 +795,16 @@ void lv_ta_set_style(lv_obj_t * ta, lv_ta_style_t type, const lv_style_t * style */ void lv_ta_set_text_sel(lv_obj_t * ta, bool en) { +#if LV_LABEL_TEXT_SEL lv_ta_ext_t * ext = lv_obj_get_ext_attr(ta); + ext->text_sel_en = en; if(!en) lv_ta_clear_selection(ta); +#else + (void) ta; /*Unused*/ + (void) en; /*Unused*/ +#endif } /*===================== @@ -950,8 +956,19 @@ const lv_style_t * lv_ta_get_style(const lv_obj_t * ta, lv_ta_style_t type) */ bool lv_ta_text_is_selected(const lv_obj_t * ta) { - lv_ta_ext_t * ext = lv_obj_get_ext_attr(ta); - return (lv_label_get_txt_sel_start(ext->label) == LV_LABEL_TEXT_SEL_OFF || lv_label_get_tsxt_sel_end(ext->label) == LV_LABEL_TEXT_SEL_OFF); +#if LV_LABEL_TEXT_SEL + lv_ta_ext_t * ext = lv_obj_get_ext_attr(ta); + + if((lv_label_get_text_sel_start(ext->label) == LV_LABEL_TEXT_SEL_OFF || + lv_label_get_text_sel_end(ext->label) == LV_LABEL_TEXT_SEL_OFF)){ + return true; + } else { + return false; + } +#else + (void) ta; /*Unused*/ + return false; +#endif } /** @@ -959,11 +976,15 @@ bool lv_ta_text_is_selected(const lv_obj_t * ta) * @param ta pointer to a text area object * @return true: selection mode is enabled, false: disabled */ -bool lv_ta_get_text_sel_mode(lv_obj_t * ta) +bool lv_ta_get_text_sel_en(lv_obj_t * ta) { +#if LV_LABEL_TEXT_SEL lv_ta_ext_t * ext = lv_obj_get_ext_attr(ta); - return ext->text_sel_en; +#else + (void) ta; /*Unused*/ + return false; +#endif } /*===================== @@ -976,13 +997,17 @@ bool lv_ta_get_text_sel_mode(lv_obj_t * ta) */ void lv_ta_clear_selection(lv_obj_t * ta) { +#if LV_LABEL_TEXT_SEL lv_ta_ext_t * ext = lv_obj_get_ext_attr(ta); - if(lv_label_get_txt_sel_start(ext->label) != LV_LABEL_TEXT_SEL_OFF || - lv_label_get_tsxt_sel_end(ext->label) != LV_LABEL_TEXT_SEL_OFF){ + if(lv_label_get_text_sel_start(ext->label) != LV_LABEL_TEXT_SEL_OFF || + lv_label_get_text_sel_end(ext->label) != LV_LABEL_TEXT_SEL_OFF){ lv_label_set_text_sel_start(ext->label, LV_LABEL_TEXT_SEL_OFF); lv_label_set_text_sel_end(ext->label, LV_LABEL_TEXT_SEL_OFF); } +#else + (void) ta; /*Unused*/ +#endif } /** @@ -1612,18 +1637,12 @@ static void update_cursor_position_on_click(lv_obj_t * ta, lv_signal_t sign, } lv_ta_ext_t * ext = lv_obj_get_ext_attr(ta); - lv_label_ext_t * ext_label = lv_obj_get_ext_attr(ext->label); lv_area_t label_coords; - bool click_outside_label; - uint16_t index_of_char_at_position; - lv_obj_get_coords(ext->label, &label_coords); lv_point_t point_act, vect_act; - lv_indev_get_point(click_source, &point_act); - lv_indev_get_vect(click_source, &vect_act); if(point_act.x < 0 || point_act.y < 0) return; /*Ignore event from keypad*/ @@ -1633,6 +1652,12 @@ static void update_cursor_position_on_click(lv_obj_t * ta, lv_signal_t sign, lv_coord_t label_width = lv_obj_get_width(ext->label); + uint16_t index_of_char_at_position; + + +#if LV_LABEL_TEXT_SEL + lv_label_ext_t * ext_label = lv_obj_get_ext_attr(ext->label); + bool click_outside_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; @@ -1697,6 +1722,21 @@ static void update_cursor_position_on_click(lv_obj_t * ta, lv_signal_t sign, ext->text_sel_in_prog = 0; } } +#else + /*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); + } + + if(sign == LV_SIGNAL_PRESSED) + lv_ta_set_cursor_pos(ta, index_of_char_at_position); +#endif } #endif diff --git a/src/lv_objx/lv_ta.h b/src/lv_objx/lv_ta.h index 9757ea152..41a5b4579 100644 --- a/src/lv_objx/lv_ta.h +++ b/src/lv_objx/lv_ta.h @@ -59,8 +59,7 @@ typedef struct lv_page_ext_t page; /*Ext. of ancestor*/ /*New data for this type */ lv_obj_t * label; /*Label of the text area*/ - lv_obj_t * placeholder; /*Place holder label of the text area, only visible if text is an empty - string*/ + lv_obj_t * placeholder; /*Place holder label. only visible if text is an empty string*/ char * pwd_tmp; /*Used to store the original text in password mode*/ const char * accapted_chars; /*Only these characters will be accepted. NULL: accept all*/ uint16_t max_length; /*The max. number of characters. 0: no limit*/ @@ -68,21 +67,22 @@ typedef struct uint8_t one_line : 1; /*One line mode (ignore line breaks)*/ struct { - const lv_style_t * style; /*Style of the cursor (NULL to use label's style)*/ - lv_coord_t valid_x; /*Used when stepping up/down in text area when stepping to a shorter - line. (Handled by the library)*/ - uint16_t - pos; /*The current cursor position (0: before 1. letter; 1: before 2. letter etc.)*/ - lv_area_t area; /*Cursor area relative to the Text Area*/ - uint16_t txt_byte_pos; /*Byte index of the letter after (on) the cursor*/ - lv_cursor_type_t type : 4; /*Shape of the cursor*/ - uint8_t - state : 1; /*Indicates that the cursor is visible now or not (Handled by the library)*/ + const lv_style_t * style; /* Style of the cursor (NULL to use label's style)*/ + lv_coord_t valid_x; /* Used when stepping up/down to a shorter line. + * (Used by the library)*/ + uint16_t pos; /* The current cursor position + * (0: before 1st letter; 1: before 2nd letter ...)*/ + lv_area_t area; /* Cursor area relative to the Text Area*/ + uint16_t txt_byte_pos; /* Byte index of the letter after (on) the cursor*/ + lv_cursor_type_t type : 4; /* Shape of the cursor*/ + uint8_t state : 1; /*Cursor is visible now or not (Handled by the library)*/ } cursor; - int tmp_sel_start; /*Temporary value*/ - int tmp_sel_end; /*Temporary value*/ +#if LV_LABEL_TEXT_SEL + uint16_t tmp_sel_start; /*Temporary value*/ + uint16_t tmp_sel_end; /*Temporary value*/ uint8_t text_sel_in_prog : 1; /*User is in process of selecting */ uint8_t text_sel_en : 1; /*Text can be selected on this text area*/ +#endif } lv_ta_ext_t; enum { @@ -388,7 +388,7 @@ bool lv_ta_text_is_selected(const lv_obj_t * ta); * @param ta pointer to a text area object * @return true: selection mode is enabled, false: disabled */ -bool lv_ta_get_text_sel_mode(lv_obj_t * ta); +bool lv_ta_get_text_sel_en(lv_obj_t * ta); /*===================== * Other functions