1 /**
2 * @file lv_list.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "../../core/lv_obj_class_private.h"
10 #include "lv_list.h"
11 #include "../../layouts/flex/lv_flex.h"
12 #include "../../display/lv_display.h"
13 #include "../label/lv_label.h"
14 #include "../image/lv_image.h"
15 #include "../button/lv_button.h"
16
17 #if LV_USE_LIST
18
19 /*********************
20 * DEFINES
21 *********************/
22 #define MY_CLASS (&lv_list_class)
23 #define MY_CLASS_BUTTON (&lv_list_button_class)
24 #define MY_CLASS_TEXT (&lv_list_text_class)
25
26 /**********************
27 * TYPEDEFS
28 **********************/
29
30 /**********************
31 * STATIC PROTOTYPES
32 **********************/
33 const lv_obj_class_t lv_list_class = {
34 .base_class = &lv_obj_class,
35 .width_def = (LV_DPI_DEF * 3) / 2,
36 .height_def = LV_DPI_DEF * 2,
37 .name = "list",
38 };
39
40 const lv_obj_class_t lv_list_button_class = {
41 .base_class = &lv_button_class,
42 .width_def = LV_PCT(100),
43 .height_def = LV_SIZE_CONTENT,
44 .name = "list-btn",
45 };
46
47 const lv_obj_class_t lv_list_text_class = {
48 .base_class = &lv_label_class,
49 .width_def = LV_PCT(100),
50 .height_def = LV_SIZE_CONTENT,
51 .name = "list-text",
52 };
53
54 /**********************
55 * STATIC VARIABLES
56 **********************/
57
58 /**********************
59 * MACROS
60 **********************/
61
62 /**********************
63 * GLOBAL FUNCTIONS
64 **********************/
65
lv_list_create(lv_obj_t * parent)66 lv_obj_t * lv_list_create(lv_obj_t * parent)
67 {
68 LV_LOG_INFO("begin");
69 lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS, parent);
70 lv_obj_class_init_obj(obj);
71 lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_COLUMN);
72 return obj;
73 }
74
lv_list_add_text(lv_obj_t * list,const char * txt)75 lv_obj_t * lv_list_add_text(lv_obj_t * list, const char * txt)
76 {
77 LV_LOG_INFO("begin");
78
79 lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS_TEXT, list);
80 lv_obj_class_init_obj(obj);
81 lv_label_set_text(obj, txt);
82 return obj;
83 }
84
lv_list_add_button(lv_obj_t * list,const void * icon,const char * txt)85 lv_obj_t * lv_list_add_button(lv_obj_t * list, const void * icon, const char * txt)
86 {
87 LV_LOG_INFO("begin");
88 lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS_BUTTON, list);
89 lv_obj_class_init_obj(obj);
90 lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_ROW);
91
92 #if LV_USE_IMAGE == 1
93 if(icon) {
94 lv_obj_t * img = lv_image_create(obj);
95 lv_image_set_src(img, icon);
96 }
97 #endif
98
99 if(txt) {
100 lv_obj_t * label = lv_label_create(obj);
101 lv_label_set_text(label, txt);
102 lv_label_set_long_mode(label, LV_LABEL_LONG_MODE_SCROLL_CIRCULAR);
103 lv_obj_set_flex_grow(label, 1);
104 }
105
106 return obj;
107 }
108
lv_list_get_button_text(lv_obj_t * list,lv_obj_t * btn)109 const char * lv_list_get_button_text(lv_obj_t * list, lv_obj_t * btn)
110 {
111 LV_UNUSED(list);
112 uint32_t i;
113 for(i = 0; i < lv_obj_get_child_count(btn); i++) {
114 lv_obj_t * child = lv_obj_get_child(btn, i);
115 if(lv_obj_check_type(child, &lv_label_class)) {
116 return lv_label_get_text(child);
117 }
118
119 }
120
121 return "";
122 }
123
lv_list_set_button_text(lv_obj_t * list,lv_obj_t * btn,const char * txt)124 void lv_list_set_button_text(lv_obj_t * list, lv_obj_t * btn, const char * txt)
125 {
126 LV_UNUSED(list);
127 uint32_t i;
128 for(i = 0; i < lv_obj_get_child_count(btn); i++) {
129 lv_obj_t * child = lv_obj_get_child(btn, i);
130 if(lv_obj_check_type(child, &lv_label_class)) {
131 lv_label_set_text(child, txt);
132 return;
133 }
134 }
135 }
136
137 /**********************
138 * STATIC FUNCTIONS
139 **********************/
140
141 #endif /*LV_USE_LIST*/
142