1 /**
2  * @file lv_table.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_table.h"
10 #if LV_USE_TABLE != 0
11 
12 #include "../core/lv_indev.h"
13 #include "../misc/lv_assert.h"
14 #include "../misc/lv_txt.h"
15 #include "../misc/lv_txt_ap.h"
16 #include "../misc/lv_math.h"
17 #include "../misc/lv_printf.h"
18 #include "../draw/lv_draw.h"
19 
20 /*********************
21  *      DEFINES
22  *********************/
23 #define MY_CLASS &lv_table_class
24 
25 /**********************
26  *      TYPEDEFS
27  **********************/
28 
29 /**********************
30  *  STATIC PROTOTYPES
31  **********************/
32 static void lv_table_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
33 static void lv_table_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
34 static void lv_table_event(const lv_obj_class_t * class_p, lv_event_t * e);
35 static void draw_main(lv_event_t * e);
36 static lv_coord_t get_row_height(lv_obj_t * obj, uint16_t row_id, const lv_font_t * font,
37                                  lv_coord_t letter_space, lv_coord_t line_space,
38                                  lv_coord_t cell_left, lv_coord_t cell_right, lv_coord_t cell_top, lv_coord_t cell_bottom);
39 static void refr_size(lv_obj_t * obj, uint32_t strat_row);
40 static lv_res_t get_pressed_cell(lv_obj_t * obj, uint16_t * row, uint16_t * col);
41 
42 /**********************
43  *  STATIC VARIABLES
44  **********************/
45 const lv_obj_class_t lv_table_class  = {
46     .constructor_cb = lv_table_constructor,
47     .destructor_cb = lv_table_destructor,
48     .event_cb = lv_table_event,
49     .width_def = LV_SIZE_CONTENT,
50     .height_def = LV_SIZE_CONTENT,
51     .base_class = &lv_obj_class,
52     .editable = LV_OBJ_CLASS_EDITABLE_TRUE,
53     .group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
54     .instance_size = sizeof(lv_table_t),
55 };
56 /**********************
57  *      MACROS
58  **********************/
59 
60 /**********************
61  *   GLOBAL FUNCTIONS
62  **********************/
63 
lv_table_create(lv_obj_t * parent)64 lv_obj_t * lv_table_create(lv_obj_t * parent)
65 {
66     LV_LOG_INFO("begin");
67     lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS, parent);
68     lv_obj_class_init_obj(obj);
69     return obj;
70 }
71 
72 /*=====================
73  * Setter functions
74  *====================*/
75 
lv_table_set_cell_value(lv_obj_t * obj,uint16_t row,uint16_t col,const char * txt)76 void lv_table_set_cell_value(lv_obj_t * obj, uint16_t row, uint16_t col, const char * txt)
77 {
78     LV_ASSERT_OBJ(obj, MY_CLASS);
79     LV_ASSERT_NULL(txt);
80 
81     lv_table_t * table = (lv_table_t *)obj;
82 
83     /*Auto expand*/
84     if(col >= table->col_cnt) lv_table_set_col_cnt(obj, col + 1);
85     if(row >= table->row_cnt) lv_table_set_row_cnt(obj, row + 1);
86 
87     uint32_t cell = row * table->col_cnt + col;
88     lv_table_cell_ctrl_t ctrl = 0;
89 
90     /*Save the control byte*/
91     if(table->cell_data[cell]) ctrl = table->cell_data[cell][0];
92 
93 #if LV_USE_ARABIC_PERSIAN_CHARS
94     /*Get the size of the Arabic text and process it*/
95     size_t len_ap = _lv_txt_ap_calc_bytes_cnt(txt);
96     table->cell_data[cell] = lv_mem_realloc(table->cell_data[cell], len_ap + 1);
97     LV_ASSERT_MALLOC(table->cell_data[cell]);
98     if(table->cell_data[cell] == NULL) return;
99 
100     _lv_txt_ap_proc(txt, &table->cell_data[cell][1]);
101 #else
102     table->cell_data[cell] = lv_mem_realloc(table->cell_data[cell], strlen(txt) + 2); /*+1: trailing '\0; +1: format byte*/
103     LV_ASSERT_MALLOC(table->cell_data[cell]);
104 
105     strcpy(table->cell_data[cell] + 1, txt);  /*+1 to skip the format byte*/
106 #endif
107 
108     table->cell_data[cell][0] = ctrl;
109     refr_size(obj, row);
110 
111     lv_obj_invalidate(obj);
112 }
113 
lv_table_set_cell_value_fmt(lv_obj_t * obj,uint16_t row,uint16_t col,const char * fmt,...)114 void lv_table_set_cell_value_fmt(lv_obj_t * obj, uint16_t row, uint16_t col, const char * fmt, ...)
115 {
116     LV_ASSERT_OBJ(obj, MY_CLASS);
117     LV_ASSERT_NULL(fmt);
118 
119     lv_table_t * table = (lv_table_t *)obj;
120     if(col >= table->col_cnt) {
121         LV_LOG_WARN("lv_table_set_cell_value: invalid column");
122         return;
123     }
124 
125     /*Auto expand*/
126     if(row >= table->row_cnt) {
127         lv_table_set_row_cnt(obj, row + 1);
128     }
129 
130     uint32_t cell = row * table->col_cnt + col;
131     lv_table_cell_ctrl_t ctrl = 0;
132 
133     /*Save the control byte*/
134     if(table->cell_data[cell]) ctrl = table->cell_data[cell][0];
135 
136     va_list ap, ap2;
137     va_start(ap, fmt);
138     va_copy(ap2, ap);
139 
140     /*Allocate space for the new text by using trick from C99 standard section 7.19.6.12*/
141     uint32_t len = lv_vsnprintf(NULL, 0, fmt, ap);
142     va_end(ap);
143 
144 #if LV_USE_ARABIC_PERSIAN_CHARS
145     /*Put together the text according to the format string*/
146     char * raw_txt = lv_mem_buf_get(len + 1);
147     LV_ASSERT_MALLOC(raw_txt);
148     if(raw_txt == NULL) {
149         va_end(ap2);
150         return;
151     }
152 
153     lv_vsnprintf(raw_txt, len + 1, fmt, ap2);
154 
155     /*Get the size of the Arabic text and process it*/
156     size_t len_ap = _lv_txt_ap_calc_bytes_cnt(raw_txt);
157     table->cell_data[cell] = lv_mem_realloc(table->cell_data[cell], len_ap + 1);
158     LV_ASSERT_MALLOC(table->cell_data[cell]);
159     if(table->cell_data[cell] == NULL) {
160         va_end(ap2);
161         return;
162     }
163     _lv_txt_ap_proc(raw_txt, &table->cell_data[cell][1]);
164 
165     lv_mem_buf_release(raw_txt);
166 #else
167     table->cell_data[cell] = lv_mem_realloc(table->cell_data[cell], len + 2); /*+1: trailing '\0; +1: format byte*/
168     LV_ASSERT_MALLOC(table->cell_data[cell]);
169     if(table->cell_data[cell] == NULL) {
170         va_end(ap2);
171         return;
172     }
173 
174     table->cell_data[cell][len + 1] = 0; /*Ensure NULL termination*/
175 
176     lv_vsnprintf(&table->cell_data[cell][1], len + 1, fmt, ap2);
177 #endif
178 
179     va_end(ap2);
180 
181     table->cell_data[cell][0] = ctrl;
182 
183     /*Refresh the row height*/
184     lv_coord_t cell_left = lv_obj_get_style_pad_left(obj, LV_PART_ITEMS);
185     lv_coord_t cell_right = lv_obj_get_style_pad_right(obj, LV_PART_ITEMS);
186     lv_coord_t cell_top = lv_obj_get_style_pad_top(obj, LV_PART_ITEMS);
187     lv_coord_t cell_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_ITEMS);
188 
189     lv_coord_t letter_space = lv_obj_get_style_text_letter_space(obj, LV_PART_ITEMS);
190     lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_ITEMS);
191     const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_ITEMS);
192 
193     lv_coord_t h = get_row_height(obj, row, font, letter_space, line_space,
194                                   cell_left, cell_right, cell_top, cell_bottom);
195 
196 
197     lv_coord_t minh = lv_obj_get_style_min_height(obj, LV_PART_ITEMS);
198     lv_coord_t maxh = lv_obj_get_style_max_height(obj, LV_PART_ITEMS);
199 
200     table->row_h[row] = LV_CLAMP(minh, h, maxh);
201 
202     lv_obj_invalidate(obj);
203 }
204 
lv_table_set_row_cnt(lv_obj_t * obj,uint16_t row_cnt)205 void lv_table_set_row_cnt(lv_obj_t * obj, uint16_t row_cnt)
206 {
207     LV_ASSERT_OBJ(obj, MY_CLASS);
208 
209     lv_table_t * table = (lv_table_t *)obj;
210     uint16_t old_row_cnt = table->row_cnt;
211     table->row_cnt         = row_cnt;
212 
213     table->row_h = lv_mem_realloc(table->row_h, table->row_cnt * sizeof(table->row_h[0]));
214     LV_ASSERT_MALLOC(table->row_h);
215     if(table->row_h == NULL) return;
216 
217     /*Free the unused cells*/
218     if(old_row_cnt > row_cnt) {
219         uint16_t old_cell_cnt = old_row_cnt * table->col_cnt;
220         uint32_t new_cell_cnt = table->col_cnt * table->row_cnt;
221         uint32_t i;
222         for(i = new_cell_cnt; i < old_cell_cnt; i++) {
223             lv_mem_free(table->cell_data[i]);
224         }
225     }
226 
227     table->cell_data = lv_mem_realloc(table->cell_data, table->row_cnt * table->col_cnt * sizeof(char *));
228     LV_ASSERT_MALLOC(table->cell_data);
229     if(table->cell_data == NULL) return;
230 
231     /*Initialize the new fields*/
232     if(old_row_cnt < row_cnt) {
233         uint32_t old_cell_cnt = old_row_cnt * table->col_cnt;
234         uint32_t new_cell_cnt = table->col_cnt * table->row_cnt;
235         lv_memset_00(&table->cell_data[old_cell_cnt], (new_cell_cnt - old_cell_cnt) * sizeof(table->cell_data[0]));
236     }
237 
238     refr_size(obj, 0) ;
239 }
240 
lv_table_set_col_cnt(lv_obj_t * obj,uint16_t col_cnt)241 void lv_table_set_col_cnt(lv_obj_t * obj, uint16_t col_cnt)
242 {
243     LV_ASSERT_OBJ(obj, MY_CLASS);
244 
245     lv_table_t * table = (lv_table_t *)obj;
246     uint16_t old_col_cnt = table->col_cnt;
247     table->col_cnt         = col_cnt;
248     table->col_w = lv_mem_realloc(table->col_w, col_cnt * sizeof(table->row_h[0]));
249     LV_ASSERT_MALLOC(table->col_w);
250     if(table->col_w == NULL) return;
251 
252     /*Free the unused cells*/
253     if(old_col_cnt > col_cnt) {
254         uint16_t old_cell_cnt = old_col_cnt * table->row_cnt;
255         uint32_t new_cell_cnt = table->col_cnt * table->row_cnt;
256         uint32_t i;
257         for(i = new_cell_cnt; i < old_cell_cnt; i++) {
258             lv_mem_free(table->cell_data[i]);
259         }
260     }
261 
262     char ** new_cell_data = lv_mem_alloc(table->row_cnt * table->col_cnt * sizeof(char *));
263     LV_ASSERT_MALLOC(new_cell_data);
264     if(new_cell_data == NULL) return;
265     uint32_t new_cell_cnt = table->col_cnt * table->row_cnt;
266     lv_memset_00(new_cell_data, new_cell_cnt * sizeof(table->cell_data[0]));
267 
268     /*Initialize the new fields*/
269     if(old_col_cnt < col_cnt) {
270         uint32_t col;
271         for(col = old_col_cnt; col < col_cnt; col++) {
272             table->col_w[col] = LV_DPI_DEF;
273         }
274     }
275 
276     /*The new column(s) messes up the mapping of `cell_data`*/
277     uint32_t old_col_start;
278     uint32_t new_col_start;
279     uint32_t min_col_cnt = LV_MIN(old_col_cnt, col_cnt);
280     uint32_t row;
281     for(row = 0; row < table->row_cnt; row++) {
282         old_col_start = row * old_col_cnt;
283         new_col_start = row * col_cnt;
284 
285         lv_memcpy_small(&new_cell_data[new_col_start], &table->cell_data[old_col_start],
286                         sizeof(new_cell_data[0]) * min_col_cnt);
287     }
288 
289     lv_mem_free(table->cell_data);
290     table->cell_data = new_cell_data;
291 
292 
293     refr_size(obj, 0) ;
294 }
295 
lv_table_set_col_width(lv_obj_t * obj,uint16_t col_id,lv_coord_t w)296 void lv_table_set_col_width(lv_obj_t * obj, uint16_t col_id, lv_coord_t w)
297 {
298     LV_ASSERT_OBJ(obj, MY_CLASS);
299 
300     lv_table_t * table = (lv_table_t *)obj;
301 
302     /*Auto expand*/
303     if(col_id >= table->col_cnt) lv_table_set_col_cnt(obj, col_id + 1);
304 
305     table->col_w[col_id] = w;
306     refr_size(obj, 0) ;
307 }
308 
lv_table_add_cell_ctrl(lv_obj_t * obj,uint16_t row,uint16_t col,lv_table_cell_ctrl_t ctrl)309 void lv_table_add_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl)
310 {
311     LV_ASSERT_OBJ(obj, MY_CLASS);
312 
313     lv_table_t * table = (lv_table_t *)obj;
314 
315     /*Auto expand*/
316     if(col >= table->col_cnt) lv_table_set_col_cnt(obj, col + 1);
317     if(row >= table->row_cnt) lv_table_set_row_cnt(obj, row + 1);
318 
319     uint32_t cell = row * table->col_cnt + col;
320 
321     if(table->cell_data[cell] == NULL) {
322         table->cell_data[cell]    = lv_mem_alloc(2); /*+1: trailing '\0; +1: format byte*/
323         LV_ASSERT_MALLOC(table->cell_data[cell]);
324         if(table->cell_data[cell] == NULL) return;
325 
326         table->cell_data[cell][0] = 0;
327         table->cell_data[cell][1] = '\0';
328     }
329 
330     table->cell_data[cell][0] |= ctrl;
331 }
332 
lv_table_clear_cell_ctrl(lv_obj_t * obj,uint16_t row,uint16_t col,lv_table_cell_ctrl_t ctrl)333 void lv_table_clear_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl)
334 {
335     LV_ASSERT_OBJ(obj, MY_CLASS);
336 
337     lv_table_t * table = (lv_table_t *)obj;
338 
339     /*Auto expand*/
340     if(col >= table->col_cnt) lv_table_set_col_cnt(obj, col + 1);
341     if(row >= table->row_cnt) lv_table_set_row_cnt(obj, row + 1);
342 
343     uint32_t cell = row * table->col_cnt + col;
344 
345     if(table->cell_data[cell] == NULL) {
346         table->cell_data[cell]    = lv_mem_alloc(2); /*+1: trailing '\0; +1: format byte*/
347         LV_ASSERT_MALLOC(table->cell_data[cell]);
348         if(table->cell_data[cell] == NULL) return;
349 
350         table->cell_data[cell][0] = 0;
351         table->cell_data[cell][1] = '\0';
352     }
353 
354     table->cell_data[cell][0] &= (~ctrl);
355 }
356 
357 /*=====================
358  * Getter functions
359  *====================*/
360 
lv_table_get_cell_value(lv_obj_t * obj,uint16_t row,uint16_t col)361 const char * lv_table_get_cell_value(lv_obj_t * obj, uint16_t row, uint16_t col)
362 {
363     LV_ASSERT_OBJ(obj, MY_CLASS);
364 
365     lv_table_t * table = (lv_table_t *)obj;
366     if(row >= table->row_cnt || col >= table->col_cnt) {
367         LV_LOG_WARN("lv_table_set_cell_value: invalid row or column");
368         return "";
369     }
370     uint32_t cell = row * table->col_cnt + col;
371 
372     if(table->cell_data[cell] == NULL) return "";
373 
374     return &table->cell_data[cell][1]; /*Skip the format byte*/
375 }
376 
lv_table_get_row_cnt(lv_obj_t * obj)377 uint16_t lv_table_get_row_cnt(lv_obj_t * obj)
378 {
379     LV_ASSERT_OBJ(obj, MY_CLASS);
380 
381     lv_table_t * table = (lv_table_t *)obj;
382     return table->row_cnt;
383 }
384 
lv_table_get_col_cnt(lv_obj_t * obj)385 uint16_t lv_table_get_col_cnt(lv_obj_t * obj)
386 {
387     LV_ASSERT_OBJ(obj, MY_CLASS);
388 
389     lv_table_t * table = (lv_table_t *)obj;
390     return table->col_cnt;
391 }
392 
lv_table_get_col_width(lv_obj_t * obj,uint16_t col)393 lv_coord_t lv_table_get_col_width(lv_obj_t * obj, uint16_t col)
394 {
395     LV_ASSERT_OBJ(obj, MY_CLASS);
396 
397     lv_table_t * table = (lv_table_t *)obj;
398 
399     if(col >= table->col_cnt) {
400         LV_LOG_WARN("lv_table_set_col_width: too big 'col_id'. Must be < LV_TABLE_COL_MAX.");
401         return 0;
402     }
403 
404     return table->col_w[col];
405 }
406 
lv_table_has_cell_ctrl(lv_obj_t * obj,uint16_t row,uint16_t col,lv_table_cell_ctrl_t ctrl)407 bool lv_table_has_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl)
408 {
409     LV_ASSERT_OBJ(obj, MY_CLASS);
410 
411     lv_table_t * table = (lv_table_t *)obj;
412     if(row >= table->row_cnt || col >= table->col_cnt) {
413         LV_LOG_WARN("lv_table_get_cell_crop: invalid row or column");
414         return false;
415     }
416     uint32_t cell = row * table->col_cnt + col;
417 
418     if(table->cell_data[cell] == NULL) return false;
419     else return (table->cell_data[cell][0] & ctrl) == ctrl ? true : false;
420 }
421 
lv_table_get_selected_cell(lv_obj_t * obj,uint16_t * row,uint16_t * col)422 void lv_table_get_selected_cell(lv_obj_t * obj, uint16_t * row, uint16_t * col)
423 {
424     lv_table_t * table = (lv_table_t *)obj;
425     *row = table->row_act;
426     *col = table->col_act;
427 }
428 
429 /**********************
430  *   STATIC FUNCTIONS
431  **********************/
432 
lv_table_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)433 static void lv_table_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
434 {
435     LV_UNUSED(class_p);
436     LV_TRACE_OBJ_CREATE("begin");
437 
438     lv_table_t * table = (lv_table_t *)obj;
439 
440     table->col_cnt = 1;
441     table->row_cnt = 1;
442     table->col_w = lv_mem_alloc(table->col_cnt * sizeof(table->col_w[0]));
443     table->row_h = lv_mem_alloc(table->row_cnt * sizeof(table->row_h[0]));
444     table->col_w[0] = LV_DPI_DEF;
445     table->row_h[0] = LV_DPI_DEF;
446     table->cell_data = lv_mem_realloc(table->cell_data, table->row_cnt * table->col_cnt * sizeof(char *));
447     table->cell_data[0] = NULL;
448 
449     LV_TRACE_OBJ_CREATE("finished");
450 }
451 
lv_table_destructor(const lv_obj_class_t * class_p,lv_obj_t * obj)452 static void lv_table_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
453 {
454     LV_UNUSED(class_p);
455     lv_table_t * table = (lv_table_t *)obj;
456     /*Free the cell texts*/
457     uint16_t i;
458     for(i = 0; i < table->col_cnt * table->row_cnt; i++) {
459         if(table->cell_data[i]) {
460             lv_mem_free(table->cell_data[i]);
461             table->cell_data[i] = NULL;
462         }
463     }
464 
465     if(table->cell_data) lv_mem_free(table->cell_data);
466     if(table->row_h) lv_mem_free(table->row_h);
467     if(table->col_w) lv_mem_free(table->col_w);
468 }
469 
lv_table_event(const lv_obj_class_t * class_p,lv_event_t * e)470 static void lv_table_event(const lv_obj_class_t * class_p, lv_event_t * e)
471 {
472     LV_UNUSED(class_p);
473 
474     lv_res_t res;
475 
476     /*Call the ancestor's event handler*/
477     res = lv_obj_event_base(MY_CLASS, e);
478     if(res != LV_RES_OK) return;
479 
480     lv_event_code_t code = lv_event_get_code(e);
481     lv_obj_t * obj = lv_event_get_target(e);
482     lv_table_t * table = (lv_table_t *)obj;
483 
484     if(code == LV_EVENT_STYLE_CHANGED) {
485         refr_size(obj, 0);
486     }
487     else if(code == LV_EVENT_GET_SELF_SIZE) {
488         lv_point_t * p = lv_event_get_param(e);
489         uint32_t i;
490         lv_coord_t w = 0;
491         for(i = 0; i < table->col_cnt; i++) w += table->col_w[i];
492 
493         lv_coord_t h = 0;
494         for(i = 0; i < table->row_cnt; i++) h += table->row_h[i];
495 
496         p->x = w - 1;
497         p->y = h - 1;
498     }
499     else if(code == LV_EVENT_PRESSED || code == LV_EVENT_PRESSING) {
500         uint16_t col;
501         uint16_t row;
502         lv_res_t pr_res = get_pressed_cell(obj, &row, &col);
503 
504         if(pr_res == LV_RES_OK && (table->col_act != col || table->row_act != row)) {
505             table->col_act = col;
506             table->row_act = row;
507             lv_obj_invalidate(obj);
508         }
509     }
510     else if(code == LV_EVENT_RELEASED) {
511         lv_obj_invalidate(obj);
512         lv_indev_t * indev = lv_indev_get_act();
513         lv_obj_t * scroll_obj = lv_indev_get_scroll_obj(indev);
514         if(table->col_act != LV_TABLE_CELL_NONE && table->row_act != LV_TABLE_CELL_NONE && scroll_obj == NULL) {
515             res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
516             if(res != LV_RES_OK) return;
517         }
518 
519         lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
520         if(indev_type == LV_INDEV_TYPE_POINTER || indev_type == LV_INDEV_TYPE_BUTTON) {
521             table->col_act = LV_TABLE_CELL_NONE;
522             table->row_act = LV_TABLE_CELL_NONE;
523         }
524     }
525     else if(code == LV_EVENT_FOCUSED) {
526         lv_obj_invalidate(obj);
527     }
528     else if(code == LV_EVENT_KEY) {
529         int32_t c = *((int32_t *)lv_event_get_param(e));
530         int32_t col = table->col_act;
531         int32_t row = table->row_act;
532         if(col == LV_TABLE_CELL_NONE || row == LV_TABLE_CELL_NONE) {
533             table->col_act = 0;
534             table->row_act = 0;
535             lv_obj_invalidate(obj);
536             return;
537         }
538 
539         if(col >= table->col_cnt) col = 0;
540         if(row >= table->row_cnt) row = 0;
541 
542         if(c == LV_KEY_LEFT) col--;
543         else if(c == LV_KEY_RIGHT) col++;
544         else if(c == LV_KEY_UP) row--;
545         else if(c == LV_KEY_DOWN) row++;
546         else return;
547 
548         if(col >= table->col_cnt) {
549             if(row < table->row_cnt - 1) {
550                 col = 0;
551                 row++;
552             }
553             else {
554                 col = table->col_cnt - 1;
555             }
556         }
557         else if(col < 0) {
558             if(row != 0) {
559                 col = table->col_cnt - 1;
560                 row--;
561             }
562             else {
563                 col = 0;
564             }
565         }
566 
567         if(row >= table->row_cnt) {
568             row = table->row_cnt - 1;
569         }
570         else if(row < 0) {
571             row = 0;
572         }
573 
574         if(table->col_act != col || table->row_act != row) {
575             table->col_act = col;
576             table->row_act = row;
577             lv_obj_invalidate(obj);
578 
579             res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
580             if(res != LV_RES_OK) return;
581         }
582     }
583     else if(code == LV_EVENT_DRAW_MAIN) {
584         draw_main(e);
585     }
586 }
587 
588 
draw_main(lv_event_t * e)589 static void draw_main(lv_event_t * e)
590 {
591     lv_obj_t * obj = lv_event_get_target(e);
592     lv_table_t * table = (lv_table_t *)obj;
593     lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
594     lv_area_t clip_area;
595     if(!_lv_area_intersect(&clip_area, &obj->coords, draw_ctx->clip_area)) return;
596 
597     const lv_area_t * clip_area_ori = draw_ctx->clip_area;
598     draw_ctx->clip_area = &clip_area;
599 
600     lv_point_t txt_size;
601     lv_area_t cell_area;
602     lv_area_t txt_area;
603     lv_text_flag_t txt_flags;
604 
605     lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
606     lv_coord_t bg_top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
607     lv_coord_t bg_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN);
608     lv_coord_t bg_left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
609     lv_coord_t bg_right = lv_obj_get_style_pad_right(obj, LV_PART_MAIN);
610 
611     lv_coord_t cell_left = lv_obj_get_style_pad_left(obj, LV_PART_ITEMS);
612     lv_coord_t cell_right = lv_obj_get_style_pad_right(obj, LV_PART_ITEMS);
613     lv_coord_t cell_top = lv_obj_get_style_pad_top(obj, LV_PART_ITEMS);
614     lv_coord_t cell_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_ITEMS);
615 
616     lv_state_t state_ori = obj->state;
617     obj->state = LV_STATE_DEFAULT;
618     obj->skip_trans = 1;
619     lv_draw_rect_dsc_t rect_dsc_def;
620     lv_draw_rect_dsc_t rect_dsc_act; /*Passed to the event to modify it*/
621     lv_draw_rect_dsc_init(&rect_dsc_def);
622     lv_obj_init_draw_rect_dsc(obj, LV_PART_ITEMS, &rect_dsc_def);
623 
624     lv_draw_label_dsc_t label_dsc_def;
625     lv_draw_label_dsc_t label_dsc_act;  /*Passed to the event to modify it*/
626     lv_draw_label_dsc_init(&label_dsc_def);
627     lv_obj_init_draw_label_dsc(obj, LV_PART_ITEMS, &label_dsc_def);
628     obj->state = state_ori;
629     obj->skip_trans = 0;
630 
631     uint16_t col;
632     uint16_t row;
633     uint16_t cell = 0;
634 
635     cell_area.y2 = obj->coords.y1 + bg_top - 1 - lv_obj_get_scroll_y(obj) + border_width;
636     lv_coord_t scroll_x = lv_obj_get_scroll_x(obj) ;
637     bool rtl = lv_obj_get_style_base_dir(obj, LV_PART_MAIN) == LV_BASE_DIR_RTL ? true : false;
638 
639     /*Handle custom drawer*/
640     lv_obj_draw_part_dsc_t part_draw_dsc;
641     lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
642     part_draw_dsc.part = LV_PART_ITEMS;
643     part_draw_dsc.class_p = MY_CLASS;
644     part_draw_dsc.type = LV_TABLE_DRAW_PART_CELL;
645     part_draw_dsc.rect_dsc = &rect_dsc_act;
646     part_draw_dsc.label_dsc = &label_dsc_act;
647 
648     for(row = 0; row < table->row_cnt; row++) {
649         lv_coord_t h_row = table->row_h[row];
650 
651         cell_area.y1 = cell_area.y2 + 1;
652         cell_area.y2 = cell_area.y1 + h_row - 1;
653 
654         if(cell_area.y1 > clip_area.y2) break;
655 
656         if(rtl) cell_area.x1 = obj->coords.x2 - bg_right - 1 - scroll_x - border_width;
657         else cell_area.x2 = obj->coords.x1 + bg_left - 1 - scroll_x + border_width;
658 
659         for(col = 0; col < table->col_cnt; col++) {
660             lv_table_cell_ctrl_t ctrl = 0;
661             if(table->cell_data[cell]) ctrl = table->cell_data[cell][0];
662 
663             if(rtl) {
664                 cell_area.x2 = cell_area.x1 - 1;
665                 cell_area.x1 = cell_area.x2 - table->col_w[col] + 1;
666             }
667             else {
668                 cell_area.x1 = cell_area.x2 + 1;
669                 cell_area.x2 = cell_area.x1 + table->col_w[col] - 1;
670             }
671 
672             uint16_t col_merge = 0;
673             for(col_merge = 0; col_merge + col < table->col_cnt - 1; col_merge++) {
674                 if(table->cell_data[cell + col_merge]) {
675                     char * next_cell_data = table->cell_data[cell + col_merge];
676                     if(next_cell_data) ctrl = next_cell_data[0];
677                     if(ctrl & LV_TABLE_CELL_CTRL_MERGE_RIGHT)
678                         if(rtl) cell_area.x1 -= table->col_w[col + col_merge + 1];
679                         else cell_area.x2 += table->col_w[col + col_merge + 1];
680                     else {
681                         break;
682                     }
683                 }
684                 else {
685                     break;
686                 }
687             }
688 
689             if(cell_area.y2 < clip_area.y1) {
690                 cell += col_merge + 1;
691                 col += col_merge;
692                 continue;
693             }
694 
695             /*Expand the cell area with a half border to avoid drawing 2 borders next to each other*/
696             lv_area_t cell_area_border;
697             lv_area_copy(&cell_area_border, &cell_area);
698             if((rect_dsc_def.border_side & LV_BORDER_SIDE_LEFT) && cell_area_border.x1 > obj->coords.x1 + bg_left) {
699                 cell_area_border.x1 -= rect_dsc_def.border_width / 2;
700             }
701             if((rect_dsc_def.border_side & LV_BORDER_SIDE_TOP) && cell_area_border.y1 > obj->coords.y1 + bg_top) {
702                 cell_area_border.y1 -= rect_dsc_def.border_width / 2;
703             }
704             if((rect_dsc_def.border_side & LV_BORDER_SIDE_RIGHT) && cell_area_border.x2 < obj->coords.x2 - bg_right - 1) {
705                 cell_area_border.x2 += rect_dsc_def.border_width / 2 + (rect_dsc_def.border_width & 0x1);
706             }
707             if((rect_dsc_def.border_side & LV_BORDER_SIDE_BOTTOM) &&
708                cell_area_border.y2 < obj->coords.y2 - bg_bottom - 1) {
709                 cell_area_border.y2 += rect_dsc_def.border_width / 2 + (rect_dsc_def.border_width & 0x1);
710             }
711 
712             lv_state_t cell_state = LV_STATE_DEFAULT;
713             if(row == table->row_act && col == table->col_act) {
714                 if(!(obj->state & LV_STATE_SCROLLED) && (obj->state & LV_STATE_PRESSED)) cell_state |= LV_STATE_PRESSED;
715                 if(obj->state & LV_STATE_FOCUSED) cell_state |= LV_STATE_FOCUSED;
716                 if(obj->state & LV_STATE_FOCUS_KEY) cell_state |= LV_STATE_FOCUS_KEY;
717                 if(obj->state & LV_STATE_EDITED) cell_state |= LV_STATE_EDITED;
718             }
719 
720             /*Set up the draw descriptors*/
721             if(cell_state == LV_STATE_DEFAULT) {
722                 lv_memcpy(&rect_dsc_act, &rect_dsc_def, sizeof(lv_draw_rect_dsc_t));
723                 lv_memcpy(&label_dsc_act, &label_dsc_def, sizeof(lv_draw_label_dsc_t));
724             }
725             /*In other cases get the styles directly without caching them*/
726             else {
727                 obj->state = cell_state;
728                 obj->skip_trans = 1;
729                 lv_draw_rect_dsc_init(&rect_dsc_act);
730                 lv_draw_label_dsc_init(&label_dsc_act);
731                 lv_obj_init_draw_rect_dsc(obj, LV_PART_ITEMS, &rect_dsc_act);
732                 lv_obj_init_draw_label_dsc(obj, LV_PART_ITEMS, &label_dsc_act);
733                 obj->state = state_ori;
734                 obj->skip_trans = 0;
735             }
736 
737             part_draw_dsc.draw_area = &cell_area_border;
738             part_draw_dsc.id = row * table->col_cnt + col;
739             lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
740 
741             lv_draw_rect(draw_ctx, &rect_dsc_act, &cell_area_border);
742 
743             if(table->cell_data[cell]) {
744                 txt_area.x1 = cell_area.x1 + cell_left;
745                 txt_area.x2 = cell_area.x2 - cell_right;
746                 txt_area.y1 = cell_area.y1 + cell_top;
747                 txt_area.y2 = cell_area.y2 - cell_bottom;
748 
749                 /*Align the content to the middle if not cropped*/
750                 bool crop = ctrl & LV_TABLE_CELL_CTRL_TEXT_CROP ? true : false;
751                 if(crop) txt_flags = LV_TEXT_FLAG_EXPAND;
752                 else txt_flags = LV_TEXT_FLAG_NONE;
753 
754                 lv_txt_get_size(&txt_size, table->cell_data[cell] + 1, label_dsc_def.font,
755                                 label_dsc_act.letter_space, label_dsc_act.line_space,
756                                 lv_area_get_width(&txt_area), txt_flags);
757 
758                 /*Align the content to the middle if not cropped*/
759                 if(!crop) {
760                     txt_area.y1 = cell_area.y1 + h_row / 2 - txt_size.y / 2;
761                     txt_area.y2 = cell_area.y1 + h_row / 2 + txt_size.y / 2;
762                 }
763 
764                 lv_area_t label_clip_area;
765                 bool label_mask_ok;
766                 label_mask_ok = _lv_area_intersect(&label_clip_area, &clip_area, &cell_area);
767                 if(label_mask_ok) {
768                     draw_ctx->clip_area = &label_clip_area;
769                     lv_draw_label(draw_ctx, &label_dsc_act, &txt_area, table->cell_data[cell] + 1, NULL);
770                     draw_ctx->clip_area = &clip_area;
771                 }
772             }
773 
774             lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
775 
776             cell += col_merge + 1;
777             col += col_merge;
778         }
779     }
780 
781     draw_ctx->clip_area = clip_area_ori;
782 }
783 
refr_size(lv_obj_t * obj,uint32_t strat_row)784 static void refr_size(lv_obj_t * obj, uint32_t strat_row)
785 {
786     lv_table_t * table = (lv_table_t *)obj;
787 
788     uint32_t i;
789 
790     lv_coord_t cell_left = lv_obj_get_style_pad_left(obj, LV_PART_ITEMS);
791     lv_coord_t cell_right = lv_obj_get_style_pad_right(obj, LV_PART_ITEMS);
792     lv_coord_t cell_top = lv_obj_get_style_pad_top(obj, LV_PART_ITEMS);
793     lv_coord_t cell_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_ITEMS);
794 
795     lv_coord_t letter_space = lv_obj_get_style_text_letter_space(obj, LV_PART_ITEMS);
796     lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_ITEMS);
797     const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_ITEMS);
798 
799     lv_coord_t minh = lv_obj_get_style_min_height(obj, LV_PART_ITEMS);
800     lv_coord_t maxh = lv_obj_get_style_max_height(obj, LV_PART_ITEMS);
801 
802     for(i = strat_row; i < table->row_cnt; i++) {
803         table->row_h[i] = get_row_height(obj, i, font, letter_space, line_space,
804                                          cell_left, cell_right, cell_top, cell_bottom);
805         table->row_h[i] = LV_CLAMP(minh, table->row_h[i], maxh);
806     }
807 
808     lv_obj_refresh_self_size(obj) ;
809 }
810 
get_row_height(lv_obj_t * obj,uint16_t row_id,const lv_font_t * font,lv_coord_t letter_space,lv_coord_t line_space,lv_coord_t cell_left,lv_coord_t cell_right,lv_coord_t cell_top,lv_coord_t cell_bottom)811 static lv_coord_t get_row_height(lv_obj_t * obj, uint16_t row_id, const lv_font_t * font,
812                                  lv_coord_t letter_space, lv_coord_t line_space,
813                                  lv_coord_t cell_left, lv_coord_t cell_right, lv_coord_t cell_top, lv_coord_t cell_bottom)
814 {
815     lv_table_t * table = (lv_table_t *)obj;
816     lv_point_t txt_size;
817     lv_coord_t txt_w;
818 
819     uint16_t row_start = row_id * table->col_cnt;
820     uint16_t cell;
821     uint16_t col;
822     lv_coord_t h_max = lv_font_get_line_height(font) + cell_top + cell_bottom;
823 
824     for(cell = row_start, col = 0; cell < row_start + table->col_cnt; cell++, col++) {
825         if(table->cell_data[cell] != NULL) {
826             txt_w              = table->col_w[col];
827             uint16_t col_merge = 0;
828             for(col_merge = 0; col_merge + col < table->col_cnt - 1; col_merge++) {
829 
830                 if(table->cell_data[cell + col_merge] != NULL) {
831                     lv_table_cell_ctrl_t ctrl = 0;
832                     char * next_cell_data = table->cell_data[cell + col_merge];
833                     if(next_cell_data) ctrl = next_cell_data[0];
834                     if(ctrl & LV_TABLE_CELL_CTRL_MERGE_RIGHT)
835                         txt_w += table->col_w[col + col_merge + 1];
836                     else
837                         break;
838                 }
839                 else {
840                     break;
841                 }
842             }
843 
844             lv_table_cell_ctrl_t ctrl = 0;
845             if(table->cell_data[cell]) ctrl = table->cell_data[cell][0];
846 
847             /*With text crop assume 1 line*/
848             if(ctrl & LV_TABLE_CELL_CTRL_TEXT_CROP) {
849                 h_max = LV_MAX(lv_font_get_line_height(font) + cell_top + cell_bottom,
850                                h_max);
851             }
852             /*Without text crop calculate the height of the text in the cell*/
853             else {
854                 txt_w -= cell_left + cell_right;
855 
856                 lv_txt_get_size(&txt_size, table->cell_data[cell] + 1, font,
857                                 letter_space, line_space, txt_w, LV_TEXT_FLAG_NONE);
858 
859                 h_max = LV_MAX(txt_size.y + cell_top + cell_bottom, h_max);
860                 cell += col_merge;
861                 col += col_merge;
862             }
863         }
864     }
865 
866     return h_max;
867 }
868 
get_pressed_cell(lv_obj_t * obj,uint16_t * row,uint16_t * col)869 static lv_res_t get_pressed_cell(lv_obj_t * obj, uint16_t * row, uint16_t * col)
870 {
871     lv_table_t * table = (lv_table_t *)obj;
872 
873     lv_indev_type_t type = lv_indev_get_type(lv_indev_get_act());
874     if(type != LV_INDEV_TYPE_POINTER && type != LV_INDEV_TYPE_BUTTON) {
875         if(col) *col = LV_TABLE_CELL_NONE;
876         if(row) *row = LV_TABLE_CELL_NONE;
877         return LV_RES_INV;
878     }
879 
880     lv_point_t p;
881     lv_indev_get_point(lv_indev_get_act(), &p);
882 
883     lv_coord_t tmp;
884     if(col) {
885         lv_coord_t x = p.x + lv_obj_get_scroll_x(obj);
886 
887         if(lv_obj_get_style_base_dir(obj, LV_PART_MAIN) == LV_BASE_DIR_RTL) {
888             x = obj->coords.x2 - lv_obj_get_style_pad_right(obj, LV_PART_MAIN) - x;
889         }
890         else {
891             x -= obj->coords.x1;
892             x -= lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
893         }
894 
895         *col = 0;
896         tmp = 0;
897         for(*col = 0; *col < table->col_cnt; (*col)++) {
898             tmp += table->col_w[*col];
899             if(x < tmp) break;
900         }
901     }
902 
903     if(row) {
904         lv_coord_t y = p.y + lv_obj_get_scroll_y(obj);;
905         y -= obj->coords.y1;
906         y -= lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
907 
908         *row = 0;
909         tmp = 0;
910 
911         for(*row = 0; *row < table->row_cnt; (*row)++) {
912             tmp += table->row_h[*row];
913             if(y < tmp) break;
914         }
915     }
916 
917     return LV_RES_OK;
918 }
919 
920 
921 #endif
922