diff --git a/docs/widgets/extra/calendar.md b/docs/widgets/extra/calendar.md index 0fd3630f0..65f484f6b 100644 --- a/docs/widgets/extra/calendar.md +++ b/docs/widgets/extra/calendar.md @@ -45,6 +45,13 @@ Only the array's pointer will be saved so the array should be a static or global The name of the days can be adjusted with `lv_calendar_set_day_names(calendar, day_names)` where `day_names` looks like `const char * day_names[7] = {"Su", "Mo", ...};` Only the pointer of the day names is saved so the elements should be static, global or constant variables. +### Custom year list + +Sets a custom year list with `lv_calendar_header_dropdown_set_year_list(calendar, years_list)` +where ``years_list`` is a pointer to the custom years list. It can be a constant string +like ``static const char * years = "2023\n2022\n2021\n2020\n2019";``, +or can be generated dynamically into a buffer as well. + ## Events - `LV_EVENT_VALUE_CHANGED` Sent if a date is clicked. `lv_calendar_get_pressed_date(calendar, &date)` set `date` to the date currently being pressed. Returns `LV_RES_OK` if there is a valid pressed date, else `LV_RES_INV`. diff --git a/src/extra/widgets/calendar/lv_calendar_header_dropdown.c b/src/extra/widgets/calendar/lv_calendar_header_dropdown.c index 5e8f90d4a..95167f399 100644 --- a/src/extra/widgets/calendar/lv_calendar_header_dropdown.c +++ b/src/extra/widgets/calendar/lv_calendar_header_dropdown.c @@ -66,6 +66,45 @@ lv_obj_t * lv_calendar_header_dropdown_create(lv_obj_t * parent) return obj; } +void lv_calendar_header_dropdown_set_year_list(lv_obj_t * parent, const char * years_list) +{ + bool child_found = false; + uint32_t idx = 0; + lv_obj_t * child = NULL; + + const uint32_t calendar_child_count = lv_obj_get_child_cnt(parent); + + /* Search for the header dropdown */ + for(idx = 0; idx < calendar_child_count; idx++) { + child = lv_obj_get_child(parent, idx); + if(lv_obj_check_type(child, &lv_calendar_header_dropdown_class)) { + child_found = true; + break; + } + } + + if(!child_found) return; + + child_found = false; + const uint32_t header_child_count = lv_obj_get_child_cnt(child); + + /* Search for the year dropdown */ + for(idx = 0; idx < header_child_count; idx++) { + child = lv_obj_get_child(child, idx); + if(lv_obj_check_type(child, &lv_dropdown_class)) { + child_found = true; + break; + } + } + + if(!child_found) return; + + lv_dropdown_clear_options(child); + lv_dropdown_set_options(child, years_list); + + lv_obj_invalidate(parent); +} + /********************** * STATIC FUNCTIONS **********************/ @@ -120,7 +159,14 @@ static void year_event_cb(lv_event_t * e) const lv_calendar_date_t * d; d = lv_calendar_get_showed_date(calendar); lv_calendar_date_t newd = *d; - newd.year = 2023 - sel; + + /* Get the first year on the options list + * NOTE: Assumes the first 4 digits in the option list are numbers */ + const char * year_p = lv_dropdown_get_options(dropdown); + const uint32_t year = (year_p[0] - '0') * 1000 + (year_p[1] - '0') * 100 + (year_p[2] - '0') * 10 + + (year_p[3] - '0'); + + newd.year = year - sel; lv_calendar_set_showed_date(calendar, newd.year, newd.month); } @@ -132,7 +178,14 @@ static void value_changed_event_cb(lv_event_t * e) const lv_calendar_date_t * cur_date = lv_calendar_get_showed_date(calendar); lv_obj_t * year_dd = lv_obj_get_child(header, 0); - lv_dropdown_set_selected(year_dd, 2023 - cur_date->year); + + /* Get the first year on the options list + * NOTE: Assumes the first 4 digits in the option list are numbers */ + const char * year_p = lv_dropdown_get_options(year_dd); + const uint32_t year = (year_p[0] - '0') * 1000 + (year_p[1] - '0') * 100 + (year_p[2] - '0') * 10 + + (year_p[3] - '0'); + + lv_dropdown_set_selected(year_dd, year - cur_date->year); lv_obj_t * month_dd = lv_obj_get_child(header, 1); lv_dropdown_set_selected(month_dd, cur_date->month - 1); diff --git a/src/extra/widgets/calendar/lv_calendar_header_dropdown.h b/src/extra/widgets/calendar/lv_calendar_header_dropdown.h index fca219768..e673a3140 100644 --- a/src/extra/widgets/calendar/lv_calendar_header_dropdown.h +++ b/src/extra/widgets/calendar/lv_calendar_header_dropdown.h @@ -36,6 +36,15 @@ extern const lv_obj_class_t lv_calendar_header_dropdown_class; */ lv_obj_t * lv_calendar_header_dropdown_create(lv_obj_t * parent); +/** + * Sets a custom calendar year list + * @param parent pointer to a calendar object + * @param years_list pointer to an const char array with the years list, see lv_dropdown set_options for more information. + * E.g. `const char * years = "2023\n2022\n2021\n2020\n2019" + * Only the pointer will be saved so this variable can't be local which will be destroyed later. + */ +void lv_calendar_header_dropdown_set_year_list(lv_obj_t * parent, const char * years_list); + /********************** * MACROS **********************/