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