/** * @file lv_ta.c * */ /********************* * INCLUDES *********************/ #include "lv_textarea.h" #if LV_USE_TEXTAREA != 0 #include #include "../lv_misc/lv_debug.h" #include "../lv_core/lv_group.h" #include "../lv_core/lv_refr.h" #include "../lv_core/lv_indev.h" #include "../lv_draw/lv_draw.h" #include "../lv_misc/lv_anim.h" #include "../lv_misc/lv_txt.h" #include "../lv_misc/lv_math.h" /********************* * DEFINES *********************/ #define MY_CLASS &lv_textarea /*Test configuration*/ #ifndef LV_TEXTAREA_DEF_CURSOR_BLINK_TIME #define LV_TEXTAREA_DEF_CURSOR_BLINK_TIME 400 /*ms*/ #endif #ifndef LV_TEXTAREA_DEF_PWD_SHOW_TIME #define LV_TEXTAREA_DEF_PWD_SHOW_TIME 1500 /*ms*/ #endif #define LV_TEXTAREA_DEF_WIDTH (2 * LV_DPI_DEF) #define LV_TEXTAREA_DEF_HEIGHT (1 * LV_DPI_DEF) #define LV_TEXTAREA_PWD_BULLET_UNICODE 0x2022 /********************** * TYPEDEFS **********************/ /********************** * STATIC PROTOTYPES **********************/ static void lv_textarea_constructor(lv_obj_t * obj, lv_obj_t * parent, const lv_obj_t * copy); static void lv_textarea_destructor(lv_obj_t * obj); static lv_draw_res_t lv_textarea_draw(lv_obj_t * obj, const lv_area_t * clip_area, lv_draw_mode_t mode); static lv_res_t lv_textarea_signal(lv_obj_t * obj, lv_signal_t sign, void * param); static void cursor_blink_anim_cb(lv_obj_t * obj, lv_anim_value_t show); static void pwd_char_hider_anim(lv_obj_t * obj, lv_anim_value_t x); static void pwd_char_hider_anim_ready(lv_anim_t * a); static void pwd_char_hider(lv_obj_t * obj); static bool char_is_accepted(lv_obj_t * obj, uint32_t c); static void start_cursor_blink(lv_obj_t * obj); static void refr_cursor_area(lv_obj_t * obj); static void update_cursor_position_on_click(lv_obj_t * obj, lv_signal_t sign, lv_indev_t * click_source); static lv_res_t insert_handler(lv_obj_t * obj, char * txt); static void draw_placeholder(lv_obj_t * obj, const lv_area_t * clip_area); static void draw_cursor(lv_obj_t * obj, const lv_area_t * clip_area); /********************** * STATIC VARIABLES **********************/ const lv_obj_class_t lv_textarea = { .constructor_cb = lv_textarea_constructor, .destructor_cb = lv_textarea_destructor, .signal_cb = lv_textarea_signal, .draw_cb = lv_textarea_draw, .editable = LV_OBJ_CLASS_EDITABLE_TRUE, .instance_size = sizeof(lv_textarea_t), .base_class = &lv_obj }; static const char * ta_insert_replace; /********************** * MACROS **********************/ /********************** * GLOBAL FUNCTIONS **********************/ /** * Create a text area objects * @param par pointer to an object, it will be the parent of the new text area * @param copy pointer to a text area object, if not NULL then the new object will be copied from it * @return pointer to the created text area */ lv_obj_t * lv_textarea_create(lv_obj_t * parent, const lv_obj_t * copy) { return lv_obj_create_from_class(&lv_textarea, parent, copy); } /*====================== * Add/remove functions *=====================*/ /** * Insert a character to the current cursor position. * To add a wide char, e.g. 'Á' use `_lv_txt_encoded_conv_wc('Á')` * @param ta pointer to a text area object * @param c a character (e.g. 'a') */ void lv_textarea_add_char(lv_obj_t * obj, uint32_t c) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; const char * letter_buf; uint32_t u32_buf[2]; u32_buf[0] = c; u32_buf[1] = 0; letter_buf = (char *)&u32_buf; #if LV_BIG_ENDIAN_SYSTEM if(c != 0) while(*letter_buf == 0) ++letter_buf; #endif lv_res_t res = insert_handler(obj, letter_buf); if(res != LV_RES_OK) return; if(ta->one_line && (c == '\n' || c == '\r')) { LV_LOG_INFO("Text area: line break ignored in one-line mode"); return; } uint32_t c_uni = _lv_txt_encoded_next((const char *)&c, NULL); if(char_is_accepted(obj, c_uni) == false) { LV_LOG_INFO("Character is no accepted by the text area (too long text or not in the " "accepted list)"); return; } if(ta->pwd_mode != 0) pwd_char_hider(obj); /*Make sure all the current text contains only '*'*/ /*If the textarea is empty, invalidate it to hide the placeholder*/ if(ta->placeholder_txt) { const char * txt = lv_label_get_text(ta->label); if(txt[0] == '\0') lv_obj_invalidate(obj); } lv_label_ins_text(ta->label, ta->cursor.pos, letter_buf); /*Insert the character*/ lv_textarea_clear_selection(obj); /*Clear selection*/ if(ta->pwd_mode != 0) { ta->pwd_tmp = lv_mem_realloc(ta->pwd_tmp, strlen(ta->pwd_tmp) + strlen(letter_buf) + 1); /*+2: the new char + \0 */ LV_ASSERT_MEM(ta->pwd_tmp); if(ta->pwd_tmp == NULL) return; _lv_txt_ins(ta->pwd_tmp, ta->cursor.pos, (const char *)letter_buf); /*Auto hide characters*/ if(ta->pwd_show_time == 0) { pwd_char_hider(obj); } else { lv_anim_path_t path; lv_anim_path_init(&path); lv_anim_path_set_cb(&path, lv_anim_path_step); lv_anim_t a; lv_anim_init(&a); lv_anim_set_var(&a, ta); lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)pwd_char_hider_anim); lv_anim_set_time(&a, ta->pwd_show_time); lv_anim_set_values(&a, 0, 1); lv_anim_set_path(&a, &path); lv_anim_set_ready_cb(&a, pwd_char_hider_anim_ready); lv_anim_start(&a); } } /*Move the cursor after the new character*/ lv_textarea_set_cursor_pos(obj, lv_textarea_get_cursor_pos(obj) + 1); lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL); } /** * Insert a text to the current cursor position * @param ta pointer to a text area object * @param txt a '\0' terminated string to insert */ void lv_textarea_add_text(lv_obj_t * obj, const char * txt) { LV_ASSERT_OBJ(obj, MY_CLASS); LV_ASSERT_NULL(txt); lv_textarea_t * ta = (lv_textarea_t *) obj; if(ta->pwd_mode != 0) pwd_char_hider(obj); /*Make sure all the current text contains only '*'*/ /*Add the character one-by-one if not all characters are accepted or there is character limit.*/ if(lv_textarea_get_accepted_chars(obj) || lv_textarea_get_max_length(obj)) { uint32_t i = 0; while(txt[i] != '\0') { uint32_t c = _lv_txt_encoded_next(txt, &i); lv_textarea_add_char(obj, _lv_txt_unicode_to_encoded(c)); } return; } lv_res_t res = insert_handler(obj, txt); if(res != LV_RES_OK) return; /*If the textarea is empty, invalidate it to hide the placeholder*/ if(ta->placeholder_txt) { const char * txt_act = lv_label_get_text(ta->label); if(txt_act[0] == '\0') lv_obj_invalidate(obj); } /*Insert the text*/ lv_label_ins_text(ta->label, ta->cursor.pos, txt); lv_textarea_clear_selection(obj); if(ta->pwd_mode != 0) { ta->pwd_tmp = lv_mem_realloc(ta->pwd_tmp, strlen(ta->pwd_tmp) + strlen(txt) + 1); LV_ASSERT_MEM(ta->pwd_tmp); if(ta->pwd_tmp == NULL) return; _lv_txt_ins(ta->pwd_tmp, ta->cursor.pos, txt); /*Auto hide characters*/ if(ta->pwd_show_time == 0) { pwd_char_hider(obj); } else { lv_anim_path_t path; lv_anim_path_init(&path); lv_anim_path_set_cb(&path, lv_anim_path_step); lv_anim_t a; lv_anim_init(&a); lv_anim_set_var(&a, ta); lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)pwd_char_hider_anim); lv_anim_set_time(&a, ta->pwd_show_time); lv_anim_set_values(&a, 0, 1); lv_anim_set_path(&a, &path); lv_anim_set_ready_cb(&a, pwd_char_hider_anim_ready); lv_anim_start(&a); } } /*Move the cursor after the new text*/ lv_textarea_set_cursor_pos(obj, lv_textarea_get_cursor_pos(obj) + _lv_txt_get_encoded_length(txt)); lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL); } /** * Delete a the left character from the current cursor position * @param ta pointer to a text area object */ void lv_textarea_del_char(lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; uint32_t cur_pos = ta->cursor.pos; if(cur_pos == 0) return; char del_buf[2] = {LV_KEY_DEL, '\0'}; lv_res_t res = insert_handler(obj, del_buf); if(res != LV_RES_OK) return; char * label_txt = lv_label_get_text(ta->label); /*Delete a character*/ _lv_txt_cut(label_txt, ta->cursor.pos - 1, 1); /*Refresh the label*/ lv_label_set_text(ta->label, label_txt); lv_textarea_clear_selection(obj); /*If the textarea became empty, invalidate it to hide the placeholder*/ if(ta->placeholder_txt) { const char * txt = lv_label_get_text(ta->label); if(txt[0] == '\0') lv_obj_invalidate(obj); } if(ta->pwd_mode != 0) { uint32_t byte_pos = _lv_txt_encoded_get_byte_id(ta->pwd_tmp, ta->cursor.pos - 1); _lv_txt_cut(ta->pwd_tmp, ta->cursor.pos - 1, _lv_txt_encoded_size(&ta->pwd_tmp[byte_pos])); ta->pwd_tmp = lv_mem_realloc(ta->pwd_tmp, strlen(ta->pwd_tmp) + 1); LV_ASSERT_MEM(ta->pwd_tmp); if(ta->pwd_tmp == NULL) return; } /*Move the cursor to the place of the deleted character*/ lv_textarea_set_cursor_pos(obj, ta->cursor.pos - 1); lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL); } /** * Delete the right character from the current cursor position * @param ta pointer to a text area object */ void lv_textarea_del_char_forward(lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); uint32_t cp = lv_textarea_get_cursor_pos(obj); lv_textarea_set_cursor_pos(obj, cp + 1); if(cp != lv_textarea_get_cursor_pos(obj)) lv_textarea_del_char(obj); } /*===================== * Setter functions *====================*/ /** * Set the text of a text area * @param ta pointer to a text area * @param txt pointer to the text */ void lv_textarea_set_text(lv_obj_t * obj, const char * txt) { LV_ASSERT_OBJ(obj, MY_CLASS); LV_ASSERT_NULL(txt); lv_textarea_t * ta = (lv_textarea_t *) obj; /*Clear the existing selection*/ lv_textarea_clear_selection(obj); /*Add the character one-by-one if not all characters are accepted or there is character limit.*/ if(lv_textarea_get_accepted_chars(obj) || lv_textarea_get_max_length(obj)) { lv_label_set_text(ta->label, ""); lv_textarea_set_cursor_pos(obj, LV_TEXTAREA_CURSOR_LAST); if(ta->pwd_mode != 0) { ta->pwd_tmp[0] = '\0'; /*Clear the password too*/ } uint32_t i = 0; while(txt[i] != '\0') { uint32_t c = _lv_txt_encoded_next(txt, &i); lv_textarea_add_char(obj, _lv_txt_unicode_to_encoded(c)); } } else { lv_label_set_text(ta->label, txt); lv_textarea_set_cursor_pos(obj, LV_TEXTAREA_CURSOR_LAST); } /*If the textarea is empty, invalidate it to hide the placeholder*/ if(ta->placeholder_txt) { const char * txt_act = lv_label_get_text(ta->label); if(txt_act[0] == '\0') lv_obj_invalidate(obj); } if(ta->pwd_mode != 0) { ta->pwd_tmp = lv_mem_realloc(ta->pwd_tmp, strlen(txt) + 1); LV_ASSERT_MEM(ta->pwd_tmp); if(ta->pwd_tmp == NULL) return; strcpy(ta->pwd_tmp, txt); /*Auto hide characters*/ if(ta->pwd_show_time == 0) { pwd_char_hider(obj); } else { lv_anim_path_t path; lv_anim_path_init(&path); lv_anim_path_set_cb(&path, lv_anim_path_step); lv_anim_t a; lv_anim_init(&a); lv_anim_set_var(&a, ta); lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)pwd_char_hider_anim); lv_anim_set_time(&a, ta->pwd_show_time); lv_anim_set_values(&a, 0, 1); lv_anim_set_path(&a, &path); lv_anim_set_ready_cb(&a, pwd_char_hider_anim_ready); lv_anim_start(&a); } } lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL); } /** * Set the placeholder_txt text of a text area * @param ta pointer to a text area * @param txt pointer to the text */ void lv_textarea_set_placeholder_text(lv_obj_t * obj, const char * txt) { LV_ASSERT_OBJ(obj, MY_CLASS); LV_ASSERT_NULL(txt); lv_textarea_t * ta = (lv_textarea_t *) obj; size_t txt_len = strlen(txt); if(txt_len == 0) { if(ta->placeholder_txt) { lv_mem_free(ta->placeholder_txt); ta->placeholder_txt = NULL; } } else { /*Allocate memory for the placeholder_txt text*/ if(ta->placeholder_txt == NULL) { ta->placeholder_txt = lv_mem_alloc(txt_len + 1); } else { ta->placeholder_txt = lv_mem_realloc(ta->placeholder_txt, txt_len + 1); } LV_ASSERT_MEM(ta->placeholder_txt); if(ta->placeholder_txt == NULL) { LV_LOG_ERROR("lv_textarea_set_placeholder_text: couldn't allocate memory for placeholder"); return; } strcpy(ta->placeholder_txt, txt); } lv_obj_invalidate(obj); } /** * Set the cursor position * @param obj pointer to a text area object * @param pos the new cursor position in character index * < 0 : index from the end of the text * LV_TEXTAREA_CURSOR_LAST: go after the last character */ void lv_textarea_set_cursor_pos(lv_obj_t * obj, int32_t pos) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; if((uint32_t)ta->cursor.pos == (uint32_t)pos) return; uint32_t len = _lv_txt_get_encoded_length(lv_label_get_text(ta->label)); if(pos < 0) pos = len + pos; if(pos > (int32_t)len || pos == LV_TEXTAREA_CURSOR_LAST) pos = len; ta->cursor.pos = pos; /*Position the label to make the cursor show*/ lv_point_t cur_pos; const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN); lv_area_t label_cords; lv_area_t ta_cords; lv_label_get_letter_pos(ta->label, pos, &cur_pos); lv_obj_get_coords(obj, &ta_cords); lv_obj_get_coords(ta->label, &label_cords); /*Check the top*/ lv_coord_t font_h = lv_font_get_line_height(font); if(cur_pos.y < lv_obj_get_scroll_top(obj)) { lv_obj_scroll_to_y(obj, cur_pos.y, LV_ANIM_ON); } /*Check the bottom*/ lv_coord_t h = lv_obj_get_height_fit(obj); if(cur_pos.y + font_h - lv_obj_get_scroll_top(obj) > h) { lv_obj_scroll_to_y(obj, cur_pos.y - h + font_h, LV_ANIM_ON); } ta->cursor.valid_x = cur_pos.x; start_cursor_blink(obj); refr_cursor_area(obj); } /** * Enable/Disable the positioning of the cursor by clicking the text on the text area. * @param ta pointer to a text area object * @param en true: enable click positions; false: disable */ void lv_textarea_set_cursor_click_pos(lv_obj_t * obj, bool en) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; ta->cursor.click_pos = en ? 1 : 0; } /** * Enable/Disable password mode * @param ta pointer to a text area object * @param en true: enable, false: disable */ void lv_textarea_set_pwd_mode(lv_obj_t * obj, bool en) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; if(ta->pwd_mode == en) return; ta->pwd_mode = en == false ? 0 : 1; /*Pwd mode is now enabled*/ if(en != false) { char * txt = lv_label_get_text(ta->label); size_t len = strlen(txt); ta->pwd_tmp = lv_mem_alloc(len + 1); LV_ASSERT_MEM(ta->pwd_tmp); if(ta->pwd_tmp == NULL) return; strcpy(ta->pwd_tmp, txt); pwd_char_hider(obj); lv_textarea_clear_selection(obj); } /*Pwd mode is now disabled*/ else { lv_textarea_clear_selection(obj); lv_label_set_text(ta->label, ta->pwd_tmp); lv_mem_free(ta->pwd_tmp); ta->pwd_tmp = NULL; } refr_cursor_area(obj); } /** * Configure the text area to one line or back to normal * @param ta pointer to a Text area object * @param en true: one line, false: normal */ void lv_textarea_set_one_line(lv_obj_t * obj, bool en) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; if(ta->one_line == en) return; if(en) { const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN); lv_coord_t font_h = lv_font_get_line_height(font); ta->one_line = 1; lv_obj_set_content_height(obj, font_h); lv_label_set_long_mode(ta->label, LV_LABEL_LONG_EXPAND); lv_obj_scroll_to(obj, 0, 0, LV_ANIM_OFF); } else { ta->one_line = 0; lv_label_set_long_mode(ta->label, LV_LABEL_LONG_WRAP); lv_obj_set_height(obj, LV_TEXTAREA_DEF_HEIGHT); lv_obj_scroll_to(obj, 0, 0, LV_ANIM_OFF); } } /** * Set a list of characters. Only these characters will be accepted by the text area * @param ta pointer to Text Area * @param list list of characters. Only the pointer is saved. E.g. "+-.,0123456789" */ void lv_textarea_set_accepted_chars(lv_obj_t * obj, const char * list) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; ta->accepted_chars = list; } /** * Set max length of a Text Area. * @param ta pointer to Text Area * @param num the maximal number of characters can be added (`lv_textarea_set_text` ignores it) */ void lv_textarea_set_max_length(lv_obj_t * obj, uint32_t num) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; ta->max_length = num; } /** * In `LV_EVENT_INSERT` the text which planned to be inserted can be replaced by an other text. * It can be used to add automatic formatting to the text area. * @param ta pointer to a text area. * @param txt pointer to a new string to insert. If `""` no text will be added. * The variable must be live after the `event_cb` exists. (Should be `global` or * `static`) */ void lv_textarea_set_insert_replace(lv_obj_t * obj, const char * txt) { LV_ASSERT_OBJ(obj, MY_CLASS); LV_UNUSED(obj); ta_insert_replace = txt; } /** * Enable/disable selection mode. * @param ta pointer to a text area object * @param en true or false to enable/disable selection mode */ void lv_textarea_set_text_sel(lv_obj_t * obj, bool en) { LV_ASSERT_OBJ(obj, MY_CLASS); #if LV_LABEL_TEXT_SEL lv_textarea_t * ta = (lv_textarea_t *) obj; ta->text_sel_en = en; if(!en) lv_textarea_clear_selection(obj); #else (void)ta; /*Unused*/ (void)en; /*Unused*/ #endif } /** * Set how long show the password before changing it to '*' * @param ta pointer to Text area * @param time show time in milliseconds. 0: hide immediately. */ void lv_textarea_set_pwd_show_time(lv_obj_t * obj, uint16_t time) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; ta->pwd_show_time = time; } /*===================== * Getter functions *====================*/ /** * Get the text of a text area. In password mode it gives the real text (not '*'s). * @param ta pointer to a text area object * @return pointer to the text */ const char * lv_textarea_get_text(const lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; const char * txt; if(ta->pwd_mode == 0) { txt = lv_label_get_text(ta->label); } else { txt = ta->pwd_tmp; } return txt; } /** * Get the placeholder_txt text of a text area * @param ta pointer to a text area object * @return pointer to the text */ const char * lv_textarea_get_placeholder_text(lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; if(ta->placeholder_txt) return ta->placeholder_txt; else return ""; } /** * Get the label of a text area * @param ta pointer to a text area object * @return pointer to the label object */ lv_obj_t * lv_textarea_get_label(const lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; return ta->label; } /** * Get the current cursor position in character index * @param ta pointer to a text area object * @return the cursor position */ uint32_t lv_textarea_get_cursor_pos(const lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; return ta->cursor.pos; } /** * Get whether the cursor click positioning is enabled or not. * @param ta pointer to a text area object * @return true: enable click positions; false: disable */ bool lv_textarea_get_cursor_click_pos(lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; return ta->cursor.click_pos ? true : false; } /** * Get the password mode attribute * @param ta pointer to a text area object * @return true: password mode is enabled, false: disabled */ bool lv_textarea_get_pwd_mode(const lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; return ta->pwd_mode == 0 ? false : true; } /** * Get the one line configuration attribute * @param ta pointer to a text area object * @return true: one line configuration is enabled, false: disabled */ bool lv_textarea_get_one_line(const lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; return ta->one_line == 0 ? false : true; } /** * Get a list of accepted characters. * @param ta pointer to Text Area * @return list of accented characters. */ const char * lv_textarea_get_accepted_chars(lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; return ta->accepted_chars; } /** * Set max length of a Text Area. * @param ta pointer to Text Area * @return the maximal number of characters to be add */ uint32_t lv_textarea_get_max_length(lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; return ta->max_length; } /** * Find whether text is selected or not. * @param ta Text area object * @return whether text is selected or not */ bool lv_textarea_text_is_selected(const lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); #if LV_LABEL_TEXT_SEL lv_textarea_t * ta = (lv_textarea_t *) obj; if((lv_label_get_text_sel_start(ta->label) == LV_DRAW_LABEL_NO_TXT_SEL || lv_label_get_text_sel_end(ta->label) == LV_DRAW_LABEL_NO_TXT_SEL)) { return true; } else { return false; } #else (void)ta; /*Unused*/ return false; #endif } /** * Find whether selection mode is enabled. * @param ta pointer to a text area object * @return true: selection mode is enabled, false: disabled */ bool lv_textarea_get_text_sel_en(lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); #if LV_LABEL_TEXT_SEL lv_textarea_t * ta = (lv_textarea_t *) obj; return ta->text_sel_en; #else (void)ta; /*Unused*/ return false; #endif } /** * Set how long show the password before changing it to '*' * @param ta pointer to Text area * @return show time in milliseconds. 0: hide immediately. */ uint16_t lv_textarea_get_pwd_show_time(lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; return ta->pwd_show_time; } /*===================== * Other functions *====================*/ /** * Clear the selection on the text area. * @param ta Text area object */ void lv_textarea_clear_selection(lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); #if LV_LABEL_TEXT_SEL lv_textarea_t * ta = (lv_textarea_t *) obj; if(lv_label_get_text_sel_start(ta->label) != LV_DRAW_LABEL_NO_TXT_SEL || lv_label_get_text_sel_end(ta->label) != LV_DRAW_LABEL_NO_TXT_SEL) { lv_label_set_text_sel_start(ta->label, LV_DRAW_LABEL_NO_TXT_SEL); lv_label_set_text_sel_end(ta->label, LV_DRAW_LABEL_NO_TXT_SEL); } #else (void)ta; /*Unused*/ #endif } /** * Move the cursor one character right * @param ta pointer to a text area object */ void lv_textarea_cursor_right(lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); uint32_t cp = lv_textarea_get_cursor_pos(obj); cp++; lv_textarea_set_cursor_pos(obj, cp); } /** * Move the cursor one character left * @param ta pointer to a text area object */ void lv_textarea_cursor_left(lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); uint32_t cp = lv_textarea_get_cursor_pos(obj); if(cp > 0) { cp--; lv_textarea_set_cursor_pos(obj, cp); } } /** * Move the cursor one line down * @param ta pointer to a text area object */ void lv_textarea_cursor_down(lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; lv_point_t pos; /*Get the position of the current letter*/ lv_label_get_letter_pos(ta->label, lv_textarea_get_cursor_pos(obj), &pos); /*Increment the y with one line and keep the valid x*/ lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN); const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN); lv_coord_t font_h = lv_font_get_line_height(font); pos.y += font_h + line_space + 1; pos.x = ta->cursor.valid_x; /*Do not go below the last line*/ if(pos.y < lv_obj_get_height(ta->label)) { /*Get the letter index on the new cursor position and set it*/ uint32_t new_cur_pos = lv_label_get_letter_on(ta->label, &pos); lv_coord_t cur_valid_x_tmp = ta->cursor.valid_x; /*Cursor position set overwrites the valid position */ lv_textarea_set_cursor_pos(obj, new_cur_pos); ta->cursor.valid_x = cur_valid_x_tmp; } } /** * Move the cursor one line up * @param ta pointer to a text area object */ void lv_textarea_cursor_up(lv_obj_t * obj) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_textarea_t * ta = (lv_textarea_t *) obj; lv_point_t pos; /*Get the position of the current letter*/ lv_label_get_letter_pos(ta->label, lv_textarea_get_cursor_pos(obj), &pos); /*Decrement the y with one line and keep the valid x*/ lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN); const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN); lv_coord_t font_h = lv_font_get_line_height(font); pos.y -= font_h + line_space - 1; pos.x = ta->cursor.valid_x; /*Get the letter index on the new cursor position and set it*/ uint32_t new_cur_pos = lv_label_get_letter_on(ta->label, &pos); lv_coord_t cur_valid_x_tmp = ta->cursor.valid_x; /*Cursor position set overwrites the valid position */ lv_textarea_set_cursor_pos(obj, new_cur_pos); ta->cursor.valid_x = cur_valid_x_tmp; } /********************** * STATIC FUNCTIONS **********************/ static void lv_textarea_constructor(lv_obj_t * obj, lv_obj_t * parent, const lv_obj_t * copy) { LV_LOG_TRACE("text area create started"); lv_textarea_t * ta = (lv_textarea_t *) obj; ta->pwd_mode = 0; ta->pwd_tmp = NULL; ta->pwd_show_time = LV_TEXTAREA_DEF_PWD_SHOW_TIME; ta->accepted_chars = NULL; ta->max_length = 0; ta->cursor.show = 1; ta->cursor.pos = 0; ta->cursor.click_pos = 1; ta->cursor.valid_x = 0; ta->one_line = 0; #if LV_LABEL_TEXT_SEL ta->text_sel_en = 0; #endif ta->label = NULL; ta->placeholder_txt = NULL; /*Init the new text area object*/ if(copy == NULL) { ta->label = lv_label_create(obj, NULL); lv_label_set_long_mode(ta->label, LV_LABEL_LONG_WRAP); lv_label_set_text(ta->label, ""); lv_obj_set_size(obj, LV_TEXTAREA_DEF_WIDTH, LV_TEXTAREA_DEF_HEIGHT); } /*Copy an existing object*/ else { lv_textarea_t * copy_ta = (lv_textarea_t *) copy; ta->label = lv_label_create(obj, copy_ta->label); ta->pwd_mode = copy_ta->pwd_mode; ta->accepted_chars = copy_ta->accepted_chars; ta->max_length = copy_ta->max_length; ta->cursor.pos = copy_ta->cursor.pos; ta->cursor.valid_x = copy_ta->cursor.valid_x; if(ta->pwd_mode != 0) pwd_char_hider(obj); if(copy_ta->placeholder_txt) { lv_textarea_set_placeholder_text(obj, copy_ta->placeholder_txt); } if(copy_ta->pwd_tmp) { uint32_t len = lv_mem_get_size(copy_ta->pwd_tmp); ta->pwd_tmp = lv_mem_alloc(len); LV_ASSERT_MEM(ta->pwd_tmp); if(ta->pwd_tmp == NULL) return; lv_memcpy(ta->pwd_tmp, copy_ta->pwd_tmp, len); } if(copy_ta->one_line) lv_textarea_set_one_line(obj, true); } start_cursor_blink(obj); LV_LOG_INFO("text area created"); } static void lv_textarea_destructor(lv_obj_t * obj) { // else if(sign == LV_SIGNAL_CLEANUP) { // if(ta->pwd_tmp != NULL) lv_mem_free(ta->pwd_tmp); // if(ta->placeholder_txt != NULL) lv_mem_free(ta->placeholder_txt); // // ta->pwd_tmp = NULL; // ta->placeholder_txt = NULL; // // _lv_obj_reset_style_list_no_refr(obj, LV_PART_MARKER); // _lv_obj_reset_style_list_no_refr(obj, LV_TEXTAREA_PART_PLACEHOLDER); // // /* (The created label will be deleted automatically) */ } static lv_draw_res_t lv_textarea_draw(lv_obj_t * obj, const lv_area_t * clip_area, lv_draw_mode_t mode) { if(mode == LV_DRAW_MODE_COVER_CHECK) { /*Return false if the object is not covers the mask_p area*/ return lv_obj_draw_base(MY_CLASS, obj, clip_area, mode); } else if(mode == LV_DRAW_MODE_MAIN_DRAW) { /*Draw the object*/ lv_obj_draw_base(MY_CLASS, obj, clip_area, mode); draw_placeholder(obj, clip_area); } else if(mode == LV_DRAW_MODE_POST_DRAW) { lv_obj_draw_base(MY_CLASS, obj, clip_area, mode); draw_cursor(obj, clip_area); } return LV_DRAW_RES_OK; } /** * Signal function of the text area * @param ta pointer to 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_textarea_signal(lv_obj_t * obj, lv_signal_t sign, void * param) { lv_res_t res; /* Include the ancient signal function */ res = lv_obj_signal_base(MY_CLASS, obj, sign, param); if(res != LV_RES_OK) return res; lv_textarea_t * ta = (lv_textarea_t *) obj; if(sign == LV_SIGNAL_STYLE_CHG) { if(ta->label) { if(ta->one_line) { lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN); lv_coord_t bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN); const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN); /*In one line mode refresh the Text Area height because 'vpad' can modify it*/ lv_coord_t font_h = lv_font_get_line_height(font); lv_obj_set_height(ta->label, font_h); lv_obj_set_height(obj, font_h + top + bottom); } else { /*In not one line mode refresh the Label width because 'hpad' can modify it*/ lv_obj_set_width(ta->label, lv_obj_get_width_fit(obj)); lv_obj_set_pos(ta->label, 0, 0); /*Be sure the Label is in the correct position*/ } lv_label_set_text(ta->label, NULL); refr_cursor_area(obj); start_cursor_blink(obj); } } else if(sign == LV_SIGNAL_FOCUS) { start_cursor_blink(obj); } else if(sign == LV_SIGNAL_COORD_CHG) { /*Set the label width according to the text area width*/ if(ta->label) { if(lv_obj_get_width(obj) != lv_area_get_width(param) || lv_obj_get_height(obj) != lv_area_get_height(param)) { lv_obj_set_width(ta->label, lv_obj_get_width_fit(obj)); lv_obj_set_pos(ta->label, 0, 0); lv_label_set_text(ta->label, NULL); /*Refresh the label*/ refr_cursor_area(obj); } } } else if(sign == LV_SIGNAL_CONTROL) { uint32_t c = *((uint32_t *)param); /*uint32_t because can be UTF-8*/ if(c == LV_KEY_RIGHT) lv_textarea_cursor_right(obj); else if(c == LV_KEY_LEFT) lv_textarea_cursor_left(obj); else if(c == LV_KEY_UP) lv_textarea_cursor_up(obj); else if(c == LV_KEY_DOWN) lv_textarea_cursor_down(obj); else if(c == LV_KEY_BACKSPACE) lv_textarea_del_char(obj); else if(c == LV_KEY_DEL) lv_textarea_del_char_forward(obj); else if(c == LV_KEY_HOME) lv_textarea_set_cursor_pos(obj, 0); else if(c == LV_KEY_END) lv_textarea_set_cursor_pos(obj, LV_TEXTAREA_CURSOR_LAST); else if(c == LV_KEY_ENTER && lv_textarea_get_one_line(obj)) lv_event_send(obj, LV_EVENT_READY, NULL); else { lv_textarea_add_char(obj, c); } } else if(sign == LV_SIGNAL_PRESSED || sign == LV_SIGNAL_PRESSING || sign == LV_SIGNAL_PRESS_LOST || sign == LV_SIGNAL_RELEASED) { update_cursor_position_on_click(obj, sign, (lv_indev_t *)param); } return res; } /** * Called to blink the cursor * @param ta pointer to a text area * @param hide 1: hide the cursor, 0: show it */ static void cursor_blink_anim_cb(lv_obj_t * obj, lv_anim_value_t show) { lv_textarea_t * ta = (lv_textarea_t *) obj; if(show != ta->cursor.show) { ta->cursor.show = show == 0 ? 0 : 1; lv_area_t area_tmp; lv_area_copy(&area_tmp, &ta->cursor.area); area_tmp.x1 += ta->label->coords.x1; area_tmp.y1 += ta->label->coords.y1; area_tmp.x2 += ta->label->coords.x1; area_tmp.y2 += ta->label->coords.y1; lv_obj_invalidate_area(obj, &area_tmp); } } /** * Dummy function to animate char hiding in pwd mode. * Does nothing, but a function is required in car hiding anim. * (pwd_char_hider callback do the real job) * @param ta unused * @param x unused */ static void pwd_char_hider_anim(lv_obj_t * obj, lv_anim_value_t x) { LV_UNUSED(obj); LV_UNUSED(x); } /** * Call when an animation is ready to convert all characters to '*' * @param a pointer to the animation */ static void pwd_char_hider_anim_ready(lv_anim_t * a) { lv_obj_t * obj = a->var; pwd_char_hider(obj); } /** * Hide all characters (convert them to '*') * @param ta: pointer to text area object */ static void pwd_char_hider(lv_obj_t * obj) { lv_textarea_t * ta = (lv_textarea_t *) obj; if(ta->pwd_mode != 0) { char * txt = lv_label_get_text(ta->label); int32_t enc_len = _lv_txt_get_encoded_length(txt); if(enc_len == 0) return; /*If the textarea's font has "bullet" character use it else fallback to "*"*/ const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN); lv_font_glyph_dsc_t g; bool has_bullet; has_bullet = lv_font_get_glyph_dsc(font, &g, LV_TEXTAREA_PWD_BULLET_UNICODE, 0); const char * bullet; if(has_bullet) bullet = LV_SYMBOL_BULLET; else bullet = "*"; size_t bullet_len = strlen(bullet); char * txt_tmp = lv_mem_buf_get(enc_len * bullet_len + 1); int32_t i; for(i = 0; i < enc_len; i++) { lv_memcpy(&txt_tmp[i * bullet_len], bullet, bullet_len); } txt_tmp[i * bullet_len] = '\0'; lv_label_set_text(ta->label, txt_tmp); lv_mem_buf_release(txt_tmp); refr_cursor_area(obj); } } /** * Test an unicode character if it is accepted or not. Checks max length and accepted char list. * @param ta pointer to a test area object * @param c an unicode character * @return true: accepted; false: rejected */ static bool char_is_accepted(lv_obj_t * obj, uint32_t c) { lv_textarea_t * ta = (lv_textarea_t *) obj; /*If no restriction accept it*/ if(ta->accepted_chars == NULL && ta->max_length == 0) return true; /*Too many characters?*/ if(ta->max_length > 0 && _lv_txt_get_encoded_length(lv_textarea_get_text(obj)) >= ta->max_length) { return false; } /*Accepted character?*/ if(ta->accepted_chars) { uint32_t i = 0; while(ta->accepted_chars[i] != '\0') { uint32_t a = _lv_txt_encoded_next(ta->accepted_chars, &i); if(a == c) return true; /*Accepted*/ } return false; /*The character wasn't in the list*/ } else { return true; /*If the accepted char list in not specified the accept the character*/ } } static void start_cursor_blink(lv_obj_t * obj) { lv_textarea_t * ta = (lv_textarea_t *) obj; uint32_t blink_time = lv_obj_get_style_anim_time(obj, LV_PART_MARKER); if(blink_time == 0) { lv_anim_del(obj, (lv_anim_exec_xcb_t)cursor_blink_anim_cb); ta->cursor.show = 1; } else { lv_anim_path_t path; lv_anim_path_init(&path); lv_anim_path_set_cb(&path, lv_anim_path_step); lv_anim_t a; lv_anim_init(&a); lv_anim_set_var(&a, ta); lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)cursor_blink_anim_cb); lv_anim_set_time(&a, blink_time); lv_anim_set_playback_time(&a, blink_time); lv_anim_set_values(&a, 1, 0); lv_anim_set_path(&a, &path); lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE); lv_anim_start(&a); } } static void refr_cursor_area(lv_obj_t * obj) { lv_textarea_t * ta = (lv_textarea_t *) obj; const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN); lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN); uint32_t cur_pos = lv_textarea_get_cursor_pos(obj); const char * txt = lv_label_get_text(ta->label); uint32_t byte_pos; byte_pos = _lv_txt_encoded_get_byte_id(txt, cur_pos); uint32_t letter = _lv_txt_encoded_next(&txt[byte_pos], NULL); lv_coord_t letter_h = lv_font_get_line_height(font); /*Set letter_w (set not 0 on non printable but valid chars)*/ lv_coord_t letter_w; if(letter == '\0' || letter == '\n' || letter == '\r') { letter_w = lv_font_get_glyph_width(font, ' ', '\0'); } else { /*`letter_next` parameter is '\0' to ignore kerning*/ letter_w = lv_font_get_glyph_width(font, letter, '\0'); } lv_point_t letter_pos; lv_label_get_letter_pos(ta->label, cur_pos, &letter_pos); /*If the cursor is out of the text (most right) draw it to the next line*/ if(letter_pos.x + ta->label->coords.x1 + letter_w > ta->label->coords.x2 && ta->one_line == 0 && lv_obj_get_style_text_align(ta->label, LV_PART_MAIN) != LV_TEXT_ALIGN_RIGHT) { letter_pos.x = 0; letter_pos.y += letter_h + line_space; if(letter != '\0') { byte_pos += _lv_txt_encoded_size(&txt[byte_pos]); letter = _lv_txt_encoded_next(&txt[byte_pos], NULL); } if(letter == '\0' || letter == '\n' || letter == '\r') { letter_w = lv_font_get_glyph_width(font, ' ', '\0'); } else { letter_w = lv_font_get_glyph_width(font, letter, '\0'); } } /*Save the byte position. It is required to draw `LV_CURSOR_BLOCK`*/ ta->cursor.txt_byte_pos = byte_pos; /*Calculate the cursor according to its type*/ lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_MARKER); lv_coord_t bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MARKER); lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_MARKER); lv_coord_t right = lv_obj_get_style_pad_right(obj, LV_PART_MARKER); lv_area_t cur_area; cur_area.x1 = letter_pos.x - left; cur_area.y1 = letter_pos.y - top; cur_area.x2 = letter_pos.x + right + letter_w; cur_area.y2 = letter_pos.y + bottom + letter_h; /*Save the new area*/ lv_area_t area_tmp; lv_area_copy(&area_tmp, &ta->cursor.area); area_tmp.x1 += ta->label->coords.x1; area_tmp.y1 += ta->label->coords.y1; area_tmp.x2 += ta->label->coords.x1; area_tmp.y2 += ta->label->coords.y1; lv_obj_invalidate_area(obj, &area_tmp); lv_area_copy(&ta->cursor.area, &cur_area); lv_area_copy(&area_tmp, &ta->cursor.area); area_tmp.x1 += ta->label->coords.x1; area_tmp.y1 += ta->label->coords.y1; area_tmp.x2 += ta->label->coords.x1; area_tmp.y2 += ta->label->coords.y1; lv_obj_invalidate_area(obj, &area_tmp); } static void update_cursor_position_on_click(lv_obj_t * obj, lv_signal_t sign, lv_indev_t * click_source) { if(click_source == NULL) return; lv_textarea_t * ta = (lv_textarea_t *) obj; if(ta->cursor.click_pos == 0) return; if(lv_indev_get_type(click_source) == LV_INDEV_TYPE_KEYPAD || lv_indev_get_type(click_source) == LV_INDEV_TYPE_ENCODER) { return; } lv_area_t label_coords; lv_obj_get_coords(ta->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*/ lv_point_t rel_pos; rel_pos.x = point_act.x - label_coords.x1; rel_pos.y = point_act.y - label_coords.y1; lv_coord_t label_width = lv_obj_get_width(ta->label); uint16_t char_id_at_click; #if LV_LABEL_TEXT_SEL lv_label_t * label_data = (lv_label_t *) ta->label; bool click_outside_label; /*Check if the click happened on the left side of the area outside the label*/ if(rel_pos.x < 0) { char_id_at_click = 0; click_outside_label = true; } /*Check if the click happened on the right side of the area outside the label*/ else if(rel_pos.x >= label_width) { char_id_at_click = LV_TEXTAREA_CURSOR_LAST; click_outside_label = true; } else { char_id_at_click = lv_label_get_letter_on(ta->label, &rel_pos); click_outside_label = !lv_label_is_char_under_pos(ta->label, &rel_pos); } if(ta->text_sel_en) { if(!ta->text_sel_in_prog && !click_outside_label && sign == LV_SIGNAL_PRESSED) { /*Input device just went down. Store the selection start position*/ ta->sel_start = char_id_at_click; ta->sel_end = LV_LABEL_TEXT_SEL_OFF; ta->text_sel_in_prog = 1; lv_obj_clear_flag(obj, LV_OBJ_FLAG_SCROLL_CHAIN); } else if(ta->text_sel_in_prog && sign == LV_SIGNAL_PRESSING) { /*Input device may be moving. Store the end position */ ta->sel_end = char_id_at_click; } else if(ta->text_sel_in_prog && (sign == LV_SIGNAL_PRESS_LOST || sign == LV_SIGNAL_RELEASED)) { /*Input device is released. Check if anything was selected.*/ lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_CHAIN); } } if(ta->text_sel_in_prog || sign == LV_SIGNAL_PRESSED) lv_textarea_set_cursor_pos(obj, char_id_at_click); if(ta->text_sel_in_prog) { /*If the selected area has changed then update the real values and*/ /*Invalidate the text area.*/ if(ta->sel_start > ta->sel_end) { if(label_data->sel_start != ta->sel_end || label_data->sel_end != ta->sel_start) { label_data->sel_start = ta->sel_end; label_data->sel_end = ta->sel_start; lv_obj_invalidate(obj); } } else if(ta->sel_start < ta->sel_end) { if(label_data->sel_start != ta->sel_start || label_data->sel_end != ta->sel_end) { label_data->sel_start = ta->sel_start; label_data->sel_end = ta->sel_end; lv_obj_invalidate(obj); } } else { if(label_data->sel_start != LV_DRAW_LABEL_NO_TXT_SEL || label_data->sel_end != LV_DRAW_LABEL_NO_TXT_SEL) { label_data->sel_start = LV_DRAW_LABEL_NO_TXT_SEL; label_data->sel_end = LV_DRAW_LABEL_NO_TXT_SEL; lv_obj_invalidate(obj); } } /*Finish selection if necessary */ if(sign == LV_SIGNAL_PRESS_LOST || sign == LV_SIGNAL_RELEASED) { ta->text_sel_in_prog = 0; } } #else /*Check if the click happened on the left side of the area outside the label*/ if(rel_pos.x < 0) { char_id_at_click = 0; } /*Check if the click happened on the right side of the area outside the label*/ else if(rel_pos.x >= label_width) { char_id_at_click = LV_TEXTAREA_CURSOR_LAST; } else { char_id_at_click = lv_label_get_letter_on(ta->label, &rel_pos); } if(sign == LV_SIGNAL_PRESSED) lv_textarea_set_cursor_pos(obj, char_id_at_click); #endif } static lv_res_t insert_handler(lv_obj_t * obj, char * txt) { ta_insert_replace = NULL; lv_event_send(obj, LV_EVENT_INSERT, txt); if(ta_insert_replace) { if(ta_insert_replace[0] == '\0') return LV_RES_INV; /*Drop this text*/ /*Add the replaced text directly it's different from the original*/ if(strcmp(ta_insert_replace, txt)) { lv_textarea_add_text(obj, ta_insert_replace); return LV_RES_INV; } } return LV_RES_OK; } static void draw_placeholder(lv_obj_t * obj, const lv_area_t * clip_area) { lv_textarea_t * ta = (lv_textarea_t *) obj; const char * txt = lv_label_get_text(ta->label); /*Draw the place holder*/ if(txt[0] == '\0' && ta->placeholder_txt && ta->placeholder_txt[0] != 0) { lv_draw_label_dsc_t ph_dsc; lv_draw_label_dsc_init(&ph_dsc); lv_obj_init_draw_label_dsc(obj, LV_PART_TEXTAREA_PLACEHOLDER, &ph_dsc); if(ta->one_line) ph_dsc.flag |= LV_TEXT_FLAG_EXPAND; lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN); lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN); lv_area_t ph_coords; lv_area_copy(&ph_coords, &obj->coords); ph_coords.x1 += left; ph_coords.x2 += left; ph_coords.y1 += top; ph_coords.y2 += top; lv_draw_label(&ph_coords, clip_area, &ph_dsc, ta->placeholder_txt, NULL); } } static void draw_cursor(lv_obj_t * obj, const lv_area_t * clip_area) { lv_textarea_t * ta = (lv_textarea_t *) obj; const char * txt = lv_label_get_text(ta->label); if(ta->cursor.show == 0) return; lv_draw_rect_dsc_t cur_dsc; lv_draw_rect_dsc_init(&cur_dsc); lv_obj_init_draw_rect_dsc(obj, LV_PART_MARKER, &cur_dsc); /*Draw he cursor according to the type*/ lv_area_t cur_area; lv_area_copy(&cur_area, &ta->cursor.area); cur_area.x1 += ta->label->coords.x1; cur_area.y1 += ta->label->coords.y1; cur_area.x2 += ta->label->coords.x1; cur_area.y2 += ta->label->coords.y1; lv_draw_rect(&cur_area, clip_area, &cur_dsc); char letter_buf[8] = {0}; lv_memcpy(letter_buf, &txt[ta->cursor.txt_byte_pos], _lv_txt_encoded_size(&txt[ta->cursor.txt_byte_pos])); if(cur_dsc.bg_opa == LV_OPA_COVER) { lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_MARKER); lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_MARKER); cur_area.x1 += left; cur_area.y1 += top; lv_draw_label_dsc_t cur_label_dsc; lv_draw_label_dsc_init(&cur_label_dsc); lv_obj_init_draw_label_dsc(obj, LV_PART_MARKER, &cur_label_dsc); lv_draw_label(&cur_area, clip_area, &cur_label_dsc, letter_buf, NULL); } } #endif