2016-07-19 14:05:27 +02:00
|
|
|
/**
|
|
|
|
* @file lv_cb.c
|
2018-06-19 09:49:58 +02:00
|
|
|
*
|
2016-07-19 14:05:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
2020-02-14 12:44:15 +01:00
|
|
|
#include "lv_checkbox.h"
|
2020-02-14 12:36:44 +01:00
|
|
|
#if LV_USE_CHECKBOX != 0
|
2016-07-19 14:05:27 +02:00
|
|
|
|
2020-06-08 13:10:43 +02:00
|
|
|
#include "../lv_misc/lv_debug.h"
|
2017-11-30 11:35:33 +01:00
|
|
|
#include "../lv_core/lv_group.h"
|
2017-11-16 15:32:33 +01:00
|
|
|
#include "../lv_themes/lv_theme.h"
|
2016-07-19 14:05:27 +02:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2020-02-14 12:36:44 +01:00
|
|
|
#define LV_OBJX_NAME "lv_checkbox"
|
2016-07-19 14:05:27 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* TYPEDEFS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC PROTOTYPES
|
|
|
|
**********************/
|
2020-02-14 12:36:44 +01:00
|
|
|
static lv_res_t lv_checkbox_signal(lv_obj_t * cb, lv_signal_t sign, void * param);
|
|
|
|
static lv_style_list_t * lv_checkbox_get_style(lv_obj_t * cb, uint8_t type);
|
2016-07-19 14:05:27 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
2019-02-26 09:25:46 +01:00
|
|
|
static lv_signal_cb_t ancestor_signal;
|
2017-07-20 12:26:34 +02:00
|
|
|
|
2016-07-19 14:05:27 +02:00
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a check box objects
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param par pointer to an object, it will be the parent of the new check box
|
|
|
|
* @param copy pointer to a check box object, if not NULL then the new object will be copied from it
|
2016-07-19 14:05:27 +02:00
|
|
|
* @return pointer to the created check box
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_obj_t * lv_checkbox_create(lv_obj_t * par, const lv_obj_t * copy)
|
2016-07-19 14:05:27 +02:00
|
|
|
{
|
2018-10-05 17:22:49 +02:00
|
|
|
LV_LOG_TRACE("check box create started");
|
2018-07-25 17:57:08 +02:00
|
|
|
|
2016-07-19 14:05:27 +02:00
|
|
|
/*Create the ancestor basic object*/
|
2020-01-20 16:11:38 +01:00
|
|
|
lv_obj_t * cb = lv_btn_create(par, copy);
|
|
|
|
LV_ASSERT_MEM(cb);
|
|
|
|
if(cb == NULL) return NULL;
|
2018-07-25 13:33:53 +02:00
|
|
|
|
2020-01-20 16:11:38 +01:00
|
|
|
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(cb);
|
2018-06-19 09:49:58 +02:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_checkbox_ext_t * ext = lv_obj_allocate_ext_attr(cb, sizeof(lv_checkbox_ext_t));
|
2019-09-24 23:14:17 +02:00
|
|
|
LV_ASSERT_MEM(ext);
|
2019-12-03 18:16:14 +01:00
|
|
|
if(ext == NULL) {
|
2020-01-20 16:11:38 +01:00
|
|
|
lv_obj_del(cb);
|
2019-12-03 18:16:14 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-07-25 13:33:53 +02:00
|
|
|
|
2017-01-02 14:10:32 +01:00
|
|
|
ext->bullet = NULL;
|
2019-04-04 07:15:40 +02:00
|
|
|
ext->label = NULL;
|
2016-07-19 14:05:27 +02:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_obj_set_signal_cb(cb, lv_checkbox_signal);
|
2016-09-27 13:43:01 +02:00
|
|
|
|
2016-07-19 14:05:27 +02:00
|
|
|
/*Init the new checkbox object*/
|
2016-10-07 11:15:46 +02:00
|
|
|
if(copy == NULL) {
|
2020-01-20 16:11:38 +01:00
|
|
|
ext->bullet = lv_obj_create(cb, NULL);
|
2016-07-19 14:05:27 +02:00
|
|
|
lv_obj_set_click(ext->bullet, false);
|
|
|
|
|
2020-01-20 16:11:38 +01:00
|
|
|
ext->label = lv_label_create(cb, NULL);
|
2017-11-05 00:48:57 +01:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_checkbox_set_text(cb, "Check box");
|
2020-02-14 13:38:48 +01:00
|
|
|
lv_btn_set_layout(cb, LV_LAYOUT_ROW_MID);
|
2020-01-20 16:11:38 +01:00
|
|
|
lv_btn_set_fit(cb, LV_FIT_TIGHT);
|
2020-02-13 13:56:08 +01:00
|
|
|
lv_btn_set_checkable(cb, true);
|
2020-02-14 22:04:00 +01:00
|
|
|
lv_obj_add_protect(cb, LV_PROTECT_PRESS_LOST);
|
2017-11-02 17:44:00 +01:00
|
|
|
|
2020-02-14 17:03:25 +01:00
|
|
|
lv_theme_apply(cb, LV_THEME_CHECKBOX);
|
2019-12-31 23:02:25 +01:00
|
|
|
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_checkbox_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
|
2020-01-20 16:11:38 +01:00
|
|
|
ext->bullet = lv_obj_create(cb, copy_ext->bullet);
|
|
|
|
ext->label = lv_label_create(cb, copy_ext->label);
|
2016-12-15 10:31:30 +01:00
|
|
|
|
2017-01-08 13:06:41 +01:00
|
|
|
/*Refresh the style with new signal function*/
|
2020-02-26 19:48:27 +01:00
|
|
|
// lv_obj_refresh_style(cb);
|
2016-07-19 14:05:27 +02:00
|
|
|
}
|
2017-07-26 11:37:02 +02:00
|
|
|
|
2018-10-05 17:22:49 +02:00
|
|
|
LV_LOG_INFO("check box created");
|
2018-07-25 17:57:08 +02:00
|
|
|
|
2020-01-20 16:11:38 +01:00
|
|
|
return cb;
|
2016-07-19 14:05:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Setter functions
|
|
|
|
*====================*/
|
|
|
|
|
|
|
|
/**
|
2019-03-01 15:25:29 +08:00
|
|
|
* Set the text of a check box. `txt` will be copied and may be deallocated
|
|
|
|
* after this function returns.
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param cb pointer to a check box
|
2019-03-01 15:25:29 +08:00
|
|
|
* @param txt the text of the check box. NULL to refresh with the current text.
|
2016-07-19 14:05:27 +02:00
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
void lv_checkbox_set_text(lv_obj_t * cb, const char * txt)
|
2016-07-19 14:05:27 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(cb, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_checkbox_ext_t * ext = lv_obj_get_ext_attr(cb);
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_label_set_text(ext->label, txt);
|
2016-07-19 14:05:27 +02:00
|
|
|
}
|
|
|
|
|
2019-03-01 15:25:29 +08:00
|
|
|
/**
|
|
|
|
* Set the text of a check box. `txt` must not be deallocated during the life
|
|
|
|
* of this checkbox.
|
|
|
|
* @param cb pointer to a check box
|
|
|
|
* @param txt the text of the check box. NULL to refresh with the current text.
|
|
|
|
*/
|
2020-04-28 21:07:10 +02:00
|
|
|
void lv_checkbox_set_text_static(lv_obj_t * cb, const char * txt)
|
2019-04-04 07:15:40 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(cb, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_checkbox_ext_t * ext = lv_obj_get_ext_attr(cb);
|
2020-04-28 21:07:10 +02:00
|
|
|
lv_label_set_text_static(ext->label, txt);
|
2019-03-01 15:25:29 +08:00
|
|
|
}
|
|
|
|
|
2016-07-19 14:05:27 +02:00
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the text of a check box
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param cb pointer to check box object
|
2016-07-19 14:05:27 +02:00
|
|
|
* @return pointer to the text of the check box
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
const char * lv_checkbox_get_text(const lv_obj_t * cb)
|
2016-07-19 14:05:27 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(cb, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_checkbox_ext_t * ext = lv_obj_get_ext_attr(cb);
|
2018-06-19 09:49:58 +02:00
|
|
|
return lv_label_get_text(ext->label);
|
2016-07-19 14:05:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
2017-11-05 00:48:57 +01:00
|
|
|
/**
|
|
|
|
* Signal function of the check box
|
|
|
|
* @param cb pointer to a check box object
|
|
|
|
* @param sign a signal type from lv_signal_t enum
|
|
|
|
* @param param pointer to a signal specific variable
|
|
|
|
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
static lv_res_t lv_checkbox_signal(lv_obj_t * cb, lv_signal_t sign, void * param)
|
2017-11-05 00:48:57 +01:00
|
|
|
{
|
|
|
|
lv_res_t res;
|
2019-12-31 23:02:25 +01:00
|
|
|
if(sign == LV_SIGNAL_GET_STYLE) {
|
2020-01-07 23:43:57 +01:00
|
|
|
lv_get_style_info_t * info = param;
|
2020-02-14 12:36:44 +01:00
|
|
|
info->result = lv_checkbox_get_style(cb, info->part);
|
2020-01-07 23:43:57 +01:00
|
|
|
if(info->result != NULL) return LV_RES_OK;
|
|
|
|
else return ancestor_signal(cb, sign, param);
|
2019-12-31 23:02:25 +01:00
|
|
|
}
|
2017-11-05 00:48:57 +01:00
|
|
|
|
|
|
|
/* Include the ancient signal function */
|
|
|
|
res = ancestor_signal(cb, sign, param);
|
2017-11-07 17:00:55 +01:00
|
|
|
if(res != LV_RES_OK) return res;
|
2019-09-26 12:54:40 +02:00
|
|
|
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
|
2017-11-05 00:48:57 +01:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_checkbox_ext_t * ext = lv_obj_get_ext_attr(cb);
|
2017-11-05 00:48:57 +01:00
|
|
|
|
2017-11-07 17:00:55 +01:00
|
|
|
if(sign == LV_SIGNAL_STYLE_CHG) {
|
2020-02-15 00:33:26 +01:00
|
|
|
const lv_font_t * font = lv_obj_get_style_text_font(ext->label, LV_LABEL_PART_MAIN);
|
2019-12-31 23:02:25 +01:00
|
|
|
lv_coord_t line_height = lv_font_get_line_height(font);
|
2020-05-14 11:29:49 +02:00
|
|
|
lv_coord_t leftp = lv_obj_get_style_pad_left(cb, LV_CHECKBOX_PART_BULLET);
|
|
|
|
lv_coord_t rightp = lv_obj_get_style_pad_right(cb, LV_CHECKBOX_PART_BULLET);
|
|
|
|
lv_coord_t topp = lv_obj_get_style_pad_top(cb, LV_CHECKBOX_PART_BULLET);
|
|
|
|
lv_coord_t bottomp = lv_obj_get_style_pad_bottom(cb, LV_CHECKBOX_PART_BULLET);
|
|
|
|
|
|
|
|
lv_obj_set_size(ext->bullet, line_height + leftp + rightp, line_height + topp + bottomp);
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_obj_set_state(ext->bullet, lv_obj_get_state(cb, LV_CHECKBOX_PART_BG));
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_PRESSED || sign == LV_SIGNAL_RELEASED || sign == LV_SIGNAL_PRESS_LOST ||
|
2020-01-28 22:15:45 +01:00
|
|
|
sign == LV_SIGNAL_FOCUS || sign == LV_SIGNAL_DEFOCUS) {
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_obj_set_state(ext->bullet, lv_obj_get_state(cb, LV_CHECKBOX_PART_BG));
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_CONTROL) {
|
2020-06-28 12:13:39 +08:00
|
|
|
#if LV_USE_GROUP
|
2018-06-19 09:49:58 +02:00
|
|
|
char c = *((char *)param);
|
2019-04-10 06:21:54 +02:00
|
|
|
if(c == LV_KEY_RIGHT || c == LV_KEY_DOWN || c == LV_KEY_LEFT || c == LV_KEY_UP) {
|
2019-06-20 14:28:25 +02:00
|
|
|
/*Follow the backgrounds state with the bullet*/
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_obj_set_state(ext->bullet, lv_obj_get_state(cb, LV_CHECKBOX_PART_BG));
|
2017-11-05 00:48:57 +01:00
|
|
|
}
|
2020-06-28 12:13:39 +08:00
|
|
|
#endif
|
2018-02-28 15:37:41 +01:00
|
|
|
}
|
2017-11-05 00:48:57 +01:00
|
|
|
|
2017-11-15 15:50:33 +01:00
|
|
|
return res;
|
2017-11-05 00:48:57 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 23:02:25 +01:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
static lv_style_list_t * lv_checkbox_get_style(lv_obj_t * cb, uint8_t type)
|
2019-12-31 23:02:25 +01:00
|
|
|
{
|
2020-01-16 14:26:36 +01:00
|
|
|
lv_style_list_t * style_dsc_p;
|
2019-12-31 23:02:25 +01:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_checkbox_ext_t * ext = lv_obj_get_ext_attr(cb);
|
2019-12-31 23:02:25 +01:00
|
|
|
switch(type) {
|
2020-02-26 19:48:27 +01:00
|
|
|
case LV_CHECKBOX_PART_BG:
|
|
|
|
style_dsc_p = &cb->style_list;
|
|
|
|
break;
|
|
|
|
case LV_CHECKBOX_PART_BULLET:
|
|
|
|
style_dsc_p = lv_obj_get_style_list(ext->bullet, LV_BTN_PART_MAIN);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
style_dsc_p = NULL;
|
2019-12-31 23:02:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return style_dsc_p;
|
|
|
|
}
|
|
|
|
|
2016-07-19 14:05:27 +02:00
|
|
|
#endif
|