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, lv_theme_style_t name);
23 static void clear_styles(lv_obj_t * obj, lv_theme_style_t name);
24 
25 /**********************
26  *  STATIC VARIABLES
27  **********************/
28 static lv_theme_t * act_theme;
29 
30 /**********************
31  *      MACROS
32  **********************/
33 
34 /**********************
35  *   GLOBAL FUNCTIONS
36  **********************/
37 
38 /**
39  * Set a theme for the system.
40  * From now, all the created objects will use styles from this theme by default
41  * @param th pointer to theme (return value of: 'lv_theme_init_xxx()')
42  */
lv_theme_set_act(lv_theme_t * th)43 void lv_theme_set_act(lv_theme_t * th)
44 {
45     act_theme = th;
46 }
47 
48 /**
49  * Get the current system theme.
50  * @return pointer to the current system theme. NULL if not set.
51  */
lv_theme_get_act(void)52 lv_theme_t * lv_theme_get_act(void)
53 {
54     return act_theme;
55 }
56 
57 /**
58  * Apply the active theme on an object
59  * @param obj pointer to an object
60  * @param name the name of the theme element to apply. E.g. `LV_THEME_BTN`
61  */
lv_theme_apply(lv_obj_t * obj,lv_theme_style_t name)62 void lv_theme_apply(lv_obj_t * obj, lv_theme_style_t name)
63 {
64     /* Remove the existing styles from all part of the object. */
65     clear_styles(obj, name);
66 
67     /*Apply the theme including the base theme(s)*/
68 
69     apply_theme(act_theme, obj, name);
70 }
71 
72 /**
73  * Copy a theme to an other or initialize a theme
74  * @param theme pointer to a theme to initialize
75  * @param copy pointer to a theme to copy
76  *             or `NULL` to initialize `theme` to empty
77  */
lv_theme_copy(lv_theme_t * theme,const lv_theme_t * copy)78 void lv_theme_copy(lv_theme_t * theme, const lv_theme_t * copy)
79 {
80     _lv_memset_00(theme, sizeof(lv_theme_t));
81 
82     if(copy) {
83         theme->font_small = copy->font_small;
84         theme->font_normal = copy->font_normal;
85         theme->font_subtitle = copy->font_subtitle;
86         theme->font_title = copy->font_title;
87         theme->color_primary = copy->color_primary;
88         theme->color_secondary = copy->color_secondary;
89         theme->flags = copy->flags;
90         theme->base = copy->base;
91         theme->apply_cb = copy->apply_cb;
92         theme->apply_xcb = copy->apply_xcb;
93     }
94 
95 }
96 
97 /**
98  * Set a base theme for a theme.
99  * The styles from the base them will be added before the styles of the current theme.
100  * Arbitrary long chain of themes can be created by setting base themes.
101  * @param new_theme pointer to theme which base should be set
102  * @param base pointer to the base theme
103  */
lv_theme_set_base(lv_theme_t * new_theme,lv_theme_t * base)104 void lv_theme_set_base(lv_theme_t * new_theme, lv_theme_t * base)
105 {
106     new_theme->base = base;
107 }
108 
109 /**
110  * Set a callback for a theme.
111  * The callback is used to add styles to different objects
112  * @param theme pointer to theme which callback should be set
113  * @param cb pointer to the callback
114  */
lv_theme_set_apply_cb(lv_theme_t * theme,lv_theme_apply_cb_t apply_cb)115 void lv_theme_set_apply_cb(lv_theme_t * theme, lv_theme_apply_cb_t apply_cb)
116 {
117     theme->apply_cb = apply_cb;
118 }
119 
120 /**
121  * Get the small font of the theme
122  * @return pointer to the font
123  */
lv_theme_get_font_small(void)124 const lv_font_t * lv_theme_get_font_small(void)
125 {
126     return act_theme->font_small;
127 }
128 
129 /**
130  * Get the normal font of the theme
131  * @return pointer to the font
132  */
lv_theme_get_font_normal(void)133 const lv_font_t * lv_theme_get_font_normal(void)
134 {
135     return act_theme->font_normal;
136 }
137 
138 /**
139  * Get the subtitle font of the theme
140  * @return pointer to the font
141  */
lv_theme_get_font_subtitle(void)142 const lv_font_t * lv_theme_get_font_subtitle(void)
143 {
144     return act_theme->font_subtitle;
145 }
146 
147 /**
148  * Get the title font of the theme
149  * @return pointer to the font
150  */
lv_theme_get_font_title(void)151 const lv_font_t * lv_theme_get_font_title(void)
152 {
153     return act_theme->font_title;
154 }
155 
156 /**
157  * Get the primary color of the theme
158  * @return the color
159  */
lv_theme_get_color_primary(void)160 lv_color_t lv_theme_get_color_primary(void)
161 {
162     return act_theme->color_primary;
163 }
164 
165 /**
166  * Get the secondary color of the theme
167  * @return the color
168  */
lv_theme_get_color_secondary(void)169 lv_color_t lv_theme_get_color_secondary(void)
170 {
171     return act_theme->color_secondary;
172 }
173 
174 /**
175  * Get the flags of the theme
176  * @return the flags
177  */
lv_theme_get_flags(void)178 uint32_t lv_theme_get_flags(void)
179 {
180     return act_theme->flags;
181 }
182 
183 /**********************
184  *   STATIC FUNCTIONS
185  **********************/
186 
apply_theme(lv_theme_t * th,lv_obj_t * obj,lv_theme_style_t name)187 static void apply_theme(lv_theme_t * th, lv_obj_t * obj, lv_theme_style_t name)
188 {
189     if(th->base) {
190         apply_theme(th->base, obj, name);
191     }
192 
193     /*apply_xcb is deprecated, use apply_cb instead*/
194     if(th->apply_xcb) {
195         th->apply_xcb(obj, name);
196     }
197     else if(th->apply_cb) {
198         th->apply_cb(act_theme, obj, name);
199     }
200 }
201 
clear_styles(lv_obj_t * obj,lv_theme_style_t name)202 static void clear_styles(lv_obj_t * obj, lv_theme_style_t name)
203 {
204     switch(name) {
205         case LV_THEME_NONE:
206             break;
207 
208         case LV_THEME_SCR:
209             lv_obj_clean_style_list(obj, LV_OBJ_PART_MAIN);
210             break;
211         case LV_THEME_OBJ:
212             lv_obj_clean_style_list(obj, LV_OBJ_PART_MAIN);
213             break;
214 #if LV_USE_CONT
215         case LV_THEME_CONT:
216             lv_obj_clean_style_list(obj, LV_OBJ_PART_MAIN);
217             break;
218 #endif
219 
220 #if LV_USE_BTN
221         case LV_THEME_BTN:
222             lv_obj_clean_style_list(obj, LV_BTN_PART_MAIN);
223             break;
224 #endif
225 
226 #if LV_USE_BTNMATRIX
227         case LV_THEME_BTNMATRIX:
228             lv_obj_clean_style_list(obj, LV_BTNMATRIX_PART_BG);
229             lv_obj_clean_style_list(obj, LV_BTNMATRIX_PART_BTN);
230             break;
231 #endif
232 
233 #if LV_USE_KEYBOARD
234         case LV_THEME_KEYBOARD:
235             lv_obj_clean_style_list(obj, LV_KEYBOARD_PART_BG);
236             lv_obj_clean_style_list(obj, LV_KEYBOARD_PART_BTN);
237             break;
238 #endif
239 
240 #if LV_USE_BAR
241         case LV_THEME_BAR:
242             lv_obj_clean_style_list(obj, LV_BAR_PART_BG);
243             lv_obj_clean_style_list(obj, LV_BAR_PART_INDIC);
244             break;
245 #endif
246 
247 #if LV_USE_SWITCH
248         case LV_THEME_SWITCH:
249             lv_obj_clean_style_list(obj, LV_SWITCH_PART_BG);
250             lv_obj_clean_style_list(obj, LV_SWITCH_PART_INDIC);
251             lv_obj_clean_style_list(obj, LV_SWITCH_PART_KNOB);
252             break;
253 #endif
254 
255 #if LV_USE_CANVAS
256         case LV_THEME_CANVAS:
257             lv_obj_clean_style_list(obj, LV_CANVAS_PART_MAIN);
258             break;
259 #endif
260 
261 #if LV_USE_IMG
262         case LV_THEME_IMAGE:
263             lv_obj_clean_style_list(obj, LV_IMG_PART_MAIN);
264             break;
265 #endif
266 
267 #if LV_USE_IMGBTN
268         case LV_THEME_IMGBTN:
269             lv_obj_clean_style_list(obj, LV_IMG_PART_MAIN);
270             break;
271 #endif
272 
273 #if LV_USE_LABEL
274         case LV_THEME_LABEL:
275             lv_obj_clean_style_list(obj, LV_LABEL_PART_MAIN);
276             break;
277 #endif
278 
279 #if LV_USE_LINE
280         case LV_THEME_LINE:
281             lv_obj_clean_style_list(obj, LV_LABEL_PART_MAIN);
282             break;
283 #endif
284 
285 #if LV_USE_ARC
286         case LV_THEME_ARC:
287             lv_obj_clean_style_list(obj, LV_ARC_PART_BG);
288             lv_obj_clean_style_list(obj, LV_ARC_PART_INDIC);
289             break;
290 #endif
291 
292 #if LV_USE_SPINNER
293         case LV_THEME_SPINNER:
294             lv_obj_clean_style_list(obj, LV_SPINNER_PART_BG);
295             lv_obj_clean_style_list(obj, LV_SPINNER_PART_INDIC);
296             break;
297 #endif
298 
299 #if LV_USE_SLIDER
300         case LV_THEME_SLIDER:
301             lv_obj_clean_style_list(obj, LV_SLIDER_PART_BG);
302             lv_obj_clean_style_list(obj, LV_SLIDER_PART_INDIC);
303             lv_obj_clean_style_list(obj, LV_SLIDER_PART_KNOB);
304             break;
305 #endif
306 
307 #if LV_USE_CHECKBOX
308         case LV_THEME_CHECKBOX:
309             lv_obj_clean_style_list(obj, LV_CHECKBOX_PART_BG);
310             lv_obj_clean_style_list(obj, LV_CHECKBOX_PART_BULLET);
311             break;
312 #endif
313 
314 #if LV_USE_MSGBOX
315         case LV_THEME_MSGBOX:
316             lv_obj_clean_style_list(obj, LV_MSGBOX_PART_BG);
317             break;
318 
319         case LV_THEME_MSGBOX_BTNS:
320             lv_obj_clean_style_list(obj, LV_MSGBOX_PART_BTN_BG);
321             lv_obj_clean_style_list(obj, LV_MSGBOX_PART_BTN);
322             break;
323 
324 #endif
325 #if LV_USE_LED
326         case LV_THEME_LED:
327             lv_obj_clean_style_list(obj, LV_LED_PART_MAIN);
328             break;
329 #endif
330 #if LV_USE_PAGE
331         case LV_THEME_PAGE:
332             lv_obj_clean_style_list(obj, LV_PAGE_PART_BG);
333             lv_obj_clean_style_list(obj, LV_PAGE_PART_SCROLLABLE);
334             lv_obj_clean_style_list(obj, LV_PAGE_PART_SCROLLBAR);
335             break;
336 #endif
337 #if LV_USE_TABVIEW
338         case LV_THEME_TABVIEW:
339             lv_obj_clean_style_list(obj, LV_TABVIEW_PART_BG);
340             lv_obj_clean_style_list(obj, LV_TABVIEW_PART_BG_SCRLLABLE);
341             lv_obj_clean_style_list(obj, LV_TABVIEW_PART_TAB_BG);
342             lv_obj_clean_style_list(obj, LV_TABVIEW_PART_INDIC);
343             lv_obj_clean_style_list(obj, LV_TABVIEW_PART_TAB_BTN);
344             break;
345 
346         case LV_THEME_TABVIEW_PAGE:
347             lv_obj_clean_style_list(obj, LV_PAGE_PART_BG);
348             lv_obj_clean_style_list(obj, LV_PAGE_PART_SCROLLABLE);
349             break;
350 #endif
351 
352 #if LV_USE_TILEVIEW
353         case LV_THEME_TILEVIEW:
354             lv_obj_clean_style_list(obj, LV_TILEVIEW_PART_BG);
355             lv_obj_clean_style_list(obj, LV_TILEVIEW_PART_SCROLLBAR);
356             lv_obj_clean_style_list(obj, LV_TILEVIEW_PART_EDGE_FLASH);
357             break;
358 #endif
359 
360 
361 #if LV_USE_ROLLER
362         case LV_THEME_ROLLER:
363             lv_obj_clean_style_list(obj, LV_ROLLER_PART_BG);
364             lv_obj_clean_style_list(obj, LV_ROLLER_PART_SELECTED);
365             break;
366 #endif
367 
368 
369 #if LV_USE_OBJMASK
370         case LV_THEME_OBJMASK:
371             lv_obj_clean_style_list(obj, LV_OBJMASK_PART_MAIN);
372             break;
373 #endif
374 
375 #if LV_USE_LIST
376         case LV_THEME_LIST:
377             lv_obj_clean_style_list(obj, LV_LIST_PART_BG);
378             lv_obj_clean_style_list(obj, LV_LIST_PART_SCROLLABLE);
379             lv_obj_clean_style_list(obj, LV_LIST_PART_SCROLLBAR);
380             break;
381 
382         case LV_THEME_LIST_BTN:
383             lv_obj_clean_style_list(obj, LV_BTN_PART_MAIN);
384             break;
385 #endif
386 
387 #if LV_USE_DROPDOWN
388         case LV_THEME_DROPDOWN:
389             lv_obj_clean_style_list(obj, LV_DROPDOWN_PART_MAIN);
390             lv_obj_clean_style_list(obj, LV_DROPDOWN_PART_LIST);
391             lv_obj_clean_style_list(obj, LV_DROPDOWN_PART_SCROLLBAR);
392             lv_obj_clean_style_list(obj, LV_DROPDOWN_PART_SELECTED);
393             break;
394 #endif
395 
396 #if LV_USE_CHART
397         case LV_THEME_CHART:
398             lv_obj_clean_style_list(obj, LV_CHART_PART_BG);
399             lv_obj_clean_style_list(obj, LV_CHART_PART_SERIES_BG);
400             lv_obj_clean_style_list(obj, LV_CHART_PART_SERIES);
401             break;
402 #endif
403 #if LV_USE_TABLE
404         case LV_THEME_TABLE:
405             lv_obj_clean_style_list(obj, LV_TABLE_PART_BG);
406             lv_obj_clean_style_list(obj, LV_TABLE_PART_CELL1);
407             lv_obj_clean_style_list(obj, LV_TABLE_PART_CELL2);
408             lv_obj_clean_style_list(obj, LV_TABLE_PART_CELL3);
409             lv_obj_clean_style_list(obj, LV_TABLE_PART_CELL4);
410             break;
411 #endif
412 
413 #if LV_USE_WIN
414         case LV_THEME_WIN:
415             lv_obj_clean_style_list(obj, LV_WIN_PART_BG);
416             lv_obj_clean_style_list(obj, LV_WIN_PART_SCROLLBAR);
417             lv_obj_clean_style_list(obj, LV_WIN_PART_CONTENT_SCROLLABLE);
418             lv_obj_clean_style_list(obj, LV_WIN_PART_HEADER);
419             break;
420 
421         case LV_THEME_WIN_BTN:
422             lv_obj_clean_style_list(obj, LV_BTN_PART_MAIN);
423             break;
424 #endif
425 
426 #if LV_USE_TEXTAREA
427         case LV_THEME_TEXTAREA:
428             lv_obj_clean_style_list(obj, LV_TEXTAREA_PART_BG);
429             lv_obj_clean_style_list(obj, LV_TEXTAREA_PART_PLACEHOLDER);
430             lv_obj_clean_style_list(obj, LV_TEXTAREA_PART_CURSOR);
431             lv_obj_clean_style_list(obj, LV_TEXTAREA_PART_SCROLLBAR);
432             break;
433 #endif
434 
435 
436 #if LV_USE_SPINBOX
437         case LV_THEME_SPINBOX:
438             lv_obj_clean_style_list(obj, LV_SPINBOX_PART_BG);
439             lv_obj_clean_style_list(obj, LV_SPINBOX_PART_CURSOR);
440             break;
441 
442         case LV_THEME_SPINBOX_BTN:
443             lv_obj_clean_style_list(obj, LV_BTN_PART_MAIN);
444             break;
445 #endif
446 
447 #if LV_USE_CALENDAR
448         case LV_THEME_CALENDAR:
449             lv_obj_clean_style_list(obj, LV_CALENDAR_PART_BG);
450             lv_obj_clean_style_list(obj, LV_CALENDAR_PART_DATE);
451             lv_obj_clean_style_list(obj, LV_CALENDAR_PART_HEADER);
452             lv_obj_clean_style_list(obj, LV_CALENDAR_PART_DAY_NAMES);
453             break;
454 #endif
455 #if LV_USE_CPICKER
456         case LV_THEME_CPICKER:
457             lv_obj_clean_style_list(obj, LV_CPICKER_PART_MAIN);
458             lv_obj_clean_style_list(obj, LV_CPICKER_PART_KNOB);
459             break;
460 #endif
461 
462 #if LV_USE_LINEMETER
463         case LV_THEME_LINEMETER:
464             lv_obj_clean_style_list(obj, LV_LINEMETER_PART_MAIN);
465             break;
466 #endif
467 #if LV_USE_GAUGE
468         case LV_THEME_GAUGE:
469             lv_obj_clean_style_list(obj, LV_GAUGE_PART_MAIN);
470             lv_obj_clean_style_list(obj, LV_GAUGE_PART_MAJOR);
471             lv_obj_clean_style_list(obj, LV_GAUGE_PART_NEEDLE);
472             break;
473 #endif
474         default:
475             break;
476     }
477 
478 }
479