Changeset ff6e91b in mainline for uspace/lib/ui/src/paint.c
- Timestamp:
- 2021-08-30T20:41:27Z (4 years ago)
- Branches:
- master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 81ec7e1
- Parents:
- 9b2e20c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ui/src/paint.c
r9b2e20c rff6e91b 38 38 #include <gfx/context.h> 39 39 #include <gfx/render.h> 40 #include <gfx/text.h> 40 41 #include <ui/paint.h> 42 #include <stdlib.h> 43 #include <str.h> 41 44 #include "../private/resource.h" 45 46 /** Single box characters */ 47 static ui_box_chars_t single_box_chars = { 48 { 49 { "\u250c", "\u2500", "\u2510" }, 50 { "\u2502", " ", "\u2502" }, 51 { "\u2514", "\u2500", "\u2518" } 52 } 53 }; 54 55 /** Double box characters */ 56 static ui_box_chars_t double_box_chars = { 57 { 58 { "\u2554", "\u2550", "\u2557" }, 59 { "\u2551", " ", "\u2551" }, 60 { "\u255a", "\u2550", "\u255d" } 61 } 62 }; 42 63 43 64 /** Paint bevel. … … 324 345 } 325 346 347 /** Paint a text box. 348 * 349 * @param resource UI resource 350 * @param rect Rectangle inside which to paint the box 351 * @param style Box style 352 * @param color Color 353 * @return EOK on success or an error code 354 */ 355 errno_t ui_paint_text_box(ui_resource_t *resource, gfx_rect_t *rect, 356 ui_box_style_t style, gfx_color_t *color) 357 { 358 errno_t rc; 359 gfx_text_fmt_t fmt; 360 gfx_rect_t srect; 361 gfx_coord2_t pos; 362 gfx_coord2_t dim; 363 size_t bufsz; 364 size_t off; 365 int i; 366 gfx_coord_t y; 367 char *str = NULL; 368 ui_box_chars_t *boxc = NULL; 369 370 gfx_rect_points_sort(rect, &srect); 371 gfx_rect_dims(&srect, &dim); 372 373 /* Is rectangle large enough to hold box? */ 374 if (dim.x < 2 || dim.y < 2) 375 return EOK; 376 377 switch (style) { 378 case ui_box_single: 379 boxc = &single_box_chars; 380 break; 381 case ui_box_double: 382 boxc = &double_box_chars; 383 break; 384 } 385 386 if (boxc == NULL) 387 return EINVAL; 388 389 gfx_text_fmt_init(&fmt); 390 fmt.color = color; 391 392 bufsz = str_size(boxc->c[0][0]) + 393 str_size(boxc->c[0][1]) * (dim.x - 2) + 394 str_size(boxc->c[0][2]) + 1; 395 396 str = malloc(bufsz); 397 if (str == NULL) 398 return ENOMEM; 399 400 /* Top edge and corners */ 401 402 str_cpy(str, bufsz, boxc->c[0][0]); 403 off = str_size(boxc->c[0][0]); 404 405 for (i = 1; i < dim.x - 1; i++) { 406 str_cpy(str + off, bufsz - off, boxc->c[0][1]); 407 off += str_size(boxc->c[0][1]); 408 } 409 410 str_cpy(str + off, bufsz - off, boxc->c[0][2]); 411 off += str_size(boxc->c[0][2]); 412 str[off] = '\0'; 413 414 pos = rect->p0; 415 rc = gfx_puttext(resource->font, &pos, &fmt, str); 416 if (rc != EOK) 417 goto error; 418 419 /* Vertical edges */ 420 for (y = rect->p0.y + 1; y < rect->p1.y - 1; y++) { 421 pos.y = y; 422 423 pos.x = rect->p0.x; 424 rc = gfx_puttext(resource->font, &pos, &fmt, 425 boxc->c[1][0]); 426 if (rc != EOK) 427 goto error; 428 429 pos.x = rect->p1.x - 1; 430 rc = gfx_puttext(resource->font, &pos, &fmt, 431 boxc->c[1][2]); 432 if (rc != EOK) 433 goto error; 434 } 435 436 /* Bottom edge and corners */ 437 438 str_cpy(str, bufsz, boxc->c[2][0]); 439 off = str_size(boxc->c[2][0]); 440 441 for (i = 1; i < dim.x - 1; i++) { 442 str_cpy(str + off, bufsz - off, boxc->c[2][1]); 443 off += str_size(boxc->c[2][1]); 444 } 445 446 str_cpy(str + off, bufsz - off, boxc->c[2][2]); 447 off += str_size(boxc->c[2][2]); 448 str[off] = '\0'; 449 450 pos.x = rect->p0.x; 451 pos.y = rect->p1.y - 1; 452 rc = gfx_puttext(resource->font, &pos, &fmt, str); 453 if (rc != EOK) 454 goto error; 455 456 free(str); 457 return EOK; 458 error: 459 if (str != NULL) 460 free(str); 461 return rc; 462 } 463 326 464 /** @} 327 465 */
Note:
See TracChangeset
for help on using the changeset viewer.