From af5c3f12f6e9d8db2f7105b461d060615ca33f6d Mon Sep 17 00:00:00 2001 From: Niklas Fiekas Date: Mon, 19 Jan 2026 10:14:48 +0100 Subject: [PATCH] feat(dropdown): static and non-static setter for text --- docs/src/widgets/dropdown.rst | 5 +- src/widgets/dropdown/lv_dropdown.c | 53 ++++++++++++++------ src/widgets/dropdown/lv_dropdown.h | 13 ++++- src/widgets/dropdown/lv_dropdown_private.h | 5 +- tests/src/test_cases/widgets/test_dropdown.c | 6 +-- 5 files changed, 57 insertions(+), 25 deletions(-) diff --git a/docs/src/widgets/dropdown.rst b/docs/src/widgets/dropdown.rst index 207c5445db..5c85bac709 100644 --- a/docs/src/widgets/dropdown.rst +++ b/docs/src/widgets/dropdown.rst @@ -120,8 +120,9 @@ will be shown on the left, otherwise on the right. Show selected ------------- -The main part can either show the selected item or static text. If -static is set with :cpp:expr:`lv_dropdown_set_text(dropdown, "Some text")` it +The main part can either show the selected item or fixed text. If +fixed text is set with :cpp:expr:`lv_dropdown_set_text(dropdown, "Some text")` +or :cpp:expr:`lv_dropdown_set_text_static(dropdown, "Some text")` it will be shown regardless of the selected item. If the text is ``NULL`` the selected option is displayed on the button. diff --git a/src/widgets/dropdown/lv_dropdown.c b/src/widgets/dropdown/lv_dropdown.c index 6dea20ed3b..2c7ddb09b5 100644 --- a/src/widgets/dropdown/lv_dropdown.c +++ b/src/widgets/dropdown/lv_dropdown.c @@ -161,13 +161,32 @@ lv_obj_t * lv_dropdown_create(lv_obj_t * parent) * Setter functions *====================*/ -void lv_dropdown_set_text(lv_obj_t * obj, const char * txt) +void lv_dropdown_set_text(lv_obj_t * obj, const char * text) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_dropdown_t * dropdown = (lv_dropdown_t *)obj; - if(dropdown->text == txt) return; - dropdown->text = txt; + char * copied_text = NULL; + if(text) { + copied_text = lv_strdup(text); + LV_ASSERT_MALLOC(copied_text); + } + + if(!dropdown->static_text) lv_free(dropdown->text); + dropdown->static_text = 0; + dropdown->text = copied_text; + + lv_obj_invalidate(obj); +} + +void lv_dropdown_set_text_static(lv_obj_t * obj, const char * text) +{ + LV_ASSERT_OBJ(obj, MY_CLASS); + lv_dropdown_t * dropdown = (lv_dropdown_t *)obj; + + if(!dropdown->static_text) lv_free(dropdown->text); + dropdown->static_text = 1; + dropdown->text = (char *)text; lv_obj_invalidate(obj); } @@ -196,7 +215,7 @@ void lv_dropdown_set_options(lv_obj_t * obj, const char * options) size_t len = lv_text_ap_calc_bytes_count(options) + 1; #endif - if(dropdown->options != NULL && dropdown->static_txt == 0) { + if(dropdown->options != NULL && dropdown->static_options == 0) { lv_free(dropdown->options); dropdown->options = NULL; } @@ -213,7 +232,7 @@ void lv_dropdown_set_options(lv_obj_t * obj, const char * options) #endif /*Now the text is dynamically allocated*/ - dropdown->static_txt = 0; + dropdown->static_options = 0; lv_obj_invalidate(obj); if(dropdown->list) lv_obj_invalidate(dropdown->list); @@ -236,12 +255,12 @@ void lv_dropdown_set_options_static(lv_obj_t * obj, const char * options) dropdown->sel_opt_id = 0; dropdown->sel_opt_id_orig = 0; - if(dropdown->static_txt == 0 && dropdown->options != NULL) { + if(dropdown->static_options == 0 && dropdown->options != NULL) { lv_free(dropdown->options); dropdown->options = NULL; } - dropdown->static_txt = 1; + dropdown->static_options = 1; dropdown->options = (char *)options; lv_obj_invalidate(obj); @@ -256,7 +275,7 @@ void lv_dropdown_add_option(lv_obj_t * obj, const char * option, uint32_t pos) lv_dropdown_t * dropdown = (lv_dropdown_t *)obj; /*Convert static options to dynamic*/ - if(dropdown->static_txt != 0) { + if(dropdown->static_options != 0) { char * static_options = dropdown->options; if(dropdown->options) { dropdown->options = lv_strdup(static_options); @@ -266,7 +285,7 @@ void lv_dropdown_add_option(lv_obj_t * obj, const char * option, uint32_t pos) } LV_ASSERT_MALLOC(dropdown->options); if(dropdown->options == NULL) return; - dropdown->static_txt = 0; + dropdown->static_options = 0; } /*Allocate space for the new option*/ @@ -326,11 +345,11 @@ void lv_dropdown_clear_options(lv_obj_t * obj) lv_dropdown_t * dropdown = (lv_dropdown_t *)obj; if(dropdown->options == NULL) return; - if(dropdown->static_txt == 0) + if(dropdown->static_options == 0) lv_free(dropdown->options); dropdown->options = NULL; - dropdown->static_txt = 1; + dropdown->static_options = 1; dropdown->option_cnt = 0; lv_obj_invalidate(obj); @@ -677,8 +696,9 @@ static void lv_dropdown_constructor(const lv_obj_class_t * class_p, lv_obj_t * o dropdown->options = NULL; dropdown->symbol = LV_SYMBOL_DOWN; dropdown->text = NULL; - dropdown->static_txt = 1; + dropdown->static_options = 1; dropdown->selected_highlight = 1; + dropdown->static_text = 1; dropdown->sel_opt_id = 0; dropdown->sel_opt_id_orig = 0; dropdown->pr_opt_id = LV_DROPDOWN_PR_NONE; @@ -707,10 +727,11 @@ static void lv_dropdown_destructor(const lv_obj_class_t * class_p, lv_obj_t * ob dropdown->list = NULL; } - if(!dropdown->static_txt) { - lv_free(dropdown->options); - dropdown->options = NULL; - } + if(!dropdown->static_options) lv_free(dropdown->options); + dropdown->options = NULL; + + if(!dropdown->static_text) lv_free(dropdown->text); + dropdown->text = NULL; } static void lv_dropdownlist_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj) diff --git a/src/widgets/dropdown/lv_dropdown.h b/src/widgets/dropdown/lv_dropdown.h index 980f64fb33..4803884434 100644 --- a/src/widgets/dropdown/lv_dropdown.h +++ b/src/widgets/dropdown/lv_dropdown.h @@ -70,9 +70,18 @@ lv_obj_t * lv_dropdown_create(lv_obj_t * parent); * If set to `NULL` the selected option's text will be displayed on the button. * If set to a specific text then that text will be shown regardless of the selected option. * @param obj pointer to a drop-down list object - * @param txt the text as a string (Only its pointer is saved) + * @param text the text as a string (Copy is saved) */ -void lv_dropdown_set_text(lv_obj_t * obj, const char * txt); +void lv_dropdown_set_text(lv_obj_t * obj, const char * text); + +/** + * Set text of the drop-down list's button. + * If set to `NULL` the selected option's text will be displayed on the button. + * If set to a specific text then that text will be shown regardless of the selected option. + * @param obj pointer to a drop-down list object + * @param text the text as a string (Only its pointer is saved) + */ +void lv_dropdown_set_text_static(lv_obj_t * obj, const char * text); /** * Set the options in a drop-down list from a string. diff --git a/src/widgets/dropdown/lv_dropdown_private.h b/src/widgets/dropdown/lv_dropdown_private.h index 8229758b5f..b50883fc07 100644 --- a/src/widgets/dropdown/lv_dropdown_private.h +++ b/src/widgets/dropdown/lv_dropdown_private.h @@ -34,7 +34,7 @@ extern "C" { struct _lv_dropdown_t { lv_obj_t obj; lv_obj_t * list; /**< The dropped down list*/ - const char * text; /**< Text to display on the dropdown's button*/ + char * text; /**< Text to display on the dropdown's button*/ const void * symbol; /**< Arrow or other icon when the drop-down list is closed*/ char * options; /**< Options in a '\n' separated list*/ uint32_t option_cnt; /**< Number of options*/ @@ -42,8 +42,9 @@ struct _lv_dropdown_t { uint32_t sel_opt_id_orig; /**< Store the original index on focus*/ uint32_t pr_opt_id; /**< Index of the currently pressed option*/ uint8_t dir : 4; /**< Direction in which the list should open*/ - uint8_t static_txt : 1; /**< 1: Only a pointer is saved in `options`*/ + uint8_t static_options : 1; /**< 1: Only a pointer is saved in `options`*/ uint8_t selected_highlight: 1; /**< 1: Make the selected option highlighted in the list*/ + uint8_t static_text : 1; /**< 1: Only a pointer is saved in `text`*/ }; struct _lv_dropdown_list_t { diff --git a/tests/src/test_cases/widgets/test_dropdown.c b/tests/src/test_cases/widgets/test_dropdown.c index b4ff530154..8c3d64469d 100644 --- a/tests/src/test_cases/widgets/test_dropdown.c +++ b/tests/src/test_cases/widgets/test_dropdown.c @@ -373,7 +373,7 @@ void test_dropdown_render_2(void) lv_dropdown_open(dd1); lv_obj_t * dd2 = lv_dropdown_create(lv_screen_active()); - lv_dropdown_set_text(dd2, "Go Up"); + lv_dropdown_set_text_static(dd2, "Go Up"); lv_dropdown_set_options(dd2, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15"); lv_dropdown_set_symbol(dd2, NULL); lv_obj_align(dd2, LV_ALIGN_LEFT_MID, 150, 50); @@ -403,7 +403,7 @@ void test_dropdown_render_2(void) lv_dropdown_open(dd5); lv_obj_t * dd6 = lv_dropdown_create(lv_screen_active()); - lv_dropdown_set_text(dd6, "Right"); + lv_dropdown_set_text_static(dd6, "Right"); lv_dropdown_set_options(dd6, "1aaa\n2aa\n3aa"); lv_dropdown_set_dir(dd6, LV_DIR_RIGHT); lv_dropdown_set_symbol(dd6, LV_SYMBOL_RIGHT); @@ -412,7 +412,7 @@ void test_dropdown_render_2(void) lv_obj_set_style_text_align(lv_dropdown_get_list(dd6), LV_TEXT_ALIGN_RIGHT, 0); lv_obj_t * dd7 = lv_dropdown_create(lv_screen_active()); - lv_dropdown_set_text(dd7, "Left"); + lv_dropdown_set_text_static(dd7, "Left"); lv_dropdown_set_options(dd7, "1aaa\n2\n3"); lv_dropdown_set_dir(dd7, LV_DIR_LEFT); lv_dropdown_set_symbol(dd7, LV_SYMBOL_LEFT);