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     refr_size_form_row(obj, 0) ;
310 }
311 
lv_table_set_col_width(lv_obj_t * obj,uint16_t col_id,lv_coord_t w)312 void lv_table_set_col_width(lv_obj_t * obj, uint16_t col_id, lv_coord_t w)
313 {
314     LV_ASSERT_OBJ(obj, MY_CLASS);
315 
316     lv_table_t * table = (lv_table_t *)obj;
317 
318     /*Auto expand*/
319     if(col_id >= table->col_cnt) lv_table_set_col_cnt(obj, col_id + 1);
320 
321     table->col_w[col_id] = w;
322     refr_size_form_row(obj, 0);
323 }
324 
lv_table_add_cell_ctrl(lv_obj_t * obj,uint16_t row,uint16_t col,lv_table_cell_ctrl_t ctrl)325 void lv_table_add_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl)
326 {
327     LV_ASSERT_OBJ(obj, MY_CLASS);
328 
329     lv_table_t * table = (lv_table_t *)obj;
330 
331     /*Auto expand*/
332     if(col >= table->col_cnt) lv_table_set_col_cnt(obj, col + 1);
333     if(row >= table->row_cnt) lv_table_set_row_cnt(obj, row + 1);
334 
335     uint32_t cell = row * table->col_cnt + col;
336 
337     if(is_cell_empty(table->cell_data[cell])) {
338         table->cell_data[cell]    = lv_mem_alloc(sizeof(lv_table_cell_t) + 1); /*+1: trailing '\0 */
339         LV_ASSERT_MALLOC(table->cell_data[cell]);
340         if(table->cell_data[cell] == NULL) return;
341 
342         table->cell_data[cell]->ctrl = 0;
343 #if LV_USE_USER_DATA
344         table->cell_data[cell]->user_data = NULL;
345 #endif
346         table->cell_data[cell]->txt[0] = '\0';
347     }
348 
349     table->cell_data[cell]->ctrl |= ctrl;
350 }
351 
lv_table_clear_cell_ctrl(lv_obj_t * obj,uint16_t row,uint16_t col,lv_table_cell_ctrl_t ctrl)352 void lv_table_clear_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl)
353 {
354     LV_ASSERT_OBJ(obj, MY_CLASS);
355 
356     lv_table_t * table = (lv_table_t *)obj;
357 
358     /*Auto expand*/
359     if(col >= table->col_cnt) lv_table_set_col_cnt(obj, col + 1);
360     if(row >= table->row_cnt) lv_table_set_row_cnt(obj, row + 1);
361 
362     uint32_t cell = row * table->col_cnt + col;
363 
364     if(is_cell_empty(table->cell_data[cell])) {
365         table->cell_data[cell]    = lv_mem_alloc(sizeof(lv_table_cell_t) + 1); /*+1: trailing '\0 */
366         LV_ASSERT_MALLOC(table->cell_data[cell]);
367         if(table->cell_data[cell] == NULL) return;
368 
369         table->cell_data[cell]->ctrl = 0;
370 #if LV_USE_USER_DATA
371         table->cell_data[cell]->user_data = NULL;
372 #endif
373         table->cell_data[cell]->txt[0] = '\0';
374     }
375 
376     table->cell_data[cell]->ctrl &= (~ctrl);
377 }
378 
379 #if LV_USE_USER_DATA
lv_table_set_cell_user_data(lv_obj_t * obj,uint16_t row,uint16_t col,void * user_data)380 void lv_table_set_cell_user_data(lv_obj_t * obj, uint16_t row, uint16_t col, void * user_data)
381 {
382     LV_ASSERT_OBJ(obj, MY_CLASS);
383 
384     lv_table_t * table = (lv_table_t *)obj;
385 
386     /*Auto expand*/
387     if(col >= table->col_cnt) lv_table_set_col_cnt(obj, col + 1);
388     if(row >= table->row_cnt) lv_table_set_row_cnt(obj, row + 1);
389 
390     uint32_t cell = row * table->col_cnt + col;
391 
392     if(is_cell_empty(table->cell_data[cell])) {
393         table->cell_data[cell]    = lv_mem_alloc(sizeof(lv_table_cell_t) + 1); /*+1: trailing '\0 */
394         LV_ASSERT_MALLOC(table->cell_data[cell]);
395         if(table->cell_data[cell] == NULL) return;
396 
397         table->cell_data[cell]->ctrl = 0;
398         table->cell_data[cell]->user_data = NULL;
399         table->cell_data[cell]->txt[0] = '\0';
400     }
401 
402     if(table->cell_data[cell]->user_data) {
403         lv_mem_free(table->cell_data[cell]->user_data);
404     }
405 
406     table->cell_data[cell]->user_data = user_data;
407 }
408 #endif
409 
410 /*=====================
411  * Getter functions
412  *====================*/
413 
lv_table_get_cell_value(lv_obj_t * obj,uint16_t row,uint16_t col)414 const char * lv_table_get_cell_value(lv_obj_t * obj, uint16_t row, uint16_t col)
415 {
416     LV_ASSERT_OBJ(obj, MY_CLASS);
417 
418     lv_table_t * table = (lv_table_t *)obj;
419     if(row >= table->row_cnt || col >= table->col_cnt) {
420         LV_LOG_WARN("invalid row or column");
421         return "";
422     }
423     uint32_t cell = row * table->col_cnt + col;
424 
425     if(is_cell_empty(table->cell_data[cell])) return "";
426 
427     return table->cell_data[cell]->txt;
428 }
429 
lv_table_get_row_cnt(lv_obj_t * obj)430 uint16_t lv_table_get_row_cnt(lv_obj_t * obj)
431 {
432     LV_ASSERT_OBJ(obj, MY_CLASS);
433 
434     lv_table_t * table = (lv_table_t *)obj;
435     return table->row_cnt;
436 }
437 
lv_table_get_col_cnt(lv_obj_t * obj)438 uint16_t lv_table_get_col_cnt(lv_obj_t * obj)
439 {
440     LV_ASSERT_OBJ(obj, MY_CLASS);
441 
442     lv_table_t * table = (lv_table_t *)obj;
443     return table->col_cnt;
444 }
445 
lv_table_get_col_width(lv_obj_t * obj,uint16_t col)446 lv_coord_t lv_table_get_col_width(lv_obj_t * obj, uint16_t col)
447 {
448     LV_ASSERT_OBJ(obj, MY_CLASS);
449 
450     lv_table_t * table = (lv_table_t *)obj;
451 
452     if(col >= table->col_cnt) {
453         LV_LOG_WARN("lv_table_set_col_width: too big 'col_id'. Must be < LV_TABLE_COL_MAX.");
454         return 0;
455     }
456 
457     return table->col_w[col];
458 }
459 
lv_table_has_cell_ctrl(lv_obj_t * obj,uint16_t row,uint16_t col,lv_table_cell_ctrl_t ctrl)460 bool lv_table_has_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl)
461 {
462     LV_ASSERT_OBJ(obj, MY_CLASS);
463 
464     lv_table_t * table = (lv_table_t *)obj;
465     if(row >= table->row_cnt || col >= table->col_cnt) {
466         LV_LOG_WARN("lv_table_get_cell_crop: invalid row or column");
467         return false;
468     }
469     uint32_t cell = row * table->col_cnt + col;
470 
471     if(is_cell_empty(table->cell_data[cell])) return false;
472     else return (table->cell_data[cell]->ctrl & ctrl) == ctrl;
473 }
474 
lv_table_get_selected_cell(lv_obj_t * obj,uint16_t * row,uint16_t * col)475 void lv_table_get_selected_cell(lv_obj_t * obj, uint16_t * row, uint16_t * col)
476 {
477     lv_table_t * table = (lv_table_t *)obj;
478     *row = table->row_act;
479     *col = table->col_act;
480 }
481 
482 #if LV_USE_USER_DATA
lv_table_get_cell_user_data(lv_obj_t * obj,uint16_t row,uint16_t col)483 void * lv_table_get_cell_user_data(lv_obj_t * obj, uint16_t row, uint16_t col)
484 {
485     LV_ASSERT_OBJ(obj, MY_CLASS);
486 
487     lv_table_t * table = (lv_table_t *)obj;
488     if(row >= table->row_cnt || col >= table->col_cnt) {
489         LV_LOG_WARN("invalid row or column");
490         return NULL;
491     }
492     uint32_t cell = row * table->col_cnt + col;
493 
494     if(is_cell_empty(table->cell_data[cell])) return NULL;
495 
496     return table->cell_data[cell]->user_data;
497 }
498 #endif
499 
500 /**********************
501  *   STATIC FUNCTIONS
502  **********************/
503 
lv_table_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)504 static void lv_table_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
505 {
506     LV_UNUSED(class_p);
507     LV_TRACE_OBJ_CREATE("begin");
508 
509     lv_table_t * table = (lv_table_t *)obj;
510 
511     table->col_cnt = 1;
512     table->row_cnt = 1;
513     table->col_w = lv_mem_alloc(table->col_cnt * sizeof(table->col_w[0]));
514     table->row_h = lv_mem_alloc(table->row_cnt * sizeof(table->row_h[0]));
515     table->col_w[0] = LV_DPI_DEF;
516     table->row_h[0] = LV_DPI_DEF;
517     table->cell_data = lv_mem_realloc(table->cell_data, table->row_cnt * table->col_cnt * sizeof(lv_table_cell_t *));
518     table->cell_data[0] = NULL;
519 
520     LV_TRACE_OBJ_CREATE("finished");
521 }
522 
lv_table_destructor(const lv_obj_class_t * class_p,lv_obj_t * obj)523 static void lv_table_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
524 {
525     LV_UNUSED(class_p);
526     lv_table_t * table = (lv_table_t *)obj;
527     /*Free the cell texts*/
528     uint16_t i;
529     for(i = 0; i < table->col_cnt * table->row_cnt; i++) {
530         if(table->cell_data[i]) {
531 #if LV_USE_USER_DATA
532             if(table->cell_data[i]->user_data) {
533                 lv_mem_free(table->cell_data[i]->user_data);
534                 table->cell_data[i]->user_data = NULL;
535             }
536 #endif
537             lv_mem_free(table->cell_data[i]);
538             table->cell_data[i] = NULL;
539         }
540     }
541 
542     if(table->cell_data) lv_mem_free(table->cell_data);
543     if(table->row_h) lv_mem_free(table->row_h);
544     if(table->col_w) lv_mem_free(table->col_w);
545 }
546 
lv_table_event(const lv_obj_class_t * class_p,lv_event_t * e)547 static void lv_table_event(const lv_obj_class_t * class_p, lv_event_t * e)
548 {
549     LV_UNUSED(class_p);
550 
551     lv_res_t res;
552 
553     /*Call the ancestor's event handler*/
554     res = lv_obj_event_base(MY_CLASS, e);
555     if(res != LV_RES_OK) return;
556 
557     lv_event_code_t code = lv_event_get_code(e);
558     lv_obj_t * obj = lv_event_get_target(e);
559     lv_table_t * table = (lv_table_t *)obj;
560 
561     if(code == LV_EVENT_STYLE_CHANGED) {
562         refr_size_form_row(obj, 0);
563     }
564     else if(code == LV_EVENT_GET_SELF_SIZE) {
565         lv_point_t * p = lv_event_get_param(e);
566         uint32_t i;
567         lv_coord_t w = 0;
568         for(i = 0; i < table->col_cnt; i++) w += table->col_w[i];
569 
570         lv_coord_t h = 0;
571         for(i = 0; i < table->row_cnt; i++) h += table->row_h[i];
572 
573         p->x = w - 1;
574         p->y = h - 1;
575     }
576     else if(code == LV_EVENT_PRESSED || code == LV_EVENT_PRESSING) {
577         uint16_t col;
578         uint16_t row;
579         lv_res_t pr_res = get_pressed_cell(obj, &row, &col);
580 
581         if(pr_res == LV_RES_OK && (table->col_act != col || table->row_act != row)) {
582             table->col_act = col;
583             table->row_act = row;
584             lv_obj_invalidate(obj);
585         }
586     }
587     else if(code == LV_EVENT_RELEASED) {
588         lv_obj_invalidate(obj);
589         lv_indev_t * indev = lv_indev_get_act();
590         lv_obj_t * scroll_obj = lv_indev_get_scroll_obj(indev);
591         if(table->col_act != LV_TABLE_CELL_NONE && table->row_act != LV_TABLE_CELL_NONE && scroll_obj == NULL) {
592             res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
593             if(res != LV_RES_OK) return;
594         }
595 
596         lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
597         if(indev_type == LV_INDEV_TYPE_POINTER || indev_type == LV_INDEV_TYPE_BUTTON) {
598             table->col_act = LV_TABLE_CELL_NONE;
599             table->row_act = LV_TABLE_CELL_NONE;
600         }
601     }
602     else if(code == LV_EVENT_FOCUSED) {
603         lv_obj_invalidate(obj);
604     }
605     else if(code == LV_EVENT_KEY) {
606         int32_t c = *((int32_t *)lv_event_get_param(e));
607         int32_t col = table->col_act;
608         int32_t row = table->row_act;
609         if(col == LV_TABLE_CELL_NONE || row == LV_TABLE_CELL_NONE) {
610             table->col_act = 0;
611             table->row_act = 0;
612             scroll_to_selected_cell(obj);
613             lv_obj_invalidate(obj);
614             return;
615         }
616 
617         if(col >= table->col_cnt) col = 0;
618         if(row >= table->row_cnt) row = 0;
619 
620         if(c == LV_KEY_LEFT) col--;
621         else if(c == LV_KEY_RIGHT) col++;
622         else if(c == LV_KEY_UP) row--;
623         else if(c == LV_KEY_DOWN) row++;
624         else return;
625 
626         if(col >= table->col_cnt) {
627             if(row < table->row_cnt - 1) {
628                 col = 0;
629                 row++;
630             }
631             else {
632                 col = table->col_cnt - 1;
633             }
634         }
635         else if(col < 0) {
636             if(row != 0) {
637                 col = table->col_cnt - 1;
638                 row--;
639             }
640             else {
641                 col = 0;
642             }
643         }
644 
645         if(row >= table->row_cnt) {
646             row = table->row_cnt - 1;
647         }
648         else if(row < 0) {
649             row = 0;
650         }
651 
652         if(table->col_act != col || table->row_act != row) {
653             table->col_act = col;
654             table->row_act = row;
655             lv_obj_invalidate(obj);
656 
657             scroll_to_selected_cell(obj);
658 
659             res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
660             if(res != LV_RES_OK) return;
661         }
662     }
663     else if(code == LV_EVENT_DRAW_MAIN) {
664         draw_main(e);
665     }
666 }
667 
draw_main(lv_event_t * e)668 static void draw_main(lv_event_t * e)
669 {
670     lv_obj_t * obj = lv_event_get_target(e);
671     lv_table_t * table = (lv_table_t *)obj;
672     lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
673     lv_area_t clip_area;
674     if(!_lv_area_intersect(&clip_area, &obj->coords, draw_ctx->clip_area)) return;
675 
676     const lv_area_t * clip_area_ori = draw_ctx->clip_area;
677     draw_ctx->clip_area = &clip_area;
678 
679     lv_point_t txt_size;
680     lv_area_t cell_area;
681 
682     lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
683     lv_coord_t bg_top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
684     lv_coord_t bg_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN);
685     lv_coord_t bg_left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
686     lv_coord_t bg_right = lv_obj_get_style_pad_right(obj, LV_PART_MAIN);
687 
688     lv_state_t state_ori = obj->state;
689     obj->state = LV_STATE_DEFAULT;
690     obj->skip_trans = 1;
691     lv_draw_rect_dsc_t rect_dsc_def;
692     lv_draw_rect_dsc_t rect_dsc_act; /*Passed to the event to modify it*/
693     lv_draw_rect_dsc_init(&rect_dsc_def);
694     lv_obj_init_draw_rect_dsc(obj, LV_PART_ITEMS, &rect_dsc_def);
695 
696     lv_draw_label_dsc_t label_dsc_def;
697     lv_draw_label_dsc_t label_dsc_act;  /*Passed to the event to modify it*/
698     lv_draw_label_dsc_init(&label_dsc_def);
699     lv_obj_init_draw_label_dsc(obj, LV_PART_ITEMS, &label_dsc_def);
700     obj->state = state_ori;
701     obj->skip_trans = 0;
702 
703     uint16_t col;
704     uint16_t row;
705     uint16_t cell = 0;
706 
707     cell_area.y2 = obj->coords.y1 + bg_top - 1 - lv_obj_get_scroll_y(obj) + border_width;
708     lv_coord_t scroll_x = lv_obj_get_scroll_x(obj) ;
709     bool rtl = lv_obj_get_style_base_dir(obj, LV_PART_MAIN) == LV_BASE_DIR_RTL;
710 
711     /*Handle custom drawer*/
712     lv_obj_draw_part_dsc_t part_draw_dsc;
713     lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
714     part_draw_dsc.part = LV_PART_ITEMS;
715     part_draw_dsc.class_p = MY_CLASS;
716     part_draw_dsc.type = LV_TABLE_DRAW_PART_CELL;
717     part_draw_dsc.rect_dsc = &rect_dsc_act;
718     part_draw_dsc.label_dsc = &label_dsc_act;
719 
720     for(row = 0; row < table->row_cnt; row++) {
721         lv_coord_t h_row = table->row_h[row];
722 
723         cell_area.y1 = cell_area.y2 + 1;
724         cell_area.y2 = cell_area.y1 + h_row - 1;
725 
726         if(cell_area.y1 > clip_area.y2) break;
727 
728         if(rtl) cell_area.x1 = obj->coords.x2 - bg_right - 1 - scroll_x - border_width;
729         else cell_area.x2 = obj->coords.x1 + bg_left - 1 - scroll_x + border_width;
730 
731         for(col = 0; col < table->col_cnt; col++) {
732             lv_table_cell_ctrl_t ctrl = 0;
733             if(table->cell_data[cell]) ctrl = table->cell_data[cell]->ctrl;
734 
735             if(rtl) {
736                 cell_area.x2 = cell_area.x1 - 1;
737                 cell_area.x1 = cell_area.x2 - table->col_w[col] + 1;
738             }
739             else {
740                 cell_area.x1 = cell_area.x2 + 1;
741                 cell_area.x2 = cell_area.x1 + table->col_w[col] - 1;
742             }
743 
744             uint16_t col_merge = 0;
745             for(col_merge = 0; col_merge + col < table->col_cnt - 1; col_merge++) {
746                 lv_table_cell_t * next_cell_data = table->cell_data[cell + col_merge];
747 
748                 if(is_cell_empty(next_cell_data)) break;
749 
750                 lv_table_cell_ctrl_t merge_ctrl = (lv_table_cell_ctrl_t) next_cell_data->ctrl;
751                 if(merge_ctrl & LV_TABLE_CELL_CTRL_MERGE_RIGHT) {
752                     lv_coord_t offset = table->col_w[col + col_merge + 1];
753 
754                     if(rtl) cell_area.x1 -= offset;
755                     else cell_area.x2 += offset;
756                 }
757                 else {
758                     break;
759                 }
760             }
761 
762             if(cell_area.y2 < clip_area.y1) {
763                 cell += col_merge + 1;
764                 col += col_merge;
765                 continue;
766             }
767 
768             /*Expand the cell area with a half border to avoid drawing 2 borders next to each other*/
769             lv_area_t cell_area_border;
770             lv_area_copy(&cell_area_border, &cell_area);
771             if((rect_dsc_def.border_side & LV_BORDER_SIDE_LEFT) && cell_area_border.x1 > obj->coords.x1 + bg_left) {
772                 cell_area_border.x1 -= rect_dsc_def.border_width / 2;
773             }
774             if((rect_dsc_def.border_side & LV_BORDER_SIDE_TOP) && cell_area_border.y1 > obj->coords.y1 + bg_top) {
775                 cell_area_border.y1 -= rect_dsc_def.border_width / 2;
776             }
777             if((rect_dsc_def.border_side & LV_BORDER_SIDE_RIGHT) && cell_area_border.x2 < obj->coords.x2 - bg_right - 1) {
778                 cell_area_border.x2 += rect_dsc_def.border_width / 2 + (rect_dsc_def.border_width & 0x1);
779             }
780             if((rect_dsc_def.border_side & LV_BORDER_SIDE_BOTTOM) &&
781                cell_area_border.y2 < obj->coords.y2 - bg_bottom - 1) {
782                 cell_area_border.y2 += rect_dsc_def.border_width / 2 + (rect_dsc_def.border_width & 0x1);
783             }
784 
785             lv_state_t cell_state = LV_STATE_DEFAULT;
786             if(row == table->row_act && col == table->col_act) {
787                 if(!(obj->state & LV_STATE_SCROLLED) && (obj->state & LV_STATE_PRESSED)) cell_state |= LV_STATE_PRESSED;
788                 if(obj->state & LV_STATE_FOCUSED) cell_state |= LV_STATE_FOCUSED;
789                 if(obj->state & LV_STATE_FOCUS_KEY) cell_state |= LV_STATE_FOCUS_KEY;
790                 if(obj->state & LV_STATE_EDITED) cell_state |= LV_STATE_EDITED;
791             }
792 
793             /*Set up the draw descriptors*/
794             if(cell_state == LV_STATE_DEFAULT) {
795                 lv_memcpy(&rect_dsc_act, &rect_dsc_def, sizeof(lv_draw_rect_dsc_t));
796                 lv_memcpy(&label_dsc_act, &label_dsc_def, sizeof(lv_draw_label_dsc_t));
797             }
798             /*In other cases get the styles directly without caching them*/
799             else {
800                 obj->state = cell_state;
801                 obj->skip_trans = 1;
802                 lv_draw_rect_dsc_init(&rect_dsc_act);
803                 lv_draw_label_dsc_init(&label_dsc_act);
804                 lv_obj_init_draw_rect_dsc(obj, LV_PART_ITEMS, &rect_dsc_act);
805                 lv_obj_init_draw_label_dsc(obj, LV_PART_ITEMS, &label_dsc_act);
806                 obj->state = state_ori;
807                 obj->skip_trans = 0;
808             }
809 
810             part_draw_dsc.draw_area = &cell_area_border;
811             part_draw_dsc.id = row * table->col_cnt + col;
812             lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
813 
814             lv_draw_rect(draw_ctx, &rect_dsc_act, &cell_area_border);
815 
816             if(table->cell_data[cell]) {
817                 const lv_coord_t cell_left = lv_obj_get_style_pad_left(obj, LV_PART_ITEMS);
818                 const lv_coord_t cell_right = lv_obj_get_style_pad_right(obj, LV_PART_ITEMS);
819                 const lv_coord_t cell_top = lv_obj_get_style_pad_top(obj, LV_PART_ITEMS);
820                 const lv_coord_t cell_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_ITEMS);
821                 lv_text_flag_t txt_flags = LV_TEXT_FLAG_NONE;
822                 lv_area_t txt_area;
823 
824                 txt_area.x1 = cell_area.x1 + cell_left;
825                 txt_area.x2 = cell_area.x2 - cell_right;
826                 txt_area.y1 = cell_area.y1 + cell_top;
827                 txt_area.y2 = cell_area.y2 - cell_bottom;
828 
829                 /*Align the content to the middle if not cropped*/
830                 bool crop = ctrl & LV_TABLE_CELL_CTRL_TEXT_CROP ? true : false;
831                 if(crop) txt_flags = LV_TEXT_FLAG_EXPAND;
832 
833                 lv_txt_get_size(&txt_size, table->cell_data[cell]->txt, label_dsc_def.font,
834                                 label_dsc_act.letter_space, label_dsc_act.line_space,
835                                 lv_area_get_width(&txt_area), txt_flags);
836 
837                 /*Align the content to the middle if not cropped*/
838                 if(!crop) {
839                     txt_area.y1 = cell_area.y1 + h_row / 2 - txt_size.y / 2;
840                     txt_area.y2 = cell_area.y1 + h_row / 2 + txt_size.y / 2;
841                 }
842 
843                 lv_area_t label_clip_area;
844                 bool label_mask_ok;
845                 label_mask_ok = _lv_area_intersect(&label_clip_area, &clip_area, &cell_area);
846                 if(label_mask_ok) {
847                     draw_ctx->clip_area = &label_clip_area;
848                     lv_draw_label(draw_ctx, &label_dsc_act, &txt_area, table->cell_data[cell]->txt, NULL);
849                     draw_ctx->clip_area = &clip_area;
850                 }
851             }
852 
853             lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
854 
855             cell += col_merge + 1;
856             col += col_merge;
857         }
858     }
859 
860     draw_ctx->clip_area = clip_area_ori;
861 }
862 
863 /* Refreshes size of the table starting from @start_row row */
refr_size_form_row(lv_obj_t * obj,uint32_t start_row)864 static void refr_size_form_row(lv_obj_t * obj, uint32_t start_row)
865 {
866     const lv_coord_t cell_pad_left = lv_obj_get_style_pad_left(obj, LV_PART_ITEMS);
867     const lv_coord_t cell_pad_right = lv_obj_get_style_pad_right(obj, LV_PART_ITEMS);
868     const lv_coord_t cell_pad_top = lv_obj_get_style_pad_top(obj, LV_PART_ITEMS);
869     const lv_coord_t cell_pad_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_ITEMS);
870 
871     lv_coord_t letter_space = lv_obj_get_style_text_letter_space(obj, LV_PART_ITEMS);
872     lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_ITEMS);
873     const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_ITEMS);
874 
875     const lv_coord_t minh = lv_obj_get_style_min_height(obj, LV_PART_ITEMS);
876     const lv_coord_t maxh = lv_obj_get_style_max_height(obj, LV_PART_ITEMS);
877 
878     lv_table_t * table = (lv_table_t *)obj;
879     uint32_t i;
880     for(i = start_row; i < table->row_cnt; i++) {
881         lv_coord_t calculated_height = get_row_height(obj, i, font, letter_space, line_space,
882                                                       cell_pad_left, cell_pad_right, cell_pad_top, cell_pad_bottom);
883         table->row_h[i] = LV_CLAMP(minh, calculated_height, maxh);
884     }
885 
886     lv_obj_refresh_self_size(obj);
887     lv_obj_invalidate(obj);
888 }
889 
refr_cell_size(lv_obj_t * obj,uint32_t row,uint32_t col)890 static void refr_cell_size(lv_obj_t * obj, uint32_t row, uint32_t col)
891 {
892     const lv_coord_t cell_pad_left = lv_obj_get_style_pad_left(obj, LV_PART_ITEMS);
893     const lv_coord_t cell_pad_right = lv_obj_get_style_pad_right(obj, LV_PART_ITEMS);
894     const lv_coord_t cell_pad_top = lv_obj_get_style_pad_top(obj, LV_PART_ITEMS);
895     const lv_coord_t cell_pad_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_ITEMS);
896 
897     lv_coord_t letter_space = lv_obj_get_style_text_letter_space(obj, LV_PART_ITEMS);
898     lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_ITEMS);
899     const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_ITEMS);
900 
901     const lv_coord_t minh = lv_obj_get_style_min_height(obj, LV_PART_ITEMS);
902     const lv_coord_t maxh = lv_obj_get_style_max_height(obj, LV_PART_ITEMS);
903 
904     lv_table_t * table = (lv_table_t *)obj;
905     lv_coord_t calculated_height = get_row_height(obj, row, font, letter_space, line_space,
906                                                   cell_pad_left, cell_pad_right, cell_pad_top, cell_pad_bottom);
907 
908     lv_coord_t prev_row_size = table->row_h[row];
909     table->row_h[row] = LV_CLAMP(minh, calculated_height, maxh);
910 
911     /*If the row height havn't changed invalidate only this cell*/
912     if(prev_row_size == table->row_h[row]) {
913         lv_area_t cell_area;
914         get_cell_area(obj, row, col, &cell_area);
915         lv_area_move(&cell_area, obj->coords.x1, obj->coords.y1);
916         lv_obj_invalidate_area(obj, &cell_area);
917     }
918     else {
919         lv_obj_refresh_self_size(obj);
920         lv_obj_invalidate(obj);
921     }
922 }
923 
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)924 static lv_coord_t get_row_height(lv_obj_t * obj, uint16_t row_id, const lv_font_t * font,
925                                  lv_coord_t letter_space, lv_coord_t line_space,
926                                  lv_coord_t cell_left, lv_coord_t cell_right, lv_coord_t cell_top, lv_coord_t cell_bottom)
927 {
928     lv_table_t * table = (lv_table_t *)obj;
929 
930     lv_coord_t h_max = lv_font_get_line_height(font) + cell_top + cell_bottom;
931     /* Calculate the cell_data index where to start */
932     uint16_t row_start = row_id * table->col_cnt;
933 
934     /* Traverse the cells in the row_id row */
935     uint16_t cell;
936     uint16_t col;
937     for(cell = row_start, col = 0; cell < row_start + table->col_cnt; cell++, col++) {
938         lv_table_cell_t * cell_data = table->cell_data[cell];
939 
940         if(is_cell_empty(cell_data)) {
941             continue;
942         }
943 
944         lv_coord_t txt_w = table->col_w[col];
945 
946         /* Traverse the current row from the first until the penultimate column.
947          * Increment the text width if the cell has the LV_TABLE_CELL_CTRL_MERGE_RIGHT control,
948          * exit the traversal when the current cell control is not LV_TABLE_CELL_CTRL_MERGE_RIGHT */
949         uint16_t col_merge = 0;
950         for(col_merge = 0; col_merge + col < table->col_cnt - 1; col_merge++) {
951             lv_table_cell_t * next_cell_data = table->cell_data[cell + col_merge];
952 
953             if(is_cell_empty(next_cell_data)) break;
954 
955             lv_table_cell_ctrl_t ctrl = (lv_table_cell_ctrl_t) next_cell_data->ctrl;
956             if(ctrl & LV_TABLE_CELL_CTRL_MERGE_RIGHT) {
957                 txt_w += table->col_w[col + col_merge + 1];
958             }
959             else {
960                 break;
961             }
962         }
963 
964         lv_table_cell_ctrl_t ctrl = (lv_table_cell_ctrl_t) cell_data->ctrl;
965 
966         /*When cropping the text we can assume the row height is equal to the line height*/
967         if(ctrl & LV_TABLE_CELL_CTRL_TEXT_CROP) {
968             h_max = LV_MAX(lv_font_get_line_height(font) + cell_top + cell_bottom,
969                            h_max);
970         }
971         /*Else we have to calculate the height of the cell text*/
972         else {
973             lv_point_t txt_size;
974             txt_w -= cell_left + cell_right;
975 
976             lv_txt_get_size(&txt_size, table->cell_data[cell]->txt, font,
977                             letter_space, line_space, txt_w, LV_TEXT_FLAG_NONE);
978 
979             h_max = LV_MAX(txt_size.y + cell_top + cell_bottom, h_max);
980             /*Skip until one element after the last merged column*/
981             cell += col_merge;
982             col += col_merge;
983         }
984     }
985 
986     return h_max;
987 }
988 
get_pressed_cell(lv_obj_t * obj,uint16_t * row,uint16_t * col)989 static lv_res_t get_pressed_cell(lv_obj_t * obj, uint16_t * row, uint16_t * col)
990 {
991     lv_table_t * table = (lv_table_t *)obj;
992 
993     lv_indev_type_t type = lv_indev_get_type(lv_indev_get_act());
994     if(type != LV_INDEV_TYPE_POINTER && type != LV_INDEV_TYPE_BUTTON) {
995         if(col) *col = LV_TABLE_CELL_NONE;
996         if(row) *row = LV_TABLE_CELL_NONE;
997         return LV_RES_INV;
998     }
999 
1000     lv_point_t p;
1001     lv_indev_get_point(lv_indev_get_act(), &p);
1002 
1003     lv_coord_t tmp;
1004     if(col) {
1005         lv_coord_t x = p.x + lv_obj_get_scroll_x(obj);
1006 
1007         if(lv_obj_get_style_base_dir(obj, LV_PART_MAIN) == LV_BASE_DIR_RTL) {
1008             x = obj->coords.x2 - lv_obj_get_style_pad_right(obj, LV_PART_MAIN) - x;
1009         }
1010         else {
1011             x -= obj->coords.x1;
1012             x -= lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
1013         }
1014 
1015         *col = 0;
1016         tmp = 0;
1017         for(*col = 0; *col < table->col_cnt; (*col)++) {
1018             tmp += table->col_w[*col];
1019             if(x < tmp) break;
1020         }
1021     }
1022 
1023     if(row) {
1024         lv_coord_t y = p.y + lv_obj_get_scroll_y(obj);;
1025         y -= obj->coords.y1;
1026         y -= lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
1027 
1028         *row = 0;
1029         tmp = 0;
1030 
1031         for(*row = 0; *row < table->row_cnt; (*row)++) {
1032             tmp += table->row_h[*row];
1033             if(y < tmp) break;
1034         }
1035     }
1036 
1037     return LV_RES_OK;
1038 }
1039 
1040 /* Returns number of bytes to allocate based on chars configuration */
get_cell_txt_len(const char * txt)1041 static size_t get_cell_txt_len(const char * txt)
1042 {
1043     size_t retval = 0;
1044 
1045 #if LV_USE_ARABIC_PERSIAN_CHARS
1046     retval = sizeof(lv_table_cell_t) + _lv_txt_ap_calc_bytes_cnt(txt) + 1;
1047 #else
1048     retval = sizeof(lv_table_cell_t) + strlen(txt) + 1;
1049 #endif
1050 
1051     return retval;
1052 }
1053 
1054 /* Copy txt into dst skipping the format byte */
copy_cell_txt(lv_table_cell_t * dst,const char * txt)1055 static void copy_cell_txt(lv_table_cell_t * dst, const char * txt)
1056 {
1057 #if LV_USE_ARABIC_PERSIAN_CHARS
1058     _lv_txt_ap_proc(txt, dst->txt);
1059 #else
1060     strcpy(dst->txt, txt);
1061 #endif
1062 }
1063 
get_cell_area(lv_obj_t * obj,uint16_t row,uint16_t col,lv_area_t * area)1064 static void get_cell_area(lv_obj_t * obj, uint16_t row, uint16_t col, lv_area_t * area)
1065 {
1066     lv_table_t * table = (lv_table_t *)obj;
1067 
1068     uint32_t c;
1069     area->x1 = 0;
1070     for(c = 0; c < col; c++) {
1071         area->x1 += table->col_w[c];
1072     }
1073 
1074     bool rtl = lv_obj_get_style_base_dir(obj, LV_PART_MAIN) == LV_BASE_DIR_RTL;
1075     if(rtl) {
1076         area->x1 += lv_obj_get_scroll_x(obj);
1077         lv_coord_t w = lv_obj_get_width(obj);
1078         area->x2 = w - area->x1 - lv_obj_get_style_pad_right(obj, 0);
1079         area->x1 = area->x2 - table->col_w[col];
1080     }
1081     else {
1082         area->x1 -= lv_obj_get_scroll_x(obj);
1083         area->x1 += lv_obj_get_style_pad_left(obj, 0);
1084         area->x2 = area->x1 + table->col_w[col] - 1;
1085     }
1086 
1087     uint32_t r;
1088     area->y1 = 0;
1089     for(r = 0; r < row; r++) {
1090         area->y1 += table->row_h[r];
1091     }
1092 
1093     area->y1 += lv_obj_get_style_pad_top(obj, 0);
1094     area->y1 -= lv_obj_get_scroll_y(obj);
1095     area->y2 = area->y1 + table->row_h[row] - 1;
1096 
1097 }
1098 
scroll_to_selected_cell(lv_obj_t * obj)1099 static void scroll_to_selected_cell(lv_obj_t * obj)
1100 {
1101     lv_table_t * table = (lv_table_t *)obj;
1102 
1103     lv_area_t a;
1104     get_cell_area(obj, table->row_act, table->col_act, &a);
1105     if(a.x1 < 0) {
1106         lv_obj_scroll_by_bounded(obj, -a.x1, 0, LV_ANIM_ON);
1107     }
1108     else if(a.x2 > lv_obj_get_width(obj)) {
1109         lv_obj_scroll_by_bounded(obj, lv_obj_get_width(obj) - a.x2, 0, LV_ANIM_ON);
1110     }
1111 
1112     if(a.y1 < 0) {
1113         lv_obj_scroll_by_bounded(obj, 0, -a.y1, LV_ANIM_ON);
1114     }
1115     else if(a.y2 > lv_obj_get_height(obj)) {
1116         lv_obj_scroll_by_bounded(obj, 0, lv_obj_get_height(obj) - a.y2, LV_ANIM_ON);
1117     }
1118 
1119 }
1120 #endif
1121