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