2018-11-01 18:48:13 +01:00
|
|
|
/**
|
2018-11-03 18:41:06 +01:00
|
|
|
* @file lv_spinbox.c
|
2018-11-01 18:48:13 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
|
|
|
#include "lv_spinbox.h"
|
|
|
|
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_SPINBOX != 0
|
2020-06-08 13:10:43 +02:00
|
|
|
#include "../lv_misc/lv_debug.h"
|
2019-01-01 01:20:10 +01:00
|
|
|
#include "../lv_themes/lv_theme.h"
|
2019-02-15 06:27:49 +01:00
|
|
|
#include "../lv_misc/lv_math.h"
|
2019-03-18 07:25:20 +01:00
|
|
|
#include "../lv_misc/lv_utils.h"
|
2018-11-01 18:48:13 +01:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2019-09-26 10:51:54 +02:00
|
|
|
#define LV_OBJX_NAME "lv_spinbox"
|
2018-11-01 18:48:13 +01:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* TYPEDEFS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC PROTOTYPES
|
|
|
|
**********************/
|
|
|
|
static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * param);
|
2020-01-16 14:26:36 +01:00
|
|
|
static lv_style_list_t * lv_spinbox_get_style(lv_obj_t * ta, uint8_t part);
|
2018-12-20 00:01:05 +01:00
|
|
|
static void lv_spinbox_updatevalue(lv_obj_t * spinbox);
|
2018-11-01 18:48:13 +01:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
2019-02-26 09:25:46 +01:00
|
|
|
static lv_signal_cb_t ancestor_signal;
|
|
|
|
static lv_design_cb_t ancestor_design;
|
2018-11-01 18:48:13 +01:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a spinbox object
|
|
|
|
* @param par pointer to an object, it will be the parent of the new spinbox
|
|
|
|
* @param copy pointer to a spinbox object, if not NULL then the new object will be copied from it
|
|
|
|
* @return pointer to the created spinbox
|
|
|
|
*/
|
|
|
|
lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy)
|
|
|
|
{
|
2018-11-05 14:02:13 +01:00
|
|
|
LV_LOG_TRACE("spinbox create started");
|
|
|
|
|
|
|
|
/*Create the ancestor of spinbox*/
|
2020-02-20 05:08:03 +01:00
|
|
|
lv_obj_t * spinbox = lv_textarea_create(par, copy);
|
|
|
|
LV_ASSERT_MEM(spinbox);
|
|
|
|
if(spinbox == NULL) return NULL;
|
2018-11-05 14:02:13 +01:00
|
|
|
|
|
|
|
/*Allocate the spinbox type specific extended data*/
|
2020-02-20 05:08:03 +01:00
|
|
|
lv_spinbox_ext_t * ext = lv_obj_allocate_ext_attr(spinbox, sizeof(lv_spinbox_ext_t));
|
2019-09-24 23:14:17 +02:00
|
|
|
LV_ASSERT_MEM(ext);
|
2019-12-03 18:16:14 +01:00
|
|
|
if(ext == NULL) {
|
2020-02-20 05:08:03 +01:00
|
|
|
lv_obj_del(spinbox);
|
2019-12-03 18:16:14 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-20 05:08:03 +01:00
|
|
|
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(spinbox);
|
|
|
|
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(spinbox);
|
2018-11-05 14:02:13 +01:00
|
|
|
|
|
|
|
/*Initialize the allocated 'ext'*/
|
2019-04-04 07:15:40 +02:00
|
|
|
ext->value = 0;
|
|
|
|
ext->dec_point_pos = 0;
|
|
|
|
ext->digit_count = 5;
|
2019-02-12 07:55:46 +01:00
|
|
|
ext->digit_padding_left = 0;
|
2019-04-04 07:15:40 +02:00
|
|
|
ext->step = 1;
|
|
|
|
ext->range_max = 99999;
|
|
|
|
ext->range_min = -99999;
|
2020-05-01 11:17:43 +02:00
|
|
|
ext->rollover = false;
|
2018-11-05 14:02:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*The signal and design functions are not copied so set them here*/
|
2020-02-20 05:08:03 +01:00
|
|
|
lv_obj_set_signal_cb(spinbox, lv_spinbox_signal);
|
|
|
|
lv_obj_set_design_cb(spinbox, ancestor_design); /*Leave the Text area's design function*/
|
2018-11-05 14:02:13 +01:00
|
|
|
|
2020-04-09 07:11:12 +02:00
|
|
|
/*Init the new spinbox*/
|
2018-11-05 14:02:13 +01:00
|
|
|
if(copy == NULL) {
|
2020-02-20 05:08:03 +01:00
|
|
|
/* No scrolling will happen here so make the scrollable non-clickable
|
|
|
|
* It allows to handle input events in the bg object only.*/
|
2020-05-12 21:29:16 +02:00
|
|
|
lv_obj_set_click(lv_page_get_scrllable(spinbox), false);
|
2020-04-06 16:21:53 +02:00
|
|
|
lv_textarea_set_one_line(spinbox, true);
|
|
|
|
lv_textarea_set_cursor_click_pos(spinbox, true);
|
|
|
|
lv_obj_set_width(spinbox, LV_DPI);
|
|
|
|
lv_theme_apply(spinbox, LV_THEME_SPINBOX);
|
2018-11-05 14:02:13 +01:00
|
|
|
}
|
|
|
|
/*Copy an existing spinbox*/
|
|
|
|
else {
|
|
|
|
lv_spinbox_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
|
|
|
|
|
2020-02-20 05:08:03 +01:00
|
|
|
lv_spinbox_set_value(spinbox, copy_ext->value);
|
|
|
|
lv_spinbox_set_digit_format(spinbox, (uint8_t)copy_ext->digit_count, (uint8_t)copy_ext->dec_point_pos);
|
|
|
|
lv_spinbox_set_range(spinbox, copy_ext->range_min, copy_ext->range_max);
|
|
|
|
lv_spinbox_set_step(spinbox, copy_ext->step);
|
2020-04-06 19:42:26 -04:00
|
|
|
lv_spinbox_set_rollover(spinbox, copy_ext->rollover);
|
2018-11-04 20:21:51 +01:00
|
|
|
|
2018-11-05 14:02:13 +01:00
|
|
|
/*Refresh the style with new signal function*/
|
2020-03-10 10:41:39 +01:00
|
|
|
lv_obj_refresh_style(spinbox, LV_STYLE_PROP_ALL);
|
2018-11-05 14:02:13 +01:00
|
|
|
}
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2020-02-20 05:08:03 +01:00
|
|
|
lv_spinbox_updatevalue(spinbox);
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2018-11-05 14:02:13 +01:00
|
|
|
LV_LOG_INFO("spinbox created");
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2020-02-20 05:08:03 +01:00
|
|
|
return spinbox;
|
2018-11-01 18:48:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Setter functions
|
|
|
|
*====================*/
|
|
|
|
|
2020-03-25 16:17:01 +01:00
|
|
|
/**
|
|
|
|
* Set spinbox rollover function
|
|
|
|
* @param spinbox pointer to spinbox
|
|
|
|
* @param b true or false to enable or disable (default)
|
|
|
|
*/
|
2020-05-01 11:17:43 +02:00
|
|
|
void lv_spinbox_set_rollover(lv_obj_t * spinbox, bool b)
|
|
|
|
{
|
2020-03-25 16:17:01 +01:00
|
|
|
LV_ASSERT_OBJ(spinbox, LV_OBJX_NAME);
|
|
|
|
|
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
|
|
|
|
2020-05-01 11:17:43 +02:00
|
|
|
ext->rollover = b;
|
2020-03-25 16:17:01 +01:00
|
|
|
}
|
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
/**
|
|
|
|
* Set spinbox value
|
|
|
|
* @param spinbox pointer to spinbox
|
|
|
|
* @param i value to be set
|
|
|
|
*/
|
2018-12-20 00:01:05 +01:00
|
|
|
void lv_spinbox_set_value(lv_obj_t * spinbox, int32_t i)
|
2018-11-01 18:48:13 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(spinbox, LV_OBJX_NAME);
|
|
|
|
|
2018-11-05 14:02:13 +01:00
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
2019-04-04 07:15:40 +02:00
|
|
|
if(ext == NULL) return;
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
if(i > ext->range_max) i = ext->range_max;
|
|
|
|
if(i < ext->range_min) i = ext->range_min;
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2018-11-05 14:02:13 +01:00
|
|
|
ext->value = i;
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2018-11-05 14:02:13 +01:00
|
|
|
lv_spinbox_updatevalue(spinbox);
|
2018-11-01 18:48:13 +01:00
|
|
|
}
|
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
/**
|
|
|
|
* Set spinbox digit format (digit count and decimal format)
|
|
|
|
* @param spinbox pointer to spinbox
|
|
|
|
* @param digit_count number of digit excluding the decimal separator and the sign
|
2019-04-04 07:15:40 +02:00
|
|
|
* @param separator_position number of digit before the decimal point. If 0, decimal point is not
|
|
|
|
* shown
|
2018-11-04 20:21:51 +01:00
|
|
|
*/
|
2019-06-06 06:05:40 +02:00
|
|
|
void lv_spinbox_set_digit_format(lv_obj_t * spinbox, uint8_t digit_count, uint8_t separator_position)
|
2018-11-01 18:48:13 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(spinbox, LV_OBJX_NAME);
|
|
|
|
|
2018-11-05 14:02:13 +01:00
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
2019-04-04 07:15:40 +02:00
|
|
|
if(ext == NULL) return;
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
if(digit_count > LV_SPINBOX_MAX_DIGIT_COUNT) digit_count = LV_SPINBOX_MAX_DIGIT_COUNT;
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2020-02-20 05:08:03 +01:00
|
|
|
if(separator_position >= digit_count) separator_position = 0;
|
2019-06-06 06:05:40 +02:00
|
|
|
if(separator_position > LV_SPINBOX_MAX_DIGIT_COUNT) separator_position = LV_SPINBOX_MAX_DIGIT_COUNT;
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2020-05-18 16:57:23 +02:00
|
|
|
if(digit_count < LV_SPINBOX_MAX_DIGIT_COUNT) {
|
2020-05-24 13:13:07 +02:00
|
|
|
int64_t max_val = _lv_pow(10, digit_count);
|
2020-04-29 13:24:07 +02:00
|
|
|
if(ext->range_max > max_val - 1) ext->range_max = max_val - 1;
|
|
|
|
if(ext->range_min < - max_val + 1) ext->range_min = - max_val + 1;
|
|
|
|
}
|
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
ext->digit_count = digit_count;
|
2018-11-05 14:02:13 +01:00
|
|
|
ext->dec_point_pos = separator_position;
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2018-11-05 14:02:13 +01:00
|
|
|
lv_spinbox_updatevalue(spinbox);
|
2018-11-01 18:48:13 +01:00
|
|
|
}
|
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
/**
|
|
|
|
* Set spinbox step
|
|
|
|
* @param spinbox pointer to spinbox
|
|
|
|
* @param step steps on increment/decrement
|
|
|
|
*/
|
2018-12-20 00:01:05 +01:00
|
|
|
void lv_spinbox_set_step(lv_obj_t * spinbox, uint32_t step)
|
2018-11-01 18:48:13 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(spinbox, LV_OBJX_NAME);
|
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
|
|
|
if(ext == NULL) return;
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
ext->step = step;
|
2018-11-01 18:48:13 +01:00
|
|
|
}
|
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
/**
|
|
|
|
* Set spinbox value range
|
|
|
|
* @param spinbox pointer to spinbox
|
|
|
|
* @param range_min maximum value, inclusive
|
|
|
|
* @param range_max minimum value, inclusive
|
|
|
|
*/
|
2018-12-20 00:01:05 +01:00
|
|
|
void lv_spinbox_set_range(lv_obj_t * spinbox, int32_t range_min, int32_t range_max)
|
2018-11-03 18:26:59 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(spinbox, LV_OBJX_NAME);
|
|
|
|
|
2018-11-05 14:02:13 +01:00
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
|
|
|
if(ext == NULL) return;
|
|
|
|
|
|
|
|
ext->range_max = range_max;
|
|
|
|
ext->range_min = range_min;
|
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
if(ext->value > ext->range_max) {
|
2018-11-05 14:02:13 +01:00
|
|
|
ext->value = ext->range_max;
|
|
|
|
lv_obj_invalidate(spinbox);
|
|
|
|
}
|
2019-04-04 07:15:40 +02:00
|
|
|
if(ext->value < ext->range_min) {
|
2018-11-05 14:02:13 +01:00
|
|
|
ext->value = ext->range_min;
|
|
|
|
lv_obj_invalidate(spinbox);
|
|
|
|
}
|
2020-04-12 14:49:02 +02:00
|
|
|
|
|
|
|
lv_spinbox_updatevalue(spinbox);
|
2018-11-03 18:26:59 +01:00
|
|
|
}
|
|
|
|
|
2018-11-05 14:48:33 +01:00
|
|
|
/**
|
|
|
|
* Set spinbox left padding in digits count (added between sign and first digit)
|
|
|
|
* @param spinbox pointer to spinbox
|
|
|
|
* @param cb Callback function called on value change event
|
|
|
|
*/
|
2018-12-20 00:01:05 +01:00
|
|
|
void lv_spinbox_set_padding_left(lv_obj_t * spinbox, uint8_t padding)
|
2018-11-05 14:48:33 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(spinbox, LV_OBJX_NAME);
|
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
2018-11-05 14:48:33 +01:00
|
|
|
ext->digit_padding_left = padding;
|
|
|
|
lv_spinbox_updatevalue(spinbox);
|
|
|
|
}
|
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
2018-11-03 18:26:59 +01:00
|
|
|
|
2020-03-25 16:17:01 +01:00
|
|
|
/**
|
|
|
|
* Get spinbox rollover function status
|
|
|
|
* @param spinbox pointer to spinbox
|
|
|
|
*/
|
2020-05-01 11:17:43 +02:00
|
|
|
bool lv_spinbox_get_rollover(lv_obj_t * spinbox)
|
|
|
|
{
|
2020-03-25 16:17:01 +01:00
|
|
|
LV_ASSERT_OBJ(spinbox, LV_OBJX_NAME);
|
|
|
|
|
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
|
|
|
|
2020-05-01 11:17:43 +02:00
|
|
|
return ext->rollover;
|
2020-03-25 16:17:01 +01:00
|
|
|
}
|
|
|
|
|
2018-11-01 18:48:13 +01:00
|
|
|
/**
|
2018-11-04 20:21:51 +01:00
|
|
|
* Get the spinbox numeral value (user has to convert to float according to its digit format)
|
|
|
|
* @param spinbox pointer to spinbox
|
|
|
|
* @return value integer value of the spinbox
|
2018-11-01 18:48:13 +01:00
|
|
|
*/
|
2018-12-20 00:01:05 +01:00
|
|
|
int32_t lv_spinbox_get_value(lv_obj_t * spinbox)
|
2018-11-01 18:48:13 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(spinbox, LV_OBJX_NAME);
|
|
|
|
|
2018-11-05 14:02:13 +01:00
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2018-11-05 14:02:13 +01:00
|
|
|
return ext->value;
|
2018-11-01 18:48:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*=====================
|
2018-11-04 20:21:51 +01:00
|
|
|
* Other functions
|
2018-11-01 18:48:13 +01:00
|
|
|
*====================*/
|
|
|
|
|
|
|
|
/**
|
2019-06-19 18:41:29 +02:00
|
|
|
* Select next lower digit for edition
|
2018-11-04 20:21:51 +01:00
|
|
|
* @param spinbox pointer to spinbox
|
2018-11-01 18:48:13 +01:00
|
|
|
*/
|
2018-11-04 20:21:51 +01:00
|
|
|
void lv_spinbox_step_next(lv_obj_t * spinbox)
|
2018-11-01 18:48:13 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(spinbox, LV_OBJX_NAME);
|
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2019-02-15 06:27:49 +01:00
|
|
|
int32_t new_step = ext->step / 10;
|
2019-04-04 07:15:40 +02:00
|
|
|
if((new_step) > 0)
|
|
|
|
ext->step = new_step;
|
|
|
|
else
|
|
|
|
ext->step = 1;
|
2018-11-03 18:07:25 +01:00
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
lv_spinbox_updatevalue(spinbox);
|
2018-11-01 18:48:13 +01:00
|
|
|
}
|
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
/**
|
2019-06-19 18:41:29 +02:00
|
|
|
* Select next higher digit for edition
|
2018-11-04 20:21:51 +01:00
|
|
|
* @param spinbox pointer to spinbox
|
|
|
|
*/
|
2019-06-25 15:14:47 +02:00
|
|
|
void lv_spinbox_step_prev(lv_obj_t * spinbox)
|
2018-11-01 18:48:13 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(spinbox, LV_OBJX_NAME);
|
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
2019-02-15 06:34:19 +01:00
|
|
|
int32_t step_limit;
|
2019-06-06 06:05:40 +02:00
|
|
|
step_limit = LV_MATH_MAX(ext->range_max, (ext->range_min < 0 ? (-ext->range_min) : ext->range_min));
|
2019-02-15 06:27:49 +01:00
|
|
|
int32_t new_step = ext->step * 10;
|
2019-02-15 06:34:19 +01:00
|
|
|
if(new_step <= step_limit) ext->step = new_step;
|
2018-11-01 18:48:13 +01:00
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
lv_spinbox_updatevalue(spinbox);
|
2018-11-01 18:48:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-04 20:21:51 +01:00
|
|
|
* Increment spinbox value by one step
|
|
|
|
* @param spinbox pointer to spinbox
|
2018-11-01 18:48:13 +01:00
|
|
|
*/
|
2018-11-04 20:21:51 +01:00
|
|
|
void lv_spinbox_increment(lv_obj_t * spinbox)
|
2018-11-01 18:48:13 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(spinbox, LV_OBJX_NAME);
|
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
|
|
|
|
2019-02-15 06:27:49 +01:00
|
|
|
if(ext->value + ext->step <= ext->range_max) {
|
2018-11-04 20:21:51 +01:00
|
|
|
/*Special mode when zero crossing*/
|
2019-02-15 06:27:49 +01:00
|
|
|
if((ext->value + ext->step) > 0 && ext->value < 0) ext->value = -ext->value;
|
2018-11-04 20:21:51 +01:00
|
|
|
ext->value += ext->step;
|
2018-11-05 14:02:13 +01:00
|
|
|
|
2020-05-01 11:17:43 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Rollover?
|
|
|
|
if((ext->rollover) && (ext->value == ext->range_max))
|
|
|
|
ext->value = ext->range_min;
|
|
|
|
else
|
|
|
|
ext->value = ext->range_max;
|
2018-11-04 20:21:51 +01:00
|
|
|
}
|
2019-02-15 06:27:49 +01:00
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
lv_spinbox_updatevalue(spinbox);
|
2018-11-01 18:48:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-04 20:21:51 +01:00
|
|
|
* Decrement spinbox value by one step
|
|
|
|
* @param spinbox pointer to spinbox
|
2018-11-01 18:48:13 +01:00
|
|
|
*/
|
2018-11-04 20:21:51 +01:00
|
|
|
void lv_spinbox_decrement(lv_obj_t * spinbox)
|
2018-11-01 18:48:13 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(spinbox, LV_OBJX_NAME);
|
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
|
|
|
|
2019-02-15 06:27:49 +01:00
|
|
|
if(ext->value - ext->step >= ext->range_min) {
|
2018-11-04 20:21:51 +01:00
|
|
|
/*Special mode when zero crossing*/
|
2019-02-15 06:27:49 +01:00
|
|
|
if((ext->value - ext->step) < 0 && ext->value > 0) ext->value = -ext->value;
|
2018-11-04 20:21:51 +01:00
|
|
|
ext->value -= ext->step;
|
2020-05-01 11:17:43 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Rollover?
|
|
|
|
if((ext->rollover) && (ext->value == ext->range_min))
|
|
|
|
ext->value = ext->range_max;
|
|
|
|
else
|
|
|
|
ext->value = ext->range_min;
|
2018-11-04 20:21:51 +01:00
|
|
|
}
|
2019-02-15 06:27:49 +01:00
|
|
|
|
2018-11-04 20:21:51 +01:00
|
|
|
lv_spinbox_updatevalue(spinbox);
|
2018-11-01 18:48:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signal function of the spinbox
|
2018-11-03 18:41:06 +01:00
|
|
|
* @param spinbox pointer to a spinbox object
|
2018-11-01 18:48:13 +01:00
|
|
|
* @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_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * param)
|
|
|
|
{
|
|
|
|
|
2019-01-03 07:16:25 -05:00
|
|
|
lv_res_t res = LV_RES_OK;
|
2020-01-07 23:43:57 +01:00
|
|
|
if(sign == LV_SIGNAL_GET_STYLE) {
|
|
|
|
lv_get_style_info_t * info = param;
|
|
|
|
info->result = lv_spinbox_get_style(spinbox, info->part);
|
|
|
|
if(info->result != NULL) return LV_RES_OK;
|
|
|
|
else return ancestor_signal(spinbox, sign, param);
|
|
|
|
}
|
2018-11-05 14:02:13 +01:00
|
|
|
|
|
|
|
/* Include the ancient signal function */
|
2019-04-04 07:15:40 +02:00
|
|
|
if(sign != LV_SIGNAL_CONTROL) {
|
2018-11-05 14:02:13 +01:00
|
|
|
res = ancestor_signal(spinbox, sign, param);
|
|
|
|
if(res != LV_RES_OK) return res;
|
|
|
|
}
|
2019-09-26 12:54:40 +02:00
|
|
|
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
|
2018-11-05 14:02:13 +01:00
|
|
|
|
2019-11-26 14:14:04 +01:00
|
|
|
|
2018-11-05 14:02:13 +01:00
|
|
|
if(sign == LV_SIGNAL_CLEANUP) {
|
|
|
|
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_GET_TYPE) {
|
2018-11-05 14:02:13 +01:00
|
|
|
lv_obj_type_t * buf = param;
|
|
|
|
uint8_t i;
|
2019-04-04 07:15:40 +02:00
|
|
|
for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
|
2018-11-05 14:02:13 +01:00
|
|
|
if(buf->type[i] == NULL) break;
|
|
|
|
}
|
|
|
|
buf->type[i] = "lv_spinbox";
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_RELEASED) {
|
2019-06-27 08:33:14 +02:00
|
|
|
/*If released with an ENCODER then move to the next digit*/
|
2019-12-26 03:37:28 +01:00
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
2019-03-12 19:20:23 +01:00
|
|
|
lv_indev_t * indev = lv_indev_get_act();
|
|
|
|
if(lv_indev_get_type(indev) == LV_INDEV_TYPE_ENCODER) {
|
2020-02-20 05:08:03 +01:00
|
|
|
#if LV_USE_GROUP
|
2019-03-12 19:20:23 +01:00
|
|
|
if(lv_group_get_editing(lv_obj_get_group(spinbox))) {
|
|
|
|
if(ext->step > 1) {
|
|
|
|
lv_spinbox_step_next(spinbox);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-03-12 19:20:23 +01:00
|
|
|
/*Restart from the MSB*/
|
|
|
|
ext->step = 1;
|
|
|
|
uint32_t i;
|
|
|
|
for(i = 0; i < ext->digit_count; i++) {
|
|
|
|
int32_t new_step = ext->step * 10;
|
|
|
|
if(new_step >= ext->range_max) break;
|
|
|
|
ext->step = new_step;
|
|
|
|
}
|
2019-06-25 15:14:47 +02:00
|
|
|
lv_spinbox_step_prev(spinbox);
|
2019-03-12 19:20:23 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 08:33:14 +02:00
|
|
|
#endif
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-02-20 05:08:03 +01:00
|
|
|
/*The cursor has been positioned to a digit.
|
|
|
|
* Set `step` accordingly*/
|
|
|
|
const char * txt = lv_textarea_get_text(spinbox);
|
|
|
|
size_t txt_len = strlen(txt);
|
|
|
|
|
|
|
|
if(txt[ext->ta.cursor.pos] == '.') {
|
|
|
|
lv_textarea_cursor_left(spinbox);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
2020-06-01 22:56:10 +02:00
|
|
|
else if(ext->ta.cursor.pos == (uint32_t)txt_len) {
|
2020-02-20 05:08:03 +01:00
|
|
|
lv_textarea_set_cursor_pos(spinbox, txt_len - 1);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(ext->ta.cursor.pos == 0 && ext->range_min < 0) {
|
2020-02-20 05:08:03 +01:00
|
|
|
lv_textarea_set_cursor_pos(spinbox, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t len = ext->digit_count - 1;
|
|
|
|
uint16_t cp = ext->ta.cursor.pos;
|
|
|
|
|
|
|
|
if(ext->ta.cursor.pos > ext->dec_point_pos && ext->dec_point_pos != 0) cp--;
|
|
|
|
uint32_t pos = len - cp;
|
|
|
|
|
|
|
|
if(ext->range_min < 0) pos++;
|
|
|
|
|
|
|
|
ext->step = 1;
|
|
|
|
uint16_t i;
|
|
|
|
for(i = 0; i < pos; i++) ext->step *= 10;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_CONTROL) {
|
2019-03-29 16:10:18 +01:00
|
|
|
lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
|
2018-11-05 14:02:13 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
uint32_t c = *((uint32_t *)param); /*uint32_t because can be UTF-8*/
|
2019-04-08 14:36:20 +02:00
|
|
|
if(c == LV_KEY_RIGHT) {
|
2019-04-04 07:15:40 +02:00
|
|
|
if(indev_type == LV_INDEV_TYPE_ENCODER)
|
|
|
|
lv_spinbox_increment(spinbox);
|
|
|
|
else
|
|
|
|
lv_spinbox_step_next(spinbox);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(c == LV_KEY_LEFT) {
|
2019-04-04 07:15:40 +02:00
|
|
|
if(indev_type == LV_INDEV_TYPE_ENCODER)
|
|
|
|
lv_spinbox_decrement(spinbox);
|
|
|
|
else
|
2019-06-25 15:14:47 +02:00
|
|
|
lv_spinbox_step_prev(spinbox);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(c == LV_KEY_UP) {
|
2018-11-05 14:02:13 +01:00
|
|
|
lv_spinbox_increment(spinbox);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(c == LV_KEY_DOWN) {
|
2018-11-05 14:02:13 +01:00
|
|
|
lv_spinbox_decrement(spinbox);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_textarea_add_char(spinbox, c);
|
2019-02-15 06:27:49 +01:00
|
|
|
}
|
2018-11-05 14:02:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2018-11-01 18:48:13 +01:00
|
|
|
}
|
|
|
|
|
2020-01-07 23:43:57 +01:00
|
|
|
/**
|
|
|
|
* Get the style descriptor of a part of the object
|
|
|
|
* @param page pointer the object
|
|
|
|
* @param part the part from `lv_spinbox_part_t`. (LV_SPINBOX_PART_...)
|
|
|
|
* @return pointer to the style descriptor of the specified part
|
|
|
|
*/
|
2020-01-16 14:26:36 +01:00
|
|
|
static lv_style_list_t * lv_spinbox_get_style(lv_obj_t * ta, uint8_t part)
|
2020-01-07 23:43:57 +01:00
|
|
|
{
|
|
|
|
LV_ASSERT_OBJ(ta, LV_OBJX_NAME);
|
|
|
|
|
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(ta);
|
2020-01-16 14:26:36 +01:00
|
|
|
lv_style_list_t * style_dsc_p;
|
2020-01-07 23:43:57 +01:00
|
|
|
|
|
|
|
switch(part) {
|
2020-02-26 19:48:27 +01:00
|
|
|
case LV_SPINBOX_PART_BG:
|
|
|
|
style_dsc_p = &ta->style_list;
|
|
|
|
break;
|
|
|
|
case LV_SPINBOX_PART_CURSOR:
|
|
|
|
style_dsc_p = &ext->ta.cursor.style;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
style_dsc_p = NULL;
|
2020-01-07 23:43:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return style_dsc_p;
|
|
|
|
}
|
2018-12-20 00:01:05 +01:00
|
|
|
static void lv_spinbox_updatevalue(lv_obj_t * spinbox)
|
2018-11-01 18:48:13 +01:00
|
|
|
{
|
2018-11-05 14:02:13 +01:00
|
|
|
lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox);
|
|
|
|
|
2019-02-15 06:27:49 +01:00
|
|
|
char buf[LV_SPINBOX_MAX_DIGIT_COUNT + 8];
|
2020-05-13 14:11:16 +02:00
|
|
|
_lv_memset_00(buf, sizeof(buf));
|
2019-02-15 06:27:49 +01:00
|
|
|
char * buf_p = buf;
|
2019-12-13 15:46:55 +01:00
|
|
|
uint8_t cur_shift_left = 0;
|
2018-11-05 14:02:13 +01:00
|
|
|
|
2020-02-26 19:48:27 +01:00
|
|
|
if(ext->range_min < 0) { // hide sign if there are only positive values
|
2019-10-06 08:29:15 +02:00
|
|
|
/*Add the sign*/
|
|
|
|
(*buf_p) = ext->value >= 0 ? '+' : '-';
|
2019-02-15 06:27:49 +01:00
|
|
|
buf_p++;
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*Cursor need shift to left*/
|
|
|
|
cur_shift_left++;
|
2019-10-08 06:25:40 +02:00
|
|
|
}
|
2018-11-05 14:02:13 +01:00
|
|
|
|
2019-12-02 12:20:01 +01:00
|
|
|
int32_t i;
|
2019-02-15 06:27:49 +01:00
|
|
|
/*padding left*/
|
|
|
|
for(i = 0; i < ext->digit_padding_left; i++) {
|
|
|
|
(*buf_p) = ' ';
|
|
|
|
buf_p++;
|
2018-11-05 14:48:33 +01:00
|
|
|
}
|
|
|
|
|
2019-02-15 06:27:49 +01:00
|
|
|
char digits[64];
|
|
|
|
/*Convert the numbers to string (the sign is already handled so always covert positive number)*/
|
2020-05-13 14:11:16 +02:00
|
|
|
_lv_utils_num_to_str(ext->value < 0 ? -ext->value : ext->value, digits);
|
2019-02-12 07:55:46 +01:00
|
|
|
|
2019-02-15 06:27:49 +01:00
|
|
|
/*Add leading zeros*/
|
|
|
|
int lz_cnt = ext->digit_count - (int)strlen(digits);
|
|
|
|
if(lz_cnt > 0) {
|
2019-12-02 12:20:01 +01:00
|
|
|
for(i = (uint16_t)strlen(digits); i >= 0; i--) {
|
2019-02-15 06:27:49 +01:00
|
|
|
digits[i + lz_cnt] = digits[i];
|
|
|
|
}
|
|
|
|
for(i = 0; i < lz_cnt; i++) {
|
|
|
|
digits[i] = '0';
|
|
|
|
}
|
2018-11-05 14:02:13 +01:00
|
|
|
}
|
|
|
|
|
2019-02-15 06:27:49 +01:00
|
|
|
int32_t intDigits;
|
|
|
|
intDigits = (ext->dec_point_pos == 0) ? ext->digit_count : ext->dec_point_pos;
|
|
|
|
|
|
|
|
/*Add the decimal part*/
|
|
|
|
for(i = 0; i < intDigits && digits[i] != '\0'; i++) {
|
|
|
|
(*buf_p) = digits[i];
|
|
|
|
buf_p++;
|
2018-11-05 14:02:13 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 07:55:46 +01:00
|
|
|
if(ext->dec_point_pos != 0) {
|
2019-02-15 06:27:49 +01:00
|
|
|
/*Insert the decimal point*/
|
|
|
|
(*buf_p) = '.';
|
|
|
|
buf_p++;
|
2019-02-12 07:55:46 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
for(/*Leave i*/; i < ext->digit_count && digits[i] != '\0'; i++) {
|
2019-02-15 06:27:49 +01:00
|
|
|
(*buf_p) = digits[i];
|
|
|
|
buf_p++;
|
2019-02-12 07:55:46 +01:00
|
|
|
}
|
2018-11-05 14:02:13 +01:00
|
|
|
}
|
|
|
|
|
2019-02-15 06:27:49 +01:00
|
|
|
/*Refresh the text*/
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_textarea_set_text(spinbox, (char *)buf);
|
2018-11-05 14:02:13 +01:00
|
|
|
|
2019-02-15 06:27:49 +01:00
|
|
|
/*Set the cursor position*/
|
2019-04-04 07:15:40 +02:00
|
|
|
int32_t step = ext->step;
|
2019-12-02 12:20:01 +01:00
|
|
|
uint8_t cur_pos = (uint8_t)ext->digit_count;
|
2019-04-04 07:15:40 +02:00
|
|
|
while(step >= 10) {
|
2018-11-05 14:02:13 +01:00
|
|
|
step /= 10;
|
2019-02-15 06:27:49 +01:00
|
|
|
cur_pos--;
|
2018-11-05 14:02:13 +01:00
|
|
|
}
|
2018-11-05 14:48:33 +01:00
|
|
|
|
2020-02-20 05:08:03 +01:00
|
|
|
if(cur_pos > intDigits) cur_pos++; /*Skip the decimal point*/
|
2019-02-15 06:27:49 +01:00
|
|
|
|
2019-12-13 15:46:55 +01:00
|
|
|
cur_pos += (ext->digit_padding_left - cur_shift_left);
|
2018-11-05 14:02:13 +01:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_textarea_set_cursor_pos(spinbox, cur_pos);
|
2018-11-01 18:48:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|