1 /**
2  * @file lv_theme_basic.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "../../../lvgl.h" /*To see all the widgets*/
10 
11 #if LV_USE_THEME_BASIC
12 
13 #include "lv_theme_basic.h"
14 #include "../../../misc/lv_gc.h"
15 
16 /*********************
17  *      DEFINES
18  *********************/
19 #define COLOR_SCR     lv_palette_lighten(LV_PALETTE_GREY, 4)
20 #define COLOR_WHITE   lv_color_white()
21 #define COLOR_LIGHT   lv_palette_lighten(LV_PALETTE_GREY, 2)
22 #define COLOR_DARK    lv_palette_main(LV_PALETTE_GREY)
23 #define COLOR_DIM     lv_palette_darken(LV_PALETTE_GREY, 2)
24 #define SCROLLBAR_WIDTH     2
25 
26 /**********************
27  *      TYPEDEFS
28  **********************/
29 typedef struct {
30     lv_style_t scr;
31     lv_style_t transp;
32     lv_style_t white;
33     lv_style_t light;
34     lv_style_t dark;
35     lv_style_t dim;
36     lv_style_t scrollbar;
37 #if LV_USE_ARC || LV_USE_COLORWHEEL
38     lv_style_t arc_line;
39     lv_style_t arc_knob;
40 #endif
41 #if LV_USE_TEXTAREA
42     lv_style_t ta_cursor;
43 #endif
44 } my_theme_styles_t;
45 
46 
47 /**********************
48  *  STATIC PROTOTYPES
49  **********************/
50 static void style_init_reset(lv_style_t * style);
51 static void theme_apply(lv_theme_t * th, lv_obj_t * obj);
52 
53 /**********************
54  *  STATIC VARIABLES
55  **********************/
56 static my_theme_styles_t * styles;
57 static lv_theme_t theme;
58 static bool inited;
59 
60 /**********************
61  *      MACROS
62  **********************/
63 
64 /**********************
65  *   STATIC FUNCTIONS
66  **********************/
67 
style_init(void)68 static void style_init(void)
69 {
70     style_init_reset(&styles->scrollbar);
71     lv_style_set_bg_opa(&styles->scrollbar, LV_OPA_COVER);
72     lv_style_set_bg_color(&styles->scrollbar, COLOR_DARK);
73     lv_style_set_width(&styles->scrollbar,  SCROLLBAR_WIDTH);
74 
75     style_init_reset(&styles->scr);
76     lv_style_set_bg_opa(&styles->scr, LV_OPA_COVER);
77     lv_style_set_bg_color(&styles->scr, COLOR_SCR);
78     lv_style_set_text_color(&styles->scr, COLOR_DIM);
79 
80 
81     style_init_reset(&styles->transp);
82     lv_style_set_bg_opa(&styles->transp, LV_OPA_TRANSP);
83 
84     style_init_reset(&styles->white);
85     lv_style_set_bg_opa(&styles->white, LV_OPA_COVER);
86     lv_style_set_bg_color(&styles->white, COLOR_WHITE);
87     lv_style_set_line_width(&styles->white, 1);
88     lv_style_set_line_color(&styles->white, COLOR_WHITE);
89     lv_style_set_arc_width(&styles->white, 2);
90     lv_style_set_arc_color(&styles->white, COLOR_WHITE);
91 
92 
93     style_init_reset(&styles->light);
94     lv_style_set_bg_opa(&styles->light, LV_OPA_COVER);
95     lv_style_set_bg_color(&styles->light, COLOR_LIGHT);
96     lv_style_set_line_width(&styles->light, 1);
97     lv_style_set_line_color(&styles->light, COLOR_LIGHT);
98     lv_style_set_arc_width(&styles->light, 2);
99     lv_style_set_arc_color(&styles->light, COLOR_LIGHT);
100 
101     style_init_reset(&styles->dark);
102     lv_style_set_bg_opa(&styles->dark, LV_OPA_COVER);
103     lv_style_set_bg_color(&styles->dark, COLOR_DARK);
104     lv_style_set_line_width(&styles->dark, 1);
105     lv_style_set_line_color(&styles->dark, COLOR_DARK);
106     lv_style_set_arc_width(&styles->dark, 2);
107     lv_style_set_arc_color(&styles->dark, COLOR_DARK);
108 
109     style_init_reset(&styles->dim);
110     lv_style_set_bg_opa(&styles->dim, LV_OPA_COVER);
111     lv_style_set_bg_color(&styles->dim, COLOR_DIM);
112     lv_style_set_line_width(&styles->dim, 1);
113     lv_style_set_line_color(&styles->dim, COLOR_DIM);
114     lv_style_set_arc_width(&styles->dim, 2);
115     lv_style_set_arc_color(&styles->dim, COLOR_DIM);
116 
117 #if LV_USE_ARC || LV_USE_COLORWHEEL
118     style_init_reset(&styles->arc_line);
119     lv_style_set_arc_width(&styles->arc_line, 6);
120     style_init_reset(&styles->arc_knob);
121     lv_style_set_pad_all(&styles->arc_knob, 5);
122 #endif
123 
124 #if LV_USE_TEXTAREA
125     style_init_reset(&styles->ta_cursor);
126     lv_style_set_border_side(&styles->ta_cursor, LV_BORDER_SIDE_LEFT);
127     lv_style_set_border_color(&styles->ta_cursor, COLOR_DIM);
128     lv_style_set_border_width(&styles->ta_cursor, 2);
129     lv_style_set_bg_opa(&styles->ta_cursor, LV_OPA_TRANSP);
130     lv_style_set_anim_time(&styles->ta_cursor, 500);
131 #endif
132 }
133 
134 
135 /**********************
136  *   GLOBAL FUNCTIONS
137  **********************/
138 
lv_theme_basic_is_inited(void)139 bool lv_theme_basic_is_inited(void)
140 {
141     return  LV_GC_ROOT(_lv_theme_basic_styles) == NULL ? false : true;
142 }
143 
lv_theme_basic_init(lv_disp_t * disp)144 lv_theme_t * lv_theme_basic_init(lv_disp_t * disp)
145 {
146 
147     /*This trick is required only to avoid the garbage collection of
148      *styles' data if LVGL is used in a binding (e.g. Micropython)
149      *In a general case styles could be in simple `static lv_style_t my_style...` variables*/
150     if(!lv_theme_basic_is_inited()) {
151         inited = false;
152         LV_GC_ROOT(_lv_theme_basic_styles) = lv_mem_alloc(sizeof(my_theme_styles_t));
153         styles = (my_theme_styles_t *)LV_GC_ROOT(_lv_theme_basic_styles);
154     }
155 
156     theme.disp = disp;
157     theme.font_small = LV_FONT_DEFAULT;
158     theme.font_normal = LV_FONT_DEFAULT;
159     theme.font_large = LV_FONT_DEFAULT;
160     theme.apply_cb = theme_apply;
161 
162     style_init();
163 
164     if(disp == NULL || lv_disp_get_theme(disp) == &theme) {
165         lv_obj_report_style_change(NULL);
166     }
167 
168     inited = true;
169 
170     return (lv_theme_t *)&theme;
171 }
172 
173 
theme_apply(lv_theme_t * th,lv_obj_t * obj)174 static void theme_apply(lv_theme_t * th, lv_obj_t * obj)
175 {
176     LV_UNUSED(th);
177 
178     if(lv_obj_get_parent(obj) == NULL) {
179         lv_obj_add_style(obj, &styles->scr, 0);
180         lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);
181         return;
182     }
183 
184     if(lv_obj_check_type(obj, &lv_obj_class)) {
185 #if LV_USE_TABVIEW
186         lv_obj_t * parent = lv_obj_get_parent(obj);
187         /*Tabview content area*/
188         if(lv_obj_check_type(parent, &lv_tabview_class)) {
189             lv_obj_add_style(obj, &styles->scr, 0);
190             return;
191         }
192         /*Tabview pages*/
193         else if(lv_obj_check_type(lv_obj_get_parent(parent), &lv_tabview_class)) {
194             lv_obj_add_style(obj, &styles->scr, 0);
195             lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);
196             return;
197         }
198 #endif
199 
200 #if LV_USE_WIN
201         /*Header*/
202         if(lv_obj_get_index(obj) == 0 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
203             lv_obj_add_style(obj, &styles->light, 0);
204             return;
205         }
206         /*Content*/
207         else if(lv_obj_get_index(obj) == 1 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
208             lv_obj_add_style(obj, &styles->light, 0);
209             lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);
210             return;
211         }
212 #endif
213         lv_obj_add_style(obj, &styles->white, 0);
214         lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);
215     }
216 #if LV_USE_BTN
217     else if(lv_obj_check_type(obj, &lv_btn_class)) {
218         lv_obj_add_style(obj, &styles->dark, 0);
219     }
220 #endif
221 
222 #if LV_USE_BTNMATRIX
223     else if(lv_obj_check_type(obj, &lv_btnmatrix_class)) {
224 #if LV_USE_MSGBOX
225         if(lv_obj_check_type(lv_obj_get_parent(obj), &lv_msgbox_class)) {
226             lv_obj_add_style(obj, &styles->light, LV_PART_ITEMS);
227             lv_obj_add_style(obj, &styles->dark, LV_PART_ITEMS | LV_STATE_PRESSED);
228             return;
229         }
230 #endif
231 #if LV_USE_TABVIEW
232         if(lv_obj_check_type(lv_obj_get_parent(obj), &lv_tabview_class)) {
233             lv_obj_add_style(obj, &styles->light, LV_PART_ITEMS);
234             lv_obj_add_style(obj, &styles->dark, LV_PART_ITEMS | LV_STATE_PRESSED);
235             return;
236         }
237 #endif
238         lv_obj_add_style(obj, &styles->white, 0);
239         lv_obj_add_style(obj, &styles->light, LV_PART_ITEMS);
240         lv_obj_add_style(obj, &styles->dark, LV_PART_ITEMS | LV_STATE_PRESSED);
241     }
242 #endif
243 
244 #if LV_USE_BAR
245     else if(lv_obj_check_type(obj, &lv_bar_class)) {
246         lv_obj_add_style(obj, &styles->light, 0);
247         lv_obj_add_style(obj, &styles->dark, LV_PART_INDICATOR);
248     }
249 #endif
250 
251 #if LV_USE_SLIDER
252     else if(lv_obj_check_type(obj, &lv_slider_class)) {
253         lv_obj_add_style(obj, &styles->light, 0);
254         lv_obj_add_style(obj, &styles->dark, LV_PART_INDICATOR);
255         lv_obj_add_style(obj, &styles->dim, LV_PART_KNOB);
256     }
257 #endif
258 
259 #if LV_USE_TABLE
260     else if(lv_obj_check_type(obj, &lv_table_class)) {
261         lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);
262         lv_obj_add_style(obj, &styles->light, LV_PART_ITEMS);
263     }
264 #endif
265 
266 #if LV_USE_CHECKBOX
267     else if(lv_obj_check_type(obj, &lv_checkbox_class)) {
268         lv_obj_add_style(obj, &styles->light, LV_PART_INDICATOR);
269         lv_obj_add_style(obj, &styles->dark, LV_PART_INDICATOR | LV_STATE_CHECKED);
270     }
271 #endif
272 
273 #if LV_USE_SWITCH
274     else if(lv_obj_check_type(obj, &lv_switch_class)) {
275         lv_obj_add_style(obj, &styles->light, 0);
276         lv_obj_add_style(obj, &styles->dark, LV_PART_INDICATOR | LV_STATE_CHECKED);
277         lv_obj_add_style(obj, &styles->dim, LV_PART_KNOB);
278     }
279 #endif
280 
281 #if LV_USE_CHART
282     else if(lv_obj_check_type(obj, &lv_chart_class)) {
283         lv_obj_add_style(obj, &styles->white, 0);
284         lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);
285         lv_obj_add_style(obj, &styles->light, LV_PART_ITEMS);
286         lv_obj_add_style(obj, &styles->dark, LV_PART_TICKS);
287         lv_obj_add_style(obj, &styles->dark, LV_PART_CURSOR);
288     }
289 #endif
290 
291 #if LV_USE_ROLLER
292     else if(lv_obj_check_type(obj, &lv_roller_class)) {
293         lv_obj_add_style(obj, &styles->light, 0);
294         lv_obj_add_style(obj, &styles->dark, LV_PART_SELECTED);
295     }
296 #endif
297 
298 #if LV_USE_DROPDOWN
299     else if(lv_obj_check_type(obj, &lv_dropdown_class)) {
300         lv_obj_add_style(obj, &styles->white, 0);
301     }
302     else if(lv_obj_check_type(obj, &lv_dropdownlist_class)) {
303         lv_obj_add_style(obj, &styles->white, 0);
304         lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);
305         lv_obj_add_style(obj, &styles->light, LV_PART_SELECTED);
306         lv_obj_add_style(obj, &styles->dark, LV_PART_SELECTED | LV_STATE_CHECKED);
307     }
308 #endif
309 
310 #if LV_USE_ARC
311     else if(lv_obj_check_type(obj, &lv_arc_class)) {
312         lv_obj_add_style(obj, &styles->light, 0);
313         lv_obj_add_style(obj, &styles->transp, 0);
314         lv_obj_add_style(obj, &styles->arc_line, 0);
315         lv_obj_add_style(obj, &styles->dark, LV_PART_INDICATOR);
316         lv_obj_add_style(obj, &styles->arc_line, LV_PART_INDICATOR);
317         lv_obj_add_style(obj, &styles->dim, LV_PART_KNOB);
318         lv_obj_add_style(obj, &styles->arc_knob, LV_PART_KNOB);
319     }
320 #endif
321 
322 #if LV_USE_SPINNER
323     else if(lv_obj_check_type(obj, &lv_spinner_class)) {
324         lv_obj_add_style(obj, &styles->light, 0);
325         lv_obj_add_style(obj, &styles->transp, 0);
326         lv_obj_add_style(obj, &styles->arc_line, 0);
327         lv_obj_add_style(obj, &styles->dark, LV_PART_INDICATOR);
328         lv_obj_add_style(obj, &styles->arc_line, LV_PART_INDICATOR);
329     }
330 #endif
331 
332 #if LV_USE_COLORWHEEL
333     else if(lv_obj_check_type(obj, &lv_colorwheel_class)) {
334         lv_obj_add_style(obj, &styles->light, 0);
335         lv_obj_add_style(obj, &styles->transp, 0);
336         lv_obj_add_style(obj, &styles->arc_line, 0);
337         lv_obj_add_style(obj, &styles->dim, LV_PART_KNOB);
338         lv_obj_add_style(obj, &styles->arc_knob, LV_PART_KNOB);
339     }
340 #endif
341 
342 #if LV_USE_METER
343     else if(lv_obj_check_type(obj, &lv_meter_class)) {
344         lv_obj_add_style(obj, &styles->light, 0);
345     }
346 #endif
347 
348 #if LV_USE_TEXTAREA
349     else if(lv_obj_check_type(obj, &lv_textarea_class)) {
350         lv_obj_add_style(obj, &styles->white, 0);
351         lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);
352         lv_obj_add_style(obj, &styles->ta_cursor, LV_PART_CURSOR);
353     }
354 #endif
355 
356 #if LV_USE_CALENDAR
357     else if(lv_obj_check_type(obj, &lv_calendar_class)) {
358         lv_obj_add_style(obj, &styles->light, 0);
359         lv_obj_add_style(obj, &styles->light, LV_PART_ITEMS | LV_STATE_PRESSED);
360     }
361 #endif
362 
363 #if LV_USE_KEYBOARD
364     else if(lv_obj_check_type(obj, &lv_keyboard_class)) {
365         lv_obj_add_style(obj, &styles->scr, 0);
366         lv_obj_add_style(obj, &styles->white, LV_PART_ITEMS);
367         lv_obj_add_style(obj, &styles->light, LV_PART_ITEMS | LV_STATE_CHECKED);
368     }
369 #endif
370 #if LV_USE_LIST
371     else if(lv_obj_check_type(obj, &lv_list_class)) {
372         lv_obj_add_style(obj, &styles->light, 0);
373         lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);
374         return;
375     }
376     else if(lv_obj_check_type(obj, &lv_list_text_class)) {
377 
378     }
379     else if(lv_obj_check_type(obj, &lv_list_btn_class)) {
380         lv_obj_add_style(obj, &styles->dark, 0);
381 
382     }
383 #endif
384 #if LV_USE_MSGBOX
385     else if(lv_obj_check_type(obj, &lv_msgbox_class)) {
386         lv_obj_add_style(obj, &styles->light, 0);
387         return;
388     }
389 #endif
390 #if LV_USE_SPINBOX
391     else if(lv_obj_check_type(obj, &lv_spinbox_class)) {
392         lv_obj_add_style(obj, &styles->light, 0);
393         lv_obj_add_style(obj, &styles->dark, LV_PART_CURSOR);
394     }
395 #endif
396 #if LV_USE_TILEVIEW
397     else if(lv_obj_check_type(obj, &lv_tileview_class)) {
398         lv_obj_add_style(obj, &styles->scr, 0);
399         lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);
400     }
401     else if(lv_obj_check_type(obj, &lv_tileview_tile_class)) {
402         lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);
403     }
404 #endif
405 
406 #if LV_USE_COLORWHEEL
407     else if(lv_obj_check_type(obj, &lv_colorwheel_class)) {
408         lv_obj_add_style(obj, &styles->light, 0);
409         lv_obj_add_style(obj, &styles->light, LV_PART_KNOB);
410     }
411 #endif
412 
413 #if LV_USE_LED
414     else if(lv_obj_check_type(obj, &lv_led_class)) {
415         lv_obj_add_style(obj, &styles->light, 0);
416     }
417 #endif
418 }
419 
420 /**********************
421  *   STATIC FUNCTIONS
422  **********************/
423 
style_init_reset(lv_style_t * style)424 static void style_init_reset(lv_style_t * style)
425 {
426     if(inited) {
427         lv_style_reset(style);
428     }
429     else {
430         lv_style_init(style);
431     }
432 }
433 
434 #endif
435