feat(dropdown): static and non-static setter for text
Some checks failed
Arduino Lint / lint (push) Has been cancelled
Build Examples with C++ Compiler / build-examples (push) Has been cancelled
MicroPython CI / Build esp32 port (push) Has been cancelled
MicroPython CI / Build rp2 port (push) Has been cancelled
MicroPython CI / Build stm32 port (push) Has been cancelled
MicroPython CI / Build unix port (push) Has been cancelled
C/C++ CI / Build OPTIONS_16BIT - Ubuntu (push) Has been cancelled
C/C++ CI / Build OPTIONS_24BIT - Ubuntu (push) Has been cancelled
C/C++ CI / Build OPTIONS_FULL_32BIT - Ubuntu (push) Has been cancelled
C/C++ CI / Build OPTIONS_NORMAL_8BIT - Ubuntu (push) Has been cancelled
C/C++ CI / Build OPTIONS_SDL - Ubuntu (push) Has been cancelled
C/C++ CI / Build OPTIONS_16BIT - cl - Windows (push) Has been cancelled
C/C++ CI / Build OPTIONS_16BIT - gcc - Windows (push) Has been cancelled
C/C++ CI / Build OPTIONS_24BIT - cl - Windows (push) Has been cancelled
C/C++ CI / Build OPTIONS_24BIT - gcc - Windows (push) Has been cancelled
C/C++ CI / Build OPTIONS_FULL_32BIT - cl - Windows (push) Has been cancelled
C/C++ CI / Build OPTIONS_FULL_32BIT - gcc - Windows (push) Has been cancelled
C/C++ CI / Build ESP IDF ESP32S3 (push) Has been cancelled
C/C++ CI / Run tests with 32bit build (push) Has been cancelled
C/C++ CI / Run tests with 64bit build (push) Has been cancelled
BOM Check / bom-check (push) Has been cancelled
Verify that lv_conf_internal.h matches repository state / verify-conf-internal (push) Has been cancelled
Verify the widget property name / verify-property-name (push) Has been cancelled
Verify code formatting / verify-formatting (push) Has been cancelled
Compare file templates with file names / template-check (push) Has been cancelled
Build docs / build-and-deploy (push) Has been cancelled
Test API JSON generator / Test API JSON (push) Has been cancelled
Install LVGL using CMake / build-examples (push) Has been cancelled
Check Makefile / Build using Makefile (push) Has been cancelled
Check Makefile for UEFI / Build using Makefile for UEFI (push) Has been cancelled
Emulated Performance Test / ARM Emulated Benchmark - Script Check (scripts/perf/tests/benchmark_results_comment/test.sh) (push) Has been cancelled
Emulated Performance Test / ARM Emulated Benchmark - Script Check (scripts/perf/tests/filter_docker_logs/test.sh) (push) Has been cancelled
Emulated Performance Test / ARM Emulated Benchmark - Script Check (scripts/perf/tests/serialize_results/test.sh) (push) Has been cancelled
Hardware Performance Test / Hardware Performance Benchmark (push) Has been cancelled
Hardware Performance Test / HW Benchmark - Save PR Number (push) Has been cancelled
Performance Tests CI / Perf Tests OPTIONS_TEST_PERF_32B - Ubuntu (push) Has been cancelled
Performance Tests CI / Perf Tests OPTIONS_TEST_PERF_64B - Ubuntu (push) Has been cancelled
Port repo release update / run-release-branch-updater (push) Has been cancelled
Verify Font License / verify-font-license (push) Has been cancelled
Verify Kconfig / verify-kconfig (push) Has been cancelled
Emulated Performance Test / ARM Emulated Benchmark 32b - lv_conf_perf32b (push) Has been cancelled
Emulated Performance Test / ARM Emulated Benchmark 64b - lv_conf_perf64b (push) Has been cancelled
Emulated Performance Test / ARM Emulated Benchmark - Save PR Number (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled

This commit is contained in:
Niklas Fiekas
2026-01-19 10:14:48 +01:00
committed by Liam Howatt
parent e20517aa70
commit af5c3f12f6
5 changed files with 57 additions and 25 deletions

View File

@@ -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.

View File

@@ -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)

View File

@@ -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.

View File

@@ -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 {

View File

@@ -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);