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

363 lines
11 KiB
C
Raw Normal View History

2016-06-08 07:25:08 +02:00
/**
* @file lv_btn.c
2018-06-19 09:49:58 +02:00
*
2016-06-08 07:25:08 +02:00
*/
/*********************
* INCLUDES
*********************/
2016-12-15 10:31:30 +01:00
#include "lv_btn.h"
#if LV_USE_BTN != 0
2016-06-08 07:25:08 +02:00
2017-10-20 15:37:50 +02:00
#include <string.h>
2017-11-30 11:35:33 +01:00
#include "../lv_core/lv_group.h"
2019-09-24 16:30:38 +02:00
#include "../lv_core/lv_debug.h"
#include "../lv_draw/lv_draw.h"
#include "../lv_themes/lv_theme.h"
2017-11-23 20:42:14 +01:00
#include "../lv_misc/lv_area.h"
#include "../lv_misc/lv_color.h"
2018-07-08 01:00:56 +02:00
#include "../lv_misc/lv_math.h"
2016-06-08 07:25:08 +02:00
/*********************
* DEFINES
*********************/
#define LV_OBJX_NAME "lv_btn"
2019-04-04 07:15:40 +02:00
#define LV_BTN_INK_VALUE_MAX 256
#define LV_BTN_INK_VALUE_MAX_SHIFT 8
2018-09-06 20:57:59 +02:00
2016-06-08 07:25:08 +02:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2019-09-06 19:53:39 +02:00
static lv_design_res_t lv_btn_design(lv_obj_t * btn, const lv_area_t * clip_area, lv_design_mode_t mode);
2018-07-08 01:00:56 +02:00
static lv_res_t lv_btn_signal(lv_obj_t * btn, lv_signal_t sign, void * param);
2018-08-26 13:49:23 +02:00
2016-06-08 07:25:08 +02:00
/**********************
* STATIC VARIABLES
**********************/
static lv_signal_cb_t ancestor_signal;
static lv_design_cb_t ancestor_design;
2018-07-08 01:00:56 +02:00
2016-06-08 07:25:08 +02:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
2019-07-10 11:56:36 +02:00
* Create a button object
2016-10-07 11:15:46 +02:00
* @param par pointer to an object, it will be the parent of the new button
* @param copy pointer to a button object, if not NULL then the new object will be copied from it
2016-06-08 07:25:08 +02:00
* @return pointer to the created button
*/
lv_obj_t * lv_btn_create(lv_obj_t * par, const lv_obj_t * copy)
2016-06-08 07:25:08 +02:00
{
2018-10-05 17:22:49 +02:00
LV_LOG_TRACE("button create started");
2018-07-25 17:57:08 +02:00
2016-10-07 11:15:46 +02:00
lv_obj_t * new_btn;
2018-06-19 09:49:58 +02:00
2017-04-13 16:12:03 +02:00
new_btn = lv_cont_create(par, copy);
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(new_btn);
if(new_btn == NULL) return NULL;
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_btn);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_btn);
2016-06-08 07:25:08 +02:00
/*Allocate the extended data*/
lv_btn_ext_t * ext = lv_obj_allocate_ext_attr(new_btn, sizeof(lv_btn_ext_t));
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(ext);
if(ext == NULL) {
lv_obj_del(new_btn);
return NULL;
}
ext->toggle = 0;
2019-12-19 11:05:04 +01:00
lv_obj_set_signal_cb(new_btn, lv_btn_signal);
lv_obj_set_design_cb(new_btn, lv_btn_design);
2018-06-19 09:49:58 +02:00
2016-06-08 07:25:08 +02:00
/*If no copy do the basic initialization*/
if(copy == NULL) {
2017-12-13 15:12:04 +01:00
/*Set layout if the button is not a screen*/
if(par != NULL) {
lv_btn_set_layout(new_btn, LV_LAYOUT_CENTER);
}
2019-04-04 07:15:40 +02:00
lv_obj_set_click(new_btn, true); /*Be sure the button is clickable*/
2018-06-19 09:49:58 +02:00
/*Set the default styles*/
2019-12-31 07:03:34 +01:00
lv_obj_reset_style(new_btn, LV_BTN_PART_MAIN);
lv_obj_add_style_class(new_btn, LV_BTN_PART_MAIN, _t(BTN));
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
/*Copy 'copy'*/
else {
2018-06-19 09:49:58 +02:00
lv_btn_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
2019-04-04 07:15:40 +02:00
ext->toggle = copy_ext->toggle;
2019-12-19 11:05:04 +01:00
// memcpy((void*) ext->styles, copy_ext->styles, sizeof(ext->styles));
2018-06-19 09:49:58 +02:00
/*Refresh the style with new signal function*/
lv_obj_refresh_style(new_btn);
2016-06-08 07:25:08 +02:00
}
2018-06-19 09:49:58 +02:00
2018-10-05 17:22:49 +02:00
LV_LOG_INFO("button created");
2018-07-25 17:57:08 +02:00
2016-10-07 11:15:46 +02:00
return new_btn;
2016-06-08 07:25:08 +02:00
}
/*=====================
2018-06-19 09:49:58 +02:00
* Setter functions
2016-06-08 07:25:08 +02:00
*====================*/
/**
* Enable the toggled states
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button object
2016-06-08 07:25:08 +02:00
* @param tgl true: enable toggled states, false: disable
*/
void lv_btn_set_toggle(lv_obj_t * btn, bool tgl)
2016-06-08 07:25:08 +02:00
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
lv_btn_ext_t * ext = lv_obj_get_ext_attr(btn);
2018-06-19 09:49:58 +02:00
ext->toggle = tgl != false ? 1 : 0;
2016-06-08 07:25:08 +02:00
}
/**
* Set the state of the button
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button object
2016-06-08 07:25:08 +02:00
* @param state the new state of the button (from lv_btn_state_t enum)
*/
2016-10-07 11:15:46 +02:00
void lv_btn_set_state(lv_obj_t * btn, lv_btn_state_t state)
2016-06-08 07:25:08 +02:00
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
2019-12-19 11:05:04 +01:00
switch(state) {
case LV_BTN_STATE_REL:
lv_obj_clear_state(btn, LV_OBJ_STATE_PRESSED | LV_OBJ_STATE_CHECKED);
break;
case LV_BTN_STATE_PR:
lv_obj_clear_state(btn, LV_OBJ_STATE_CHECKED);
lv_obj_set_state(btn, LV_OBJ_STATE_PRESSED);
break;
case LV_BTN_STATE_TGL_REL:
lv_obj_set_state(btn, LV_OBJ_STATE_CHECKED);
lv_obj_clear_state(btn, LV_OBJ_STATE_PRESSED);
break;
case LV_BTN_STATE_TGL_PR:
lv_obj_set_state(btn, LV_OBJ_STATE_PRESSED | LV_OBJ_STATE_CHECKED);
break;
}
2016-06-08 07:25:08 +02:00
}
2017-10-20 15:37:50 +02:00
/**
* Toggle the state of the button (ON->OFF, OFF->ON)
* @param btn pointer to a button object
*/
void lv_btn_toggle(lv_obj_t * btn)
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
if(lv_obj_get_state(btn, LV_BTN_PART_MAIN) & LV_OBJ_STATE_CHECKED) {
2019-12-19 11:05:04 +01:00
lv_obj_clear_state(btn, LV_OBJ_STATE_CHECKED);
} else {
lv_obj_set_state(btn, LV_OBJ_STATE_CHECKED);
2017-10-20 15:37:50 +02:00
}
}
2016-06-08 07:25:08 +02:00
/*=====================
2018-06-19 09:49:58 +02:00
* Getter functions
2016-06-08 07:25:08 +02:00
*====================*/
/**
* Get the current state of the button
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button object
2016-06-08 07:25:08 +02:00
* @return the state of the button (from lv_btn_state_t enum)
*/
lv_btn_state_t lv_btn_get_state(const lv_obj_t * btn)
2016-06-08 07:25:08 +02:00
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
lv_obj_state_t state = lv_obj_get_state(btn, LV_BTN_PART_MAIN);
2019-12-19 11:05:04 +01:00
if(state & LV_OBJ_STATE_CHECKED) {
if(state & LV_OBJ_STATE_PRESSED) return LV_BTN_STATE_TGL_PR;
else return LV_BTN_STATE_TGL_REL;
} else {
if(state & LV_OBJ_STATE_PRESSED) return LV_BTN_STATE_PR;
else return LV_BTN_STATE_REL;
}
2016-06-08 07:25:08 +02:00
}
/**
* Get the toggle enable attribute of the button
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button object
2019-07-10 11:56:36 +02:00
* @return true: toggle enabled, false: disabled
2016-06-08 07:25:08 +02:00
*/
bool lv_btn_get_toggle(const lv_obj_t * btn)
2016-06-08 07:25:08 +02:00
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
lv_btn_ext_t * ext = lv_obj_get_ext_attr(btn);
2018-06-19 09:49:58 +02:00
return ext->toggle != 0 ? true : false;
2016-06-08 07:25:08 +02:00
}
lv_style_list_t * lv_btn_get_style(lv_obj_t * cont, uint8_t type)
2016-06-08 07:25:08 +02:00
{
lv_style_list_t * style_dsc_p;
2019-12-19 11:05:04 +01:00
switch(type) {
case LV_BTN_PART_MAIN:
2019-12-19 11:05:04 +01:00
style_dsc_p = &cont->style_dsc;
break;
default:
style_dsc_p = NULL;
}
2019-12-19 11:05:04 +01:00
return style_dsc_p;
2016-06-08 07:25:08 +02:00
}
/**********************
* STATIC FUNCTIONS
**********************/
2018-07-08 01:00:56 +02:00
/**
* Handle the drawing related tasks of the drop down lists
* @param btn pointer to an object
* @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
2019-09-06 19:53:39 +02:00
* @param return an element of `lv_design_res_t`
2018-07-08 01:00:56 +02:00
*/
2019-09-06 19:53:39 +02:00
static lv_design_res_t lv_btn_design(lv_obj_t * btn, const lv_area_t * clip_area, lv_design_mode_t mode)
2018-07-08 01:00:56 +02:00
{
2018-10-05 17:22:49 +02:00
if(mode == LV_DESIGN_COVER_CHK) {
2019-09-06 19:53:39 +02:00
return ancestor_design(btn, clip_area, mode);
2018-10-05 17:22:49 +02:00
} else if(mode == LV_DESIGN_DRAW_MAIN) {
2018-07-08 01:00:56 +02:00
2019-12-19 22:44:18 +01:00
lv_draw_rect_dsc_t draw_dsc;
lv_draw_rect_dsc_init(&draw_dsc);
lv_obj_init_draw_rect_dsc(btn, LV_OBJ_PART_MAIN, &draw_dsc);
lv_draw_rect(&btn->coords, clip_area, &draw_dsc);
2019-12-19 11:05:04 +01:00
2020-01-06 22:14:04 +01:00
if(lv_obj_get_style_int(btn, LV_OBJ_PART_MAIN, LV_STYLE_CLIP_CORNER)) {
2019-12-19 11:05:04 +01:00
lv_draw_mask_radius_param_t * mp = lv_mem_buf_get(sizeof(lv_draw_mask_radius_param_t));
2020-01-06 22:14:04 +01:00
lv_coord_t r = lv_obj_get_style_int(btn, LV_OBJ_PART_MAIN, LV_STYLE_RADIUS);
2019-12-19 11:05:04 +01:00
lv_draw_mask_radius_init(mp, &btn->coords, r, false);
/*Add the mask and use `obj+8` as custom id. Don't use `obj` directly because it might be used by the user*/
lv_draw_mask_add(mp, btn + 8);
}
2018-10-05 17:22:49 +02:00
} else if(mode == LV_DESIGN_DRAW_POST) {
2019-09-06 19:53:39 +02:00
ancestor_design(btn, clip_area, mode);
2018-10-05 17:22:49 +02:00
}
2018-07-08 01:00:56 +02:00
2019-09-06 19:53:39 +02:00
return LV_DESIGN_RES_OK;
2018-07-08 01:00:56 +02:00
}
2016-06-08 07:25:08 +02:00
/**
* Signal function of the button
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button 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
2016-06-08 07:25:08 +02:00
*/
static lv_res_t lv_btn_signal(lv_obj_t * btn, lv_signal_t sign, void * param)
2016-06-08 07:25:08 +02:00
{
lv_res_t res;
/* Include the ancient signal function */
res = ancestor_signal(btn, 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);
2016-06-08 07:25:08 +02:00
2019-04-04 07:15:40 +02:00
bool tgl = lv_btn_get_toggle(btn);
2019-12-19 11:05:04 +01:00
lv_btn_state_t state = lv_btn_get_state(btn);
if(sign == LV_SIGNAL_PRESSED) {
/*Refresh the state*/
2019-12-19 11:05:04 +01:00
if(state == LV_BTN_STATE_REL) {
lv_btn_set_state(btn, LV_BTN_STATE_PR);
2019-12-19 11:05:04 +01:00
} else if(state == LV_BTN_STATE_TGL_REL) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_PR);
}
2018-07-08 01:00:56 +02:00
} else if(sign == LV_SIGNAL_PRESS_LOST) {
/*Refresh the state*/
2019-12-19 11:05:04 +01:00
if(state == LV_BTN_STATE_PR)
2019-04-04 07:15:40 +02:00
lv_btn_set_state(btn, LV_BTN_STATE_REL);
2019-12-19 11:05:04 +01:00
else if(state == LV_BTN_STATE_TGL_PR)
2019-04-04 07:15:40 +02:00
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
2018-06-19 09:49:58 +02:00
} else if(sign == LV_SIGNAL_PRESSING) {
/*When the button begins to drag revert pressed states to released*/
if(lv_indev_is_dragging(param) != false) {
2019-12-19 11:05:04 +01:00
if(state == LV_BTN_STATE_PR)
2019-04-04 07:15:40 +02:00
lv_btn_set_state(btn, LV_BTN_STATE_REL);
2019-12-19 11:05:04 +01:00
else if(state == LV_BTN_STATE_TGL_PR)
2019-04-04 07:15:40 +02:00
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
}
2018-06-19 09:49:58 +02:00
} else if(sign == LV_SIGNAL_RELEASED) {
/*If not dragged and it was not long press action then
*change state and run the action*/
2019-03-01 06:26:03 +01:00
if(lv_indev_is_dragging(param) == false) {
2019-06-20 14:28:25 +02:00
uint32_t toggled = 0;
2019-12-19 11:05:04 +01:00
if(state == LV_BTN_STATE_PR && tgl == false) {
lv_btn_set_state(btn, LV_BTN_STATE_REL);
2019-06-20 14:28:25 +02:00
toggled = 0;
2019-12-19 11:05:04 +01:00
} else if(state == LV_BTN_STATE_TGL_PR && tgl == false) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
2019-06-20 14:28:25 +02:00
toggled = 1;
2019-12-19 11:05:04 +01:00
} else if(state == LV_BTN_STATE_PR && tgl == true) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
2019-06-20 14:28:25 +02:00
toggled = 1;
2019-12-19 11:05:04 +01:00
} else if(state == LV_BTN_STATE_TGL_PR && tgl == true) {
lv_btn_set_state(btn, LV_BTN_STATE_REL);
2019-06-20 14:28:25 +02:00
toggled = 0;
}
2019-03-01 06:26:03 +01:00
if(tgl) {
2019-06-20 14:28:25 +02:00
res = lv_event_send(btn, LV_EVENT_VALUE_CHANGED, &toggled);
2019-03-07 00:42:08 +01:00
if(res != LV_RES_OK) return res;
2019-03-01 06:26:03 +01:00
}
} else { /*If dragged change back the state*/
2019-12-19 23:16:53 +01:00
if(state == LV_BTN_STATE_PR) {
lv_btn_set_state(btn, LV_BTN_STATE_REL);
2019-12-19 11:05:04 +01:00
} else if(state == LV_BTN_STATE_TGL_PR) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
}
}
} else if(sign == LV_SIGNAL_CONTROL) {
2018-06-19 09:49:58 +02:00
char c = *((char *)param);
2019-04-08 14:36:20 +02:00
if(c == LV_KEY_RIGHT || c == LV_KEY_UP) {
2019-06-20 14:28:25 +02:00
if(lv_btn_get_toggle(btn)) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
uint32_t state = 1;
2019-06-27 07:16:15 +02:00
res = lv_event_send(btn, LV_EVENT_VALUE_CHANGED, &state);
2019-06-20 14:28:25 +02:00
if(res != LV_RES_OK) return res;
}
2019-04-08 14:36:20 +02:00
} else if(c == LV_KEY_LEFT || c == LV_KEY_DOWN) {
2019-06-20 14:28:25 +02:00
if(lv_btn_get_toggle(btn)) {
lv_btn_set_state(btn, LV_BTN_STATE_REL);
uint32_t state = 0;
2019-06-27 07:16:15 +02:00
res = lv_event_send(btn, LV_EVENT_VALUE_CHANGED, &state);
2019-06-20 14:28:25 +02:00
if(res != LV_RES_OK) return res;
}
}
2018-02-28 15:37:41 +01:00
}
return res;
2016-06-08 07:25:08 +02:00
}
2016-06-08 07:25:08 +02:00
#endif