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 
927 /**
928  * Called to blink the cursor
929  * @param ta pointer to a text area
930  * @param hide 1: hide the cursor, 0: show it
931  */
cursor_blink_anim_cb(void * obj,int32_t show)932 static void cursor_blink_anim_cb(void * obj, int32_t show)
933 {
934     lv_textarea_t * ta = (lv_textarea_t *)obj;
935     if(show != ta->cursor.show) {
936         ta->cursor.show = show ? 1U : 0U;
937         lv_area_t area_tmp;
938         lv_area_copy(&area_tmp, &ta->cursor.area);
939         area_tmp.x1 += ta->label->coords.x1;
940         area_tmp.y1 += ta->label->coords.y1;
941         area_tmp.x2 += ta->label->coords.x1;
942         area_tmp.y2 += ta->label->coords.y1;
943         lv_obj_invalidate_area(obj, &area_tmp);
944     }
945 }
946 
947 /**
948  * Dummy function to animate char hiding in pwd mode.
949  * Does nothing, but a function is required in car hiding anim.
950  * (pwd_char_hider callback do the real job)
951  * @param ta unused
952  * @param x unused
953  */
pwd_char_hider_anim(void * obj,int32_t x)954 static void pwd_char_hider_anim(void * obj, int32_t x)
955 {
956     LV_UNUSED(obj);
957     LV_UNUSED(x);
958 }
959 
960 /**
961  * Call when an animation is ready to convert all characters to '*'
962  * @param a pointer to the animation
963  */
pwd_char_hider_anim_ready(lv_anim_t * a)964 static void pwd_char_hider_anim_ready(lv_anim_t * a)
965 {
966     lv_obj_t * obj = a->var;
967     pwd_char_hider(obj);
968 }
969 
970 /**
971  * Hide all characters (convert them to '*')
972  * @param ta pointer to text area object
973  */
pwd_char_hider(lv_obj_t * obj)974 static void pwd_char_hider(lv_obj_t * obj)
975 {
976     lv_textarea_t * ta = (lv_textarea_t *)obj;
977     if(ta->pwd_mode == 0) {
978         return;
979     }
980 
981     /* When ta->label is empty we get 0 back */
982     char * txt = lv_label_get_text(ta->label);
983     uint32_t enc_len = _lv_txt_get_encoded_length(txt);
984     if(enc_len == 0) return;
985 
986     const char * bullet = lv_textarea_get_password_bullet(obj);
987     const size_t bullet_len = strlen(bullet);
988     char * txt_tmp = lv_mem_buf_get(enc_len * bullet_len + 1);
989 
990     uint32_t i;
991     for(i = 0; i < enc_len; i++) {
992         lv_memcpy(&txt_tmp[i * bullet_len], bullet, bullet_len);
993     }
994     txt_tmp[i * bullet_len] = '\0';
995 
996     lv_label_set_text(ta->label, txt_tmp);
997     lv_mem_buf_release(txt_tmp);
998 
999     refr_cursor_area(obj);
1000 }
1001 
1002 /**
1003  * Test a unicode character if it is accepted or not. Checks max length and accepted char list.
1004  * @param ta pointer to a test area object
1005  * @param c a unicode character
1006  * @return true: accepted; false: rejected
1007  */
char_is_accepted(lv_obj_t * obj,uint32_t c)1008 static bool char_is_accepted(lv_obj_t * obj, uint32_t c)
1009 {
1010     lv_textarea_t * ta = (lv_textarea_t *)obj;
1011 
1012     /*Too many characters?*/
1013     if(ta->max_length > 0 && _lv_txt_get_encoded_length(lv_textarea_get_text(obj)) >= ta->max_length) {
1014         return false;
1015     }
1016 
1017     if(ta->accepted_chars == NULL || ta->accepted_chars[0] == '\0') return true;
1018     /*Accepted character?*/
1019     uint32_t i = 0;
1020 
1021     while(ta->accepted_chars[i] != '\0') {
1022         uint32_t a = _lv_txt_encoded_next(ta->accepted_chars, &i);
1023         if(a == c) return true; /*Accepted*/
1024     }
1025 
1026     return false; /*The character wasn't in the list*/
1027 }
1028 
start_cursor_blink(lv_obj_t * obj)1029 static void start_cursor_blink(lv_obj_t * obj)
1030 {
1031     lv_textarea_t * ta = (lv_textarea_t *)obj;
1032     uint32_t blink_time = lv_obj_get_style_anim_time(obj, LV_PART_CURSOR);
1033     if(blink_time == 0) {
1034         lv_anim_del(obj, cursor_blink_anim_cb);
1035         ta->cursor.show = 1;
1036     }
1037     else {
1038         lv_anim_t a;
1039         lv_anim_init(&a);
1040         lv_anim_set_var(&a, ta);
1041         lv_anim_set_exec_cb(&a, cursor_blink_anim_cb);
1042         lv_anim_set_time(&a, blink_time);
1043         lv_anim_set_playback_time(&a, blink_time);
1044         lv_anim_set_values(&a, 1, 0);
1045         lv_anim_set_path_cb(&a, lv_anim_path_step);
1046         lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
1047         lv_anim_start(&a);
1048     }
1049 }
1050 
refr_cursor_area(lv_obj_t * obj)1051 static void refr_cursor_area(lv_obj_t * obj)
1052 {
1053     lv_textarea_t * ta = (lv_textarea_t *)obj;
1054 
1055     const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
1056     lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN);
1057 
1058     uint32_t cur_pos = lv_textarea_get_cursor_pos(obj);
1059     const char * txt = lv_label_get_text(ta->label);
1060 
1061     uint32_t byte_pos = _lv_txt_encoded_get_byte_id(txt, cur_pos);
1062     uint32_t letter = _lv_txt_encoded_next(&txt[byte_pos], NULL);
1063 
1064     /* Letter height and width */
1065     const lv_coord_t letter_h = lv_font_get_line_height(font);
1066     /*Set letter_w (set not 0 on non printable but valid chars)*/
1067     uint32_t letter_space = letter;
1068     if(is_valid_but_non_printable_char(letter)) {
1069         letter_space = ' ';
1070     }
1071     lv_coord_t letter_w = lv_font_get_glyph_width(font, letter_space, IGNORE_KERNING);
1072 
1073     lv_point_t letter_pos;
1074     lv_label_get_letter_pos(ta->label, cur_pos, &letter_pos);
1075 
1076     lv_text_align_t align = lv_obj_calculate_style_text_align(ta->label, LV_PART_MAIN, lv_label_get_text(ta->label));
1077 
1078     /*If the cursor is out of the text (most right) draw it to the next line*/
1079     if(((letter_pos.x + ta->label->coords.x1) + letter_w > ta->label->coords.x2) &&
1080        (ta->one_line == 0 && align != LV_TEXT_ALIGN_RIGHT)) {
1081 
1082         letter_pos.x = 0;
1083         letter_pos.y += letter_h + line_space;
1084 
1085         if(letter != '\0') {
1086             byte_pos += _lv_txt_encoded_size(&txt[byte_pos]);
1087             letter = _lv_txt_encoded_next(&txt[byte_pos], NULL);
1088         }
1089 
1090         uint32_t tmp = letter;
1091         if(is_valid_but_non_printable_char(letter)) {
1092             tmp = ' ';
1093         }
1094         letter_w = lv_font_get_glyph_width(font, tmp, IGNORE_KERNING);
1095     }
1096 
1097     /*Save the byte position. It is required to draw `LV_CURSOR_BLOCK`*/
1098     ta->cursor.txt_byte_pos = byte_pos;
1099 
1100     /*Calculate the cursor according to its type*/
1101     lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_CURSOR);
1102     lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_CURSOR) + border_width;
1103     lv_coord_t bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_CURSOR) + border_width;
1104     lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_CURSOR) + border_width;
1105     lv_coord_t right = lv_obj_get_style_pad_right(obj, LV_PART_CURSOR) + border_width;
1106 
1107     lv_area_t cur_area;
1108     cur_area.x1 = letter_pos.x - left;
1109     cur_area.y1 = letter_pos.y - top;
1110     cur_area.x2 = letter_pos.x + right + letter_w - 1;
1111     cur_area.y2 = letter_pos.y + bottom + letter_h - 1;
1112 
1113     /*Save the new area*/
1114     lv_area_t area_tmp;
1115     lv_area_copy(&area_tmp, &ta->cursor.area);
1116     area_tmp.x1 += ta->label->coords.x1;
1117     area_tmp.y1 += ta->label->coords.y1;
1118     area_tmp.x2 += ta->label->coords.x1;
1119     area_tmp.y2 += ta->label->coords.y1;
1120     lv_obj_invalidate_area(obj, &area_tmp);
1121 
1122     lv_area_copy(&ta->cursor.area, &cur_area);
1123 
1124     lv_area_copy(&area_tmp, &ta->cursor.area);
1125     area_tmp.x1 += ta->label->coords.x1;
1126     area_tmp.y1 += ta->label->coords.y1;
1127     area_tmp.x2 += ta->label->coords.x1;
1128     area_tmp.y2 += ta->label->coords.y1;
1129     lv_obj_invalidate_area(obj, &area_tmp);
1130 }
1131 
update_cursor_position_on_click(lv_event_t * e)1132 static void update_cursor_position_on_click(lv_event_t * e)
1133 {
1134     lv_indev_t * click_source = lv_indev_get_act();
1135     if(click_source == NULL) return;
1136 
1137     lv_obj_t * obj = lv_event_get_target(e);
1138     lv_textarea_t * ta = (lv_textarea_t *)obj;
1139     if(ta->cursor.click_pos == 0) return;
1140 
1141     if(lv_indev_get_type(click_source) == LV_INDEV_TYPE_KEYPAD ||
1142        lv_indev_get_type(click_source) == LV_INDEV_TYPE_ENCODER) {
1143         return;
1144     }
1145 
1146     lv_area_t label_coords;
1147     lv_obj_get_coords(ta->label, &label_coords);
1148 
1149     lv_point_t point_act, vect_act;
1150     lv_indev_get_point(click_source, &point_act);
1151     lv_indev_get_vect(click_source, &vect_act);
1152 
1153     if(point_act.x < 0 || point_act.y < 0) return; /*Ignore event from keypad*/
1154     lv_point_t rel_pos;
1155     rel_pos.x = point_act.x - label_coords.x1;
1156     rel_pos.y = point_act.y - label_coords.y1;
1157 
1158     const lv_event_code_t code = lv_event_get_code(e);
1159 
1160     lv_coord_t label_width = lv_obj_get_width(ta->label);
1161     uint16_t char_id_at_click = 0;
1162 
1163 #if LV_LABEL_TEXT_SELECTION
1164     lv_label_t * label_data = (lv_label_t *)ta->label;
1165     bool click_outside_label = false;
1166     /*Check if the click happened on the left side of the area outside the label*/
1167     if(rel_pos.x < 0) {
1168         char_id_at_click = 0;
1169         click_outside_label = true;
1170     }
1171     /*Check if the click happened on the right side of the area outside the label*/
1172     else if(rel_pos.x >= label_width) {
1173         char_id_at_click = LV_TEXTAREA_CURSOR_LAST;
1174         click_outside_label = true;
1175     }
1176     else {
1177         char_id_at_click = lv_label_get_letter_on(ta->label, &rel_pos);
1178         click_outside_label = !lv_label_is_char_under_pos(ta->label, &rel_pos);
1179     }
1180 
1181     if(ta->text_sel_en) {
1182         if(!ta->text_sel_in_prog && !click_outside_label && code == LV_EVENT_PRESSED) {
1183             /*Input device just went down. Store the selection start position*/
1184             ta->sel_start    = char_id_at_click;
1185             ta->sel_end      = LV_LABEL_TEXT_SELECTION_OFF;
1186             ta->text_sel_in_prog = 1;
1187             lv_obj_clear_flag(obj, LV_OBJ_FLAG_SCROLL_CHAIN);
1188         }
1189         else if(ta->text_sel_in_prog && code == LV_EVENT_PRESSING) {
1190             /*Input device may be moving. Store the end position*/
1191             ta->sel_end = char_id_at_click;
1192         }
1193         else if(ta->text_sel_in_prog && (code == LV_EVENT_PRESS_LOST || code == LV_EVENT_RELEASED)) {
1194             /*Input device is released. Check if anything was selected.*/
1195             lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_CHAIN);
1196         }
1197     }
1198 
1199     if(ta->text_sel_in_prog || code == LV_EVENT_PRESSED) lv_textarea_set_cursor_pos(obj, char_id_at_click);
1200 
1201     if(ta->text_sel_in_prog) {
1202         /*If the selected area has changed then update the real values and*/
1203 
1204         /*Invalidate the text area.*/
1205         if(ta->sel_start > ta->sel_end) {
1206             if(label_data->sel_start != ta->sel_end || label_data->sel_end != ta->sel_start) {
1207                 label_data->sel_start = ta->sel_end;
1208                 label_data->sel_end   = ta->sel_start;
1209                 lv_obj_invalidate(obj);
1210             }
1211         }
1212         else if(ta->sel_start < ta->sel_end) {
1213             if(label_data->sel_start != ta->sel_start || label_data->sel_end != ta->sel_end) {
1214                 label_data->sel_start = ta->sel_start;
1215                 label_data->sel_end   = ta->sel_end;
1216                 lv_obj_invalidate(obj);
1217             }
1218         }
1219         else {
1220             if(label_data->sel_start != LV_DRAW_LABEL_NO_TXT_SEL || label_data->sel_end != LV_DRAW_LABEL_NO_TXT_SEL) {
1221                 label_data->sel_start = LV_DRAW_LABEL_NO_TXT_SEL;
1222                 label_data->sel_end   = LV_DRAW_LABEL_NO_TXT_SEL;
1223                 lv_obj_invalidate(obj);
1224             }
1225         }
1226         /*Finish selection if necessary*/
1227         if(code == LV_EVENT_PRESS_LOST || code == LV_EVENT_RELEASED) {
1228             ta->text_sel_in_prog = 0;
1229         }
1230     }
1231 #else
1232     /*Check if the click happened on the left side of the area outside the label*/
1233     if(rel_pos.x < 0) {
1234         char_id_at_click = 0;
1235     }
1236     /*Check if the click happened on the right side of the area outside the label*/
1237     else if(rel_pos.x >= label_width) {
1238         char_id_at_click = LV_TEXTAREA_CURSOR_LAST;
1239     }
1240     else {
1241         char_id_at_click = lv_label_get_letter_on(ta->label, &rel_pos);
1242     }
1243 
1244     if(code == LV_EVENT_PRESSED) lv_textarea_set_cursor_pos(obj, char_id_at_click);
1245 #endif
1246 }
1247 
1248 /* Returns LV_RES_OK when no operation were performed
1249  * Returns LV_RES_INV when a user defined text was inserted */
insert_handler(lv_obj_t * obj,const char * txt)1250 static lv_res_t insert_handler(lv_obj_t * obj, const char * txt)
1251 {
1252     ta_insert_replace = NULL;
1253     lv_event_send(obj, LV_EVENT_INSERT, (char *)txt);
1254 
1255     /* Drop txt if insert replace is set to '\0' */
1256     if(ta_insert_replace && ta_insert_replace[0] == '\0')
1257         return LV_RES_INV;
1258 
1259     if(ta_insert_replace) {
1260         /*Add the replaced text directly it's different from the original*/
1261         if(strcmp(ta_insert_replace, txt)) {
1262             lv_textarea_add_text(obj, ta_insert_replace);
1263             return LV_RES_INV;
1264         }
1265     }
1266 
1267     return LV_RES_OK;
1268 }
1269 
draw_placeholder(lv_event_t * e)1270 static void draw_placeholder(lv_event_t * e)
1271 {
1272     lv_obj_t * obj = lv_event_get_target(e);
1273     lv_textarea_t * ta = (lv_textarea_t *)obj;
1274     lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
1275     const char * txt = lv_label_get_text(ta->label);
1276 
1277     /*Draw the place holder*/
1278     if(txt[0] == '\0' && ta->placeholder_txt && ta->placeholder_txt[0] != 0) {
1279         lv_draw_label_dsc_t ph_dsc;
1280         lv_draw_label_dsc_init(&ph_dsc);
1281         lv_obj_init_draw_label_dsc(obj, LV_PART_TEXTAREA_PLACEHOLDER, &ph_dsc);
1282 
1283         if(ta->one_line) ph_dsc.flag |= LV_TEXT_FLAG_EXPAND;
1284 
1285         lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
1286         lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
1287         lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
1288         lv_area_t ph_coords;
1289         lv_area_copy(&ph_coords, &obj->coords);
1290         lv_area_move(&ph_coords, left + border_width, top + border_width);
1291         lv_draw_label(draw_ctx, &ph_dsc, &ph_coords, ta->placeholder_txt, NULL);
1292     }
1293 }
1294 
draw_cursor(lv_event_t * e)1295 static void draw_cursor(lv_event_t * e)
1296 {
1297     lv_obj_t * obj = lv_event_get_target(e);
1298     lv_textarea_t * ta = (lv_textarea_t *)obj;
1299     lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
1300     const char * txt = lv_label_get_text(ta->label);
1301 
1302     if(ta->cursor.show == 0) return;
1303 
1304     lv_draw_rect_dsc_t cur_dsc;
1305     lv_draw_rect_dsc_init(&cur_dsc);
1306     lv_obj_init_draw_rect_dsc(obj, LV_PART_CURSOR, &cur_dsc);
1307 
1308     /*Draw he cursor according to the type*/
1309     lv_area_t cur_area;
1310     lv_area_copy(&cur_area, &ta->cursor.area);
1311 
1312 
1313     cur_area.x1 += ta->label->coords.x1;
1314     cur_area.y1 += ta->label->coords.y1;
1315     cur_area.x2 += ta->label->coords.x1;
1316     cur_area.y2 += ta->label->coords.y1;
1317 
1318     lv_draw_rect(draw_ctx, &cur_dsc, &cur_area);
1319 
1320     lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_CURSOR);
1321     lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_CURSOR) + border_width;
1322     lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_CURSOR) + border_width;
1323     char letter_buf[8] = {0};
1324     lv_memcpy(letter_buf, &txt[ta->cursor.txt_byte_pos], _lv_txt_encoded_size(&txt[ta->cursor.txt_byte_pos]));
1325 
1326     cur_area.x1 += left;
1327     cur_area.y1 += top;
1328 
1329     /*Draw the letter over the cursor only if
1330      *the cursor has background or the letter has different color than the original.
1331      *Else the original letter is drawn twice which makes it look bolder*/
1332     lv_color_t label_color = lv_obj_get_style_text_color(ta->label, 0);
1333     lv_draw_label_dsc_t cur_label_dsc;
1334     lv_draw_label_dsc_init(&cur_label_dsc);
1335     lv_obj_init_draw_label_dsc(obj, LV_PART_CURSOR, &cur_label_dsc);
1336     if(cur_dsc.bg_opa > LV_OPA_MIN || cur_label_dsc.color.full != label_color.full) {
1337         lv_draw_label(draw_ctx, &cur_label_dsc, &cur_area, letter_buf, NULL);
1338     }
1339 }
1340 
auto_hide_characters(lv_obj_t * obj)1341 static void auto_hide_characters(lv_obj_t * obj)
1342 {
1343     lv_textarea_t * ta = (lv_textarea_t *) obj;
1344 
1345     if(ta->pwd_show_time == 0) {
1346         pwd_char_hider(obj);
1347     }
1348     else {
1349         lv_anim_t a;
1350         lv_anim_init(&a);
1351         lv_anim_set_var(&a, ta);
1352         lv_anim_set_exec_cb(&a, pwd_char_hider_anim);
1353         lv_anim_set_time(&a, ta->pwd_show_time);
1354         lv_anim_set_values(&a, 0, 1);
1355         lv_anim_set_path_cb(&a, lv_anim_path_step);
1356         lv_anim_set_ready_cb(&a, pwd_char_hider_anim_ready);
1357         lv_anim_start(&a);
1358     }
1359 }
1360 
is_valid_but_non_printable_char(const uint32_t letter)1361 static inline bool is_valid_but_non_printable_char(const uint32_t letter)
1362 {
1363     if(letter == '\0' || letter == '\n' || letter == '\r') {
1364         return true;
1365     }
1366 
1367     return false;
1368 }
1369 
1370 #endif
1371