1 /**
2  * @file lv_cb.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_cb.h"
10 #if LV_USE_CB != 0
11 
12 #include "../lv_core/lv_group.h"
13 #include "../lv_themes/lv_theme.h"
14 
15 /*********************
16  *      DEFINES
17  *********************/
18 
19 /**********************
20  *      TYPEDEFS
21  **********************/
22 
23 /**********************
24  *  STATIC PROTOTYPES
25  **********************/
26 static bool lv_cb_design(lv_obj_t * cb, const lv_area_t * mask, lv_design_mode_t mode);
27 static bool lv_bullet_design(lv_obj_t * bullet, const lv_area_t * mask, lv_design_mode_t mode);
28 static lv_res_t lv_cb_signal(lv_obj_t * cb, lv_signal_t sign, void * param);
29 
30 /**********************
31  *  STATIC VARIABLES
32  **********************/
33 static lv_design_cb_t ancestor_bg_design;
34 static lv_design_cb_t ancestor_bullet_design;
35 static lv_signal_cb_t ancestor_signal;
36 
37 /**********************
38  *      MACROS
39  **********************/
40 
41 /**********************
42  *   GLOBAL FUNCTIONS
43  **********************/
44 
45 /**
46  * Create a check box objects
47  * @param par pointer to an object, it will be the parent of the new check box
48  * @param copy pointer to a check box object, if not NULL then the new object will be copied from it
49  * @return pointer to the created check box
50  */
lv_cb_create(lv_obj_t * par,const lv_obj_t * copy)51 lv_obj_t * lv_cb_create(lv_obj_t * par, const lv_obj_t * copy)
52 {
53 
54     LV_LOG_TRACE("check box create started");
55 
56     /*Create the ancestor basic object*/
57     lv_obj_t * new_cb = lv_btn_create(par, copy);
58     lv_mem_assert(new_cb);
59     if(new_cb == NULL) return NULL;
60 
61     if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_cb);
62     if(ancestor_bg_design == NULL) ancestor_bg_design = lv_obj_get_design_cb(new_cb);
63 
64     lv_cb_ext_t * ext = lv_obj_allocate_ext_attr(new_cb, sizeof(lv_cb_ext_t));
65     lv_mem_assert(ext);
66     if(ext == NULL) return NULL;
67 
68     ext->bullet = NULL;
69     ext->label  = NULL;
70 
71     lv_obj_set_signal_cb(new_cb, lv_cb_signal);
72     lv_obj_set_design_cb(new_cb, lv_cb_design);
73 
74     /*Init the new checkbox object*/
75     if(copy == NULL) {
76         ext->bullet = lv_btn_create(new_cb, NULL);
77         if(ancestor_bullet_design == NULL) ancestor_bullet_design = lv_obj_get_design_cb(ext->bullet);
78         lv_obj_set_click(ext->bullet, false);
79 
80         ext->label = lv_label_create(new_cb, NULL);
81 
82         lv_cb_set_text(new_cb, "Check box");
83         lv_btn_set_layout(new_cb, LV_LAYOUT_ROW_M);
84         lv_btn_set_fit(new_cb, LV_FIT_TIGHT);
85         lv_btn_set_toggle(new_cb, true);
86         lv_obj_set_protect(new_cb, LV_PROTECT_PRESS_LOST);
87 
88         /*Set the default styles*/
89         lv_theme_t * th = lv_theme_get_current();
90         if(th) {
91             lv_cb_set_style(new_cb, LV_CB_STYLE_BG, th->style.cb.bg);
92             lv_cb_set_style(new_cb, LV_CB_STYLE_BOX_REL, th->style.cb.box.rel);
93             lv_cb_set_style(new_cb, LV_CB_STYLE_BOX_PR, th->style.cb.box.pr);
94             lv_cb_set_style(new_cb, LV_CB_STYLE_BOX_TGL_REL, th->style.cb.box.tgl_rel);
95             lv_cb_set_style(new_cb, LV_CB_STYLE_BOX_TGL_PR, th->style.cb.box.tgl_pr);
96             lv_cb_set_style(new_cb, LV_CB_STYLE_BOX_INA, th->style.cb.box.ina);
97         } else {
98             lv_cb_set_style(new_cb, LV_CB_STYLE_BG, &lv_style_transp);
99             lv_cb_set_style(new_cb, LV_CB_STYLE_BOX_REL, &lv_style_pretty);
100         }
101     } else {
102         lv_cb_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
103         ext->bullet            = lv_btn_create(new_cb, copy_ext->bullet);
104         ext->label             = lv_label_create(new_cb, copy_ext->label);
105 
106         /*Refresh the style with new signal function*/
107         lv_obj_refresh_style(new_cb);
108     }
109 
110     lv_obj_set_design_cb(ext->bullet, lv_bullet_design);
111 
112     LV_LOG_INFO("check box created");
113 
114     return new_cb;
115 }
116 
117 /*=====================
118  * Setter functions
119  *====================*/
120 
121 /**
122  * Set the text of a check box. `txt` will be copied and may be deallocated
123  * after this function returns.
124  * @param cb pointer to a check box
125  * @param txt the text of the check box. NULL to refresh with the current text.
126  */
lv_cb_set_text(lv_obj_t * cb,const char * txt)127 void lv_cb_set_text(lv_obj_t * cb, const char * txt)
128 {
129     lv_cb_ext_t * ext = lv_obj_get_ext_attr(cb);
130     lv_label_set_text(ext->label, txt);
131 }
132 
133 /**
134  * Set the text of a check box. `txt` must not be deallocated during the life
135  * of this checkbox.
136  * @param cb pointer to a check box
137  * @param txt the text of the check box. NULL to refresh with the current text.
138  */
lv_cb_set_static_text(lv_obj_t * cb,const char * txt)139 void lv_cb_set_static_text(lv_obj_t * cb, const char * txt)
140 {
141     lv_cb_ext_t * ext = lv_obj_get_ext_attr(cb);
142     lv_label_set_static_text(ext->label, txt);
143 }
144 
145 /**
146  * Set a style of a check box
147  * @param cb pointer to check box object
148  * @param type which style should be set
149  * @param style pointer to a style
150  *  */
lv_cb_set_style(lv_obj_t * cb,lv_cb_style_t type,const lv_style_t * style)151 void lv_cb_set_style(lv_obj_t * cb, lv_cb_style_t type, const lv_style_t * style)
152 {
153     lv_cb_ext_t * ext = lv_obj_get_ext_attr(cb);
154 
155     switch(type) {
156         case LV_CB_STYLE_BG:
157             lv_btn_set_style(cb, LV_BTN_STYLE_REL, style);
158             lv_btn_set_style(cb, LV_BTN_STYLE_PR, style);
159             lv_btn_set_style(cb, LV_BTN_STYLE_TGL_REL, style);
160             lv_btn_set_style(cb, LV_BTN_STYLE_TGL_PR, style);
161             lv_btn_set_style(cb, LV_BTN_STYLE_INA, style);
162             break;
163         case LV_CB_STYLE_BOX_REL: lv_btn_set_style(ext->bullet, LV_BTN_STYLE_REL, style); break;
164         case LV_CB_STYLE_BOX_PR: lv_btn_set_style(ext->bullet, LV_BTN_STYLE_PR, style); break;
165         case LV_CB_STYLE_BOX_TGL_REL: lv_btn_set_style(ext->bullet, LV_BTN_STYLE_TGL_REL, style); break;
166         case LV_CB_STYLE_BOX_TGL_PR: lv_btn_set_style(ext->bullet, LV_BTN_STYLE_TGL_PR, style); break;
167         case LV_CB_STYLE_BOX_INA: lv_btn_set_style(ext->bullet, LV_BTN_STYLE_INA, style); break;
168     }
169 }
170 
171 /*=====================
172  * Getter functions
173  *====================*/
174 
175 /**
176  * Get the text of a check box
177  * @param cb pointer to check box object
178  * @return pointer to the text of the check box
179  */
lv_cb_get_text(const lv_obj_t * cb)180 const char * lv_cb_get_text(const lv_obj_t * cb)
181 {
182     lv_cb_ext_t * ext = lv_obj_get_ext_attr(cb);
183     return lv_label_get_text(ext->label);
184 }
185 
186 /**
187  * Get a style of a button
188  * @param cb pointer to check box object
189  * @param type which style should be get
190  * @return style pointer to the style
191  *  */
lv_cb_get_style(const lv_obj_t * cb,lv_cb_style_t type)192 const lv_style_t * lv_cb_get_style(const lv_obj_t * cb, lv_cb_style_t type)
193 {
194     const lv_style_t * style = NULL;
195     lv_cb_ext_t * ext        = lv_obj_get_ext_attr(cb);
196 
197     switch(type) {
198         case LV_CB_STYLE_BOX_REL: style = lv_btn_get_style(ext->bullet, LV_BTN_STYLE_REL); break;
199         case LV_CB_STYLE_BOX_PR: style = lv_btn_get_style(ext->bullet, LV_BTN_STYLE_PR); break;
200         case LV_CB_STYLE_BOX_TGL_REL: style = lv_btn_get_style(ext->bullet, LV_BTN_STYLE_TGL_REL); break;
201         case LV_CB_STYLE_BOX_TGL_PR: style = lv_btn_get_style(ext->bullet, LV_BTN_STYLE_TGL_PR); break;
202         case LV_CB_STYLE_BOX_INA: style = lv_btn_get_style(ext->bullet, LV_BTN_STYLE_INA); break;
203         default: style = NULL; break;
204     }
205 
206     return style;
207 }
208 
209 /**********************
210  *   STATIC FUNCTIONS
211  **********************/
212 
213 /**
214  * Handle the drawing related tasks of the check boxes
215  * @param cb pointer to an object
216  * @param mask the object will be drawn only in this area
217  * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
218  *                                  (return 'true' if yes)
219  *             LV_DESIGN_DRAW: draw the object (always return 'true')
220  *             LV_DESIGN_DRAW_POST: drawing after every children are drawn
221  * @param return true/false, depends on 'mode'
222  */
lv_cb_design(lv_obj_t * cb,const lv_area_t * mask,lv_design_mode_t mode)223 static bool lv_cb_design(lv_obj_t * cb, const lv_area_t * mask, lv_design_mode_t mode)
224 {
225     bool result = true;
226 
227     if(mode == LV_DESIGN_COVER_CHK) {
228         /*Return false if the object is not covers the mask_p area*/
229         result = ancestor_bg_design(cb, mask, mode);
230     } else if(mode == LV_DESIGN_DRAW_MAIN || mode == LV_DESIGN_DRAW_POST) {
231         lv_cb_ext_t * cb_ext      = lv_obj_get_ext_attr(cb);
232         lv_btn_ext_t * bullet_ext = lv_obj_get_ext_attr(cb_ext->bullet);
233 
234         /*Be sure the state of the bullet is the same as the parent button*/
235         bullet_ext->state = cb_ext->bg_btn.state;
236 
237         result = ancestor_bg_design(cb, mask, mode);
238 
239     } else {
240         result = ancestor_bg_design(cb, mask, mode);
241     }
242 
243     return result;
244 }
245 
246 /**
247  * Handle the drawing related tasks of the check boxes
248  * @param bullet pointer to an object
249  * @param mask the object will be drawn only in this area
250  * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
251  *                                  (return 'true' if yes)
252  *             LV_DESIGN_DRAW: draw the object (always return 'true')
253  *             LV_DESIGN_DRAW_POST: drawing after every children are drawn
254  * @param return true/false, depends on 'mode'
255  */
lv_bullet_design(lv_obj_t * bullet,const lv_area_t * mask,lv_design_mode_t mode)256 static bool lv_bullet_design(lv_obj_t * bullet, const lv_area_t * mask, lv_design_mode_t mode)
257 {
258     if(mode == LV_DESIGN_COVER_CHK) {
259         return ancestor_bullet_design(bullet, mask, mode);
260     } else if(mode == LV_DESIGN_DRAW_MAIN) {
261 #if LV_USE_GROUP
262         /* If the check box is the active in a group and
263          * the background is not visible (transparent)
264          * then activate the style of the bullet*/
265         const lv_style_t * style_ori  = lv_obj_get_style(bullet);
266         lv_obj_t * bg                 = lv_obj_get_parent(bullet);
267         const lv_style_t * style_page = lv_obj_get_style(bg);
268         lv_group_t * g                = lv_obj_get_group(bg);
269         if(style_page->body.opa == LV_OPA_TRANSP) { /*Is the Background visible?*/
270             if(lv_group_get_focused(g) == bg) {
271                 lv_style_t * style_mod;
272                 style_mod       = lv_group_mod_style(g, style_ori);
273                 bullet->style_p = style_mod; /*Temporally change the style to the activated */
274             }
275         }
276 #endif
277         ancestor_bullet_design(bullet, mask, mode);
278 
279 #if LV_USE_GROUP
280         bullet->style_p = style_ori; /*Revert the style*/
281 #endif
282     } else if(mode == LV_DESIGN_DRAW_POST) {
283         ancestor_bullet_design(bullet, mask, mode);
284     }
285 
286     return true;
287 }
288 
289 /**
290  * Signal function of the check box
291  * @param cb pointer to a check box object
292  * @param sign a signal type from lv_signal_t enum
293  * @param param pointer to a signal specific variable
294  * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
295  */
lv_cb_signal(lv_obj_t * cb,lv_signal_t sign,void * param)296 static lv_res_t lv_cb_signal(lv_obj_t * cb, lv_signal_t sign, void * param)
297 {
298     lv_res_t res;
299 
300     /* Include the ancient signal function */
301     res = ancestor_signal(cb, sign, param);
302     if(res != LV_RES_OK) return res;
303 
304     lv_cb_ext_t * ext = lv_obj_get_ext_attr(cb);
305 
306     if(sign == LV_SIGNAL_STYLE_CHG) {
307         const lv_style_t * label_style = lv_label_get_style(ext->label, LV_LABEL_STYLE_MAIN);
308         lv_obj_set_size(ext->bullet, lv_font_get_line_height(label_style->text.font),
309                         lv_font_get_line_height(label_style->text.font));
310         lv_btn_set_state(ext->bullet, lv_btn_get_state(cb));
311     } else if(sign == LV_SIGNAL_PRESSED || sign == LV_SIGNAL_RELEASED || sign == LV_SIGNAL_PRESS_LOST) {
312         lv_btn_set_state(ext->bullet, lv_btn_get_state(cb));
313     } else if(sign == LV_SIGNAL_CONTROL) {
314         char c = *((char *)param);
315         if(c == LV_KEY_RIGHT || c == LV_KEY_DOWN || c == LV_KEY_LEFT || c == LV_KEY_UP) {
316             /*Follow the backgrounds state with the bullet*/
317             lv_btn_set_state(ext->bullet, lv_btn_get_state(cb));
318         }
319     } else if(sign == LV_SIGNAL_GET_TYPE) {
320         lv_obj_type_t * buf = param;
321         uint8_t i;
322         for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
323             if(buf->type[i] == NULL) break;
324         }
325         buf->type[i] = "lv_cb";
326     }
327 
328     return res;
329 }
330 
331 #endif
332