2016-09-30 15:29:00 +02:00
|
|
|
/**
|
|
|
|
* @file lv_ta.c
|
2018-06-19 09:49:58 +02:00
|
|
|
*
|
2016-09-30 15:29:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
2020-02-14 12:44:15 +01:00
|
|
|
#include "lv_textarea.h"
|
2020-02-14 12:36:44 +01:00
|
|
|
#if LV_USE_TEXTAREA != 0
|
|
|
|
|
2019-03-20 05:46:21 +01:00
|
|
|
#include <string.h>
|
2020-06-08 13:10:43 +02:00
|
|
|
#include "../lv_misc/lv_debug.h"
|
2017-11-30 11:35:33 +01:00
|
|
|
#include "../lv_core/lv_group.h"
|
2018-12-30 15:02:16 +01:00
|
|
|
#include "../lv_core/lv_refr.h"
|
2020-09-30 06:12:29 +02:00
|
|
|
#include "../lv_core/lv_indev.h"
|
2016-10-04 09:45:39 +02:00
|
|
|
#include "../lv_draw/lv_draw.h"
|
2017-11-23 20:42:14 +01:00
|
|
|
#include "../lv_misc/lv_anim.h"
|
2017-11-26 23:57:39 +01:00
|
|
|
#include "../lv_misc/lv_txt.h"
|
2017-11-23 20:42:14 +01:00
|
|
|
#include "../lv_misc/lv_math.h"
|
2016-09-30 15:29:00 +02:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2021-02-04 14:46:11 +01:00
|
|
|
#define MY_CLASS &lv_textarea
|
2017-01-12 09:49:13 +01:00
|
|
|
|
2019-09-26 10:51:54 +02:00
|
|
|
/*Test configuration*/
|
2020-02-14 12:36:44 +01:00
|
|
|
#ifndef LV_TEXTAREA_DEF_CURSOR_BLINK_TIME
|
2020-02-26 19:48:27 +01:00
|
|
|
#define LV_TEXTAREA_DEF_CURSOR_BLINK_TIME 400 /*ms*/
|
2017-01-12 09:49:13 +01:00
|
|
|
#endif
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
#ifndef LV_TEXTAREA_DEF_PWD_SHOW_TIME
|
2020-02-26 19:48:27 +01:00
|
|
|
#define LV_TEXTAREA_DEF_PWD_SHOW_TIME 1500 /*ms*/
|
2017-07-07 16:22:24 +02:00
|
|
|
#endif
|
|
|
|
|
2021-02-04 14:46:11 +01:00
|
|
|
#define LV_TEXTAREA_DEF_WIDTH (2 * LV_DPI_DEF)
|
|
|
|
#define LV_TEXTAREA_DEF_HEIGHT (1 * LV_DPI_DEF)
|
2016-09-30 15:29:00 +02:00
|
|
|
|
2020-02-17 16:04:16 +01:00
|
|
|
#define LV_TEXTAREA_PWD_BULLET_UNICODE 0x2022
|
|
|
|
|
2016-09-30 15:29:00 +02:00
|
|
|
/**********************
|
|
|
|
* TYPEDEFS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC PROTOTYPES
|
|
|
|
**********************/
|
2021-01-23 23:03:50 +01:00
|
|
|
static void lv_textarea_constructor(lv_obj_t * obj, lv_obj_t * parent, const lv_obj_t * copy);
|
|
|
|
static void lv_textarea_destructor(lv_obj_t * obj);
|
|
|
|
static lv_draw_res_t lv_textarea_draw(lv_obj_t * obj, const lv_area_t * clip_area, lv_draw_mode_t mode);
|
|
|
|
static lv_res_t lv_textarea_signal(lv_obj_t * obj, lv_signal_t sign, void * param);
|
2021-01-26 15:09:36 +01:00
|
|
|
static void cursor_blink_anim_cb(lv_obj_t * obj, lv_anim_value_t show);
|
|
|
|
static void pwd_char_hider_anim(lv_obj_t * obj, lv_anim_value_t x);
|
|
|
|
static void pwd_char_hider_anim_ready(lv_anim_t * a);
|
2021-01-23 23:03:50 +01:00
|
|
|
static void pwd_char_hider(lv_obj_t * obj);
|
|
|
|
static bool char_is_accepted(lv_obj_t * obj, uint32_t c);
|
|
|
|
static void start_cursor_blink(lv_obj_t * obj);
|
|
|
|
static void refr_cursor_area(lv_obj_t * obj);
|
|
|
|
static void update_cursor_position_on_click(lv_obj_t * obj, lv_signal_t sign, lv_indev_t * click_source);
|
2021-02-04 14:46:11 +01:00
|
|
|
static lv_res_t insert_handler(lv_obj_t * obj, char * txt);
|
2021-01-23 23:03:50 +01:00
|
|
|
static void draw_placeholder(lv_obj_t * obj, const lv_area_t * clip_area);
|
|
|
|
static void draw_cursor(lv_obj_t * obj, const lv_area_t * clip_area);
|
2016-09-30 15:29:00 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
2021-01-23 23:03:50 +01:00
|
|
|
const lv_obj_class_t lv_textarea = {
|
2021-02-04 14:46:11 +01:00
|
|
|
.constructor_cb = lv_textarea_constructor,
|
|
|
|
.destructor_cb = lv_textarea_destructor,
|
2021-01-23 23:03:50 +01:00
|
|
|
.signal_cb = lv_textarea_signal,
|
|
|
|
.draw_cb = lv_textarea_draw,
|
2021-02-04 14:46:11 +01:00
|
|
|
.editable = LV_OBJ_CLASS_EDITABLE_TRUE,
|
2021-01-23 23:03:50 +01:00
|
|
|
.instance_size = sizeof(lv_textarea_t),
|
|
|
|
.base_class = &lv_obj
|
|
|
|
};
|
|
|
|
|
2019-03-20 05:46:21 +01:00
|
|
|
static const char * ta_insert_replace;
|
2016-09-30 15:29:00 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a text area objects
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param par pointer to an object, it will be the parent of the new text area
|
|
|
|
* @param copy pointer to a text area object, if not NULL then the new object will be copied from it
|
2016-09-30 15:29:00 +02:00
|
|
|
* @return pointer to the created text area
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_obj_t * lv_textarea_create(lv_obj_t * parent, const lv_obj_t * copy)
|
2016-09-30 15:29:00 +02:00
|
|
|
{
|
2021-01-23 23:03:50 +01:00
|
|
|
return lv_obj_create_from_class(&lv_textarea, parent, copy);
|
2016-09-30 15:29:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-15 15:50:33 +01:00
|
|
|
/*======================
|
|
|
|
* Add/remove functions
|
|
|
|
*=====================*/
|
2016-09-30 15:29:00 +02:00
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/**
|
2018-08-09 23:37:00 +02:00
|
|
|
* Insert a character to the current cursor position.
|
2020-06-29 20:46:49 +02:00
|
|
|
* To add a wide char, e.g. 'Á' use `_lv_txt_encoded_conv_wc('Á')`
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to a text area object
|
2018-08-09 23:37:00 +02:00
|
|
|
* @param c a character (e.g. 'a')
|
2016-10-04 09:45:39 +02:00
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_add_char(lv_obj_t * obj, uint32_t c)
|
2016-09-30 15:29:00 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2020-07-07 09:36:59 +02:00
|
|
|
const char * letter_buf;
|
2020-07-01 10:08:45 -03:00
|
|
|
|
|
|
|
uint32_t u32_buf[2];
|
|
|
|
u32_buf[0] = c;
|
|
|
|
u32_buf[1] = 0;
|
2020-07-07 09:36:59 +02:00
|
|
|
|
|
|
|
letter_buf = (char *)&u32_buf;
|
2020-07-01 10:08:45 -03:00
|
|
|
|
|
|
|
#if LV_BIG_ENDIAN_SYSTEM
|
2020-07-07 09:36:59 +02:00
|
|
|
if(c != 0) while(*letter_buf == 0) ++letter_buf;
|
2020-07-01 10:08:45 -03:00
|
|
|
#endif
|
2019-03-20 05:46:21 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_res_t res = insert_handler(obj, letter_buf);
|
2020-07-22 16:28:03 +02:00
|
|
|
if(res != LV_RES_OK) return;
|
2020-08-04 10:07:29 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->one_line && (c == '\n' || c == '\r')) {
|
2018-10-05 17:22:49 +02:00
|
|
|
LV_LOG_INFO("Text area: line break ignored in one-line mode");
|
|
|
|
return;
|
2018-07-25 19:36:53 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 14:11:16 +02:00
|
|
|
uint32_t c_uni = _lv_txt_encoded_next((const char *)&c, NULL);
|
2018-08-09 23:37:00 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(char_is_accepted(obj, c_uni) == false) {
|
2019-04-04 07:15:40 +02:00
|
|
|
LV_LOG_INFO("Character is no accepted by the text area (too long text or not in the "
|
|
|
|
"accepted list)");
|
2018-10-05 17:22:49 +02:00
|
|
|
return;
|
2018-08-04 08:46:44 +02:00
|
|
|
}
|
2020-08-04 10:07:29 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->pwd_mode != 0) pwd_char_hider(obj); /*Make sure all the current text contains only '*'*/
|
2017-07-07 16:22:24 +02:00
|
|
|
|
2020-02-25 09:15:56 +01:00
|
|
|
/*If the textarea is empty, invalidate it to hide the placeholder*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->placeholder_txt) {
|
|
|
|
const char * txt = lv_label_get_text(ta->label);
|
|
|
|
if(txt[0] == '\0') lv_obj_invalidate(obj);
|
2020-02-25 09:15:56 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_label_ins_text(ta->label, ta->cursor.pos, letter_buf); /*Insert the character*/
|
|
|
|
lv_textarea_clear_selection(obj); /*Clear selection*/
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->pwd_mode != 0) {
|
|
|
|
ta->pwd_tmp = lv_mem_realloc(ta->pwd_tmp, strlen(ta->pwd_tmp) + strlen(letter_buf) + 1); /*+2: the new char + \0 */
|
|
|
|
LV_ASSERT_MEM(ta->pwd_tmp);
|
|
|
|
if(ta->pwd_tmp == NULL) return;
|
2018-07-25 14:43:46 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
_lv_txt_ins(ta->pwd_tmp, ta->cursor.pos, (const char *)letter_buf);
|
2017-08-29 15:08:18 +02:00
|
|
|
|
2017-11-27 17:48:54 +01:00
|
|
|
/*Auto hide characters*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->pwd_show_time == 0) {
|
|
|
|
pwd_char_hider(obj);
|
2020-09-11 14:25:33 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
lv_anim_path_t path;
|
|
|
|
lv_anim_path_init(&path);
|
|
|
|
lv_anim_path_set_cb(&path, lv_anim_path_step);
|
|
|
|
|
|
|
|
lv_anim_t a;
|
|
|
|
lv_anim_init(&a);
|
|
|
|
lv_anim_set_var(&a, ta);
|
|
|
|
lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)pwd_char_hider_anim);
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_anim_set_time(&a, ta->pwd_show_time);
|
2020-09-11 14:25:33 +02:00
|
|
|
lv_anim_set_values(&a, 0, 1);
|
|
|
|
lv_anim_set_path(&a, &path);
|
|
|
|
lv_anim_set_ready_cb(&a, pwd_char_hider_anim_ready);
|
|
|
|
lv_anim_start(&a);
|
2020-09-15 11:14:32 +02:00
|
|
|
}
|
2017-07-07 16:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-08-29 15:08:18 +02:00
|
|
|
/*Move the cursor after the new character*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_set_cursor_pos(obj, lv_textarea_get_cursor_pos(obj) + 1);
|
2018-11-24 20:31:06 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
|
2016-10-01 00:25:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/**
|
|
|
|
* Insert a text to the current cursor position
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
* @param txt a '\0' terminated string to insert
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_add_text(lv_obj_t * obj, const char * txt)
|
2016-10-01 00:25:10 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_NULL(txt);
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2017-11-05 14:12:50 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->pwd_mode != 0) pwd_char_hider(obj); /*Make sure all the current text contains only '*'*/
|
2018-07-25 21:52:50 +02:00
|
|
|
|
2018-08-09 23:37:00 +02:00
|
|
|
/*Add the character one-by-one if not all characters are accepted or there is character limit.*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(lv_textarea_get_accepted_chars(obj) || lv_textarea_get_max_length(obj)) {
|
2018-10-05 17:22:49 +02:00
|
|
|
uint32_t i = 0;
|
|
|
|
while(txt[i] != '\0') {
|
2020-05-13 14:11:16 +02:00
|
|
|
uint32_t c = _lv_txt_encoded_next(txt, &i);
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_add_char(obj, _lv_txt_unicode_to_encoded(c));
|
2018-10-05 17:22:49 +02:00
|
|
|
}
|
|
|
|
return;
|
2018-08-04 08:46:44 +02:00
|
|
|
}
|
2020-08-04 10:07:29 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_res_t res = insert_handler(obj, txt);
|
2020-07-22 16:28:03 +02:00
|
|
|
if(res != LV_RES_OK) return;
|
|
|
|
|
2020-02-25 09:15:56 +01:00
|
|
|
/*If the textarea is empty, invalidate it to hide the placeholder*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->placeholder_txt) {
|
|
|
|
const char * txt_act = lv_label_get_text(ta->label);
|
|
|
|
if(txt_act[0] == '\0') lv_obj_invalidate(obj);
|
2020-02-25 09:15:56 +01:00
|
|
|
}
|
|
|
|
|
2018-08-04 08:46:44 +02:00
|
|
|
/*Insert the text*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_label_ins_text(ta->label, ta->cursor.pos, txt);
|
|
|
|
lv_textarea_clear_selection(obj);
|
2018-07-25 19:36:53 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->pwd_mode != 0) {
|
|
|
|
ta->pwd_tmp = lv_mem_realloc(ta->pwd_tmp, strlen(ta->pwd_tmp) + strlen(txt) + 1);
|
|
|
|
LV_ASSERT_MEM(ta->pwd_tmp);
|
|
|
|
if(ta->pwd_tmp == NULL) return;
|
2017-09-22 13:58:01 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
_lv_txt_ins(ta->pwd_tmp, ta->cursor.pos, txt);
|
2017-08-29 15:08:18 +02:00
|
|
|
|
2019-06-06 06:05:40 +02:00
|
|
|
/*Auto hide characters*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->pwd_show_time == 0) {
|
|
|
|
pwd_char_hider(obj);
|
2020-09-11 14:25:33 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
lv_anim_path_t path;
|
|
|
|
lv_anim_path_init(&path);
|
|
|
|
lv_anim_path_set_cb(&path, lv_anim_path_step);
|
|
|
|
|
|
|
|
lv_anim_t a;
|
|
|
|
lv_anim_init(&a);
|
|
|
|
lv_anim_set_var(&a, ta);
|
|
|
|
lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)pwd_char_hider_anim);
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_anim_set_time(&a, ta->pwd_show_time);
|
2020-09-11 14:25:33 +02:00
|
|
|
lv_anim_set_values(&a, 0, 1);
|
|
|
|
lv_anim_set_path(&a, &path);
|
|
|
|
lv_anim_set_ready_cb(&a, pwd_char_hider_anim_ready);
|
|
|
|
lv_anim_start(&a);
|
2020-09-15 11:14:32 +02:00
|
|
|
}
|
2017-07-07 16:22:24 +02:00
|
|
|
}
|
2017-08-29 15:08:18 +02:00
|
|
|
|
|
|
|
/*Move the cursor after the new text*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_set_cursor_pos(obj, lv_textarea_get_cursor_pos(obj) + _lv_txt_get_encoded_length(txt));
|
2018-11-24 20:31:06 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
|
2016-10-01 00:25:10 +02:00
|
|
|
}
|
|
|
|
|
2017-11-15 15:50:33 +01:00
|
|
|
/**
|
|
|
|
* Delete a the left character from the current cursor position
|
|
|
|
* @param ta pointer to a text area object
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_del_char(lv_obj_t * obj)
|
2017-11-15 15:50:33 +01:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
uint32_t cur_pos = ta->cursor.pos;
|
2017-11-15 15:50:33 +01:00
|
|
|
|
|
|
|
if(cur_pos == 0) return;
|
|
|
|
|
2019-04-08 14:36:20 +02:00
|
|
|
char del_buf[2] = {LV_KEY_DEL, '\0'};
|
2019-03-20 05:46:21 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_res_t res = insert_handler(obj, del_buf);
|
2020-07-22 16:28:03 +02:00
|
|
|
if(res != LV_RES_OK) return;
|
2019-03-20 05:46:21 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
char * label_txt = lv_label_get_text(ta->label);
|
2020-01-10 18:16:20 +01:00
|
|
|
|
2017-11-15 15:50:33 +01:00
|
|
|
/*Delete a character*/
|
2021-01-23 23:03:50 +01:00
|
|
|
_lv_txt_cut(label_txt, ta->cursor.pos - 1, 1);
|
2017-11-15 15:50:33 +01:00
|
|
|
/*Refresh the label*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_label_set_text(ta->label, label_txt);
|
|
|
|
lv_textarea_clear_selection(obj);
|
2017-11-15 15:50:33 +01:00
|
|
|
|
2020-02-25 09:15:56 +01:00
|
|
|
/*If the textarea became empty, invalidate it to hide the placeholder*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->placeholder_txt) {
|
|
|
|
const char * txt = lv_label_get_text(ta->label);
|
|
|
|
if(txt[0] == '\0') lv_obj_invalidate(obj);
|
2020-02-25 09:15:56 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->pwd_mode != 0) {
|
|
|
|
uint32_t byte_pos = _lv_txt_encoded_get_byte_id(ta->pwd_tmp, ta->cursor.pos - 1);
|
|
|
|
_lv_txt_cut(ta->pwd_tmp, ta->cursor.pos - 1, _lv_txt_encoded_size(&ta->pwd_tmp[byte_pos]));
|
2019-03-28 06:11:50 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->pwd_tmp = lv_mem_realloc(ta->pwd_tmp, strlen(ta->pwd_tmp) + 1);
|
|
|
|
LV_ASSERT_MEM(ta->pwd_tmp);
|
|
|
|
if(ta->pwd_tmp == NULL) return;
|
2017-11-15 15:50:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*Move the cursor to the place of the deleted character*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_set_cursor_pos(obj, ta->cursor.pos - 1);
|
2019-02-02 01:26:52 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
|
2020-01-10 18:16:20 +01:00
|
|
|
|
2017-11-15 15:50:33 +01:00
|
|
|
}
|
|
|
|
|
2019-02-01 23:53:09 +01:00
|
|
|
/**
|
|
|
|
* Delete the right character from the current cursor position
|
|
|
|
* @param ta pointer to a text area object
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_del_char_forward(lv_obj_t * obj)
|
2019-02-01 23:53:09 +01:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
uint32_t cp = lv_textarea_get_cursor_pos(obj);
|
|
|
|
lv_textarea_set_cursor_pos(obj, cp + 1);
|
|
|
|
if(cp != lv_textarea_get_cursor_pos(obj)) lv_textarea_del_char(obj);
|
2019-02-01 23:53:09 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 15:50:33 +01:00
|
|
|
/*=====================
|
|
|
|
* Setter functions
|
|
|
|
*====================*/
|
|
|
|
|
2016-12-15 10:31:30 +01:00
|
|
|
/**
|
2016-12-18 22:07:03 +01:00
|
|
|
* Set the text of a text area
|
2016-12-15 10:31:30 +01:00
|
|
|
* @param ta pointer to a text area
|
|
|
|
* @param txt pointer to the text
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_set_text(lv_obj_t * obj, const char * txt)
|
2016-12-15 10:31:30 +01:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_NULL(txt);
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2016-12-15 10:31:30 +01:00
|
|
|
|
2019-03-27 17:22:44 -04:00
|
|
|
/*Clear the existing selection*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_clear_selection(obj);
|
2019-03-27 17:22:44 -04:00
|
|
|
|
2018-10-05 17:22:49 +02:00
|
|
|
/*Add the character one-by-one if not all characters are accepted or there is character limit.*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(lv_textarea_get_accepted_chars(obj) || lv_textarea_get_max_length(obj)) {
|
|
|
|
lv_label_set_text(ta->label, "");
|
|
|
|
lv_textarea_set_cursor_pos(obj, LV_TEXTAREA_CURSOR_LAST);
|
|
|
|
if(ta->pwd_mode != 0) {
|
|
|
|
ta->pwd_tmp[0] = '\0'; /*Clear the password too*/
|
2020-01-11 16:02:23 +01:00
|
|
|
}
|
2018-10-05 17:22:49 +02:00
|
|
|
uint32_t i = 0;
|
|
|
|
while(txt[i] != '\0') {
|
2020-05-13 14:11:16 +02:00
|
|
|
uint32_t c = _lv_txt_encoded_next(txt, &i);
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_add_char(obj, _lv_txt_unicode_to_encoded(c));
|
2018-10-05 17:22:49 +02:00
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_label_set_text(ta->label, txt);
|
|
|
|
lv_textarea_set_cursor_pos(obj, LV_TEXTAREA_CURSOR_LAST);
|
2018-10-05 17:22:49 +02:00
|
|
|
}
|
2018-08-09 23:37:00 +02:00
|
|
|
|
2020-02-25 09:15:56 +01:00
|
|
|
/*If the textarea is empty, invalidate it to hide the placeholder*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->placeholder_txt) {
|
|
|
|
const char * txt_act = lv_label_get_text(ta->label);
|
|
|
|
if(txt_act[0] == '\0') lv_obj_invalidate(obj);
|
2020-02-25 09:15:56 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->pwd_mode != 0) {
|
|
|
|
ta->pwd_tmp = lv_mem_realloc(ta->pwd_tmp, strlen(txt) + 1);
|
|
|
|
LV_ASSERT_MEM(ta->pwd_tmp);
|
|
|
|
if(ta->pwd_tmp == NULL) return;
|
|
|
|
strcpy(ta->pwd_tmp, txt);
|
2017-08-29 15:08:18 +02:00
|
|
|
|
2019-06-06 06:05:40 +02:00
|
|
|
/*Auto hide characters*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->pwd_show_time == 0) {
|
|
|
|
pwd_char_hider(obj);
|
2020-09-11 14:25:33 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
lv_anim_path_t path;
|
|
|
|
lv_anim_path_init(&path);
|
|
|
|
lv_anim_path_set_cb(&path, lv_anim_path_step);
|
|
|
|
|
|
|
|
lv_anim_t a;
|
|
|
|
lv_anim_init(&a);
|
|
|
|
lv_anim_set_var(&a, ta);
|
|
|
|
lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)pwd_char_hider_anim);
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_anim_set_time(&a, ta->pwd_show_time);
|
2020-09-11 14:25:33 +02:00
|
|
|
lv_anim_set_values(&a, 0, 1);
|
|
|
|
lv_anim_set_path(&a, &path);
|
|
|
|
lv_anim_set_ready_cb(&a, pwd_char_hider_anim_ready);
|
|
|
|
lv_anim_start(&a);
|
2020-09-15 11:14:32 +02:00
|
|
|
}
|
2017-07-07 16:22:24 +02:00
|
|
|
}
|
2019-02-02 01:26:52 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
|
2019-02-02 01:26:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-25 09:15:56 +01:00
|
|
|
* Set the placeholder_txt text of a text area
|
2019-04-04 07:15:40 +02:00
|
|
|
* @param ta pointer to a text area
|
|
|
|
* @param txt pointer to the text
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_set_placeholder_text(lv_obj_t * obj, const char * txt)
|
2019-02-02 01:26:52 +01:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_NULL(txt);
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2019-02-02 01:26:52 +01:00
|
|
|
|
2020-02-25 09:15:56 +01:00
|
|
|
size_t txt_len = strlen(txt);
|
2019-02-02 01:26:52 +01:00
|
|
|
|
2020-02-25 09:15:56 +01:00
|
|
|
if(txt_len == 0) {
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->placeholder_txt) {
|
|
|
|
lv_mem_free(ta->placeholder_txt);
|
|
|
|
ta->placeholder_txt = NULL;
|
2019-02-03 01:33:21 +01:00
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-02-14 17:03:25 +01:00
|
|
|
|
2020-02-25 09:15:56 +01:00
|
|
|
/*Allocate memory for the placeholder_txt text*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->placeholder_txt == NULL) {
|
|
|
|
ta->placeholder_txt = lv_mem_alloc(txt_len + 1);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->placeholder_txt = lv_mem_realloc(ta->placeholder_txt, txt_len + 1);
|
2019-02-02 01:26:52 +01:00
|
|
|
|
2020-02-25 09:15:56 +01:00
|
|
|
}
|
2021-01-23 23:03:50 +01:00
|
|
|
LV_ASSERT_MEM(ta->placeholder_txt);
|
|
|
|
if(ta->placeholder_txt == NULL) {
|
2020-02-25 09:15:56 +01:00
|
|
|
LV_LOG_ERROR("lv_textarea_set_placeholder_text: couldn't allocate memory for placeholder");
|
|
|
|
return;
|
|
|
|
}
|
2021-01-23 23:03:50 +01:00
|
|
|
strcpy(ta->placeholder_txt, txt);
|
2020-02-25 09:15:56 +01:00
|
|
|
}
|
2019-11-02 21:34:41 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_obj_invalidate(obj);
|
2016-12-15 10:31:30 +01:00
|
|
|
}
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/**
|
|
|
|
* Set the cursor position
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param obj pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
* @param pos the new cursor position in character index
|
2016-12-15 10:31:30 +01:00
|
|
|
* < 0 : index from the end of the text
|
2020-02-14 12:36:44 +01:00
|
|
|
* LV_TEXTAREA_CURSOR_LAST: go after the last character
|
2016-10-04 09:45:39 +02:00
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_set_cursor_pos(lv_obj_t * obj, int32_t pos)
|
2016-10-01 00:25:10 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
if((uint32_t)ta->cursor.pos == (uint32_t)pos) return;
|
2018-05-16 23:09:30 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
uint32_t len = _lv_txt_get_encoded_length(lv_label_get_text(ta->label));
|
2016-10-04 09:45:39 +02:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
if(pos < 0) pos = len + pos;
|
2016-12-15 10:31:30 +01:00
|
|
|
|
2020-06-01 22:56:10 +02:00
|
|
|
if(pos > (int32_t)len || pos == LV_TEXTAREA_CURSOR_LAST) pos = len;
|
2016-10-04 09:45:39 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->cursor.pos = pos;
|
2016-10-04 09:45:39 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
/*Position the label to make the cursor show*/
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_point_t cur_pos;
|
2021-01-23 23:03:50 +01:00
|
|
|
const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_area_t label_cords;
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_area_t ta_cords;
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_label_get_letter_pos(ta->label, pos, &cur_pos);
|
|
|
|
lv_obj_get_coords(obj, &ta_cords);
|
|
|
|
lv_obj_get_coords(ta->label, &label_cords);
|
2016-10-04 09:45:39 +02:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
/*Check the top*/
|
2020-01-03 11:06:11 +01:00
|
|
|
lv_coord_t font_h = lv_font_get_line_height(font);
|
2021-01-23 23:03:50 +01:00
|
|
|
if(cur_pos.y < lv_obj_get_scroll_top(obj)) {
|
|
|
|
lv_obj_scroll_to_y(obj, cur_pos.y, LV_ANIM_ON);
|
2020-11-17 13:42:08 +01:00
|
|
|
}
|
|
|
|
/*Check the bottom*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_coord_t h = lv_obj_get_height_fit(obj);
|
|
|
|
if(cur_pos.y + font_h - lv_obj_get_scroll_top(obj) > h) {
|
|
|
|
lv_obj_scroll_to_y(obj, cur_pos.y - h + font_h, LV_ANIM_ON);
|
2019-05-25 16:27:36 +02:00
|
|
|
}
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->cursor.valid_x = cur_pos.x;
|
2018-05-16 23:09:30 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
start_cursor_blink(obj);
|
2018-12-30 15:02:16 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
refr_cursor_area(obj);
|
2017-09-15 10:22:12 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 16:04:15 +02:00
|
|
|
/**
|
2021-01-17 06:03:31 -08:00
|
|
|
* Enable/Disable the positioning of the cursor by clicking the text on the text area.
|
2019-06-14 16:04:15 +02:00
|
|
|
* @param ta pointer to a text area object
|
|
|
|
* @param en true: enable click positions; false: disable
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_set_cursor_click_pos(lv_obj_t * obj, bool en)
|
2019-06-14 16:04:15 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
ta->cursor.click_pos = en ? 1 : 0;
|
2019-06-14 16:04:15 +02:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:22:24 +02:00
|
|
|
/**
|
|
|
|
* Enable/Disable password mode
|
2017-11-05 00:48:57 +01:00
|
|
|
* @param ta pointer to a text area object
|
2018-12-22 19:45:55 +01:00
|
|
|
* @param en true: enable, false: disable
|
2017-07-07 16:22:24 +02:00
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_set_pwd_mode(lv_obj_t * obj, bool en)
|
2017-07-07 16:22:24 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
if(ta->pwd_mode == en) return;
|
2017-07-07 16:22:24 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->pwd_mode = en == false ? 0 : 1;
|
2017-07-07 16:22:24 +02:00
|
|
|
/*Pwd mode is now enabled*/
|
2020-11-28 19:57:14 -05:00
|
|
|
if(en != false) {
|
2021-01-23 23:03:50 +01:00
|
|
|
char * txt = lv_label_get_text(ta->label);
|
2019-12-02 12:20:01 +01:00
|
|
|
size_t len = strlen(txt);
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->pwd_tmp = lv_mem_alloc(len + 1);
|
|
|
|
LV_ASSERT_MEM(ta->pwd_tmp);
|
|
|
|
if(ta->pwd_tmp == NULL) return;
|
2018-07-25 13:33:53 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
strcpy(ta->pwd_tmp, txt);
|
2017-07-07 16:22:24 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
pwd_char_hider(obj);
|
2017-07-07 16:22:24 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_clear_selection(obj);
|
2017-07-07 16:22:24 +02:00
|
|
|
}
|
|
|
|
/*Pwd mode is now disabled*/
|
2020-11-28 19:57:14 -05:00
|
|
|
else {
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_clear_selection(obj);
|
|
|
|
lv_label_set_text(ta->label, ta->pwd_tmp);
|
|
|
|
lv_mem_free(ta->pwd_tmp);
|
|
|
|
ta->pwd_tmp = NULL;
|
2017-07-07 16:22:24 +02:00
|
|
|
}
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
refr_cursor_area(obj);
|
2017-07-07 16:22:24 +02:00
|
|
|
}
|
2017-08-16 11:28:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure the text area to one line or back to normal
|
|
|
|
* @param ta pointer to a Text area object
|
|
|
|
* @param en true: one line, false: normal
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_set_one_line(lv_obj_t * obj, bool en)
|
2017-08-16 11:28:04 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
if(ta->one_line == en) return;
|
2018-05-16 23:09:30 +02:00
|
|
|
|
2018-09-25 16:21:31 +02:00
|
|
|
if(en) {
|
2021-01-23 23:03:50 +01:00
|
|
|
const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
|
2020-01-03 11:06:11 +01:00
|
|
|
lv_coord_t font_h = lv_font_get_line_height(font);
|
2017-08-16 11:28:04 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->one_line = 1;
|
|
|
|
lv_obj_set_content_height(obj, font_h);
|
|
|
|
lv_label_set_long_mode(ta->label, LV_LABEL_LONG_EXPAND);
|
|
|
|
lv_obj_scroll_to(obj, 0, 0, LV_ANIM_OFF);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->one_line = 0;
|
|
|
|
lv_label_set_long_mode(ta->label, LV_LABEL_LONG_WRAP);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_obj_set_height(obj, LV_TEXTAREA_DEF_HEIGHT);
|
|
|
|
lv_obj_scroll_to(obj, 0, 0, LV_ANIM_OFF);
|
2018-09-25 16:21:31 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-04 08:46:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a list of characters. Only these characters will be accepted by the text area
|
|
|
|
* @param ta pointer to Text Area
|
|
|
|
* @param list list of characters. Only the pointer is saved. E.g. "+-.,0123456789"
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_set_accepted_chars(lv_obj_t * obj, const char * list)
|
2018-08-04 08:46:44 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2018-08-04 08:46:44 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->accepted_chars = list;
|
2018-08-04 08:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set max length of a Text Area.
|
|
|
|
* @param ta pointer to Text Area
|
2020-02-14 12:36:44 +01:00
|
|
|
* @param num the maximal number of characters can be added (`lv_textarea_set_text` ignores it)
|
2018-08-04 08:46:44 +02:00
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_set_max_length(lv_obj_t * obj, uint32_t num)
|
2018-08-04 08:46:44 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2018-08-04 08:46:44 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->max_length = num;
|
2018-08-04 08:46:44 +02:00
|
|
|
}
|
|
|
|
|
2019-03-20 05:46:21 +01:00
|
|
|
/**
|
|
|
|
* In `LV_EVENT_INSERT` the text which planned to be inserted can be replaced by an other text.
|
|
|
|
* It can be used to add automatic formatting to the text area.
|
|
|
|
* @param ta pointer to a text area.
|
|
|
|
* @param txt pointer to a new string to insert. If `""` no text will be added.
|
2019-04-04 07:15:40 +02:00
|
|
|
* The variable must be live after the `event_cb` exists. (Should be `global` or
|
|
|
|
* `static`)
|
2019-03-20 05:46:21 +01:00
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_set_insert_replace(lv_obj_t * obj, const char * txt)
|
2019-03-20 05:46:21 +01:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
LV_UNUSED(obj);
|
2019-03-20 05:46:21 +01:00
|
|
|
ta_insert_replace = txt;
|
|
|
|
}
|
|
|
|
|
2019-03-27 19:17:48 -04:00
|
|
|
/**
|
|
|
|
* Enable/disable selection mode.
|
|
|
|
* @param ta pointer to a text area object
|
|
|
|
* @param en true or false to enable/disable selection mode
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_set_text_sel(lv_obj_t * obj, bool en)
|
2019-04-04 07:15:40 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2019-04-18 07:11:43 +02:00
|
|
|
#if LV_LABEL_TEXT_SEL
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2019-04-18 07:11:43 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->text_sel_en = en;
|
2019-04-18 06:45:45 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(!en) lv_textarea_clear_selection(obj);
|
2019-04-18 07:11:43 +02:00
|
|
|
#else
|
2019-06-06 06:05:40 +02:00
|
|
|
(void)ta; /*Unused*/
|
|
|
|
(void)en; /*Unused*/
|
2019-04-18 07:11:43 +02:00
|
|
|
#endif
|
2019-03-27 19:17:48 -04:00
|
|
|
}
|
|
|
|
|
2019-05-25 16:27:36 +02:00
|
|
|
/**
|
|
|
|
* Set how long show the password before changing it to '*'
|
|
|
|
* @param ta pointer to Text area
|
|
|
|
* @param time show time in milliseconds. 0: hide immediately.
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_set_pwd_show_time(lv_obj_t * obj, uint16_t time)
|
2019-05-25 16:27:36 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
ta->pwd_show_time = time;
|
2019-05-25 16:27:36 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 15:29:00 +02:00
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/**
|
2018-08-04 08:46:44 +02:00
|
|
|
* Get the text of a text area. In password mode it gives the real text (not '*'s).
|
2017-06-13 14:49:41 +02:00
|
|
|
* @param ta pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
* @return pointer to the text
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
const char * lv_textarea_get_text(const lv_obj_t * obj)
|
2016-10-04 09:45:39 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2017-07-07 16:22:24 +02:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
const char * txt;
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->pwd_mode == 0) {
|
|
|
|
txt = lv_label_get_text(ta->label);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-01-23 23:03:50 +01:00
|
|
|
txt = ta->pwd_tmp;
|
2018-06-19 09:49:58 +02:00
|
|
|
}
|
2017-07-07 16:22:24 +02:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
return txt;
|
2016-10-04 09:45:39 +02:00
|
|
|
}
|
|
|
|
|
2019-02-02 01:26:52 +01:00
|
|
|
/**
|
2020-02-25 09:15:56 +01:00
|
|
|
* Get the placeholder_txt text of a text area
|
2019-04-04 07:15:40 +02:00
|
|
|
* @param ta pointer to a text area object
|
|
|
|
* @return pointer to the text
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
const char * lv_textarea_get_placeholder_text(lv_obj_t * obj)
|
2019-02-02 01:26:52 +01:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
if(ta->placeholder_txt) return ta->placeholder_txt;
|
2020-02-25 09:15:56 +01:00
|
|
|
else return "";
|
2019-02-02 01:26:52 +01:00
|
|
|
}
|
2017-06-13 14:49:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the label of a text area
|
|
|
|
* @param ta pointer to a text area object
|
|
|
|
* @return pointer to the label object
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_obj_t * lv_textarea_get_label(const lv_obj_t * obj)
|
2017-06-13 14:49:41 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
return ta->label;
|
2017-06-13 14:49:41 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/**
|
|
|
|
* Get the current cursor position in character index
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
* @return the cursor position
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
uint32_t lv_textarea_get_cursor_pos(const lv_obj_t * obj)
|
2017-09-15 10:22:12 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
return ta->cursor.pos;
|
2017-09-15 10:22:12 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 16:04:15 +02:00
|
|
|
/**
|
|
|
|
* Get whether the cursor click positioning is enabled or not.
|
|
|
|
* @param ta pointer to a text area object
|
|
|
|
* @return true: enable click positions; false: disable
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
bool lv_textarea_get_cursor_click_pos(lv_obj_t * obj)
|
2019-06-14 16:04:15 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
return ta->cursor.click_pos ? true : false;
|
2019-06-14 16:04:15 +02:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:22:24 +02:00
|
|
|
/**
|
2017-11-05 14:12:50 +01:00
|
|
|
* Get the password mode attribute
|
2017-07-07 16:22:24 +02:00
|
|
|
* @param ta pointer to a text area object
|
|
|
|
* @return true: password mode is enabled, false: disabled
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
bool lv_textarea_get_pwd_mode(const lv_obj_t * obj)
|
2017-07-07 16:22:24 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
return ta->pwd_mode == 0 ? false : true;
|
2017-11-05 14:12:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the one line configuration attribute
|
|
|
|
* @param ta pointer to a text area object
|
|
|
|
* @return true: one line configuration is enabled, false: disabled
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
bool lv_textarea_get_one_line(const lv_obj_t * obj)
|
2017-11-05 14:12:50 +01:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
return ta->one_line == 0 ? false : true;
|
2017-07-07 16:22:24 +02:00
|
|
|
}
|
|
|
|
|
2018-08-04 08:46:44 +02:00
|
|
|
/**
|
|
|
|
* Get a list of accepted characters.
|
|
|
|
* @param ta pointer to Text Area
|
|
|
|
* @return list of accented characters.
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
const char * lv_textarea_get_accepted_chars(lv_obj_t * obj)
|
2018-08-04 08:46:44 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2018-08-04 08:46:44 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
return ta->accepted_chars;
|
2018-08-04 08:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set max length of a Text Area.
|
|
|
|
* @param ta pointer to Text Area
|
|
|
|
* @return the maximal number of characters to be add
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
uint32_t lv_textarea_get_max_length(lv_obj_t * obj)
|
2018-08-04 08:46:44 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
return ta->max_length;
|
2018-08-04 08:46:44 +02:00
|
|
|
}
|
|
|
|
|
2019-03-27 18:36:57 -04:00
|
|
|
/**
|
|
|
|
* Find whether text is selected or not.
|
|
|
|
* @param ta Text area object
|
|
|
|
* @return whether text is selected or not
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
bool lv_textarea_text_is_selected(const lv_obj_t * obj)
|
2019-04-04 07:15:40 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2019-04-18 07:11:43 +02:00
|
|
|
#if LV_LABEL_TEXT_SEL
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2019-04-18 07:11:43 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if((lv_label_get_text_sel_start(ta->label) == LV_DRAW_LABEL_NO_TXT_SEL ||
|
|
|
|
lv_label_get_text_sel_end(ta->label) == LV_DRAW_LABEL_NO_TXT_SEL)) {
|
2019-04-18 07:11:43 +02:00
|
|
|
return true;
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-04-18 07:11:43 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
2019-06-06 06:05:40 +02:00
|
|
|
(void)ta; /*Unused*/
|
2019-04-18 07:11:43 +02:00
|
|
|
return false;
|
|
|
|
#endif
|
2019-03-27 18:36:57 -04:00
|
|
|
}
|
|
|
|
|
2019-03-27 19:17:48 -04:00
|
|
|
/**
|
|
|
|
* Find whether selection mode is enabled.
|
|
|
|
* @param ta pointer to a text area object
|
|
|
|
* @return true: selection mode is enabled, false: disabled
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
bool lv_textarea_get_text_sel_en(lv_obj_t * obj)
|
2019-04-04 07:15:40 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2019-04-18 07:11:43 +02:00
|
|
|
#if LV_LABEL_TEXT_SEL
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
return ta->text_sel_en;
|
2019-04-18 07:11:43 +02:00
|
|
|
#else
|
2019-06-06 06:05:40 +02:00
|
|
|
(void)ta; /*Unused*/
|
2019-04-18 07:11:43 +02:00
|
|
|
return false;
|
|
|
|
#endif
|
2019-03-27 19:17:48 -04:00
|
|
|
}
|
|
|
|
|
2019-05-25 16:27:36 +02:00
|
|
|
/**
|
|
|
|
* Set how long show the password before changing it to '*'
|
|
|
|
* @param ta pointer to Text area
|
|
|
|
* @return show time in milliseconds. 0: hide immediately.
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
uint16_t lv_textarea_get_pwd_show_time(lv_obj_t * obj)
|
2019-05-25 16:27:36 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-05-25 16:27:36 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
return ta->pwd_show_time;
|
2019-05-25 16:27:36 +02:00
|
|
|
}
|
|
|
|
|
2017-11-15 15:50:33 +01:00
|
|
|
/*=====================
|
|
|
|
* Other functions
|
|
|
|
*====================*/
|
|
|
|
|
2019-03-27 17:22:44 -04:00
|
|
|
/**
|
|
|
|
* Clear the selection on the text area.
|
|
|
|
* @param ta Text area object
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_clear_selection(lv_obj_t * obj)
|
2019-04-04 07:15:40 +02:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2019-04-18 07:11:43 +02:00
|
|
|
#if LV_LABEL_TEXT_SEL
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2019-04-04 07:15:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(lv_label_get_text_sel_start(ta->label) != LV_DRAW_LABEL_NO_TXT_SEL ||
|
|
|
|
lv_label_get_text_sel_end(ta->label) != LV_DRAW_LABEL_NO_TXT_SEL) {
|
|
|
|
lv_label_set_text_sel_start(ta->label, LV_DRAW_LABEL_NO_TXT_SEL);
|
|
|
|
lv_label_set_text_sel_end(ta->label, LV_DRAW_LABEL_NO_TXT_SEL);
|
2019-04-04 07:15:40 +02:00
|
|
|
}
|
2019-04-18 07:11:43 +02:00
|
|
|
#else
|
2019-06-06 06:05:40 +02:00
|
|
|
(void)ta; /*Unused*/
|
2019-04-18 07:11:43 +02:00
|
|
|
#endif
|
2019-03-27 17:22:44 -04:00
|
|
|
}
|
|
|
|
|
2017-11-15 15:50:33 +01:00
|
|
|
/**
|
|
|
|
* Move the cursor one character right
|
|
|
|
* @param ta pointer to a text area object
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_cursor_right(lv_obj_t * obj)
|
2017-11-15 15:50:33 +01:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
uint32_t cp = lv_textarea_get_cursor_pos(obj);
|
2017-11-15 15:50:33 +01:00
|
|
|
cp++;
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_set_cursor_pos(obj, cp);
|
2017-11-15 15:50:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the cursor one character left
|
|
|
|
* @param ta pointer to a text area object
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_cursor_left(lv_obj_t * obj)
|
2017-11-15 15:50:33 +01:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
uint32_t cp = lv_textarea_get_cursor_pos(obj);
|
2019-04-04 07:15:40 +02:00
|
|
|
if(cp > 0) {
|
2017-11-15 15:50:33 +01:00
|
|
|
cp--;
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_set_cursor_pos(obj, cp);
|
2017-11-15 15:50:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the cursor one line down
|
|
|
|
* @param ta pointer to a text area object
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_cursor_down(lv_obj_t * obj)
|
2017-11-15 15:50:33 +01:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_point_t pos;
|
2017-11-15 15:50:33 +01:00
|
|
|
|
|
|
|
/*Get the position of the current letter*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_label_get_letter_pos(ta->label, lv_textarea_get_cursor_pos(obj), &pos);
|
2017-11-15 15:50:33 +01:00
|
|
|
|
|
|
|
/*Increment the y with one line and keep the valid x*/
|
2020-01-03 11:06:11 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN);
|
|
|
|
const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
|
2020-01-03 11:06:11 +01:00
|
|
|
lv_coord_t font_h = lv_font_get_line_height(font);
|
|
|
|
pos.y += font_h + line_space + 1;
|
2021-01-23 23:03:50 +01:00
|
|
|
pos.x = ta->cursor.valid_x;
|
2017-11-15 15:50:33 +01:00
|
|
|
|
|
|
|
/*Do not go below the last line*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(pos.y < lv_obj_get_height(ta->label)) {
|
2017-11-15 15:50:33 +01:00
|
|
|
/*Get the letter index on the new cursor position and set it*/
|
2021-01-23 23:03:50 +01:00
|
|
|
uint32_t new_cur_pos = lv_label_get_letter_on(ta->label, &pos);
|
2017-11-15 15:50:33 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_coord_t cur_valid_x_tmp = ta->cursor.valid_x; /*Cursor position set overwrites the valid position */
|
|
|
|
lv_textarea_set_cursor_pos(obj, new_cur_pos);
|
|
|
|
ta->cursor.valid_x = cur_valid_x_tmp;
|
2017-11-15 15:50:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the cursor one line up
|
|
|
|
* @param ta pointer to a text area object
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
void lv_textarea_cursor_up(lv_obj_t * obj)
|
2017-11-15 15:50:33 +01:00
|
|
|
{
|
2021-02-04 14:46:11 +01:00
|
|
|
LV_ASSERT_OBJ(obj, MY_CLASS);
|
2019-09-26 12:54:40 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_point_t pos;
|
2017-11-15 15:50:33 +01:00
|
|
|
|
|
|
|
/*Get the position of the current letter*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_label_get_letter_pos(ta->label, lv_textarea_get_cursor_pos(obj), &pos);
|
2017-11-15 15:50:33 +01:00
|
|
|
|
|
|
|
/*Decrement the y with one line and keep the valid x*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN);
|
|
|
|
const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
|
2019-04-23 15:56:59 +02:00
|
|
|
lv_coord_t font_h = lv_font_get_line_height(font);
|
2020-01-03 11:06:11 +01:00
|
|
|
pos.y -= font_h + line_space - 1;
|
2021-01-23 23:03:50 +01:00
|
|
|
pos.x = ta->cursor.valid_x;
|
2017-11-15 15:50:33 +01:00
|
|
|
|
|
|
|
/*Get the letter index on the new cursor position and set it*/
|
2021-01-23 23:03:50 +01:00
|
|
|
uint32_t new_cur_pos = lv_label_get_letter_on(ta->label, &pos);
|
|
|
|
lv_coord_t cur_valid_x_tmp = ta->cursor.valid_x; /*Cursor position set overwrites the valid position */
|
|
|
|
lv_textarea_set_cursor_pos(obj, new_cur_pos);
|
|
|
|
ta->cursor.valid_x = cur_valid_x_tmp;
|
2017-11-15 15:50:33 +01:00
|
|
|
}
|
|
|
|
|
2016-09-30 15:29:00 +02:00
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
static void lv_textarea_constructor(lv_obj_t * obj, lv_obj_t * parent, const lv_obj_t * copy)
|
|
|
|
{
|
|
|
|
LV_LOG_TRACE("text area create started");
|
|
|
|
|
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
|
|
|
|
ta->pwd_mode = 0;
|
|
|
|
ta->pwd_tmp = NULL;
|
|
|
|
ta->pwd_show_time = LV_TEXTAREA_DEF_PWD_SHOW_TIME;
|
|
|
|
ta->accepted_chars = NULL;
|
|
|
|
ta->max_length = 0;
|
|
|
|
ta->cursor.show = 1;
|
|
|
|
ta->cursor.pos = 0;
|
|
|
|
ta->cursor.click_pos = 1;
|
|
|
|
ta->cursor.valid_x = 0;
|
|
|
|
ta->one_line = 0;
|
|
|
|
#if LV_LABEL_TEXT_SEL
|
|
|
|
ta->text_sel_en = 0;
|
|
|
|
#endif
|
|
|
|
ta->label = NULL;
|
|
|
|
ta->placeholder_txt = NULL;
|
|
|
|
|
|
|
|
/*Init the new text area object*/
|
|
|
|
if(copy == NULL) {
|
|
|
|
ta->label = lv_label_create(obj, NULL);
|
|
|
|
lv_label_set_long_mode(ta->label, LV_LABEL_LONG_WRAP);
|
|
|
|
lv_label_set_text(ta->label, "");
|
|
|
|
lv_obj_set_size(obj, LV_TEXTAREA_DEF_WIDTH, LV_TEXTAREA_DEF_HEIGHT);
|
|
|
|
}
|
|
|
|
/*Copy an existing object*/
|
|
|
|
else {
|
|
|
|
|
|
|
|
lv_textarea_t * copy_ta = (lv_textarea_t *) copy;
|
|
|
|
ta->label = lv_label_create(obj, copy_ta->label);
|
|
|
|
ta->pwd_mode = copy_ta->pwd_mode;
|
|
|
|
ta->accepted_chars = copy_ta->accepted_chars;
|
|
|
|
ta->max_length = copy_ta->max_length;
|
|
|
|
ta->cursor.pos = copy_ta->cursor.pos;
|
|
|
|
ta->cursor.valid_x = copy_ta->cursor.valid_x;
|
|
|
|
|
|
|
|
if(ta->pwd_mode != 0) pwd_char_hider(obj);
|
|
|
|
|
|
|
|
if(copy_ta->placeholder_txt) {
|
|
|
|
lv_textarea_set_placeholder_text(obj, copy_ta->placeholder_txt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(copy_ta->pwd_tmp) {
|
|
|
|
uint32_t len = lv_mem_get_size(copy_ta->pwd_tmp);
|
|
|
|
ta->pwd_tmp = lv_mem_alloc(len);
|
|
|
|
LV_ASSERT_MEM(ta->pwd_tmp);
|
|
|
|
if(ta->pwd_tmp == NULL) return;
|
|
|
|
|
|
|
|
lv_memcpy(ta->pwd_tmp, copy_ta->pwd_tmp, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(copy_ta->one_line) lv_textarea_set_one_line(obj, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
start_cursor_blink(obj);
|
|
|
|
|
|
|
|
LV_LOG_INFO("text area created");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lv_textarea_destructor(lv_obj_t * obj)
|
|
|
|
{
|
|
|
|
// else if(sign == LV_SIGNAL_CLEANUP) {
|
|
|
|
// if(ta->pwd_tmp != NULL) lv_mem_free(ta->pwd_tmp);
|
|
|
|
// if(ta->placeholder_txt != NULL) lv_mem_free(ta->placeholder_txt);
|
|
|
|
//
|
|
|
|
// ta->pwd_tmp = NULL;
|
|
|
|
// ta->placeholder_txt = NULL;
|
|
|
|
//
|
|
|
|
// _lv_obj_reset_style_list_no_refr(obj, LV_PART_MARKER);
|
|
|
|
// _lv_obj_reset_style_list_no_refr(obj, LV_TEXTAREA_PART_PLACEHOLDER);
|
|
|
|
//
|
|
|
|
// /* (The created label will be deleted automatically) */
|
|
|
|
}
|
|
|
|
|
|
|
|
static lv_draw_res_t lv_textarea_draw(lv_obj_t * obj, const lv_area_t * clip_area, lv_draw_mode_t mode)
|
2016-09-30 15:29:00 +02:00
|
|
|
{
|
2021-01-23 23:03:50 +01:00
|
|
|
if(mode == LV_DRAW_MODE_COVER_CHECK) {
|
2018-06-19 09:49:58 +02:00
|
|
|
/*Return false if the object is not covers the mask_p area*/
|
2021-02-04 14:46:11 +01:00
|
|
|
return lv_obj_draw_base(MY_CLASS, obj, clip_area, mode);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
2021-01-23 23:03:50 +01:00
|
|
|
else if(mode == LV_DRAW_MODE_MAIN_DRAW) {
|
2018-06-19 09:49:58 +02:00
|
|
|
/*Draw the object*/
|
2021-02-04 14:46:11 +01:00
|
|
|
lv_obj_draw_base(MY_CLASS, obj, clip_area, mode);
|
2021-01-23 23:03:50 +01:00
|
|
|
draw_placeholder(obj, clip_area);
|
2016-10-04 09:45:39 +02:00
|
|
|
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
2021-01-23 23:03:50 +01:00
|
|
|
else if(mode == LV_DRAW_MODE_POST_DRAW) {
|
2021-02-04 14:46:11 +01:00
|
|
|
lv_obj_draw_base(MY_CLASS, obj, clip_area, mode);
|
2021-01-23 23:03:50 +01:00
|
|
|
draw_cursor(obj, clip_area);
|
2016-09-30 15:29:00 +02:00
|
|
|
}
|
2021-01-21 15:18:20 +01:00
|
|
|
return LV_DRAW_RES_OK;
|
2016-10-04 09:45:39 +02:00
|
|
|
}
|
2016-09-30 15:29:00 +02:00
|
|
|
|
2017-11-05 15:19:36 +01:00
|
|
|
/**
|
|
|
|
* Signal function of the text area
|
|
|
|
* @param ta pointer to a text area object
|
|
|
|
* @param sign a signal type from lv_signal_t enum
|
|
|
|
* @param param pointer to a signal specific variable
|
2017-11-11 14:23:05 +01:00
|
|
|
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
|
2017-11-05 15:19:36 +01:00
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
static lv_res_t lv_textarea_signal(lv_obj_t * obj, lv_signal_t sign, void * param)
|
2017-11-05 15:19:36 +01:00
|
|
|
{
|
|
|
|
lv_res_t res;
|
|
|
|
/* Include the ancient signal function */
|
2021-02-04 14:46:11 +01:00
|
|
|
res = lv_obj_signal_base(MY_CLASS, obj, sign, param);
|
2017-11-07 17:00:55 +01:00
|
|
|
if(res != LV_RES_OK) return res;
|
2017-11-05 15:19:36 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2020-02-15 02:30:20 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(sign == LV_SIGNAL_STYLE_CHG) {
|
|
|
|
if(ta->label) {
|
|
|
|
if(ta->one_line) {
|
|
|
|
lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
|
|
|
|
lv_coord_t bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN);
|
|
|
|
const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
|
2020-01-03 11:06:11 +01:00
|
|
|
|
2017-11-07 17:00:55 +01:00
|
|
|
/*In one line mode refresh the Text Area height because 'vpad' can modify it*/
|
2020-01-03 11:06:11 +01:00
|
|
|
lv_coord_t font_h = lv_font_get_line_height(font);
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_obj_set_height(ta->label, font_h);
|
|
|
|
lv_obj_set_height(obj, font_h + top + bottom);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-11-07 17:00:55 +01:00
|
|
|
/*In not one line mode refresh the Label width because 'hpad' can modify it*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_obj_set_width(ta->label, lv_obj_get_width_fit(obj));
|
|
|
|
lv_obj_set_pos(ta->label, 0, 0); /*Be sure the Label is in the correct position*/
|
2019-02-02 01:26:52 +01:00
|
|
|
|
2017-11-07 17:00:55 +01:00
|
|
|
}
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_label_set_text(ta->label, NULL);
|
|
|
|
refr_cursor_area(obj);
|
|
|
|
start_cursor_blink(obj);
|
2017-11-07 17:00:55 +01:00
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
2021-02-04 14:46:11 +01:00
|
|
|
else if(sign == LV_SIGNAL_FOCUS) {
|
|
|
|
start_cursor_blink(obj);
|
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
else if(sign == LV_SIGNAL_COORD_CHG) {
|
2017-11-07 17:00:55 +01:00
|
|
|
/*Set the label width according to the text area width*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->label) {
|
|
|
|
if(lv_obj_get_width(obj) != lv_area_get_width(param) || lv_obj_get_height(obj) != lv_area_get_height(param)) {
|
|
|
|
lv_obj_set_width(ta->label, lv_obj_get_width_fit(obj));
|
|
|
|
lv_obj_set_pos(ta->label, 0, 0);
|
|
|
|
lv_label_set_text(ta->label, NULL); /*Refresh the label*/
|
2018-12-30 15:02:16 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
refr_cursor_area(obj);
|
2017-11-05 15:19:36 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_CONTROL) {
|
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)
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_cursor_right(obj);
|
2019-04-08 14:36:20 +02:00
|
|
|
else if(c == LV_KEY_LEFT)
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_cursor_left(obj);
|
2019-04-08 14:36:20 +02:00
|
|
|
else if(c == LV_KEY_UP)
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_cursor_up(obj);
|
2019-04-08 14:36:20 +02:00
|
|
|
else if(c == LV_KEY_DOWN)
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_cursor_down(obj);
|
2019-04-08 14:36:20 +02:00
|
|
|
else if(c == LV_KEY_BACKSPACE)
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_del_char(obj);
|
2019-04-08 14:36:20 +02:00
|
|
|
else if(c == LV_KEY_DEL)
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_del_char_forward(obj);
|
2019-04-08 14:36:20 +02:00
|
|
|
else if(c == LV_KEY_HOME)
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_set_cursor_pos(obj, 0);
|
2019-04-08 14:36:20 +02:00
|
|
|
else if(c == LV_KEY_END)
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_set_cursor_pos(obj, LV_TEXTAREA_CURSOR_LAST);
|
|
|
|
else if(c == LV_KEY_ENTER && lv_textarea_get_one_line(obj))
|
2021-01-26 15:09:36 +01:00
|
|
|
lv_event_send(obj, LV_EVENT_READY, NULL);
|
2017-11-19 19:28:45 +01:00
|
|
|
else {
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_add_char(obj, c);
|
2017-11-19 19:28:45 +01:00
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_PRESSED || sign == LV_SIGNAL_PRESSING || sign == LV_SIGNAL_PRESS_LOST ||
|
|
|
|
sign == LV_SIGNAL_RELEASED) {
|
2021-01-23 23:03:50 +01:00
|
|
|
update_cursor_position_on_click(obj, sign, (lv_indev_t *)param);
|
2019-02-02 05:11:18 +01:00
|
|
|
}
|
2017-11-15 15:50:33 +01:00
|
|
|
return res;
|
2017-11-05 15:19:36 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:31:30 +01:00
|
|
|
/**
|
2017-04-21 09:15:39 +02:00
|
|
|
* Called to blink the cursor
|
2016-12-15 10:31:30 +01:00
|
|
|
* @param ta pointer to a text area
|
2017-04-21 09:15:39 +02:00
|
|
|
* @param hide 1: hide the cursor, 0: show it
|
2016-12-15 10:31:30 +01:00
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
static void cursor_blink_anim_cb(lv_obj_t * obj, lv_anim_value_t show)
|
2016-12-15 10:31:30 +01:00
|
|
|
{
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
if(show != ta->cursor.show) {
|
|
|
|
ta->cursor.show = show == 0 ? 0 : 1;
|
|
|
|
lv_area_t area_tmp;
|
|
|
|
lv_area_copy(&area_tmp, &ta->cursor.area);
|
|
|
|
area_tmp.x1 += ta->label->coords.x1;
|
|
|
|
area_tmp.y1 += ta->label->coords.y1;
|
|
|
|
area_tmp.x2 += ta->label->coords.x1;
|
|
|
|
area_tmp.y2 += ta->label->coords.y1;
|
|
|
|
lv_obj_invalidate_area(obj, &area_tmp);
|
2018-06-19 09:49:58 +02:00
|
|
|
}
|
2016-12-15 10:31:30 +01:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:22:24 +02:00
|
|
|
/**
|
|
|
|
* Dummy function to animate char hiding in pwd mode.
|
2017-09-15 10:22:12 +02:00
|
|
|
* Does nothing, but a function is required in car hiding anim.
|
2017-07-07 16:22:24 +02:00
|
|
|
* (pwd_char_hider callback do the real job)
|
|
|
|
* @param ta unused
|
|
|
|
* @param x unused
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
static void pwd_char_hider_anim(lv_obj_t * obj, lv_anim_value_t x)
|
2017-07-07 16:22:24 +02:00
|
|
|
{
|
2021-01-23 23:03:50 +01:00
|
|
|
LV_UNUSED(obj);
|
|
|
|
LV_UNUSED(x);
|
2017-07-07 16:22:24 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 12:04:02 +02:00
|
|
|
/**
|
|
|
|
* Call when an animation is ready to convert all characters to '*'
|
|
|
|
* @param a pointer to the animation
|
|
|
|
*/
|
|
|
|
static void pwd_char_hider_anim_ready(lv_anim_t * a)
|
|
|
|
{
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_obj_t * obj = a->var;
|
|
|
|
pwd_char_hider(obj);
|
2019-05-08 12:04:02 +02:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:22:24 +02:00
|
|
|
/**
|
|
|
|
* Hide all characters (convert them to '*')
|
|
|
|
* @param ta: pointer to text area object
|
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
static void pwd_char_hider(lv_obj_t * obj)
|
2017-07-07 16:22:24 +02:00
|
|
|
{
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
if(ta->pwd_mode != 0) {
|
|
|
|
char * txt = lv_label_get_text(ta->label);
|
2020-06-01 17:51:47 +01:00
|
|
|
int32_t enc_len = _lv_txt_get_encoded_length(txt);
|
2020-02-17 16:04:16 +01:00
|
|
|
if(enc_len == 0) return;
|
|
|
|
|
|
|
|
/*If the textarea's font has "bullet" character use it else fallback to "*"*/
|
2021-01-23 23:03:50 +01:00
|
|
|
const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
|
2020-02-17 16:04:16 +01:00
|
|
|
lv_font_glyph_dsc_t g;
|
|
|
|
bool has_bullet;
|
|
|
|
has_bullet = lv_font_get_glyph_dsc(font, &g, LV_TEXTAREA_PWD_BULLET_UNICODE, 0);
|
|
|
|
const char * bullet;
|
2020-04-06 14:23:57 +02:00
|
|
|
if(has_bullet) bullet = LV_SYMBOL_BULLET;
|
2020-02-17 16:04:16 +01:00
|
|
|
else bullet = "*";
|
|
|
|
|
|
|
|
size_t bullet_len = strlen(bullet);
|
2021-01-23 20:46:42 +01:00
|
|
|
char * txt_tmp = lv_mem_buf_get(enc_len * bullet_len + 1);
|
2020-06-01 22:56:10 +02:00
|
|
|
int32_t i;
|
2020-02-17 16:04:16 +01:00
|
|
|
for(i = 0; i < enc_len; i++) {
|
2021-01-23 20:46:42 +01:00
|
|
|
lv_memcpy(&txt_tmp[i * bullet_len], bullet, bullet_len);
|
2018-04-14 22:06:13 +02:00
|
|
|
}
|
2017-09-22 13:58:01 +02:00
|
|
|
|
2020-02-17 16:04:16 +01:00
|
|
|
txt_tmp[i * bullet_len] = '\0';
|
2017-07-07 16:22:24 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_label_set_text(ta->label, txt_tmp);
|
2021-01-23 20:46:42 +01:00
|
|
|
lv_mem_buf_release(txt_tmp);
|
2021-01-23 23:03:50 +01:00
|
|
|
refr_cursor_area(obj);
|
2017-07-07 16:22:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 23:37:00 +02:00
|
|
|
/**
|
|
|
|
* Test an unicode character if it is accepted or not. Checks max length and accepted char list.
|
|
|
|
* @param ta pointer to a test area object
|
|
|
|
* @param c an unicode character
|
2020-06-15 17:12:48 +03:00
|
|
|
* @return true: accepted; false: rejected
|
2018-08-09 23:37:00 +02:00
|
|
|
*/
|
2021-01-23 23:03:50 +01:00
|
|
|
static bool char_is_accepted(lv_obj_t * obj, uint32_t c)
|
2018-08-04 08:46:44 +02:00
|
|
|
{
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2018-10-05 17:22:49 +02:00
|
|
|
|
|
|
|
/*If no restriction accept it*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->accepted_chars == NULL && ta->max_length == 0) return true;
|
2018-10-05 17:22:49 +02:00
|
|
|
|
|
|
|
/*Too many characters?*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->max_length > 0 && _lv_txt_get_encoded_length(lv_textarea_get_text(obj)) >= ta->max_length) {
|
2018-10-05 17:22:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Accepted character?*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->accepted_chars) {
|
2018-10-05 17:22:49 +02:00
|
|
|
uint32_t i = 0;
|
2020-02-25 15:32:35 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
while(ta->accepted_chars[i] != '\0') {
|
|
|
|
uint32_t a = _lv_txt_encoded_next(ta->accepted_chars, &i);
|
2018-10-05 17:22:49 +02:00
|
|
|
if(a == c) return true; /*Accepted*/
|
|
|
|
}
|
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
return false; /*The character wasn't in the list*/
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-04-04 07:15:40 +02:00
|
|
|
return true; /*If the accepted char list in not specified the accept the character*/
|
2018-10-05 17:22:49 +02:00
|
|
|
}
|
2018-08-04 08:46:44 +02:00
|
|
|
}
|
2018-12-30 15:02:16 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
static void start_cursor_blink(lv_obj_t * obj)
|
|
|
|
{
|
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
uint32_t blink_time = lv_obj_get_style_anim_time(obj, LV_PART_MARKER);
|
|
|
|
if(blink_time == 0) {
|
|
|
|
lv_anim_del(obj, (lv_anim_exec_xcb_t)cursor_blink_anim_cb);
|
|
|
|
ta->cursor.show = 1;
|
|
|
|
} else {
|
|
|
|
lv_anim_path_t path;
|
|
|
|
lv_anim_path_init(&path);
|
|
|
|
lv_anim_path_set_cb(&path, lv_anim_path_step);
|
|
|
|
|
|
|
|
lv_anim_t a;
|
|
|
|
lv_anim_init(&a);
|
|
|
|
lv_anim_set_var(&a, ta);
|
|
|
|
lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)cursor_blink_anim_cb);
|
|
|
|
lv_anim_set_time(&a, blink_time);
|
|
|
|
lv_anim_set_playback_time(&a, blink_time);
|
|
|
|
lv_anim_set_values(&a, 1, 0);
|
|
|
|
lv_anim_set_path(&a, &path);
|
|
|
|
lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
|
|
|
|
lv_anim_start(&a);
|
|
|
|
}
|
|
|
|
}
|
2018-12-30 15:02:16 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
static void refr_cursor_area(lv_obj_t * obj)
|
2018-12-30 15:02:16 +01:00
|
|
|
{
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
2019-06-14 16:04:15 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
|
|
|
|
lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN);
|
2018-12-30 15:02:16 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
uint32_t cur_pos = lv_textarea_get_cursor_pos(obj);
|
|
|
|
const char * txt = lv_label_get_text(ta->label);
|
2019-03-28 06:11:50 +01:00
|
|
|
|
2018-12-30 15:02:16 +01:00
|
|
|
uint32_t byte_pos;
|
2020-05-13 14:11:16 +02:00
|
|
|
byte_pos = _lv_txt_encoded_get_byte_id(txt, cur_pos);
|
2019-03-28 06:11:50 +01:00
|
|
|
|
2020-05-13 14:11:16 +02:00
|
|
|
uint32_t letter = _lv_txt_encoded_next(&txt[byte_pos], NULL);
|
2018-12-30 15:02:16 +01:00
|
|
|
|
2020-01-03 11:06:11 +01:00
|
|
|
lv_coord_t letter_h = lv_font_get_line_height(font);
|
2019-05-01 16:43:32 +02:00
|
|
|
|
2018-12-30 15:02:16 +01:00
|
|
|
/*Set letter_w (set not 0 on non printable but valid chars)*/
|
|
|
|
lv_coord_t letter_w;
|
|
|
|
if(letter == '\0' || letter == '\n' || letter == '\r') {
|
2020-01-03 11:06:11 +01:00
|
|
|
letter_w = lv_font_get_glyph_width(font, ' ', '\0');
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-05-01 16:43:32 +02:00
|
|
|
/*`letter_next` parameter is '\0' to ignore kerning*/
|
2020-01-03 11:06:11 +01:00
|
|
|
letter_w = lv_font_get_glyph_width(font, letter, '\0');
|
2018-12-30 15:02:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lv_point_t letter_pos;
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_label_get_letter_pos(ta->label, cur_pos, &letter_pos);
|
2018-12-30 15:02:16 +01:00
|
|
|
|
|
|
|
/*If the cursor is out of the text (most right) draw it to the next line*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(letter_pos.x + ta->label->coords.x1 + letter_w > ta->label->coords.x2 && ta->one_line == 0 &&
|
|
|
|
lv_obj_get_style_text_align(ta->label, LV_PART_MAIN) != LV_TEXT_ALIGN_RIGHT) {
|
2018-12-30 15:02:16 +01:00
|
|
|
letter_pos.x = 0;
|
2020-01-03 11:06:11 +01:00
|
|
|
letter_pos.y += letter_h + line_space;
|
2018-12-30 15:02:16 +01:00
|
|
|
|
|
|
|
if(letter != '\0') {
|
2020-05-13 14:11:16 +02:00
|
|
|
byte_pos += _lv_txt_encoded_size(&txt[byte_pos]);
|
|
|
|
letter = _lv_txt_encoded_next(&txt[byte_pos], NULL);
|
2018-12-30 15:02:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(letter == '\0' || letter == '\n' || letter == '\r') {
|
2020-01-03 11:06:11 +01:00
|
|
|
letter_w = lv_font_get_glyph_width(font, ' ', '\0');
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-01-03 11:06:11 +01:00
|
|
|
letter_w = lv_font_get_glyph_width(font, letter, '\0');
|
2018-12-30 15:02:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Save the byte position. It is required to draw `LV_CURSOR_BLOCK`*/
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->cursor.txt_byte_pos = byte_pos;
|
2018-12-30 15:02:16 +01:00
|
|
|
|
2020-01-03 11:06:11 +01:00
|
|
|
/*Calculate the cursor according to its type*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_MARKER);
|
|
|
|
lv_coord_t bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MARKER);
|
|
|
|
lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_MARKER);
|
|
|
|
lv_coord_t right = lv_obj_get_style_pad_right(obj, LV_PART_MARKER);
|
2020-01-03 11:06:11 +01:00
|
|
|
|
2018-12-30 15:02:16 +01:00
|
|
|
lv_area_t cur_area;
|
2020-01-10 06:52:57 +01:00
|
|
|
cur_area.x1 = letter_pos.x - left;
|
|
|
|
cur_area.y1 = letter_pos.y - top;
|
|
|
|
cur_area.x2 = letter_pos.x + right + letter_w;
|
|
|
|
cur_area.y2 = letter_pos.y + bottom + letter_h;
|
2018-12-30 15:02:16 +01:00
|
|
|
|
|
|
|
/*Save the new area*/
|
|
|
|
lv_area_t area_tmp;
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_area_copy(&area_tmp, &ta->cursor.area);
|
|
|
|
area_tmp.x1 += ta->label->coords.x1;
|
|
|
|
area_tmp.y1 += ta->label->coords.y1;
|
|
|
|
area_tmp.x2 += ta->label->coords.x1;
|
|
|
|
area_tmp.y2 += ta->label->coords.y1;
|
|
|
|
lv_obj_invalidate_area(obj, &area_tmp);
|
|
|
|
|
|
|
|
lv_area_copy(&ta->cursor.area, &cur_area);
|
|
|
|
|
|
|
|
lv_area_copy(&area_tmp, &ta->cursor.area);
|
|
|
|
area_tmp.x1 += ta->label->coords.x1;
|
|
|
|
area_tmp.y1 += ta->label->coords.y1;
|
|
|
|
area_tmp.x2 += ta->label->coords.x1;
|
|
|
|
area_tmp.y2 += ta->label->coords.y1;
|
|
|
|
lv_obj_invalidate_area(obj, &area_tmp);
|
2018-12-30 15:02:16 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
static void update_cursor_position_on_click(lv_obj_t * obj, lv_signal_t sign, lv_indev_t * click_source)
|
2019-02-02 22:59:28 +01:00
|
|
|
{
|
2019-03-12 14:29:37 +01:00
|
|
|
if(click_source == NULL) return;
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
if(ta->cursor.click_pos == 0) return;
|
2019-06-14 16:04:15 +02:00
|
|
|
|
2019-03-12 14:29:37 +01:00
|
|
|
if(lv_indev_get_type(click_source) == LV_INDEV_TYPE_KEYPAD ||
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_indev_get_type(click_source) == LV_INDEV_TYPE_ENCODER) {
|
2019-03-12 14:29:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-02 22:59:28 +01:00
|
|
|
lv_area_t label_coords;
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_obj_get_coords(ta->label, &label_coords);
|
2019-02-02 22:59:28 +01:00
|
|
|
|
2019-03-27 19:11:04 -04:00
|
|
|
lv_point_t point_act, vect_act;
|
2019-02-20 17:08:38 +01:00
|
|
|
lv_indev_get_point(click_source, &point_act);
|
2019-03-27 19:11:04 -04:00
|
|
|
lv_indev_get_vect(click_source, &vect_act);
|
|
|
|
|
2019-02-20 17:08:38 +01:00
|
|
|
if(point_act.x < 0 || point_act.y < 0) return; /*Ignore event from keypad*/
|
2019-10-11 12:01:58 +02:00
|
|
|
lv_point_t rel_pos;
|
|
|
|
rel_pos.x = point_act.x - label_coords.x1;
|
|
|
|
rel_pos.y = point_act.y - label_coords.y1;
|
2019-02-02 22:59:28 +01:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_coord_t label_width = lv_obj_get_width(ta->label);
|
2019-02-02 22:59:28 +01:00
|
|
|
|
2019-10-11 12:01:58 +02:00
|
|
|
uint16_t char_id_at_click;
|
2019-04-18 07:11:43 +02:00
|
|
|
|
|
|
|
#if LV_LABEL_TEXT_SEL
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_label_t * label_data = (lv_label_t *) ta->label;
|
2019-04-18 07:11:43 +02:00
|
|
|
bool click_outside_label;
|
2019-02-02 22:59:28 +01:00
|
|
|
/*Check if the click happened on the left side of the area outside the label*/
|
2019-10-11 12:01:58 +02:00
|
|
|
if(rel_pos.x < 0) {
|
|
|
|
char_id_at_click = 0;
|
2019-04-04 07:15:40 +02:00
|
|
|
click_outside_label = true;
|
2019-02-02 22:59:28 +01:00
|
|
|
}
|
|
|
|
/*Check if the click happened on the right side of the area outside the label*/
|
2019-10-11 12:01:58 +02:00
|
|
|
else if(rel_pos.x >= label_width) {
|
2020-02-14 12:36:44 +01:00
|
|
|
char_id_at_click = LV_TEXTAREA_CURSOR_LAST;
|
2019-04-04 07:15:40 +02:00
|
|
|
click_outside_label = true;
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-01-23 23:03:50 +01:00
|
|
|
char_id_at_click = lv_label_get_letter_on(ta->label, &rel_pos);
|
|
|
|
click_outside_label = !lv_label_is_char_under_pos(ta->label, &rel_pos);
|
2019-02-02 22:59:28 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->text_sel_en) {
|
|
|
|
if(!ta->text_sel_in_prog && !click_outside_label && sign == LV_SIGNAL_PRESSED) {
|
2019-04-18 06:45:45 +02:00
|
|
|
/*Input device just went down. Store the selection start position*/
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->sel_start = char_id_at_click;
|
|
|
|
ta->sel_end = LV_LABEL_TEXT_SEL_OFF;
|
|
|
|
ta->text_sel_in_prog = 1;
|
|
|
|
lv_obj_clear_flag(obj, LV_OBJ_FLAG_SCROLL_CHAIN);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
2021-01-23 23:03:50 +01:00
|
|
|
else if(ta->text_sel_in_prog && sign == LV_SIGNAL_PRESSING) {
|
2019-04-18 06:45:45 +02:00
|
|
|
/*Input device may be moving. Store the end position */
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->sel_end = char_id_at_click;
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
2021-01-23 23:03:50 +01:00
|
|
|
else if(ta->text_sel_in_prog && (sign == LV_SIGNAL_PRESS_LOST || sign == LV_SIGNAL_RELEASED)) {
|
2019-04-18 06:45:45 +02:00
|
|
|
/*Input device is released. Check if anything was selected.*/
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_CHAIN);
|
2019-04-18 06:45:45 +02:00
|
|
|
}
|
2019-03-27 16:39:37 -04:00
|
|
|
}
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->text_sel_in_prog || sign == LV_SIGNAL_PRESSED) lv_textarea_set_cursor_pos(obj, char_id_at_click);
|
2019-03-27 19:11:04 -04:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->text_sel_in_prog) {
|
2019-04-04 07:15:40 +02:00
|
|
|
/*If the selected area has changed then update the real values and*/
|
2019-04-14 09:47:45 -07:00
|
|
|
|
2019-10-11 12:01:58 +02:00
|
|
|
/*Invalidate the text area.*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->sel_start > ta->sel_end) {
|
|
|
|
if(label_data->sel_start != ta->sel_end || label_data->sel_end != ta->sel_start) {
|
|
|
|
label_data->sel_start = ta->sel_end;
|
|
|
|
label_data->sel_end = ta->sel_start;
|
|
|
|
lv_obj_invalidate(obj);
|
2019-04-04 07:15:40 +02:00
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
2021-01-23 23:03:50 +01:00
|
|
|
else if(ta->sel_start < ta->sel_end) {
|
|
|
|
if(label_data->sel_start != ta->sel_start || label_data->sel_end != ta->sel_end) {
|
|
|
|
label_data->sel_start = ta->sel_start;
|
|
|
|
label_data->sel_end = ta->sel_end;
|
|
|
|
lv_obj_invalidate(obj);
|
2019-04-04 07:15:40 +02:00
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-01-23 23:03:50 +01:00
|
|
|
if(label_data->sel_start != LV_DRAW_LABEL_NO_TXT_SEL || label_data->sel_end != LV_DRAW_LABEL_NO_TXT_SEL) {
|
|
|
|
label_data->sel_start = LV_DRAW_LABEL_NO_TXT_SEL;
|
|
|
|
label_data->sel_end = LV_DRAW_LABEL_NO_TXT_SEL;
|
|
|
|
lv_obj_invalidate(obj);
|
2019-04-04 07:15:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*Finish selection if necessary */
|
|
|
|
if(sign == LV_SIGNAL_PRESS_LOST || sign == LV_SIGNAL_RELEASED) {
|
2021-01-23 23:03:50 +01:00
|
|
|
ta->text_sel_in_prog = 0;
|
2019-04-04 07:15:40 +02:00
|
|
|
}
|
2019-03-27 16:39:37 -04:00
|
|
|
}
|
2019-04-18 07:11:43 +02:00
|
|
|
#else
|
|
|
|
/*Check if the click happened on the left side of the area outside the label*/
|
2019-10-11 12:01:58 +02:00
|
|
|
if(rel_pos.x < 0) {
|
|
|
|
char_id_at_click = 0;
|
2019-04-18 07:11:43 +02:00
|
|
|
}
|
|
|
|
/*Check if the click happened on the right side of the area outside the label*/
|
2019-10-11 12:01:58 +02:00
|
|
|
else if(rel_pos.x >= label_width) {
|
2020-02-14 12:36:44 +01:00
|
|
|
char_id_at_click = LV_TEXTAREA_CURSOR_LAST;
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-01-23 23:03:50 +01:00
|
|
|
char_id_at_click = lv_label_get_letter_on(ta->label, &rel_pos);
|
2019-04-18 07:11:43 +02:00
|
|
|
}
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(sign == LV_SIGNAL_PRESSED) lv_textarea_set_cursor_pos(obj, char_id_at_click);
|
2019-04-18 07:11:43 +02:00
|
|
|
#endif
|
2019-02-02 22:59:28 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 14:46:11 +01:00
|
|
|
static lv_res_t insert_handler(lv_obj_t * obj, char * txt)
|
2020-07-22 16:28:03 +02:00
|
|
|
{
|
|
|
|
ta_insert_replace = NULL;
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_event_send(obj, LV_EVENT_INSERT, txt);
|
2020-07-22 16:28:03 +02:00
|
|
|
if(ta_insert_replace) {
|
|
|
|
if(ta_insert_replace[0] == '\0') return LV_RES_INV; /*Drop this text*/
|
|
|
|
|
|
|
|
/*Add the replaced text directly it's different from the original*/
|
|
|
|
if(strcmp(ta_insert_replace, txt)) {
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_add_text(obj, ta_insert_replace);
|
2020-07-22 16:28:03 +02:00
|
|
|
return LV_RES_INV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return LV_RES_OK;
|
|
|
|
}
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
static void draw_placeholder(lv_obj_t * obj, const lv_area_t * clip_area)
|
2020-09-30 06:12:29 +02:00
|
|
|
{
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
const char * txt = lv_label_get_text(ta->label);
|
2020-09-30 06:12:29 +02:00
|
|
|
|
|
|
|
/*Draw the place holder*/
|
2021-01-23 23:03:50 +01:00
|
|
|
if(txt[0] == '\0' && ta->placeholder_txt && ta->placeholder_txt[0] != 0) {
|
2020-09-30 06:12:29 +02:00
|
|
|
lv_draw_label_dsc_t ph_dsc;
|
|
|
|
lv_draw_label_dsc_init(&ph_dsc);
|
2021-02-07 12:37:37 +01:00
|
|
|
lv_obj_init_draw_label_dsc(obj, LV_PART_TEXTAREA_PLACEHOLDER, &ph_dsc);
|
2020-09-30 06:12:29 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->one_line) ph_dsc.flag |= LV_TEXT_FLAG_EXPAND;
|
2020-09-30 06:12:29 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
|
|
|
|
lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
|
2020-11-28 16:04:06 +01:00
|
|
|
lv_area_t ph_coords;
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_area_copy(&ph_coords, &obj->coords);
|
2020-11-28 16:04:06 +01:00
|
|
|
ph_coords.x1 += left;
|
|
|
|
ph_coords.x2 += left;
|
|
|
|
ph_coords.y1 += top;
|
|
|
|
ph_coords.y2 += top;
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_draw_label(&ph_coords, clip_area, &ph_dsc, ta->placeholder_txt, NULL);
|
2020-09-30 06:12:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
static void draw_cursor(lv_obj_t * obj, const lv_area_t * clip_area)
|
2020-09-30 06:12:29 +02:00
|
|
|
{
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_textarea_t * ta = (lv_textarea_t *) obj;
|
|
|
|
const char * txt = lv_label_get_text(ta->label);
|
2020-09-30 06:12:29 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
if(ta->cursor.show == 0) return;
|
2020-09-30 06:12:29 +02:00
|
|
|
|
|
|
|
lv_draw_rect_dsc_t cur_dsc;
|
|
|
|
lv_draw_rect_dsc_init(&cur_dsc);
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_obj_init_draw_rect_dsc(obj, LV_PART_MARKER, &cur_dsc);
|
2020-09-30 06:12:29 +02:00
|
|
|
|
|
|
|
/*Draw he cursor according to the type*/
|
|
|
|
lv_area_t cur_area;
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_area_copy(&cur_area, &ta->cursor.area);
|
2020-09-30 06:12:29 +02:00
|
|
|
|
2021-01-23 23:03:50 +01:00
|
|
|
cur_area.x1 += ta->label->coords.x1;
|
|
|
|
cur_area.y1 += ta->label->coords.y1;
|
|
|
|
cur_area.x2 += ta->label->coords.x1;
|
|
|
|
cur_area.y2 += ta->label->coords.y1;
|
2020-09-30 06:12:29 +02:00
|
|
|
|
|
|
|
lv_draw_rect(&cur_area, clip_area, &cur_dsc);
|
|
|
|
|
|
|
|
char letter_buf[8] = {0};
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_memcpy(letter_buf, &txt[ta->cursor.txt_byte_pos], _lv_txt_encoded_size(&txt[ta->cursor.txt_byte_pos]));
|
2020-09-30 06:12:29 +02:00
|
|
|
|
|
|
|
if(cur_dsc.bg_opa == LV_OPA_COVER) {
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_MARKER);
|
|
|
|
lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_MARKER);
|
2020-09-30 06:12:29 +02:00
|
|
|
cur_area.x1 += left;
|
|
|
|
cur_area.y1 += top;
|
|
|
|
|
|
|
|
lv_draw_label_dsc_t cur_label_dsc;
|
|
|
|
lv_draw_label_dsc_init(&cur_label_dsc);
|
2021-01-23 23:03:50 +01:00
|
|
|
lv_obj_init_draw_label_dsc(obj, LV_PART_MARKER, &cur_label_dsc);
|
2020-09-30 06:12:29 +02:00
|
|
|
lv_draw_label(&cur_area, clip_area, &cur_label_dsc, letter_buf, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 15:29:00 +02:00
|
|
|
#endif
|