1 /**
2  * @file lv_theme.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_theme_private.h"
10 #include "../core/lv_obj_private.h"
11 #include "../core/lv_obj_class_private.h"
12 #include "../../lvgl.h"
13 
14 /*********************
15  *      DEFINES
16  *********************/
17 
18 /**********************
19  *      TYPEDEFS
20  **********************/
21 
22 /**********************
23  *  STATIC PROTOTYPES
24  **********************/
25 static void apply_theme(lv_theme_t * th, lv_obj_t * obj);
26 static void apply_theme_recursion(lv_theme_t * th, lv_obj_t * obj);
27 
28 /**********************
29  *  STATIC VARIABLES
30  **********************/
31 
32 /**********************
33  *      MACROS
34  **********************/
35 
36 /**********************
37  *   GLOBAL FUNCTIONS
38  **********************/
39 
lv_theme_get_from_obj(lv_obj_t * obj)40 lv_theme_t  * lv_theme_get_from_obj(lv_obj_t * obj)
41 {
42     lv_display_t * disp = obj ? lv_obj_get_display(obj) : lv_display_get_default();
43     return lv_display_get_theme(disp);
44 }
45 
lv_theme_apply(lv_obj_t * obj)46 void lv_theme_apply(lv_obj_t * obj)
47 {
48     lv_theme_t * th = lv_theme_get_from_obj(obj);
49     if(th == NULL) return;
50 
51     lv_obj_remove_style_all(obj);
52 
53     apply_theme_recursion(th, obj);    /*Apply the theme including the base theme(s)*/
54 }
55 
lv_theme_set_parent(lv_theme_t * new_theme,lv_theme_t * base)56 void lv_theme_set_parent(lv_theme_t * new_theme, lv_theme_t * base)
57 {
58     new_theme->parent = base;
59 }
60 
lv_theme_set_apply_cb(lv_theme_t * theme,lv_theme_apply_cb_t apply_cb)61 void lv_theme_set_apply_cb(lv_theme_t * theme, lv_theme_apply_cb_t apply_cb)
62 {
63     theme->apply_cb = apply_cb;
64 }
65 
lv_theme_get_font_small(lv_obj_t * obj)66 const lv_font_t * lv_theme_get_font_small(lv_obj_t * obj)
67 {
68     lv_theme_t * th = lv_theme_get_from_obj(obj);
69     return th ? th->font_small : LV_FONT_DEFAULT;
70 }
71 
lv_theme_get_font_normal(lv_obj_t * obj)72 const lv_font_t * lv_theme_get_font_normal(lv_obj_t * obj)
73 {
74     lv_theme_t * th = lv_theme_get_from_obj(obj);
75     return th ? th->font_normal : LV_FONT_DEFAULT;
76 }
77 
lv_theme_get_font_large(lv_obj_t * obj)78 const lv_font_t * lv_theme_get_font_large(lv_obj_t * obj)
79 {
80     lv_theme_t * th = lv_theme_get_from_obj(obj);
81     return th ? th->font_large : LV_FONT_DEFAULT;
82 }
83 
lv_theme_get_color_primary(lv_obj_t * obj)84 lv_color_t lv_theme_get_color_primary(lv_obj_t * obj)
85 {
86     lv_theme_t * th = lv_theme_get_from_obj(obj);
87     return th ? th->color_primary : lv_palette_main(LV_PALETTE_BLUE_GREY);
88 }
89 
lv_theme_get_color_secondary(lv_obj_t * obj)90 lv_color_t lv_theme_get_color_secondary(lv_obj_t * obj)
91 {
92     lv_theme_t * th = lv_theme_get_from_obj(obj);
93     return th ? th->color_secondary : lv_palette_main(LV_PALETTE_BLUE);
94 }
95 
96 /**********************
97  *   STATIC FUNCTIONS
98  **********************/
99 
apply_theme(lv_theme_t * th,lv_obj_t * obj)100 static void apply_theme(lv_theme_t * th, lv_obj_t * obj)
101 {
102     if(th->parent) apply_theme(th->parent, obj);
103     if(th->apply_cb) th->apply_cb(th, obj);
104 }
105 
apply_theme_recursion(lv_theme_t * th,lv_obj_t * obj)106 static void apply_theme_recursion(lv_theme_t * th, lv_obj_t * obj)
107 {
108     const lv_obj_class_t * original_class_p = obj->class_p;
109 
110     if(obj->class_p->base_class && obj->class_p->theme_inheritable == LV_OBJ_CLASS_THEME_INHERITABLE_TRUE) {
111         /*Apply the base class theme in obj*/
112         obj->class_p = obj->class_p->base_class;
113 
114         /*apply the base first*/
115         apply_theme_recursion(th, obj);
116     }
117 
118     /*Restore the original class*/
119     obj->class_p = original_class_p;
120 
121     apply_theme(th, obj);
122 }
123