1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00
lvgl/src/widgets/lv_textarea.c

1327 lines
39 KiB
C
Raw Normal View History

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"
#if LV_USE_TEXTAREA != 0
#include <string.h>
#include "../misc/lv_assert.h"
#include "../core/lv_group.h"
#include "../core/lv_refr.h"
#include "../core/lv_indev.h"
#include "../draw/lv_draw.h"
#include "../misc/lv_anim.h"
#include "../misc/lv_txt.h"
#include "../misc/lv_math.h"
2016-09-30 15:29:00 +02:00
/*********************
* DEFINES
*********************/
2021-02-07 22:39:54 +01:00
#define MY_CLASS &lv_textarea_class
/*Test configuration*/
#ifndef LV_TEXTAREA_DEF_CURSOR_BLINK_TIME
2020-02-26 19:48:27 +01:00
#define LV_TEXTAREA_DEF_CURSOR_BLINK_TIME 400 /*ms*/
#endif
#ifndef LV_TEXTAREA_DEF_PWD_SHOW_TIME
2020-02-26 19:48:27 +01:00
#define LV_TEXTAREA_DEF_PWD_SHOW_TIME 1500 /*ms*/
#endif
#define LV_TEXTAREA_PWD_BULLET_UNICODE 0x2022
2016-09-30 15:29:00 +02:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void lv_textarea_constructor(lv_obj_t * obj);
2021-01-23 23:03:50 +01:00
static void lv_textarea_destructor(lv_obj_t * obj);
2021-03-18 15:13:35 +01:00
static void lv_textarea_event(lv_obj_t * obj, lv_event_t e);
2021-02-22 20:27:46 +01:00
static void cursor_blink_anim_cb(void * obj, int32_t show);
static void pwd_char_hider_anim(void * obj, int32_t x);
2021-01-26 15:09:36 +01:00
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);
2021-03-18 15:13:35 +01:00
static void update_cursor_position_on_click(lv_obj_t * obj, lv_event_t e);
2021-02-14 14:56:34 +01:00
static lv_res_t insert_handler(lv_obj_t * obj, const char * txt);
static void draw_placeholder(lv_obj_t * obj);
static void draw_cursor(lv_obj_t * obj);
2016-09-30 15:29:00 +02:00
/**********************
* STATIC VARIABLES
**********************/
2021-02-07 22:39:54 +01:00
const lv_obj_class_t lv_textarea_class = {
.constructor_cb = lv_textarea_constructor,
.destructor_cb = lv_textarea_destructor,
2021-03-18 15:13:35 +01:00
.event_cb = lv_textarea_event,
2021-04-08 12:48:48 +02:00
.group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
.width_def = LV_DPI_DEF * 2,
.height_def = LV_DPI_DEF,
2021-01-23 23:03:50 +01:00
.instance_size = sizeof(lv_textarea_t),
2021-02-07 22:39:54 +01:00
.base_class = &lv_obj_class
2021-01-23 23:03:50 +01:00
};
static const char * ta_insert_replace;
2016-09-30 15:29:00 +02:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
lv_obj_t * lv_textarea_create(lv_obj_t * parent)
2016-09-30 15:29:00 +02:00
{
2021-02-28 20:42:48 +01:00
LV_LOG_INFO("begin")
return lv_obj_create_from_class(&lv_textarea_class, parent);
2016-09-30 15:29:00 +02:00
}
/*======================
* Add/remove functions
*=====================*/
2016-09-30 15:29:00 +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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
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;
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;
#if LV_BIG_ENDIAN_SYSTEM
2020-07-07 09:36:59 +02:00
if(c != 0) while(*letter_buf == 0) ++letter_buf;
#endif
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;
}
uint32_t c_uni = _lv_txt_encoded_next((const char *)&c, NULL);
2021-01-23 23:03:50 +01:00
if(char_is_accepted(obj, c_uni) == false) {
2021-02-28 20:42:48 +01:00
LV_LOG_INFO("Character is not accepted by the text area (too long text or not in the accepted list)");
2018-10-05 17:22:49 +02:00
return;
}
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 '*'*/
/*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);
}
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*/
2021-02-08 09:53:03 +01:00
LV_ASSERT_MALLOC(ta->pwd_tmp);
2021-01-23 23:03:50 +01:00
if(ta->pwd_tmp == NULL) return;
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);
}
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);
2021-02-22 20:27:46 +01:00
lv_anim_set_exec_cb(&a, pwd_char_hider_anim);
2021-01-23 23:03:50 +01:00
lv_anim_set_time(&a, ta->pwd_show_time);
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-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
}
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
LV_ASSERT_NULL(txt);
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 '*'*/
/*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') {
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;
}
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;
/*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);
}
/*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);
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);
2021-02-08 09:53:03 +01:00
LV_ASSERT_MALLOC(ta->pwd_tmp);
2021-01-23 23:03:50 +01:00
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);
}
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);
2021-02-22 20:27:46 +01:00
lv_anim_set_exec_cb(&a, pwd_char_hider_anim);
2021-01-23 23:03:50 +01:00
lv_anim_set_time(&a, ta->pwd_show_time);
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-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
}
2021-01-23 23:03:50 +01:00
void lv_textarea_del_char(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
uint32_t cur_pos = ta->cursor.pos;
if(cur_pos == 0) return;
2019-04-08 14:36:20 +02:00
char del_buf[2] = {LV_KEY_DEL, '\0'};
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;
2021-01-23 23:03:50 +01:00
char * label_txt = lv_label_get_text(ta->label);
/*Delete a character*/
2021-01-23 23:03:50 +01:00
_lv_txt_cut(label_txt, ta->cursor.pos - 1, 1);
/*Refresh the label*/
2021-01-23 23:03:50 +01:00
lv_label_set_text(ta->label, label_txt);
lv_textarea_clear_selection(obj);
/*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);
}
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]));
2021-01-23 23:03:50 +01:00
ta->pwd_tmp = lv_mem_realloc(ta->pwd_tmp, strlen(ta->pwd_tmp) + 1);
2021-02-08 09:53:03 +01:00
LV_ASSERT_MALLOC(ta->pwd_tmp);
2021-01-23 23:03:50 +01:00
if(ta->pwd_tmp == NULL) return;
}
/*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);
}
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
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
}
/*=====================
* Setter functions
*====================*/
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
LV_ASSERT_NULL(txt);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2016-12-15 10:31:30 +01:00
/*Clear the existing selection*/
2021-01-23 23:03:50 +01:00
lv_textarea_clear_selection(obj);
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') {
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
}
/*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);
}
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);
2021-02-08 09:53:03 +01:00
LV_ASSERT_MALLOC(ta->pwd_tmp);
2021-01-23 23:03:50 +01:00
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);
}
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);
2021-02-22 20:27:46 +01:00
lv_anim_set_exec_cb(&a, pwd_char_hider_anim);
2021-01-23 23:03:50 +01:00
lv_anim_set_time(&a, ta->pwd_show_time);
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
}
}
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
}
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
LV_ASSERT_NULL(txt);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2019-02-02 01:26:52 +01:00
size_t txt_len = strlen(txt);
2019-02-02 01:26:52 +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
/*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
}
2021-02-08 09:53:03 +01:00
LV_ASSERT_MALLOC(ta->placeholder_txt);
2021-01-23 23:03:50 +01:00
if(ta->placeholder_txt == NULL) {
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);
}
2021-01-23 23:03:50 +01:00
lv_obj_invalidate(obj);
2016-12-15 10:31:30 +01: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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
if((uint32_t)ta->cursor.pos == (uint32_t)pos) return;
2021-01-23 23:03:50 +01:00
uint32_t len = _lv_txt_get_encoded_length(lv_label_get_text(ta->label));
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;
2021-01-23 23:03:50 +01:00
ta->cursor.pos = pos;
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);
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);
}
/*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);
}
2021-01-23 23:03:50 +01:00
ta->cursor.valid_x = cur_pos.x;
2021-01-23 23:03:50 +01:00
start_cursor_blink(obj);
2021-01-23 23:03:50 +01:00
refr_cursor_area(obj);
}
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
ta->cursor.click_pos = en ? 1 : 0;
2019-06-14 16:04:15 +02:00
}
2021-02-27 22:19:58 +01:00
void lv_textarea_set_password_mode(lv_obj_t * obj, bool en)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
if(ta->pwd_mode == en) return;
2021-01-23 23:03:50 +01:00
ta->pwd_mode = en == false ? 0 : 1;
/*Pwd mode is now enabled*/
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);
2021-02-08 09:53:03 +01:00
LV_ASSERT_MALLOC(ta->pwd_tmp);
2021-01-23 23:03:50 +01:00
if(ta->pwd_tmp == NULL) return;
2021-01-23 23:03:50 +01:00
strcpy(ta->pwd_tmp, txt);
2021-01-23 23:03:50 +01:00
pwd_char_hider(obj);
2021-01-23 23:03:50 +01:00
lv_textarea_clear_selection(obj);
}
/*Pwd mode is now disabled*/
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;
}
2021-01-23 23:03:50 +01:00
refr_cursor_area(obj);
}
2017-08-16 11:28:04 +02:00
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
if(ta->one_line == en) return;
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);
lv_obj_set_height(obj, LV_DPI_DEF);
2021-01-23 23:03:50 +01:00
lv_obj_scroll_to(obj, 0, 0, LV_ANIM_OFF);
2018-09-25 16:21:31 +02:00
}
}
2021-01-23 23:03:50 +01:00
void lv_textarea_set_accepted_chars(lv_obj_t * obj, const char * list)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
ta->accepted_chars = list;
}
2021-01-23 23:03:50 +01:00
void lv_textarea_set_max_length(lv_obj_t * obj, uint32_t num)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
ta->max_length = num;
}
2021-01-23 23:03:50 +01:00
void lv_textarea_set_insert_replace(lv_obj_t * obj, const char * txt)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
2021-01-23 23:03:50 +01:00
LV_UNUSED(obj);
ta_insert_replace = txt;
}
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
2019-04-18 07:11:43 +02:00
#if LV_LABEL_TEXT_SEL
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
(void)obj; /*Unused*/
2019-06-06 06:05:40 +02:00
(void)en; /*Unused*/
2019-04-18 07:11:43 +02:00
#endif
}
2021-02-27 22:19:58 +01:00
void lv_textarea_set_password_show_time(lv_obj_t * obj, uint16_t time)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
ta->pwd_show_time = time;
}
void lv_textarea_set_align(lv_obj_t * obj, lv_text_align_t align)
{
lv_obj_set_style_text_align(obj, align, 0);
switch(align) {
default:
case LV_TEXT_ALIGN_LEFT:
lv_obj_align(lv_textarea_get_label(obj), LV_ALIGN_TOP_LEFT, 0, 0);
break;
case LV_TEXT_ALIGN_RIGHT:
lv_obj_align(lv_textarea_get_label(obj), LV_ALIGN_TOP_RIGHT, 0, 0);
break;
case LV_TEXT_ALIGN_CENTER:
lv_obj_align(lv_textarea_get_label(obj), LV_ALIGN_TOP_MID, 0, 0);
break;
}
}
2016-09-30 15:29:00 +02:00
/*=====================
* Getter functions
*====================*/
2021-01-23 23:03:50 +01:00
const char * lv_textarea_get_text(const lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
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
}
2018-06-19 09:49:58 +02:00
return txt;
}
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
if(ta->placeholder_txt) return ta->placeholder_txt;
else return "";
2019-02-02 01:26:52 +01:00
}
2017-06-13 14:49:41 +02:00
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
return ta->label;
2017-06-13 14:49:41 +02:00
}
2021-01-23 23:03:50 +01:00
uint32_t lv_textarea_get_cursor_pos(const lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
return ta->cursor.pos;
}
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
return ta->cursor.click_pos ? true : false;
2019-06-14 16:04:15 +02:00
}
2021-02-27 22:19:58 +01:00
bool lv_textarea_get_password_mode(const lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
return ta->pwd_mode == 0 ? false : true;
2017-11-05 14:12:50 +01:00
}
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
return ta->one_line == 0 ? false : true;
}
2021-01-23 23:03:50 +01:00
const char * lv_textarea_get_accepted_chars(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
return ta->accepted_chars;
}
2021-01-23 23:03:50 +01:00
uint32_t lv_textarea_get_max_length(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
return ta->max_length;
}
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
2019-04-18 07:11:43 +02:00
#if LV_LABEL_TEXT_SEL
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
(void)obj; /*Unused*/
2019-04-18 07:11:43 +02:00
return false;
#endif
2019-03-27 18:36:57 -04:00
}
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
2019-04-18 07:11:43 +02:00
#if LV_LABEL_TEXT_SEL
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
return ta->text_sel_en;
2019-04-18 07:11:43 +02:00
#else
(void)obj; /*Unused*/
2019-04-18 07:11:43 +02:00
return false;
#endif
}
2021-02-27 22:19:58 +01:00
uint16_t lv_textarea_get_password_show_time(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
return ta->pwd_show_time;
}
/*=====================
* Other functions
*====================*/
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
{
LV_ASSERT_OBJ(obj, MY_CLASS);
2019-04-18 07:11:43 +02:00
#if LV_LABEL_TEXT_SEL
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
(void)obj; /*Unused*/
2019-04-18 07:11:43 +02:00
#endif
}
2021-01-23 23:03:50 +01:00
void lv_textarea_cursor_right(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
2021-01-23 23:03:50 +01:00
uint32_t cp = lv_textarea_get_cursor_pos(obj);
cp++;
2021-01-23 23:03:50 +01:00
lv_textarea_set_cursor_pos(obj, cp);
}
2021-01-23 23:03:50 +01:00
void lv_textarea_cursor_left(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
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) {
cp--;
2021-01-23 23:03:50 +01:00
lv_textarea_set_cursor_pos(obj, cp);
}
}
2021-01-23 23:03:50 +01:00
void lv_textarea_cursor_down(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2017-11-23 21:28:36 +01:00
lv_point_t pos;
/*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);
/*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;
/*Do not go below the last line*/
2021-01-23 23:03:50 +01:00
if(pos.y < lv_obj_get_height(ta->label)) {
/*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*/
2021-01-23 23:03:50 +01:00
lv_textarea_set_cursor_pos(obj, new_cur_pos);
ta->cursor.valid_x = cur_valid_x_tmp;
}
}
2021-01-23 23:03:50 +01:00
void lv_textarea_cursor_up(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_textarea_t * ta = (lv_textarea_t *)obj;
2017-11-23 21:28:36 +01:00
lv_point_t pos;
/*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);
/*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;
/*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*/
2021-01-23 23:03:50 +01:00
lv_textarea_set_cursor_pos(obj, new_cur_pos);
ta->cursor.valid_x = cur_valid_x_tmp;
}
2016-09-30 15:29:00 +02:00
/**********************
* STATIC FUNCTIONS
**********************/
static void lv_textarea_constructor(lv_obj_t * obj)
2021-01-23 23:03:50 +01:00
{
2021-03-01 11:57:44 +01:00
LV_TRACE_OBJ_CREATE("begin");
2021-01-23 23:03:50 +01:00
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
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;
ta->label = lv_label_create(obj);
lv_label_set_long_mode(ta->label, LV_LABEL_LONG_WRAP);
lv_label_set_text(ta->label, "");
lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
2021-01-23 23:03:50 +01:00
start_cursor_blink(obj);
2021-03-01 11:57:44 +01:00
LV_TRACE_OBJ_CREATE("finished");
2021-01-23 23:03:50 +01:00
}
static void lv_textarea_destructor(lv_obj_t * obj)
{
2021-02-10 22:59:53 +01:00
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-02-10 22:59:53 +01:00
if(ta->pwd_tmp != NULL) {
lv_mem_free(ta->pwd_tmp);
ta->pwd_tmp = NULL;
}
if(ta->placeholder_txt != NULL) {
lv_mem_free(ta->placeholder_txt);
ta->placeholder_txt = NULL;
}
2021-01-23 23:03:50 +01:00
}
2021-03-18 15:13:35 +01:00
static void lv_textarea_event(lv_obj_t * obj, lv_event_t e)
2017-11-05 15:19:36 +01:00
{
lv_res_t res;
/*Call the ancestor's event handler*/
2021-03-18 15:13:35 +01:00
res = lv_obj_event_base(MY_CLASS, obj, e);
if(res != LV_RES_OK) return;
2017-11-05 15:19:36 +01:00
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-03-18 17:18:11 +01:00
if(e == LV_EVENT_STYLE_CHANGED) {
2021-01-23 23:03:50 +01:00
if(ta->label) {
lv_label_set_text(ta->label, NULL);
refr_cursor_area(obj);
start_cursor_blink(obj);
}
2020-02-26 19:48:27 +01:00
}
2021-03-18 15:13:35 +01:00
else if(e == LV_EVENT_FOCUSED) {
start_cursor_blink(obj);
}
else if(e == LV_EVENT_SIZE_CHANGED) {
/*Set the label width according to the text area width*/
2021-01-23 23:03:50 +01:00
if(ta->label) {
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*/
refr_cursor_area(obj);
2017-11-05 15:19:36 +01:00
}
2020-02-26 19:48:27 +01:00
}
2021-03-18 15:13:35 +01:00
else if(e == LV_EVENT_KEY) {
uint32_t c = *((uint32_t *)lv_event_get_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
}
2021-03-18 15:13:35 +01:00
else if(e == LV_EVENT_PRESSED || e == LV_EVENT_PRESSING || e == LV_EVENT_PRESS_LOST ||
e == LV_EVENT_RELEASED) {
update_cursor_position_on_click(obj, e);
2019-02-02 05:11:18 +01:00
}
else if(e == LV_EVENT_DRAW_MAIN) {
draw_placeholder(obj);
}
else if(e == LV_EVENT_DRAW_POST) {
draw_cursor(obj);
}
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-02-22 20:27:46 +01:00
static void cursor_blink_anim_cb(void * obj, int32_t show)
2016-12-15 10:31:30 +01:00
{
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
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
}
/**
* Dummy function to animate char hiding in pwd mode.
* Does nothing, but a function is required in car hiding anim.
* (pwd_char_hider callback do the real job)
* @param ta unused
* @param x unused
*/
2021-02-22 20:27:46 +01:00
static void pwd_char_hider_anim(void * obj, int32_t x)
{
2021-01-23 23:03:50 +01:00
LV_UNUSED(obj);
LV_UNUSED(x);
}
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
}
/**
* 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)
{
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
if(ta->pwd_mode != 0) {
char * txt = lv_label_get_text(ta->label);
int32_t enc_len = _lv_txt_get_encoded_length(txt);
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);
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;
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;
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);
}
2017-09-22 13:58:01 +02:00
txt_tmp[i * bullet_len] = '\0';
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);
}
}
/**
* 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
* @return true: accepted; false: rejected
*/
2021-01-23 23:03:50 +01:00
static bool char_is_accepted(lv_obj_t * obj, uint32_t c)
{
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
}
}
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_CURSOR);
2021-01-23 23:03:50 +01:00
if(blink_time == 0) {
2021-02-22 20:27:46 +01:00
lv_anim_del(obj, cursor_blink_anim_cb);
2021-01-23 23:03:50 +01:00
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);
2021-02-22 20:27:46 +01:00
lv_anim_set_exec_cb(&a, cursor_blink_anim_cb);
2021-01-23 23:03:50 +01:00
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);
}
}
2021-01-23 23:03:50 +01:00
static void refr_cursor_area(lv_obj_t * obj)
{
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);
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);
uint32_t byte_pos;
byte_pos = _lv_txt_encoded_get_byte_id(txt, cur_pos);
uint32_t letter = _lv_txt_encoded_next(&txt[byte_pos], NULL);
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
/*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');
}
lv_point_t letter_pos;
2021-01-23 23:03:50 +01:00
lv_label_get_letter_pos(ta->label, cur_pos, &letter_pos);
/*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) {
letter_pos.x = 0;
2020-01-03 11:06:11 +01:00
letter_pos.y += letter_h + line_space;
if(letter != '\0') {
byte_pos += _lv_txt_encoded_size(&txt[byte_pos]);
letter = _lv_txt_encoded_next(&txt[byte_pos], NULL);
}
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');
}
}
/*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;
2020-01-03 11:06:11 +01:00
/*Calculate the cursor according to its type*/
lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_CURSOR);
lv_coord_t bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_CURSOR);
lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_CURSOR);
lv_coord_t right = lv_obj_get_style_pad_right(obj, LV_PART_CURSOR);
2020-01-03 11:06:11 +01:00
lv_area_t cur_area;
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;
/*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);
}
2021-03-18 15:13:35 +01:00
static void update_cursor_position_on_click(lv_obj_t * obj, lv_event_t e)
2019-02-02 22:59:28 +01:00
{
2021-03-18 15:13:35 +01:00
lv_indev_t * click_source = lv_indev_get_act();
2019-03-12 14:29:37 +01:00
if(click_source == NULL) return;
lv_textarea_t * ta = (lv_textarea_t *)obj;
2021-01-23 23:03:50 +01:00
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
lv_point_t point_act, vect_act;
lv_indev_get_point(click_source, &point_act);
lv_indev_get_vect(click_source, &vect_act);
if(point_act.x < 0 || point_act.y < 0) return; /*Ignore event from keypad*/
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
uint16_t char_id_at_click;
2019-04-18 07:11:43 +02:00
#if LV_LABEL_TEXT_SEL
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*/
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*/
else if(rel_pos.x >= label_width) {
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) {
2021-03-18 15:13:35 +01:00
if(!ta->text_sel_in_prog && !click_outside_label && e == LV_EVENT_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-03-18 15:13:35 +01:00
else if(ta->text_sel_in_prog && e == LV_EVENT_PRESSING) {
/*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-03-18 15:13:35 +01:00
else if(ta->text_sel_in_prog && (e == LV_EVENT_PRESS_LOST || e == LV_EVENT_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
}
}
2021-03-18 15:13:35 +01:00
if(ta->text_sel_in_prog || e == LV_EVENT_PRESSED) lv_textarea_set_cursor_pos(obj, char_id_at_click);
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*/
/*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*/
2021-03-18 15:13:35 +01:00
if(e == LV_EVENT_PRESS_LOST || e == LV_EVENT_RELEASED) {
2021-01-23 23:03:50 +01:00
ta->text_sel_in_prog = 0;
2019-04-04 07:15:40 +02: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*/
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*/
else if(rel_pos.x >= label_width) {
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-03-18 15:13:35 +01:00
if(e == LV_EVENT_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-14 14:56:34 +01:00
static lv_res_t insert_handler(lv_obj_t * obj, const char * txt)
2020-07-22 16:28:03 +02:00
{
ta_insert_replace = NULL;
2021-02-14 14:56:34 +01:00
lv_event_send(obj, LV_EVENT_INSERT, (char*)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;
}
static void draw_placeholder(lv_obj_t * obj)
2020-09-30 06:12:29 +02:00
{
lv_textarea_t * ta = (lv_textarea_t *)obj;
const lv_area_t * clip_area = lv_event_get_param();
2021-01-23 23:03:50 +01:00
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
}
}
static void draw_cursor(lv_obj_t * obj)
2020-09-30 06:12:29 +02:00
{
lv_textarea_t * ta = (lv_textarea_t *)obj;
const lv_area_t * clip_area = lv_event_get_param();
2021-01-23 23:03:50 +01:00
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);
lv_obj_init_draw_rect_dsc(obj, LV_PART_CURSOR, &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) {
lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_CURSOR);
lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_CURSOR);
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);
lv_obj_init_draw_label_dsc(obj, LV_PART_CURSOR, &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