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