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