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     lv_obj_invalidate(obj);
289 }
290 
lv_dropdown_set_dir(lv_obj_t * obj,lv_dir_t dir)291 void lv_dropdown_set_dir(lv_obj_t * obj, lv_dir_t dir)
292 {
293     LV_ASSERT_OBJ(obj, MY_CLASS);
294 
295     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
296     if(dropdown->dir == dir) return;
297 
298     dropdown->dir = dir;
299 
300     lv_obj_invalidate(obj);
301 }
302 
lv_dropdown_set_symbol(lv_obj_t * obj,const void * symbol)303 void lv_dropdown_set_symbol(lv_obj_t * obj, const void * symbol)
304 {
305     LV_ASSERT_OBJ(obj, MY_CLASS);
306 
307     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
308     dropdown->symbol = symbol;
309     lv_obj_invalidate(obj);
310 }
311 
lv_dropdown_set_selected_highlight(lv_obj_t * obj,bool en)312 void lv_dropdown_set_selected_highlight(lv_obj_t * obj, bool en)
313 {
314     LV_ASSERT_OBJ(obj, MY_CLASS);
315 
316     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
317     dropdown->selected_highlight = en;
318     if(dropdown->list) lv_obj_invalidate(dropdown->list);
319 }
320 
321 /*=====================
322  * Getter functions
323  *====================*/
324 
lv_dropdown_get_list(lv_obj_t * obj)325 lv_obj_t * lv_dropdown_get_list(lv_obj_t * obj)
326 {
327     LV_ASSERT_OBJ(obj, MY_CLASS);
328     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
329 
330     return dropdown->list;
331 }
332 
lv_dropdown_get_text(lv_obj_t * obj)333 const char * lv_dropdown_get_text(lv_obj_t * obj)
334 {
335     LV_ASSERT_OBJ(obj, MY_CLASS);
336     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
337 
338     return dropdown->text;
339 }
340 
lv_dropdown_get_options(const lv_obj_t * obj)341 const char * lv_dropdown_get_options(const lv_obj_t * obj)
342 {
343     LV_ASSERT_OBJ(obj, MY_CLASS);
344 
345     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
346     return dropdown->options == NULL ? "" : dropdown->options;
347 }
348 
lv_dropdown_get_selected(const lv_obj_t * obj)349 uint16_t lv_dropdown_get_selected(const lv_obj_t * obj)
350 {
351     LV_ASSERT_OBJ(obj, MY_CLASS);
352 
353     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
354 
355     return dropdown->sel_opt_id;
356 }
357 
lv_dropdown_get_option_cnt(const lv_obj_t * obj)358 uint16_t lv_dropdown_get_option_cnt(const lv_obj_t * obj)
359 {
360     LV_ASSERT_OBJ(obj, MY_CLASS);
361 
362     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
363 
364     return dropdown->option_cnt;
365 }
366 
lv_dropdown_get_selected_str(const lv_obj_t * obj,char * buf,uint32_t buf_size)367 void lv_dropdown_get_selected_str(const lv_obj_t * obj, char * buf, uint32_t buf_size)
368 {
369     LV_ASSERT_OBJ(obj, MY_CLASS);
370 
371     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
372 
373     uint32_t i;
374     uint32_t line        = 0;
375     size_t txt_len;
376 
377     if(dropdown->options)  {
378         txt_len     = strlen(dropdown->options);
379     }
380     else {
381         buf[0] = '\0';
382         return;
383     }
384 
385     for(i = 0; i < txt_len && line != dropdown->sel_opt_id_orig; i++) {
386         if(dropdown->options[i] == '\n') line++;
387     }
388 
389     uint32_t c;
390     for(c = 0; i < txt_len && dropdown->options[i] != '\n'; c++, i++) {
391         if(buf_size && c >= buf_size - 1) {
392             LV_LOG_WARN("lv_dropdown_get_selected_str: the buffer was too small");
393             break;
394         }
395         buf[c] = dropdown->options[i];
396     }
397 
398     buf[c] = '\0';
399 }
400 
lv_dropdown_get_symbol(lv_obj_t * obj)401 const char * lv_dropdown_get_symbol(lv_obj_t * obj)
402 {
403     LV_ASSERT_OBJ(obj, MY_CLASS);
404     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
405     return dropdown->symbol;
406 }
407 
lv_dropdown_get_selected_highlight(lv_obj_t * obj)408 bool lv_dropdown_get_selected_highlight(lv_obj_t * obj)
409 {
410     LV_ASSERT_OBJ(obj, MY_CLASS);
411     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
412     return dropdown->selected_highlight;
413 }
414 
lv_dropdown_get_dir(const lv_obj_t * obj)415 lv_dir_t lv_dropdown_get_dir(const lv_obj_t * obj)
416 {
417     LV_ASSERT_OBJ(obj, MY_CLASS);
418     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
419     return dropdown->dir;
420 }
421 
422 /*=====================
423  * Other functions
424  *====================*/
425 
lv_dropdown_open(lv_obj_t * dropdown_obj)426 void lv_dropdown_open(lv_obj_t * dropdown_obj)
427 {
428     LV_ASSERT_OBJ(dropdown_obj, MY_CLASS);
429 
430     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
431 
432     lv_obj_add_state(dropdown_obj, LV_STATE_CHECKED);
433     lv_obj_set_parent(dropdown->list, lv_obj_get_screen(dropdown_obj));
434     lv_obj_move_to_index(dropdown->list, -1);
435     lv_obj_clear_flag(dropdown->list, LV_OBJ_FLAG_HIDDEN);
436 
437     /*To allow styling the list*/
438     lv_event_send(dropdown_obj, LV_EVENT_READY, NULL);
439 
440     lv_obj_t * label = get_label(dropdown_obj);
441     lv_label_set_text_static(label, dropdown->options);
442     lv_obj_set_width(dropdown->list, LV_SIZE_CONTENT);
443 
444     lv_obj_update_layout(label);
445     /*Set smaller width to the width of the button*/
446     if(lv_obj_get_width(dropdown->list) <= lv_obj_get_width(dropdown_obj) &&
447        (dropdown->dir == LV_DIR_TOP || dropdown->dir == LV_DIR_BOTTOM)) {
448         lv_obj_set_width(dropdown->list, lv_obj_get_width(dropdown_obj));
449     }
450 
451     lv_coord_t label_h = lv_obj_get_height(label);
452     lv_coord_t border_width = lv_obj_get_style_border_width(dropdown->list, LV_PART_MAIN);
453     lv_coord_t top = lv_obj_get_style_pad_top(dropdown->list, LV_PART_MAIN) + border_width;
454     lv_coord_t bottom = lv_obj_get_style_pad_bottom(dropdown->list, LV_PART_MAIN) + border_width;
455 
456     lv_coord_t list_fit_h = label_h + top + bottom;
457     lv_coord_t list_h = list_fit_h;
458 
459     lv_dir_t dir = dropdown->dir;
460     /*No space on the bottom? See if top is better.*/
461     if(dropdown->dir == LV_DIR_BOTTOM) {
462         if(dropdown_obj->coords.y2 + list_h > LV_VER_RES) {
463             if(dropdown_obj->coords.y1 > LV_VER_RES - dropdown_obj->coords.y2) {
464                 /*There is more space on the top, so make it drop up*/
465                 dir = LV_DIR_TOP;
466                 list_h = dropdown_obj->coords.y1 - 1;
467             }
468             else {
469                 list_h = LV_VER_RES - dropdown_obj->coords.y2 - 1 ;
470             }
471         }
472     }
473     /*No space on the top? See if bottom is better.*/
474     else if(dropdown->dir == LV_DIR_TOP) {
475         if(dropdown_obj->coords.y1 - list_h < 0) {
476             if(dropdown_obj->coords.y1 < LV_VER_RES - dropdown_obj->coords.y2) {
477                 /*There is more space on the top, so make it drop up*/
478                 dir = LV_DIR_BOTTOM;
479                 list_h = LV_VER_RES - dropdown_obj->coords.y2;
480             }
481             else {
482                 list_h = dropdown_obj->coords.y1;
483             }
484         }
485     }
486 
487     if(list_h > list_fit_h) list_h = list_fit_h;
488     lv_obj_set_height(dropdown->list, list_h);
489 
490     position_to_selected(dropdown_obj);
491 
492     if(dir == LV_DIR_BOTTOM)     lv_obj_align_to(dropdown->list, dropdown_obj, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
493     else if(dir == LV_DIR_TOP)   lv_obj_align_to(dropdown->list, dropdown_obj, LV_ALIGN_OUT_TOP_LEFT, 0, 0);
494     else if(dir == LV_DIR_LEFT)  lv_obj_align_to(dropdown->list, dropdown_obj, LV_ALIGN_OUT_LEFT_TOP, 0, 0);
495     else if(dir == LV_DIR_RIGHT) lv_obj_align_to(dropdown->list, dropdown_obj, LV_ALIGN_OUT_RIGHT_TOP, 0, 0);
496 
497     lv_obj_update_layout(dropdown->list);
498 
499     if(dropdown->dir == LV_DIR_LEFT || dropdown->dir == LV_DIR_RIGHT) {
500         lv_coord_t y1 = lv_obj_get_y(dropdown->list);
501         lv_coord_t y2 = lv_obj_get_y2(dropdown->list);
502         if(y2 >= LV_VER_RES) {
503             lv_obj_set_y(dropdown->list, y1 - (y2 - LV_VER_RES) - 1);
504         }
505     }
506 
507     lv_text_align_t align = lv_obj_calculate_style_text_align(label, LV_PART_MAIN, dropdown->options);
508 
509     switch(align) {
510         default:
511         case LV_TEXT_ALIGN_LEFT:
512             lv_obj_align(label, LV_ALIGN_TOP_LEFT, 0, 0);
513             break;
514         case LV_TEXT_ALIGN_RIGHT:
515             lv_obj_align(label, LV_ALIGN_TOP_RIGHT, 0, 0);
516             break;
517         case LV_TEXT_ALIGN_CENTER:
518             lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
519             break;
520 
521     }
522 }
523 
lv_dropdown_close(lv_obj_t * obj)524 void lv_dropdown_close(lv_obj_t * obj)
525 {
526     LV_ASSERT_OBJ(obj, MY_CLASS);
527 
528     lv_obj_clear_state(obj, LV_STATE_CHECKED);
529     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
530 
531     dropdown->pr_opt_id = LV_DROPDOWN_PR_NONE;
532     lv_obj_add_flag(dropdown->list, LV_OBJ_FLAG_HIDDEN);
533 
534     lv_event_send(obj, LV_EVENT_CANCEL, NULL);
535 }
536 
lv_dropdown_is_open(lv_obj_t * obj)537 bool lv_dropdown_is_open(lv_obj_t * obj)
538 {
539     LV_ASSERT_OBJ(obj, MY_CLASS);
540     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
541 
542     return lv_obj_has_flag(dropdown->list, LV_OBJ_FLAG_HIDDEN) ? false : true;
543 }
544 
545 /**********************
546  *   STATIC FUNCTIONS
547  **********************/
548 
lv_dropdown_list_create(lv_obj_t * parent)549 static lv_obj_t * lv_dropdown_list_create(lv_obj_t * parent)
550 {
551     LV_LOG_INFO("begin");
552     lv_obj_t * obj = lv_obj_class_create_obj(&lv_dropdownlist_class, parent);
553     lv_obj_class_init_obj(obj);
554     return obj;
555 }
556 
lv_dropdown_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)557 static void lv_dropdown_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
558 {
559     LV_UNUSED(class_p);
560     LV_TRACE_OBJ_CREATE("begin");
561 
562     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
563 
564     /*Initialize the allocated 'ext'*/
565     dropdown->list          = NULL;
566     dropdown->options     = NULL;
567     dropdown->symbol         = LV_SYMBOL_DOWN;
568     dropdown->text         = NULL;
569     dropdown->static_txt = 1;
570     dropdown->selected_highlight = 1;
571     dropdown->sel_opt_id      = 0;
572     dropdown->sel_opt_id_orig = 0;
573     dropdown->pr_opt_id = LV_DROPDOWN_PR_NONE;
574     dropdown->option_cnt      = 0;
575     dropdown->dir = LV_DIR_BOTTOM;
576 
577     lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
578     lv_dropdown_set_options_static(obj, "Option 1\nOption 2\nOption 3");
579 
580     dropdown->list = lv_dropdown_list_create(lv_obj_get_screen(obj));
581     lv_dropdown_list_t * list = (lv_dropdown_list_t *)dropdown->list;
582     list->dropdown = obj;
583 
584     LV_TRACE_OBJ_CREATE("finished");
585 }
586 
lv_dropdown_destructor(const lv_obj_class_t * class_p,lv_obj_t * obj)587 static void lv_dropdown_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
588 {
589     LV_UNUSED(class_p);
590     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
591 
592     if(dropdown->list) {
593         lv_obj_del(dropdown->list);
594         dropdown->list = NULL;
595     }
596 
597     if(!dropdown->static_txt) {
598         lv_mem_free(dropdown->options);
599         dropdown->options = NULL;
600     }
601 }
602 
lv_dropdownlist_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)603 static void lv_dropdownlist_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
604 {
605     LV_UNUSED(class_p);
606     LV_TRACE_OBJ_CREATE("begin");
607 
608     lv_obj_clear_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
609     lv_obj_clear_flag(obj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
610     lv_obj_add_flag(obj, LV_OBJ_FLAG_IGNORE_LAYOUT);
611     lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
612 
613     lv_label_create(obj);
614 
615     LV_TRACE_OBJ_CREATE("finished");
616 }
617 
lv_dropdownlist_destructor(const lv_obj_class_t * class_p,lv_obj_t * list_obj)618 static void lv_dropdownlist_destructor(const lv_obj_class_t * class_p, lv_obj_t * list_obj)
619 {
620     LV_UNUSED(class_p);
621     lv_dropdown_list_t * list = (lv_dropdown_list_t *)list_obj;
622     lv_obj_t * dropdown_obj = list->dropdown;
623     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
624     dropdown->list = NULL;
625 }
626 
lv_dropdown_event(const lv_obj_class_t * class_p,lv_event_t * e)627 static void lv_dropdown_event(const lv_obj_class_t * class_p, lv_event_t * e)
628 {
629     LV_UNUSED(class_p);
630 
631     lv_res_t res;
632 
633     /*Call the ancestor's event handler*/
634     res = lv_obj_event_base(MY_CLASS, e);
635     if(res != LV_RES_OK) return;
636 
637     lv_event_code_t code = lv_event_get_code(e);
638     lv_obj_t * obj = lv_event_get_target(e);
639     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
640 
641     if(code == LV_EVENT_FOCUSED) {
642         lv_group_t * g             = lv_obj_get_group(obj);
643         bool editing               = lv_group_get_editing(g);
644         lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
645 
646         /*Encoders need special handling*/
647         if(indev_type == LV_INDEV_TYPE_ENCODER) {
648             /*Open the list if editing*/
649             if(editing) {
650                 lv_dropdown_open(obj);
651             }
652             /*Close the list if navigating*/
653             else {
654                 dropdown->sel_opt_id = dropdown->sel_opt_id_orig;
655                 lv_dropdown_close(obj);
656             }
657         }
658     }
659     else if(code == LV_EVENT_DEFOCUSED || code == LV_EVENT_LEAVE) {
660         lv_dropdown_close(obj);
661     }
662     else if(code == LV_EVENT_RELEASED) {
663         res = btn_release_handler(obj);
664         if(res != LV_RES_OK) return;
665     }
666     else if(code == LV_EVENT_STYLE_CHANGED) {
667         lv_obj_refresh_self_size(obj);
668     }
669     else if(code == LV_EVENT_SIZE_CHANGED) {
670         lv_obj_refresh_self_size(obj);
671     }
672     else if(code == LV_EVENT_GET_SELF_SIZE) {
673         lv_point_t * p = lv_event_get_param(e);
674         const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
675         p->y = lv_font_get_line_height(font);
676     }
677     else if(code == LV_EVENT_KEY) {
678         char c = *((char *)lv_event_get_param(e));
679         if(c == LV_KEY_RIGHT || c == LV_KEY_DOWN) {
680             if(!lv_dropdown_is_open(obj)) {
681                 lv_dropdown_open(obj);
682             }
683             else if(dropdown->sel_opt_id + 1 < dropdown->option_cnt) {
684                 dropdown->sel_opt_id++;
685                 position_to_selected(obj);
686             }
687         }
688         else if(c == LV_KEY_LEFT || c == LV_KEY_UP) {
689 
690             if(!lv_dropdown_is_open(obj)) {
691                 lv_dropdown_open(obj);
692             }
693             else if(dropdown->sel_opt_id > 0) {
694                 dropdown->sel_opt_id--;
695                 position_to_selected(obj);
696             }
697         }
698         else if(c == LV_KEY_ESC) {
699             dropdown->sel_opt_id = dropdown->sel_opt_id_orig;
700             lv_dropdown_close(obj);
701         }
702         else if(c == LV_KEY_ENTER) {
703             /* Handle the ENTER key only if it was send by an other object.
704              * Do no process it if ENTER is sent by the dropdown because it's handled in LV_EVENT_RELEASED */
705             lv_obj_t * indev_obj = lv_indev_get_obj_act();
706             if(indev_obj != obj) {
707                 res = btn_release_handler(obj);
708                 if(res != LV_RES_OK) return;
709             }
710         }
711     }
712     else if(code == LV_EVENT_DRAW_MAIN) {
713         draw_main(e);
714     }
715 }
716 
lv_dropdown_list_event(const lv_obj_class_t * class_p,lv_event_t * e)717 static void lv_dropdown_list_event(const lv_obj_class_t * class_p, lv_event_t * e)
718 {
719     LV_UNUSED(class_p);
720 
721     lv_res_t res;
722 
723     /*Call the ancestor's event handler*/
724     lv_event_code_t code = lv_event_get_code(e);
725     if(code != LV_EVENT_DRAW_POST) {
726         res = lv_obj_event_base(MY_CLASS_LIST, e);
727         if(res != LV_RES_OK) return;
728     }
729     lv_obj_t * list = lv_event_get_target(e);
730     lv_obj_t * dropdown_obj = ((lv_dropdown_list_t *)list)->dropdown;
731     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
732 
733     if(code == LV_EVENT_RELEASED) {
734         if(lv_indev_get_scroll_obj(lv_indev_get_act()) == NULL) {
735             list_release_handler(list);
736         }
737     }
738     else if(code == LV_EVENT_PRESSED) {
739         list_press_handler(list);
740     }
741     else if(code == LV_EVENT_SCROLL_BEGIN) {
742         dropdown->pr_opt_id = LV_DROPDOWN_PR_NONE;
743         lv_obj_invalidate(list);
744     }
745     else if(code == LV_EVENT_DRAW_POST) {
746         draw_list(e);
747         res = lv_obj_event_base(MY_CLASS_LIST, e);
748         if(res != LV_RES_OK) return;
749     }
750 }
751 
752 
draw_main(lv_event_t * e)753 static void draw_main(lv_event_t * e)
754 {
755     lv_obj_t * obj = lv_event_get_target(e);
756     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
757     lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
758 
759     lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
760     lv_coord_t left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN) + border_width;
761     lv_coord_t right = lv_obj_get_style_pad_right(obj, LV_PART_MAIN) + border_width;
762     lv_coord_t top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN) + border_width;
763 
764     lv_draw_label_dsc_t symbol_dsc;
765     lv_draw_label_dsc_init(&symbol_dsc);
766     lv_obj_init_draw_label_dsc(obj, LV_PART_INDICATOR, &symbol_dsc);
767 
768     /*If no text specified use the selected option*/
769     const char * opt_txt;
770     if(dropdown->text) opt_txt = dropdown->text;
771     else {
772         char * buf = lv_mem_buf_get(128);
773         lv_dropdown_get_selected_str(obj, buf, 128);
774         opt_txt = buf;
775     }
776 
777     bool symbol_to_left = false;
778     if(dropdown->dir == LV_DIR_LEFT) symbol_to_left = true;
779     if(lv_obj_get_style_base_dir(obj, LV_PART_MAIN) == LV_BASE_DIR_RTL) symbol_to_left = true;
780 
781     if(dropdown->symbol) {
782         lv_img_src_t symbol_type = lv_img_src_get_type(dropdown->symbol);
783         lv_coord_t symbol_w;
784         lv_coord_t symbol_h;
785         if(symbol_type == LV_IMG_SRC_SYMBOL) {
786             lv_point_t size;
787             lv_txt_get_size(&size, dropdown->symbol, symbol_dsc.font, symbol_dsc.letter_space, symbol_dsc.line_space, LV_COORD_MAX,
788                             symbol_dsc.flag);
789             symbol_w = size.x;
790             symbol_h = size.y;
791         }
792         else {
793             lv_img_header_t header;
794             lv_res_t res = lv_img_decoder_get_info(dropdown->symbol, &header);
795             if(res == LV_RES_OK) {
796                 symbol_w = header.w;
797                 symbol_h = header.h;
798             }
799             else {
800                 symbol_w = -1;
801                 symbol_h = -1;
802             }
803         }
804 
805         lv_area_t symbol_area;
806         if(symbol_to_left) {
807             symbol_area.x1 = obj->coords.x1 + left;
808             symbol_area.x2 = symbol_area.x1 + symbol_w - 1;
809         }
810         else {
811             symbol_area.x1 = obj->coords.x2 - right - symbol_w;
812             symbol_area.x2 = symbol_area.x1 + symbol_w - 1;
813         }
814 
815         if(symbol_type == LV_IMG_SRC_SYMBOL) {
816             symbol_area.y1 = obj->coords.y1 + top;
817             symbol_area.y2 = symbol_area.y1 + symbol_h - 1;
818             lv_draw_label(draw_ctx, &symbol_dsc, &symbol_area, dropdown->symbol, NULL);
819         }
820         else {
821             symbol_area.y1 = obj->coords.y1 + (lv_obj_get_height(obj) - symbol_h) / 2;
822             symbol_area.y2 = symbol_area.y1 + symbol_h - 1;
823             lv_draw_img_dsc_t img_dsc;
824             lv_draw_img_dsc_init(&img_dsc);
825             lv_obj_init_draw_img_dsc(obj, LV_PART_INDICATOR, &img_dsc);
826             img_dsc.pivot.x = symbol_w / 2;
827             img_dsc.pivot.y = symbol_h / 2;
828             img_dsc.angle = lv_obj_get_style_transform_angle(obj, LV_PART_INDICATOR);
829             lv_draw_img(draw_ctx, &img_dsc, &symbol_area, dropdown->symbol);
830         }
831     }
832 
833     lv_draw_label_dsc_t label_dsc;
834     lv_draw_label_dsc_init(&label_dsc);
835     lv_obj_init_draw_label_dsc(obj, LV_PART_MAIN, &label_dsc);
836 
837     lv_point_t size;
838     lv_txt_get_size(&size, opt_txt, label_dsc.font, label_dsc.letter_space, label_dsc.line_space, LV_COORD_MAX,
839                     label_dsc.flag);
840 
841     lv_area_t txt_area;
842     txt_area.y1 = obj->coords.y1 + top;
843     txt_area.y2 = txt_area.y1 + size.y;
844     /*Center align the text if no symbol*/
845     if(dropdown->symbol == NULL) {
846         txt_area.x1 = obj->coords.x1 + (lv_obj_get_width(obj) - size.x) / 2;
847         txt_area.x2 = txt_area.x1 + size.x;
848     }
849     else {
850         /*Text to the right*/
851         if(symbol_to_left) {
852             txt_area.x1 = obj->coords.x2 - right - size.x;
853             txt_area.x2 = txt_area.x1 + size.x;
854         }
855         else {
856             txt_area.x1 = obj->coords.x1 + left;
857             txt_area.x2 = txt_area.x1 + size.x;
858         }
859     }
860     lv_draw_label(draw_ctx, &label_dsc, &txt_area, opt_txt, NULL);
861 
862     if(dropdown->text == NULL) {
863         lv_mem_buf_release((char *)opt_txt);
864     }
865 }
866 
draw_list(lv_event_t * e)867 static void draw_list(lv_event_t * e)
868 {
869     lv_obj_t * list_obj = lv_event_get_target(e);
870     lv_dropdown_list_t * list = (lv_dropdown_list_t *)list_obj;
871     lv_obj_t * dropdown_obj = list->dropdown;
872     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
873     lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
874 
875     /* Clip area might be too large too to shadow but
876      * the selected option can be drawn on only the background*/
877     lv_area_t clip_area_core;
878     bool has_common;
879     has_common = _lv_area_intersect(&clip_area_core, draw_ctx->clip_area, &dropdown->list->coords);
880     if(has_common) {
881         const lv_area_t * clip_area_ori = draw_ctx->clip_area;
882         draw_ctx->clip_area = &clip_area_core;
883         if(dropdown->selected_highlight) {
884             if(dropdown->pr_opt_id == dropdown->sel_opt_id) {
885                 draw_box(dropdown_obj, draw_ctx, dropdown->pr_opt_id, LV_STATE_CHECKED | LV_STATE_PRESSED);
886                 draw_box_label(dropdown_obj, draw_ctx, dropdown->pr_opt_id, LV_STATE_CHECKED | LV_STATE_PRESSED);
887             }
888             else {
889                 draw_box(dropdown_obj, draw_ctx, dropdown->pr_opt_id, LV_STATE_PRESSED);
890                 draw_box_label(dropdown_obj, draw_ctx, dropdown->pr_opt_id, LV_STATE_PRESSED);
891                 draw_box(dropdown_obj, draw_ctx, dropdown->sel_opt_id, LV_STATE_CHECKED);
892                 draw_box_label(dropdown_obj, draw_ctx, dropdown->sel_opt_id, LV_STATE_CHECKED);
893             }
894         }
895         else {
896             draw_box(dropdown_obj, draw_ctx, dropdown->pr_opt_id, LV_STATE_PRESSED);
897             draw_box_label(dropdown_obj, draw_ctx, dropdown->pr_opt_id, LV_STATE_PRESSED);
898         }
899         draw_ctx->clip_area = clip_area_ori;
900     }
901 }
902 
draw_box(lv_obj_t * dropdown_obj,lv_draw_ctx_t * draw_ctx,uint16_t id,lv_state_t state)903 static void draw_box(lv_obj_t * dropdown_obj, lv_draw_ctx_t * draw_ctx, uint16_t id, lv_state_t state)
904 {
905     if(id == LV_DROPDOWN_PR_NONE) return;
906 
907     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
908     lv_obj_t * list_obj = dropdown->list;
909     lv_state_t state_ori = list_obj->state;
910 
911     if(state != list_obj->state) {
912         list_obj->state = state;
913         list_obj->skip_trans = 1;
914     }
915 
916     /*Draw a rectangle under the selected item*/
917     const lv_font_t * font    = lv_obj_get_style_text_font(list_obj, LV_PART_SELECTED);
918     lv_coord_t line_space = lv_obj_get_style_text_line_space(list_obj,  LV_PART_SELECTED);
919     lv_coord_t font_h         = lv_font_get_line_height(font);
920 
921     /*Draw the selected*/
922     lv_obj_t * label = get_label(dropdown_obj);
923     lv_area_t rect_area;
924     rect_area.y1 = label->coords.y1;
925     rect_area.y1 += id * (font_h + line_space);
926     rect_area.y1 -= line_space / 2;
927 
928     rect_area.y2 = rect_area.y1 + font_h + line_space - 1;
929     rect_area.x1 = dropdown->list->coords.x1;
930     rect_area.x2 = dropdown->list->coords.x2;
931 
932     lv_draw_rect_dsc_t sel_rect;
933     lv_draw_rect_dsc_init(&sel_rect);
934     lv_obj_init_draw_rect_dsc(list_obj,  LV_PART_SELECTED, &sel_rect);
935     lv_draw_rect(draw_ctx, &sel_rect, &rect_area);
936 
937     list_obj->state = state_ori;
938     list_obj->skip_trans = 0;
939 }
940 
draw_box_label(lv_obj_t * dropdown_obj,lv_draw_ctx_t * draw_ctx,uint16_t id,lv_state_t state)941 static void draw_box_label(lv_obj_t * dropdown_obj, lv_draw_ctx_t * draw_ctx, uint16_t id, lv_state_t state)
942 {
943     if(id == LV_DROPDOWN_PR_NONE) return;
944 
945     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
946     lv_obj_t * list_obj = dropdown->list;
947     lv_state_t state_orig = list_obj->state;
948 
949     if(state != list_obj->state) {
950         list_obj->state = state;
951         list_obj->skip_trans = 1;
952     }
953 
954     lv_draw_label_dsc_t label_dsc;
955     lv_draw_label_dsc_init(&label_dsc);
956     lv_obj_init_draw_label_dsc(list_obj, LV_PART_SELECTED, &label_dsc);
957 
958     label_dsc.line_space = lv_obj_get_style_text_line_space(list_obj,
959                                                             LV_PART_SELECTED);  /*Line space should come from the list*/
960 
961     lv_obj_t * label = get_label(dropdown_obj);
962     if(label == NULL) return;
963 
964     lv_coord_t font_h        = lv_font_get_line_height(label_dsc.font);
965 
966     lv_area_t area_sel;
967     area_sel.y1 = label->coords.y1;
968     area_sel.y1 += id * (font_h + label_dsc.line_space);
969     area_sel.y1 -= label_dsc.line_space / 2;
970 
971     area_sel.y2 = area_sel.y1 + font_h + label_dsc.line_space - 1;
972     area_sel.x1 = list_obj->coords.x1;
973     area_sel.x2 = list_obj->coords.x2;
974     lv_area_t mask_sel;
975     bool area_ok;
976     area_ok = _lv_area_intersect(&mask_sel, draw_ctx->clip_area, &area_sel);
977     if(area_ok) {
978         const lv_area_t * clip_area_ori = draw_ctx->clip_area;
979         draw_ctx->clip_area = &mask_sel;
980         lv_draw_label(draw_ctx, &label_dsc, &label->coords, lv_label_get_text(label), NULL);
981         draw_ctx->clip_area = clip_area_ori;
982     }
983     list_obj->state = state_orig;
984     list_obj->skip_trans = 0;
985 }
986 
987 
btn_release_handler(lv_obj_t * obj)988 static lv_res_t btn_release_handler(lv_obj_t * obj)
989 {
990     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
991     lv_indev_t * indev = lv_indev_get_act();
992     if(lv_indev_get_scroll_obj(indev) == NULL) {
993         if(lv_dropdown_is_open(obj)) {
994             lv_dropdown_close(obj);
995             if(dropdown->sel_opt_id_orig != dropdown->sel_opt_id) {
996                 dropdown->sel_opt_id_orig = dropdown->sel_opt_id;
997                 lv_res_t res;
998                 uint32_t id  = dropdown->sel_opt_id; /*Just to use uint32_t in event data*/
999                 res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, &id);
1000                 if(res != LV_RES_OK) return res;
1001                 lv_obj_invalidate(obj);
1002             }
1003             lv_indev_type_t indev_type = lv_indev_get_type(indev);
1004             if(indev_type == LV_INDEV_TYPE_ENCODER) {
1005                 lv_group_set_editing(lv_obj_get_group(obj), false);
1006             }
1007         }
1008         else {
1009             lv_dropdown_open(obj);
1010         }
1011     }
1012     else {
1013         dropdown->sel_opt_id = dropdown->sel_opt_id_orig;
1014         lv_obj_invalidate(obj);
1015     }
1016     return LV_RES_OK;
1017 }
1018 
1019 /**
1020  * Called when a drop down list is released to open it or set new option
1021  * @param list pointer to the drop down list's list
1022  * @return LV_RES_INV if the list is not being deleted in the user callback. Else LV_RES_OK
1023  */
list_release_handler(lv_obj_t * list_obj)1024 static lv_res_t list_release_handler(lv_obj_t * list_obj)
1025 {
1026     lv_dropdown_list_t * list = (lv_dropdown_list_t *) list_obj;
1027     lv_obj_t * dropdown_obj = list->dropdown;
1028     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
1029 
1030     lv_indev_t * indev = lv_indev_get_act();
1031     /*Leave edit mode once a new item is selected*/
1032     if(lv_indev_get_type(indev) == LV_INDEV_TYPE_ENCODER) {
1033         dropdown->sel_opt_id_orig = dropdown->sel_opt_id;
1034         lv_group_t * g      = lv_obj_get_group(dropdown_obj);
1035         if(lv_group_get_editing(g)) {
1036             lv_group_set_editing(g, false);
1037         }
1038     }
1039 
1040     /*Search the clicked option (For KEYPAD and ENCODER the new value should be already set)*/
1041     if(lv_indev_get_type(indev) == LV_INDEV_TYPE_POINTER || lv_indev_get_type(indev) == LV_INDEV_TYPE_BUTTON) {
1042         lv_point_t p;
1043         lv_indev_get_point(indev, &p);
1044         dropdown->sel_opt_id     = get_id_on_point(dropdown_obj, p.y);
1045         dropdown->sel_opt_id_orig = dropdown->sel_opt_id;
1046     }
1047 
1048     lv_dropdown_close(dropdown_obj);
1049 
1050     /*Invalidate to refresh the text*/
1051     if(dropdown->text == NULL) lv_obj_invalidate(dropdown_obj);
1052 
1053     uint32_t id  = dropdown->sel_opt_id; /*Just to use uint32_t in event data*/
1054     lv_res_t res = lv_event_send(dropdown_obj, LV_EVENT_VALUE_CHANGED, &id);
1055     if(res != LV_RES_OK) return res;
1056 
1057     return LV_RES_OK;
1058 }
1059 
list_press_handler(lv_obj_t * list_obj)1060 static void list_press_handler(lv_obj_t * list_obj)
1061 {
1062     lv_dropdown_list_t * list = (lv_dropdown_list_t *) list_obj;
1063     lv_obj_t * dropdown_obj = list->dropdown;
1064     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
1065 
1066     lv_indev_t * indev = lv_indev_get_act();
1067     if(indev && (lv_indev_get_type(indev) == LV_INDEV_TYPE_POINTER || lv_indev_get_type(indev) == LV_INDEV_TYPE_BUTTON)) {
1068         lv_point_t p;
1069         lv_indev_get_point(indev, &p);
1070         dropdown->pr_opt_id = get_id_on_point(dropdown_obj, p.y);
1071         lv_obj_invalidate(list_obj);
1072     }
1073 }
1074 
get_id_on_point(lv_obj_t * dropdown_obj,lv_coord_t y)1075 static uint16_t get_id_on_point(lv_obj_t * dropdown_obj, lv_coord_t y)
1076 {
1077     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
1078     lv_obj_t * label = get_label(dropdown_obj);
1079     if(label == NULL) return 0;
1080     y -= label->coords.y1;
1081 
1082     const lv_font_t * font         = lv_obj_get_style_text_font(label, LV_PART_MAIN);
1083     lv_coord_t font_h              = lv_font_get_line_height(font);
1084     lv_coord_t line_space = lv_obj_get_style_text_line_space(label, LV_PART_MAIN);
1085 
1086     y += line_space / 2;
1087     lv_coord_t h = font_h + line_space;
1088 
1089     uint16_t opt = y / h;
1090 
1091     if(opt >= dropdown->option_cnt) opt = dropdown->option_cnt - 1;
1092     return opt;
1093 }
1094 
1095 /**
1096  * Set the position of list when it is closed to show the selected item
1097  * @param ddlist pointer to a drop down list
1098  */
position_to_selected(lv_obj_t * dropdown_obj)1099 static void position_to_selected(lv_obj_t * dropdown_obj)
1100 {
1101     lv_dropdown_t * dropdown = (lv_dropdown_t *)dropdown_obj;
1102 
1103     lv_obj_t * label = get_label(dropdown_obj);
1104     if(label == NULL) return;
1105 
1106     if(lv_obj_get_height(label) <= lv_obj_get_content_height(dropdown_obj)) return;
1107 
1108     const lv_font_t * font         = lv_obj_get_style_text_font(label, LV_PART_MAIN);
1109     lv_coord_t font_h              = lv_font_get_line_height(font);
1110     lv_coord_t line_space = lv_obj_get_style_text_line_space(label, LV_PART_MAIN);
1111     lv_coord_t unit_h = font_h + line_space;
1112     lv_coord_t line_y1 = dropdown->sel_opt_id * unit_h;
1113 
1114     /*Scroll to the selected option*/
1115     lv_obj_scroll_to_y(dropdown->list, line_y1, LV_ANIM_OFF);
1116     lv_obj_invalidate(dropdown->list);
1117 }
1118 
get_label(const lv_obj_t * obj)1119 static lv_obj_t * get_label(const lv_obj_t * obj)
1120 {
1121     lv_dropdown_t * dropdown = (lv_dropdown_t *)obj;
1122     if(dropdown->list == NULL) return NULL;
1123 
1124     return lv_obj_get_child(dropdown->list, 0);
1125 }
1126 
1127 #endif
1128