1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00
lvgl/lv_objx/lv_cb.c

284 lines
7.7 KiB
C
Raw Normal View History

2016-07-19 14:05:27 +02:00
/**
* @file lv_cb.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_conf.h"
#if USE_LV_CB != 0
#include "lv_cb.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2016-08-04 11:33:35 +02:00
#if 0 /*The ancestor design function is used */
2016-10-07 11:15:46 +02:00
static bool lv_cb_design(lv_obj_t * cb, const area_t * mask, lv_design_mode_t mode);
2016-08-04 11:33:35 +02:00
#endif
static void lv_cbs_init(void);
2016-07-19 14:05:27 +02:00
/**********************
* STATIC VARIABLES
**********************/
static lv_cbs_t lv_cbs_def =
{
2016-07-19 14:05:27 +02:00
};
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/*-----------------
* Create function
*-----------------*/
/**
* 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
*/
2016-10-07 11:15:46 +02:00
lv_obj_t * lv_cb_create(lv_obj_t * par, lv_obj_t * copy)
2016-07-19 14:05:27 +02:00
{
/*Create the ancestor basic object*/
2016-10-07 11:15:46 +02:00
lv_obj_t * new_cb = lv_btn_create(par, copy);
dm_assert(new_cb);
2016-07-19 14:05:27 +02:00
2016-10-07 11:15:46 +02:00
lv_cb_ext_t * ext = lv_obj_alloc_ext(new_cb, sizeof(lv_cb_ext_t));
2016-07-19 14:05:27 +02:00
dm_assert(ext);
2016-10-07 11:15:46 +02:00
lv_obj_set_signal_f(new_cb, lv_cb_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) {
lv_rect_set_layout(new_cb, LV_RECT_LAYOUT_ROW_M);
lv_rect_set_fit(new_cb, true, true);
lv_btn_set_tgl(new_cb, true);
2016-07-19 14:05:27 +02:00
2016-10-07 11:15:46 +02:00
ext->bullet = lv_btn_create(new_cb, NULL);
2016-07-19 14:05:27 +02:00
lv_obj_set_click(ext->bullet, false);
2016-10-07 11:15:46 +02:00
ext->label = lv_label_create(new_cb, NULL);
2016-07-19 14:05:27 +02:00
lv_label_set_text(ext->label, "Check box");
2016-10-07 11:15:46 +02:00
lv_obj_set_style(new_cb, lv_cbs_get(LV_CBS_DEF, NULL));
2016-07-19 14:05:27 +02:00
} else {
2016-10-07 11:15:46 +02:00
lv_cb_ext_t * copy_ext = lv_obj_get_ext(copy);
ext->bullet = lv_btn_create(new_cb, copy_ext->bullet);
ext->label = lv_label_create(new_cb, copy_ext->label);
2016-12-15 10:31:30 +01:00
lv_obj_set_style(new_cb, lv_obj_get_style(copy));
2016-07-19 14:05:27 +02:00
}
2016-10-07 11:15:46 +02:00
lv_obj_align_us(new_cb, NULL, LV_ALIGN_CENTER, 0, 0);
2016-07-19 14:05:27 +02:00
2016-10-07 11:15:46 +02:00
return new_cb;
2016-07-19 14:05:27 +02:00
}
/**
* Signal function of the check box
2016-10-07 11:15:46 +02:00
* @param cb pointer to a check box object
2016-07-19 14:05:27 +02:00
* @param sign a signal type from lv_signal_t enum
* @param param pointer to a signal specific variable
*/
2016-10-07 11:15:46 +02:00
bool lv_cb_signal(lv_obj_t * cb, lv_signal_t sign, void * param)
2016-07-19 14:05:27 +02:00
{
bool valid;
/* Include the ancient signal function */
2016-10-07 11:15:46 +02:00
valid = lv_btn_signal(cb, sign, param);
2016-07-19 14:05:27 +02:00
2016-10-07 11:15:46 +02:00
lv_cb_ext_t * ext = lv_obj_get_ext(cb);
lv_cbs_t * cbs = lv_obj_get_style(cb);
2016-07-19 14:05:27 +02:00
/* The object can be deleted so check its validity and then
* make the object specific signal handling */
if(valid != false) {
switch(sign) {
case LV_SIGNAL_PRESSED:
case LV_SIGNAL_RELEASED:
case LV_SIGNAL_LONG_PRESS:
2016-09-27 13:43:01 +02:00
case LV_SIGNAL_PRESS_LOST:
2016-10-07 11:15:46 +02:00
lv_btn_set_state(ext->bullet, lv_btn_get_state(cb));
2016-07-19 14:05:27 +02:00
break;
case LV_SIGNAL_STYLE_CHG:
2016-10-07 11:15:46 +02:00
lv_obj_set_size(ext->bullet, cbs->bullet_size, cbs->bullet_size);
lv_obj_set_style(ext->bullet, &cbs->bullet);
lv_obj_set_style(ext->label, &cbs->label);
2016-07-19 14:05:27 +02:00
break;
case LV_SIGNAL_CLEANUP:
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
break;
2016-07-19 14:05:27 +02:00
default:
break;
}
}
return valid;
}
/*=====================
* Setter functions
*====================*/
/**
* Set the text of a check box
2016-10-07 11:15:46 +02:00
* @param cb pointer to a check box
2016-07-19 14:05:27 +02:00
* @param txt the text of the check box
*/
2016-10-07 11:15:46 +02:00
void lv_cb_set_text(lv_obj_t * cb, const char * txt)
2016-07-19 14:05:27 +02:00
{
2016-10-07 11:15:46 +02:00
lv_cb_ext_t * ext = lv_obj_get_ext(cb);
lv_label_set_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
*/
2016-10-07 11:15:46 +02:00
const char * lv_cb_get_text(lv_obj_t * cb)
2016-07-19 14:05:27 +02:00
{
2016-10-07 11:15:46 +02:00
lv_cb_ext_t * ext = lv_obj_get_ext(cb);
return lv_label_get_text(ext->label);
2016-07-19 14:05:27 +02:00
}
/**
* Return with a pointer to a built-in style and/or copy it to a variable
* @param style a style name from lv_cbs_builtin_t enum
2016-10-07 11:15:46 +02:00
* @param copy copy the style to this variable. (NULL if unused)
2016-07-19 14:05:27 +02:00
* @return pointer to an lv_cbs_t style
*/
2016-10-07 11:15:46 +02:00
lv_cbs_t * lv_cbs_get(lv_cbs_builtin_t style, lv_cbs_t * copy)
2016-07-19 14:05:27 +02:00
{
static bool style_inited = false;
/*Make the style initialization if it is not done yet*/
if(style_inited == false) {
lv_cbs_init();
style_inited = true;
}
2016-07-19 14:05:27 +02:00
lv_cbs_t *style_p;
switch(style) {
case LV_CBS_DEF:
style_p = &lv_cbs_def;
break;
default:
style_p = &lv_cbs_def;
}
2016-10-07 11:15:46 +02:00
if(copy != NULL) {
if(style_p != NULL) memcpy(copy, style_p, sizeof(lv_cbs_t));
else memcpy(copy, &lv_cbs_def, sizeof(lv_cbs_t));
2016-07-19 14:05:27 +02:00
}
return style_p;
}
/**********************
* STATIC FUNCTIONS
**********************/
2016-08-04 11:33:35 +02:00
#if 0 /*The ancestor design function is used */
2016-07-19 14:05:27 +02:00
/**
* Handle the drawing related tasks of the check boxes
2016-10-07 11:15:46 +02:00
* @param cb pointer to an object
2016-07-19 14:05:27 +02:00
* @param mask the object will be drawn only in this area
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
* (return 'true' if yes)
* LV_DESIGN_DRAW: draw the object (always return 'true')
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
2016-07-19 14:05:27 +02:00
* @param return true/false, depends on 'mode'
*/
2016-10-07 11:15:46 +02:00
static bool lv_cb_design(lv_obj_t * cb, const area_t * mask, lv_design_mode_t mode)
2016-07-19 14:05:27 +02:00
{
if(mode == LV_DESIGN_COVER_CHK) {
/*Return false if the object is not covers the mask_p area*/
return false;
}
/*Draw the object*/
return true;
}
#endif
/**
* Initialize the rectangle styles
*/
static void lv_cbs_init(void)
{
/*Default style*/
/*Bg style*/
2016-10-07 11:15:46 +02:00
lv_btns_get(LV_RECTS_TRANSP, &lv_cbs_def.bg);
lv_cbs_def.bg.rects.hpad = 0 * LV_STYLE_MULT;
lv_cbs_def.bg.rects.vpad = 0 * LV_STYLE_MULT;
lv_cbs_def.bg.rects.opad = 5 * LV_STYLE_MULT;
/*Bullet style*/
lv_btns_get(LV_BTNS_DEF, &lv_cbs_def.bullet);
lv_cbs_def.bullet.mcolor[LV_BTN_STATE_REL] = COLOR_WHITE;
lv_cbs_def.bullet.gcolor[LV_BTN_STATE_REL] = COLOR_SILVER;
lv_cbs_def.bullet.bcolor[LV_BTN_STATE_REL] = COLOR_BLACK;
2016-12-15 10:31:30 +01:00
lv_cbs_def.bullet.light_en[LV_BTN_STATE_REL] = 0;
lv_cbs_def.bullet.mcolor[LV_BTN_STATE_PR] = COLOR_SILVER;
lv_cbs_def.bullet.gcolor[LV_BTN_STATE_PR] = COLOR_GRAY;
lv_cbs_def.bullet.bcolor[LV_BTN_STATE_PR] = COLOR_BLACK;
2016-12-15 10:31:30 +01:00
lv_cbs_def.bullet.light_en[LV_BTN_STATE_PR] = 0;
lv_cbs_def.bullet.mcolor[LV_BTN_STATE_TGL_REL] = COLOR_MAKE(0x20, 0x30, 0x40);
lv_cbs_def.bullet.gcolor[LV_BTN_STATE_TGL_REL] = COLOR_MAKE(0x10, 0x20, 0x30);
lv_cbs_def.bullet.bcolor[LV_BTN_STATE_TGL_REL] = COLOR_WHITE;
2016-12-15 10:31:30 +01:00
lv_cbs_def.bullet.light_en[LV_BTN_STATE_TGL_REL] = 0;
lv_cbs_def.bullet.mcolor[LV_BTN_STATE_TGL_PR] = COLOR_MAKE(0x50, 0x70, 0x90);
lv_cbs_def.bullet.gcolor[LV_BTN_STATE_TGL_PR] = COLOR_MAKE(0x20, 0x30, 0x40);
lv_cbs_def.bullet.bcolor[LV_BTN_STATE_TGL_PR] = COLOR_WHITE;
2016-12-15 10:31:30 +01:00
lv_cbs_def.bullet.light_en[LV_BTN_STATE_TGL_PR] = 0;
lv_cbs_def.bullet.mcolor[LV_BTN_STATE_INA] = COLOR_SILVER;
lv_cbs_def.bullet.gcolor[LV_BTN_STATE_INA] = COLOR_GRAY;
lv_cbs_def.bullet.bcolor[LV_BTN_STATE_INA] = COLOR_WHITE;
2016-12-15 10:31:30 +01:00
lv_cbs_def.bullet.light_en[LV_BTN_STATE_INA] = 0;
lv_cbs_def.bullet.rects.bwidth = 2 * LV_STYLE_MULT;
lv_cbs_def.bullet.rects.bopa = 70;
lv_cbs_def.bullet.rects.empty = 0;
lv_cbs_def.bullet.rects.round = LV_OBJ_DEF_WIDTH / 3 / 4;
lv_cbs_def.bullet.rects.hpad = 0 * LV_STYLE_MULT;
lv_cbs_def.bullet.rects.vpad = 0 * LV_STYLE_MULT;
lv_cbs_def.bullet.rects.opad = 0 * LV_STYLE_MULT;
/*Label*/
lv_labels_get(LV_LABELS_TXT, &lv_cbs_def.label);
lv_cbs_def.label.objs.color = COLOR_MAKE(0x10, 0x18, 0x20);
/*Others*/
lv_cbs_def.bullet_size = LV_OBJ_DEF_WIDTH / 3;
}
2016-07-19 14:05:27 +02:00
#endif