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