diff options
Diffstat (limited to 'src/libs/lvgl/src/lv_themes/lv_theme.c')
-rw-r--r-- | src/libs/lvgl/src/lv_themes/lv_theme.c | 479 |
1 files changed, 414 insertions, 65 deletions
diff --git a/src/libs/lvgl/src/lv_themes/lv_theme.c b/src/libs/lvgl/src/lv_themes/lv_theme.c index 21cc38c2..e77fed07 100644 --- a/src/libs/lvgl/src/lv_themes/lv_theme.c +++ b/src/libs/lvgl/src/lv_themes/lv_theme.c @@ -6,8 +6,7 @@ /********************* * INCLUDES *********************/ -#include "lv_theme.h" -#include "../lv_core/lv_obj.h" +#include "../../lvgl.h" /********************* * DEFINES @@ -20,25 +19,13 @@ /********************** * STATIC PROTOTYPES **********************/ +static void apply_theme(lv_theme_t * th, lv_obj_t * obj, lv_theme_style_t name); +static void clear_styles(lv_obj_t * obj, lv_theme_style_t name); /********************** * STATIC VARIABLES **********************/ - -#if LV_THEME_LIVE_UPDATE == 0 -static lv_theme_t * current_theme; -#else -/* If live update is used then a big `lv_style_t` array is used to store the real styles of the - * theme not only pointers. On `lv_theme_set_current` the styles of the theme are copied to this - * array. The pointers in `current_theme` are initialized to point to the styles in the array. This - * way the theme styles will always point to the same memory address even after theme is change. - * (The pointers in the theme points to the styles declared by the theme itself) */ - -/* Store the styles in this array. */ -static lv_style_t th_styles[LV_THEME_STYLE_COUNT]; -static bool inited = false; -static lv_theme_t current_theme; -#endif +static lv_theme_t * act_theme; /********************** * MACROS @@ -53,72 +40,434 @@ static lv_theme_t current_theme; * From now, all the created objects will use styles from this theme by default * @param th pointer to theme (return value of: 'lv_theme_init_xxx()') */ -void lv_theme_set_current(lv_theme_t * th) +void lv_theme_set_act(lv_theme_t * th) { -#if LV_THEME_LIVE_UPDATE == 0 - current_theme = th; + act_theme = th; +} -#if LV_USE_GROUP - /*Copy group style modification callback functions*/ - memcpy(¤t_theme->group, &th->group, sizeof(th->group)); -#endif +/** + * Get the current system theme. + * @return pointer to the current system theme. NULL if not set. + */ +lv_theme_t * lv_theme_get_act(void) +{ + return act_theme; +} - /*Let the object know their style might change*/ - lv_obj_report_style_mod(NULL); +/** + * Apply the active theme on an object + * @param obj pointer to an object + * @param name the name of the theme element to apply. E.g. `LV_THEME_BTN` + */ +void lv_theme_apply(lv_obj_t * obj, lv_theme_style_t name) +{ + /* Remove the existing styles from all part of the object. */ + clear_styles(obj, name); -#else - uint32_t style_num = sizeof(th->style) / sizeof(lv_style_t *); /*Number of styles in a theme*/ + /*Apply the theme including the base theme(s)*/ - if(!inited) { - /*Initialize the style pointers `current_theme` to point to the `th_styles` style array */ - uint16_t i; - lv_style_t ** cur_th_style_p = (lv_style_t **)¤t_theme.style; - for(i = 0; i < style_num; i++) { - uintptr_t adr = (uintptr_t)&th_styles[i]; - memcpy(&cur_th_style_p[i], &adr, sizeof(lv_style_t *)); - } - inited = true; - } + apply_theme(act_theme, obj, name); +} - /*Copy the styles pointed by the new theme to the `th_styles` style array*/ - uint16_t i; - lv_style_t ** th_style = (lv_style_t **)&th->style; - for(i = 0; i < style_num; i++) { - uintptr_t s = (uintptr_t)th_style[i]; - if(s) memcpy(&th_styles[i], (void *)s, sizeof(lv_style_t)); +/** + * Copy a theme to an other or initialize a theme + * @param theme pointer to a theme to initialize + * @param copy pointer to a theme to copy + * or `NULL` to initialize `theme` to empty + */ +void lv_theme_copy(lv_theme_t * theme, const lv_theme_t * copy) +{ + _lv_memset_00(theme, sizeof(lv_theme_t)); + + if(copy) { + theme->font_small = copy->font_small; + theme->font_normal = copy->font_normal; + theme->font_subtitle = copy->font_subtitle; + theme->font_title = copy->font_title; + theme->color_primary = copy->color_primary; + theme->color_secondary = copy->color_secondary; + theme->flags = copy->flags; + theme->base = copy->base; + theme->apply_cb = copy->apply_cb; + theme->apply_xcb = copy->apply_xcb; } +} -#if LV_USE_GROUP - /*Copy group style modification callback functions*/ - memcpy(¤t_theme.group, &th->group, sizeof(th->group)); -#endif +/** + * Set a base theme for a theme. + * The styles from the base them will be added before the styles of the current theme. + * Arbitrary long chain of themes can be created by setting base themes. + * @param new_theme pointer to theme which base should be set + * @param base pointer to the base theme + */ +void lv_theme_set_base(lv_theme_t * new_theme, lv_theme_t * base) +{ + new_theme->base = base; +} - /*Let the object know their style might change*/ - lv_obj_report_style_mod(NULL); +/** + * Set a callback for a theme. + * The callback is used to add styles to different objects + * @param theme pointer to theme which callback should be set + * @param cb pointer to the callback + */ +void lv_theme_set_apply_cb(lv_theme_t * theme, lv_theme_apply_cb_t apply_cb) +{ + theme->apply_cb = apply_cb; +} -#endif +/** + * Get the small font of the theme + * @return pointer to the font + */ +const lv_font_t * lv_theme_get_font_small(void) +{ + return act_theme->font_small; +} -#if LV_USE_GROUP - lv_group_report_style_mod(NULL); -#endif +/** + * Get the normal font of the theme + * @return pointer to the font + */ +const lv_font_t * lv_theme_get_font_normal(void) +{ + return act_theme->font_normal; } /** - * Get the current system theme. - * @return pointer to the current system theme. NULL if not set. + * Get the subtitle font of the theme + * @return pointer to the font */ -lv_theme_t * lv_theme_get_current(void) +const lv_font_t * lv_theme_get_font_subtitle(void) { -#if LV_THEME_LIVE_UPDATE == 0 - return current_theme; -#else - if(!inited) - return NULL; - else - return ¤t_theme; -#endif + return act_theme->font_subtitle; +} + +/** + * Get the title font of the theme + * @return pointer to the font + */ +const lv_font_t * lv_theme_get_font_title(void) +{ + return act_theme->font_title; +} + +/** + * Get the primary color of the theme + * @return the color + */ +lv_color_t lv_theme_get_color_primary(void) +{ + return act_theme->color_primary; +} + +/** + * Get the secondary color of the theme + * @return the color + */ +lv_color_t lv_theme_get_color_secondary(void) +{ + return act_theme->color_secondary; +} + +/** + * Get the flags of the theme + * @return the flags + */ +uint32_t lv_theme_get_flags(void) +{ + return act_theme->flags; } /********************** * STATIC FUNCTIONS **********************/ + +static void apply_theme(lv_theme_t * th, lv_obj_t * obj, lv_theme_style_t name) +{ + if(th->base) { + apply_theme(th->base, obj, name); + } + + /*apply_xcb is deprecated, use apply_cb instead*/ + if(th->apply_xcb) { + th->apply_xcb(obj, name); + } + else if(th->apply_cb) { + th->apply_cb(act_theme, obj, name); + } +} + +static void clear_styles(lv_obj_t * obj, lv_theme_style_t name) +{ + switch(name) { + case LV_THEME_NONE: + break; + + case LV_THEME_SCR: + lv_obj_clean_style_list(obj, LV_OBJ_PART_MAIN); + break; + case LV_THEME_OBJ: + lv_obj_clean_style_list(obj, LV_OBJ_PART_MAIN); + break; +#if LV_USE_CONT + case LV_THEME_CONT: + lv_obj_clean_style_list(obj, LV_OBJ_PART_MAIN); + break; +#endif + +#if LV_USE_BTN + case LV_THEME_BTN: + lv_obj_clean_style_list(obj, LV_BTN_PART_MAIN); + break; +#endif + +#if LV_USE_BTNMATRIX + case LV_THEME_BTNMATRIX: + lv_obj_clean_style_list(obj, LV_BTNMATRIX_PART_BG); + lv_obj_clean_style_list(obj, LV_BTNMATRIX_PART_BTN); + break; +#endif + +#if LV_USE_KEYBOARD + case LV_THEME_KEYBOARD: + lv_obj_clean_style_list(obj, LV_KEYBOARD_PART_BG); + lv_obj_clean_style_list(obj, LV_KEYBOARD_PART_BTN); + break; +#endif + +#if LV_USE_BAR + case LV_THEME_BAR: + lv_obj_clean_style_list(obj, LV_BAR_PART_BG); + lv_obj_clean_style_list(obj, LV_BAR_PART_INDIC); + break; +#endif + +#if LV_USE_SWITCH + case LV_THEME_SWITCH: + lv_obj_clean_style_list(obj, LV_SWITCH_PART_BG); + lv_obj_clean_style_list(obj, LV_SWITCH_PART_INDIC); + lv_obj_clean_style_list(obj, LV_SWITCH_PART_KNOB); + break; +#endif + +#if LV_USE_CANVAS + case LV_THEME_CANVAS: + lv_obj_clean_style_list(obj, LV_CANVAS_PART_MAIN); + break; +#endif + +#if LV_USE_IMG + case LV_THEME_IMAGE: + lv_obj_clean_style_list(obj, LV_IMG_PART_MAIN); + break; +#endif + +#if LV_USE_IMGBTN + case LV_THEME_IMGBTN: + lv_obj_clean_style_list(obj, LV_IMG_PART_MAIN); + break; +#endif + +#if LV_USE_LABEL + case LV_THEME_LABEL: + lv_obj_clean_style_list(obj, LV_LABEL_PART_MAIN); + break; +#endif + +#if LV_USE_LINE + case LV_THEME_LINE: + lv_obj_clean_style_list(obj, LV_LABEL_PART_MAIN); + break; +#endif + +#if LV_USE_ARC + case LV_THEME_ARC: + lv_obj_clean_style_list(obj, LV_ARC_PART_BG); + lv_obj_clean_style_list(obj, LV_ARC_PART_INDIC); + break; +#endif + +#if LV_USE_SPINNER + case LV_THEME_SPINNER: + lv_obj_clean_style_list(obj, LV_SPINNER_PART_BG); + lv_obj_clean_style_list(obj, LV_SPINNER_PART_INDIC); + break; +#endif + +#if LV_USE_SLIDER + case LV_THEME_SLIDER: + lv_obj_clean_style_list(obj, LV_SLIDER_PART_BG); + lv_obj_clean_style_list(obj, LV_SLIDER_PART_INDIC); + lv_obj_clean_style_list(obj, LV_SLIDER_PART_KNOB); + break; +#endif + +#if LV_USE_CHECKBOX + case LV_THEME_CHECKBOX: + lv_obj_clean_style_list(obj, LV_CHECKBOX_PART_BG); + lv_obj_clean_style_list(obj, LV_CHECKBOX_PART_BULLET); + break; +#endif + +#if LV_USE_MSGBOX + case LV_THEME_MSGBOX: + lv_obj_clean_style_list(obj, LV_MSGBOX_PART_BG); + break; + + case LV_THEME_MSGBOX_BTNS: + lv_obj_clean_style_list(obj, LV_MSGBOX_PART_BTN_BG); + lv_obj_clean_style_list(obj, LV_MSGBOX_PART_BTN); + break; + +#endif +#if LV_USE_LED + case LV_THEME_LED: + lv_obj_clean_style_list(obj, LV_LED_PART_MAIN); + break; +#endif +#if LV_USE_PAGE + case LV_THEME_PAGE: + lv_obj_clean_style_list(obj, LV_PAGE_PART_BG); + lv_obj_clean_style_list(obj, LV_PAGE_PART_SCROLLABLE); + lv_obj_clean_style_list(obj, LV_PAGE_PART_SCROLLBAR); + break; +#endif +#if LV_USE_TABVIEW + case LV_THEME_TABVIEW: + lv_obj_clean_style_list(obj, LV_TABVIEW_PART_BG); + lv_obj_clean_style_list(obj, LV_TABVIEW_PART_BG_SCROLLABLE); + lv_obj_clean_style_list(obj, LV_TABVIEW_PART_TAB_BG); + lv_obj_clean_style_list(obj, LV_TABVIEW_PART_INDIC); + lv_obj_clean_style_list(obj, LV_TABVIEW_PART_TAB_BTN); + break; + + case LV_THEME_TABVIEW_PAGE: + lv_obj_clean_style_list(obj, LV_PAGE_PART_BG); + lv_obj_clean_style_list(obj, LV_PAGE_PART_SCROLLABLE); + break; +#endif + +#if LV_USE_TILEVIEW + case LV_THEME_TILEVIEW: + lv_obj_clean_style_list(obj, LV_TILEVIEW_PART_BG); + lv_obj_clean_style_list(obj, LV_TILEVIEW_PART_SCROLLBAR); + lv_obj_clean_style_list(obj, LV_TILEVIEW_PART_EDGE_FLASH); + break; +#endif + +#if LV_USE_ROLLER + case LV_THEME_ROLLER: + lv_obj_clean_style_list(obj, LV_ROLLER_PART_BG); + lv_obj_clean_style_list(obj, LV_ROLLER_PART_SELECTED); + break; +#endif + +#if LV_USE_OBJMASK + case LV_THEME_OBJMASK: + lv_obj_clean_style_list(obj, LV_OBJMASK_PART_MAIN); + break; +#endif + +#if LV_USE_LIST + case LV_THEME_LIST: + lv_obj_clean_style_list(obj, LV_LIST_PART_BG); + lv_obj_clean_style_list(obj, LV_LIST_PART_SCROLLABLE); + lv_obj_clean_style_list(obj, LV_LIST_PART_SCROLLBAR); + break; + + case LV_THEME_LIST_BTN: + lv_obj_clean_style_list(obj, LV_BTN_PART_MAIN); + break; +#endif + +#if LV_USE_DROPDOWN + case LV_THEME_DROPDOWN: + lv_obj_clean_style_list(obj, LV_DROPDOWN_PART_MAIN); + lv_obj_clean_style_list(obj, LV_DROPDOWN_PART_LIST); + lv_obj_clean_style_list(obj, LV_DROPDOWN_PART_SCROLLBAR); + lv_obj_clean_style_list(obj, LV_DROPDOWN_PART_SELECTED); + break; +#endif + +#if LV_USE_CHART + case LV_THEME_CHART: + lv_obj_clean_style_list(obj, LV_CHART_PART_BG); + lv_obj_clean_style_list(obj, LV_CHART_PART_SERIES_BG); + lv_obj_clean_style_list(obj, LV_CHART_PART_SERIES); + break; +#endif +#if LV_USE_TABLE + case LV_THEME_TABLE: + lv_obj_clean_style_list(obj, LV_TABLE_PART_BG); + lv_obj_clean_style_list(obj, LV_TABLE_PART_CELL1); + lv_obj_clean_style_list(obj, LV_TABLE_PART_CELL2); + lv_obj_clean_style_list(obj, LV_TABLE_PART_CELL3); + lv_obj_clean_style_list(obj, LV_TABLE_PART_CELL4); + break; +#endif + +#if LV_USE_WIN + case LV_THEME_WIN: + lv_obj_clean_style_list(obj, LV_WIN_PART_BG); + lv_obj_clean_style_list(obj, LV_WIN_PART_SCROLLBAR); + lv_obj_clean_style_list(obj, LV_WIN_PART_CONTENT_SCROLLABLE); + lv_obj_clean_style_list(obj, LV_WIN_PART_HEADER); + break; + + case LV_THEME_WIN_BTN: + lv_obj_clean_style_list(obj, LV_BTN_PART_MAIN); + break; +#endif + +#if LV_USE_TEXTAREA + case LV_THEME_TEXTAREA: + lv_obj_clean_style_list(obj, LV_TEXTAREA_PART_BG); + lv_obj_clean_style_list(obj, LV_TEXTAREA_PART_PLACEHOLDER); + lv_obj_clean_style_list(obj, LV_TEXTAREA_PART_CURSOR); + lv_obj_clean_style_list(obj, LV_TEXTAREA_PART_SCROLLBAR); + break; +#endif + +#if LV_USE_SPINBOX + case LV_THEME_SPINBOX: + lv_obj_clean_style_list(obj, LV_SPINBOX_PART_BG); + lv_obj_clean_style_list(obj, LV_SPINBOX_PART_CURSOR); + break; + + case LV_THEME_SPINBOX_BTN: + lv_obj_clean_style_list(obj, LV_BTN_PART_MAIN); + break; +#endif + +#if LV_USE_CALENDAR + case LV_THEME_CALENDAR: + lv_obj_clean_style_list(obj, LV_CALENDAR_PART_BG); + lv_obj_clean_style_list(obj, LV_CALENDAR_PART_DATE); + lv_obj_clean_style_list(obj, LV_CALENDAR_PART_HEADER); + lv_obj_clean_style_list(obj, LV_CALENDAR_PART_DAY_NAMES); + break; +#endif +#if LV_USE_CPICKER + case LV_THEME_CPICKER: + lv_obj_clean_style_list(obj, LV_CPICKER_PART_MAIN); + lv_obj_clean_style_list(obj, LV_CPICKER_PART_KNOB); + break; +#endif + +#if LV_USE_LINEMETER + case LV_THEME_LINEMETER: + lv_obj_clean_style_list(obj, LV_LINEMETER_PART_MAIN); + break; +#endif +#if LV_USE_GAUGE + case LV_THEME_GAUGE: + lv_obj_clean_style_list(obj, LV_GAUGE_PART_MAIN); + lv_obj_clean_style_list(obj, LV_GAUGE_PART_MAJOR); + lv_obj_clean_style_list(obj, LV_GAUGE_PART_NEEDLE); + break; +#endif + default: + break; + } +} |