1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-21 06:53:01 +08:00
lvgl/src/lv_widgets/lv_checkbox.c

219 lines
6.2 KiB
C
Raw Normal View History

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"
#if LV_USE_CHECKBOX != 0
2016-07-19 14:05:27 +02:00
2019-09-24 16:30:38 +02:00
#include "../lv_core/lv_debug.h"
2017-11-30 11:35:33 +01:00
#include "../lv_core/lv_group.h"
#include "../lv_themes/lv_theme.h"
2016-07-19 14:05:27 +02:00
/*********************
* DEFINES
*********************/
#define LV_OBJX_NAME "lv_checkbox"
2016-07-19 14:05:27 +02:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
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
**********************/
static lv_signal_cb_t ancestor_signal;
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
*/
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;
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
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);
if(ext == NULL) {
2020-01-20 16:11:38 +01:00
lv_obj_del(cb);
return NULL;
}
ext->bullet = NULL;
2019-04-04 07:15:40 +02:00
ext->label = NULL;
2016-07-19 14:05:27 +02: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);
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
2016-07-19 14:05:27 +02:00
} else {
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
/*Refresh the style with new signal function*/
2020-01-20 16:11:38 +01:00
// lv_obj_refresh_style(cb);
2016-07-19 14:05:27 +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
*/
void lv_checkbox_set_text(lv_obj_t * cb, const char * txt)
2016-07-19 14:05:27 +02:00
{
LV_ASSERT_OBJ(cb, LV_OBJX_NAME);
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.
*/
void lv_checkbox_set_static_text(lv_obj_t * cb, const char * txt)
2019-04-04 07:15:40 +02:00
{
LV_ASSERT_OBJ(cb, LV_OBJX_NAME);
lv_checkbox_ext_t * ext = lv_obj_get_ext_attr(cb);
2019-03-01 15:25:29 +08:00
lv_label_set_static_text(ext->label, txt);
}
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
*/
const char * lv_checkbox_get_text(const lv_obj_t * cb)
2016-07-19 14:05:27 +02:00
{
LV_ASSERT_OBJ(cb, LV_OBJX_NAME);
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
**********************/
/**
* 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
*/
static lv_res_t lv_checkbox_signal(lv_obj_t * cb, lv_signal_t sign, void * param)
{
lv_res_t res;
2019-12-31 23:02:25 +01:00
if(sign == LV_SIGNAL_GET_STYLE) {
lv_get_style_info_t * info = param;
info->result = lv_checkbox_get_style(cb, info->part);
if(info->result != NULL) return LV_RES_OK;
else return ancestor_signal(cb, sign, param);
2019-12-31 23:02:25 +01:00
}
/* Include the ancient signal function */
res = ancestor_signal(cb, sign, param);
if(res != LV_RES_OK) return res;
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
lv_checkbox_ext_t * ext = lv_obj_get_ext_attr(cb);
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);
lv_obj_set_size(ext->bullet, line_height, line_height);
lv_obj_set_state(ext->bullet, lv_obj_get_state(cb, LV_CHECKBOX_PART_BG));
2020-01-28 22:15:45 +01:00
} else if(sign == LV_SIGNAL_PRESSED || sign == LV_SIGNAL_RELEASED || sign == LV_SIGNAL_PRESS_LOST ||
sign == LV_SIGNAL_FOCUS || sign == LV_SIGNAL_DEFOCUS) {
lv_obj_set_state(ext->bullet, lv_obj_get_state(cb, LV_CHECKBOX_PART_BG));
2020-01-31 06:45:33 +01:00
} else if(sign == LV_SIGNAL_CONTROL) {
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*/
lv_obj_set_state(ext->bullet, lv_obj_get_state(cb, LV_CHECKBOX_PART_BG));
}
2018-02-28 15:37:41 +01:00
}
return res;
}
2019-12-31 23:02:25 +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
{
lv_style_list_t * style_dsc_p;
2019-12-31 23:02:25 +01:00
lv_checkbox_ext_t * ext = lv_obj_get_ext_attr(cb);
2019-12-31 23:02:25 +01:00
switch(type) {
case LV_CHECKBOX_PART_BG:
2020-01-16 21:25:11 +01:00
style_dsc_p = &cb->style_list;
2019-12-31 23:02:25 +01:00
break;
case LV_CHECKBOX_PART_BULLET:
2020-01-23 17:16:11 +01:00
style_dsc_p = lv_obj_get_style_list(ext->bullet, LV_BTN_PART_MAIN);
2019-12-31 23:02:25 +01:00
break;
default:
style_dsc_p = NULL;
}
return style_dsc_p;
}
2016-07-19 14:05:27 +02:00
#endif