1 /**
2  * @file lv_theme.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "../../lvgl.h"
10 
11 /*********************
12  *      DEFINES
13  *********************/
14 
15 /**********************
16  *      TYPEDEFS
17  **********************/
18 
19 /**********************
20  *  STATIC PROTOTYPES
21  **********************/
22 static void apply_theme(lv_theme_t * th, lv_obj_t * obj);
23 
24 /**********************
25  *  STATIC VARIABLES
26  **********************/
27 
28 /**********************
29  *      MACROS
30  **********************/
31 
32 /**********************
33  *   GLOBAL FUNCTIONS
34  **********************/
35 
lv_theme_get_from_obj(lv_obj_t * obj)36 lv_theme_t  * lv_theme_get_from_obj(lv_obj_t * obj)
37 {
38     lv_disp_t * disp = obj ? lv_obj_get_disp(obj) : lv_disp_get_default();
39     return lv_disp_get_theme(disp);
40 }
41 
42 /**
43  * Apply the active theme on an object
44  * @param obj pointer to an object
45  * @param name the name of the theme element to apply. E.g. `LV_THEME_BTN`
46  */
lv_theme_apply(lv_obj_t * obj)47 void lv_theme_apply(lv_obj_t * obj)
48 {
49     lv_theme_t * th = lv_theme_get_from_obj(obj);
50     if(th == NULL) return;
51 
52     lv_obj_remove_style_all(obj);
53 
54     apply_theme(th, obj);    /*Apply the theme including the base theme(s)*/
55 }
56 
57 /**
58  * Set a base theme for a theme.
59  * The styles from the base them will be added before the styles of the current theme.
60  * Arbitrary long chain of themes can be created by setting base themes.
61  * @param new_theme pointer to theme which base should be set
62  * @param base pointer to the base theme
63  */
lv_theme_set_parent(lv_theme_t * new_theme,lv_theme_t * base)64 void lv_theme_set_parent(lv_theme_t * new_theme, lv_theme_t * base)
65 {
66     new_theme->parent = base;
67 }
68 
69 /**
70  * Set a callback for a theme.
71  * The callback is used to add styles to different objects
72  * @param theme pointer to theme which callback should be set
73  * @param cb pointer to the callback
74  */
lv_theme_set_apply_cb(lv_theme_t * theme,lv_theme_apply_cb_t apply_cb)75 void lv_theme_set_apply_cb(lv_theme_t * theme, lv_theme_apply_cb_t apply_cb)
76 {
77     theme->apply_cb = apply_cb;
78 }
79 
lv_theme_get_font_small(lv_obj_t * obj)80 const lv_font_t * lv_theme_get_font_small(lv_obj_t * obj)
81 {
82     lv_theme_t * th = lv_theme_get_from_obj(obj);
83     return th ? th->font_small : LV_FONT_DEFAULT;
84 }
85 
lv_theme_get_font_normal(lv_obj_t * obj)86 const lv_font_t * lv_theme_get_font_normal(lv_obj_t * obj)
87 {
88     lv_theme_t * th = lv_theme_get_from_obj(obj);
89     return th ? th->font_normal : LV_FONT_DEFAULT;
90 }
91 
lv_theme_get_font_large(lv_obj_t * obj)92 const lv_font_t * lv_theme_get_font_large(lv_obj_t * obj)
93 {
94     lv_theme_t * th = lv_theme_get_from_obj(obj);
95     return th ? th->font_large : LV_FONT_DEFAULT;
96 }
97 
lv_theme_get_color_primary(lv_obj_t * obj)98 lv_color_t lv_theme_get_color_primary(lv_obj_t * obj)
99 {
100     lv_theme_t * th = lv_theme_get_from_obj(obj);
101     return th ? th->color_primary : lv_palette_main(LV_PALETTE_BLUE_GREY);
102 }
103 
lv_theme_get_color_secondary(lv_obj_t * obj)104 lv_color_t lv_theme_get_color_secondary(lv_obj_t * obj)
105 {
106     lv_theme_t * th = lv_theme_get_from_obj(obj);
107     return th ? th->color_secondary : lv_palette_main(LV_PALETTE_BLUE);
108 }
109 
110 /**********************
111  *   STATIC FUNCTIONS
112  **********************/
113 
apply_theme(lv_theme_t * th,lv_obj_t * obj)114 static void apply_theme(lv_theme_t * th, lv_obj_t * obj)
115 {
116     if(th->parent) apply_theme(th->parent, obj);
117     if(th->apply_cb) th->apply_cb(th, obj);
118 }
119