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 /**
148  * Clear control bits of the cell.
149  * @param obj       pointer to a Table object
150  * @param row       id of the row [0 .. row_cnt -1]
151  * @param col       id of the column [0 .. col_cnt -1]
152  * @param ctrl      OR-ed values from ::lv_table_cell_ctrl_t
153  */
154 void lv_table_clear_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl);
155 
156 #if LV_USE_USER_DATA
157 /**
158  * Add custom user data to the cell.
159  * @param obj       pointer to a Table object
160  * @param row       id of the row [0 .. row_cnt -1]
161  * @param col       id of the column [0 .. col_cnt -1]
162  * @param user_data pointer to the new user_data. It must be allocated by user as it will be freed automatically
163  */
164 void lv_table_set_cell_user_data(lv_obj_t * obj, uint16_t row, uint16_t col, void * user_data);
165 #endif
166 
167 /*=====================
168  * Getter functions
169  *====================*/
170 
171 /**
172  * Get the value of a cell.
173  * @param obj       pointer to a Table object
174  * @param row       id of the row [0 .. row_cnt -1]
175  * @param col       id of the column [0 .. col_cnt -1]
176  * @return          text in the cell
177  */
178 const char * lv_table_get_cell_value(lv_obj_t * obj, uint16_t row, uint16_t col);
179 
180 /**
181  * Get the number of rows.
182  * @param obj       table pointer to a Table object
183  * @return          number of rows.
184  */
185 uint16_t lv_table_get_row_cnt(lv_obj_t * obj);
186 
187 /**
188  * Get the number of columns.
189  * @param obj       table pointer to a Table object
190  * @return          number of columns.
191  */
192 uint16_t lv_table_get_col_cnt(lv_obj_t * obj);
193 
194 /**
195  * Get the width of a column
196  * @param obj       table pointer to a Table object
197  * @param col       id of the column [0 .. LV_TABLE_COL_MAX -1]
198  * @return          width of the column
199  */
200 lv_coord_t lv_table_get_col_width(lv_obj_t * obj, uint16_t col);
201 
202 /**
203  * Get whether a cell has the control bits
204  * @param obj       pointer to a Table object
205  * @param row       id of the row [0 .. row_cnt -1]
206  * @param col       id of the column [0 .. col_cnt -1]
207  * @param ctrl      OR-ed values from ::lv_table_cell_ctrl_t
208  * @return          true: all control bits are set; false: not all control bits are set
209  */
210 bool lv_table_has_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl);
211 
212 /**
213  * Get the selected cell (pressed and or focused)
214  * @param obj       pointer to a table object
215  * @param row       pointer to variable to store the selected row (LV_TABLE_CELL_NONE: if no cell selected)
216  * @param col       pointer to variable to store the selected column  (LV_TABLE_CELL_NONE: if no cell selected)
217  */
218 void lv_table_get_selected_cell(lv_obj_t * obj, uint16_t * row, uint16_t * col);
219 
220 #if LV_USE_USER_DATA
221 /**
222  * Get custom user data to the cell.
223  * @param obj       pointer to a Table object
224  * @param row       id of the row [0 .. row_cnt -1]
225  * @param col       id of the column [0 .. col_cnt -1]
226  */
227 void * lv_table_get_cell_user_data(lv_obj_t * obj, uint16_t row, uint16_t col);
228 #endif
229 
230 /**********************
231  *      MACROS
232  **********************/
233 
234 #endif /*LV_USE_TABLE*/
235 
236 #ifdef __cplusplus
237 } /*extern "C"*/
238 #endif
239 
240 #endif /*LV_TABLE_H*/
241