1 /**
2 * @file lv_ddlist.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_dropdown.h"
10 #if LV_USE_DROPDOWN != 0
11
12 #include "../lv_misc/lv_debug.h"
13 #include "../lv_draw/lv_draw.h"
14 #include "../lv_core/lv_group.h"
15 #include "../lv_core/lv_indev.h"
16 #include "../lv_core/lv_disp.h"
17 #include "../lv_themes/lv_theme.h"
18 #include "../lv_font/lv_symbol_def.h"
19 #include "../lv_misc/lv_anim.h"
20 #include "../lv_misc/lv_math.h"
21 #include <string.h>
22
23 /*********************
24 * DEFINES
25 *********************/
26 #define LV_OBJX_NAME "lv_dropdown"
27
28 #if LV_USE_ANIMATION == 0
29 #undef LV_DROPDOWN_DEF_ANIM_TIME
30 #define LV_DROPDOWN_DEF_ANIM_TIME 0 /*No animation*/
31 #endif
32
33 #define LV_DROPDOWN_PR_NONE 0xFFFF
34
35 /**********************
36 * TYPEDEFS
37 **********************/
38 typedef struct {
39 lv_page_ext_t page;
40 lv_obj_t * ddlist; /*Pointer to the ddlist where the page belongs*/
41 } lv_dropdown_page_ext_t;
42
43 /**********************
44 * STATIC PROTOTYPES
45 **********************/
46 static lv_design_res_t lv_dropdown_design(lv_obj_t * ddlist, const lv_area_t * clip_area, lv_design_mode_t mode);
47 static lv_design_res_t lv_dropdown_page_design(lv_obj_t * ddlist, const lv_area_t * clip_area, lv_design_mode_t mode);
48 static lv_res_t lv_dropdown_signal(lv_obj_t * ddlist, lv_signal_t sign, void * param);
49 static lv_res_t lv_dropdown_page_signal(lv_obj_t * page, lv_signal_t sign, void * param);
50 static lv_res_t lv_dropdown_page_scrl_signal(lv_obj_t * scrl, lv_signal_t sign, void * param);
51 static lv_style_list_t * lv_dropdown_get_style(lv_obj_t * ddlist, uint8_t part);
52 static void draw_box(lv_obj_t * ddlist, const lv_area_t * clip_area, uint16_t id, lv_state_t state);
53 static void draw_box_label(lv_obj_t * ddlist, const lv_area_t * clip_area, uint16_t id, lv_state_t state);
54 static lv_res_t page_release_handler(lv_obj_t * page);
55 static void page_press_handler(lv_obj_t * page);
56 static uint16_t get_id_on_point(lv_obj_t * ddlist, lv_coord_t x, lv_coord_t y);
57 static void position_to_selected(lv_obj_t * ddlist);
58 static lv_obj_t * get_label(const lv_obj_t * ddlist);
59
60 /**********************
61 * STATIC VARIABLES
62 **********************/
63 static lv_signal_cb_t ancestor_signal;
64 static lv_signal_cb_t ancestor_page_signal;
65 static lv_signal_cb_t ancestor_page_scrl_signal;
66 static lv_design_cb_t ancestor_design;
67 static lv_design_cb_t ancestor_page_design;
68
69 /**********************
70 * MACROS
71 **********************/
72
73 /**********************
74 * GLOBAL FUNCTIONS
75 **********************/
76
77 /**
78 * Create a drop down list objects
79 * @param par pointer to an object, it will be the parent of the new drop down list
80 * @param copy pointer to a drop down list object, if not NULL then the new object will be copied
81 * from it
82 * @return pointer to the created drop down list
83 */
lv_dropdown_create(lv_obj_t * par,const lv_obj_t * copy)84 lv_obj_t * lv_dropdown_create(lv_obj_t * par, const lv_obj_t * copy)
85 {
86 LV_LOG_TRACE("drop down list create started");
87
88 /*Create the ancestor drop down list*/
89 lv_obj_t * ddlist = lv_obj_create(par, copy);
90 LV_ASSERT_MEM(ddlist);
91 if(ddlist == NULL) return NULL;
92
93 if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(ddlist);
94 if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(ddlist);
95
96 /*Allocate the drop down list type specific extended data*/
97 lv_dropdown_ext_t * ext = lv_obj_allocate_ext_attr(ddlist, sizeof(lv_dropdown_ext_t));
98 LV_ASSERT_MEM(ext);
99 if(ext == NULL) {
100 lv_obj_del(ddlist);
101 return NULL;
102 }
103
104 /*Initialize the allocated 'ext' */
105 ext->page = NULL;
106 ext->options = NULL;
107 ext->symbol = LV_SYMBOL_DOWN;
108 ext->text = "Select";
109 ext->static_txt = 1;
110 ext->show_selected = 1;
111 ext->sel_opt_id = 0;
112 ext->sel_opt_id_orig = 0;
113 ext->pr_opt_id = LV_DROPDOWN_PR_NONE;
114 ext->option_cnt = 0;
115 ext->dir = LV_DROPDOWN_DIR_DOWN;
116 ext->max_height = (3 * lv_disp_get_ver_res(NULL)) / 4;
117 lv_style_list_init(&ext->style_page);
118 lv_style_list_init(&ext->style_scrlbar);
119 lv_style_list_init(&ext->style_selected);
120
121 /*The signal and design functions are not copied so set them here*/
122 lv_obj_set_signal_cb(ddlist, lv_dropdown_signal);
123 lv_obj_set_design_cb(ddlist, lv_dropdown_design);
124
125 /*Init the new drop down list drop down list*/
126 if(copy == NULL) {
127 lv_obj_set_width(ddlist, LV_DPX(150));
128 lv_dropdown_set_options_static(ddlist, "Option 1\nOption 2\nOption 3");
129 lv_theme_apply(ddlist, LV_THEME_DROPDOWN);
130 }
131 /*Copy an existing drop down list*/
132 else {
133 lv_dropdown_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
134 if(copy_ext->static_txt == 0)
135 lv_dropdown_set_options(ddlist, lv_dropdown_get_options(copy));
136 else
137 lv_dropdown_set_options_static(ddlist, lv_dropdown_get_options(copy));
138 ext->option_cnt = copy_ext->option_cnt;
139 ext->sel_opt_id = copy_ext->sel_opt_id;
140 ext->sel_opt_id_orig = copy_ext->sel_opt_id;
141 ext->symbol = copy_ext->symbol;
142 ext->max_height = copy_ext->max_height;
143 ext->text = copy_ext->text;
144 ext->dir = copy_ext->dir;
145 ext->show_selected = copy_ext->show_selected;
146 lv_style_list_copy(&ext->style_page, ©_ext->style_page);
147 lv_style_list_copy(&ext->style_selected, ©_ext->style_selected);
148 lv_style_list_copy(&ext->style_scrlbar, ©_ext->style_scrlbar);
149 }
150
151 LV_LOG_INFO("drop down list created");
152
153 return ddlist;
154 }
155
156 /*=====================
157 * Setter functions
158 *====================*/
159
160 /**
161 * Set text of the ddlist (Displayed on the button if `show_selected = false`)
162 * @param ddlist pointer to a drop down list object
163 * @param txt the text as a string (Only it's pointer is saved)
164 */
lv_dropdown_set_text(lv_obj_t * ddlist,const char * txt)165 void lv_dropdown_set_text(lv_obj_t * ddlist, const char * txt)
166 {
167 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
168 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
169 if(ext->text == txt) return;
170
171 ext->text = txt;
172
173 lv_obj_invalidate(ddlist);
174 }
175
176 /**
177 * Clear any options in a drop down list. Static or dynamic.
178 * @param ddlist pointer to drop down list object
179 */
lv_dropdown_clear_options(lv_obj_t * ddlist)180 void lv_dropdown_clear_options(lv_obj_t * ddlist)
181 {
182 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
183 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
184 if(ext->options == NULL) return;
185
186 if(ext->static_txt == 0)
187 lv_mem_free(ext->options);
188
189 ext->options = NULL;
190 ext->static_txt = 0;
191 ext->option_cnt = 0;
192
193 lv_obj_invalidate(ddlist);
194 }
195
196 /**
197 * Set the options in a drop down list from a string
198 * @param ddlist pointer to drop down list object
199 * @param options a string with '\n' separated options. E.g. "One\nTwo\nThree"
200 * The options string can be destroyed after calling this function
201 */
lv_dropdown_set_options(lv_obj_t * ddlist,const char * options)202 void lv_dropdown_set_options(lv_obj_t * ddlist, const char * options)
203 {
204 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
205 LV_ASSERT_STR(options);
206
207 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
208
209 /*Count the '\n'-s to determine the number of options*/
210 ext->option_cnt = 0;
211 uint32_t i;
212 for(i = 0; options[i] != '\0'; i++) {
213 if(options[i] == '\n') ext->option_cnt++;
214 }
215 ext->option_cnt++; /*Last option has no `\n`*/
216 ext->sel_opt_id = 0;
217 ext->sel_opt_id_orig = 0;
218
219 /*Allocate space for the new text*/
220 size_t len = strlen(options) + 1;
221 if(ext->options != NULL && ext->static_txt == 0) {
222 lv_mem_free(ext->options);
223 ext->options = NULL;
224 }
225
226 ext->options = lv_mem_alloc(len);
227
228 LV_ASSERT_MEM(ext->options);
229 if(ext->options == NULL) return;
230
231 strcpy(ext->options, options);
232
233 /*Now the text is dynamically allocated*/
234 ext->static_txt = 0;
235 }
236
237 /**
238 * Set the options in a drop down list from a string
239 * @param ddlist pointer to drop down list object
240 * @param options a static string with '\n' separated options. E.g. "One\nTwo\nThree"
241 */
lv_dropdown_set_options_static(lv_obj_t * ddlist,const char * options)242 void lv_dropdown_set_options_static(lv_obj_t * ddlist, const char * options)
243 {
244 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
245 LV_ASSERT_STR(options);
246
247 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
248
249 /*Count the '\n'-s to determine the number of options*/
250 ext->option_cnt = 0;
251 uint32_t i;
252 for(i = 0; options[i] != '\0'; i++) {
253 if(options[i] == '\n') ext->option_cnt++;
254 }
255 ext->option_cnt++; /*Last option has no `\n`*/
256 ext->sel_opt_id = 0;
257 ext->sel_opt_id_orig = 0;
258
259 if(ext->static_txt == 0 && ext->options != NULL) {
260 lv_mem_free(ext->options);
261 ext->options = NULL;
262 }
263
264 ext->static_txt = 1;
265 ext->options = (char *)options;
266 }
267
268 /**
269 * Add an options to a drop down list from a string. Only works for dynamic options.
270 * @param ddlist pointer to drop down list object
271 * @param option a string without '\n'. E.g. "Four"
272 * @param pos the insert position, indexed from 0, LV_DROPDOWN_POS_LAST = end of string
273 */
lv_dropdown_add_option(lv_obj_t * ddlist,const char * option,uint32_t pos)274 void lv_dropdown_add_option(lv_obj_t * ddlist, const char * option, uint32_t pos)
275 {
276 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
277 LV_ASSERT_STR(option);
278
279 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
280
281 /*Convert static options to dynamic*/
282 if(ext->static_txt != 0) {
283 char * static_options = ext->options;
284 size_t len = strlen(static_options) + 1;
285
286 ext->options = lv_mem_alloc(len);
287 LV_ASSERT_MEM(ext->options);
288 if(ext->options == NULL) return;
289
290 strcpy(ext->options, static_options);
291 ext->static_txt = 0;
292 }
293
294 /*Allocate space for the new option*/
295 size_t old_len = (ext->options == NULL) ? 0 : strlen(ext->options);
296 size_t ins_len = strlen(option);
297 size_t new_len = ins_len + old_len + 2; /* +2 for terminating NULL and possible \n */
298 ext->options = lv_mem_realloc(ext->options, new_len + 1);
299 LV_ASSERT_MEM(ext->options);
300 if(ext->options == NULL) return;
301
302 ext->options[old_len] = 0;
303
304 /*Find the insert character position*/
305 uint32_t insert_pos = old_len;
306 if(pos != LV_DROPDOWN_POS_LAST) {
307 uint32_t opcnt = 0;
308 for(insert_pos = 0; ext->options[insert_pos] != 0; insert_pos++) {
309 if(opcnt == pos)
310 break;
311 if(ext->options[insert_pos] == '\n')
312 opcnt++;
313 }
314 }
315
316 /*Add delimiter to existing options*/
317 if((insert_pos > 0) && (pos >= ext->option_cnt))
318 _lv_txt_ins(ext->options, _lv_txt_encoded_get_char_id(ext->options, insert_pos++), "\n");
319
320 /*Insert the new option, adding \n if necessary*/
321 char * ins_buf = _lv_mem_buf_get(ins_len + 2); /* + 2 for terminating NULL and possible \n */
322 LV_ASSERT_MEM(ins_buf);
323 if(ins_buf == NULL) return;
324 strcpy(ins_buf, option);
325 if(pos < ext->option_cnt)
326 strcat(ins_buf, "\n");
327 _lv_txt_ins(ext->options, _lv_txt_encoded_get_char_id(ext->options, insert_pos), ins_buf);
328 _lv_mem_buf_release(ins_buf);
329
330 ext->option_cnt++;
331
332 lv_obj_invalidate(ddlist);
333 }
334
335 /**
336 * Set the selected option
337 * @param ddlist pointer to drop down list object
338 * @param sel_opt id of the selected option (0 ... number of option - 1);
339 */
lv_dropdown_set_selected(lv_obj_t * ddlist,uint16_t sel_opt)340 void lv_dropdown_set_selected(lv_obj_t * ddlist, uint16_t sel_opt)
341 {
342 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
343
344 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
345 if(ext->sel_opt_id == sel_opt) return;
346
347 ext->sel_opt_id = sel_opt < ext->option_cnt ? sel_opt : ext->option_cnt - 1;
348 ext->sel_opt_id_orig = ext->sel_opt_id;
349 /*Move the list to show the current option*/
350 if(ext->page != NULL) {
351 lv_obj_invalidate(ddlist);
352 }
353 }
354
355 /**
356 * Set the direction of the a drop down list
357 * @param ddlist pointer to a drop down list object
358 * @param dir LV_DROPDOWN_DIR_LEF/RIGHT/TOP/BOTTOM
359 */
lv_dropdown_set_dir(lv_obj_t * ddlist,lv_dropdown_dir_t dir)360 void lv_dropdown_set_dir(lv_obj_t * ddlist, lv_dropdown_dir_t dir)
361 {
362 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
363
364 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
365 if(ext->dir == dir) return;
366
367 ext->dir = dir;
368
369 lv_obj_invalidate(ddlist);
370 }
371
372 /**
373 * Set the maximal height for the drop down list
374 * @param ddlist pointer to a drop down list
375 * @param h the maximal height
376 */
lv_dropdown_set_max_height(lv_obj_t * ddlist,lv_coord_t h)377 void lv_dropdown_set_max_height(lv_obj_t * ddlist, lv_coord_t h)
378 {
379 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
380
381 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
382 if(ext->max_height == h) return;
383
384 ext->max_height = h;
385
386 if(ext->page) {
387 if(h == 0) {
388 lv_cont_set_fit(ext->page, LV_FIT_TIGHT);
389 }
390 else {
391 lv_cont_set_fit2(ext->page, LV_FIT_TIGHT, LV_FIT_NONE);
392 lv_obj_set_height(ext->page, h);
393 }
394 }
395 }
396
397 /**
398 * Set an arrow or other symbol to display when the drop-down list is closed.
399 * @param ddlist pointer to drop down list object
400 * @param symbol a text like `LV_SYMBOL_DOWN` or NULL to not draw icon
401 */
lv_dropdown_set_symbol(lv_obj_t * ddlist,const char * symbol)402 void lv_dropdown_set_symbol(lv_obj_t * ddlist, const char * symbol)
403 {
404 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
405
406 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
407 ext->symbol = symbol;
408 lv_obj_invalidate(ddlist);
409 }
410
411 /**
412 * Set whether the ddlist highlight the last selected option and display its text or not
413 * @param ddlist pointer to a drop down list object
414 * @param show true/false
415 */
lv_dropdown_set_show_selected(lv_obj_t * ddlist,bool show)416 void lv_dropdown_set_show_selected(lv_obj_t * ddlist, bool show)
417 {
418 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
419
420 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
421 if(ext->show_selected == show) return;
422
423 ext->show_selected = show;
424
425 lv_obj_invalidate(ddlist);
426 }
427
428 /*=====================
429 * Getter functions
430 *====================*/
431
432 /**
433 * Get text of the ddlist (Displayed on the button if `show_selected = false`)
434 * @param ddlist pointer to a drop down list object
435 * @return the text string
436 */
lv_dropdown_get_text(lv_obj_t * ddlist)437 const char * lv_dropdown_get_text(lv_obj_t * ddlist)
438 {
439 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
440 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
441
442 return ext->text;
443 }
444
445 /**
446 * Get the options of a drop down list
447 * @param ddlist pointer to drop down list object
448 * @return the options separated by '\n'-s (E.g. "Option1\nOption2\nOption3")
449 */
lv_dropdown_get_options(const lv_obj_t * ddlist)450 const char * lv_dropdown_get_options(const lv_obj_t * ddlist)
451 {
452 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
453
454 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
455 return ext->options;
456 }
457
458 /**
459 * Get the selected option
460 * @param ddlist pointer to drop down list object
461 * @return id of the selected option (0 ... number of option - 1);
462 */
lv_dropdown_get_selected(const lv_obj_t * ddlist)463 uint16_t lv_dropdown_get_selected(const lv_obj_t * ddlist)
464 {
465 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
466
467 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
468
469 return ext->sel_opt_id;
470 }
471
472 /**
473 * Get the total number of options
474 * @param ddlist pointer to drop down list object
475 * @return the total number of options in the list
476 */
lv_dropdown_get_option_cnt(const lv_obj_t * ddlist)477 uint16_t lv_dropdown_get_option_cnt(const lv_obj_t * ddlist)
478 {
479 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
480
481 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
482
483 return ext->option_cnt;
484 }
485
486 /**
487 * Get the current selected option as a string
488 * @param ddlist pointer to ddlist object
489 * @param buf pointer to an array to store the string
490 * @param buf_size size of `buf` in bytes. 0: to ignore it.
491 */
lv_dropdown_get_selected_str(const lv_obj_t * ddlist,char * buf,uint32_t buf_size)492 void lv_dropdown_get_selected_str(const lv_obj_t * ddlist, char * buf, uint32_t buf_size)
493 {
494 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
495
496 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
497
498 uint32_t i;
499 uint32_t line = 0;
500 size_t txt_len = strlen(ext->options);
501
502 for(i = 0; i < txt_len && line != ext->sel_opt_id_orig; i++) {
503 if(ext->options[i] == '\n') line++;
504 }
505
506 uint32_t c;
507 for(c = 0; i < txt_len && ext->options[i] != '\n'; c++, i++) {
508 if(buf_size && c >= buf_size - 1) {
509 LV_LOG_WARN("lv_dropdown_get_selected_str: the buffer was too small")
510 break;
511 }
512 buf[c] = ext->options[i];
513 }
514
515 buf[c] = '\0';
516 }
517
518 /**
519 * Get the fix height value.
520 * @param ddlist pointer to a drop down list object
521 * @return the height if the ddlist is opened (0: auto size)
522 */
lv_dropdown_get_max_height(const lv_obj_t * ddlist)523 lv_coord_t lv_dropdown_get_max_height(const lv_obj_t * ddlist)
524 {
525 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
526
527 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
528 return ext->max_height;
529 }
530
531 /**
532 * Get the symbol to draw when the drop-down list is closed
533 * @param ddlist pointer to drop down list object
534 * @return the symbol or NULL if not enabled
535 */
lv_dropdown_get_symbol(lv_obj_t * ddlist)536 const char * lv_dropdown_get_symbol(lv_obj_t * ddlist)
537 {
538 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
539
540 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
541
542 return ext->symbol;
543 }
544
545 /**
546 * Get the direction of the drop down list
547 * @param ddlist pointer to a drop down list object
548 * @return LV_DROPDOWN_DIR_LEF/RIGHT/TOP/BOTTOM
549 */
lv_dropdown_get_dir(const lv_obj_t * ddlist)550 lv_dropdown_dir_t lv_dropdown_get_dir(const lv_obj_t * ddlist)
551 {
552 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
553
554 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
555
556 return ext->dir;
557 }
558
559 /**
560 * Get whether the ddlist highlight the last selected option and display its text or not
561 * @param ddlist pointer to a drop down list object
562 * @return true/false
563 */
lv_dropdown_get_show_selected(lv_obj_t * ddlist)564 bool lv_dropdown_get_show_selected(lv_obj_t * ddlist)
565 {
566 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
567
568 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
569
570 return ext->show_selected ? true : false;
571
572 }
573
574 /*=====================
575 * Other functions
576 *====================*/
577
578 /**
579 * Open the drop down list with or without animation
580 * @param ddlist pointer to drop down list object
581 */
lv_dropdown_open(lv_obj_t * ddlist)582 void lv_dropdown_open(lv_obj_t * ddlist)
583 {
584 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
585 if(ext->page) return;
586
587 ext->page = lv_page_create(lv_obj_get_screen(ddlist), NULL);
588 lv_obj_add_protect(ext->page, LV_PROTECT_POS | LV_PROTECT_CLICK_FOCUS);
589 lv_obj_add_protect(lv_page_get_scrollable(ext->page), LV_PROTECT_CLICK_FOCUS);
590
591 lv_obj_set_base_dir(ext->page, lv_obj_get_base_dir(ddlist));
592
593 if(ancestor_page_signal == NULL) ancestor_page_signal = lv_obj_get_signal_cb(ext->page);
594 if(ancestor_page_scrl_signal == NULL) ancestor_page_scrl_signal = lv_obj_get_signal_cb(lv_page_get_scrollable(
595 ext->page));
596 if(ancestor_page_design == NULL) ancestor_page_design = lv_obj_get_design_cb(ext->page);
597
598 lv_dropdown_page_ext_t * page_ext = lv_obj_allocate_ext_attr(ext->page, sizeof(lv_dropdown_page_ext_t));
599 LV_ASSERT_MEM(page_ext);
600 if(page_ext == NULL) {
601 lv_obj_del(ext->page);
602 ext->page = NULL;
603 return;
604 }
605 page_ext->ddlist = ddlist;
606
607 lv_obj_set_design_cb(ext->page, lv_dropdown_page_design);
608 lv_obj_set_signal_cb(ext->page, lv_dropdown_page_signal);
609 lv_obj_set_signal_cb(lv_page_get_scrollable(ext->page), lv_dropdown_page_scrl_signal);
610
611 lv_style_list_copy(lv_obj_get_style_list(ext->page, LV_PAGE_PART_BG), &ext->style_page);
612 lv_style_list_copy(lv_obj_get_style_list(ext->page, LV_PAGE_PART_SCROLLBAR), &ext->style_scrlbar);
613 lv_obj_clean_style_list(ext->page, LV_PAGE_PART_SCROLLABLE);
614 lv_obj_refresh_style(ext->page, LV_OBJ_PART_ALL, LV_STYLE_PROP_ALL);
615
616 lv_obj_t * label = lv_label_create(ext->page, NULL);
617 lv_label_set_text_static(label, ext->options);
618
619 lv_cont_set_fit2(ext->page, LV_FIT_TIGHT, LV_FIT_NONE);
620 /*Set small width to the width of the button*/
621 if(lv_obj_get_width(ext->page) < lv_obj_get_width(ddlist) &&
622 (ext->dir == LV_DROPDOWN_DIR_UP || ext->dir == LV_DROPDOWN_DIR_DOWN)) {
623 lv_cont_set_fit2(ext->page, LV_FIT_NONE, LV_FIT_NONE);
624 lv_obj_set_width(ext->page, lv_obj_get_width(ddlist));
625 }
626
627 lv_coord_t label_h = lv_obj_get_height(label);
628 lv_style_int_t top = lv_obj_get_style_pad_top(ddlist, LV_DROPDOWN_PART_LIST);
629 lv_style_int_t bottom = lv_obj_get_style_pad_bottom(ddlist, LV_DROPDOWN_PART_LIST);
630
631 lv_coord_t list_fit_h = label_h + top + bottom;
632 lv_coord_t list_h = list_fit_h;
633 if(list_h > ext->max_height) list_h = ext->max_height;
634
635 lv_dropdown_dir_t dir = ext->dir;
636 /*No place on the bottom? See if top is better.*/
637 if(ext->dir == LV_DROPDOWN_DIR_DOWN) {
638 if(ddlist->coords.y2 + list_h > LV_VER_RES) {
639 if(ddlist->coords.y1 > LV_VER_RES - ddlist->coords.y2) {
640 /*There is more space on the top, so make it drop up*/
641 dir = LV_DROPDOWN_DIR_UP;
642 list_h = ddlist->coords.y1;
643 }
644 else {
645 list_h = LV_VER_RES - ddlist->coords.y2;
646 }
647 }
648 }
649 /*No place on the top? See if bottom is better.*/
650 else if(ext->dir == LV_DROPDOWN_DIR_UP) {
651 if(ddlist->coords.y1 - list_h < 0) {
652 if(ddlist->coords.y1 < LV_VER_RES - ddlist->coords.y2) {
653 /*There is more space on the top, so make it drop up*/
654 dir = LV_DROPDOWN_DIR_DOWN;
655 list_h = LV_VER_RES - ddlist->coords.y2;
656 }
657 else {
658 list_h = ddlist->coords.y1;
659 }
660 }
661 }
662
663 if(list_h > list_fit_h) list_h = list_fit_h;
664 if(list_h > ext->max_height) list_h = ext->max_height;
665
666 lv_obj_set_height(ext->page, list_h);
667
668 position_to_selected(ddlist);
669
670 if(dir == LV_DROPDOWN_DIR_DOWN) lv_obj_align(ext->page, ddlist, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
671 else if(dir == LV_DROPDOWN_DIR_UP) lv_obj_align(ext->page, ddlist, LV_ALIGN_OUT_TOP_LEFT, 0, 0);
672 else if(dir == LV_DROPDOWN_DIR_LEFT) lv_obj_align(ext->page, ddlist, LV_ALIGN_OUT_LEFT_TOP, 0, 0);
673 else if(dir == LV_DROPDOWN_DIR_RIGHT)lv_obj_align(ext->page, ddlist, LV_ALIGN_OUT_RIGHT_TOP, 0, 0);
674
675 if(ext->dir == LV_DROPDOWN_DIR_LEFT || ext->dir == LV_DROPDOWN_DIR_RIGHT) {
676 if(ext->page->coords.y2 > LV_VER_RES) {
677 lv_obj_set_y(ext->page, lv_obj_get_y(ext->page) - (ext->page->coords.y2 - LV_VER_RES));
678 }
679 }
680
681 if(lv_label_get_align(label) == LV_LABEL_ALIGN_RIGHT) {
682 lv_obj_set_x(label, lv_obj_get_width_fit(ext->page) - lv_obj_get_width(label));
683 }
684 }
685
686 /**
687 * Close (Collapse) the drop down list
688 * @param ddlist pointer to drop down list object
689 */
lv_dropdown_close(lv_obj_t * ddlist)690 void lv_dropdown_close(lv_obj_t * ddlist)
691 {
692 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
693 if(ext->page == NULL) return;
694
695 ext->pr_opt_id = LV_DROPDOWN_PR_NONE;
696 lv_obj_del(ext->page);
697 ext->page = NULL;
698 }
699
700 /**********************
701 * STATIC FUNCTIONS
702 **********************/
703
704 /**
705 * Handle the drawing related tasks of the drop down list
706 * @param ddlist pointer to an object
707 * @param clip_area the object will be drawn only in this area
708 * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
709 * (return 'true' if yes)
710 * LV_DESIGN_DRAW: draw the object (always return 'true')
711 * LV_DESIGN_DRAW_POST: drawing after every children are drawn
712 * @param return an element of `lv_design_res_t`
713 */
lv_dropdown_design(lv_obj_t * ddlist,const lv_area_t * clip_area,lv_design_mode_t mode)714 static lv_design_res_t lv_dropdown_design(lv_obj_t * ddlist, const lv_area_t * clip_area, lv_design_mode_t mode)
715 {
716 /*Return false if the object is not covers the mask_p area*/
717 if(mode == LV_DESIGN_COVER_CHK) {
718 return ancestor_design(ddlist, clip_area, mode);
719 }
720 /*Draw the object*/
721 else if(mode == LV_DESIGN_DRAW_MAIN) {
722 ancestor_design(ddlist, clip_area, mode);
723
724 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
725
726 lv_style_int_t left = lv_obj_get_style_pad_left(ddlist, LV_DROPDOWN_PART_MAIN);
727 lv_style_int_t right = lv_obj_get_style_pad_right(ddlist, LV_DROPDOWN_PART_MAIN);
728 lv_style_int_t top = lv_obj_get_style_pad_top(ddlist, LV_DROPDOWN_PART_MAIN);
729
730 lv_draw_label_dsc_t label_dsc;
731 lv_draw_label_dsc_init(&label_dsc);
732 lv_obj_init_draw_label_dsc(ddlist, LV_DROPDOWN_PART_MAIN, &label_dsc);
733
734 lv_area_t txt_area;
735 lv_point_t txt_size;
736
737 const char * opt_txt = ext->text;
738 if(ext->show_selected) {
739 char * buf = _lv_mem_buf_get(128);
740 lv_dropdown_get_selected_str(ddlist, buf, 128);
741 opt_txt = buf;
742 }
743
744 const char * txt;
745
746 bool rev = false;
747 if(ext->dir == LV_DROPDOWN_DIR_LEFT) rev = true;
748 if(lv_obj_get_base_dir(ddlist) == LV_BIDI_DIR_RTL) rev = true;
749
750 txt = rev ? ext->symbol : opt_txt;
751 if(txt) {
752 _lv_txt_get_size(&txt_size, txt, label_dsc.font, label_dsc.letter_space, label_dsc.line_space, LV_COORD_MAX,
753 label_dsc.flag);
754
755 txt_area.y1 = ddlist->coords.y1 + top;
756 txt_area.y2 = txt_area.y1 + txt_size.y;
757
758 /*Center align the text if no symbol*/
759 if(ext->symbol == NULL && txt == opt_txt) {
760 txt_area.x1 = ddlist->coords.x1 + (lv_obj_get_width(ddlist) - txt_size.x) / 2;
761 txt_area.x2 = txt_area.x1 + txt_size.x;
762 }
763 else {
764 txt_area.x1 = ddlist->coords.x1 + left;
765 txt_area.x2 = txt_area.x1 + txt_size.x;
766 }
767 lv_draw_label(&txt_area, clip_area, &label_dsc, txt, NULL);
768 }
769
770 txt = rev ? opt_txt : ext->symbol;
771 if(txt) {
772 _lv_txt_get_size(&txt_size, txt, label_dsc.font, label_dsc.letter_space, label_dsc.line_space, LV_COORD_MAX,
773 label_dsc.flag);
774 txt_area.y1 = ddlist->coords.y1 + top;
775 txt_area.y2 = txt_area.y1 + txt_size.y;
776
777 /*Center align the text if no symbol*/
778 if(ext->symbol == NULL && txt == opt_txt) {
779 txt_area.x1 = ddlist->coords.x1 + (lv_obj_get_width(ddlist) - txt_size.x) / 2;
780 txt_area.x2 = txt_area.x1 + txt_size.x;
781 }
782 else {
783 txt_area.x1 = ddlist->coords.x2 - right - txt_size.x;
784 txt_area.x2 = txt_area.x1 + txt_size.x;
785 }
786
787 lv_draw_label(&txt_area, clip_area, &label_dsc, txt, NULL);
788 }
789
790 if(ext->show_selected) {
791 _lv_mem_buf_release((char *)opt_txt);
792 }
793
794 }
795 else if(mode == LV_DESIGN_DRAW_POST) {
796 ancestor_design(ddlist, clip_area, mode);
797 }
798
799 return LV_DESIGN_RES_OK;
800 }
801
802 /**
803 * Handle the drawing related tasks of the drop down list's page
804 * @param page pointer to an object
805 * @param clip_area the object will be drawn only in this area
806 * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
807 * (return 'true' if yes)
808 * LV_DESIGN_DRAW: draw the object (always return 'true')
809 * LV_DESIGN_DRAW_POST: drawing after every children are drawn
810 * @param return an element of `lv_design_res_t`
811 */
lv_dropdown_page_design(lv_obj_t * page,const lv_area_t * clip_area,lv_design_mode_t mode)812 static lv_design_res_t lv_dropdown_page_design(lv_obj_t * page, const lv_area_t * clip_area, lv_design_mode_t mode)
813 {
814 /*Return false if the object is not covers the mask_p area*/
815 if(mode == LV_DESIGN_COVER_CHK) {
816 return ancestor_page_design(page, clip_area, mode);
817 }
818 /*Draw the object*/
819 else if(mode == LV_DESIGN_DRAW_MAIN) {
820 ancestor_page_design(page, clip_area, mode);
821
822 lv_dropdown_page_ext_t * page_ext = lv_obj_get_ext_attr(page);
823 lv_obj_t * ddlist = page_ext->ddlist;
824 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
825
826 /*Draw the boxes if the page is not being deleted*/
827 if(ext->page) {
828 /* Clip area might be too large too to shadow but
829 * the selected option can be drawn on only the background*/
830 lv_area_t clip_area_core;
831 bool has_common;
832 has_common = _lv_area_intersect(&clip_area_core, clip_area, &ext->page->coords);
833 if(has_common) {
834 if(ext->pr_opt_id != LV_DROPDOWN_PR_NONE) {
835 draw_box(ddlist, &clip_area_core, ext->pr_opt_id, LV_STATE_PRESSED);
836 }
837
838 draw_box(ddlist, &clip_area_core, ext->sel_opt_id, LV_STATE_DEFAULT);
839 }
840 }
841 }
842 /*Post draw when the children are drawn*/
843 else if(mode == LV_DESIGN_DRAW_POST) {
844 /*Draw the scrollbar in the ancestor page design function*/
845 ancestor_page_design(page, clip_area, mode);
846
847 /*Redraw the text on the selected area with a different color*/
848 lv_dropdown_page_ext_t * page_ext = lv_obj_get_ext_attr(page);
849 lv_obj_t * ddlist = page_ext->ddlist;
850 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
851
852 /*Draw the box labels if the page is not being deleted*/
853 if(ext->page) {
854 /* Clip area might be too large too to shadow but
855 * the selected option can be drawn on only the background*/
856 lv_area_t clip_area_core;
857 bool has_common;
858 has_common = _lv_area_intersect(&clip_area_core, clip_area, &ext->page->coords);
859 if(has_common) {
860 if(ext->pr_opt_id != LV_DROPDOWN_PR_NONE) {
861 draw_box_label(ddlist, &clip_area_core, ext->pr_opt_id, LV_STATE_PRESSED);
862 }
863
864 draw_box_label(ddlist, &clip_area_core, ext->sel_opt_id, LV_STATE_DEFAULT);
865 }
866 }
867 }
868
869 return LV_DESIGN_RES_OK;
870 }
871
872 /**
873 * Signal function of the drop down list
874 * @param ddlist pointer to a drop down list object
875 * @param sign a signal type from lv_signal_t enum
876 * @param param pointer to a signal specific variable
877 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
878 */
lv_dropdown_signal(lv_obj_t * ddlist,lv_signal_t sign,void * param)879 static lv_res_t lv_dropdown_signal(lv_obj_t * ddlist, lv_signal_t sign, void * param)
880 {
881 lv_res_t res;
882
883 /* Include the ancient signal function */
884 res = ancestor_signal(ddlist, sign, param);
885 if(res != LV_RES_OK) return res;
886 if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
887
888 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
889
890 if(sign == LV_SIGNAL_GET_STYLE) {
891 lv_get_style_info_t * info = param;
892 info->result = lv_dropdown_get_style(ddlist, info->part);
893 if(info->result != NULL) return LV_RES_OK;
894 return LV_RES_OK;
895 }
896 else if(sign == LV_SIGNAL_GET_STATE_DSC) {
897 lv_get_state_info_t * info = param;
898 if(info->part == LV_DROPDOWN_PART_LIST ||
899 info->part == LV_DROPDOWN_PART_SCROLLBAR ||
900 info->part == LV_DROPDOWN_PART_SELECTED) {
901 info->result = lv_obj_get_state(ext->page, LV_PAGE_PART_BG);
902 }
903 }
904 else if(sign == LV_SIGNAL_CLEANUP) {
905 lv_dropdown_close(ddlist);
906 if(ext->static_txt == 0) {
907 lv_mem_free(ext->options);
908 ext->options = NULL;
909 }
910
911 /*`lv_obj_clean_style_list` is not required because these styles are only copied to the page
912 * so they can have transitions or other object related things. */
913 _lv_style_list_reset(&ext->style_page);
914 _lv_style_list_reset(&ext->style_scrlbar);
915 _lv_style_list_reset(&ext->style_selected);
916
917 }
918 else if(sign == LV_SIGNAL_FOCUS) {
919 #if LV_USE_GROUP
920 lv_group_t * g = lv_obj_get_group(ddlist);
921 bool editing = lv_group_get_editing(g);
922 lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
923
924 /*Encoders need special handling*/
925 if(indev_type == LV_INDEV_TYPE_ENCODER) {
926 /*Open the list if editing*/
927 if(editing) lv_dropdown_open(ddlist);
928 /*Close the list if navigating*/
929 else
930 lv_dropdown_close(ddlist);
931 }
932 #endif
933 }
934 else if(sign == LV_SIGNAL_DEFOCUS || sign == LV_SIGNAL_LEAVE) {
935 lv_dropdown_close(ddlist);
936 }
937 else if(sign == LV_SIGNAL_RELEASED) {
938 lv_indev_t * indev = lv_indev_get_act();
939 if(lv_indev_is_dragging(indev) == false) {
940 if(ext->page) {
941 lv_dropdown_close(ddlist);
942 if(ext->sel_opt_id_orig != ext->sel_opt_id) {
943 ext->sel_opt_id_orig = ext->sel_opt_id;
944 uint32_t id = ext->sel_opt_id; /*Just to use uint32_t in event data*/
945 res = lv_event_send(ddlist, LV_EVENT_VALUE_CHANGED, &id);
946 if(res != LV_RES_OK) return res;
947 lv_obj_invalidate(ddlist);
948 }
949 #if LV_USE_GROUP
950 lv_indev_type_t indev_type = lv_indev_get_type(indev);
951 if(indev_type == LV_INDEV_TYPE_ENCODER) {
952 lv_group_set_editing(lv_obj_get_group(ddlist), false);
953 }
954 #endif
955 }
956 else {
957 lv_dropdown_open(ddlist);
958 }
959 }
960 else {
961 ext->sel_opt_id = ext->sel_opt_id_orig;
962 lv_obj_invalidate(ddlist);
963 }
964 }
965 else if(sign == LV_SIGNAL_COORD_CHG) {
966 if(ext->page) lv_dropdown_close(ddlist);
967 }
968 else if(sign == LV_SIGNAL_STYLE_CHG) {
969 lv_style_int_t top = lv_obj_get_style_pad_top(ddlist, LV_DROPDOWN_PART_MAIN);
970 lv_style_int_t bottom = lv_obj_get_style_pad_bottom(ddlist, LV_DROPDOWN_PART_MAIN);
971 const lv_font_t * font = lv_obj_get_style_text_font(ddlist, LV_DROPDOWN_PART_MAIN);
972 lv_obj_set_height(ddlist, top + bottom + lv_font_get_line_height(font));
973
974 if(ext->page) lv_obj_refresh_style(ext->page, LV_OBJ_PART_ALL, LV_STYLE_PROP_ALL);
975 }
976 else if(sign == LV_SIGNAL_CONTROL) {
977 #if LV_USE_GROUP
978 char c = *((char *)param);
979 if(c == LV_KEY_RIGHT || c == LV_KEY_DOWN) {
980 if(ext->page == NULL) {
981 lv_dropdown_open(ddlist);
982 }
983 else if(ext->sel_opt_id + 1 < ext->option_cnt) {
984 ext->sel_opt_id++;
985 position_to_selected(ddlist);
986 }
987 }
988 else if(c == LV_KEY_LEFT || c == LV_KEY_UP) {
989
990 if(ext->page == NULL) {
991 lv_dropdown_open(ddlist);
992 }
993 else if(ext->sel_opt_id > 0) {
994 ext->sel_opt_id--;
995 position_to_selected(ddlist);
996 }
997 }
998 else if(c == LV_KEY_ESC) {
999 ext->sel_opt_id = ext->sel_opt_id_orig;
1000 lv_dropdown_close(ddlist);
1001 }
1002 #endif
1003 }
1004 else if(sign == LV_SIGNAL_GET_EDITABLE) {
1005 #if LV_USE_GROUP
1006 bool * editable = (bool *)param;
1007 *editable = true;
1008 #endif
1009 }
1010
1011 return res;
1012 }
1013
1014 /**
1015 * Signal function of the drop down list's scrollable part
1016 * @param scrl pointer to a drop down list's scrollable part
1017 * @param sign a signal type from lv_signal_t enum
1018 * @param param pointer to a signal specific variable
1019 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
1020 */
lv_dropdown_page_signal(lv_obj_t * page,lv_signal_t sign,void * param)1021 static lv_res_t lv_dropdown_page_signal(lv_obj_t * page, lv_signal_t sign, void * param)
1022 {
1023 lv_res_t res;
1024
1025 /* Include the ancient signal function */
1026 res = ancestor_page_signal(page, sign, param);
1027 if(res != LV_RES_OK) return res;
1028 if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, "");
1029
1030 lv_dropdown_page_ext_t * page_ext = lv_obj_get_ext_attr(page);
1031 lv_obj_t * ddlist = page_ext->ddlist;
1032
1033 if(sign == LV_SIGNAL_REFR_EXT_DRAW_PAD) {
1034 /* Make possible to draw on the full width of the background to redraw the selected rectangle
1035 * when the ddlist is scrolled in fix height mode.
1036 * (The scrollable is scrolled then "select rectangle" is drawn on the bg too)*/
1037 lv_style_int_t left = lv_obj_get_style_pad_left(ddlist, LV_DROPDOWN_PART_LIST);
1038 lv_style_int_t right = lv_obj_get_style_pad_right(ddlist, LV_DROPDOWN_PART_LIST);
1039 lv_obj_t * scrl = lv_page_get_scrollable(page);
1040 scrl->ext_draw_pad = LV_MATH_MAX3(scrl->ext_draw_pad, left, right);
1041 }
1042 else if(sign == LV_SIGNAL_RELEASED) {
1043 if(lv_indev_is_dragging(lv_indev_get_act()) == false) {
1044 page_release_handler(page);
1045 }
1046 }
1047 else if(sign == LV_SIGNAL_PRESSED) {
1048 page_press_handler(page);
1049 }
1050 else if(sign == LV_SIGNAL_CLEANUP) {
1051 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
1052 ext->page = NULL; /*The page is just being deleted*/
1053 }
1054
1055 return res;
1056 }
1057
1058 /**
1059 * Signal function of the drop down list's scrollable part
1060 * @param scrl pointer to a drop down list's scrollable part
1061 * @param sign a signal type from lv_signal_t enum
1062 * @param param pointer to a signal specific variable
1063 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
1064 */
lv_dropdown_page_scrl_signal(lv_obj_t * scrl,lv_signal_t sign,void * param)1065 static lv_res_t lv_dropdown_page_scrl_signal(lv_obj_t * scrl, lv_signal_t sign, void * param)
1066 {
1067 lv_res_t res;
1068
1069 /* Include the ancient signal function */
1070 res = ancestor_page_scrl_signal(scrl, sign, param);
1071 if(res != LV_RES_OK) return res;
1072 if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, "");
1073
1074 lv_obj_t * page = lv_obj_get_parent(scrl);
1075 lv_dropdown_page_ext_t * page_ext = lv_obj_get_ext_attr(page);
1076 lv_obj_t * ddlist = page_ext->ddlist;
1077 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
1078
1079 if(sign == LV_SIGNAL_RELEASED) {
1080 if(lv_indev_is_dragging(lv_indev_get_act()) == false) {
1081 page_release_handler(page);
1082 }
1083 }
1084 else if(sign == LV_SIGNAL_PRESSED) {
1085 page_press_handler(page);
1086 }
1087 else if(sign == LV_SIGNAL_DRAG_BEGIN) {
1088 ext->pr_opt_id = LV_DROPDOWN_PR_NONE;
1089 lv_obj_invalidate(page);
1090 }
1091 else if(sign == LV_SIGNAL_REFR_EXT_DRAW_PAD) {
1092 /* Make possible to draw on the full width of the background to redraw the selected rectangle
1093 * when the ddlist is scrolled in fix height mode.
1094 * (The scrollable is scrolled then "select rectangle" is drawn on the bg too)*/
1095 lv_style_int_t left = lv_obj_get_style_pad_left(ddlist, LV_DROPDOWN_PART_LIST);
1096 lv_style_int_t right = lv_obj_get_style_pad_right(ddlist, LV_DROPDOWN_PART_LIST);
1097 scrl->ext_draw_pad = LV_MATH_MAX3(scrl->ext_draw_pad, left, right);
1098 }
1099
1100 return res;
1101 }
1102
1103
1104 /**
1105 * Get the style descriptor of a part of the object
1106 * @param page pointer the object
1107 * @param part the part from `lv_dropdown_part_t`. (LV_DROPDOWN_PART_...)
1108 * @return pointer to the style descriptor of the specified part
1109 */
lv_dropdown_get_style(lv_obj_t * ddlist,uint8_t part)1110 static lv_style_list_t * lv_dropdown_get_style(lv_obj_t * ddlist, uint8_t part)
1111 {
1112 LV_ASSERT_OBJ(ddlist, LV_OBJX_NAME);
1113
1114 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
1115 lv_style_list_t * style_dsc_p;
1116
1117 switch(part) {
1118 case LV_DROPDOWN_PART_MAIN:
1119 style_dsc_p = &ddlist->style_list;
1120 break;
1121 case LV_DROPDOWN_PART_LIST:
1122 style_dsc_p = &ext->style_page;
1123 break;
1124 case LV_DROPDOWN_PART_SCROLLBAR:
1125 style_dsc_p = &ext->style_scrlbar;
1126 break;
1127 case LV_DROPDOWN_PART_SELECTED:
1128 style_dsc_p = &ext->style_selected;
1129 break;
1130 default:
1131 style_dsc_p = NULL;
1132 }
1133
1134 return style_dsc_p;
1135 }
1136
draw_box(lv_obj_t * ddlist,const lv_area_t * clip_area,uint16_t id,lv_state_t state)1137 static void draw_box(lv_obj_t * ddlist, const lv_area_t * clip_area, uint16_t id, lv_state_t state)
1138 {
1139 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
1140 lv_obj_t * page = ext->page;
1141 lv_state_t state_orig = page->state;
1142
1143 if(state != page->state) {
1144 _lv_obj_disable_style_caching(ddlist, true);
1145 page->state = state;
1146 }
1147
1148 /*Draw a rectangle under the selected item*/
1149 const lv_font_t * font = lv_obj_get_style_text_font(ddlist, LV_DROPDOWN_PART_LIST);
1150 lv_style_int_t line_space = lv_obj_get_style_text_line_space(ddlist, LV_DROPDOWN_PART_LIST);
1151 lv_coord_t font_h = lv_font_get_line_height(font);
1152
1153 /*Draw the selected*/
1154 lv_obj_t * label = get_label(ddlist);
1155 lv_area_t rect_area;
1156 rect_area.y1 = label->coords.y1;
1157 rect_area.y1 += id * (font_h + line_space);
1158 rect_area.y1 -= line_space / 2;
1159
1160 rect_area.y2 = rect_area.y1 + font_h + line_space - 1;
1161 rect_area.x1 = ext->page->coords.x1;
1162 rect_area.x2 = ext->page->coords.x2;
1163
1164 lv_draw_rect_dsc_t sel_rect;
1165 lv_draw_rect_dsc_init(&sel_rect);
1166 lv_obj_init_draw_rect_dsc(ddlist, LV_DROPDOWN_PART_SELECTED, &sel_rect);
1167 lv_draw_rect(&rect_area, clip_area, &sel_rect);
1168
1169 page->state = state_orig;
1170 _lv_obj_disable_style_caching(ddlist, false);
1171 }
1172
1173
1174
draw_box_label(lv_obj_t * ddlist,const lv_area_t * clip_area,uint16_t id,lv_state_t state)1175 static void draw_box_label(lv_obj_t * ddlist, const lv_area_t * clip_area, uint16_t id, lv_state_t state)
1176 {
1177 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
1178 lv_obj_t * page = ext->page;
1179 lv_state_t state_orig = page->state;
1180
1181 if(state != page->state) {
1182 page->state = state;
1183 _lv_obj_disable_style_caching(ddlist, true);
1184 }
1185
1186 lv_draw_label_dsc_t label_dsc;
1187 lv_draw_label_dsc_init(&label_dsc);
1188 lv_obj_init_draw_label_dsc(ddlist, LV_DROPDOWN_PART_SELECTED, &label_dsc);
1189
1190 label_dsc.line_space = lv_obj_get_style_text_line_space(ddlist,
1191 LV_DROPDOWN_PART_LIST); /*Line space should come from the page*/
1192
1193 lv_obj_t * label = get_label(ddlist);
1194 if(label == NULL) return;
1195
1196 lv_label_align_t align = lv_label_get_align(label);
1197
1198 if(align == LV_LABEL_ALIGN_CENTER) label_dsc.flag |= LV_TXT_FLAG_CENTER;
1199 else if(align == LV_LABEL_ALIGN_RIGHT) label_dsc.flag |= LV_TXT_FLAG_RIGHT;
1200
1201 lv_coord_t font_h = lv_font_get_line_height(label_dsc.font);
1202
1203 lv_area_t area_sel;
1204 area_sel.y1 = label->coords.y1;
1205 area_sel.y1 += id * (font_h + label_dsc.line_space);
1206 area_sel.y1 -= label_dsc.line_space / 2;
1207
1208 area_sel.y2 = area_sel.y1 + font_h + label_dsc.line_space - 1;
1209 area_sel.x1 = page->coords.x1;
1210 area_sel.x2 = page->coords.x2;
1211 lv_area_t mask_sel;
1212 bool area_ok;
1213 area_ok = _lv_area_intersect(&mask_sel, clip_area, &area_sel);
1214 if(area_ok) {
1215 lv_draw_label(&label->coords, &mask_sel, &label_dsc, lv_label_get_text(label), NULL);
1216 }
1217 page->state = state_orig;
1218 _lv_obj_disable_style_caching(ddlist, false);
1219 }
1220
1221 /**
1222 * Called when a drop down list is released to open it or set new option
1223 * @param page pointer to the drop down list's page
1224 * @return LV_RES_INV if the page is not being deleted in the user callback. Else LV_RES_OK
1225 */
page_release_handler(lv_obj_t * page)1226 static lv_res_t page_release_handler(lv_obj_t * page)
1227 {
1228 lv_dropdown_page_ext_t * page_ext = lv_obj_get_ext_attr(page);
1229 lv_obj_t * ddlist = page_ext->ddlist;
1230
1231 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
1232
1233 lv_indev_t * indev = lv_indev_get_act();
1234 #if LV_USE_GROUP
1235 /*Leave edit mode once a new item is selected*/
1236 if(lv_indev_get_type(indev) == LV_INDEV_TYPE_ENCODER) {
1237 ext->sel_opt_id_orig = ext->sel_opt_id;
1238 lv_group_t * g = lv_obj_get_group(ddlist);
1239 if(lv_group_get_editing(g)) {
1240 lv_group_set_editing(g, false);
1241 }
1242 }
1243 #endif
1244
1245 /*Search the clicked option (For KEYPAD and ENCODER the new value should be already set)*/
1246 if(lv_indev_get_type(indev) == LV_INDEV_TYPE_POINTER || lv_indev_get_type(indev) == LV_INDEV_TYPE_BUTTON) {
1247 lv_point_t p;
1248 lv_indev_get_point(indev, &p);
1249 ext->sel_opt_id = get_id_on_point(ddlist, p.x, p.y);
1250 ext->sel_opt_id_orig = ext->sel_opt_id;
1251 }
1252
1253 lv_dropdown_close(ddlist);
1254
1255 /*Invalidate to refresh the text*/
1256 if(ext->show_selected) lv_obj_invalidate(ddlist);
1257
1258 uint32_t id = ext->sel_opt_id; /*Just to use uint32_t in event data*/
1259 lv_res_t res = lv_event_send(ddlist, LV_EVENT_VALUE_CHANGED, &id);
1260 if(res != LV_RES_OK) return res;
1261 if(ext->page == NULL) return LV_RES_INV;
1262
1263 return LV_RES_OK;
1264 }
1265
page_press_handler(lv_obj_t * page)1266 static void page_press_handler(lv_obj_t * page)
1267 {
1268 lv_dropdown_page_ext_t * page_ext = lv_obj_get_ext_attr(page);
1269 lv_obj_t * ddlist = page_ext->ddlist;
1270
1271 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
1272
1273 lv_indev_t * indev = lv_indev_get_act();
1274 if(indev && (lv_indev_get_type(indev) == LV_INDEV_TYPE_POINTER || lv_indev_get_type(indev) == LV_INDEV_TYPE_BUTTON)) {
1275 lv_point_t p;
1276 lv_indev_get_point(indev, &p);
1277 ext->pr_opt_id = get_id_on_point(ddlist, p.x, p.y);
1278 lv_obj_invalidate(page);
1279 }
1280 }
1281
get_id_on_point(lv_obj_t * ddlist,lv_coord_t x,lv_coord_t y)1282 static uint16_t get_id_on_point(lv_obj_t * ddlist, lv_coord_t x, lv_coord_t y)
1283 {
1284 lv_obj_t * label = get_label(ddlist);
1285 if(label == NULL) return 0;
1286 x -= label->coords.x1;
1287 y -= label->coords.y1;
1288 uint32_t letter_i;
1289
1290 const char * txt = lv_label_get_text(label);
1291
1292 lv_point_t p = {x, y};
1293 letter_i = lv_label_get_letter_on(label, &p);
1294 uint32_t letter_i_byte_pos = _lv_txt_encoded_get_byte_id(txt, letter_i);
1295 uint16_t opt = 0;
1296 uint32_t i = 0;
1297 uint32_t i_prev = 0;
1298
1299 uint32_t letter_cnt = 0;
1300 for(letter_cnt = 0; letter_cnt < letter_i; letter_cnt++) {
1301 uint32_t letter = _lv_txt_encoded_next(txt, &i);
1302 /*Count the lines to reach the clicked letter. But ignore the last '\n' because it
1303 * still belongs to the clicked line*/
1304 if(letter == '\n' && i_prev != letter_i_byte_pos) opt++;
1305 i_prev = i;
1306 }
1307
1308 return opt;
1309 }
1310
1311 /**
1312 * Set the position of list when it is closed to show the selected item
1313 * @param ddlist pointer to a drop down list
1314 */
position_to_selected(lv_obj_t * ddlist)1315 static void position_to_selected(lv_obj_t * ddlist)
1316 {
1317 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
1318
1319 const lv_font_t * font = lv_obj_get_style_text_font(ddlist, LV_DROPDOWN_PART_LIST);
1320 lv_coord_t font_h = lv_font_get_line_height(font);
1321 lv_obj_t * scrl = lv_page_get_scrollable(ext->page);
1322 lv_obj_t * label = get_label(ddlist);
1323 if(label == NULL) return;
1324
1325 lv_coord_t h = lv_obj_get_height(ext->page);
1326 lv_style_int_t line_space = lv_obj_get_style_text_line_space(ddlist, LV_DROPDOWN_PART_LIST);
1327
1328 lv_coord_t line_y1 = ext->sel_opt_id * (font_h + line_space) + label->coords.y1 - scrl->coords.y1;
1329
1330 lv_obj_set_y(scrl, -line_y1 + (h - font_h) / 2);
1331 lv_obj_invalidate(ext->page);
1332 }
1333
1334
1335
get_label(const lv_obj_t * ddlist)1336 static lv_obj_t * get_label(const lv_obj_t * ddlist)
1337 {
1338 lv_dropdown_ext_t * ext = lv_obj_get_ext_attr(ddlist);
1339 if(ext->page == NULL) return NULL;
1340
1341 return lv_obj_get_child(lv_page_get_scrollable(ext->page), NULL);
1342 }
1343
1344 #endif
1345