Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_objx/lv_mbox.c
1476 views
1
/*
2
* Copyright (c) 2019 CTCaer
3
*
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms and conditions of the GNU General Public License,
6
* version 2, as published by the Free Software Foundation.
7
*
8
* This program is distributed in the hope it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11
* more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15
*/
16
17
/**
18
* @file lv_mbox.c
19
*
20
*/
21
22
23
/*********************
24
* INCLUDES
25
*********************/
26
#include "lv_mbox.h"
27
#if USE_LV_MBOX != 0
28
29
#include "../lv_core/lv_group.h"
30
#include "../lv_themes/lv_theme.h"
31
#include "../lv_misc/lv_anim.h"
32
#include "../lv_misc/lv_math.h"
33
34
/*********************
35
* DEFINES
36
*********************/
37
38
#if USE_LV_ANIMATION
39
# ifndef LV_MBOX_CLOSE_ANIM_TIME
40
# define LV_MBOX_CLOSE_ANIM_TIME 200 /*List close animation time) */
41
# endif
42
#else
43
# undef LV_MBOX_CLOSE_ANIM_TIME
44
# define LV_MBOX_CLOSE_ANIM_TIME 0 /*No animations*/
45
#endif
46
47
/**********************
48
* TYPEDEFS
49
**********************/
50
51
/**********************
52
* STATIC PROTOTYPES
53
**********************/
54
static lv_res_t lv_mbox_signal(lv_obj_t * mbox, lv_signal_t sign, void * param);
55
static void mbox_realign(lv_obj_t * mbox);
56
static lv_res_t lv_mbox_close_action(lv_obj_t * btn, const char * txt);
57
#if USE_LV_ANIMATION
58
static void lv_mbox_close_end_cb(lv_obj_t * mbox);
59
#endif
60
61
/**********************
62
* STATIC VARIABLES
63
**********************/
64
static lv_signal_func_t ancestor_signal;
65
66
/**********************
67
* MACROS
68
**********************/
69
70
/**********************
71
* GLOBAL FUNCTIONS
72
**********************/
73
74
/**
75
* Create a message box objects
76
* @param par pointer to an object, it will be the parent of the new message box
77
* @param copy pointer to a message box object, if not NULL then the new object will be copied from it
78
* @return pointer to the created message box
79
*/
80
lv_obj_t * lv_mbox_create(lv_obj_t * par, const lv_obj_t * copy)
81
{
82
LV_LOG_TRACE("mesasge box create started");
83
84
/*Create the ancestor message box*/
85
lv_obj_t * new_mbox = lv_cont_create(par, copy);
86
lv_mem_assert(new_mbox);
87
if(new_mbox == NULL) return NULL;
88
89
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_mbox);
90
91
/*Allocate the message box type specific extended data*/
92
lv_mbox_ext_t * ext = lv_obj_allocate_ext_attr(new_mbox, sizeof(lv_mbox_ext_t));
93
lv_mem_assert(ext);
94
if(ext == NULL) return NULL;
95
96
ext->text = NULL;
97
ext->btnm = NULL;
98
ext->anim_time = LV_MBOX_CLOSE_ANIM_TIME;
99
100
/*The signal and design functions are not copied so set them here*/
101
lv_obj_set_signal_func(new_mbox, lv_mbox_signal);
102
103
/*Init the new message box message box*/
104
if(copy == NULL) {
105
ext->text = lv_label_create(new_mbox, NULL);
106
lv_label_set_align(ext->text, LV_LABEL_ALIGN_CENTER);
107
lv_label_set_long_mode(ext->text, LV_LABEL_LONG_BREAK);
108
lv_label_set_text(ext->text, "Message");
109
110
lv_cont_set_layout(new_mbox, LV_LAYOUT_COL_M);
111
lv_cont_set_fit(new_mbox, false, true);
112
lv_obj_set_width(new_mbox, LV_HOR_RES / 2);
113
lv_obj_align(new_mbox, NULL, LV_ALIGN_CENTER, 0, 0);
114
115
/*Set the default styles*/
116
lv_theme_t * th = lv_theme_get_current();
117
if(th) {
118
lv_mbox_set_style(new_mbox, LV_MBOX_STYLE_BG, th->mbox.bg);
119
} else {
120
lv_mbox_set_style(new_mbox, LV_MBOX_STYLE_BG, &lv_style_pretty);
121
}
122
123
}
124
/*Copy an existing message box*/
125
else {
126
lv_mbox_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
127
128
ext->text = lv_label_create(new_mbox, copy_ext->text);
129
130
/*Copy the buttons and the label on them*/
131
if(copy_ext->btnm) ext->btnm = lv_btnm_create(new_mbox, copy_ext->btnm);
132
133
/*Refresh the style with new signal function*/
134
lv_obj_refresh_style(new_mbox);
135
}
136
137
138
LV_LOG_INFO("mesasge box created");
139
140
return new_mbox;
141
}
142
143
/*======================
144
* Add/remove functions
145
*=====================*/
146
147
/**
148
* Add button to the message box
149
* @param mbox pointer to message box object
150
* @param btn_map button descriptor (button matrix map).
151
* E.g. a const char *txt[] = {"ok", "close", ""} (Can not be local variable)
152
* @param action a function which will be called when a button is released
153
*/
154
void lv_mbox_add_btns(lv_obj_t * mbox, const char ** btn_map, lv_btnm_action_t action)
155
{
156
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
157
158
/*Create a button matrix if not exists yet*/
159
if(ext->btnm == NULL) {
160
ext->btnm = lv_btnm_create(mbox, NULL);
161
162
/*Set the default styles*/
163
lv_theme_t * th = lv_theme_get_current();
164
if(th) {
165
lv_mbox_set_style(mbox, LV_MBOX_STYLE_BTN_BG, th->mbox.btn.bg);
166
lv_mbox_set_style(mbox, LV_MBOX_STYLE_BTN_REL, th->mbox.btn.rel);
167
lv_mbox_set_style(mbox, LV_MBOX_STYLE_BTN_PR, th->mbox.btn.pr);
168
} else {
169
lv_btnm_set_style(ext->btnm, LV_BTNM_STYLE_BG, &lv_style_transp_fit);
170
}
171
}
172
173
lv_btnm_set_map(ext->btnm, btn_map);
174
if(action == NULL) lv_btnm_set_action(ext->btnm, lv_mbox_close_action); /*Set a default action anyway*/
175
else lv_btnm_set_action(ext->btnm, action);
176
177
mbox_realign(mbox);
178
}
179
180
/*=====================
181
* Setter functions
182
*====================*/
183
184
/**
185
* Set the text of the message box
186
* @param mbox pointer to a message box
187
* @param txt a '\0' terminated character string which will be the message box text
188
*/
189
void lv_mbox_set_text(lv_obj_t * mbox, const char * txt)
190
{
191
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
192
lv_label_set_text(ext->text, txt);
193
194
mbox_realign(mbox);
195
}
196
197
198
/**
199
* Stop the action to call when button is released
200
* @param pointer to an 'lv_btnm_action_t' action. In the action you need to use `lv_mbox_get_from_btn()` to get the `mbox`.
201
* @param pointer to an 'lv_btnm_action_t' action
202
*/
203
void lv_mbox_set_action(lv_obj_t * mbox, lv_btnm_action_t action)
204
{
205
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
206
lv_btnm_set_action(ext->btnm, action);
207
}
208
209
210
/**
211
* Set animation duration
212
* @param mbox pointer to a message box object
213
* @param anim_time animation length in milliseconds (0: no animation)
214
*/
215
void lv_mbox_set_anim_time(lv_obj_t * mbox, uint16_t anim_time)
216
{
217
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
218
#if USE_LV_ANIMATION == 0
219
anim_time = 0;
220
#endif
221
222
ext->anim_time = anim_time;
223
}
224
225
/**
226
* Automatically delete the message box after a given time
227
* @param mbox pointer to a message box object
228
* @param delay a time (in milliseconds) to wait before delete the message box
229
*/
230
void lv_mbox_start_auto_close(lv_obj_t * mbox, uint16_t delay)
231
{
232
#if USE_LV_ANIMATION
233
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
234
235
if(ext->anim_time != 0) {
236
/*Add shrinking animations*/
237
lv_obj_animate(mbox, LV_ANIM_GROW_H | LV_ANIM_OUT, ext->anim_time, delay, NULL);
238
lv_obj_animate(mbox, LV_ANIM_GROW_V | LV_ANIM_OUT, ext->anim_time, delay, lv_mbox_close_end_cb);
239
240
/*Disable fit to let shrinking work*/
241
lv_cont_set_fit(mbox, false, false);
242
} else {
243
lv_obj_animate(mbox, LV_ANIM_NONE, ext->anim_time, delay, lv_mbox_close_end_cb);
244
}
245
#else
246
(void)delay; /*Unused*/
247
lv_obj_del(mbox);
248
#endif
249
}
250
251
/**
252
* Stop the auto. closing of message box
253
* @param mbox pointer to a message box object
254
*/
255
void lv_mbox_stop_auto_close(lv_obj_t * mbox)
256
{
257
#if USE_LV_ANIMATION
258
lv_anim_del(mbox, NULL);
259
#else
260
(void)mbox; /*Unused*/
261
#endif
262
}
263
264
/**
265
* Set a style of a message box
266
* @param mbox pointer to a message box object
267
* @param type which style should be set
268
* @param style pointer to a style
269
*/
270
void lv_mbox_set_style(lv_obj_t * mbox, lv_mbox_style_t type, lv_style_t * style)
271
{
272
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
273
274
switch(type) {
275
case LV_MBOX_STYLE_BG:
276
lv_obj_set_style(mbox, style);
277
break;
278
case LV_MBOX_STYLE_BTN_BG:
279
lv_btnm_set_style(ext->btnm, LV_BTNM_STYLE_BG, style);
280
break;
281
case LV_MBOX_STYLE_BTN_REL:
282
lv_btnm_set_style(ext->btnm, LV_BTNM_STYLE_BTN_REL, style);
283
break;
284
case LV_MBOX_STYLE_BTN_PR:
285
lv_btnm_set_style(ext->btnm, LV_BTNM_STYLE_BTN_PR, style);
286
break;
287
case LV_MBOX_STYLE_BTN_TGL_REL:
288
lv_btnm_set_style(ext->btnm, LV_BTNM_STYLE_BTN_TGL_REL, style);
289
break;
290
case LV_MBOX_STYLE_BTN_TGL_PR:
291
lv_btnm_set_style(ext->btnm, LV_BTNM_STYLE_BTN_TGL_PR, style);
292
break;
293
case LV_MBOX_STYLE_BTN_INA:
294
lv_btnm_set_style(ext->btnm, LV_BTNM_STYLE_BTN_INA, style);
295
break;
296
}
297
298
mbox_realign(mbox);
299
300
}
301
302
/**
303
* Set whether recoloring is enabled
304
* @param btnm pointer to button matrix object
305
* @param en whether recoloring is enabled
306
*/
307
void lv_mbox_set_recolor(lv_obj_t * mbox, bool en)
308
{
309
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
310
311
if(ext->btnm)
312
lv_btnm_set_recolor(ext->btnm, en);
313
}
314
315
void lv_mbox_set_recolor_text(lv_obj_t * mbox, bool en)
316
{
317
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
318
319
if (ext->text)
320
lv_label_set_recolor(ext->text, en);
321
}
322
323
/*=====================
324
* Getter functions
325
*====================*/
326
327
/**
328
* Get the text of the message box
329
* @param mbox pointer to a message box object
330
* @return pointer to the text of the message box
331
*/
332
const char * lv_mbox_get_text(const lv_obj_t * mbox)
333
{
334
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
335
336
return lv_label_get_text(ext->text);
337
}
338
339
/**
340
* Get the message box object from one of its button.
341
* It is useful in the button release actions where only the button is known
342
* @param btn pointer to a button of a message box
343
* @return pointer to the button's message box
344
*/
345
lv_obj_t * lv_mbox_get_from_btn(const lv_obj_t * btn)
346
{
347
lv_obj_t * mbox = lv_obj_get_parent(btn);
348
349
return mbox;
350
}
351
352
/**
353
* Get the animation duration (close animation time)
354
* @param mbox pointer to a message box object
355
* @return animation length in milliseconds (0: no animation)
356
*/
357
uint16_t lv_mbox_get_anim_time(const lv_obj_t * mbox)
358
{
359
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
360
return ext->anim_time;
361
}
362
363
/**
364
* Get a style of a message box
365
* @param mbox pointer to a message box object
366
* @param type which style should be get
367
* @return style pointer to a style
368
*/
369
lv_style_t * lv_mbox_get_style(const lv_obj_t * mbox, lv_mbox_style_t type)
370
{
371
lv_style_t * style = NULL;
372
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
373
374
switch(type) {
375
case LV_MBOX_STYLE_BG:
376
style = lv_obj_get_style(mbox);
377
break;
378
case LV_MBOX_STYLE_BTN_BG:
379
style = lv_btnm_get_style(ext->btnm, LV_BTNM_STYLE_BG);
380
break;
381
case LV_MBOX_STYLE_BTN_REL:
382
style = lv_btnm_get_style(ext->btnm, LV_BTNM_STYLE_BTN_REL);
383
break;
384
case LV_MBOX_STYLE_BTN_PR:
385
style = lv_btnm_get_style(ext->btnm, LV_BTNM_STYLE_BTN_PR);
386
break;
387
case LV_MBOX_STYLE_BTN_TGL_REL:
388
style = lv_btnm_get_style(ext->btnm, LV_BTNM_STYLE_BTN_TGL_REL);
389
break;
390
case LV_MBOX_STYLE_BTN_TGL_PR:
391
style = lv_btnm_get_style(ext->btnm, LV_BTNM_STYLE_BTN_TGL_PR);
392
break;
393
case LV_MBOX_STYLE_BTN_INA:
394
style = lv_btnm_get_style(ext->btnm, LV_BTNM_STYLE_BTN_INA);
395
break;
396
default:
397
style = NULL;
398
break;
399
}
400
401
return style;
402
}
403
404
/**
405
* Get whether recoloring is enabled
406
* @param btnm pointer to button matrix object
407
* @return whether recoloring is enabled
408
*/
409
bool lv_mbox_get_recolor(const lv_obj_t * mbox)
410
{
411
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
412
413
if(!ext->btnm)
414
return false;
415
416
return lv_btnm_get_recolor(ext->btnm);
417
}
418
419
420
/**********************
421
* STATIC FUNCTIONS
422
**********************/
423
424
/**
425
* Signal function of the message box
426
* @param mbox pointer to a message box object
427
* @param sign a signal type from lv_signal_t enum
428
* @param param pointer to a signal specific variable
429
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
430
*/
431
static lv_res_t lv_mbox_signal(lv_obj_t * mbox, lv_signal_t sign, void * param)
432
{
433
lv_res_t res;
434
435
/*Translate LV_GROUP_KEY_UP/DOWN to LV_GROUP_KEY_LEFT/RIGHT */
436
char c_trans = 0;
437
if(sign == LV_SIGNAL_CONTROLL) {
438
c_trans = *((char *)param);
439
if(c_trans == LV_GROUP_KEY_DOWN) c_trans = LV_GROUP_KEY_LEFT;
440
if(c_trans == LV_GROUP_KEY_UP) c_trans = LV_GROUP_KEY_RIGHT;
441
442
param = &c_trans;
443
}
444
445
/* Include the ancient signal function */
446
res = ancestor_signal(mbox, sign, param);
447
if(res != LV_RES_OK) return res;
448
449
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
450
if(sign == LV_SIGNAL_CORD_CHG) {
451
if(lv_obj_get_width(mbox) != lv_area_get_width(param)) {
452
mbox_realign(mbox);
453
}
454
} else if(sign == LV_SIGNAL_STYLE_CHG) {
455
mbox_realign(mbox);
456
457
} else if(sign == LV_SIGNAL_FOCUS || sign == LV_SIGNAL_DEFOCUS ||
458
sign == LV_SIGNAL_CONTROLL || sign == LV_SIGNAL_GET_EDITABLE) {
459
if(ext->btnm) {
460
ext->btnm->signal_func(ext->btnm, sign, param);
461
}
462
463
/* The button matrix with ENCODER input supposes it's in a group but in this case it isn't (Only the message box's container)
464
* So so some actions here instead*/
465
if(sign == LV_SIGNAL_FOCUS) {
466
#if USE_LV_GROUP
467
lv_indev_t * indev = lv_indev_get_act();
468
lv_hal_indev_type_t indev_type = lv_indev_get_type(indev);
469
if(indev_type == LV_INDEV_TYPE_ENCODER) {
470
/*In navigation mode don't select any button but in edit mode select the fist*/
471
lv_btnm_ext_t * btnm_ext = lv_obj_get_ext_attr(ext->btnm);
472
if(lv_group_get_editing(lv_obj_get_group(mbox))) btnm_ext->btn_id_pr = 0;
473
else btnm_ext->btn_id_pr = LV_BTNM_PR_NONE;
474
}
475
#endif
476
}
477
478
479
} else if(sign == LV_SIGNAL_GET_TYPE) {
480
lv_obj_type_t * buf = param;
481
uint8_t i;
482
for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
483
if(buf->type[i] == NULL) break;
484
}
485
buf->type[i] = "lv_mbox";
486
}
487
488
return res;
489
}
490
491
/**
492
* Resize the button holder to fit
493
* @param mbox pointer to message box object
494
*/
495
static void mbox_realign(lv_obj_t * mbox)
496
{
497
lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox);
498
499
lv_style_t * style = lv_mbox_get_style(mbox, LV_MBOX_STYLE_BG);
500
lv_coord_t w = lv_obj_get_width(mbox) - 2 * style->body.padding.hor;
501
502
if(ext->text) {
503
lv_obj_set_width(ext->text, w);
504
}
505
506
if(ext->btnm) {
507
lv_style_t * btn_bg_style = lv_mbox_get_style(mbox, LV_MBOX_STYLE_BTN_BG);
508
lv_style_t * btn_rel_style = lv_mbox_get_style(mbox, LV_MBOX_STYLE_BTN_REL);
509
lv_coord_t font_h = lv_font_get_height(btn_rel_style->text.font);
510
lv_obj_set_size(ext->btnm, w, font_h + 2 * btn_rel_style->body.padding.ver + 2 * btn_bg_style->body.padding.ver);
511
}
512
}
513
514
static lv_res_t lv_mbox_close_action(lv_obj_t * btn, const char * txt)
515
{
516
lv_obj_t * mbox = lv_mbox_get_from_btn(btn);
517
518
if(txt[0] != '\0') {
519
lv_mbox_start_auto_close(mbox, 0);
520
return LV_RES_INV;
521
}
522
523
return LV_RES_OK;
524
}
525
526
#if USE_LV_ANIMATION
527
static void lv_mbox_close_end_cb(lv_obj_t * mbox)
528
{
529
lv_obj_del(mbox);
530
}
531
#endif
532
#endif
533
534