1 /**
2  * @file lv_ta.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_textarea.h"
10 #if LV_USE_TEXTAREA != 0
11 
12 #include <string.h>
13 #include "../misc/lv_assert.h"
14 #include "../core/lv_group.h"
15 #include "../core/lv_refr.h"
16 #include "../core/lv_indev.h"
17 #include "../draw/lv_draw.h"
18 #include "../misc/lv_anim.h"
19 #include "../misc/lv_txt.h"
20 #include "../misc/lv_math.h"
21 
22 /*********************
23  *      DEFINES
24  *********************/
25 #define MY_CLASS &lv_textarea_class
26 
27 /*Test configuration*/
28 #ifndef LV_TEXTAREA_DEF_CURSOR_BLINK_TIME
29     #define LV_TEXTAREA_DEF_CURSOR_BLINK_TIME 400 /*ms*/
30 #endif
31 
32 #ifndef LV_TEXTAREA_DEF_PWD_SHOW_TIME
33     #define LV_TEXTAREA_DEF_PWD_SHOW_TIME 1500 /*ms*/
34 #endif
35 
36 #define LV_TEXTAREA_PWD_BULLET_UNICODE      0x2022
37 #define IGNORE_KERNING                      '\0'
38 
39 /**********************
40  *      TYPEDEFS
41  **********************/
42 
43 /**********************
44  *  STATIC PROTOTYPES
45  **********************/
46 static void lv_textarea_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
47 static void lv_textarea_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
48 static void lv_textarea_event(const lv_obj_class_t * class_p, lv_event_t * e);
49 static void label_event_cb(lv_event_t * e);
50 static void cursor_blink_anim_cb(void * obj, int32_t show);
51 static void pwd_char_hider_anim(void * obj, int32_t x);
52 static void pwd_char_hider_anim_ready(lv_anim_t * a);
53 static void pwd_char_hider(lv_obj_t * obj);
54 static bool char_is_accepted(lv_obj_t * obj, uint32_t c);
55 static void start_cursor_blink(lv_obj_t * obj);
56 static void refr_cursor_area(lv_obj_t * obj);
57 static void update_cursor_position_on_click(lv_event_t * e);
58 static lv_res_t insert_handler(lv_obj_t * obj, const char * txt);
59 static void draw_placeholder(lv_event_t * e);
60 static void draw_cursor(lv_event_t * e);
61 static void auto_hide_characters(lv_obj_t * obj);
62 static inline bool is_valid_but_non_printable_char(const uint32_t letter);
63 
64 /**********************
65  *  STATIC VARIABLES
66  **********************/
67 const lv_obj_class_t lv_textarea_class = {
68     .constructor_cb = lv_textarea_constructor,
69     .destructor_cb = lv_textarea_destructor,
70     .event_cb = lv_textarea_event,
71     .group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
72     .width_def = LV_DPI_DEF * 2,
73     .height_def = LV_DPI_DEF,
74     .instance_size = sizeof(lv_textarea_t),
75     .base_class = &lv_obj_class
76 };
77 
78 static const char * ta_insert_replace;
79 
80 /**********************
81  *      MACROS
82  **********************/
83 
84 /**********************
85  *   GLOBAL FUNCTIONS
86  **********************/
87 
lv_textarea_create(lv_obj_t * parent)88 lv_obj_t * lv_textarea_create(lv_obj_t * parent)
89 {
90     LV_LOG_INFO("begin");
91     lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS, parent);
92     lv_obj_class_init_obj(obj);
93     return obj;
94 }
95 
96 /*======================
97  * Add/remove functions
98  *=====================*/
99 
lv_textarea_add_char(lv_obj_t * obj,uint32_t c)100 void lv_textarea_add_char(lv_obj_t * obj, uint32_t c)
101 {
102     LV_ASSERT_OBJ(obj, MY_CLASS);
103 
104     lv_textarea_t * ta = (lv_textarea_t *)obj;
105 
106     if(ta->one_line && (c == '\n' || c == '\r')) {
107         LV_LOG_INFO("Text area: line break ignored in one-line mode");
108         return;
109     }
110 
111     uint32_t u32_buf[2];
112     u32_buf[0] = c;
113     u32_buf[1] = 0;
114 
115     const char * letter_buf = (char *)&u32_buf;
116 
117 #if LV_BIG_ENDIAN_SYSTEM
118     if(c != 0) while(*letter_buf == 0) ++letter_buf;
119 #endif
120 
121     lv_res_t res = insert_handler(obj, letter_buf);
122     if(res != LV_RES_OK) return;
123 
124     uint32_t c_uni = _lv_txt_encoded_next((const char *)&c, NULL);
125 
126     if(char_is_accepted(obj, c_uni) == false) {
127         LV_LOG_INFO("Character is not accepted by the text area (too long text or not in the accepted list)");
128         return;
129     }
130 
131     if(ta->pwd_mode) pwd_char_hider(obj); /*Make sure all the current text contains only '*'*/
132 
133     /*If the textarea is empty, invalidate it to hide the placeholder*/
134     if(ta->placeholder_txt) {
135         const char * txt = lv_label_get_text(ta->label);
136         if(txt[0] == '\0') lv_obj_invalidate(obj);
137     }
138 
139     lv_label_ins_text(ta->label, ta->cursor.pos, letter_buf); /*Insert the character*/
140     lv_textarea_clear_selection(obj); /*Clear selection*/
141 
142     if(ta->pwd_mode) {
143         /*+2: the new char + \0*/
144         size_t realloc_size = strlen(ta->pwd_tmp) + strlen(letter_buf) + 1;
145         ta->pwd_tmp = lv_mem_realloc(ta->pwd_tmp, realloc_size);
146         LV_ASSERT_MALLOC(ta->pwd_tmp);
147         if(ta->pwd_tmp == NULL) return;
148 
149         _lv_txt_ins(ta->pwd_tmp, ta->cursor.pos, (const char *)letter_buf);
150 
151         /*Auto hide characters*/
152         auto_hide_characters(obj);
153     }
154 
155     /*Move the cursor after the new character*/
156     lv_textarea_set_cursor_pos(obj, lv_textarea_get_cursor_pos(obj) + 1);
157 
158     lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
159 }
160 
lv_textarea_add_text(lv_obj_t * obj,const char * txt)161 void lv_textarea_add_text(lv_obj_t * obj, const char * txt)
162 {
163     LV_ASSERT_OBJ(obj, MY_CLASS);
164     LV_ASSERT_NULL(txt);
165 
166     lv_textarea_t * ta = (lv_textarea_t *)obj;
167 
168     if(ta->pwd_mode) pwd_char_hider(obj); /*Make sure all the current text contains only '*'*/
169 
170     /*Add the character one-by-one if not all characters are accepted or there is character limit.*/
171     if(lv_textarea_get_accepted_chars(obj) || lv_textarea_get_max_length(obj)) {
172         uint32_t i = 0;
173         while(txt[i] != '\0') {
174             uint32_t c = _lv_txt_encoded_next(txt, &i);
175             lv_textarea_add_char(obj, _lv_txt_unicode_to_encoded(c));
176         }
177         return;
178     }
179 
180     lv_res_t res = insert_handler(obj, txt);
181     if(res != LV_RES_OK) return;
182 
183     /*If the textarea is empty, invalidate it to hide the placeholder*/
184     if(ta->placeholder_txt) {
185         const char * txt_act = lv_label_get_text(ta->label);
186         if(txt_act[0] == '\0') lv_obj_invalidate(obj);
187     }
188 
189     /*Insert the text*/
190     lv_label_ins_text(ta->label, ta->cursor.pos, txt);
191     lv_textarea_clear_selection(obj);
192 
193     if(ta->pwd_mode) {
194         size_t realloc_size = strlen(ta->pwd_tmp) + strlen(txt) + 1;
195         ta->pwd_tmp = lv_mem_realloc(ta->pwd_tmp, realloc_size);
196         LV_ASSERT_MALLOC(ta->pwd_tmp);
197         if(ta->pwd_tmp == NULL) return;
198 
199         _lv_txt_ins(ta->pwd_tmp, ta->cursor.pos, txt);
200 
201         /*Auto hide characters*/
202         auto_hide_characters(obj);
203     }
204 
205     /*Move the cursor after the new text*/
206     lv_textarea_set_cursor_pos(obj, lv_textarea_get_cursor_pos(obj) + _lv_txt_get_encoded_length(txt));
207 
208     lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
209 }
210 
lv_textarea_del_char(lv_obj_t * obj)211 void lv_textarea_del_char(lv_obj_t * obj)
212 {
213     LV_ASSERT_OBJ(obj, MY_CLASS);
214 
215     lv_textarea_t * ta = (lv_textarea_t *)obj;
216     uint32_t cur_pos  = ta->cursor.pos;
217 
218     if(cur_pos == 0) return;
219 
220     char del_buf[2]   = {LV_KEY_DEL, '\0'};
221 
222     lv_res_t res = insert_handler(obj, del_buf);
223     if(res != LV_RES_OK) return;
224 
225     char * label_txt = lv_label_get_text(ta->label);
226 
227     /*Delete a character*/
228     _lv_txt_cut(label_txt, ta->cursor.pos - 1, 1);
229 
230     /*Refresh the label*/
231     lv_label_set_text(ta->label, label_txt);
232     lv_textarea_clear_selection(obj);
233 
234     /*If the textarea became empty, invalidate it to hide the placeholder*/
235     if(ta->placeholder_txt) {
236         const char * txt = lv_label_get_text(ta->label);
237         if(txt[0] == '\0') lv_obj_invalidate(obj);
238     }
239 
240     if(ta->pwd_mode) {
241         _lv_txt_cut(ta->pwd_tmp, ta->cursor.pos - 1, 1);
242 
243         ta->pwd_tmp = lv_mem_realloc(ta->pwd_tmp, strlen(ta->pwd_tmp) + 1);
244         LV_ASSERT_MALLOC(ta->pwd_tmp);
245         if(ta->pwd_tmp == NULL) return;
246     }
247 
248     /*Move the cursor to the place of the deleted character*/
249     lv_textarea_set_cursor_pos(obj, ta->cursor.pos - 1);
250 
251     lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
252 
253 }
254 
lv_textarea_del_char_forward(lv_obj_t * obj)255 void lv_textarea_del_char_forward(lv_obj_t * obj)
256 {
257     LV_ASSERT_OBJ(obj, MY_CLASS);
258 
259     uint32_t cp = lv_textarea_get_cursor_pos(obj);
260     lv_textarea_set_cursor_pos(obj, cp + 1);
261     if(cp != lv_textarea_get_cursor_pos(obj)) lv_textarea_del_char(obj);
262 }
263 
264 /*=====================
265  * Setter functions
266  *====================*/
267 
lv_textarea_set_text(lv_obj_t * obj,const char * txt)268 void lv_textarea_set_text(lv_obj_t * obj, const char * txt)
269 {
270     LV_ASSERT_OBJ(obj, MY_CLASS);
271     LV_ASSERT_NULL(txt);
272 
273     lv_textarea_t * ta = (lv_textarea_t *)obj;
274 
275     /*Clear the existing selection*/
276     lv_textarea_clear_selection(obj);
277 
278     /*Add the character one-by-one if not all characters are accepted or there is character limit.*/
279     if(lv_textarea_get_accepted_chars(obj) || lv_textarea_get_max_length(obj)) {
280         lv_label_set_text(ta->label, "");
281         lv_textarea_set_cursor_pos(obj, LV_TEXTAREA_CURSOR_LAST);
282         if(ta->pwd_mode) {
283             ta->pwd_tmp[0] = '\0'; /*Clear the password too*/
284         }
285         uint32_t i = 0;
286         while(txt[i] != '\0') {
287             uint32_t c = _lv_txt_encoded_next(txt, &i);
288             lv_textarea_add_char(obj, _lv_txt_unicode_to_encoded(c));
289         }
290     }
291     else {
292         lv_label_set_text(ta->label, txt);
293         lv_textarea_set_cursor_pos(obj, LV_TEXTAREA_CURSOR_LAST);
294     }
295 
296     /*If the textarea is empty, invalidate it to hide the placeholder*/
297     if(ta->placeholder_txt) {
298         const char * txt_act = lv_label_get_text(ta->label);
299         if(txt_act[0] == '\0') lv_obj_invalidate(obj);
300     }
301 
302     if(ta->pwd_mode) {
303         ta->pwd_tmp = lv_mem_realloc(ta->pwd_tmp, strlen(txt) + 1);
304         LV_ASSERT_MALLOC(ta->pwd_tmp);
305         if(ta->pwd_tmp == NULL) return;
306         strcpy(ta->pwd_tmp, txt);
307 
308         /*Auto hide characters*/
309         auto_hide_characters(obj);
310     }
311 
312     lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
313 }
314 
lv_textarea_set_placeholder_text(lv_obj_t * obj,const char * txt)315 void lv_textarea_set_placeholder_text(lv_obj_t * obj, const char * txt)
316 {
317     LV_ASSERT_OBJ(obj, MY_CLASS);
318     LV_ASSERT_NULL(txt);
319 
320     lv_textarea_t * ta = (lv_textarea_t *)obj;
321 
322     size_t txt_len = strlen(txt);
323     if((txt_len == 0) && (ta->placeholder_txt)) {
324         lv_mem_free(ta->placeholder_txt);
325         ta->placeholder_txt = NULL;
326     }
327     else {
328         /*Allocate memory for the placeholder_txt text*/
329         /*NOTE: Using special realloc behavior, malloc-like when data_p is NULL*/
330         ta->placeholder_txt = lv_mem_realloc(ta->placeholder_txt, txt_len + 1);
331         LV_ASSERT_MALLOC(ta->placeholder_txt);
332         if(ta->placeholder_txt == NULL) {
333             LV_LOG_ERROR("lv_textarea_set_placeholder_text: couldn't allocate memory for placeholder");
334             return;
335         }
336 
337         strcpy(ta->placeholder_txt, txt);
338         ta->placeholder_txt[txt_len] = '\0';
339     }
340 
341     lv_obj_invalidate(obj);
342 }
343 
lv_textarea_set_cursor_pos(lv_obj_t * obj,int32_t pos)344 void lv_textarea_set_cursor_pos(lv_obj_t * obj, int32_t pos)
345 {
346     LV_ASSERT_OBJ(obj, MY_CLASS);
347 
348     lv_textarea_t * ta = (lv_textarea_t *)obj;
349     if((uint32_t)ta->cursor.pos == (uint32_t)pos) return;
350 
351     uint32_t len = _lv_txt_get_encoded_length(lv_label_get_text(ta->label));
352 
353     if(pos < 0) pos = len + pos;
354 
355     if(pos > (int32_t)len || pos == LV_TEXTAREA_CURSOR_LAST) pos = len;
356 
357     ta->cursor.pos = pos;
358 
359     /*Position the label to make the cursor visible*/
360     lv_obj_update_layout(obj);
361 
362     lv_point_t cur_pos;
363     const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
364     lv_label_get_letter_pos(ta->label, pos, &cur_pos);
365 
366     /*The text area needs to have it's final size to see if the cursor is out of the area or not*/
367 
368     /*Check the top*/
369     lv_coord_t font_h = lv_font_get_line_height(font);
370     if(cur_pos.y < lv_obj_get_scroll_top(obj)) {
371         lv_obj_scroll_to_y(obj, cur_pos.y, LV_ANIM_ON);
372     }
373     /*Check the bottom*/
374     lv_coord_t h = lv_obj_get_content_height(obj);
375     if(cur_pos.y + font_h - lv_obj_get_scroll_top(obj) > h) {
376         lv_obj_scroll_to_y(obj, cur_pos.y - h + font_h, LV_ANIM_ON);
377     }
378 
379     /*Check the left*/
380     if(cur_pos.x < lv_obj_get_scroll_left(obj)) {
381         lv_obj_scroll_to_x(obj, cur_pos.x, LV_ANIM_ON);
382     }
383     /*Check the right*/
384     lv_coord_t w = lv_obj_get_content_width(obj);
385     if(cur_pos.x + font_h - lv_obj_get_scroll_left(obj) > w) {
386         lv_obj_scroll_to_x(obj, cur_pos.x - w + font_h, LV_ANIM_ON);
387     }
388 
389     ta->cursor.valid_x = cur_pos.x;
390 
391     start_cursor_blink(obj);
392 
393     refr_cursor_area(obj);
394 }
395 
lv_textarea_set_cursor_click_pos(lv_obj_t * obj,bool en)396 void lv_textarea_set_cursor_click_pos(lv_obj_t * obj, bool en)
397 {
398     LV_ASSERT_OBJ(obj, MY_CLASS);
399 
400     lv_textarea_t * ta = (lv_textarea_t *)obj;
401     ta->cursor.click_pos = en ? 1U : 0U;
402 }
403 
lv_textarea_set_password_mode(lv_obj_t * obj,bool en)404 void lv_textarea_set_password_mode(lv_obj_t * obj, bool en)
405 {
406     LV_ASSERT_OBJ(obj, MY_CLASS);
407 
408     lv_textarea_t * ta = (lv_textarea_t *)obj;
409     if(ta->pwd_mode == en) return;
410 
411     ta->pwd_mode = en ? 1U : 0U;
412     /*Pwd mode is now enabled*/
413     if(en) {
414         char * txt = lv_label_get_text(ta->label);
415         size_t len = strlen(txt);
416 
417         ta->pwd_tmp = lv_mem_alloc(len + 1);
418         LV_ASSERT_MALLOC(ta->pwd_tmp);
419         if(ta->pwd_tmp == NULL) return;
420 
421         strcpy(ta->pwd_tmp, txt);
422 
423         pwd_char_hider(obj);
424 
425         lv_textarea_clear_selection(obj);
426     }
427     /*Pwd mode is now disabled*/
428     else {
429         lv_textarea_clear_selection(obj);
430         lv_label_set_text(ta->label, ta->pwd_tmp);
431         lv_mem_free(ta->pwd_tmp);
432         ta->pwd_tmp = NULL;
433     }
434 
435     refr_cursor_area(obj);
436 }
437 
lv_textarea_set_password_bullet(lv_obj_t * obj,const char * bullet)438 void lv_textarea_set_password_bullet(lv_obj_t * obj, const char * bullet)
439 {
440     LV_ASSERT_OBJ(obj, MY_CLASS);
441     LV_ASSERT_NULL(bullet);
442 
443     lv_textarea_t * ta = (lv_textarea_t *)obj;
444 
445     if(!bullet && (ta->pwd_bullet)) {
446         lv_mem_free(ta->pwd_bullet);
447         ta->pwd_bullet = NULL;
448     }
449     else {
450         size_t txt_len = strlen(bullet);
451 
452         /*Allocate memory for the pwd_bullet text*/
453         /*NOTE: Using special realloc behavior, malloc-like when data_p is NULL*/
454         ta->pwd_bullet = lv_mem_realloc(ta->pwd_bullet, txt_len + 1);
455         LV_ASSERT_MALLOC(ta->pwd_bullet);
456         if(ta->pwd_bullet == NULL) {
457             LV_LOG_ERROR("lv_textarea_set_password_bullet: couldn't allocate memory for bullet");
458             return;
459         }
460 
461         strcpy(ta->pwd_bullet, bullet);
462         ta->pwd_bullet[txt_len] = '\0';
463     }
464 
465     lv_obj_invalidate(obj);
466 }
467 
lv_textarea_set_one_line(lv_obj_t * obj,bool en)468 void lv_textarea_set_one_line(lv_obj_t * obj, bool en)
469 {
470     LV_ASSERT_OBJ(obj, MY_CLASS);
471 
472     lv_textarea_t * ta = (lv_textarea_t *)obj;
473     if(ta->one_line == en) return;
474 
475     ta->one_line = en ? 1U : 0U;
476     lv_coord_t width = en ? LV_SIZE_CONTENT : lv_pct(100);
477     lv_coord_t min_width_value = en ? lv_pct(100) : 0;
478 
479     lv_obj_set_width(ta->label, width);
480     lv_obj_set_style_min_width(ta->label, min_width_value, 0);
481 
482     if(en) {
483         lv_obj_set_height(obj, LV_SIZE_CONTENT);
484     }
485     else {
486         lv_obj_remove_local_style_prop(obj, LV_STYLE_HEIGHT, LV_PART_MAIN);
487     }
488 
489     lv_obj_scroll_to(obj, 0, 0, LV_ANIM_OFF);
490 }
491 
lv_textarea_set_accepted_chars(lv_obj_t * obj,const char * list)492 void lv_textarea_set_accepted_chars(lv_obj_t * obj, const char * list)
493 {
494     LV_ASSERT_OBJ(obj, MY_CLASS);
495 
496     lv_textarea_t * ta = (lv_textarea_t *)obj;
497 
498     ta->accepted_chars = list;
499 }
500 
lv_textarea_set_max_length(lv_obj_t * obj,uint32_t num)501 void lv_textarea_set_max_length(lv_obj_t * obj, uint32_t num)
502 {
503     LV_ASSERT_OBJ(obj, MY_CLASS);
504 
505     lv_textarea_t * ta = (lv_textarea_t *)obj;
506 
507     ta->max_length = num;
508 }
509 
lv_textarea_set_insert_replace(lv_obj_t * obj,const char * txt)510 void lv_textarea_set_insert_replace(lv_obj_t * obj, const char * txt)
511 {
512     LV_ASSERT_OBJ(obj, MY_CLASS);
513 
514     LV_UNUSED(obj);
515     ta_insert_replace = txt;
516 }
517 
lv_textarea_set_text_selection(lv_obj_t * obj,bool en)518 void lv_textarea_set_text_selection(lv_obj_t * obj, bool en)
519 {
520     LV_ASSERT_OBJ(obj, MY_CLASS);
521 
522 #if LV_LABEL_TEXT_SELECTION
523     lv_textarea_t * ta = (lv_textarea_t *)obj;
524 
525     ta->text_sel_en = en;
526 
527     if(!en) lv_textarea_clear_selection(obj);
528 #else
529     LV_UNUSED(obj); /*Unused*/
530     LV_UNUSED(en);  /*Unused*/
531 #endif
532 }
533 
lv_textarea_set_password_show_time(lv_obj_t * obj,uint16_t time)534 void lv_textarea_set_password_show_time(lv_obj_t * obj, uint16_t time)
535 {
536     LV_ASSERT_OBJ(obj, MY_CLASS);
537 
538     lv_textarea_t * ta = (lv_textarea_t *)obj;
539     ta->pwd_show_time = time;
540 }
541 
lv_textarea_set_align(lv_obj_t * obj,lv_text_align_t align)542 void lv_textarea_set_align(lv_obj_t * obj, lv_text_align_t align)
543 {
544     LV_LOG_WARN("Deprecated: use the normal text_align style property instead");
545     lv_obj_set_style_text_align(obj, align, 0);
546 
547     switch(align) {
548         default:
549         case LV_TEXT_ALIGN_LEFT:
550             lv_obj_align(lv_textarea_get_label(obj), LV_ALIGN_TOP_LEFT, 0, 0);
551             break;
552         case LV_TEXT_ALIGN_RIGHT:
553             lv_obj_align(lv_textarea_get_label(obj), LV_ALIGN_TOP_RIGHT, 0, 0);
554             break;
555         case LV_TEXT_ALIGN_CENTER:
556             lv_obj_align(lv_textarea_get_label(obj), LV_ALIGN_TOP_MID, 0, 0);
557             break;
558     }
559 }
560 
561 /*=====================
562  * Getter functions
563  *====================*/
564 
lv_textarea_get_text(const lv_obj_t * obj)565 const char * lv_textarea_get_text(const lv_obj_t * obj)
566 {
567     LV_ASSERT_OBJ(obj, MY_CLASS);
568 
569     lv_textarea_t * ta = (lv_textarea_t *)obj;
570 
571     const char * txt;
572     if(ta->pwd_mode == 0) {
573         txt = lv_label_get_text(ta->label);
574     }
575     else {
576         txt = ta->pwd_tmp;
577     }
578 
579     return txt;
580 }
581 
lv_textarea_get_placeholder_text(lv_obj_t * obj)582 const char * lv_textarea_get_placeholder_text(lv_obj_t * obj)
583 {
584     LV_ASSERT_OBJ(obj, MY_CLASS);
585 
586     lv_textarea_t * ta = (lv_textarea_t *)obj;
587     if(ta->placeholder_txt) return ta->placeholder_txt;
588     else return "";
589 }
590 
lv_textarea_get_label(const lv_obj_t * obj)591 lv_obj_t * lv_textarea_get_label(const lv_obj_t * obj)
592 {
593     LV_ASSERT_OBJ(obj, MY_CLASS);
594 
595     lv_textarea_t * ta = (lv_textarea_t *)obj;
596     return ta->label;
597 }
598 
lv_textarea_get_cursor_pos(const lv_obj_t * obj)599 uint32_t lv_textarea_get_cursor_pos(const lv_obj_t * obj)
600 {
601     LV_ASSERT_OBJ(obj, MY_CLASS);
602 
603     lv_textarea_t * ta = (lv_textarea_t *)obj;
604     return ta->cursor.pos;
605 }
606 
lv_textarea_get_cursor_click_pos(lv_obj_t * obj)607 bool lv_textarea_get_cursor_click_pos(lv_obj_t * obj)
608 {
609     LV_ASSERT_OBJ(obj, MY_CLASS);
610 
611     lv_textarea_t * ta = (lv_textarea_t *)obj;
612     return ta->cursor.click_pos ? true : false;
613 }
614 
lv_textarea_get_password_mode(const lv_obj_t * obj)615 bool lv_textarea_get_password_mode(const lv_obj_t * obj)
616 {
617     LV_ASSERT_OBJ(obj, MY_CLASS);
618 
619     lv_textarea_t * ta = (lv_textarea_t *)obj;
620     return ta->pwd_mode == 1U;
621 }
622 
lv_textarea_get_password_bullet(lv_obj_t * obj)623 const char * lv_textarea_get_password_bullet(lv_obj_t * obj)
624 {
625     LV_ASSERT_OBJ(obj, MY_CLASS);
626 
627     lv_textarea_t * ta = (lv_textarea_t *)obj;
628 
629     if(ta->pwd_bullet) return ta->pwd_bullet;
630 
631     lv_font_glyph_dsc_t g;
632     const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
633 
634     /*If the textarea's font has the bullet character use it else fallback to "*"*/
635     if(lv_font_get_glyph_dsc(font, &g, LV_TEXTAREA_PWD_BULLET_UNICODE, 0))
636         return LV_SYMBOL_BULLET;
637     return "*";
638 }
639 
lv_textarea_get_one_line(const lv_obj_t * obj)640 bool lv_textarea_get_one_line(const lv_obj_t * obj)
641 {
642     LV_ASSERT_OBJ(obj, MY_CLASS);
643 
644     lv_textarea_t * ta = (lv_textarea_t *)obj;
645     return ta->one_line == 1U;
646 }
647 
lv_textarea_get_accepted_chars(lv_obj_t * obj)648 const char * lv_textarea_get_accepted_chars(lv_obj_t * obj)
649 {
650     LV_ASSERT_OBJ(obj, MY_CLASS);
651 
652     lv_textarea_t * ta = (lv_textarea_t *)obj;
653 
654     return ta->accepted_chars;
655 }
656 
lv_textarea_get_max_length(lv_obj_t * obj)657 uint32_t lv_textarea_get_max_length(lv_obj_t * obj)
658 {
659     LV_ASSERT_OBJ(obj, MY_CLASS);
660 
661     lv_textarea_t * ta = (lv_textarea_t *)obj;
662     return ta->max_length;
663 }
664 
lv_textarea_text_is_selected(const lv_obj_t * obj)665 bool lv_textarea_text_is_selected(const lv_obj_t * obj)
666 {
667     LV_ASSERT_OBJ(obj, MY_CLASS);
668 
669 #if LV_LABEL_TEXT_SELECTION
670     lv_textarea_t * ta = (lv_textarea_t *)obj;
671 
672     if((lv_label_get_text_selection_start(ta->label) != LV_DRAW_LABEL_NO_TXT_SEL ||
673         lv_label_get_text_selection_end(ta->label) != LV_DRAW_LABEL_NO_TXT_SEL)) {
674         return true;
675     }
676     else {
677         return false;
678     }
679 #else
680     LV_UNUSED(obj); /*Unused*/
681     return false;
682 #endif
683 }
684 
lv_textarea_get_text_selection(lv_obj_t * obj)685 bool lv_textarea_get_text_selection(lv_obj_t * obj)
686 {
687     LV_ASSERT_OBJ(obj, MY_CLASS);
688 
689 #if LV_LABEL_TEXT_SELECTION
690     lv_textarea_t * ta = (lv_textarea_t *)obj;
691     return ta->text_sel_en;
692 #else
693     LV_UNUSED(obj); /*Unused*/
694     return false;
695 #endif
696 }
697 
lv_textarea_get_password_show_time(lv_obj_t * obj)698 uint16_t lv_textarea_get_password_show_time(lv_obj_t * obj)
699 {
700     LV_ASSERT_OBJ(obj, MY_CLASS);
701 
702     lv_textarea_t * ta = (lv_textarea_t *)obj;
703 
704     return ta->pwd_show_time;
705 }
706 
707 /*=====================
708  * Other functions
709  *====================*/
710 
lv_textarea_clear_selection(lv_obj_t * obj)711 void lv_textarea_clear_selection(lv_obj_t * obj)
712 {
713     LV_ASSERT_OBJ(obj, MY_CLASS);
714 
715 #if LV_LABEL_TEXT_SELECTION
716     lv_textarea_t * ta = (lv_textarea_t *)obj;
717 
718     if(lv_label_get_text_selection_start(ta->label) != LV_DRAW_LABEL_NO_TXT_SEL ||
719        lv_label_get_text_selection_end(ta->label) != LV_DRAW_LABEL_NO_TXT_SEL) {
720         lv_label_set_text_sel_start(ta->label, LV_DRAW_LABEL_NO_TXT_SEL);
721         lv_label_set_text_sel_end(ta->label, LV_DRAW_LABEL_NO_TXT_SEL);
722     }
723 #else
724     LV_UNUSED(obj); /*Unused*/
725 #endif
726 }
727 
lv_textarea_cursor_right(lv_obj_t * obj)728 void lv_textarea_cursor_right(lv_obj_t * obj)
729 {
730     LV_ASSERT_OBJ(obj, MY_CLASS);
731 
732     uint32_t cp = lv_textarea_get_cursor_pos(obj);
733     cp++;
734     lv_textarea_set_cursor_pos(obj, cp);
735 }
736 
lv_textarea_cursor_left(lv_obj_t * obj)737 void lv_textarea_cursor_left(lv_obj_t * obj)
738 {
739     LV_ASSERT_OBJ(obj, MY_CLASS);
740 
741     uint32_t cp = lv_textarea_get_cursor_pos(obj);
742     if(cp > 0) {
743         cp--;
744         lv_textarea_set_cursor_pos(obj, cp);
745     }
746 }
747 
lv_textarea_cursor_down(lv_obj_t * obj)748 void lv_textarea_cursor_down(lv_obj_t * obj)
749 {
750     LV_ASSERT_OBJ(obj, MY_CLASS);
751 
752     lv_textarea_t * ta = (lv_textarea_t *)obj;
753     lv_point_t pos;
754 
755     /*Get the position of the current letter*/
756     lv_label_get_letter_pos(ta->label, lv_textarea_get_cursor_pos(obj), &pos);
757 
758     /*Increment the y with one line and keep the valid x*/
759 
760     lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN);
761     const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
762     lv_coord_t font_h              = lv_font_get_line_height(font);
763     pos.y += font_h + line_space + 1;
764     pos.x = ta->cursor.valid_x;
765 
766     /*Do not go below the last line*/
767     if(pos.y < lv_obj_get_height(ta->label)) {
768         /*Get the letter index on the new cursor position and set it*/
769         uint32_t new_cur_pos = lv_label_get_letter_on(ta->label, &pos);
770 
771         lv_coord_t cur_valid_x_tmp = ta->cursor.valid_x; /*Cursor position set overwrites the valid position*/
772         lv_textarea_set_cursor_pos(obj, new_cur_pos);
773         ta->cursor.valid_x = cur_valid_x_tmp;
774     }
775 }
776 
lv_textarea_cursor_up(lv_obj_t * obj)777 void lv_textarea_cursor_up(lv_obj_t * obj)
778 {
779     LV_ASSERT_OBJ(obj, MY_CLASS);
780 
781     lv_textarea_t * ta = (lv_textarea_t *)obj;
782     lv_point_t pos;
783 
784     /*Get the position of the current letter*/
785     lv_label_get_letter_pos(ta->label, lv_textarea_get_cursor_pos(obj), &pos);
786 
787     /*Decrement the y with one line and keep the valid x*/
788     lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN);
789     const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
790     lv_coord_t font_h              = lv_font_get_line_height(font);
791     pos.y -= font_h + line_space - 1;
792     pos.x = ta->cursor.valid_x;
793 
794     /*Get the letter index on the new cursor position and set it*/
795     uint32_t new_cur_pos       = lv_label_get_letter_on(ta->label, &pos);
796     lv_coord_t cur_valid_x_tmp = ta->cursor.valid_x; /*Cursor position set overwrites the valid position*/
797     lv_textarea_set_cursor_pos(obj, new_cur_pos);
798     ta->cursor.valid_x = cur_valid_x_tmp;
799 }
800 
801 /**********************
802  *   STATIC FUNCTIONS
803  **********************/
804 
lv_textarea_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)805 static void lv_textarea_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
806 {
807     LV_UNUSED(class_p);
808     LV_TRACE_OBJ_CREATE("begin");
809 
810     lv_textarea_t * ta = (lv_textarea_t *)obj;
811 
812     ta->pwd_mode          = 0;
813     ta->pwd_tmp           = NULL;
814     ta->pwd_bullet        = NULL;
815     ta->pwd_show_time     = LV_TEXTAREA_DEF_PWD_SHOW_TIME;
816     ta->accepted_chars    = NULL;
817     ta->max_length        = 0;
818     ta->cursor.show      = 1;
819     /*It will be set to zero later (with zero value lv_textarea_set_cursor_pos(obj, 0); wouldn't do anything as there is no difference)*/
820     ta->cursor.pos        = 1;
821     ta->cursor.click_pos  = 1;
822     ta->cursor.valid_x    = 0;
823     ta->one_line          = 0;
824 #if LV_LABEL_TEXT_SELECTION
825     ta->text_sel_en = 0;
826 #endif
827     ta->label       = NULL;
828     ta->placeholder_txt = NULL;
829 
830     ta->label = lv_label_create(obj);
831     lv_obj_set_width(ta->label, lv_pct(100));
832     lv_label_set_text(ta->label, "");
833     lv_obj_add_event_cb(ta->label, label_event_cb, LV_EVENT_ALL, NULL);
834     lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
835     lv_textarea_set_cursor_pos(obj, 0);
836 
837     start_cursor_blink(obj);
838 
839     LV_TRACE_OBJ_CREATE("finished");
840 }
841 
lv_textarea_destructor(const lv_obj_class_t * class_p,lv_obj_t * obj)842 static void lv_textarea_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
843 {
844     LV_UNUSED(class_p);
845 
846     lv_textarea_t * ta = (lv_textarea_t *)obj;
847     if(ta->pwd_tmp != NULL) {
848         lv_mem_free(ta->pwd_tmp);
849         ta->pwd_tmp = NULL;
850     }
851     if(ta->pwd_bullet != NULL) {
852         lv_mem_free(ta->pwd_bullet);
853         ta->pwd_bullet = NULL;
854     }
855     if(ta->placeholder_txt != NULL) {
856         lv_mem_free(ta->placeholder_txt);
857         ta->placeholder_txt = NULL;
858     }
859 }
860 
lv_textarea_event(const lv_obj_class_t * class_p,lv_event_t * e)861 static void lv_textarea_event(const lv_obj_class_t * class_p, lv_event_t * e)
862 {
863     LV_UNUSED(class_p);
864 
865     lv_res_t res;
866     /*Call the ancestor's event handler*/
867     res = lv_obj_event_base(MY_CLASS, e);
868     if(res != LV_RES_OK) return;
869 
870     lv_event_code_t code = lv_event_get_code(e);
871     lv_obj_t * obj = lv_event_get_target(e);
872 
873     if(code == LV_EVENT_FOCUSED) {
874         start_cursor_blink(obj);
875     }
876     else if(code == LV_EVENT_KEY) {
877         uint32_t c = *((uint32_t *)lv_event_get_param(e)); /*uint32_t because can be UTF-8*/
878         if(c == LV_KEY_RIGHT)
879             lv_textarea_cursor_right(obj);
880         else if(c == LV_KEY_LEFT)
881             lv_textarea_cursor_left(obj);
882         else if(c == LV_KEY_UP)
883             lv_textarea_cursor_up(obj);
884         else if(c == LV_KEY_DOWN)
885             lv_textarea_cursor_down(obj);
886         else if(c == LV_KEY_BACKSPACE)
887             lv_textarea_del_char(obj);
888         else if(c == LV_KEY_DEL)
889             lv_textarea_del_char_forward(obj);
890         else if(c == LV_KEY_HOME)
891             lv_textarea_set_cursor_pos(obj, 0);
892         else if(c == LV_KEY_END)
893             lv_textarea_set_cursor_pos(obj, LV_TEXTAREA_CURSOR_LAST);
894         else if(c == LV_KEY_ENTER && lv_textarea_get_one_line(obj))
895             lv_event_send(obj, LV_EVENT_READY, NULL);
896         else {
897             lv_textarea_add_char(obj, c);
898         }
899     }
900     else if(code == LV_EVENT_PRESSED || code == LV_EVENT_PRESSING || code == LV_EVENT_PRESS_LOST ||
901             code == LV_EVENT_RELEASED) {
902         update_cursor_position_on_click(e);
903     }
904     else if(code == LV_EVENT_DRAW_MAIN) {
905         draw_placeholder(e);
906     }
907     else if(code == LV_EVENT_DRAW_POST) {
908         draw_cursor(e);
909     }
910 }
911 
label_event_cb(lv_event_t * e)912 static void label_event_cb(lv_event_t * e)
913 {
914     lv_event_code_t code = lv_event_get_code(e);
915     lv_obj_t * label = lv_event_get_target(e);
916     lv_obj_t * ta = lv_obj_get_parent(label);
917 
918     if(code == LV_EVENT_STYLE_CHANGED || code == LV_EVENT_SIZE_CHANGED) {
919         lv_label_set_text(label, NULL);
920         refr_cursor_area(ta);
921         start_cursor_blink(ta);
922     }
923 }
924 
925 /**
926  * Called to blink the cursor
927  * @param ta pointer to a text area
928  * @param hide 1: hide the cursor, 0: show it
929  */
cursor_blink_anim_cb(void * obj,int32_t show)930 static void cursor_blink_anim_cb(void * obj, int32_t show)
931 {
932     lv_textarea_t * ta = (lv_textarea_t *)obj;
933     if(show != ta->cursor.show) {
934         ta->cursor.show = show ? 1U : 0U;
935         lv_area_t area_tmp;
936         lv_area_copy(&area_tmp, &ta->cursor.area);
937         area_tmp.x1 += ta->label->coords.x1;
938         area_tmp.y1 += ta->label->coords.y1;
939         area_tmp.x2 += ta->label->coords.x1;
940         area_tmp.y2 += ta->label->coords.y1;
941         lv_obj_invalidate_area(obj, &area_tmp);
942     }
943 }
944 
945 /**
946  * Dummy function to animate char hiding in pwd mode.
947  * Does nothing, but a function is required in car hiding anim.
948  * (pwd_char_hider callback do the real job)
949  * @param ta unused
950  * @param x unused
951  */
pwd_char_hider_anim(void * obj,int32_t x)952 static void pwd_char_hider_anim(void * obj, int32_t x)
953 {
954     LV_UNUSED(obj);
955     LV_UNUSED(x);
956 }
957 
958 /**
959  * Call when an animation is ready to convert all characters to '*'
960  * @param a pointer to the animation
961  */
pwd_char_hider_anim_ready(lv_anim_t * a)962 static void pwd_char_hider_anim_ready(lv_anim_t * a)
963 {
964     lv_obj_t * obj = a->var;
965     pwd_char_hider(obj);
966 }
967 
968 /**
969  * Hide all characters (convert them to '*')
970  * @param ta pointer to text area object
971  */
pwd_char_hider(lv_obj_t * obj)972 static void pwd_char_hider(lv_obj_t * obj)
973 {
974     lv_textarea_t * ta = (lv_textarea_t *)obj;
975     if(ta->pwd_mode == 0) {
976         return;
977     }
978 
979     /* When ta->label is empty we get 0 back */
980     char * txt = lv_label_get_text(ta->label);
981     uint32_t enc_len = _lv_txt_get_encoded_length(txt);
982     if(enc_len == 0) return;
983 
984     const char * bullet = lv_textarea_get_password_bullet(obj);
985     const size_t bullet_len = strlen(bullet);
986     char * txt_tmp = lv_mem_buf_get(enc_len * bullet_len + 1);
987 
988     uint32_t i;
989     for(i = 0; i < enc_len; i++) {
990         lv_memcpy(&txt_tmp[i * bullet_len], bullet, bullet_len);
991     }
992     txt_tmp[i * bullet_len] = '\0';
993 
994     lv_label_set_text(ta->label, txt_tmp);
995     lv_mem_buf_release(txt_tmp);
996 
997     refr_cursor_area(obj);
998 }
999 
1000 /**
1001  * Test a unicode character if it is accepted or not. Checks max length and accepted char list.
1002  * @param ta pointer to a test area object
1003  * @param c a unicode character
1004  * @return true: accepted; false: rejected
1005  */
char_is_accepted(lv_obj_t * obj,uint32_t c)1006 static bool char_is_accepted(lv_obj_t * obj, uint32_t c)
1007 {
1008     lv_textarea_t * ta = (lv_textarea_t *)obj;
1009 
1010     /*Too many characters?*/
1011     if(ta->max_length > 0 && _lv_txt_get_encoded_length(lv_textarea_get_text(obj)) >= ta->max_length) {
1012         return false;
1013     }
1014 
1015     if(ta->accepted_chars == NULL || ta->accepted_chars[0] == '\0') return true;
1016     /*Accepted character?*/
1017     uint32_t i = 0;
1018 
1019     while(ta->accepted_chars[i] != '\0') {
1020         uint32_t a = _lv_txt_encoded_next(ta->accepted_chars, &i);
1021         if(a == c) return true; /*Accepted*/
1022     }
1023 
1024     return false; /*The character wasn't in the list*/
1025 }
1026 
start_cursor_blink(lv_obj_t * obj)1027 static void start_cursor_blink(lv_obj_t * obj)
1028 {
1029     lv_textarea_t * ta = (lv_textarea_t *)obj;
1030     uint32_t blink_time = lv_obj_get_style_anim_time(obj, LV_PART_CURSOR);
1031     if(blink_time == 0) {
1032         lv_anim_del(obj, cursor_blink_anim_cb);
1033         ta->cursor.show = 1;
1034     }
1035     else {
1036         lv_anim_t a;
1037         lv_anim_init(&a);
1038         lv_anim_set_var(&a, ta);
1039         lv_anim_set_exec_cb(&a, cursor_blink_anim_cb);
1040         lv_anim_set_time(&a, blink_time);
1041         lv_anim_set_playback_time(&a, blink_time);
1042         lv_anim_set_values(&a, 1, 0);
1043         lv_anim_set_path_cb(&a, lv_anim_path_step);
1044         lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
1045         lv_anim_start(&a);
1046     }
1047 }
1048 
refr_cursor_area(lv_obj_t * obj)1049 static void refr_cursor_area(lv_obj_t * obj)
1050 {
1051     lv_textarea_t * ta = (lv_textarea_t *)obj;
1052 
1053     const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
1054     lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN);
1055 
1056     uint32_t cur_pos = lv_textarea_get_cursor_pos(obj);
1057     const char * txt = lv_label_get_text(ta->label);
1058 
1059     uint32_t byte_pos = _lv_txt_encoded_get_byte_id(txt, cur_pos);
1060     uint32_t letter = _lv_txt_encoded_next(&txt[byte_pos], NULL);
1061 
1062     /* Letter height and width */
1063     const lv_coord_t letter_h = lv_font_get_line_height(font);
1064     /*Set letter_w (set not 0 on non printable but valid chars)*/
1065     uint32_t letter_space = letter;
1066     if(is_valid_but_non_printable_char(letter)) {
1067         letter_space = ' ';
1068     }
1069     lv_coord_t letter_w = lv_font_get_glyph_width(font, letter_space, IGNORE_KERNING);
1070 
1071     lv_point_t letter_pos;
1072     lv_label_get_letter_pos(ta->label, cur_pos, &letter_pos);
1073 
1074     lv_text_align_t align = lv_obj_calculate_style_text_align(ta->label, LV_PART_MAIN, lv_label_get_text(ta->label));
1075 
1076     /*If the cursor is out of the text (most right) draw it to the next line*/
1077     if(((letter_pos.x + ta->label->coords.x1) + letter_w > ta->label->coords.x2) &&
1078        (ta->one_line == 0 && align != LV_TEXT_ALIGN_RIGHT)) {
1079 
1080         letter_pos.x = 0;
1081         letter_pos.y += letter_h + line_space;
1082 
1083         if(letter != '\0') {
1084             byte_pos += _lv_txt_encoded_size(&txt[byte_pos]);
1085             letter = _lv_txt_encoded_next(&txt[byte_pos], NULL);
1086         }
1087 
1088         uint32_t tmp = letter;
1089         if(is_valid_but_non_printable_char(letter)) {
1090             tmp = ' ';
1091         }
1092         letter_w = lv_font_get_glyph_width(font, tmp, IGNORE_KERNING);
1093     }
1094 
1095     /*Save the byte position. It is required to draw `LV_CURSOR_BLOCK`*/
1096     ta->cursor.txt_byte_pos = byte_pos;
1097 
1098     /*Calculate the cursor according to its type*/
1099     lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_CURSOR);
1100     lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_CURSOR) + border_width;
1101     lv_coord_t bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_CURSOR) + border_width;
1102     lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_CURSOR) + border_width;
1103     lv_coord_t right = lv_obj_get_style_pad_right(obj, LV_PART_CURSOR) + border_width;
1104 
1105     lv_area_t cur_area;
1106     cur_area.x1 = letter_pos.x - left;
1107     cur_area.y1 = letter_pos.y - top;
1108     cur_area.x2 = letter_pos.x + right + letter_w - 1;
1109     cur_area.y2 = letter_pos.y + bottom + letter_h - 1;
1110 
1111     /*Save the new area*/
1112     lv_area_t area_tmp;
1113     lv_area_copy(&area_tmp, &ta->cursor.area);
1114     area_tmp.x1 += ta->label->coords.x1;
1115     area_tmp.y1 += ta->label->coords.y1;
1116     area_tmp.x2 += ta->label->coords.x1;
1117     area_tmp.y2 += ta->label->coords.y1;
1118     lv_obj_invalidate_area(obj, &area_tmp);
1119 
1120     lv_area_copy(&ta->cursor.area, &cur_area);
1121 
1122     lv_area_copy(&area_tmp, &ta->cursor.area);
1123     area_tmp.x1 += ta->label->coords.x1;
1124     area_tmp.y1 += ta->label->coords.y1;
1125     area_tmp.x2 += ta->label->coords.x1;
1126     area_tmp.y2 += ta->label->coords.y1;
1127     lv_obj_invalidate_area(obj, &area_tmp);
1128 }
1129 
update_cursor_position_on_click(lv_event_t * e)1130 static void update_cursor_position_on_click(lv_event_t * e)
1131 {
1132     lv_indev_t * click_source = lv_indev_get_act();
1133     if(click_source == NULL) return;
1134 
1135     lv_obj_t * obj = lv_event_get_target(e);
1136     lv_textarea_t * ta = (lv_textarea_t *)obj;
1137     if(ta->cursor.click_pos == 0) return;
1138 
1139     if(lv_indev_get_type(click_source) == LV_INDEV_TYPE_KEYPAD ||
1140        lv_indev_get_type(click_source) == LV_INDEV_TYPE_ENCODER) {
1141         return;
1142     }
1143 
1144     lv_area_t label_coords;
1145     lv_obj_get_coords(ta->label, &label_coords);
1146 
1147     lv_point_t point_act, vect_act;
1148     lv_indev_get_point(click_source, &point_act);
1149     lv_indev_get_vect(click_source, &vect_act);
1150 
1151     if(point_act.x < 0 || point_act.y < 0) return; /*Ignore event from keypad*/
1152     lv_point_t rel_pos;
1153     rel_pos.x = point_act.x - label_coords.x1;
1154     rel_pos.y = point_act.y - label_coords.y1;
1155 
1156     const lv_event_code_t code = lv_event_get_code(e);
1157 
1158     lv_coord_t label_width = lv_obj_get_width(ta->label);
1159     uint16_t char_id_at_click = 0;
1160 
1161 #if LV_LABEL_TEXT_SELECTION
1162     lv_label_t * label_data = (lv_label_t *)ta->label;
1163     bool click_outside_label = false;
1164     /*Check if the click happened on the left side of the area outside the label*/
1165     if(rel_pos.x < 0) {
1166         char_id_at_click = 0;
1167         click_outside_label = true;
1168     }
1169     /*Check if the click happened on the right side of the area outside the label*/
1170     else if(rel_pos.x >= label_width) {
1171         char_id_at_click = LV_TEXTAREA_CURSOR_LAST;
1172         click_outside_label = true;
1173     }
1174     else {
1175         char_id_at_click = lv_label_get_letter_on(ta->label, &rel_pos);
1176         click_outside_label = !lv_label_is_char_under_pos(ta->label, &rel_pos);
1177     }
1178 
1179     if(ta->text_sel_en) {
1180         if(!ta->text_sel_in_prog && !click_outside_label && code == LV_EVENT_PRESSED) {
1181             /*Input device just went down. Store the selection start position*/
1182             ta->sel_start    = char_id_at_click;
1183             ta->sel_end      = LV_LABEL_TEXT_SELECTION_OFF;
1184             ta->text_sel_in_prog = 1;
1185             lv_obj_clear_flag(obj, LV_OBJ_FLAG_SCROLL_CHAIN);
1186         }
1187         else if(ta->text_sel_in_prog && code == LV_EVENT_PRESSING) {
1188             /*Input device may be moving. Store the end position*/
1189             ta->sel_end = char_id_at_click;
1190         }
1191         else if(ta->text_sel_in_prog && (code == LV_EVENT_PRESS_LOST || code == LV_EVENT_RELEASED)) {
1192             /*Input device is released. Check if anything was selected.*/
1193             lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_CHAIN);
1194         }
1195     }
1196 
1197     if(ta->text_sel_in_prog || code == LV_EVENT_PRESSED) lv_textarea_set_cursor_pos(obj, char_id_at_click);
1198 
1199     if(ta->text_sel_in_prog) {
1200         /*If the selected area has changed then update the real values and*/
1201 
1202         /*Invalidate the text area.*/
1203         if(ta->sel_start > ta->sel_end) {
1204             if(label_data->sel_start != ta->sel_end || label_data->sel_end != ta->sel_start) {
1205                 label_data->sel_start = ta->sel_end;
1206                 label_data->sel_end   = ta->sel_start;
1207                 lv_obj_invalidate(obj);
1208             }
1209         }
1210         else if(ta->sel_start < ta->sel_end) {
1211             if(label_data->sel_start != ta->sel_start || label_data->sel_end != ta->sel_end) {
1212                 label_data->sel_start = ta->sel_start;
1213                 label_data->sel_end   = ta->sel_end;
1214                 lv_obj_invalidate(obj);
1215             }
1216         }
1217         else {
1218             if(label_data->sel_start != LV_DRAW_LABEL_NO_TXT_SEL || label_data->sel_end != LV_DRAW_LABEL_NO_TXT_SEL) {
1219                 label_data->sel_start = LV_DRAW_LABEL_NO_TXT_SEL;
1220                 label_data->sel_end   = LV_DRAW_LABEL_NO_TXT_SEL;
1221                 lv_obj_invalidate(obj);
1222             }
1223         }
1224         /*Finish selection if necessary*/
1225         if(code == LV_EVENT_PRESS_LOST || code == LV_EVENT_RELEASED) {
1226             ta->text_sel_in_prog = 0;
1227         }
1228     }
1229 #else
1230     /*Check if the click happened on the left side of the area outside the label*/
1231     if(rel_pos.x < 0) {
1232         char_id_at_click = 0;
1233     }
1234     /*Check if the click happened on the right side of the area outside the label*/
1235     else if(rel_pos.x >= label_width) {
1236         char_id_at_click = LV_TEXTAREA_CURSOR_LAST;
1237     }
1238     else {
1239         char_id_at_click = lv_label_get_letter_on(ta->label, &rel_pos);
1240     }
1241 
1242     if(code == LV_EVENT_PRESSED) lv_textarea_set_cursor_pos(obj, char_id_at_click);
1243 #endif
1244 }
1245 
1246 /* Returns LV_RES_OK when no operation were performed
1247  * Returns LV_RES_INV when a user defined text was inserted */
insert_handler(lv_obj_t * obj,const char * txt)1248 static lv_res_t insert_handler(lv_obj_t * obj, const char * txt)
1249 {
1250     ta_insert_replace = NULL;
1251     lv_event_send(obj, LV_EVENT_INSERT, (char *)txt);
1252 
1253     /* Drop txt if insert replace is set to '\0' */
1254     if(ta_insert_replace && ta_insert_replace[0] == '\0')
1255         return LV_RES_INV;
1256 
1257     if(ta_insert_replace) {
1258         /*Add the replaced text directly it's different from the original*/
1259         if(strcmp(ta_insert_replace, txt)) {
1260             lv_textarea_add_text(obj, ta_insert_replace);
1261             return LV_RES_INV;
1262         }
1263     }
1264 
1265     return LV_RES_OK;
1266 }
1267 
draw_placeholder(lv_event_t * e)1268 static void draw_placeholder(lv_event_t * e)
1269 {
1270     lv_obj_t * obj = lv_event_get_target(e);
1271     lv_textarea_t * ta = (lv_textarea_t *)obj;
1272     lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
1273     const char * txt = lv_label_get_text(ta->label);
1274 
1275     /*Draw the place holder*/
1276     if(txt[0] == '\0' && ta->placeholder_txt && ta->placeholder_txt[0] != 0) {
1277         lv_draw_label_dsc_t ph_dsc;
1278         lv_draw_label_dsc_init(&ph_dsc);
1279         lv_obj_init_draw_label_dsc(obj, LV_PART_TEXTAREA_PLACEHOLDER, &ph_dsc);
1280 
1281         if(ta->one_line) ph_dsc.flag |= LV_TEXT_FLAG_EXPAND;
1282 
1283         lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
1284         lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
1285         lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
1286         lv_area_t ph_coords;
1287         lv_area_copy(&ph_coords, &obj->coords);
1288         lv_area_move(&ph_coords, left + border_width, top + border_width);
1289         lv_draw_label(draw_ctx, &ph_dsc, &ph_coords, ta->placeholder_txt, NULL);
1290     }
1291 }
1292 
draw_cursor(lv_event_t * e)1293 static void draw_cursor(lv_event_t * e)
1294 {
1295     lv_obj_t * obj = lv_event_get_target(e);
1296     lv_textarea_t * ta = (lv_textarea_t *)obj;
1297     lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
1298     const char * txt = lv_label_get_text(ta->label);
1299 
1300     if(ta->cursor.show == 0) return;
1301 
1302     lv_draw_rect_dsc_t cur_dsc;
1303     lv_draw_rect_dsc_init(&cur_dsc);
1304     lv_obj_init_draw_rect_dsc(obj, LV_PART_CURSOR, &cur_dsc);
1305 
1306     /*Draw he cursor according to the type*/
1307     lv_area_t cur_area;
1308     lv_area_copy(&cur_area, &ta->cursor.area);
1309 
1310     cur_area.x1 += ta->label->coords.x1;
1311     cur_area.y1 += ta->label->coords.y1;
1312     cur_area.x2 += ta->label->coords.x1;
1313     cur_area.y2 += ta->label->coords.y1;
1314 
1315     lv_draw_rect(draw_ctx, &cur_dsc, &cur_area);
1316 
1317     lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_CURSOR);
1318     lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_CURSOR) + border_width;
1319     lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_CURSOR) + border_width;
1320     char letter_buf[8] = {0};
1321     lv_memcpy(letter_buf, &txt[ta->cursor.txt_byte_pos], _lv_txt_encoded_size(&txt[ta->cursor.txt_byte_pos]));
1322 
1323     cur_area.x1 += left;
1324     cur_area.y1 += top;
1325 
1326     /*Draw the letter over the cursor only if
1327      *the cursor has background or the letter has different color than the original.
1328      *Else the original letter is drawn twice which makes it look bolder*/
1329     lv_color_t label_color = lv_obj_get_style_text_color(ta->label, 0);
1330     lv_draw_label_dsc_t cur_label_dsc;
1331     lv_draw_label_dsc_init(&cur_label_dsc);
1332     lv_obj_init_draw_label_dsc(obj, LV_PART_CURSOR, &cur_label_dsc);
1333     if(cur_dsc.bg_opa > LV_OPA_MIN || cur_label_dsc.color.full != label_color.full) {
1334         lv_draw_label(draw_ctx, &cur_label_dsc, &cur_area, letter_buf, NULL);
1335     }
1336 }
1337 
auto_hide_characters(lv_obj_t * obj)1338 static void auto_hide_characters(lv_obj_t * obj)
1339 {
1340     lv_textarea_t * ta = (lv_textarea_t *) obj;
1341 
1342     if(ta->pwd_show_time == 0) {
1343         pwd_char_hider(obj);
1344     }
1345     else {
1346         lv_anim_t a;
1347         lv_anim_init(&a);
1348         lv_anim_set_var(&a, ta);
1349         lv_anim_set_exec_cb(&a, pwd_char_hider_anim);
1350         lv_anim_set_time(&a, ta->pwd_show_time);
1351         lv_anim_set_values(&a, 0, 1);
1352         lv_anim_set_path_cb(&a, lv_anim_path_step);
1353         lv_anim_set_ready_cb(&a, pwd_char_hider_anim_ready);
1354         lv_anim_start(&a);
1355     }
1356 }
1357 
is_valid_but_non_printable_char(const uint32_t letter)1358 static inline bool is_valid_but_non_printable_char(const uint32_t letter)
1359 {
1360     if(letter == '\0' || letter == '\n' || letter == '\r') {
1361         return true;
1362     }
1363 
1364     return false;
1365 }
1366 
1367 #endif
1368