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

171 lines
4.5 KiB
C
Raw Normal View History

2017-08-18 17:43:14 +02:00
/**
* @file lv_sw.h
2018-06-19 09:49:58 +02:00
*
2017-08-18 17:43:14 +02:00
*/
#ifndef LV_SWITCH_H
#define LV_SWITCH_H
2017-08-18 17:43:14 +02:00
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
#include "../lv_conf_internal.h"
#if LV_USE_SWITCH != 0
2017-08-18 17:43:14 +02:00
2017-08-18 21:14:11 +02:00
/*Testing of dependencies*/
#if LV_USE_SLIDER == 0
#error "lv_sw: lv_slider is required. Enable it in lv_conf.h (LV_USE_SLIDER 1)"
2017-08-18 21:14:11 +02:00
#endif
2017-11-30 11:35:33 +01:00
#include "../lv_core/lv_obj.h"
2020-01-18 23:34:34 +01:00
#include "lv_bar.h"
2017-08-18 17:43:14 +02:00
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/*Data of switch*/
typedef struct
{
2020-01-18 23:34:34 +01:00
lv_bar_ext_t bar; /*Ext. of ancestor*/
2017-08-18 17:43:14 +02:00
/*New data for this type */
2020-01-18 23:34:34 +01:00
lv_style_list_t style_knob; /*Style of the knob*/
2019-09-16 10:58:28 +02:00
uint8_t state :1; /*The current state*/
} lv_switch_ext_t;
2017-08-18 17:43:14 +02:00
2019-06-27 18:07:26 -04:00
/**
2019-12-31 22:13:32 +01:00
* Switch parts.
2019-06-27 18:07:26 -04:00
*/
2018-09-18 13:59:40 +02:00
enum {
LV_SWITCH_PART_BG = LV_BAR_PART_BG, /**< Switch background. */
LV_SWITCH_PART_INDIC = LV_BAR_PART_INDIC, /**< Switch fill area. */
LV_SWITCH_PART_KNOB = _LV_BAR_PART_VIRTUAL_LAST, /**< Switch knob. */
_LV_SWITCH_PART_VIRTUAL_LAST
2018-09-18 13:59:40 +02:00
};
2019-12-31 22:13:32 +01:00
typedef uint8_t lv_switch_part_t;
2017-08-18 17:43:14 +02:00
/**********************
* GLOBAL PROTOTYPES
**********************/
/**
* Create a switch objects
* @param par pointer to an object, it will be the parent of the new switch
* @param copy pointer to a switch object, if not NULL then the new object will be copied from it
* @return pointer to the created switch
*/
lv_obj_t * lv_switch_create(lv_obj_t * par, const lv_obj_t * copy);
2017-08-18 17:43:14 +02:00
2017-11-10 15:01:40 +01:00
/*=====================
* Setter functions
*====================*/
2017-08-18 17:43:14 +02:00
/**
2017-11-09 17:13:04 +01:00
* Turn ON the switch
2017-08-18 17:43:14 +02:00
* @param sw pointer to a switch object
2019-06-11 13:51:14 +02:00
* @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
2017-08-18 17:43:14 +02:00
*/
void lv_switch_on(lv_obj_t * sw, lv_anim_enable_t anim);
2017-11-09 17:13:04 +01:00
/**
* Turn OFF the switch
* @param sw pointer to a switch object
2019-06-11 13:51:14 +02:00
* @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
2017-11-09 17:13:04 +01:00
*/
void lv_switch_off(lv_obj_t * sw, lv_anim_enable_t anim);
2019-01-25 17:09:59 -08:00
/**
* Toggle the position of the switch
* @param sw pointer to a switch object
2019-06-11 13:51:14 +02:00
* @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
2019-01-25 17:09:59 -08:00
* @return resulting state of the switch.
*/
bool lv_switch_toggle(lv_obj_t * sw, lv_anim_enable_t anim);
2017-11-10 15:01:40 +01:00
/**
2019-12-31 22:13:32 +01:00
* Set an image to display on the knob of the switch.
2017-11-10 15:01:40 +01:00
* @param sw pointer to a switch object
2019-11-30 11:41:18 +01:00
* @param img_src pointer to an `lv_img_dsc_t` variable or a path to an image
* (not an `lv_img` object)
2017-11-10 15:01:40 +01:00
*/
void lv_switch_set_knob_img(lv_obj_t * sw, const void * img_src);
/**
* Set an image to display on the knob of the switch when it's in ON state
* @param sw pointer to a switch object
2019-11-30 11:41:18 +01:00
* @param img_src pointer to an `lv_img_dsc_t` variable or a path to an image
* (not an `lv_img` object)
*/
void lv_switch_set_knob_on_img(lv_obj_t * sw, const void * img_src);
2017-11-10 15:01:40 +01:00
/**
* Set the animation time of the switch
* @param sw pointer to a switch object
* @param anim_time animation time
* @return style pointer to a style
*/
static inline void lv_switch_set_anim_time(lv_obj_t * sw, uint16_t anim_time)
{
lv_bar_set_anim_time(sw, anim_time);
}
2017-11-10 15:01:40 +01:00
/*=====================
* Getter functions
*====================*/
/**
* Get the state of a switch
* @param sw pointer to a switch object
* @return false: OFF; true: ON
*/
static inline bool lv_switch_get_state(const lv_obj_t * sw)
{
lv_switch_ext_t * ext = (lv_switch_ext_t *)lv_obj_get_ext_attr(sw);
2019-09-16 10:58:28 +02:00
return ext->state ? true : false;
}
/**
* Get an image to display on the knob of the switch when it's in OFF state
* @param sw pointer to a switch object
2019-11-30 11:41:18 +01:00
* @return the image source: pointer to an `lv_img_dsc_t` variable or a path to an image
* (not an `lv_img` object)
*/
const void * lv_slider_get_knob_off_img(lv_obj_t * sw, const void * img_src);
/**
* Get an image to display on the knob of the switch when it's in ON state
* @param sw pointer to a switch object
2019-11-30 11:41:18 +01:00
* @return the image source: pointer to an `lv_img_dsc_t` variable or a path to an image
* (not an `lv_img` object)
*/
const void * lv_slider_get_knob_on_img(lv_obj_t * sw, const void * img_src);
/**
* Get the animation time of the switch
* @param sw pointer to a switch object
* @return style pointer to a style
*/
static inline uint16_t lv_switch_get_anim_time(const lv_obj_t * sw)
{
return lv_bar_get_anim_time(sw);
}
2017-11-10 15:01:40 +01:00
2017-08-18 17:43:14 +02:00
/**********************
* MACROS
**********************/
#endif /*LV_USE_SWITCH*/
2017-08-18 17:43:14 +02:00
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*LV_SWITCH_H*/