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

317 lines
9.2 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
2020-01-18 23:34:34 +01:00
lv_obj_t * btn;
2018-06-19 09:49:58 +02:00
2020-01-18 23:34:34 +01:00
btn = lv_cont_create(par, copy);
LV_ASSERT_MEM(btn);
if(btn == NULL) return NULL;
2020-01-18 23:34:34 +01:00
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(btn);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(btn);
2016-06-08 07:25:08 +02:00
/*Allocate the extended data*/
2020-01-18 23:34:34 +01:00
lv_btn_ext_t * ext = lv_obj_allocate_ext_attr(btn, sizeof(lv_btn_ext_t));
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(ext);
if(ext == NULL) {
2020-01-18 23:34:34 +01:00
lv_obj_del(btn);
return NULL;
}
2020-02-23 07:16:40 +01:00
ext->checkable = 0;
2019-12-19 11:05:04 +01:00
2020-01-18 23:34:34 +01:00
lv_obj_set_signal_cb(btn, lv_btn_signal);
lv_obj_set_design_cb(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*/
2020-03-26 15:50:57 +01:00
if(par) {
2020-05-12 13:57:43 +02:00
lv_obj_set_size(btn, LV_DPI, LV_DPI / 3);
2020-03-26 15:50:57 +01:00
lv_btn_set_layout(btn, LV_LAYOUT_CENTER);
}
2017-12-13 15:12:04 +01:00
2020-01-18 23:34:34 +01:00
lv_obj_set_click(btn, true); /*Be sure the button is clickable*/
2020-01-18 23:34:34 +01:00
lv_theme_apply(btn, LV_THEME_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);
2020-02-23 07:16:40 +01:00
ext->checkable = copy_ext->checkable;
2018-06-19 09:49:58 +02:00
/*Refresh the style with new signal function*/
2020-03-10 10:41:39 +01:00
lv_obj_refresh_style(btn, LV_STYLE_PROP_ALL);
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
2020-01-18 23:34:34 +01:00
return 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
*/
2020-02-13 13:56:08 +01:00
void lv_btn_set_checkable(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
2020-02-23 07:16:40 +01:00
ext->checkable = 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) {
2020-02-13 13:56:08 +01:00
case LV_BTN_STATE_RELEASED:
2020-04-28 21:02:22 +02:00
lv_obj_clear_state(btn, LV_STATE_PRESSED | LV_STATE_CHECKED | LV_STATE_DISABLED);
2019-12-19 11:05:04 +01:00
break;
2020-02-13 13:56:08 +01:00
case LV_BTN_STATE_PRESSED:
2020-04-28 21:02:22 +02:00
lv_obj_clear_state(btn, LV_STATE_CHECKED | LV_STATE_DISABLED);
lv_obj_add_state(btn, LV_STATE_PRESSED);
2019-12-19 11:05:04 +01:00
break;
2020-02-13 13:56:08 +01:00
case LV_BTN_STATE_CHECKED_RELEASED:
lv_obj_add_state(btn, LV_STATE_CHECKED);
2020-04-28 21:02:22 +02:00
lv_obj_clear_state(btn, LV_STATE_PRESSED | LV_STATE_DISABLED);
2019-12-19 11:05:04 +01:00
break;
2020-02-13 13:56:08 +01:00
case LV_BTN_STATE_CHECKED_PRESSED:
lv_obj_add_state(btn, LV_STATE_PRESSED | LV_STATE_CHECKED);
2020-04-28 21:02:22 +02:00
lv_obj_clear_state(btn, LV_STATE_DISABLED);
2019-12-19 11:05:04 +01:00
break;
2020-02-13 13:56:08 +01:00
case LV_BTN_STATE_DISABLED:
2020-04-28 21:02:22 +02:00
lv_obj_clear_state(btn, LV_STATE_PRESSED | LV_STATE_CHECKED);
lv_obj_add_state(btn, LV_STATE_DISABLED);
2020-01-18 23:34:34 +01:00
break;
2020-04-28 21:02:22 +02:00
case LV_BTN_STATE_CHECKED_DISABLED:
lv_obj_clear_state(btn, LV_STATE_PRESSED);
lv_obj_add_state(btn, LV_STATE_DISABLED | LV_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_STATE_CHECKED) {
lv_obj_clear_state(btn, LV_STATE_CHECKED);
2020-02-26 19:48:27 +01:00
}
else {
lv_obj_add_state(btn, LV_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
* @return the state of the button (from lv_btn_state_t enum).
* If the button is in disabled state `LV_BTN_STATE_DISABLED` will be ORed to the other button states.
2016-06-08 07:25:08 +02:00
*/
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_state_t obj_state = lv_obj_get_state(btn, LV_BTN_PART_MAIN);
2019-12-19 11:05:04 +01:00
2020-04-28 21:02:22 +02:00
if(obj_state & LV_STATE_DISABLED) {
if(obj_state & LV_STATE_CHECKED) return LV_BTN_STATE_CHECKED_DISABLED;
else return LV_BTN_STATE_DISABLED;
2020-02-26 19:48:27 +01:00
}
2020-04-28 21:02:22 +02:00
if(obj_state & LV_STATE_CHECKED) {
if(obj_state & LV_STATE_PRESSED) return LV_BTN_STATE_CHECKED_PRESSED;
else return LV_BTN_STATE_CHECKED_RELEASED;
} else {
if(obj_state & LV_STATE_PRESSED) return LV_BTN_STATE_PRESSED;
else return LV_BTN_STATE_RELEASED;
2019-12-19 11:05:04 +01:00
}
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
*/
2020-02-13 13:56:08 +01:00
bool lv_btn_get_checkable(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
2020-02-23 07:16:40 +01:00
return ext->checkable != 0 ? true : false;
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);
2020-02-26 19:48:27 +01:00
}
else if(mode == LV_DESIGN_DRAW_MAIN) {
2020-01-21 06:26:37 +01:00
ancestor_design(btn, clip_area, mode);
2020-02-26 19:48:27 +01: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
2020-02-13 13:56:08 +01:00
bool tgl = lv_btn_get_checkable(btn);
if(sign == LV_SIGNAL_RELEASED) {
/*If not dragged and it was not long press action then
*change state and run the action*/
2020-02-13 13:56:08 +01:00
if(lv_indev_is_dragging(param) == false && tgl) {
2019-06-20 14:28:25 +02:00
uint32_t toggled = 0;
if(lv_obj_get_state(btn, LV_BTN_PART_MAIN) & LV_STATE_CHECKED) {
2020-02-13 13:56:08 +01:00
lv_btn_set_state(btn, LV_BTN_STATE_RELEASED);
2019-06-20 14:28:25 +02:00
toggled = 0;
2020-02-26 19:48:27 +01:00
}
else {
2020-02-13 13:56:08 +01:00
lv_btn_set_state(btn, LV_BTN_STATE_CHECKED_RELEASED);
2019-06-20 14:28:25 +02:00
toggled = 1;
}
2019-03-01 06:26:03 +01:00
2020-02-13 13:56:08 +01:00
res = lv_event_send(btn, LV_EVENT_VALUE_CHANGED, &toggled);
if(res != LV_RES_OK) return res;
}
2020-02-26 19:48:27 +01:00
}
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) {
2020-02-13 13:56:08 +01:00
if(lv_btn_get_checkable(btn)) {
lv_btn_set_state(btn, LV_BTN_STATE_CHECKED_RELEASED);
2019-06-20 14:28:25 +02:00
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;
}
2020-02-26 19:48:27 +01:00
}
else if(c == LV_KEY_LEFT || c == LV_KEY_DOWN) {
2020-02-13 13:56:08 +01:00
if(lv_btn_get_checkable(btn)) {
lv_btn_set_state(btn, LV_BTN_STATE_RELEASED);
2019-06-20 14:28:25 +02:00
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