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