1 /**
2  * @file lv_dropdown.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "../core/lv_obj.h"
10 #include "lv_dropdown.h"
11 #if LV_USE_DROPDOWN != 0
12 
13 #include "../misc/lv_assert.h"
14 #include "../draw/lv_draw.h"
15 #include "../core/lv_group.h"
16 #include "../core/lv_indev.h"
17 #include "../core/lv_disp.h"
18 #include "../font/lv_symbol_def.h"
19 #include "../misc/lv_anim.h"
20 #include "../misc/lv_math.h"
21 #include "../misc/lv_txt_ap.h"
22 #include <string.h>
23 
24 /*********************
25  *      DEFINES
26  *********************/
27 #define MY_CLASS &lv_dropdown_class
28 #define MY_CLASS_LIST &lv_dropdownlist_class
29 
30 #define LV_DROPDOWN_PR_NONE 0xFFFF
31 
32 /**********************
33  *      TYPEDEFS
34  **********************/
35 
36 /**********************
37  *  STATIC PROTOTYPES
38  **********************/
39 static lv_obj_t * lv_dropdown_list_create(lv_obj_t * parent);
40 static void lv_dropdown_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
41 static void lv_dropdown_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
42 static void lv_dropdown_event(const lv_obj_class_t * class_p, lv_event_t * e);
43 static void draw_main(lv_event_t * e);
44 
45 static void lv_dropdownlist_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
46 static void lv_dropdownlist_destructor(const lv_obj_class_t * class_p, lv_obj_t * list_obj);
47 static void lv_dropdown_list_event(const lv_obj_class_t * class_p, lv_event_t * e);
48 static void draw_list(lv_event_t * e);
49 
50 static void draw_box(lv_obj_t * dropdown_obj, lv_draw_ctx_t * draw_ctx, uint16_t id, lv_state_t state);
51 static void draw_box_label(lv_obj_t * dropdown_obj, lv_draw_ctx_t * draw_ctx, uint16_t id, lv_state_t state);
52 static lv_res_t btn_release_handler(lv_obj_t * obj);
53 static lv_res_t list_release_handler(lv_obj_t * list_obj);
54 static void list_press_handler(lv_obj_t * page);
55 static uint16_t get_id_on_point(lv_obj_t * dropdown_obj, lv_coord_t y);
56 static void position_to_selected(lv_obj_t * obj);
57 static lv_obj_t * get_label(const lv_obj_t * obj);
58 
59 /**********************
60  *  STATIC VARIABLES
61  **********************/
62 const lv_obj_class_t lv_dropdown_class = {
63     .constructor_cb = lv_dropdown_constructor,
64     .destructor_cb = lv_dropdown_destructor,
65     .event_cb = lv_dropdown_event,
66     .width_def = LV_DPI_DEF,
67     .height_def = LV_SIZE_CONTENT,
68     .instance_size = sizeof(lv_dropdown_t),
69     .editable = LV_OBJ_CLASS_EDITABLE_TRUE,
70     .group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
71     .base_class = &lv_obj_class
72 };
73 
74 const lv_obj_class_t lv_dropdownlist_class = {
75     .constructor_cb = lv_dropdownlist_constructor,
76     .destructor_cb = lv_dropdownlist_destructor,
77     .event_cb = lv_dropdown_list_event,
78     .instance_size = sizeof(lv_dropdown_list_t),
79     .base_class = &lv_obj_class
80 };
81 
82 
83 /**********************
84  *      MACROS
85  **********************/
86 
87 /**********************
88  *   GLOBAL FUNCTIONS
89  **********************/
90 
lv_dropdown_create(lv_obj_t * parent)91 lv_obj_t * lv_dropdown_create(lv_obj_t * parent)
92 {
93     LV_LOG_INFO("begin");
94     lv_obj_t * obj = lv_obj_class_create_obj(&lv_dropdown_class, parent);
95     lv_obj_class_init_obj(obj);
96     return obj;
97 }
98 
99 /*=====================
100  * Setter functions
101  *====================*/
102 
lv_dropdown_set_text(lv_obj_t * obj,const char * txt)103 void lv_dropdown_set_text(lv_obj_t * obj, const char * txt)
104 {
105     LV_ASSERT_OBJ(obj, MY_CLASS);
106     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
107     if(dropdown->text == txt) return;
108 
109     dropdown->text = txt;
110 
111     lv_obj_invalidate(obj);
112 }
113 
lv_dropdown_set_options(lv_obj_t * obj,const char * options)114 void lv_dropdown_set_options(lv_obj_t * obj, const char * options)
115 {
116     LV_ASSERT_OBJ(obj, MY_CLASS);
117     LV_ASSERT_NULL(options);
118 
119     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
120 
121     /*Count the '\n'-s to determine the number of options*/
122     dropdown->option_cnt = 0;
123     uint32_t i;
124     for(i = 0; options[i] != '\0'; i++) {
125         if(options[i] == '\n') dropdown->option_cnt++;
126     }
127     dropdown->option_cnt++;   /*Last option has no `\n`*/
128     dropdown->sel_opt_id      = 0;
129     dropdown->sel_opt_id_orig = 0;
130 
131     /*Allocate space for the new text*/
132 #if LV_USE_ARABIC_PERSIAN_CHARS == 0
133     size_t len = strlen(options) + 1;
134 #else
135     size_t len = _lv_txt_ap_calc_bytes_cnt(options) + 1;
136 #endif
137 
138     if(dropdown->options != NULL && dropdown->static_txt == 0) {
139         lv_mem_free(dropdown->options);
140         dropdown->options = NULL;
141     }
142 
143     dropdown->options = lv_mem_alloc(len);
144 
145     LV_ASSERT_MALLOC(dropdown->options);
146     if(dropdown->options == NULL) return;
147 
148 #if LV_USE_ARABIC_PERSIAN_CHARS == 0
149     strcpy(dropdown->options, options);
150 #else
151     _lv_txt_ap_proc(options, dropdown->options);
152 #endif
153 
154     /*Now the text is dynamically allocated*/
155     dropdown->static_txt = 0;
156 
157     lv_obj_invalidate(obj);
158     if(dropdown->list) lv_obj_invalidate(dropdown->list);
159 }
160 
lv_dropdown_set_options_static(lv_obj_t * obj,const char * options)161 void lv_dropdown_set_options_static(lv_obj_t * obj, const char * options)
162 {
163     LV_ASSERT_OBJ(obj, MY_CLASS);
164     LV_ASSERT_NULL(options);
165 
166     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
167 
168     /*Count the '\n'-s to determine the number of options*/
169     dropdown->option_cnt = 0;
170     uint32_t i;
171     for(i = 0; options[i] != '\0'; i++) {
172         if(options[i] == '\n') dropdown->option_cnt++;
173     }
174     dropdown->option_cnt++;   /*Last option has no `\n`*/
175     dropdown->sel_opt_id      = 0;
176     dropdown->sel_opt_id_orig = 0;
177 
178     if(dropdown->static_txt == 0 && dropdown->options != NULL) {
179         lv_mem_free(dropdown->options);
180         dropdown->options = NULL;
181     }
182 
183     dropdown->static_txt = 1;
184     dropdown->options = (char *)options;
185 
186     lv_obj_invalidate(obj);
187     if(dropdown->list) lv_obj_invalidate(dropdown->list);
188 }
189 
lv_dropdown_add_option(lv_obj_t * obj,const char * option,uint32_t pos)190 void lv_dropdown_add_option(lv_obj_t * obj, const char * option, uint32_t pos)
191 {
192     LV_ASSERT_OBJ(obj, MY_CLASS);
193     LV_ASSERT_NULL(option);
194 
195     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
196 
197     /*Convert static options to dynamic*/
198     if(dropdown->static_txt != 0) {
199         char * static_options = dropdown->options;
200         size_t len = strlen(static_options) + 1;
201 
202         dropdown->options = lv_mem_alloc(len);
203         LV_ASSERT_MALLOC(dropdown->options);
204         if(dropdown->options == NULL) return;
205 
206         strcpy(dropdown->options, static_options);
207         dropdown->static_txt = 0;
208     }
209 
210     /*Allocate space for the new option*/
211     size_t old_len = (dropdown->options == NULL) ? 0 : strlen(dropdown->options);
212 #if LV_USE_ARABIC_PERSIAN_CHARS == 0
213     size_t ins_len = strlen(option) + 1;
214 #else
215     size_t ins_len = _lv_txt_ap_calc_bytes_cnt(option) + 1;
216 #endif
217 
218     size_t new_len = ins_len + old_len + 2; /*+2 for terminating NULL and possible \n*/
219     dropdown->options        = lv_mem_realloc(dropdown->options, new_len + 1);
220     LV_ASSERT_MALLOC(dropdown->options);
221     if(dropdown->options == NULL) return;
222 
223     dropdown->options[old_len] = '\0';
224 
225     /*Find the insert character position*/
226     uint32_t insert_pos = old_len;
227     if(pos != LV_DROPDOWN_POS_LAST) {
228         uint32_t opcnt = 0;
229         for(insert_pos = 0; dropdown->options[insert_pos] != 0; insert_pos++) {
230             if(opcnt == pos)
231                 break;
232             if(dropdown->options[insert_pos] == '\n')
233                 opcnt++;
234         }
235     }
236 
237     /*Add delimiter to existing options*/
238     if((insert_pos > 0) && (pos >= dropdown->option_cnt))
239         _lv_txt_ins(dropdown->options, _lv_txt_encoded_get_char_id(dropdown->options, insert_pos++), "\n");
240 
241     /*Insert the new option, adding \n if necessary*/
242     char * ins_buf = lv_mem_buf_get(ins_len + 2); /*+ 2 for terminating NULL and possible \n*/
243     LV_ASSERT_MALLOC(ins_buf);
244     if(ins_buf == NULL) return;
245 #if LV_USE_ARABIC_PERSIAN_CHARS == 0
246     strcpy(ins_buf, option);
247 #else
248     _lv_txt_ap_proc(option, ins_buf);
249 #endif
250     if(pos < dropdown->option_cnt) strcat(ins_buf, "\n");
251 
252     _lv_txt_ins(dropdown->options, _lv_txt_encoded_get_char_id(dropdown->options, insert_pos), ins_buf);
253     lv_mem_buf_release(ins_buf);
254 
255     dropdown->option_cnt++;
256 
257     lv_obj_invalidate(obj);
258     if(dropdown->list) lv_obj_invalidate(dropdown->list);
259 }
260 
lv_dropdown_clear_options(lv_obj_t * obj)261 void lv_dropdown_clear_options(lv_obj_t * obj)
262 {
263     LV_ASSERT_OBJ(obj, MY_CLASS);
264     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
265     if(dropdown->options == NULL) return;
266 
267     if(dropdown->static_txt == 0)
268         lv_mem_free(dropdown->options);
269 
270     dropdown->options = NULL;
271     dropdown->static_txt = 0;
272     dropdown->option_cnt = 0;
273 
274     lv_obj_invalidate(obj);
275     if(dropdown->list) lv_obj_invalidate(dropdown->list);
276 }
277 
lv_dropdown_set_selected(lv_obj_t * obj,uint16_t sel_opt)278 void lv_dropdown_set_selected(lv_obj_t * obj, uint16_t sel_opt)
279 {
280     LV_ASSERT_OBJ(obj, MY_CLASS);
281 
282     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
283     if(dropdown->sel_opt_id == sel_opt) return;
284 
285     dropdown->sel_opt_id      = sel_opt < dropdown->option_cnt ? sel_opt : dropdown->option_cnt - 1;
286     dropdown->sel_opt_id_orig = dropdown->sel_opt_id;
287 
288     if(dropdown->list) {
289         position_to_selected(obj);
290     }
291 
292     lv_obj_invalidate(obj);
293 }
294 
lv_dropdown_set_dir(lv_obj_t * obj,lv_dir_t dir)295 void lv_dropdown_set_dir(lv_obj_t * obj, lv_dir_t dir)
296 {
297     LV_ASSERT_OBJ(obj, MY_CLASS);
298 
299     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
300     if(dropdown->dir == dir) return;
301 
302     dropdown->dir = dir;
303 
304     lv_obj_invalidate(obj);
305 }
306 
lv_dropdown_set_symbol(lv_obj_t * obj,const void * symbol)307 void lv_dropdown_set_symbol(lv_obj_t * obj, const void * symbol)
308 {
309     LV_ASSERT_OBJ(obj, MY_CLASS);
310 
311     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
312     dropdown->symbol = symbol;
313     lv_obj_invalidate(obj);
314 }
315 
lv_dropdown_set_selected_highlight(lv_obj_t * obj,bool en)316 void lv_dropdown_set_selected_highlight(lv_obj_t * obj, bool en)
317 {
318     LV_ASSERT_OBJ(obj, MY_CLASS);
319 
320     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
321     dropdown->selected_highlight = en;
322     if(dropdown->list) lv_obj_invalidate(dropdown->list);
323 }
324 
325 /*=====================
326  * Getter functions
327  *====================*/
328 
lv_dropdown_get_list(lv_obj_t * obj)329 lv_obj_t * lv_dropdown_get_list(lv_obj_t * obj)
330 {
331     LV_ASSERT_OBJ(obj, MY_CLASS);
332     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
333 
334     return dropdown->list;
335 }
336 
lv_dropdown_get_text(lv_obj_t * obj)337 const char * lv_dropdown_get_text(lv_obj_t * obj)
338 {
339     LV_ASSERT_OBJ(obj, MY_CLASS);
340     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
341 
342     return dropdown->text;
343 }
344 
lv_dropdown_get_options(const lv_obj_t * obj)345 const char * lv_dropdown_get_options(const lv_obj_t * obj)
346 {
347     LV_ASSERT_OBJ(obj, MY_CLASS);
348 
349     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
350     return dropdown->options == NULL ? "" : dropdown->options;
351 }
352 
lv_dropdown_get_selected(const lv_obj_t * obj)353 uint16_t lv_dropdown_get_selected(const lv_obj_t * obj)
354 {
355     LV_ASSERT_OBJ(obj, MY_CLASS);
356 
357     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
358 
359     return dropdown->sel_opt_id;
360 }
361 
lv_dropdown_get_option_cnt(const lv_obj_t * obj)362 uint16_t lv_dropdown_get_option_cnt(const lv_obj_t * obj)
363 {
364     LV_ASSERT_OBJ(obj, MY_CLASS);
365 
366     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
367 
368     return dropdown->option_cnt;
369 }
370 
lv_dropdown_get_selected_str(const lv_obj_t * obj,char * buf,uint32_t buf_size)371 void lv_dropdown_get_selected_str(const lv_obj_t * obj, char * buf, uint32_t buf_size)
372 {
373     LV_ASSERT_OBJ(obj, MY_CLASS);
374 
375     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
376 
377     uint32_t i;
378     uint32_t line        = 0;
379     size_t txt_len;
380 
381     if(dropdown->options)  {
382         txt_len     = strlen(dropdown->options);
383     }
384     else {
385         buf[0] = '\0';
386         return;
387     }
388 
389     for(i = 0; i < txt_len && line != dropdown->sel_opt_id_orig; i++) {
390         if(dropdown->options[i] == '\n') line++;
391     }
392 
393     uint32_t c;
394     for(c = 0; i < txt_len && dropdown->options[i] != '\n'; c++, i++) {
395         if(buf_size && c >= buf_size - 1) {
396             LV_LOG_WARN("lv_dropdown_get_selected_str: the buffer was too small");
397             break;
398         }
399         buf[c] = dropdown->options[i];
400     }
401 
402     buf[c] = '\0';
403 }
404 
lv_dropdown_get_option_index(lv_obj_t * obj,const char * option)405 int32_t lv_dropdown_get_option_index(lv_obj_t * obj, const char * option)
406 {
407     const char * opts = lv_dropdown_get_options(obj);
408     uint32_t char_i = 0;
409     uint32_t opt_i = 0;
410     uint32_t option_len = strlen(option);
411     const char * start = opts;
412 
413     while(start[char_i] != '\0') {
414         for(char_i = 0; (start[char_i] != '\n') && (start[char_i] != '\0'); char_i++);
415 
416         if(option_len == char_i && memcmp(start, option, LV_MIN(option_len, char_i)) == 0) {
417             return opt_i;
418         }
419 
420         start = &start[char_i];
421         if(start[0] == '\n') start++;
422         char_i = 0;
423         opt_i++;
424     }
425 
426     return -1;
427 }
428 
429 
lv_dropdown_get_symbol(lv_obj_t * obj)430 const char * lv_dropdown_get_symbol(lv_obj_t * obj)
431 {
432     LV_ASSERT_OBJ(obj, MY_CLASS);
433     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
434     return dropdown->symbol;
435 }
436 
lv_dropdown_get_selected_highlight(lv_obj_t * obj)437 bool lv_dropdown_get_selected_highlight(lv_obj_t * obj)
438 {
439     LV_ASSERT_OBJ(obj, MY_CLASS);
440     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
441     return dropdown->selected_highlight;
442 }
443 
lv_dropdown_get_dir(const lv_obj_t * obj)444 lv_dir_t lv_dropdown_get_dir(const lv_obj_t * obj)
445 {
446     LV_ASSERT_OBJ(obj, MY_CLASS);
447     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
448     return dropdown->dir;
449 }
450 
451 /*=====================
452  * Other functions
453  *====================*/
454 
lv_dropdown_open(lv_obj_t * dropdown_obj)455 void lv_dropdown_open(lv_obj_t * dropdown_obj)
456 {
457     LV_ASSERT_OBJ(dropdown_obj, MY_CLASS);
458 
459     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
460 
461     lv_obj_add_state(dropdown_obj, LV_STATE_CHECKED);
462     lv_obj_set_parent(dropdown->list, lv_obj_get_screen(dropdown_obj));
463     lv_obj_move_to_index(dropdown->list, -1);
464     lv_obj_clear_flag(dropdown->list, LV_OBJ_FLAG_HIDDEN);
465 
466     /*To allow styling the list*/
467     lv_event_send(dropdown_obj, LV_EVENT_READY, NULL);
468 
469     lv_obj_t * label = get_label(dropdown_obj);
470     lv_label_set_text_static(label, dropdown->options);
471     lv_obj_set_width(dropdown->list, LV_SIZE_CONTENT);
472 
473     lv_obj_update_layout(label);
474     /*Set smaller width to the width of the button*/
475     if(lv_obj_get_width(dropdown->list) <= lv_obj_get_width(dropdown_obj) &&
476        (dropdown->dir == LV_DIR_TOP || dropdown->dir == LV_DIR_BOTTOM)) {
477         lv_obj_set_width(dropdown->list, lv_obj_get_width(dropdown_obj));
478     }
479 
480     lv_coord_t label_h = lv_obj_get_height(label);
481     lv_coord_t border_width = lv_obj_get_style_border_width(dropdown->list, LV_PART_MAIN);
482     lv_coord_t top = lv_obj_get_style_pad_top(dropdown->list, LV_PART_MAIN) + border_width;
483     lv_coord_t bottom = lv_obj_get_style_pad_bottom(dropdown->list, LV_PART_MAIN) + border_width;
484 
485     lv_coord_t list_fit_h = label_h + top + bottom;
486     lv_coord_t list_h = list_fit_h;
487 
488     lv_dir_t dir = dropdown->dir;
489     /*No space on the bottom? See if top is better.*/
490     if(dropdown->dir == LV_DIR_BOTTOM) {
491         if(dropdown_obj->coords.y2 + list_h > LV_VER_RES) {
492             if(dropdown_obj->coords.y1 > LV_VER_RES - dropdown_obj->coords.y2) {
493                 /*There is more space on the top, so make it drop up*/
494                 dir = LV_DIR_TOP;
495                 list_h = dropdown_obj->coords.y1 - 1;
496             }
497             else {
498                 list_h = LV_VER_RES - dropdown_obj->coords.y2 - 1 ;
499             }
500         }
501     }
502     /*No space on the top? See if bottom is better.*/
503     else if(dropdown->dir == LV_DIR_TOP) {
504         if(dropdown_obj->coords.y1 - list_h < 0) {
505             if(dropdown_obj->coords.y1 < LV_VER_RES - dropdown_obj->coords.y2) {
506                 /*There is more space on the top, so make it drop up*/
507                 dir = LV_DIR_BOTTOM;
508                 list_h = LV_VER_RES - dropdown_obj->coords.y2;
509             }
510             else {
511                 list_h = dropdown_obj->coords.y1;
512             }
513         }
514     }
515 
516     if(list_h > list_fit_h) list_h = list_fit_h;
517     lv_obj_set_height(dropdown->list, list_h);
518 
519     position_to_selected(dropdown_obj);
520 
521     if(dir == LV_DIR_BOTTOM)     lv_obj_align_to(dropdown->list, dropdown_obj, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
522     else if(dir == LV_DIR_TOP)   lv_obj_align_to(dropdown->list, dropdown_obj, LV_ALIGN_OUT_TOP_LEFT, 0, 0);
523     else if(dir == LV_DIR_LEFT)  lv_obj_align_to(dropdown->list, dropdown_obj, LV_ALIGN_OUT_LEFT_TOP, 0, 0);
524     else if(dir == LV_DIR_RIGHT) lv_obj_align_to(dropdown->list, dropdown_obj, LV_ALIGN_OUT_RIGHT_TOP, 0, 0);
525 
526     lv_obj_update_layout(dropdown->list);
527 
528     if(dropdown->dir == LV_DIR_LEFT || dropdown->dir == LV_DIR_RIGHT) {
529         lv_coord_t y1 = lv_obj_get_y(dropdown->list);
530         lv_coord_t y2 = lv_obj_get_y2(dropdown->list);
531         if(y2 >= LV_VER_RES) {
532             lv_obj_set_y(dropdown->list, y1 - (y2 - LV_VER_RES) - 1);
533         }
534     }
535 
536     lv_text_align_t align = lv_obj_calculate_style_text_align(label, LV_PART_MAIN, dropdown->options);
537 
538     switch(align) {
539         default:
540         case LV_TEXT_ALIGN_LEFT:
541             lv_obj_align(label, LV_ALIGN_TOP_LEFT, 0, 0);
542             break;
543         case LV_TEXT_ALIGN_RIGHT:
544             lv_obj_align(label, LV_ALIGN_TOP_RIGHT, 0, 0);
545             break;
546         case LV_TEXT_ALIGN_CENTER:
547             lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 0);
548             break;
549 
550     }
551 }
552 
lv_dropdown_close(lv_obj_t * obj)553 void lv_dropdown_close(lv_obj_t * obj)
554 {
555     LV_ASSERT_OBJ(obj, MY_CLASS);
556 
557     lv_obj_clear_state(obj, LV_STATE_CHECKED);
558     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
559 
560     dropdown->pr_opt_id = LV_DROPDOWN_PR_NONE;
561     lv_obj_add_flag(dropdown->list, LV_OBJ_FLAG_HIDDEN);
562 
563     lv_event_send(obj, LV_EVENT_CANCEL, NULL);
564 }
565 
lv_dropdown_is_open(lv_obj_t * obj)566 bool lv_dropdown_is_open(lv_obj_t * obj)
567 {
568     LV_ASSERT_OBJ(obj, MY_CLASS);
569     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
570 
571     return lv_obj_has_flag(dropdown->list, LV_OBJ_FLAG_HIDDEN) ? false : true;
572 }
573 
574 /**********************
575  *   STATIC FUNCTIONS
576  **********************/
577 
lv_dropdown_list_create(lv_obj_t * parent)578 static lv_obj_t * lv_dropdown_list_create(lv_obj_t * parent)
579 {
580     LV_LOG_INFO("begin");
581     lv_obj_t * obj = lv_obj_class_create_obj(&lv_dropdownlist_class, parent);
582     lv_obj_class_init_obj(obj);
583     return obj;
584 }
585 
lv_dropdown_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)586 static void lv_dropdown_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
587 {
588     LV_UNUSED(class_p);
589     LV_TRACE_OBJ_CREATE("begin");
590 
591     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
592 
593     /*Initialize the allocated 'ext'*/
594     dropdown->list          = NULL;
595     dropdown->options     = NULL;
596     dropdown->symbol         = LV_SYMBOL_DOWN;
597     dropdown->text         = NULL;
598     dropdown->static_txt = 1;
599     dropdown->selected_highlight = 1;
600     dropdown->sel_opt_id      = 0;
601     dropdown->sel_opt_id_orig = 0;
602     dropdown->pr_opt_id = LV_DROPDOWN_PR_NONE;
603     dropdown->option_cnt      = 0;
604     dropdown->dir = LV_DIR_BOTTOM;
605 
606     lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
607     lv_dropdown_set_options_static(obj, "Option 1\nOption 2\nOption 3");
608 
609     dropdown->list = lv_dropdown_list_create(lv_obj_get_screen(obj));
610     lv_dropdown_list_t * list = (lv_dropdown_list_t *)dropdown->list;
611     list->dropdown = obj;
612 
613     LV_TRACE_OBJ_CREATE("finished");
614 }
615 
lv_dropdown_destructor(const lv_obj_class_t * class_p,lv_obj_t * obj)616 static void lv_dropdown_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
617 {
618     LV_UNUSED(class_p);
619     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
620 
621     if(dropdown->list) {
622         lv_obj_del(dropdown->list);
623         dropdown->list = NULL;
624     }
625 
626     if(!dropdown->static_txt) {
627         lv_mem_free(dropdown->options);
628         dropdown->options = NULL;
629     }
630 }
631 
lv_dropdownlist_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)632 static void lv_dropdownlist_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
633 {
634     LV_UNUSED(class_p);
635     LV_TRACE_OBJ_CREATE("begin");
636 
637     lv_obj_clear_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
638     lv_obj_clear_flag(obj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
639     lv_obj_add_flag(obj, LV_OBJ_FLAG_IGNORE_LAYOUT);
640     lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
641 
642     lv_label_create(obj);
643 
644     LV_TRACE_OBJ_CREATE("finished");
645 }
646 
lv_dropdownlist_destructor(const lv_obj_class_t * class_p,lv_obj_t * list_obj)647 static void lv_dropdownlist_destructor(const lv_obj_class_t * class_p, lv_obj_t * list_obj)
648 {
649     LV_UNUSED(class_p);
650     lv_dropdown_list_t * list = (lv_dropdown_list_t *)list_obj;
651     lv_obj_t * dropdown_obj = list->dropdown;
652     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
653     dropdown->list = NULL;
654 }
655 
lv_dropdown_event(const lv_obj_class_t * class_p,lv_event_t * e)656 static void lv_dropdown_event(const lv_obj_class_t * class_p, lv_event_t * e)
657 {
658     LV_UNUSED(class_p);
659 
660     lv_res_t res;
661 
662     /*Call the ancestor's event handler*/
663     res = lv_obj_event_base(MY_CLASS, e);
664     if(res != LV_RES_OK) return;
665 
666     lv_event_code_t code = lv_event_get_code(e);
667     lv_obj_t * obj = lv_event_get_target(e);
668     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
669 
670     if(code == LV_EVENT_FOCUSED) {
671         lv_group_t * g             = lv_obj_get_group(obj);
672         bool editing               = lv_group_get_editing(g);
673         lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
674 
675         /*Encoders need special handling*/
676         if(indev_type == LV_INDEV_TYPE_ENCODER) {
677             /*Open the list if editing*/
678             if(editing) {
679                 lv_dropdown_open(obj);
680             }
681             /*Close the list if navigating*/
682             else {
683                 dropdown->sel_opt_id = dropdown->sel_opt_id_orig;
684                 lv_dropdown_close(obj);
685             }
686         }
687     }
688     else if(code == LV_EVENT_DEFOCUSED || code == LV_EVENT_LEAVE) {
689         lv_dropdown_close(obj);
690     }
691     else if(code == LV_EVENT_RELEASED) {
692         res = btn_release_handler(obj);
693         if(res != LV_RES_OK) return;
694     }
695     else if(code == LV_EVENT_STYLE_CHANGED) {
696         lv_obj_refresh_self_size(obj);
697     }
698     else if(code == LV_EVENT_SIZE_CHANGED) {
699         lv_obj_refresh_self_size(obj);
700     }
701     else if(code == LV_EVENT_GET_SELF_SIZE) {
702         lv_point_t * p = lv_event_get_param(e);
703         const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
704         p->y = lv_font_get_line_height(font);
705     }
706     else if(code == LV_EVENT_KEY) {
707         char c = *((char *)lv_event_get_param(e));
708         if(c == LV_KEY_RIGHT || c == LV_KEY_DOWN) {
709             if(!lv_dropdown_is_open(obj)) {
710                 lv_dropdown_open(obj);
711             }
712             else if(dropdown->sel_opt_id + 1 < dropdown->option_cnt) {
713                 dropdown->sel_opt_id++;
714                 position_to_selected(obj);
715             }
716         }
717         else if(c == LV_KEY_LEFT || c == LV_KEY_UP) {
718 
719             if(!lv_dropdown_is_open(obj)) {
720                 lv_dropdown_open(obj);
721             }
722             else if(dropdown->sel_opt_id > 0) {
723                 dropdown->sel_opt_id--;
724                 position_to_selected(obj);
725             }
726         }
727         else if(c == LV_KEY_ESC) {
728             dropdown->sel_opt_id = dropdown->sel_opt_id_orig;
729             lv_dropdown_close(obj);
730         }
731         else if(c == LV_KEY_ENTER) {
732             /* Handle the ENTER key only if it was send by an other object.
733              * Do no process it if ENTER is sent by the dropdown because it's handled in LV_EVENT_RELEASED */
734             lv_obj_t * indev_obj = lv_indev_get_obj_act();
735             if(indev_obj != obj) {
736                 res = btn_release_handler(obj);
737                 if(res != LV_RES_OK) return;
738             }
739         }
740     }
741     else if(code == LV_EVENT_DRAW_MAIN) {
742         draw_main(e);
743     }
744 }
745 
lv_dropdown_list_event(const lv_obj_class_t * class_p,lv_event_t * e)746 static void lv_dropdown_list_event(const lv_obj_class_t * class_p, lv_event_t * e)
747 {
748     LV_UNUSED(class_p);
749 
750     lv_res_t res;
751 
752     /*Call the ancestor's event handler*/
753     lv_event_code_t code = lv_event_get_code(e);
754     if(code != LV_EVENT_DRAW_POST) {
755         res = lv_obj_event_base(MY_CLASS_LIST, e);
756         if(res != LV_RES_OK) return;
757     }
758     lv_obj_t * list = lv_event_get_target(e);
759     lv_obj_t * dropdown_obj = ((lv_dropdown_list_t *)list)->dropdown;
760     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
761 
762     if(code == LV_EVENT_RELEASED) {
763         if(lv_indev_get_scroll_obj(lv_indev_get_act()) == NULL) {
764             list_release_handler(list);
765         }
766     }
767     else if(code == LV_EVENT_PRESSED) {
768         list_press_handler(list);
769     }
770     else if(code == LV_EVENT_SCROLL_BEGIN) {
771         dropdown->pr_opt_id = LV_DROPDOWN_PR_NONE;
772         lv_obj_invalidate(list);
773     }
774     else if(code == LV_EVENT_DRAW_POST) {
775         draw_list(e);
776         res = lv_obj_event_base(MY_CLASS_LIST, e);
777         if(res != LV_RES_OK) return;
778     }
779 }
780 
781 
draw_main(lv_event_t * e)782 static void draw_main(lv_event_t * e)
783 {
784     lv_obj_t * obj = lv_event_get_target(e);
785     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
786     lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
787 
788     lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
789     lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN) + border_width;
790     lv_coord_t right = lv_obj_get_style_pad_right(obj, LV_PART_MAIN) + border_width;
791     lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN) + border_width;
792 
793     lv_draw_label_dsc_t symbol_dsc;
794     lv_draw_label_dsc_init(&symbol_dsc);
795     lv_obj_init_draw_label_dsc(obj, LV_PART_INDICATOR, &symbol_dsc);
796 
797     /*If no text specified use the selected option*/
798     const char * opt_txt;
799     if(dropdown->text) opt_txt = dropdown->text;
800     else {
801         char * buf = lv_mem_buf_get(128);
802         lv_dropdown_get_selected_str(obj, buf, 128);
803         opt_txt = buf;
804     }
805 
806     bool symbol_to_left = false;
807     if(dropdown->dir == LV_DIR_LEFT) symbol_to_left = true;
808     if(lv_obj_get_style_base_dir(obj, LV_PART_MAIN) == LV_BASE_DIR_RTL) symbol_to_left = true;
809 
810     if(dropdown->symbol) {
811         lv_img_src_t symbol_type = lv_img_src_get_type(dropdown->symbol);
812         lv_coord_t symbol_w;
813         lv_coord_t symbol_h;
814         if(symbol_type == LV_IMG_SRC_SYMBOL) {
815             lv_point_t size;
816             lv_txt_get_size(&size, dropdown->symbol, symbol_dsc.font, symbol_dsc.letter_space, symbol_dsc.line_space, LV_COORD_MAX,
817                             symbol_dsc.flag);
818             symbol_w = size.x;
819             symbol_h = size.y;
820         }
821         else {
822             lv_img_header_t header;
823             lv_res_t res = lv_img_decoder_get_info(dropdown->symbol, &header);
824             if(res == LV_RES_OK) {
825                 symbol_w = header.w;
826                 symbol_h = header.h;
827             }
828             else {
829                 symbol_w = -1;
830                 symbol_h = -1;
831             }
832         }
833 
834         lv_area_t symbol_area;
835         if(symbol_to_left) {
836             symbol_area.x1 = obj->coords.x1 + left;
837             symbol_area.x2 = symbol_area.x1 + symbol_w - 1;
838         }
839         else {
840             symbol_area.x1 = obj->coords.x2 - right - symbol_w;
841             symbol_area.x2 = symbol_area.x1 + symbol_w - 1;
842         }
843 
844         if(symbol_type == LV_IMG_SRC_SYMBOL) {
845             symbol_area.y1 = obj->coords.y1 + top;
846             symbol_area.y2 = symbol_area.y1 + symbol_h - 1;
847             lv_draw_label(draw_ctx, &symbol_dsc, &symbol_area, dropdown->symbol, NULL);
848         }
849         else {
850             symbol_area.y1 = obj->coords.y1 + (lv_obj_get_height(obj) - symbol_h) / 2;
851             symbol_area.y2 = symbol_area.y1 + symbol_h - 1;
852             lv_draw_img_dsc_t img_dsc;
853             lv_draw_img_dsc_init(&img_dsc);
854             lv_obj_init_draw_img_dsc(obj, LV_PART_INDICATOR, &img_dsc);
855             img_dsc.pivot.x = symbol_w / 2;
856             img_dsc.pivot.y = symbol_h / 2;
857             img_dsc.angle = lv_obj_get_style_transform_angle(obj, LV_PART_INDICATOR);
858             lv_draw_img(draw_ctx, &img_dsc, &symbol_area, dropdown->symbol);
859         }
860     }
861 
862     lv_draw_label_dsc_t label_dsc;
863     lv_draw_label_dsc_init(&label_dsc);
864     lv_obj_init_draw_label_dsc(obj, LV_PART_MAIN, &label_dsc);
865 
866     lv_point_t size;
867     lv_txt_get_size(&size, opt_txt, label_dsc.font, label_dsc.letter_space, label_dsc.line_space, LV_COORD_MAX,
868                     label_dsc.flag);
869 
870     lv_area_t txt_area;
871     txt_area.y1 = obj->coords.y1 + top;
872     txt_area.y2 = txt_area.y1 + size.y;
873     /*Center align the text if no symbol*/
874     if(dropdown->symbol == NULL) {
875         txt_area.x1 = obj->coords.x1 + (lv_obj_get_width(obj) - size.x) / 2;
876         txt_area.x2 = txt_area.x1 + size.x;
877     }
878     else {
879         /*Text to the right*/
880         if(symbol_to_left) {
881             txt_area.x1 = obj->coords.x2 - right - size.x;
882             txt_area.x2 = txt_area.x1 + size.x;
883         }
884         else {
885             txt_area.x1 = obj->coords.x1 + left;
886             txt_area.x2 = txt_area.x1 + size.x;
887         }
888     }
889     lv_draw_label(draw_ctx, &label_dsc, &txt_area, opt_txt, NULL);
890 
891     if(dropdown->text == NULL) {
892         lv_mem_buf_release((char *)opt_txt);
893     }
894 }
895 
draw_list(lv_event_t * e)896 static void draw_list(lv_event_t * e)
897 {
898     lv_obj_t * list_obj = lv_event_get_target(e);
899     lv_dropdown_list_t * list = (lv_dropdown_list_t *)list_obj;
900     lv_obj_t * dropdown_obj = list->dropdown;
901     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
902     lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
903 
904     /* Clip area might be too large too to shadow but
905      * the selected option can be drawn on only the background*/
906     lv_area_t clip_area_core;
907     bool has_common;
908     has_common = _lv_area_intersect(&clip_area_core, draw_ctx->clip_area, &dropdown->list->coords);
909     if(has_common) {
910         const lv_area_t * clip_area_ori = draw_ctx->clip_area;
911         draw_ctx->clip_area = &clip_area_core;
912         if(dropdown->selected_highlight) {
913             if(dropdown->pr_opt_id == dropdown->sel_opt_id) {
914                 draw_box(dropdown_obj, draw_ctx, dropdown->pr_opt_id, LV_STATE_CHECKED | LV_STATE_PRESSED);
915                 draw_box_label(dropdown_obj, draw_ctx, dropdown->pr_opt_id, LV_STATE_CHECKED | LV_STATE_PRESSED);
916             }
917             else {
918                 draw_box(dropdown_obj, draw_ctx, dropdown->pr_opt_id, LV_STATE_PRESSED);
919                 draw_box_label(dropdown_obj, draw_ctx, dropdown->pr_opt_id, LV_STATE_PRESSED);
920                 draw_box(dropdown_obj, draw_ctx, dropdown->sel_opt_id, LV_STATE_CHECKED);
921                 draw_box_label(dropdown_obj, draw_ctx, dropdown->sel_opt_id, LV_STATE_CHECKED);
922             }
923         }
924         else {
925             draw_box(dropdown_obj, draw_ctx, dropdown->pr_opt_id, LV_STATE_PRESSED);
926             draw_box_label(dropdown_obj, draw_ctx, dropdown->pr_opt_id, LV_STATE_PRESSED);
927         }
928         draw_ctx->clip_area = clip_area_ori;
929     }
930 }
931 
draw_box(lv_obj_t * dropdown_obj,lv_draw_ctx_t * draw_ctx,uint16_t id,lv_state_t state)932 static void draw_box(lv_obj_t * dropdown_obj, lv_draw_ctx_t * draw_ctx, uint16_t id, lv_state_t state)
933 {
934     if(id == LV_DROPDOWN_PR_NONE) return;
935 
936     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
937     lv_obj_t * list_obj = dropdown->list;
938     lv_state_t state_ori = list_obj->state;
939 
940     if(state != list_obj->state) {
941         list_obj->state = state;
942         list_obj->skip_trans = 1;
943     }
944 
945     /*Draw a rectangle under the selected item*/
946     const lv_font_t * font    = lv_obj_get_style_text_font(list_obj, LV_PART_SELECTED);
947     lv_coord_t line_space = lv_obj_get_style_text_line_space(list_obj,  LV_PART_SELECTED);
948     lv_coord_t font_h         = lv_font_get_line_height(font);
949 
950     /*Draw the selected*/
951     lv_obj_t * label = get_label(dropdown_obj);
952     lv_area_t rect_area;
953     rect_area.y1 = label->coords.y1;
954     rect_area.y1 += id * (font_h + line_space);
955     rect_area.y1 -= line_space / 2;
956 
957     rect_area.y2 = rect_area.y1 + font_h + line_space - 1;
958     rect_area.x1 = dropdown->list->coords.x1;
959     rect_area.x2 = dropdown->list->coords.x2;
960 
961     lv_draw_rect_dsc_t sel_rect;
962     lv_draw_rect_dsc_init(&sel_rect);
963     lv_obj_init_draw_rect_dsc(list_obj,  LV_PART_SELECTED, &sel_rect);
964     lv_draw_rect(draw_ctx, &sel_rect, &rect_area);
965 
966     list_obj->state = state_ori;
967     list_obj->skip_trans = 0;
968 }
969 
draw_box_label(lv_obj_t * dropdown_obj,lv_draw_ctx_t * draw_ctx,uint16_t id,lv_state_t state)970 static void draw_box_label(lv_obj_t * dropdown_obj, lv_draw_ctx_t * draw_ctx, uint16_t id, lv_state_t state)
971 {
972     if(id == LV_DROPDOWN_PR_NONE) return;
973 
974     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
975     lv_obj_t * list_obj = dropdown->list;
976     lv_state_t state_orig = list_obj->state;
977 
978     if(state != list_obj->state) {
979         list_obj->state = state;
980         list_obj->skip_trans = 1;
981     }
982 
983     lv_draw_label_dsc_t label_dsc;
984     lv_draw_label_dsc_init(&label_dsc);
985     lv_obj_init_draw_label_dsc(list_obj, LV_PART_SELECTED, &label_dsc);
986 
987     label_dsc.line_space = lv_obj_get_style_text_line_space(list_obj,
988                                                             LV_PART_SELECTED);  /*Line space should come from the list*/
989 
990     lv_obj_t * label = get_label(dropdown_obj);
991     if(label == NULL) return;
992 
993     lv_coord_t font_h        = lv_font_get_line_height(label_dsc.font);
994 
995     lv_area_t area_sel;
996     area_sel.y1 = label->coords.y1;
997     area_sel.y1 += id * (font_h + label_dsc.line_space);
998     area_sel.y1 -= label_dsc.line_space / 2;
999 
1000     area_sel.y2 = area_sel.y1 + font_h + label_dsc.line_space - 1;
1001     area_sel.x1 = list_obj->coords.x1;
1002     area_sel.x2 = list_obj->coords.x2;
1003     lv_area_t mask_sel;
1004     bool area_ok;
1005     area_ok = _lv_area_intersect(&mask_sel, draw_ctx->clip_area, &area_sel);
1006     if(area_ok) {
1007         const lv_area_t * clip_area_ori = draw_ctx->clip_area;
1008         draw_ctx->clip_area = &mask_sel;
1009         lv_draw_label(draw_ctx, &label_dsc, &label->coords, lv_label_get_text(label), NULL);
1010         draw_ctx->clip_area = clip_area_ori;
1011     }
1012     list_obj->state = state_orig;
1013     list_obj->skip_trans = 0;
1014 }
1015 
1016 
btn_release_handler(lv_obj_t * obj)1017 static lv_res_t btn_release_handler(lv_obj_t * obj)
1018 {
1019     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
1020     lv_indev_t * indev = lv_indev_get_act();
1021     if(lv_indev_get_scroll_obj(indev) == NULL) {
1022         if(lv_dropdown_is_open(obj)) {
1023             lv_dropdown_close(obj);
1024             if(dropdown->sel_opt_id_orig != dropdown->sel_opt_id) {
1025                 dropdown->sel_opt_id_orig = dropdown->sel_opt_id;
1026                 lv_res_t res;
1027                 uint32_t id  = dropdown->sel_opt_id; /*Just to use uint32_t in event data*/
1028                 res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, &id);
1029                 if(res != LV_RES_OK) return res;
1030                 lv_obj_invalidate(obj);
1031             }
1032             lv_indev_type_t indev_type = lv_indev_get_type(indev);
1033             if(indev_type == LV_INDEV_TYPE_ENCODER) {
1034                 lv_group_set_editing(lv_obj_get_group(obj), false);
1035             }
1036         }
1037         else {
1038             lv_dropdown_open(obj);
1039         }
1040     }
1041     else {
1042         dropdown->sel_opt_id = dropdown->sel_opt_id_orig;
1043         lv_obj_invalidate(obj);
1044     }
1045     return LV_RES_OK;
1046 }
1047 
1048 /**
1049  * Called when a drop down list is released to open it or set new option
1050  * @param list pointer to the drop down list's list
1051  * @return LV_RES_INV if the list is not being deleted in the user callback. Else LV_RES_OK
1052  */
list_release_handler(lv_obj_t * list_obj)1053 static lv_res_t list_release_handler(lv_obj_t * list_obj)
1054 {
1055     lv_dropdown_list_t * list = (lv_dropdown_list_t *) list_obj;
1056     lv_obj_t * dropdown_obj = list->dropdown;
1057     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
1058 
1059     lv_indev_t * indev = lv_indev_get_act();
1060     /*Leave edit mode once a new item is selected*/
1061     if(lv_indev_get_type(indev) == LV_INDEV_TYPE_ENCODER) {
1062         dropdown->sel_opt_id_orig = dropdown->sel_opt_id;
1063         lv_group_t * g      = lv_obj_get_group(dropdown_obj);
1064         if(lv_group_get_editing(g)) {
1065             lv_group_set_editing(g, false);
1066         }
1067     }
1068 
1069     /*Search the clicked option (For KEYPAD and ENCODER the new value should be already set)*/
1070     if(lv_indev_get_type(indev) == LV_INDEV_TYPE_POINTER || lv_indev_get_type(indev) == LV_INDEV_TYPE_BUTTON) {
1071         lv_point_t p;
1072         lv_indev_get_point(indev, &p);
1073         dropdown->sel_opt_id     = get_id_on_point(dropdown_obj, p.y);
1074         dropdown->sel_opt_id_orig = dropdown->sel_opt_id;
1075     }
1076 
1077     lv_dropdown_close(dropdown_obj);
1078 
1079     /*Invalidate to refresh the text*/
1080     if(dropdown->text == NULL) lv_obj_invalidate(dropdown_obj);
1081 
1082     uint32_t id  = dropdown->sel_opt_id; /*Just to use uint32_t in event data*/
1083     lv_res_t res = lv_event_send(dropdown_obj, LV_EVENT_VALUE_CHANGED, &id);
1084     if(res != LV_RES_OK) return res;
1085 
1086     return LV_RES_OK;
1087 }
1088 
list_press_handler(lv_obj_t * list_obj)1089 static void list_press_handler(lv_obj_t * list_obj)
1090 {
1091     lv_dropdown_list_t * list = (lv_dropdown_list_t *) list_obj;
1092     lv_obj_t * dropdown_obj = list->dropdown;
1093     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
1094 
1095     lv_indev_t * indev = lv_indev_get_act();
1096     if(indev && (lv_indev_get_type(indev) == LV_INDEV_TYPE_POINTER || lv_indev_get_type(indev) == LV_INDEV_TYPE_BUTTON)) {
1097         lv_point_t p;
1098         lv_indev_get_point(indev, &p);
1099         dropdown->pr_opt_id = get_id_on_point(dropdown_obj, p.y);
1100         lv_obj_invalidate(list_obj);
1101     }
1102 }
1103 
get_id_on_point(lv_obj_t * dropdown_obj,lv_coord_t y)1104 static uint16_t get_id_on_point(lv_obj_t * dropdown_obj, lv_coord_t y)
1105 {
1106     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
1107     lv_obj_t * label = get_label(dropdown_obj);
1108     if(label == NULL) return 0;
1109     y -= label->coords.y1;
1110 
1111     const lv_font_t * font         = lv_obj_get_style_text_font(label, LV_PART_MAIN);
1112     lv_coord_t font_h              = lv_font_get_line_height(font);
1113     lv_coord_t line_space = lv_obj_get_style_text_line_space(label, LV_PART_MAIN);
1114 
1115     y += line_space / 2;
1116     lv_coord_t h = font_h + line_space;
1117 
1118     uint16_t opt = y / h;
1119 
1120     if(opt >= dropdown->option_cnt) opt = dropdown->option_cnt - 1;
1121     return opt;
1122 }
1123 
1124 /**
1125  * Set the position of list when it is closed to show the selected item
1126  * @param ddlist pointer to a drop down list
1127  */
position_to_selected(lv_obj_t * dropdown_obj)1128 static void position_to_selected(lv_obj_t * dropdown_obj)
1129 {
1130     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
1131 
1132     lv_obj_t * label = get_label(dropdown_obj);
1133     if(label == NULL) return;
1134 
1135     if(lv_obj_get_height(label) <= lv_obj_get_content_height(dropdown_obj)) return;
1136 
1137     const lv_font_t * font         = lv_obj_get_style_text_font(label, LV_PART_MAIN);
1138     lv_coord_t font_h              = lv_font_get_line_height(font);
1139     lv_coord_t line_space = lv_obj_get_style_text_line_space(label, LV_PART_MAIN);
1140     lv_coord_t unit_h = font_h + line_space;
1141     lv_coord_t line_y1 = dropdown->sel_opt_id * unit_h;
1142 
1143     /*Scroll to the selected option*/
1144     lv_obj_scroll_to_y(dropdown->list, line_y1, LV_ANIM_OFF);
1145     lv_obj_invalidate(dropdown->list);
1146 }
1147 
get_label(const lv_obj_t * obj)1148 static lv_obj_t * get_label(const lv_obj_t * obj)
1149 {
1150     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
1151     if(dropdown->list == NULL) return NULL;
1152 
1153     return lv_obj_get_child(dropdown->list, 0);
1154 }
1155 
1156 #endif
1157