1 /**
2  * @file lv_table.h
3  *
4  */
5 
6 #ifndef LV_TABLE_H
7 #define LV_TABLE_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include "../lv_conf_internal.h"
17 
18 #if LV_USE_TABLE != 0
19 
20 /*Testing of dependencies*/
21 #if LV_USE_LABEL == 0
22 #error "lv_table: lv_label is required. Enable it in lv_conf.h (LV_USE_LABEL  1) "
23 #endif
24 
25 #include "../lv_core/lv_obj.h"
26 #include "lv_label.h"
27 
28 /*********************
29  *      DEFINES
30  *********************/
31 #ifndef LV_TABLE_COL_MAX
32 #define LV_TABLE_COL_MAX 12
33 #endif
34 
35 #define LV_TABLE_CELL_STYLE_CNT 4
36 /**********************
37  *      TYPEDEFS
38  **********************/
39 
40 /**
41  * Internal table cell format structure.
42  *
43  * Use the `lv_table` APIs instead.
44  */
45 typedef union {
46     struct {
47         uint8_t align : 2;
48         uint8_t right_merge : 1;
49         uint8_t type : 2;
50         uint8_t crop : 1;
51     } s;
52     uint8_t format_byte;
53 } lv_table_cell_format_t;
54 
55 /*Data of table*/
56 typedef struct {
57     /*New data for this type */
58     uint16_t col_cnt;
59     uint16_t row_cnt;
60     char ** cell_data;
61     lv_coord_t * row_h;
62     lv_style_list_t cell_style[LV_TABLE_CELL_STYLE_CNT];
63     lv_coord_t col_w[LV_TABLE_COL_MAX];
64     uint8_t cell_types : 4; /*Keep track which cell types exists to avoid dealing with unused ones*/
65 } lv_table_ext_t;
66 
67 /*Parts of the table*/
68 enum {
69     LV_TABLE_PART_BG,
70     LV_TABLE_PART_CELL1,
71     LV_TABLE_PART_CELL2,
72     LV_TABLE_PART_CELL3,
73     LV_TABLE_PART_CELL4,
74 };
75 
76 /**********************
77  * GLOBAL PROTOTYPES
78  **********************/
79 
80 /**
81  * Create a table object
82  * @param par pointer to an object, it will be the parent of the new table
83  * @param copy pointer to a table object, if not NULL then the new object will be copied from it
84  * @return pointer to the created table
85  */
86 lv_obj_t * lv_table_create(lv_obj_t * par, const lv_obj_t * copy);
87 
88 /*=====================
89  * Setter functions
90  *====================*/
91 
92 /**
93  * Set the value of a cell.
94  * @param table pointer to a Table object
95  * @param row id of the row [0 .. row_cnt -1]
96  * @param col id of the column [0 .. col_cnt -1]
97  * @param txt text to display in the cell. It will be copied and saved so this variable is not
98  * required after this function call.
99  */
100 void lv_table_set_cell_value(lv_obj_t * table, uint16_t row, uint16_t col, const char * txt);
101 
102 /**
103  * Set the number of rows
104  * @param table table pointer to a Table object
105  * @param row_cnt number of rows
106  */
107 void lv_table_set_row_cnt(lv_obj_t * table, uint16_t row_cnt);
108 
109 /**
110  * Set the number of columns
111  * @param table table pointer to a Table object
112  * @param col_cnt number of columns. Must be < LV_TABLE_COL_MAX
113  */
114 void lv_table_set_col_cnt(lv_obj_t * table, uint16_t col_cnt);
115 
116 /**
117  * Set the width of a column
118  * @param table table pointer to a Table object
119  * @param col_id id of the column [0 .. LV_TABLE_COL_MAX -1]
120  * @param w width of the column
121  */
122 void lv_table_set_col_width(lv_obj_t * table, uint16_t col_id, lv_coord_t w);
123 
124 /**
125  * Set the text align in a cell
126  * @param table pointer to a Table object
127  * @param row id of the row [0 .. row_cnt -1]
128  * @param col id of the column [0 .. col_cnt -1]
129  * @param align LV_LABEL_ALIGN_LEFT or LV_LABEL_ALIGN_CENTER or LV_LABEL_ALIGN_RIGHT
130  */
131 void lv_table_set_cell_align(lv_obj_t * table, uint16_t row, uint16_t col, lv_label_align_t align);
132 
133 /**
134  * Set the type of a cell.
135  * @param table pointer to a Table object
136  * @param row id of the row [0 .. row_cnt -1]
137  * @param col id of the column [0 .. col_cnt -1]
138  * @param type 1,2,3 or 4. The cell style will be chosen accordingly.
139  */
140 void lv_table_set_cell_type(lv_obj_t * table, uint16_t row, uint16_t col, uint8_t type);
141 
142 /**
143  * Set the cell crop. (Don't adjust the height of the cell according to its content)
144  * @param table pointer to a Table object
145  * @param row id of the row [0 .. row_cnt -1]
146  * @param col id of the column [0 .. col_cnt -1]
147  * @param crop true: crop the cell content; false: set the cell height to the content.
148  */
149 void lv_table_set_cell_crop(lv_obj_t * table, uint16_t row, uint16_t col, bool crop);
150 
151 /**
152  * Merge a cell with the right neighbor. The value of the cell to the right won't be displayed.
153  * @param table table pointer to a Table object
154  * @param row id of the row [0 .. row_cnt -1]
155  * @param col id of the column [0 .. col_cnt -1]
156  * @param en true: merge right; false: don't merge right
157  */
158 void lv_table_set_cell_merge_right(lv_obj_t * table, uint16_t row, uint16_t col, bool en);
159 
160 /*=====================
161  * Getter functions
162  *====================*/
163 
164 /**
165  * Get the value of a cell.
166  * @param table pointer to a Table object
167  * @param row id of the row [0 .. row_cnt -1]
168  * @param col id of the column [0 .. col_cnt -1]
169  * @return text in the cell
170  */
171 const char * lv_table_get_cell_value(lv_obj_t * table, uint16_t row, uint16_t col);
172 
173 /**
174  * Get the number of rows.
175  * @param table table pointer to a Table object
176  * @return number of rows.
177  */
178 uint16_t lv_table_get_row_cnt(lv_obj_t * table);
179 
180 /**
181  * Get the number of columns.
182  * @param table table pointer to a Table object
183  * @return number of columns.
184  */
185 uint16_t lv_table_get_col_cnt(lv_obj_t * table);
186 
187 /**
188  * Get the width of a column
189  * @param table table pointer to a Table object
190  * @param col_id id of the column [0 .. LV_TABLE_COL_MAX -1]
191  * @return width of the column
192  */
193 lv_coord_t lv_table_get_col_width(lv_obj_t * table, uint16_t col_id);
194 
195 /**
196  * Get the text align of a cell
197  * @param table pointer to a Table object
198  * @param row id of the row [0 .. row_cnt -1]
199  * @param col id of the column [0 .. col_cnt -1]
200  * @return LV_LABEL_ALIGN_LEFT (default in case of error) or LV_LABEL_ALIGN_CENTER or
201  * LV_LABEL_ALIGN_RIGHT
202  */
203 lv_label_align_t lv_table_get_cell_align(lv_obj_t * table, uint16_t row, uint16_t col);
204 
205 /**
206  * Get the type of a cell
207  * @param table pointer to a Table object
208  * @param row id of the row [0 .. row_cnt -1]
209  * @param col id of the column [0 .. col_cnt -1]
210  * @return 1,2,3 or 4
211  */
212 lv_label_align_t lv_table_get_cell_type(lv_obj_t * table, uint16_t row, uint16_t col);
213 
214 /**
215  * Get the crop property of a cell
216  * @param table pointer to a Table object
217  * @param row id of the row [0 .. row_cnt -1]
218  * @param col id of the column [0 .. col_cnt -1]
219  * @return true: text crop enabled; false: disabled
220  */
221 lv_label_align_t lv_table_get_cell_crop(lv_obj_t * table, uint16_t row, uint16_t col);
222 
223 /**
224  * Get the cell merge attribute.
225  * @param table table pointer to a Table object
226  * @param row id of the row [0 .. row_cnt -1]
227  * @param col id of the column [0 .. col_cnt -1]
228  * @return true: merge right; false: don't merge right
229  */
230 bool lv_table_get_cell_merge_right(lv_obj_t * table, uint16_t row, uint16_t col);
231 
232 /**
233  * Get the last pressed or being pressed cell
234  * @param table pointer to a table object
235  * @param row pointer to variable to store the pressed row
236  * @param col pointer to variable to store the pressed column
237  * @return LV_RES_OK: a valid pressed cell was found, LV_RES_INV: no valid cell is pressed
238  */
239 lv_res_t lv_table_get_pressed_cell(lv_obj_t * table, uint16_t * row, uint16_t * col);
240 
241 /*=====================
242  * Other functions
243  *====================*/
244 
245 /**********************
246  *      MACROS
247  **********************/
248 
249 #endif /*LV_USE_TABLE*/
250 
251 #ifdef __cplusplus
252 } /* extern "C" */
253 #endif
254 
255 #endif /*LV_TABLE_H*/
256