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 "../core/lv_obj.h"
26 #include "lv_label.h"
27 
28 /*********************
29  *      DEFINES
30  *********************/
31 #define LV_TABLE_CELL_NONE 0XFFFF
32 LV_EXPORT_CONST_INT(LV_TABLE_CELL_NONE);
33 
34 /**********************
35  *      TYPEDEFS
36  **********************/
37 
38 enum {
39     LV_TABLE_CELL_CTRL_MERGE_RIGHT = 1 << 0,
40     LV_TABLE_CELL_CTRL_TEXT_CROP   = 1 << 1,
41     LV_TABLE_CELL_CTRL_CUSTOM_1    = 1 << 4,
42     LV_TABLE_CELL_CTRL_CUSTOM_2    = 1 << 5,
43     LV_TABLE_CELL_CTRL_CUSTOM_3    = 1 << 6,
44     LV_TABLE_CELL_CTRL_CUSTOM_4    = 1 << 7,
45 };
46 
47 typedef uint8_t  lv_table_cell_ctrl_t;
48 
49 /*Data of cell*/
50 typedef struct {
51     lv_table_cell_ctrl_t ctrl;
52 #if LV_USE_USER_DATA
53     void * user_data; /**< Custom user data*/
54 #endif
55     char txt[];
56 } lv_table_cell_t;
57 
58 /*Data of table*/
59 typedef struct {
60     lv_obj_t obj;
61     uint16_t col_cnt;
62     uint16_t row_cnt;
63     lv_table_cell_t ** cell_data;
64     lv_coord_t * row_h;
65     lv_coord_t * col_w;
66     uint16_t col_act;
67     uint16_t row_act;
68 } lv_table_t;
69 
70 extern const lv_obj_class_t lv_table_class;
71 
72 /**
73  * `type` field in `lv_obj_draw_part_dsc_t` if `class_p = lv_table_class`
74  * Used in `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END`
75  */
76 typedef enum {
77     LV_TABLE_DRAW_PART_CELL,       /**< A cell*/
78 } lv_table_draw_part_type_t;
79 
80 /**********************
81  * GLOBAL PROTOTYPES
82  **********************/
83 
84 /**
85  * Create a table object
86  * @param parent        pointer to an object, it will be the parent of the new table
87  * @return              pointer to the created table
88  */
89 lv_obj_t * lv_table_create(lv_obj_t * parent);
90 
91 /*=====================
92  * Setter functions
93  *====================*/
94 
95 /**
96  * Set the value of a cell.
97  * @param obj           pointer to a Table object
98  * @param row           id of the row [0 .. row_cnt -1]
99  * @param col           id of the column [0 .. col_cnt -1]
100  * @param txt           text to display in the cell. It will be copied and saved so this variable is not required after this function call.
101  * @note                New roes/columns are added automatically if required
102  */
103 void lv_table_set_cell_value(lv_obj_t * obj, uint16_t row, uint16_t col, const char * txt);
104 
105 /**
106  * Set the value of a cell.  Memory will be allocated to store the text by the table.
107  * @param obj           pointer to a Table object
108  * @param row           id of the row [0 .. row_cnt -1]
109  * @param col           id of the column [0 .. col_cnt -1]
110  * @param fmt           `printf`-like format
111  * @note                New roes/columns are added automatically if required
112  */
113 void lv_table_set_cell_value_fmt(lv_obj_t * obj, uint16_t row, uint16_t col, const char * fmt, ...);
114 
115 /**
116  * Set the number of rows
117  * @param obj           table pointer to a Table object
118  * @param row_cnt       number of rows
119  */
120 void lv_table_set_row_cnt(lv_obj_t * obj, uint16_t row_cnt);
121 
122 /**
123  * Set the number of columns
124  * @param obj       table pointer to a Table object
125  * @param col_cnt   number of columns.
126  */
127 void lv_table_set_col_cnt(lv_obj_t * obj, uint16_t col_cnt);
128 
129 /**
130  * Set the width of a column
131  * @param obj       table pointer to a Table object
132  * @param col_id    id of the column [0 .. LV_TABLE_COL_MAX -1]
133  * @param w         width of the column
134  */
135 void lv_table_set_col_width(lv_obj_t * obj, uint16_t col_id, lv_coord_t w);
136 
137 /**
138  * Add control bits to the cell.
139  * @param obj       pointer to a Table object
140  * @param row       id of the row [0 .. row_cnt -1]
141  * @param col       id of the column [0 .. col_cnt -1]
142  * @param ctrl      OR-ed values from ::lv_table_cell_ctrl_t
143  */
144 void lv_table_add_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl);
145 
146 /**
147  * Clear control bits of the cell.
148  * @param obj       pointer to a Table object
149  * @param row       id of the row [0 .. row_cnt -1]
150  * @param col       id of the column [0 .. col_cnt -1]
151  * @param ctrl      OR-ed values from ::lv_table_cell_ctrl_t
152  */
153 void lv_table_clear_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl);
154 
155 #if LV_USE_USER_DATA
156 /**
157  * Add custom user data to the cell.
158  * @param obj       pointer to a Table object
159  * @param row       id of the row [0 .. row_cnt -1]
160  * @param col       id of the column [0 .. col_cnt -1]
161  * @param user_data pointer to the new user_data. It must be allocated by user as it will be freed automatically
162  */
163 void lv_table_set_cell_user_data(lv_obj_t * obj, uint16_t row, uint16_t col, void * user_data);
164 #endif
165 
166 /*=====================
167  * Getter functions
168  *====================*/
169 
170 /**
171  * Get the value of a cell.
172  * @param obj       pointer to a Table object
173  * @param row       id of the row [0 .. row_cnt -1]
174  * @param col       id of the column [0 .. col_cnt -1]
175  * @return          text in the cell
176  */
177 const char * lv_table_get_cell_value(lv_obj_t * obj, uint16_t row, uint16_t col);
178 
179 /**
180  * Get the number of rows.
181  * @param obj       table pointer to a Table object
182  * @return          number of rows.
183  */
184 uint16_t lv_table_get_row_cnt(lv_obj_t * obj);
185 
186 /**
187  * Get the number of columns.
188  * @param obj       table pointer to a Table object
189  * @return          number of columns.
190  */
191 uint16_t lv_table_get_col_cnt(lv_obj_t * obj);
192 
193 /**
194  * Get the width of a column
195  * @param obj       table pointer to a Table object
196  * @param col       id of the column [0 .. LV_TABLE_COL_MAX -1]
197  * @return          width of the column
198  */
199 lv_coord_t lv_table_get_col_width(lv_obj_t * obj, uint16_t col);
200 
201 /**
202  * Get whether a cell has the control bits
203  * @param obj       pointer to a Table object
204  * @param row       id of the row [0 .. row_cnt -1]
205  * @param col       id of the column [0 .. col_cnt -1]
206  * @param ctrl      OR-ed values from ::lv_table_cell_ctrl_t
207  * @return          true: all control bits are set; false: not all control bits are set
208  */
209 bool lv_table_has_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl);
210 
211 /**
212  * Get the selected cell (pressed and or focused)
213  * @param obj       pointer to a table object
214  * @param row       pointer to variable to store the selected row (LV_TABLE_CELL_NONE: if no cell selected)
215  * @param col       pointer to variable to store the selected column  (LV_TABLE_CELL_NONE: if no cell selected)
216  */
217 void lv_table_get_selected_cell(lv_obj_t * obj, uint16_t * row, uint16_t * col);
218 
219 #if LV_USE_USER_DATA
220 /**
221  * Get custom user data to the cell.
222  * @param obj       pointer to a Table object
223  * @param row       id of the row [0 .. row_cnt -1]
224  * @param col       id of the column [0 .. col_cnt -1]
225  */
226 void * lv_table_get_cell_user_data(lv_obj_t * obj, uint16_t row, uint16_t col);
227 #endif
228 
229 /**********************
230  *      MACROS
231  **********************/
232 
233 #endif /*LV_USE_TABLE*/
234 
235 #ifdef __cplusplus
236 } /*extern "C"*/
237 #endif
238 
239 #endif /*LV_TABLE_H*/
240