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 table*/
50 typedef struct {
51     lv_obj_t obj;
52     uint16_t col_cnt;
53     uint16_t row_cnt;
54     char ** cell_data;
55     lv_coord_t * row_h;
56     lv_coord_t * col_w;
57     uint16_t col_act;
58     uint16_t row_act;
59 } lv_table_t;
60 
61 extern const lv_obj_class_t lv_table_class;
62 
63 /**
64  * `type` field in `lv_obj_draw_part_dsc_t` if `class_p = lv_table_class`
65  * Used in `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END`
66  */
67 typedef enum {
68     LV_TABLE_DRAW_PART_CELL,       /**< A cell*/
69 } lv_table_draw_part_type_t;
70 
71 /**********************
72  * GLOBAL PROTOTYPES
73  **********************/
74 
75 /**
76  * Create a table object
77  * @param parent        pointer to an object, it will be the parent of the new table
78  * @return              pointer to the created table
79  */
80 lv_obj_t * lv_table_create(lv_obj_t * parent);
81 
82 /*=====================
83  * Setter functions
84  *====================*/
85 
86 /**
87  * Set the value of a cell.
88  * @param obj           pointer to a Table object
89  * @param row           id of the row [0 .. row_cnt -1]
90  * @param col           id of the column [0 .. col_cnt -1]
91  * @param txt           text to display in the cell. It will be copied and saved so this variable is not required after this function call.
92  * @note                New roes/columns are added automatically if required
93  */
94 void lv_table_set_cell_value(lv_obj_t * obj, uint16_t row, uint16_t col, const char * txt);
95 
96 /**
97  * Set the value of a cell.  Memory will be allocated to store the text by the table.
98  * @param obj           pointer to a Table object
99  * @param row           id of the row [0 .. row_cnt -1]
100  * @param col           id of the column [0 .. col_cnt -1]
101  * @param fmt           `printf`-like format
102  * @note                New roes/columns are added automatically if required
103  */
104 void lv_table_set_cell_value_fmt(lv_obj_t * obj, uint16_t row, uint16_t col, const char * fmt, ...);
105 
106 /**
107  * Set the number of rows
108  * @param obj           table pointer to a Table object
109  * @param row_cnt       number of rows
110  */
111 void lv_table_set_row_cnt(lv_obj_t * obj, uint16_t row_cnt);
112 
113 /**
114  * Set the number of columns
115  * @param obj       table pointer to a Table object
116  * @param col_cnt   number of columns.
117  */
118 void lv_table_set_col_cnt(lv_obj_t * obj, uint16_t col_cnt);
119 
120 /**
121  * Set the width of a column
122  * @param obj       table pointer to a Table object
123  * @param col_id    id of the column [0 .. LV_TABLE_COL_MAX -1]
124  * @param w         width of the column
125  */
126 void lv_table_set_col_width(lv_obj_t * obj, uint16_t col_id, lv_coord_t w);
127 
128 /**
129  * Add control bits to the cell.
130  * @param obj       pointer to a Table object
131  * @param row       id of the row [0 .. row_cnt -1]
132  * @param col       id of the column [0 .. col_cnt -1]
133  * @param ctrl      OR-ed values from ::lv_table_cell_ctrl_t
134  */
135 void lv_table_add_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl);
136 
137 
138 /**
139  * Clear control bits of the cell.
140  * @param obj       pointer to a Table object
141  * @param row       id of the row [0 .. row_cnt -1]
142  * @param col       id of the column [0 .. col_cnt -1]
143  * @param ctrl      OR-ed values from ::lv_table_cell_ctrl_t
144  */
145 void lv_table_clear_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl);
146 
147 /*=====================
148  * Getter functions
149  *====================*/
150 
151 /**
152  * Get the value of a cell.
153  * @param obj       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  * @return          text in the cell
157  */
158 const char * lv_table_get_cell_value(lv_obj_t * obj, uint16_t row, uint16_t col);
159 
160 /**
161  * Get the number of rows.
162  * @param obj       table pointer to a Table object
163  * @return          number of rows.
164  */
165 uint16_t lv_table_get_row_cnt(lv_obj_t * obj);
166 
167 /**
168  * Get the number of columns.
169  * @param obj       table pointer to a Table object
170  * @return          number of columns.
171  */
172 uint16_t lv_table_get_col_cnt(lv_obj_t * obj);
173 
174 /**
175  * Get the width of a column
176  * @param obj       table pointer to a Table object
177  * @param col       id of the column [0 .. LV_TABLE_COL_MAX -1]
178  * @return          width of the column
179  */
180 lv_coord_t lv_table_get_col_width(lv_obj_t * obj, uint16_t col);
181 
182 /**
183  * Get whether a cell has the control bits
184  * @param obj       pointer to a Table object
185  * @param row       id of the row [0 .. row_cnt -1]
186  * @param col       id of the column [0 .. col_cnt -1]
187  * @param ctrl      OR-ed values from ::lv_table_cell_ctrl_t
188  * @return          true: all control bits are set; false: not all control bits are set
189  */
190 bool lv_table_has_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl);
191 
192 /**
193  * Get the selected cell (pressed and or focused)
194  * @param obj       pointer to a table object
195  * @param row       pointer to variable to store the selected row (LV_TABLE_CELL_NONE: if no cell selected)
196  * @param col       pointer to variable to store the selected column  (LV_TABLE_CELL_NONE: if no cell selected)
197  */
198 void lv_table_get_selected_cell(lv_obj_t * obj, uint16_t * row, uint16_t * col);
199 
200 /**********************
201  *      MACROS
202  **********************/
203 
204 #endif /*LV_USE_TABLE*/
205 
206 #ifdef __cplusplus
207 } /*extern "C"*/
208 #endif
209 
210 #endif /*LV_TABLE_H*/
211