1 /**
2  * @file lv_list.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_list.h"
10 #if LV_USE_LIST != 0
11 
12 #include "../lv_misc/lv_debug.h"
13 #include "../lv_core/lv_group.h"
14 #include "../lv_themes/lv_theme.h"
15 #include "../lv_misc/lv_anim.h"
16 #include "../lv_misc/lv_math.h"
17 
18 /*********************
19  *      DEFINES
20  *********************/
21 #define LV_OBJX_NAME "lv_list"
22 
23 #define LV_LIST_LAYOUT_DEF LV_LAYOUT_COLUMN_MID
24 
25 #if LV_USE_ANIMATION == 0
26     #undef LV_LIST_DEF_ANIM_TIME
27     #define LV_LIST_DEF_ANIM_TIME 0
28 #endif
29 
30 /**********************
31  *      TYPEDEFS
32  **********************/
33 
34 /**********************
35  *  STATIC PROTOTYPES
36  **********************/
37 static lv_res_t lv_list_signal(lv_obj_t * list, lv_signal_t sign, void * param);
38 static lv_res_t lv_list_btn_signal(lv_obj_t * btn, lv_signal_t sign, void * param);
39 static lv_style_list_t * lv_list_get_style(lv_obj_t * list, uint8_t part);
40 static bool lv_list_is_list_btn(lv_obj_t * list_btn);
41 static bool lv_list_is_list_img(lv_obj_t * list_btn);
42 static bool lv_list_is_list_label(lv_obj_t * list_btn);
43 
44 /**********************
45  *  STATIC VARIABLES
46  **********************/
47 #if LV_USE_IMG
48     static lv_signal_cb_t img_signal;
49 #endif
50 static lv_signal_cb_t label_signal;
51 static lv_signal_cb_t ancestor_page_signal;
52 static lv_signal_cb_t ancestor_btn_signal;
53 
54 
55 /**********************
56  *      MACROS
57  **********************/
58 
59 /**********************
60  *   GLOBAL FUNCTIONS
61  **********************/
62 
63 /**
64  * Create a list objects
65  * @param par pointer to an object, it will be the parent of the new list
66  * @param copy pointer to a list object, if not NULL then the new object will be copied from it
67  * @return pointer to the created list
68  */
lv_list_create(lv_obj_t * par,const lv_obj_t * copy)69 lv_obj_t * lv_list_create(lv_obj_t * par, const lv_obj_t * copy)
70 {
71     LV_LOG_TRACE("list create started");
72 
73     /*Create the ancestor basic object*/
74     lv_obj_t * list = lv_page_create(par, copy);
75     LV_ASSERT_MEM(list);
76     if(list == NULL) return NULL;
77 
78     if(ancestor_page_signal == NULL) ancestor_page_signal = lv_obj_get_signal_cb(list);
79 
80     lv_list_ext_t * ext = lv_obj_allocate_ext_attr(list, sizeof(lv_list_ext_t));
81     LV_ASSERT_MEM(ext);
82     if(ext == NULL) {
83         lv_obj_del(list);
84         return NULL;
85     }
86 
87 #if LV_USE_GROUP
88     ext->last_sel_btn = NULL;
89 #endif
90     ext->act_sel_btn = NULL;
91 
92     lv_obj_set_signal_cb(list, lv_list_signal);
93 
94     /*Init the new list object*/
95     if(copy == NULL) {
96         lv_page_set_anim_time(list, LV_LIST_DEF_ANIM_TIME);
97         lv_page_set_scrollable_fit2(list, LV_FIT_PARENT, LV_FIT_TIGHT);
98         lv_obj_set_size(list, 2 * LV_DPI, 3 * LV_DPI);
99         lv_page_set_scrl_layout(list, LV_LIST_LAYOUT_DEF);
100         lv_list_set_scrollbar_mode(list, LV_SCROLLBAR_MODE_DRAG);
101 
102         lv_theme_apply(list, LV_THEME_LIST);
103 
104     }
105     else {
106         lv_obj_t * copy_btn = lv_list_get_next_btn(copy, NULL);
107         while(copy_btn) {
108             const void * img_src = NULL;
109 #if LV_USE_IMG
110             lv_obj_t * copy_img = lv_list_get_btn_img(copy_btn);
111             if(copy_img) img_src = lv_img_get_src(copy_img);
112 #endif
113             lv_list_add_btn(list, img_src, lv_list_get_btn_text(copy_btn));
114             copy_btn = lv_list_get_next_btn(copy, copy_btn);
115         }
116 
117         /*Refresh the style with new signal function*/
118         lv_obj_refresh_style(list, LV_OBJ_PART_ALL, LV_STYLE_PROP_ALL);
119     }
120 
121     LV_LOG_INFO("list created");
122 
123     return list;
124 }
125 
126 /**
127  * Delete all children of the scrl object, without deleting scrl child.
128  * @param list pointer to an object
129  */
lv_list_clean(lv_obj_t * list)130 void lv_list_clean(lv_obj_t * list)
131 {
132     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
133 
134     lv_obj_t * scrl = lv_page_get_scrollable(list);
135     lv_obj_clean(scrl);
136 }
137 
138 /*======================
139  * Add/remove functions
140  *=====================*/
141 /**
142  * Add a list element to the list
143  * @param list pointer to list object
144  * @param img_fn file name of an image before the text (NULL if unused)
145  * @param txt text of the list element (NULL if unused)
146  * @return pointer to the new list element which can be customized (a button)
147  */
lv_list_add_btn(lv_obj_t * list,const void * img_src,const char * txt)148 lv_obj_t * lv_list_add_btn(lv_obj_t * list, const void * img_src, const char * txt)
149 {
150     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
151 
152     lv_obj_t * last_btn = lv_list_get_prev_btn(list, NULL);
153 
154     /*The coordinates may changed due to autofit so revert them at the end*/
155     lv_coord_t pos_x_ori = lv_obj_get_x(list);
156     lv_coord_t pos_y_ori = lv_obj_get_y(list);
157 
158     lv_obj_t * scrl =  lv_page_get_scrollable(list);
159     lv_obj_add_protect(scrl, LV_PROTECT_CHILD_CHG);
160 
161     /*Create a list element with the image an the text*/
162     lv_obj_t * btn;
163     btn = lv_btn_create(list, NULL);
164 
165     lv_obj_add_protect(btn, LV_PROTECT_CHILD_CHG);
166 
167     /*Save the original signal function because it will be required in `lv_list_btn_signal`*/
168     if(ancestor_btn_signal == NULL) ancestor_btn_signal = lv_obj_get_signal_cb(btn);
169 
170     /*Set the default styles*/
171     lv_theme_apply(btn, LV_THEME_LIST_BTN);
172 
173     lv_page_glue_obj(btn, true);
174     lv_btn_set_layout(btn, LV_LAYOUT_ROW_MID);
175 
176     lv_layout_t list_layout = lv_list_get_layout(list);
177     bool layout_ver = false;
178     if(list_layout == LV_LAYOUT_COLUMN_MID || list_layout == LV_LAYOUT_COLUMN_LEFT ||
179        list_layout == LV_LAYOUT_COLUMN_RIGHT) {
180         layout_ver = true;
181     }
182 
183     if(layout_ver) {
184         lv_btn_set_fit2(btn, LV_FIT_PARENT, LV_FIT_TIGHT);
185     }
186     else {
187         lv_coord_t w = last_btn ? lv_obj_get_width(last_btn) : (LV_DPI * 3) / 2;
188         lv_btn_set_fit2(btn, LV_FIT_NONE, LV_FIT_TIGHT);
189         lv_obj_set_width(btn, w);
190     }
191 
192 
193     lv_obj_add_protect(btn, LV_PROTECT_PRESS_LOST);
194     lv_obj_set_signal_cb(btn, lv_list_btn_signal);
195 
196 
197 #if LV_USE_IMG != 0
198     lv_obj_t * img = NULL;
199     if(img_src) {
200         img = lv_img_create(btn, NULL);
201         lv_img_set_src(img, img_src);
202         lv_obj_set_click(img, false);
203         if(img_signal == NULL) img_signal = lv_obj_get_signal_cb(img);
204     }
205 #endif
206     if(txt != NULL) {
207         lv_obj_t * label = lv_label_create(btn, NULL);
208         lv_label_set_text(label, txt);
209         lv_obj_set_click(label, false);
210         lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL_CIRC);
211         if(lv_obj_get_base_dir(btn) == LV_BIDI_DIR_RTL) {
212             lv_coord_t pad = lv_obj_get_style_pad_left(btn, LV_BTN_PART_MAIN);
213             lv_obj_set_width(label, label->coords.x2 - btn->coords.x1 - pad);
214         }
215         else {
216             lv_coord_t pad = lv_obj_get_style_pad_right(btn, LV_BTN_PART_MAIN);
217             lv_obj_set_width(label, btn->coords.x2 - label->coords.x1 - pad);
218         }
219         if(label_signal == NULL) label_signal = lv_obj_get_signal_cb(label);
220     }
221 
222 #if LV_USE_GROUP
223     /* If this is the first item to be added to the list and the list is
224      * focused, select it */
225     {
226         lv_group_t * g = lv_obj_get_group(list);
227         if(lv_list_get_next_btn(list, NULL) == btn && lv_group_get_focused(g) == list) {
228             lv_list_focus_btn(list, btn);
229         }
230     }
231 #endif
232 
233     lv_obj_clear_protect(scrl, LV_PROTECT_CHILD_CHG);
234     lv_obj_clear_protect(btn, LV_PROTECT_CHILD_CHG);
235     btn->signal_cb(btn, LV_SIGNAL_CHILD_CHG, NULL);
236 
237     lv_obj_set_pos(list, pos_x_ori, pos_y_ori);
238 
239     return btn;
240 }
241 
242 /**
243  * Remove the index of the button in the list
244  * @param list pointer to a list object
245  * @param index pointer to a the button's index in the list, index must be 0 <= index <
246  * lv_list_ext_t.size
247  * @return true: successfully deleted
248  */
lv_list_remove(const lv_obj_t * list,uint16_t index)249 bool lv_list_remove(const lv_obj_t * list, uint16_t index)
250 {
251     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
252 
253     uint16_t count = 0;
254     lv_obj_t * e   = lv_list_get_next_btn(list, NULL);
255     while(e != NULL) {
256         if(count == index) {
257             lv_obj_del(e);
258             return true;
259         }
260         e = lv_list_get_next_btn(list, e);
261         count++;
262     }
263     return false;
264 }
265 
266 /*=====================
267  * Setter functions
268  *====================*/
269 
270 /**
271  * Make a button selected
272  * @param list pointer to a list object
273  * @param btn pointer to a button to select
274  *            NULL to not select any buttons
275  */
lv_list_focus_btn(lv_obj_t * list,lv_obj_t * btn)276 void lv_list_focus_btn(lv_obj_t * list, lv_obj_t * btn)
277 {
278     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
279     if(btn) {
280         LV_ASSERT_OBJ(btn, "lv_btn");
281     }
282 
283     lv_list_ext_t * ext = lv_obj_get_ext_attr(list);
284 
285     /*Defocus the current button*/
286     if(ext->act_sel_btn) lv_obj_clear_state(ext->act_sel_btn, LV_STATE_FOCUSED);
287 
288 #if LV_USE_GROUP
289     /*Don't forget which button was selected.
290      * It will be restored when the list is focused again.*/
291     if(btn) ext->last_sel_btn = btn;
292 #endif
293 
294     /*Focus the new button*/
295     ext->act_sel_btn = btn;
296 
297     if(ext->act_sel_btn) {
298         lv_obj_add_state(ext->act_sel_btn, LV_STATE_FOCUSED);
299         lv_page_focus(list, ext->act_sel_btn, LV_ANIM_ON);
300     }
301 }
302 
303 /**
304  * Set layout of a list
305  * @param list pointer to a list object
306  * @param layout which layout should be used
307  */
lv_list_set_layout(lv_obj_t * list,lv_layout_t layout)308 void lv_list_set_layout(lv_obj_t * list, lv_layout_t layout)
309 {
310     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
311 
312     /* Update list layout if necessary */
313     if(layout == lv_list_get_layout(list)) return;
314 
315     /* Get the first button on the list */
316     lv_obj_t * btn = lv_list_get_prev_btn(list, NULL);
317 
318     /* Visit all buttons on the list and update their layout  */
319     while(btn != NULL) {
320         /*If a column layout set the buttons' width to list width*/
321         if(layout == LV_LAYOUT_COLUMN_MID || layout == LV_LAYOUT_COLUMN_LEFT || layout == LV_LAYOUT_COLUMN_RIGHT) {
322             lv_btn_set_fit2(btn, LV_FIT_PARENT, LV_FIT_TIGHT);
323         }
324         /*If a row layout set the buttons' width according to the content*/
325         else if(layout == LV_LAYOUT_ROW_MID || layout == LV_LAYOUT_ROW_TOP || layout == LV_LAYOUT_ROW_BOTTOM) {
326             lv_btn_set_fit(btn, LV_FIT_TIGHT);
327         }
328 
329         btn = lv_list_get_prev_btn(list, btn);
330     }
331 
332     if(layout == LV_LAYOUT_COLUMN_MID || layout == LV_LAYOUT_COLUMN_LEFT || layout == LV_LAYOUT_COLUMN_RIGHT) {
333         lv_page_set_scrollable_fit2(list, LV_FIT_PARENT, LV_FIT_TIGHT);
334     }
335     else if(layout == LV_LAYOUT_ROW_MID || layout == LV_LAYOUT_ROW_TOP || layout == LV_LAYOUT_ROW_BOTTOM) {
336         lv_page_set_scrollable_fit2(list, LV_FIT_TIGHT, LV_FIT_TIGHT);
337         lv_cont_set_fit2(list, LV_FIT_NONE, LV_FIT_TIGHT);
338     }
339 
340     lv_page_set_scrl_layout(list, layout);
341 }
342 
343 /*=====================
344  * Getter functions
345  *====================*/
346 
347 /**
348  * Get the text of a list element
349  * @param btn pointer to list element
350  * @return pointer to the text
351  */
lv_list_get_btn_text(const lv_obj_t * btn)352 const char * lv_list_get_btn_text(const lv_obj_t * btn)
353 {
354     LV_ASSERT_OBJ(btn, "lv_btn");
355 
356     lv_obj_t * label = lv_list_get_btn_label(btn);
357     if(label == NULL) return "";
358     return lv_label_get_text(label);
359 }
360 
361 /**
362  * Get the label object from a list element
363  * @param btn pointer to a list element (button)
364  * @return pointer to the label from the list element or NULL if not found
365  */
lv_list_get_btn_label(const lv_obj_t * btn)366 lv_obj_t * lv_list_get_btn_label(const lv_obj_t * btn)
367 {
368     LV_ASSERT_OBJ(btn, "lv_btn");
369 
370     lv_obj_t * label = lv_obj_get_child_back(btn, NULL);
371     if(label == NULL) return NULL;
372 
373     while(lv_list_is_list_label(label) == false) {
374         label = lv_obj_get_child_back(btn, label);
375         if(label == NULL) break;
376     }
377 
378     return label;
379 }
380 
381 /**
382  * Get the image object from a list element
383  * @param btn pointer to a list element (button)
384  * @return pointer to the image from the list element or NULL if not found
385  */
lv_list_get_btn_img(const lv_obj_t * btn)386 lv_obj_t * lv_list_get_btn_img(const lv_obj_t * btn)
387 {
388     LV_ASSERT_OBJ(btn, "lv_btn");
389 
390 #if LV_USE_IMG != 0
391     lv_obj_t * img = lv_obj_get_child_back(btn, NULL);
392     if(img == NULL) return NULL;
393 
394     while(lv_list_is_list_img(img) == false) {
395         img = lv_obj_get_child_back(btn, img);
396         if(img == NULL) break;
397     }
398 
399     return img;
400 #else
401     return NULL;
402 #endif
403 }
404 
405 /**
406  * Get the previous button from list. (Starts from the bottom button)
407  * @param list pointer to a list object
408  * @param prev_btn pointer to button. Search the previous before it.
409  * @return pointer to the previous button or NULL when no more buttons
410  */
lv_list_get_prev_btn(const lv_obj_t * list,lv_obj_t * prev_btn)411 lv_obj_t * lv_list_get_prev_btn(const lv_obj_t * list, lv_obj_t * prev_btn)
412 {
413     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
414 
415     /* Not a good practice but user can add/create objects to the lists manually.
416      * When getting the next button try to be sure that it is at least a button */
417 
418     lv_obj_t * btn;
419     lv_obj_t * scrl = lv_page_get_scrollable(list);
420 
421     btn = lv_obj_get_child(scrl, prev_btn);
422     if(btn == NULL) return NULL;
423 
424     while(lv_list_is_list_btn(btn) == false) {
425         btn = lv_obj_get_child(scrl, btn);
426         if(btn == NULL) break;
427     }
428 
429     return btn;
430 }
431 
432 /**
433  * Get the next button from list. (Starts from the top button)
434  * @param list pointer to a list object
435  * @param prev_btn pointer to button. Search the next after it.
436  * @return pointer to the next button or NULL when no more buttons
437  */
lv_list_get_next_btn(const lv_obj_t * list,lv_obj_t * prev_btn)438 lv_obj_t * lv_list_get_next_btn(const lv_obj_t * list, lv_obj_t * prev_btn)
439 {
440     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
441 
442     /* Not a good practice but user can add/create objects to the lists manually.
443      * When getting the next button try to be sure that it is at least a button */
444 
445     lv_obj_t * btn;
446     lv_obj_t * scrl = lv_page_get_scrollable(list);
447 
448     btn = lv_obj_get_child_back(scrl, prev_btn);
449     if(btn == NULL) return NULL;
450 
451     while(lv_list_is_list_btn(btn) == false) {
452         btn = lv_obj_get_child_back(scrl, btn);
453         if(btn == NULL) break;
454     }
455 
456     return btn;
457 }
458 
459 /**
460  * Get the index of the button in the list
461  * @param list pointer to a list object. If NULL, assumes btn is part of a list.
462  * @param btn pointer to a list element (button)
463  * @return the index of the button in the list, or -1 of the button not in this list
464  */
lv_list_get_btn_index(const lv_obj_t * list,const lv_obj_t * btn)465 int32_t lv_list_get_btn_index(const lv_obj_t * list, const lv_obj_t * btn)
466 {
467     LV_ASSERT_OBJ(btn, "lv_btn");
468 
469     int index = 0;
470     if(list == NULL) {
471         /* no list provided, assuming btn is part of a list */
472         list = lv_obj_get_parent(lv_obj_get_parent(btn));
473     }
474     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
475 
476     lv_obj_t * e = lv_list_get_next_btn(list, NULL);
477     while(e != NULL) {
478         if(e == btn) {
479             return index;
480         }
481         index++;
482         e = lv_list_get_next_btn(list, e);
483     }
484     return -1;
485 }
486 
487 /**
488  * Get the number of buttons in the list
489  * @param list pointer to a list object
490  * @return the number of buttons in the list
491  */
lv_list_get_size(const lv_obj_t * list)492 uint16_t lv_list_get_size(const lv_obj_t * list)
493 {
494     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
495 
496     uint16_t size = 0;
497     lv_obj_t * btn = lv_list_get_next_btn(list, NULL);
498     while(btn) {
499         size++;
500         btn = lv_list_get_next_btn(list, btn);
501     }
502     return size;
503 }
504 
505 #if LV_USE_GROUP
506 /**
507  * Get the currently selected button
508  * @param list pointer to a list object
509  * @return pointer to the selected button
510  */
lv_list_get_btn_selected(const lv_obj_t * list)511 lv_obj_t * lv_list_get_btn_selected(const lv_obj_t * list)
512 {
513     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
514 
515     lv_list_ext_t * ext = lv_obj_get_ext_attr(list);
516     return ext->act_sel_btn;
517 }
518 #endif
519 
520 /**
521  * Get layout of a list
522  * @param list pointer to a list object
523  * @return layout of the list object
524  */
lv_list_get_layout(lv_obj_t * list)525 lv_layout_t lv_list_get_layout(lv_obj_t * list)
526 {
527     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
528 
529     return lv_page_get_scrl_layout(list);
530 }
531 
532 /*=====================
533  * Other functions
534  *====================*/
535 
536 /**
537  * Move the list elements up by one
538  * @param list pointer a to list object
539  */
lv_list_up(const lv_obj_t * list)540 void lv_list_up(const lv_obj_t * list)
541 {
542     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
543 
544     /*Search the first list element which 'y' coordinate is below the parent
545      * and position the list to show this element on the bottom*/
546     lv_obj_t * scrl = lv_page_get_scrollable(list);
547     lv_obj_t * e;
548     lv_obj_t * e_prev = NULL;
549 
550     e                 = lv_list_get_prev_btn(list, NULL);
551     while(e != NULL) {
552         if(e->coords.y2 <= list->coords.y2) {
553             if(e_prev != NULL) {
554                 lv_coord_t new_y = lv_obj_get_height(list) - (lv_obj_get_y(e_prev) + lv_obj_get_height(e_prev));
555                 if(lv_list_get_anim_time(list) == 0) {
556                     lv_obj_set_y(scrl, new_y);
557                 }
558                 else {
559 #if LV_USE_ANIMATION
560                     lv_anim_t a;
561                     lv_anim_init(&a);
562                     lv_anim_set_var(&a, scrl);
563                     lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
564                     lv_anim_set_values(&a, lv_obj_get_y(scrl), new_y);
565                     lv_anim_set_time(&a, LV_LIST_DEF_ANIM_TIME);
566                     lv_anim_start(&a);
567 #endif
568                 }
569             }
570             break;
571         }
572         e_prev = e;
573         e      = lv_list_get_prev_btn(list, e);
574     }
575 }
576 
577 /**
578  * Move the list elements down by one
579  * @param list pointer to a list object
580  */
lv_list_down(const lv_obj_t * list)581 void lv_list_down(const lv_obj_t * list)
582 {
583     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
584 
585     /*Search the first list element which 'y' coordinate is above the parent
586      * and position the list to show this element on the top*/
587     lv_obj_t * scrl = lv_page_get_scrollable(list);
588     lv_obj_t * e;
589     e = lv_list_get_prev_btn(list, NULL);
590     while(e != NULL) {
591         if(e->coords.y1 < list->coords.y1) {
592             lv_coord_t new_y = -lv_obj_get_y(e);
593             if(lv_list_get_anim_time(list) == 0) {
594                 lv_obj_set_y(scrl, new_y);
595             }
596             else {
597 #if LV_USE_ANIMATION
598                 lv_anim_t a;
599                 lv_anim_init(&a);
600                 lv_anim_set_var(&a, scrl);
601                 lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
602                 lv_anim_set_values(&a, lv_obj_get_y(scrl), new_y);
603                 lv_anim_set_time(&a, LV_LIST_DEF_ANIM_TIME);
604                 lv_anim_start(&a);
605 #endif
606             }
607             break;
608         }
609         e = lv_list_get_prev_btn(list, e);
610     }
611 }
612 
613 /**
614  * Focus on a list button. It ensures that the button will be visible on the list.
615  * @param btn pointer to a list button to focus
616  * @param anim_en LV_ANIM_ON: scroll with animation, LV_ANOM_OFF: without animation
617  */
lv_list_focus(const lv_obj_t * btn,lv_anim_enable_t anim)618 void lv_list_focus(const lv_obj_t * btn, lv_anim_enable_t anim)
619 {
620     LV_ASSERT_OBJ(btn, "");
621 
622 #if LV_USE_ANIMATION == 0
623     anim = false;
624 #endif
625 
626     lv_obj_t * list = lv_obj_get_parent(lv_obj_get_parent(btn));
627 
628     lv_page_focus(list, btn, anim == LV_ANIM_OFF ? 0 : lv_list_get_anim_time(list));
629 }
630 
631 /**********************
632  *   STATIC FUNCTIONS
633  **********************/
634 
635 /**
636  * Signal function of the list
637  * @param list pointer to a list object
638  * @param sign a signal type from lv_signal_t enum
639  * @param param pointer to a signal specific variable
640  * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
641  */
lv_list_signal(lv_obj_t * list,lv_signal_t sign,void * param)642 static lv_res_t lv_list_signal(lv_obj_t * list, lv_signal_t sign, void * param)
643 {
644     lv_res_t res;
645     if(sign == LV_SIGNAL_GET_STYLE) {
646         lv_get_style_info_t * info = param;
647         info->result = lv_list_get_style(list, info->part);
648         if(info->result != NULL) return LV_RES_OK;
649         else return ancestor_page_signal(list, sign, param);
650     }
651 
652     /* Include the ancient signal function */
653     res = ancestor_page_signal(list, sign, param);
654     if(res != LV_RES_OK) return res;
655     if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
656 
657     if(sign == LV_SIGNAL_RELEASED || sign == LV_SIGNAL_PRESSED || sign == LV_SIGNAL_PRESSING ||
658        sign == LV_SIGNAL_LONG_PRESS || sign == LV_SIGNAL_LONG_PRESS_REP) {
659 #if LV_USE_GROUP
660         /*If pressed/released etc by a KEYPAD or ENCODER delegate signal and events to the button*/
661         lv_indev_t * indev         = lv_indev_get_act();
662         lv_indev_type_t indev_type = lv_indev_get_type(indev);
663         if(indev_type == LV_INDEV_TYPE_KEYPAD ||
664            (indev_type == LV_INDEV_TYPE_ENCODER && lv_group_get_editing(lv_obj_get_group(list)))) {
665             lv_list_ext_t * ext = lv_obj_get_ext_attr(list);
666 
667             /*The page receives the key presses so the events should be propagated to the selected
668              * button*/
669             if(ext->act_sel_btn) {
670                 res = lv_signal_send(ext->act_sel_btn, sign, param);
671                 if(res != LV_RES_OK) return res;
672                 if(sign == LV_SIGNAL_PRESSED) {
673                     res = lv_event_send(ext->act_sel_btn, LV_EVENT_PRESSED, NULL);
674                     if(res != LV_RES_OK) return res;
675                 }
676                 else if(sign == LV_SIGNAL_PRESSING) {
677                     res = lv_event_send(ext->act_sel_btn, LV_EVENT_PRESSING, NULL);
678                     if(res != LV_RES_OK) return res;
679                 }
680                 else if(sign == LV_SIGNAL_LONG_PRESS) {
681                     res = lv_event_send(ext->act_sel_btn, LV_EVENT_LONG_PRESSED, NULL);
682                     if(res != LV_RES_OK) return res;
683                 }
684                 else if(sign == LV_SIGNAL_LONG_PRESS_REP) {
685                     res = lv_event_send(ext->act_sel_btn, LV_EVENT_LONG_PRESSED_REPEAT, NULL);
686                     if(res != LV_RES_OK) return res;
687                 }
688                 else if(sign == LV_SIGNAL_RELEASED) {
689                     if(indev->proc.long_pr_sent == 0) {
690                         res = lv_event_send(ext->act_sel_btn, LV_EVENT_SHORT_CLICKED, NULL);
691                         if(res != LV_RES_OK) return res;
692                     }
693                     if(lv_indev_is_dragging(indev) == false) {
694                         res = lv_event_send(ext->act_sel_btn, LV_EVENT_CLICKED, NULL);
695                         if(res != LV_RES_OK) return res;
696                     }
697 
698                     res = lv_event_send(ext->act_sel_btn, LV_EVENT_RELEASED, NULL);
699                     if(res != LV_RES_OK) return res;
700                 }
701             }
702         }
703 #endif
704     }
705     else if(sign == LV_SIGNAL_FOCUS) {
706 
707 #if LV_USE_GROUP
708         lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
709         /*With ENCODER focus the button only in edit mode*/
710         lv_group_t * g = lv_obj_get_group(list);
711         if((indev_type == LV_INDEV_TYPE_KEYPAD) ||
712            (indev_type == LV_INDEV_TYPE_ENCODER && lv_group_get_editing(g))) {
713             lv_list_ext_t * ext = lv_obj_get_ext_attr(list);
714             /* Select the last used button, or use the first no last button */
715             if(ext->last_sel_btn) lv_list_focus_btn(list, ext->last_sel_btn);
716             else lv_list_focus_btn(list, lv_list_get_next_btn(list, NULL));
717         }
718         if(indev_type == LV_INDEV_TYPE_ENCODER && lv_group_get_editing(g) == false) {
719             lv_list_ext_t * ext = lv_obj_get_ext_attr(list);
720             if(ext->act_sel_btn) lv_obj_clear_state(ext->act_sel_btn, LV_STATE_PRESSED);
721             if(ext->last_sel_btn) lv_obj_clear_state(ext->last_sel_btn, LV_STATE_PRESSED);
722         }
723 #endif
724     }
725     else if(sign == LV_SIGNAL_DEFOCUS) {
726 
727 #if LV_USE_GROUP
728         /*De-select the selected btn*/
729         lv_list_focus_btn(list, NULL);
730         lv_list_ext_t * ext = lv_obj_get_ext_attr(list);
731         ext->act_sel_btn   = NULL;
732 #endif
733     }
734     else if(sign == LV_SIGNAL_GET_EDITABLE) {
735 #if LV_USE_GROUP
736         bool * editable = (bool *)param;
737         *editable       = true;
738 #endif
739     }
740     else if(sign == LV_SIGNAL_CONTROL) {
741 
742 #if LV_USE_GROUP
743         char c = *((char *)param);
744         if(c == LV_KEY_RIGHT || c == LV_KEY_DOWN) {
745             lv_list_ext_t * ext = lv_obj_get_ext_attr(list);
746             /*If there is a valid selected button the make the previous selected*/
747             if(ext->act_sel_btn) {
748                 lv_obj_t * btn_prev = lv_list_get_next_btn(list, ext->act_sel_btn);
749                 if(btn_prev) lv_list_focus_btn(list, btn_prev);
750             }
751             /*If there is no selected button the make the first selected*/
752             else {
753                 lv_obj_t * btn = lv_list_get_next_btn(list, NULL);
754                 if(btn)
755                     lv_list_focus_btn(list,
756                                       btn); /*If there are no buttons on the list then there is no first button*/
757             }
758         }
759         else if(c == LV_KEY_LEFT || c == LV_KEY_UP) {
760             lv_list_ext_t * ext = lv_obj_get_ext_attr(list);
761             /*If there is a valid selected button the make the next selected*/
762             if(ext->act_sel_btn != NULL) {
763                 lv_obj_t * btn_next = lv_list_get_prev_btn(list, ext->act_sel_btn);
764                 if(btn_next) lv_list_focus_btn(list, btn_next);
765             }
766             /*If there is no selected button the make the first selected*/
767             else {
768                 lv_obj_t * btn = lv_list_get_next_btn(list, NULL);
769                 if(btn) lv_list_focus_btn(list, btn);
770             }
771         }
772         else if(c == LV_KEY_ESC) {
773             lv_list_ext_t * ext = lv_obj_get_ext_attr(list);
774             /* Handle ESC/Cancel event */
775             res = lv_event_send(ext->act_sel_btn, LV_EVENT_CANCEL, NULL);
776         }
777 #endif
778     }
779     return res;
780 }
781 
782 /**
783  * Signal function of the list buttons
784  * @param btn pointer to a button on the list
785  * @param sign a signal type from lv_signal_t enum
786  * @param param pointer to a signal specific variable
787  * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
788  */
lv_list_btn_signal(lv_obj_t * btn,lv_signal_t sign,void * param)789 static lv_res_t lv_list_btn_signal(lv_obj_t * btn, lv_signal_t sign, void * param)
790 {
791     lv_res_t res;
792 
793     /* Include the ancient signal function */
794     res = ancestor_btn_signal(btn, sign, param);
795     if(res != LV_RES_OK) return res;
796     if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, "");
797 
798     if((sign == LV_SIGNAL_COORD_CHG && (lv_obj_get_width(btn) != lv_area_get_width(param) ||
799                                         lv_obj_get_height(btn) != lv_area_get_height(param))) ||
800        sign == LV_SIGNAL_STYLE_CHG) {
801         lv_obj_t * label = lv_list_get_btn_label(btn);
802         if(label) {
803             const lv_font_t * font = lv_obj_get_style_text_font(label, LV_LABEL_PART_MAIN);
804             lv_coord_t font_h = lv_font_get_line_height(font);
805             if(lv_obj_get_base_dir(btn) == LV_BIDI_DIR_RTL) {
806                 lv_coord_t pad = lv_obj_get_style_pad_left(btn, LV_BTN_PART_MAIN);
807                 lv_obj_set_size(label, label->coords.x2 - btn->coords.x1 - pad, font_h);
808             }
809             else {
810                 lv_coord_t pad = lv_obj_get_style_pad_right(btn, LV_BTN_PART_MAIN);
811                 lv_obj_set_size(label, btn->coords.x2 - label->coords.x1 - pad + 1, font_h);
812             }
813         }
814     }
815     else if(sign == LV_SIGNAL_RELEASED) {
816         if(lv_indev_is_dragging(lv_indev_get_act()) == false) {
817             lv_obj_t * list = lv_obj_get_parent(lv_obj_get_parent(btn));
818             lv_list_focus_btn(list, btn);
819 #if LV_USE_GROUP
820             if(lv_obj_get_group(list)) {
821                 lv_group_focus_obj(list);
822             }
823 #endif
824         }
825     }
826     else if(sign == LV_SIGNAL_CLEANUP) {
827 #if LV_USE_GROUP
828         lv_obj_t * list = lv_obj_get_parent(lv_obj_get_parent(btn));
829         lv_list_ext_t * ext = lv_obj_get_ext_attr(list);
830         lv_obj_t * sel  = lv_list_get_btn_selected(list);
831         if(sel == btn) lv_list_focus_btn(list, lv_list_get_next_btn(list, btn));
832         if(ext->last_sel_btn == btn) ext->last_sel_btn = NULL;
833 #endif
834     }
835 
836     return res;
837 }
838 
839 /**
840  * Get the style descriptor of a part of the object
841  * @param list pointer the object
842  * @param part the part from `lv_page_list_t`. (LV_LIST_PART_...)
843  * @return pointer to the style descriptor of the specified part
844  */
lv_list_get_style(lv_obj_t * list,uint8_t part)845 static lv_style_list_t * lv_list_get_style(lv_obj_t * list, uint8_t part)
846 {
847     LV_ASSERT_OBJ(list, LV_OBJX_NAME);
848 
849     lv_list_ext_t * ext = lv_obj_get_ext_attr(list);
850     lv_style_list_t * style_dsc_p;
851 
852     switch(part) {
853         case LV_LIST_PART_BG:
854             style_dsc_p = &list->style_list;
855             break;
856         case LV_LIST_PART_SCROLLABLE:
857             style_dsc_p = &ext->page.scrl->style_list;
858             break;
859         case LV_LIST_PART_SCROLLBAR:
860             style_dsc_p = &ext->page.scrlbar.style;
861             break;
862 #if LV_USE_ANIMATION
863         case LV_LIST_PART_EDGE_FLASH:
864             style_dsc_p = &ext->page.edge_flash.style;
865             break;
866 #endif
867         default:
868             style_dsc_p = NULL;
869     }
870 
871     return style_dsc_p;
872 }
873 
874 /**
875  * Check if this is really a list button or another object.
876  * @param list_btn List button
877  */
lv_list_is_list_btn(lv_obj_t * list_btn)878 static bool lv_list_is_list_btn(lv_obj_t * list_btn)
879 {
880     lv_obj_type_t type;
881 
882     lv_obj_get_type(list_btn, &type);
883     uint8_t cnt;
884     for(cnt = 0; cnt < LV_MAX_ANCESTOR_NUM; cnt++) {
885         if(type.type[cnt] == NULL) break;
886         if(!strcmp(type.type[cnt], "lv_btn")) return true;
887     }
888     return false;
889 }
890 
891 /**
892  * Check if this is really a list label or another object.
893  * @param list_label List label
894  */
lv_list_is_list_label(lv_obj_t * list_label)895 static bool lv_list_is_list_label(lv_obj_t * list_label)
896 {
897     lv_obj_type_t type;
898 
899     lv_obj_get_type(list_label, &type);
900     uint8_t cnt;
901     for(cnt = 0; cnt < LV_MAX_ANCESTOR_NUM; cnt++) {
902         if(type.type[cnt] == NULL) break;
903         if(!strcmp(type.type[cnt], "lv_label")) return true;
904     }
905     return false;
906 }
907 
908 /**
909  * Check if this is really a list image or another object.
910  * @param list_image List image
911  */
lv_list_is_list_img(lv_obj_t * list_img)912 static bool lv_list_is_list_img(lv_obj_t * list_img)
913 {
914     lv_obj_type_t type;
915 
916     lv_obj_get_type(list_img, &type);
917     uint8_t cnt;
918     for(cnt = 0; cnt < LV_MAX_ANCESTOR_NUM; cnt++) {
919         if(type.type[cnt] == NULL) break;
920         if(!strcmp(type.type[cnt], "lv_img")) return true;
921     }
922     return false;
923 }
924 
925 #endif
926