Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_objx/lv_cb.c
1476 views
1
/**
2
* @file lv_cb.c
3
*
4
*/
5
6
/*********************
7
* INCLUDES
8
*********************/
9
#include "lv_cb.h"
10
#if USE_LV_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_func_t ancestor_bg_design;
34
static lv_design_func_t ancestor_bullet_design;
35
static lv_signal_func_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
*/
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_func(new_cb);
62
if(ancestor_bg_design == NULL) ancestor_bg_design = lv_obj_get_design_func(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_func(new_cb, lv_cb_signal);
72
lv_obj_set_design_func(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_func(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, true, true);
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->cb.bg);
92
lv_cb_set_style(new_cb, LV_CB_STYLE_BOX_REL, th->cb.box.rel);
93
lv_cb_set_style(new_cb, LV_CB_STYLE_BOX_PR, th->cb.box.pr);
94
lv_cb_set_style(new_cb, LV_CB_STYLE_BOX_TGL_REL, th->cb.box.tgl_rel);
95
lv_cb_set_style(new_cb, LV_CB_STYLE_BOX_TGL_PR, th->cb.box.tgl_pr);
96
lv_cb_set_style(new_cb, LV_CB_STYLE_BOX_INA, th->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_func(ext->bullet, lv_bullet_design);
111
112
113
LV_LOG_INFO("check box created");
114
115
return new_cb;
116
}
117
118
/*=====================
119
* Setter functions
120
*====================*/
121
122
/**
123
* Set the text of a check box
124
* @param cb pointer to a check box
125
* @param txt the text of the check box
126
*/
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 a style of a check box
135
* @param cb pointer to check box object
136
* @param type which style should be set
137
* @param style pointer to a style
138
* */
139
void lv_cb_set_style(lv_obj_t * cb, lv_cb_style_t type, lv_style_t * style)
140
{
141
lv_cb_ext_t * ext = lv_obj_get_ext_attr(cb);
142
143
switch(type) {
144
case LV_CB_STYLE_BG:
145
lv_btn_set_style(cb, LV_BTN_STYLE_REL, style);
146
lv_btn_set_style(cb, LV_BTN_STYLE_PR, style);
147
lv_btn_set_style(cb, LV_BTN_STYLE_TGL_REL, style);
148
lv_btn_set_style(cb, LV_BTN_STYLE_TGL_PR, style);
149
lv_btn_set_style(cb, LV_BTN_STYLE_INA, style);
150
break;
151
case LV_CB_STYLE_BOX_REL:
152
lv_btn_set_style(ext->bullet, LV_BTN_STYLE_REL, style);
153
break;
154
case LV_CB_STYLE_BOX_PR:
155
lv_btn_set_style(ext->bullet, LV_BTN_STYLE_PR, style);
156
break;
157
case LV_CB_STYLE_BOX_TGL_REL:
158
lv_btn_set_style(ext->bullet, LV_BTN_STYLE_TGL_REL, style);
159
break;
160
case LV_CB_STYLE_BOX_TGL_PR:
161
lv_btn_set_style(ext->bullet, LV_BTN_STYLE_TGL_PR, style);
162
break;
163
case LV_CB_STYLE_BOX_INA:
164
lv_btn_set_style(ext->bullet, LV_BTN_STYLE_INA, style);
165
break;
166
}
167
}
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
*/
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
/**
188
* Get a style of a button
189
* @param cb pointer to check box object
190
* @param type which style should be get
191
* @return style pointer to the style
192
* */
193
lv_style_t * lv_cb_get_style(const lv_obj_t * cb, lv_cb_style_t type)
194
{
195
lv_style_t * style = NULL;
196
lv_cb_ext_t * ext = lv_obj_get_ext_attr(cb);
197
198
switch(type) {
199
case LV_CB_STYLE_BOX_REL:
200
style = lv_btn_get_style(ext->bullet, LV_BTN_STYLE_REL);
201
break;
202
case LV_CB_STYLE_BOX_PR:
203
style = lv_btn_get_style(ext->bullet, LV_BTN_STYLE_PR);
204
break;
205
case LV_CB_STYLE_BOX_TGL_REL:
206
style = lv_btn_get_style(ext->bullet, LV_BTN_STYLE_TGL_REL);
207
break;
208
case LV_CB_STYLE_BOX_TGL_PR:
209
style = lv_btn_get_style(ext->bullet, LV_BTN_STYLE_TGL_PR);
210
break;
211
case LV_CB_STYLE_BOX_INA:
212
style = lv_btn_get_style(ext->bullet, LV_BTN_STYLE_INA);
213
break;
214
default:
215
style = NULL;
216
break;
217
}
218
219
return style;
220
}
221
222
/**********************
223
* STATIC FUNCTIONS
224
**********************/
225
226
/**
227
* Handle the drawing related tasks of the check boxes
228
* @param cb pointer to an object
229
* @param mask the object will be drawn only in this area
230
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
231
* (return 'true' if yes)
232
* LV_DESIGN_DRAW: draw the object (always return 'true')
233
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
234
* @param return true/false, depends on 'mode'
235
*/
236
static bool lv_cb_design(lv_obj_t * cb, const lv_area_t * mask, lv_design_mode_t mode)
237
{
238
bool result = true;
239
240
if(mode == LV_DESIGN_COVER_CHK) {
241
/*Return false if the object is not covers the mask_p area*/
242
result = ancestor_bg_design(cb, mask, mode);
243
} else if(mode == LV_DESIGN_DRAW_MAIN || mode == LV_DESIGN_DRAW_POST) {
244
lv_cb_ext_t * cb_ext = lv_obj_get_ext_attr(cb);
245
lv_btn_ext_t * bullet_ext = lv_obj_get_ext_attr(cb_ext->bullet);
246
247
/*Be sure the state of the bullet is the same as the parent button*/
248
bullet_ext->state = cb_ext->bg_btn.state;
249
250
result = ancestor_bg_design(cb, mask, mode);
251
252
} else {
253
result = ancestor_bg_design(cb, mask, mode);
254
}
255
256
return result;
257
}
258
259
/**
260
* Handle the drawing related tasks of the check boxes
261
* @param bullet pointer to an object
262
* @param mask the object will be drawn only in this area
263
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
264
* (return 'true' if yes)
265
* LV_DESIGN_DRAW: draw the object (always return 'true')
266
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
267
* @param return true/false, depends on 'mode'
268
*/
269
static bool lv_bullet_design(lv_obj_t * bullet, const lv_area_t * mask, lv_design_mode_t mode)
270
{
271
if(mode == LV_DESIGN_COVER_CHK) {
272
return ancestor_bullet_design(bullet, mask, mode);
273
} else if(mode == LV_DESIGN_DRAW_MAIN) {
274
#if USE_LV_GROUP
275
/* If the check box is the active in a group and
276
* the background is not visible (transparent or empty)
277
* then activate the style of the bullet*/
278
lv_style_t * style_ori = lv_obj_get_style(bullet);
279
lv_obj_t * bg = lv_obj_get_parent(bullet);
280
lv_style_t * style_page = lv_obj_get_style(bg);
281
lv_group_t * g = lv_obj_get_group(bg);
282
if(style_page->body.empty != 0 || style_page->body.opa == LV_OPA_TRANSP) { /*Background is visible?*/
283
if(lv_group_get_focused(g) == bg) {
284
lv_style_t * style_mod;
285
style_mod = lv_group_mod_style(g, style_ori);
286
bullet->style_p = style_mod; /*Temporally change the style to the activated */
287
}
288
}
289
#endif
290
ancestor_bullet_design(bullet, mask, mode);
291
292
#if USE_LV_GROUP
293
bullet->style_p = style_ori; /*Revert the style*/
294
#endif
295
} else if(mode == LV_DESIGN_DRAW_POST) {
296
ancestor_bullet_design(bullet, mask, mode);
297
}
298
299
return true;
300
}
301
302
303
/**
304
* Signal function of the check box
305
* @param cb pointer to a check box object
306
* @param sign a signal type from lv_signal_t enum
307
* @param param pointer to a signal specific variable
308
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
309
*/
310
static lv_res_t lv_cb_signal(lv_obj_t * cb, lv_signal_t sign, void * param)
311
{
312
lv_res_t res;
313
314
/* Include the ancient signal function */
315
res = ancestor_signal(cb, sign, param);
316
if(res != LV_RES_OK) return res;
317
318
lv_cb_ext_t * ext = lv_obj_get_ext_attr(cb);
319
320
if(sign == LV_SIGNAL_STYLE_CHG) {
321
lv_style_t * label_style = lv_label_get_style(ext->label);
322
lv_obj_set_size(ext->bullet, lv_font_get_height(label_style->text.font), lv_font_get_height(label_style->text.font));
323
lv_btn_set_state(ext->bullet, lv_btn_get_state(cb));
324
} else if(sign == LV_SIGNAL_PRESSED ||
325
sign == LV_SIGNAL_RELEASED ||
326
sign == LV_SIGNAL_PRESS_LOST) {
327
lv_btn_set_state(ext->bullet, lv_btn_get_state(cb));
328
} else if(sign == LV_SIGNAL_CONTROLL) {
329
char c = *((char *)param);
330
if(c == LV_GROUP_KEY_RIGHT || c == LV_GROUP_KEY_DOWN ||
331
c == LV_GROUP_KEY_LEFT || c == LV_GROUP_KEY_UP ||
332
c == LV_GROUP_KEY_ENTER) {
333
lv_btn_set_state(ext->bullet, lv_btn_get_state(cb));
334
}
335
} else if(sign == LV_SIGNAL_GET_TYPE) {
336
lv_obj_type_t * buf = param;
337
uint8_t i;
338
for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
339
if(buf->type[i] == NULL) break;
340
}
341
buf->type[i] = "lv_cb";
342
}
343
344
return res;
345
}
346
347
#endif
348
349