diff --git a/docs/src/widgets/textarea.rst b/docs/src/widgets/textarea.rst index 0866a824eb..a6f923b673 100644 --- a/docs/src/widgets/textarea.rst +++ b/docs/src/widgets/textarea.rst @@ -143,7 +143,8 @@ Accepted characters ------------------- You can set a list of accepted characters with -:cpp:expr:`lv_textarea_set_accepted_chars(textarea, list)` where ``list`` is a +:cpp:expr:`lv_textarea_set_accepted_chars(textarea, list)` or +:cpp:expr:`lv_textarea_set_accepted_chars_static(textarea, list)` where ``list`` is a pointer to a NUL-terminated string, or NULL to accept all characters. Characters entered not in this list will be ignored. diff --git a/src/widgets/textarea/lv_textarea.c b/src/widgets/textarea/lv_textarea.c index aa027aa4c7..c7a9d69d1b 100644 --- a/src/widgets/textarea/lv_textarea.c +++ b/src/widgets/textarea/lv_textarea.c @@ -559,9 +559,29 @@ void lv_textarea_set_accepted_chars(lv_obj_t * obj, const char * list) lv_textarea_t * ta = (lv_textarea_t *)obj; - ta->accepted_chars = list; + char * copied_list = NULL; + if(list) { + copied_list = lv_strdup(list); + LV_ASSERT_MALLOC(copied_list); + } + + if(!ta->static_accepted_chars) lv_free(ta->accepted_chars); + ta->static_accepted_chars = 0; + ta->accepted_chars = copied_list; } +void lv_textarea_set_accepted_chars_static(lv_obj_t * obj, const char * list) +{ + LV_ASSERT_OBJ(obj, MY_CLASS); + + lv_textarea_t * ta = (lv_textarea_t *)obj; + + if(!ta->static_accepted_chars) lv_free(ta->accepted_chars); + ta->static_accepted_chars = 1; + ta->accepted_chars = (char *)list; +} + + void lv_textarea_set_max_length(lv_obj_t * obj, uint32_t num) { LV_ASSERT_OBJ(obj, MY_CLASS); @@ -893,6 +913,7 @@ static void lv_textarea_constructor(const lv_obj_class_t * class_p, lv_obj_t * o ta->pwd_bullet = NULL; ta->pwd_show_time = LV_TEXTAREA_DEF_PWD_SHOW_TIME; ta->accepted_chars = NULL; + ta->static_accepted_chars = 1; ta->max_length = 0; ta->cursor.show = 1; /*It will be set to zero later (with zero value lv_textarea_set_cursor_pos(obj, 0); wouldn't do anything as there is no difference)*/ @@ -938,6 +959,8 @@ static void lv_textarea_destructor(const lv_obj_class_t * class_p, lv_obj_t * ob lv_free(ta->placeholder_txt); ta->placeholder_txt = NULL; } + if(!ta->static_accepted_chars) lv_free(ta->accepted_chars); + ta->accepted_chars = NULL; } static void lv_textarea_event(const lv_obj_class_t * class_p, lv_event_t * e) diff --git a/src/widgets/textarea/lv_textarea.h b/src/widgets/textarea/lv_textarea.h index 13c23e2fa1..24e57802bd 100644 --- a/src/widgets/textarea/lv_textarea.h +++ b/src/widgets/textarea/lv_textarea.h @@ -158,10 +158,17 @@ void lv_textarea_set_one_line(lv_obj_t * obj, bool en); /** * Set a list of characters. Only these characters will be accepted by the text area * @param obj pointer to a text area object - * @param list list of characters. Only the pointer is saved. E.g. "+-.,0123456789" + * @param list list of characters. A copy is saved. Example: "+-.,0123456789" */ void lv_textarea_set_accepted_chars(lv_obj_t * obj, const char * list); +/** + * Set a list of characters. Only these characters will be accepted by the text area + * @param obj pointer to a text area object + * @param list list of characters. Only the pointer is saved. Example: "+-.,0123456789" + */ +void lv_textarea_set_accepted_chars_static(lv_obj_t * obj, const char * list); + /** * Set max length of a Text Area. * @param obj pointer to a text area object diff --git a/src/widgets/textarea/lv_textarea_private.h b/src/widgets/textarea/lv_textarea_private.h index 68102e8caa..40a50cddaa 100644 --- a/src/widgets/textarea/lv_textarea_private.h +++ b/src/widgets/textarea/lv_textarea_private.h @@ -34,7 +34,7 @@ struct _lv_textarea_t { char * placeholder_txt; /**< Place holder label. only visible if text is an empty string */ char * pwd_tmp; /**< Used to store the original text in password mode */ char * pwd_bullet; /**< Replacement characters displayed in password mode */ - const char * accepted_chars; /**< Only these characters will be accepted. NULL: accept all */ + char * accepted_chars; /**< Only these characters will be accepted. NULL: accept all */ uint32_t max_length; /**< The max. number of characters. 0: no limit */ uint32_t pwd_show_time; /**< Time to show characters in password mode before change them to '*' */ struct { @@ -55,6 +55,7 @@ struct _lv_textarea_t { #endif uint8_t pwd_mode : 1; /**< Replace characters with '*' */ uint8_t one_line : 1; /**< One line mode (ignore line breaks) */ + uint8_t static_accepted_chars : 1; /**<1: Only a pointer is saved in `accepted_chars` */ };