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_form_row(lv_obj_t * obj, uint32_t start_row);
40 static void refr_cell_size(lv_obj_t * obj, uint32_t row, uint32_t col);
41 static lv_res_t get_pressed_cell(lv_obj_t * obj, uint16_t * row, uint16_t * col);
42 static size_t get_cell_txt_len(const char * txt);
43 static void copy_cell_txt(lv_table_cell_t * dst, const char * txt);
44 static void get_cell_area(lv_obj_t * obj, uint16_t row, uint16_t col, lv_area_t * area);
45 static void scroll_to_selected_cell(lv_obj_t * obj);
46 
is_cell_empty(void * cell)47 static inline bool is_cell_empty(void * cell)
48 {
49     return cell == NULL;
50 }
51 
52 /**********************
53  *  STATIC VARIABLES
54  **********************/
55 const lv_obj_class_t lv_table_class  = {
56     .constructor_cb = lv_table_constructor,
57     .destructor_cb = lv_table_destructor,
58     .event_cb = lv_table_event,
59     .width_def = LV_SIZE_CONTENT,
60     .height_def = LV_SIZE_CONTENT,
61     .base_class = &lv_obj_class,
62     .editable = LV_OBJ_CLASS_EDITABLE_TRUE,
63     .group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
64     .instance_size = sizeof(lv_table_t),
65 };
66 /**********************
67  *      MACROS
68  **********************/
69 
70 /**********************
71  *   GLOBAL FUNCTIONS
72  **********************/
73 
lv_table_create(lv_obj_t * parent)74 lv_obj_t * lv_table_create(lv_obj_t * parent)
75 {
76     LV_LOG_INFO("begin");
77     lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS, parent);
78     lv_obj_class_init_obj(obj);
79     return obj;
80 }
81 
82 /*=====================
83  * Setter functions
84  *====================*/
85 
lv_table_set_cell_value(lv_obj_t * obj,uint16_t row,uint16_t col,const char * txt)86 void lv_table_set_cell_value(lv_obj_t * obj, uint16_t row, uint16_t col, const char * txt)
87 {
88     LV_ASSERT_OBJ(obj, MY_CLASS);
89     LV_ASSERT_NULL(txt);
90 
91     lv_table_t * table = (lv_table_t *)obj;
92 
93     /*Auto expand*/
94     if(col >= table->col_cnt) lv_table_set_col_cnt(obj, col + 1);
95     if(row >= table->row_cnt) lv_table_set_row_cnt(obj, row + 1);
96 
97     uint32_t cell = row * table->col_cnt + col;
98     lv_table_cell_ctrl_t ctrl = 0;
99 
100     /*Save the control byte*/
101     if(table->cell_data[cell]) ctrl = table->cell_data[cell]->ctrl;
102 
103 #if LV_USE_USER_DATA
104     void * user_data = NULL;
105 
106     /*Save the user data*/
107     if(table->cell_data[cell]) user_data = table->cell_data[cell]->user_data;
108 #endif
109 
110     size_t to_allocate = get_cell_txt_len(txt);
111 
112     table->cell_data[cell] = lv_mem_realloc(table->cell_data[cell], to_allocate);
113     LV_ASSERT_MALLOC(table->cell_data[cell]);
114     if(table->cell_data[cell] == NULL) return;
115 
116     copy_cell_txt(table->cell_data[cell], txt);
117 
118     table->cell_data[cell]->ctrl = ctrl;
119 #if LV_USE_USER_DATA
120     table->cell_data[cell]->user_data = user_data;
121 #endif
122     refr_cell_size(obj, row, col);
123 }
124 
lv_table_set_cell_value_fmt(lv_obj_t * obj,uint16_t row,uint16_t col,const char * fmt,...)125 void lv_table_set_cell_value_fmt(lv_obj_t * obj, uint16_t row, uint16_t col, const char * fmt, ...)
126 {
127     LV_ASSERT_OBJ(obj, MY_CLASS);
128     LV_ASSERT_NULL(fmt);
129 
130     lv_table_t * table = (lv_table_t *)obj;
131     if(col >= table->col_cnt) {
132         lv_table_set_col_cnt(obj, col + 1);
133     }
134 
135     /*Auto expand*/
136     if(row >= table->row_cnt) {
137         lv_table_set_row_cnt(obj, row + 1);
138     }
139 
140     uint32_t cell = row * table->col_cnt + col;
141     lv_table_cell_ctrl_t ctrl = 0;
142 
143     /*Save the control byte*/
144     if(table->cell_data[cell]) ctrl = table->cell_data[cell]->ctrl;
145 
146 #if LV_USE_USER_DATA
147     void * user_data = NULL;
148 
149     /*Save the user_data*/
150     if(table->cell_data[cell]) user_data = table->cell_data[cell]->user_data;
151 #endif
152 
153     va_list ap, ap2;
154     va_start(ap, fmt);
155     va_copy(ap2, ap);
156 
157     /*Allocate space for the new text by using trick from C99 standard section 7.19.6.12*/
158     uint32_t len = lv_vsnprintf(NULL, 0, fmt, ap);
159     va_end(ap);
160 
161 #if LV_USE_ARABIC_PERSIAN_CHARS
162     /*Put together the text according to the format string*/
163     char * raw_txt = lv_mem_buf_get(len + 1);
164     LV_ASSERT_MALLOC(raw_txt);
165     if(raw_txt == NULL) {
166         va_end(ap2);
167         return;
168     }
169 
170     lv_vsnprintf(raw_txt, len + 1, fmt, ap2);
171 
172     /*Get the size of the Arabic text and process it*/
173     size_t len_ap = _lv_txt_ap_calc_bytes_cnt(raw_txt);
174     table->cell_data[cell] = lv_mem_realloc(table->cell_data[cell], sizeof(lv_table_cell_t) + len_ap + 1);
175     LV_ASSERT_MALLOC(table->cell_data[cell]);
176     if(table->cell_data[cell] == NULL) {
177         va_end(ap2);
178         return;
179     }
180     _lv_txt_ap_proc(raw_txt, table->cell_data[cell]->txt);
181 
182     lv_mem_buf_release(raw_txt);
183 #else
184     table->cell_data[cell] = lv_mem_realloc(table->cell_data[cell],
185                                             sizeof(lv_table_cell_t) + len + 1); /*+1: trailing '\0; */
186     LV_ASSERT_MALLOC(table->cell_data[cell]);
187     if(table->cell_data[cell] == NULL) {
188         va_end(ap2);
189         return;
190     }
191 
192     table->cell_data[cell]->txt[len] = 0; /*Ensure NULL termination*/
193 
194     lv_vsnprintf(table->cell_data[cell]->txt, len + 1, fmt, ap2);
195 #endif
196 
197     va_end(ap2);
198 
199     table->cell_data[cell]->ctrl = ctrl;
200 #if LV_USE_USER_DATA
201     table->cell_data[cell]->user_data = user_data;
202 #endif
203     refr_cell_size(obj, row, col);
204 }
205 
lv_table_set_row_cnt(lv_obj_t * obj,uint16_t row_cnt)206 void lv_table_set_row_cnt(lv_obj_t * obj, uint16_t row_cnt)
207 {
208     LV_ASSERT_OBJ(obj, MY_CLASS);
209 
210     lv_table_t * table = (lv_table_t *)obj;
211 
212     if(table->row_cnt == row_cnt) return;
213 
214     uint16_t old_row_cnt = table->row_cnt;
215     table->row_cnt         = row_cnt;
216 
217     table->row_h = lv_mem_realloc(table->row_h, table->row_cnt * sizeof(table->row_h[0]));
218     LV_ASSERT_MALLOC(table->row_h);
219     if(table->row_h == NULL) return;
220 
221     /*Free the unused cells*/
222     if(old_row_cnt > row_cnt) {
223         uint16_t old_cell_cnt = old_row_cnt * table->col_cnt;
224         uint32_t new_cell_cnt = table->col_cnt * table->row_cnt;
225         uint32_t i;
226         for(i = new_cell_cnt; i < old_cell_cnt; i++) {
227 #if LV_USE_USER_DATA
228             if(table->cell_data[i]->user_data) {
229                 lv_mem_free(table->cell_data[i]->user_data);
230                 table->cell_data[i]->user_data = NULL;
231             }
232 #endif
233             lv_mem_free(table->cell_data[i]);
234         }
235     }
236 
237     table->cell_data = lv_mem_realloc(table->cell_data, table->row_cnt * table->col_cnt * sizeof(lv_table_cell_t *));
238     LV_ASSERT_MALLOC(table->cell_data);
239     if(table->cell_data == NULL) return;
240 
241     /*Initialize the new fields*/
242     if(old_row_cnt < row_cnt) {
243         uint32_t old_cell_cnt = old_row_cnt * table->col_cnt;
244         uint32_t new_cell_cnt = table->col_cnt * table->row_cnt;
245         lv_memset_00(&table->cell_data[old_cell_cnt], (new_cell_cnt - old_cell_cnt) * sizeof(table->cell_data[0]));
246     }
247 
248     refr_size_form_row(obj, 0);
249 }
250 
lv_table_set_col_cnt(lv_obj_t * obj,uint16_t col_cnt)251 void lv_table_set_col_cnt(lv_obj_t * obj, uint16_t col_cnt)
252 {
253     LV_ASSERT_OBJ(obj, MY_CLASS);
254 
255     lv_table_t * table = (lv_table_t *)obj;
256 
257     if(table->col_cnt == col_cnt) return;
258 
259     uint16_t old_col_cnt = table->col_cnt;
260     table->col_cnt         = col_cnt;
261 
262     lv_table_cell_t ** new_cell_data = lv_mem_alloc(table->row_cnt * table->col_cnt * sizeof(lv_table_cell_t *));
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 
267     lv_memset_00(new_cell_data, new_cell_cnt * sizeof(table->cell_data[0]));
268 
269     /*The new column(s) messes up the mapping of `cell_data`*/
270     uint32_t old_col_start;
271     uint32_t new_col_start;
272     uint32_t min_col_cnt = LV_MIN(old_col_cnt, col_cnt);
273     uint32_t row;
274     for(row = 0; row < table->row_cnt; row++) {
275         old_col_start = row * old_col_cnt;
276         new_col_start = row * col_cnt;
277 
278         lv_memcpy_small(&new_cell_data[new_col_start], &table->cell_data[old_col_start],
279                         sizeof(new_cell_data[0]) * min_col_cnt);
280 
281         /*Free the old cells (only if the table becomes smaller)*/
282         int32_t i;
283         for(i = 0; i < (int32_t)old_col_cnt - col_cnt; i++) {
284             uint32_t idx = old_col_start + min_col_cnt + i;
285 #if LV_USE_USER_DATA
286             if(table->cell_data[idx]->user_data) {
287                 lv_mem_free(table->cell_data[idx]->user_data);
288                 table->cell_data[idx]->user_data = NULL;
289             }
290 #endif
291             lv_mem_free(table->cell_data[idx]);
292             table->cell_data[idx] = NULL;
293         }
294     }
295 
296     lv_mem_free(table->cell_data);
297     table->cell_data = new_cell_data;
298 
299     /*Initialize the new column widths if any*/
300     table->col_w = lv_mem_realloc(table->col_w, col_cnt * sizeof(table->col_w[0]));
301     LV_ASSERT_MALLOC(table->col_w);
302     if(table->col_w == NULL) return;
303 
304     uint32_t col;
305     for(col = old_col_cnt; col < col_cnt; col++) {
306         table->col_w[col] = LV_DPI_DEF;
307     }
308 
309 
310     refr_size_form_row(obj, 0) ;
311 }
312 
lv_table_set_col_width(lv_obj_t * obj,uint16_t col_id,lv_coord_t w)313 void lv_table_set_col_width(lv_obj_t * obj, uint16_t col_id, lv_coord_t w)
314 {
315     LV_ASSERT_OBJ(obj, MY_CLASS);
316 
317     lv_table_t * table = (lv_table_t *)obj;
318 
319     /*Auto expand*/
320     if(col_id >= table->col_cnt) lv_table_set_col_cnt(obj, col_id + 1);
321 
322     table->col_w[col_id] = w;
323     refr_size_form_row(obj, 0);
324 }
325 
lv_table_add_cell_ctrl(lv_obj_t * obj,uint16_t row,uint16_t col,lv_table_cell_ctrl_t ctrl)326 void lv_table_add_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl)
327 {
328     LV_ASSERT_OBJ(obj, MY_CLASS);
329 
330     lv_table_t * table = (lv_table_t *)obj;
331 
332     /*Auto expand*/
333     if(col >= table->col_cnt) lv_table_set_col_cnt(obj, col + 1);
334     if(row >= table->row_cnt) lv_table_set_row_cnt(obj, row + 1);
335 
336     uint32_t cell = row * table->col_cnt + col;
337 
338     if(is_cell_empty(table->cell_data[cell])) {
339         table->cell_data[cell]    = lv_mem_alloc(sizeof(lv_table_cell_t) + 1); /*+1: trailing '\0 */
340         LV_ASSERT_MALLOC(table->cell_data[cell]);
341         if(table->cell_data[cell] == NULL) return;
342 
343         table->cell_data[cell]->ctrl = 0;
344 #if LV_USE_USER_DATA
345         table->cell_data[cell]->user_data = NULL;
346 #endif
347         table->cell_data[cell]->txt[0] = '\0';
348     }
349 
350     table->cell_data[cell]->ctrl |= ctrl;
351 }
352 
lv_table_clear_cell_ctrl(lv_obj_t * obj,uint16_t row,uint16_t col,lv_table_cell_ctrl_t ctrl)353 void lv_table_clear_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl)
354 {
355     LV_ASSERT_OBJ(obj, MY_CLASS);
356 
357     lv_table_t * table = (lv_table_t *)obj;
358 
359     /*Auto expand*/
360     if(col >= table->col_cnt) lv_table_set_col_cnt(obj, col + 1);
361     if(row >= table->row_cnt) lv_table_set_row_cnt(obj, row + 1);
362 
363     uint32_t cell = row * table->col_cnt + col;
364 
365     if(is_cell_empty(table->cell_data[cell])) {
366         table->cell_data[cell]    = lv_mem_alloc(sizeof(lv_table_cell_t) + 1); /*+1: trailing '\0 */
367         LV_ASSERT_MALLOC(table->cell_data[cell]);
368         if(table->cell_data[cell] == NULL) return;
369 
370         table->cell_data[cell]->ctrl = 0;
371 #if LV_USE_USER_DATA
372         table->cell_data[cell]->user_data = NULL;
373 #endif
374         table->cell_data[cell]->txt[0] = '\0';
375     }
376 
377     table->cell_data[cell]->ctrl &= (~ctrl);
378 }
379 
380 #if LV_USE_USER_DATA
lv_table_set_cell_user_data(lv_obj_t * obj,uint16_t row,uint16_t col,void * user_data)381 void lv_table_set_cell_user_data(lv_obj_t * obj, uint16_t row, uint16_t col, void * user_data)
382 {
383     LV_ASSERT_OBJ(obj, MY_CLASS);
384 
385     lv_table_t * table = (lv_table_t *)obj;
386 
387     /*Auto expand*/
388     if(col >= table->col_cnt) lv_table_set_col_cnt(obj, col + 1);
389     if(row >= table->row_cnt) lv_table_set_row_cnt(obj, row + 1);
390 
391     uint32_t cell = row * table->col_cnt + col;
392 
393     if(is_cell_empty(table->cell_data[cell])) {
394         table->cell_data[cell]    = lv_mem_alloc(sizeof(lv_table_cell_t) + 1); /*+1: trailing '\0 */
395         LV_ASSERT_MALLOC(table->cell_data[cell]);
396         if(table->cell_data[cell] == NULL) return;
397 
398         table->cell_data[cell]->ctrl = 0;
399         table->cell_data[cell]->user_data = NULL;
400         table->cell_data[cell]->txt[0] = '\0';
401     }
402 
403     if(table->cell_data[cell]->user_data) {
404         lv_mem_free(table->cell_data[cell]->user_data);
405     }
406 
407     table->cell_data[cell]->user_data = user_data;
408 }
409 #endif
410 
411 /*=====================
412  * Getter functions
413  *====================*/
414 
lv_table_get_cell_value(lv_obj_t * obj,uint16_t row,uint16_t col)415 const char * lv_table_get_cell_value(lv_obj_t * obj, uint16_t row, uint16_t col)
416 {
417     LV_ASSERT_OBJ(obj, MY_CLASS);
418 
419     lv_table_t * table = (lv_table_t *)obj;
420     if(row >= table->row_cnt || col >= table->col_cnt) {
421         LV_LOG_WARN("invalid row or column");
422         return "";
423     }
424     uint32_t cell = row * table->col_cnt + col;
425 
426     if(is_cell_empty(table->cell_data[cell])) return "";
427 
428     return table->cell_data[cell]->txt;
429 }
430 
lv_table_get_row_cnt(lv_obj_t * obj)431 uint16_t lv_table_get_row_cnt(lv_obj_t * obj)
432 {
433     LV_ASSERT_OBJ(obj, MY_CLASS);
434 
435     lv_table_t * table = (lv_table_t *)obj;
436     return table->row_cnt;
437 }
438 
lv_table_get_col_cnt(lv_obj_t * obj)439 uint16_t lv_table_get_col_cnt(lv_obj_t * obj)
440 {
441     LV_ASSERT_OBJ(obj, MY_CLASS);
442 
443     lv_table_t * table = (lv_table_t *)obj;
444     return table->col_cnt;
445 }
446 
lv_table_get_col_width(lv_obj_t * obj,uint16_t col)447 lv_coord_t lv_table_get_col_width(lv_obj_t * obj, uint16_t col)
448 {
449     LV_ASSERT_OBJ(obj, MY_CLASS);
450 
451     lv_table_t * table = (lv_table_t *)obj;
452 
453     if(col >= table->col_cnt) {
454         LV_LOG_WARN("lv_table_set_col_width: too big 'col_id'. Must be < LV_TABLE_COL_MAX.");
455         return 0;
456     }
457 
458     return table->col_w[col];
459 }
460 
lv_table_has_cell_ctrl(lv_obj_t * obj,uint16_t row,uint16_t col,lv_table_cell_ctrl_t ctrl)461 bool lv_table_has_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl)
462 {
463     LV_ASSERT_OBJ(obj, MY_CLASS);
464 
465     lv_table_t * table = (lv_table_t *)obj;
466     if(row >= table->row_cnt || col >= table->col_cnt) {
467         LV_LOG_WARN("lv_table_get_cell_crop: invalid row or column");
468         return false;
469     }
470     uint32_t cell = row * table->col_cnt + col;
471 
472     if(is_cell_empty(table->cell_data[cell])) return false;
473     else return (table->cell_data[cell]->ctrl & ctrl) == ctrl;
474 }
475 
lv_table_get_selected_cell(lv_obj_t * obj,uint16_t * row,uint16_t * col)476 void lv_table_get_selected_cell(lv_obj_t * obj, uint16_t * row, uint16_t * col)
477 {
478     lv_table_t * table = (lv_table_t *)obj;
479     *row = table->row_act;
480     *col = table->col_act;
481 }
482 
483 #if LV_USE_USER_DATA
lv_table_get_cell_user_data(lv_obj_t * obj,uint16_t row,uint16_t col)484 void * lv_table_get_cell_user_data(lv_obj_t * obj, uint16_t row, uint16_t col)
485 {
486     LV_ASSERT_OBJ(obj, MY_CLASS);
487 
488     lv_table_t * table = (lv_table_t *)obj;
489     if(row >= table->row_cnt || col >= table->col_cnt) {
490         LV_LOG_WARN("invalid row or column");
491         return NULL;
492     }
493     uint32_t cell = row * table->col_cnt + col;
494 
495     if(is_cell_empty(table->cell_data[cell])) return NULL;
496 
497     return table->cell_data[cell]->user_data;
498 }
499 #endif
500 
501 /**********************
502  *   STATIC FUNCTIONS
503  **********************/
504 
lv_table_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)505 static void lv_table_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
506 {
507     LV_UNUSED(class_p);
508     LV_TRACE_OBJ_CREATE("begin");
509 
510     lv_table_t * table = (lv_table_t *)obj;
511 
512     table->col_cnt = 1;
513     table->row_cnt = 1;
514     table->col_w = lv_mem_alloc(table->col_cnt * sizeof(table->col_w[0]));
515     table->row_h = lv_mem_alloc(table->row_cnt * sizeof(table->row_h[0]));
516     table->col_w[0] = LV_DPI_DEF;
517     table->row_h[0] = LV_DPI_DEF;
518     table->cell_data = lv_mem_realloc(table->cell_data, table->row_cnt * table->col_cnt * sizeof(lv_table_cell_t *));
519     table->cell_data[0] = NULL;
520 
521     LV_TRACE_OBJ_CREATE("finished");
522 }
523 
lv_table_destructor(const lv_obj_class_t * class_p,lv_obj_t * obj)524 static void lv_table_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
525 {
526     LV_UNUSED(class_p);
527     lv_table_t * table = (lv_table_t *)obj;
528     /*Free the cell texts*/
529     uint16_t i;
530     for(i = 0; i < table->col_cnt * table->row_cnt; i++) {
531         if(table->cell_data[i]) {
532 #if LV_USE_USER_DATA
533             if(table->cell_data[i]->user_data) {
534                 lv_mem_free(table->cell_data[i]->user_data);
535                 table->cell_data[i]->user_data = NULL;
536             }
537 #endif
538             lv_mem_free(table->cell_data[i]);
539             table->cell_data[i] = NULL;
540         }
541     }
542 
543     if(table->cell_data) lv_mem_free(table->cell_data);
544     if(table->row_h) lv_mem_free(table->row_h);
545     if(table->col_w) lv_mem_free(table->col_w);
546 }
547 
lv_table_event(const lv_obj_class_t * class_p,lv_event_t * e)548 static void lv_table_event(const lv_obj_class_t * class_p, lv_event_t * e)
549 {
550     LV_UNUSED(class_p);
551 
552     lv_res_t res;
553 
554     /*Call the ancestor's event handler*/
555     res = lv_obj_event_base(MY_CLASS, e);
556     if(res != LV_RES_OK) return;
557 
558     lv_event_code_t code = lv_event_get_code(e);
559     lv_obj_t * obj = lv_event_get_target(e);
560     lv_table_t * table = (lv_table_t *)obj;
561 
562     if(code == LV_EVENT_STYLE_CHANGED) {
563         refr_size_form_row(obj, 0);
564     }
565     else if(code == LV_EVENT_GET_SELF_SIZE) {
566         lv_point_t * p = lv_event_get_param(e);
567         uint32_t i;
568         lv_coord_t w = 0;
569         for(i = 0; i < table->col_cnt; i++) w += table->col_w[i];
570 
571         lv_coord_t h = 0;
572         for(i = 0; i < table->row_cnt; i++) h += table->row_h[i];
573 
574         p->x = w - 1;
575         p->y = h - 1;
576     }
577     else if(code == LV_EVENT_PRESSED || code == LV_EVENT_PRESSING) {
578         uint16_t col;
579         uint16_t row;
580         lv_res_t pr_res = get_pressed_cell(obj, &row, &col);
581 
582         if(pr_res == LV_RES_OK && (table->col_act != col || table->row_act != row)) {
583             table->col_act = col;
584             table->row_act = row;
585             lv_obj_invalidate(obj);
586         }
587     }
588     else if(code == LV_EVENT_RELEASED) {
589         lv_obj_invalidate(obj);
590         lv_indev_t * indev = lv_indev_get_act();
591         lv_obj_t * scroll_obj = lv_indev_get_scroll_obj(indev);
592         if(table->col_act != LV_TABLE_CELL_NONE && table->row_act != LV_TABLE_CELL_NONE && scroll_obj == NULL) {
593             res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
594             if(res != LV_RES_OK) return;
595         }
596 
597         lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
598         if(indev_type == LV_INDEV_TYPE_POINTER || indev_type == LV_INDEV_TYPE_BUTTON) {
599             table->col_act = LV_TABLE_CELL_NONE;
600             table->row_act = LV_TABLE_CELL_NONE;
601         }
602     }
603     else if(code == LV_EVENT_FOCUSED) {
604         lv_obj_invalidate(obj);
605     }
606     else if(code == LV_EVENT_KEY) {
607         int32_t c = *((int32_t *)lv_event_get_param(e));
608         int32_t col = table->col_act;
609         int32_t row = table->row_act;
610         if(col == LV_TABLE_CELL_NONE || row == LV_TABLE_CELL_NONE) {
611             table->col_act = 0;
612             table->row_act = 0;
613             scroll_to_selected_cell(obj);
614             lv_obj_invalidate(obj);
615             return;
616         }
617 
618         if(col >= table->col_cnt) col = 0;
619         if(row >= table->row_cnt) row = 0;
620 
621         if(c == LV_KEY_LEFT) col--;
622         else if(c == LV_KEY_RIGHT) col++;
623         else if(c == LV_KEY_UP) row--;
624         else if(c == LV_KEY_DOWN) row++;
625         else return;
626 
627         if(col >= table->col_cnt) {
628             if(row < table->row_cnt - 1) {
629                 col = 0;
630                 row++;
631             }
632             else {
633                 col = table->col_cnt - 1;
634             }
635         }
636         else if(col < 0) {
637             if(row != 0) {
638                 col = table->col_cnt - 1;
639                 row--;
640             }
641             else {
642                 col = 0;
643             }
644         }
645 
646         if(row >= table->row_cnt) {
647             row = table->row_cnt - 1;
648         }
649         else if(row < 0) {
650             row = 0;
651         }
652 
653         if(table->col_act != col || table->row_act != row) {
654             table->col_act = col;
655             table->row_act = row;
656             lv_obj_invalidate(obj);
657 
658             scroll_to_selected_cell(obj);
659 
660             res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
661             if(res != LV_RES_OK) return;
662         }
663     }
664     else if(code == LV_EVENT_DRAW_MAIN) {
665         draw_main(e);
666     }
667 }
668 
669 
draw_main(lv_event_t * e)670 static void draw_main(lv_event_t * e)
671 {
672     lv_obj_t * obj = lv_event_get_target(e);
673     lv_table_t * table = (lv_table_t *)obj;
674     lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
675     lv_area_t clip_area;
676     if(!_lv_area_intersect(&clip_area, &obj->coords, draw_ctx->clip_area)) return;
677 
678     const lv_area_t * clip_area_ori = draw_ctx->clip_area;
679     draw_ctx->clip_area = &clip_area;
680 
681     lv_point_t txt_size;
682     lv_area_t cell_area;
683 
684     lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
685     lv_coord_t bg_top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
686     lv_coord_t bg_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN);
687     lv_coord_t bg_left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
688     lv_coord_t bg_right = lv_obj_get_style_pad_right(obj, LV_PART_MAIN);
689 
690     lv_state_t state_ori = obj->state;
691     obj->state = LV_STATE_DEFAULT;
692     obj->skip_trans = 1;
693     lv_draw_rect_dsc_t rect_dsc_def;
694     lv_draw_rect_dsc_t rect_dsc_act; /*Passed to the event to modify it*/
695     lv_draw_rect_dsc_init(&rect_dsc_def);
696     lv_obj_init_draw_rect_dsc(obj, LV_PART_ITEMS, &rect_dsc_def);
697 
698     lv_draw_label_dsc_t label_dsc_def;
699     lv_draw_label_dsc_t label_dsc_act;  /*Passed to the event to modify it*/
700     lv_draw_label_dsc_init(&label_dsc_def);
701     lv_obj_init_draw_label_dsc(obj, LV_PART_ITEMS, &label_dsc_def);
702     obj->state = state_ori;
703     obj->skip_trans = 0;
704 
705     uint16_t col;
706     uint16_t row;
707     uint16_t cell = 0;
708 
709     cell_area.y2 = obj->coords.y1 + bg_top - 1 - lv_obj_get_scroll_y(obj) + border_width;
710     lv_coord_t scroll_x = lv_obj_get_scroll_x(obj) ;
711     bool rtl = lv_obj_get_style_base_dir(obj, LV_PART_MAIN) == LV_BASE_DIR_RTL;
712 
713     /*Handle custom drawer*/
714     lv_obj_draw_part_dsc_t part_draw_dsc;
715     lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
716     part_draw_dsc.part = LV_PART_ITEMS;
717     part_draw_dsc.class_p = MY_CLASS;
718     part_draw_dsc.type = LV_TABLE_DRAW_PART_CELL;
719     part_draw_dsc.rect_dsc = &rect_dsc_act;
720     part_draw_dsc.label_dsc = &label_dsc_act;
721 
722     for(row = 0; row < table->row_cnt; row++) {
723         lv_coord_t h_row = table->row_h[row];
724 
725         cell_area.y1 = cell_area.y2 + 1;
726         cell_area.y2 = cell_area.y1 + h_row - 1;
727 
728         if(cell_area.y1 > clip_area.y2) break;
729 
730         if(rtl) cell_area.x1 = obj->coords.x2 - bg_right - 1 - scroll_x - border_width;
731         else cell_area.x2 = obj->coords.x1 + bg_left - 1 - scroll_x + border_width;
732 
733         for(col = 0; col < table->col_cnt; col++) {
734             lv_table_cell_ctrl_t ctrl = 0;
735             if(table->cell_data[cell]) ctrl = table->cell_data[cell]->ctrl;
736 
737             if(rtl) {
738                 cell_area.x2 = cell_area.x1 - 1;
739                 cell_area.x1 = cell_area.x2 - table->col_w[col] + 1;
740             }
741             else {
742                 cell_area.x1 = cell_area.x2 + 1;
743                 cell_area.x2 = cell_area.x1 + table->col_w[col] - 1;
744             }
745 
746             uint16_t col_merge = 0;
747             for(col_merge = 0; col_merge + col < table->col_cnt - 1; col_merge++) {
748                 lv_table_cell_t * next_cell_data = table->cell_data[cell + col_merge];
749 
750                 if(is_cell_empty(next_cell_data)) break;
751 
752                 lv_table_cell_ctrl_t merge_ctrl = (lv_table_cell_ctrl_t) next_cell_data->ctrl;
753                 if(merge_ctrl & LV_TABLE_CELL_CTRL_MERGE_RIGHT) {
754                     lv_coord_t offset = table->col_w[col + col_merge + 1];
755 
756                     if(rtl) cell_area.x1 -= offset;
757                     else cell_area.x2 += offset;
758                 }
759                 else {
760                     break;
761                 }
762             }
763 
764             if(cell_area.y2 < clip_area.y1) {
765                 cell += col_merge + 1;
766                 col += col_merge;
767                 continue;
768             }
769 
770             /*Expand the cell area with a half border to avoid drawing 2 borders next to each other*/
771             lv_area_t cell_area_border;
772             lv_area_copy(&cell_area_border, &cell_area);
773             if((rect_dsc_def.border_side & LV_BORDER_SIDE_LEFT) && cell_area_border.x1 > obj->coords.x1 + bg_left) {
774                 cell_area_border.x1 -= rect_dsc_def.border_width / 2;
775             }
776             if((rect_dsc_def.border_side & LV_BORDER_SIDE_TOP) && cell_area_border.y1 > obj->coords.y1 + bg_top) {
777                 cell_area_border.y1 -= rect_dsc_def.border_width / 2;
778             }
779             if((rect_dsc_def.border_side & LV_BORDER_SIDE_RIGHT) && cell_area_border.x2 < obj->coords.x2 - bg_right - 1) {
780                 cell_area_border.x2 += rect_dsc_def.border_width / 2 + (rect_dsc_def.border_width & 0x1);
781             }
782             if((rect_dsc_def.border_side & LV_BORDER_SIDE_BOTTOM) &&
783                cell_area_border.y2 < obj->coords.y2 - bg_bottom - 1) {
784                 cell_area_border.y2 += rect_dsc_def.border_width / 2 + (rect_dsc_def.border_width & 0x1);
785             }
786 
787             lv_state_t cell_state = LV_STATE_DEFAULT;
788             if(row == table->row_act && col == table->col_act) {
789                 if(!(obj->state & LV_STATE_SCROLLED) && (obj->state & LV_STATE_PRESSED)) cell_state |= LV_STATE_PRESSED;
790                 if(obj->state & LV_STATE_FOCUSED) cell_state |= LV_STATE_FOCUSED;
791                 if(obj->state & LV_STATE_FOCUS_KEY) cell_state |= LV_STATE_FOCUS_KEY;
792                 if(obj->state & LV_STATE_EDITED) cell_state |= LV_STATE_EDITED;
793             }
794 
795             /*Set up the draw descriptors*/
796             if(cell_state == LV_STATE_DEFAULT) {
797                 lv_memcpy(&rect_dsc_act, &rect_dsc_def, sizeof(lv_draw_rect_dsc_t));
798                 lv_memcpy(&label_dsc_act, &label_dsc_def, sizeof(lv_draw_label_dsc_t));
799             }
800             /*In other cases get the styles directly without caching them*/
801             else {
802                 obj->state = cell_state;
803                 obj->skip_trans = 1;
804                 lv_draw_rect_dsc_init(&rect_dsc_act);
805                 lv_draw_label_dsc_init(&label_dsc_act);
806                 lv_obj_init_draw_rect_dsc(obj, LV_PART_ITEMS, &rect_dsc_act);
807                 lv_obj_init_draw_label_dsc(obj, LV_PART_ITEMS, &label_dsc_act);
808                 obj->state = state_ori;
809                 obj->skip_trans = 0;
810             }
811 
812             part_draw_dsc.draw_area = &cell_area_border;
813             part_draw_dsc.id = row * table->col_cnt + col;
814             lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
815 
816             lv_draw_rect(draw_ctx, &rect_dsc_act, &cell_area_border);
817 
818             if(table->cell_data[cell]) {
819                 const lv_coord_t cell_left = lv_obj_get_style_pad_left(obj, LV_PART_ITEMS);
820                 const lv_coord_t cell_right = lv_obj_get_style_pad_right(obj, LV_PART_ITEMS);
821                 const lv_coord_t cell_top = lv_obj_get_style_pad_top(obj, LV_PART_ITEMS);
822                 const lv_coord_t cell_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_ITEMS);
823                 lv_text_flag_t txt_flags = LV_TEXT_FLAG_NONE;
824                 lv_area_t txt_area;
825 
826                 txt_area.x1 = cell_area.x1 + cell_left;
827                 txt_area.x2 = cell_area.x2 - cell_right;
828                 txt_area.y1 = cell_area.y1 + cell_top;
829                 txt_area.y2 = cell_area.y2 - cell_bottom;
830 
831                 /*Align the content to the middle if not cropped*/
832                 bool crop = ctrl & LV_TABLE_CELL_CTRL_TEXT_CROP ? true : false;
833                 if(crop) txt_flags = LV_TEXT_FLAG_EXPAND;
834 
835                 lv_txt_get_size(&txt_size, table->cell_data[cell]->txt, label_dsc_def.font,
836                                 label_dsc_act.letter_space, label_dsc_act.line_space,
837                                 lv_area_get_width(&txt_area), txt_flags);
838 
839                 /*Align the content to the middle if not cropped*/
840                 if(!crop) {
841                     txt_area.y1 = cell_area.y1 + h_row / 2 - txt_size.y / 2;
842                     txt_area.y2 = cell_area.y1 + h_row / 2 + txt_size.y / 2;
843                 }
844 
845                 lv_area_t label_clip_area;
846                 bool label_mask_ok;
847                 label_mask_ok = _lv_area_intersect(&label_clip_area, &clip_area, &cell_area);
848                 if(label_mask_ok) {
849                     draw_ctx->clip_area = &label_clip_area;
850                     lv_draw_label(draw_ctx, &label_dsc_act, &txt_area, table->cell_data[cell]->txt, NULL);
851                     draw_ctx->clip_area = &clip_area;
852                 }
853             }
854 
855             lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
856 
857             cell += col_merge + 1;
858             col += col_merge;
859         }
860     }
861 
862     draw_ctx->clip_area = clip_area_ori;
863 }
864 
865 /* Refreshes size of the table starting from @start_row row */
refr_size_form_row(lv_obj_t * obj,uint32_t start_row)866 static void refr_size_form_row(lv_obj_t * obj, uint32_t start_row)
867 {
868     const lv_coord_t cell_pad_left = lv_obj_get_style_pad_left(obj, LV_PART_ITEMS);
869     const lv_coord_t cell_pad_right = lv_obj_get_style_pad_right(obj, LV_PART_ITEMS);
870     const lv_coord_t cell_pad_top = lv_obj_get_style_pad_top(obj, LV_PART_ITEMS);
871     const lv_coord_t cell_pad_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_ITEMS);
872 
873     lv_coord_t letter_space = lv_obj_get_style_text_letter_space(obj, LV_PART_ITEMS);
874     lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_ITEMS);
875     const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_ITEMS);
876 
877     const lv_coord_t minh = lv_obj_get_style_min_height(obj, LV_PART_ITEMS);
878     const lv_coord_t maxh = lv_obj_get_style_max_height(obj, LV_PART_ITEMS);
879 
880     lv_table_t * table = (lv_table_t *)obj;
881     uint32_t i;
882     for(i = start_row; i < table->row_cnt; i++) {
883         lv_coord_t calculated_height = get_row_height(obj, i, font, letter_space, line_space,
884                                                       cell_pad_left, cell_pad_right, cell_pad_top, cell_pad_bottom);
885         table->row_h[i] = LV_CLAMP(minh, calculated_height, maxh);
886     }
887 
888     lv_obj_refresh_self_size(obj);
889     lv_obj_invalidate(obj);
890 }
891 
892 
refr_cell_size(lv_obj_t * obj,uint32_t row,uint32_t col)893 static void refr_cell_size(lv_obj_t * obj, uint32_t row, uint32_t col)
894 {
895     const lv_coord_t cell_pad_left = lv_obj_get_style_pad_left(obj, LV_PART_ITEMS);
896     const lv_coord_t cell_pad_right = lv_obj_get_style_pad_right(obj, LV_PART_ITEMS);
897     const lv_coord_t cell_pad_top = lv_obj_get_style_pad_top(obj, LV_PART_ITEMS);
898     const lv_coord_t cell_pad_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_ITEMS);
899 
900     lv_coord_t letter_space = lv_obj_get_style_text_letter_space(obj, LV_PART_ITEMS);
901     lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_ITEMS);
902     const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_ITEMS);
903 
904     const lv_coord_t minh = lv_obj_get_style_min_height(obj, LV_PART_ITEMS);
905     const lv_coord_t maxh = lv_obj_get_style_max_height(obj, LV_PART_ITEMS);
906 
907     lv_table_t * table = (lv_table_t *)obj;
908     lv_coord_t calculated_height = get_row_height(obj, row, font, letter_space, line_space,
909                                                   cell_pad_left, cell_pad_right, cell_pad_top, cell_pad_bottom);
910 
911     lv_coord_t prev_row_size = table->row_h[row];
912     table->row_h[row] = LV_CLAMP(minh, calculated_height, maxh);
913 
914     /*If the row height havn't changed invalidate only this cell*/
915     if(prev_row_size == table->row_h[row]) {
916         lv_area_t cell_area;
917         get_cell_area(obj, row, col, &cell_area);
918         lv_area_move(&cell_area, obj->coords.x1, obj->coords.y1);
919         lv_obj_invalidate_area(obj, &cell_area);
920     }
921     else {
922         lv_obj_refresh_self_size(obj);
923         lv_obj_invalidate(obj);
924     }
925 }
926 
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)927 static lv_coord_t get_row_height(lv_obj_t * obj, uint16_t row_id, const lv_font_t * font,
928                                  lv_coord_t letter_space, lv_coord_t line_space,
929                                  lv_coord_t cell_left, lv_coord_t cell_right, lv_coord_t cell_top, lv_coord_t cell_bottom)
930 {
931     lv_table_t * table = (lv_table_t *)obj;
932 
933     lv_coord_t h_max = lv_font_get_line_height(font) + cell_top + cell_bottom;
934     /* Calculate the cell_data index where to start */
935     uint16_t row_start = row_id * table->col_cnt;
936 
937     /* Traverse the cells in the row_id row */
938     uint16_t cell;
939     uint16_t col;
940     for(cell = row_start, col = 0; cell < row_start + table->col_cnt; cell++, col++) {
941         lv_table_cell_t * cell_data = table->cell_data[cell];
942 
943         if(is_cell_empty(cell_data)) {
944             continue;
945         }
946 
947         lv_coord_t txt_w = table->col_w[col];
948 
949         /* Traverse the current row from the first until the penultimate column.
950          * Increment the text width if the cell has the LV_TABLE_CELL_CTRL_MERGE_RIGHT control,
951          * exit the traversal when the current cell control is not LV_TABLE_CELL_CTRL_MERGE_RIGHT */
952         uint16_t col_merge = 0;
953         for(col_merge = 0; col_merge + col < table->col_cnt - 1; col_merge++) {
954             lv_table_cell_t * next_cell_data = table->cell_data[cell + col_merge];
955 
956             if(is_cell_empty(next_cell_data)) break;
957 
958             lv_table_cell_ctrl_t ctrl = (lv_table_cell_ctrl_t) next_cell_data->ctrl;
959             if(ctrl & LV_TABLE_CELL_CTRL_MERGE_RIGHT) {
960                 txt_w += table->col_w[col + col_merge + 1];
961             }
962             else {
963                 break;
964             }
965         }
966 
967         lv_table_cell_ctrl_t ctrl = (lv_table_cell_ctrl_t) cell_data->ctrl;
968 
969         /*When cropping the text we can assume the row height is equal to the line height*/
970         if(ctrl & LV_TABLE_CELL_CTRL_TEXT_CROP) {
971             h_max = LV_MAX(lv_font_get_line_height(font) + cell_top + cell_bottom,
972                            h_max);
973         }
974         /*Else we have to calculate the height of the cell text*/
975         else {
976             lv_point_t txt_size;
977             txt_w -= cell_left + cell_right;
978 
979             lv_txt_get_size(&txt_size, table->cell_data[cell]->txt, font,
980                             letter_space, line_space, txt_w, LV_TEXT_FLAG_NONE);
981 
982             h_max = LV_MAX(txt_size.y + cell_top + cell_bottom, h_max);
983             /*Skip until one element after the last merged column*/
984             cell += col_merge;
985             col += col_merge;
986         }
987     }
988 
989     return h_max;
990 }
991 
get_pressed_cell(lv_obj_t * obj,uint16_t * row,uint16_t * col)992 static lv_res_t get_pressed_cell(lv_obj_t * obj, uint16_t * row, uint16_t * col)
993 {
994     lv_table_t * table = (lv_table_t *)obj;
995 
996     lv_indev_type_t type = lv_indev_get_type(lv_indev_get_act());
997     if(type != LV_INDEV_TYPE_POINTER && type != LV_INDEV_TYPE_BUTTON) {
998         if(col) *col = LV_TABLE_CELL_NONE;
999         if(row) *row = LV_TABLE_CELL_NONE;
1000         return LV_RES_INV;
1001     }
1002 
1003     lv_point_t p;
1004     lv_indev_get_point(lv_indev_get_act(), &p);
1005 
1006     lv_coord_t tmp;
1007     if(col) {
1008         lv_coord_t x = p.x + lv_obj_get_scroll_x(obj);
1009 
1010         if(lv_obj_get_style_base_dir(obj, LV_PART_MAIN) == LV_BASE_DIR_RTL) {
1011             x = obj->coords.x2 - lv_obj_get_style_pad_right(obj, LV_PART_MAIN) - x;
1012         }
1013         else {
1014             x -= obj->coords.x1;
1015             x -= lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
1016         }
1017 
1018         *col = 0;
1019         tmp = 0;
1020         for(*col = 0; *col < table->col_cnt; (*col)++) {
1021             tmp += table->col_w[*col];
1022             if(x < tmp) break;
1023         }
1024     }
1025 
1026     if(row) {
1027         lv_coord_t y = p.y + lv_obj_get_scroll_y(obj);;
1028         y -= obj->coords.y1;
1029         y -= lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
1030 
1031         *row = 0;
1032         tmp = 0;
1033 
1034         for(*row = 0; *row < table->row_cnt; (*row)++) {
1035             tmp += table->row_h[*row];
1036             if(y < tmp) break;
1037         }
1038     }
1039 
1040     return LV_RES_OK;
1041 }
1042 
1043 /* Returns number of bytes to allocate based on chars configuration */
get_cell_txt_len(const char * txt)1044 static size_t get_cell_txt_len(const char * txt)
1045 {
1046     size_t retval = 0;
1047 
1048 #if LV_USE_ARABIC_PERSIAN_CHARS
1049     retval = sizeof(lv_table_cell_t) + _lv_txt_ap_calc_bytes_cnt(txt) + 1;
1050 #else
1051     retval = sizeof(lv_table_cell_t) + strlen(txt) + 1;
1052 #endif
1053 
1054     return retval;
1055 }
1056 
1057 /* Copy txt into dst skipping the format byte */
copy_cell_txt(lv_table_cell_t * dst,const char * txt)1058 static void copy_cell_txt(lv_table_cell_t * dst, const char * txt)
1059 {
1060 #if LV_USE_ARABIC_PERSIAN_CHARS
1061     _lv_txt_ap_proc(txt, dst->txt);
1062 #else
1063     strcpy(dst->txt, txt);
1064 #endif
1065 }
1066 
get_cell_area(lv_obj_t * obj,uint16_t row,uint16_t col,lv_area_t * area)1067 static void get_cell_area(lv_obj_t * obj, uint16_t row, uint16_t col, lv_area_t * area)
1068 {
1069     lv_table_t * table = (lv_table_t *)obj;
1070 
1071     uint32_t c;
1072     area->x1 = 0;
1073     for(c = 0; c < col; c++) {
1074         area->x1 += table->col_w[c];
1075     }
1076 
1077     bool rtl = lv_obj_get_style_base_dir(obj, LV_PART_MAIN) == LV_BASE_DIR_RTL;
1078     if(rtl) {
1079         area->x1 += lv_obj_get_scroll_x(obj);
1080         lv_coord_t w = lv_obj_get_width(obj);
1081         area->x2 = w - area->x1 - lv_obj_get_style_pad_right(obj, 0);
1082         area->x1 = area->x2 - table->col_w[col];
1083     }
1084     else {
1085         area->x1 -= lv_obj_get_scroll_x(obj);
1086         area->x1 += lv_obj_get_style_pad_left(obj, 0);
1087         area->x2 = area->x1 + table->col_w[col] - 1;
1088     }
1089 
1090     uint32_t r;
1091     area->y1 = 0;
1092     for(r = 0; r < row; r++) {
1093         area->y1 += table->row_h[r];
1094     }
1095 
1096     area->y1 += lv_obj_get_style_pad_top(obj, 0);
1097     area->y1 -= lv_obj_get_scroll_y(obj);
1098     area->y2 = area->y1 + table->row_h[row] - 1;
1099 
1100 }
1101 
1102 
scroll_to_selected_cell(lv_obj_t * obj)1103 static void scroll_to_selected_cell(lv_obj_t * obj)
1104 {
1105     lv_table_t * table = (lv_table_t *)obj;
1106 
1107     lv_area_t a;
1108     get_cell_area(obj, table->row_act, table->col_act, &a);
1109     if(a.x1 < 0) {
1110         lv_obj_scroll_by_bounded(obj, -a.x1, 0, LV_ANIM_ON);
1111     }
1112     else if(a.x2 > lv_obj_get_width(obj)) {
1113         lv_obj_scroll_by_bounded(obj, lv_obj_get_width(obj) - a.x2, 0, LV_ANIM_ON);
1114     }
1115 
1116     if(a.y1 < 0) {
1117         lv_obj_scroll_by_bounded(obj, 0, -a.y1, LV_ANIM_ON);
1118     }
1119     else if(a.y2 > lv_obj_get_height(obj)) {
1120         lv_obj_scroll_by_bounded(obj, 0, lv_obj_get_height(obj) - a.y2, LV_ANIM_ON);
1121     }
1122 
1123 }
1124 #endif
1125