Changeset 66a2becf in mainline
- Timestamp:
- 2020-11-11T18:05:01Z (4 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- b93ec7c0
- Parents:
- d942ca4
- Location:
- uspace
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/fontedit/fontedit.c
rd942ca4 r66a2becf 33 33 */ 34 34 35 #include <canvas.h>36 #include <draw/surface.h>37 35 #include <fibril.h> 38 #include <guigfx/canvas.h>39 36 #include <gfx/color.h> 40 37 #include <gfx/font.h> … … 47 44 #include <stdlib.h> 48 45 #include <str.h> 49 #include <task.h> 50 #include <window.h> 46 #include <ui/ui.h> 47 #include <ui/wdecor.h> 48 #include <ui/window.h> 51 49 #include "fontedit.h" 52 50 … … 58 56 59 57 static errno_t font_edit_paint(font_edit_t *); 58 59 static void font_edit_close_event(ui_window_t *, void *); 60 static void font_edit_kbd_event(ui_window_t *, void *, kbd_event_t *); 61 static void font_edit_pos_event(ui_window_t *, void *, pos_event_t *); 62 63 static ui_window_cb_t font_edit_window_cb = { 64 .close = font_edit_close_event, 65 .kbd = font_edit_kbd_event, 66 .pos = font_edit_pos_event 67 }; 60 68 61 69 /** Clear screen. … … 159 167 } 160 168 169 /** Handle font editor close event. 170 * 171 * @param window Window 172 * @param arg Argument (font_edit_t *) 173 */ 174 static void font_edit_close_event(ui_window_t *window, void *arg) 175 { 176 font_edit_t *fedit = (font_edit_t *) arg; 177 178 ui_quit(fedit->ui); 179 } 180 161 181 /** Handle font editor position event. 162 182 * 163 * @param widget Canvas widget 164 * @param data Position event 165 */ 166 static void font_edit_pos_event(widget_t *widget, void *data) 167 { 168 pos_event_t *event = (pos_event_t *) data; 169 font_edit_t *fedit; 183 * @param window Window 184 * @param arg Argument (font_edit_t *) 185 * @param event Position event 186 */ 187 static void font_edit_pos_event(ui_window_t *window, void *arg, 188 pos_event_t *event) 189 { 190 font_edit_t *fedit = (font_edit_t *) arg; 191 gfx_coord2_t pos; 192 gfx_rect_t rect; 170 193 int x, y; 171 194 172 fedit = (font_edit_t *) widget_get_data(widget); 173 174 if (event->type == POS_PRESS) { 175 x = gfx_coord_div_rneg((int)event->hpos - glyph_orig_x, 176 glyph_scale); 177 y = gfx_coord_div_rneg((int)event->vpos - glyph_orig_y, 178 glyph_scale); 179 180 printf("x=%d y=%d\n", x, y); 181 gfx_glyph_bmp_setpix(fedit->gbmp, x, y, fedit->pen_color); 182 font_edit_paint(fedit); 183 } 195 ui_window_get_app_rect(window, &rect); 196 197 pos.x = event->hpos; 198 pos.y = event->vpos; 199 200 if (event->type != POS_PRESS) 201 return; 202 203 if (!gfx_pix_inside_rect(&pos, &rect)) 204 return; 205 206 x = gfx_coord_div_rneg(pos.x - glyph_orig_x - 207 rect.p0.x, glyph_scale); 208 y = gfx_coord_div_rneg(pos.y - glyph_orig_y - 209 rect.p0.y, glyph_scale); 210 211 printf("x=%d y=%d\n", x, y); 212 gfx_glyph_bmp_setpix(fedit->gbmp, x, y, fedit->pen_color); 213 font_edit_paint(fedit); 184 214 } 185 215 … … 371 401 /** Handle font editor keyboard event. 372 402 * 373 * @param widget Canvas widget 374 * @param data Position event 375 */ 376 static void font_edit_kbd_event(widget_t *widget, void *data) 377 { 378 kbd_event_t *event = (kbd_event_t *) data; 379 font_edit_t *fedit; 380 381 fedit = (font_edit_t *) widget_get_data(widget); 403 * @param window Window 404 * @param arg Argument (font_edit_t *) 405 * @param event Keyboard event 406 */ 407 static void font_edit_kbd_event(ui_window_t *window, void *arg, 408 kbd_event_t *event) 409 { 410 font_edit_t *fedit = (font_edit_t *) arg; 382 411 383 412 if (event->type != KEY_PRESS) … … 440 469 static errno_t font_edit_paint_preview(font_edit_t *fedit) 441 470 { 471 gfx_color_t *color; 442 472 errno_t rc; 473 474 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color); 475 if (rc != EOK) 476 return rc; 477 478 rc = gfx_set_color(fedit->gc, color); 479 if (rc != EOK) 480 goto error; 443 481 444 482 rc = font_edit_paint_preview_str(fedit, 20, 20, 445 483 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 446 484 if (rc != EOK) 447 return rc;485 goto error; 448 486 449 487 rc = font_edit_paint_preview_str(fedit, 20, 40, 450 488 "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"); 451 489 if (rc != EOK) 452 return rc;490 goto error; 453 491 454 492 rc = font_edit_paint_preview_str(fedit, 20, 60, 455 493 "abcdefghijklmnopqrstuvwxyz"); 456 494 if (rc != EOK) 457 return rc;495 goto error; 458 496 459 497 rc = font_edit_paint_preview_str(fedit, 20, 80, 460 498 "the quick brown fox jumps over the lazy dog"); 461 499 if (rc != EOK) 462 return rc;500 goto error; 463 501 464 502 rc = font_edit_paint_preview_str(fedit, 20, 100, 465 503 "0123456789,./<>?;'\\:\"|[]{}`~!@#$%^&*()-_=+"); 466 504 if (rc != EOK) 467 return rc;505 goto error; 468 506 469 507 return EOK; 508 error: 509 gfx_color_delete(color); 510 return rc; 470 511 } 471 512 … … 620 661 /** Create font editor. 621 662 * 622 * @param display_s vc Display service663 * @param display_spec Display specifier 623 664 * @param fname Font file to open or @c NULL to create new font 624 665 * @param rfedit Place to store pointer to new font editor 625 666 * @return EOK on success or an error code 626 667 */ 627 static errno_t font_edit_create(const char *display_s vc, const char *fname,668 static errno_t font_edit_create(const char *display_spec, const char *fname, 628 669 font_edit_t **rfedit) 629 670 { 630 canvas_gc_t *cgc = NULL; 631 window_t *window = NULL; 632 pixel_t *pixbuf = NULL; 633 surface_t *surface = NULL; 634 canvas_t *canvas = NULL; 671 ui_t *ui = NULL; 672 ui_wnd_params_t params; 673 gfx_rect_t rect; 674 gfx_rect_t wrect; 675 gfx_coord2_t off; 676 ui_window_t *window = NULL; 677 gfx_context_t *gc = NULL; 635 678 font_edit_t *fedit = NULL; 636 679 gfx_typeface_t *tface = NULL; … … 643 686 gfx_glyph_bmp_t *bmp; 644 687 gfx_coord_t vw, vh; 645 gfx_context_t *gc;646 688 errno_t rc; 647 689 … … 652 694 } 653 695 654 printf("Init canvas..\n"); 655 656 window = window_open(display_svc, NULL, 657 WINDOW_MAIN | WINDOW_DECORATED, "Font Editor"); 658 if (window == NULL) { 659 printf("Error creating window.\n"); 660 rc = ENOMEM; 696 printf("Init UI..\n"); 697 698 rc = ui_create(display_spec, &ui); 699 if (rc != EOK) { 700 printf("Error initializing UI (%s)\n", display_spec); 661 701 goto error; 662 702 } … … 665 705 vh = 300; 666 706 667 pixbuf = calloc(vw * vh, sizeof(pixel_t)); 668 if (pixbuf == NULL) { 669 printf("Error allocating memory for pixel buffer.\n"); 670 rc = ENOMEM; 671 goto error; 672 } 673 674 surface = surface_create(vw, vh, pixbuf, 0); 675 if (surface == NULL) { 676 printf("Error creating surface.\n"); 677 rc = ENOMEM; 678 goto error; 679 } 680 681 /* Memory block pixbuf is now owned by surface */ 682 pixbuf = NULL; 683 684 canvas = create_canvas(window_root(window), fedit, vw, vh, 685 surface); 686 if (canvas == NULL) { 687 printf("Error creating canvas.\n"); 688 rc = ENOMEM; 689 goto error; 690 } 691 692 window_resize(window, 0, 0, vw + 10, vh + 30, WINDOW_PLACEMENT_ANY); 693 window_exec(window); 694 695 printf("Create canvas GC\n"); 696 rc = canvas_gc_create(canvas, surface, &cgc); 707 rect.p0.x = 0; 708 rect.p0.y = 0; 709 rect.p1.x = vw; 710 rect.p1.y = vh; 711 712 ui_wnd_params_init(¶ms); 713 params.caption = "Font Editor"; 714 715 /* 716 * Compute window rectangle such that application area corresponds 717 * to rect 718 */ 719 ui_wdecor_rect_from_app(&rect, &wrect); 720 off = wrect.p0; 721 gfx_rect_rtranslate(&off, &wrect, ¶ms.rect); 722 723 rc = ui_window_create(ui, ¶ms, &window); 697 724 if (rc != EOK) { 698 printf("Error creating canvas GC.\n"); 699 goto error; 700 } 701 702 gc = canvas_gc_get_ctx(cgc); 725 printf("Error creating window.\n"); 726 goto error; 727 } 728 729 ui_window_set_cb(window, &font_edit_window_cb, (void *) fedit); 730 731 rc = ui_window_get_app_gc(window, &gc); 732 if (rc != EOK) { 733 printf("Error creating graphic context.\n"); 734 goto error; 735 } 703 736 704 737 if (fname == NULL) { … … 755 788 } 756 789 757 sig_connect(&canvas->position_event, &canvas->widget,758 font_edit_pos_event);759 sig_connect(&canvas->keyboard_event, &canvas->widget,760 font_edit_kbd_event);761 762 790 if (fname == NULL) 763 791 fname = "new.tpf"; 764 792 765 fedit->cgc = cgc; 793 fedit->ui = ui; 794 fedit->window = window; 766 795 fedit->gc = gc; 767 796 fedit->width = vw; … … 790 819 if (tface != NULL) 791 820 gfx_typeface_destroy(tface); 792 if (surface != NULL)793 surface_destroy(surface);794 if (pixbuf != NULL)795 free(pixbuf);796 821 if (window != NULL) 797 window_close(window); 822 ui_window_destroy(window); 823 if (ui != NULL) 824 ui_destroy(ui); 798 825 if (fedit != NULL) 799 826 free(fedit); … … 801 828 } 802 829 830 /** Destroy font editor. 831 * 832 * @param fedit Font editor 833 */ 834 static void font_edit_destroy(font_edit_t *fedit) 835 { 836 gfx_glyph_bmp_close(fedit->gbmp); 837 gfx_glyph_destroy(fedit->glyph); 838 gfx_font_close(fedit->font); 839 gfx_typeface_destroy(fedit->typeface); 840 ui_window_destroy(fedit->window); 841 ui_destroy(fedit->ui); 842 free(fedit); 843 } 844 803 845 static void print_syntax(void) 804 846 { 805 printf("Syntax: fontedit [-d <display >] [<file.tpf>]\n");847 printf("Syntax: fontedit [-d <display-spec>] [<file.tpf>]\n"); 806 848 } 807 849 … … 809 851 { 810 852 errno_t rc; 811 const char *display_s vc =DISPLAY_DEFAULT;853 const char *display_spec = UI_DISPLAY_DEFAULT; 812 854 const char *fname = NULL; 813 855 font_edit_t *fedit; … … 824 866 } 825 867 826 display_s vc = argv[i++];868 display_spec = argv[i++]; 827 869 } else { 828 870 printf("Invalid option '%s'.\n", argv[i]); … … 845 887 } 846 888 847 rc = font_edit_create(display_s vc, fname, &fedit);889 rc = font_edit_create(display_spec, fname, &fedit); 848 890 if (rc != EOK) 849 891 return 1; … … 851 893 (void) font_edit_paint(fedit); 852 894 853 task_retval(0); 854 async_manager(); 855 895 ui_run(fedit->ui); 896 font_edit_destroy(fedit); 897 898 (void) font_edit_kbd_event; 899 (void) font_edit_pos_event; 856 900 return 0; 857 901 } -
uspace/app/fontedit/fontedit.h
rd942ca4 r66a2becf 40 40 #include <gfx/glyph_bmp.h> 41 41 #include <gfx/typeface.h> 42 #include <guigfx/canvas.h> 42 #include <ui/ui.h> 43 #include <ui/window.h> 43 44 44 45 /** Font editor */ 45 46 typedef struct { 46 /** Canvas GC */ 47 canvas_gc_t *cgc; 47 /** UI */ 48 ui_t *ui; 49 /** Window */ 50 ui_window_t *window; 48 51 /** Graphic context */ 49 52 gfx_context_t *gc; -
uspace/app/fontedit/meson.build
rd942ca4 r66a2becf 27 27 # 28 28 29 deps = [ ' gfx', 'guigfx', 'gfxfont' ]29 deps = [ 'ui', 'gfx', 'gfxfont' ] 30 30 src = files( 31 31 'fontedit.c', -
uspace/lib/ui/include/ui/window.h
rd942ca4 r66a2becf 55 55 extern ui_resource_t *ui_window_get_res(ui_window_t *); 56 56 extern gfx_context_t *ui_window_get_gc(ui_window_t *); 57 extern errno_t ui_window_get_app_gc(ui_window_t *, gfx_context_t **); 57 58 extern void ui_window_get_app_rect(ui_window_t *, gfx_rect_t *); 58 59 extern errno_t ui_window_paint(ui_window_t *); -
uspace/lib/ui/meson.build
rd942ca4 r66a2becf 27 27 # 28 28 29 deps = [ 'gfx', 'gfxfont', ' display' ]29 deps = [ 'gfx', 'gfxfont', 'memgfx', 'display' ] 30 30 src = files( 31 31 'src/control.c', -
uspace/lib/ui/private/window.h
rd942ca4 r66a2becf 59 59 /** Window GC */ 60 60 gfx_context_t *gc; 61 /** Application area bitmap */ 62 gfx_bitmap_t *app_bmp; 63 /** Application area GC */ 64 gfx_context_t *app_gc; 61 65 /** UI resource. Ideally this would be in ui_t. */ 62 66 struct ui_resource *res; -
uspace/lib/ui/src/window.c
rd942ca4 r66a2becf 36 36 #include <display.h> 37 37 #include <errno.h> 38 #include <gfx/bitmap.h> 38 39 #include <gfx/context.h> 39 40 #include <gfx/render.h> … … 41 42 #include <io/pos_event.h> 42 43 #include <mem.h> 44 #include <memgfx/memgc.h> 43 45 #include <stdlib.h> 44 46 #include <ui/control.h> … … 74 76 .move = wd_move 75 77 }; 78 79 static void ui_window_app_update(void *, gfx_rect_t *); 76 80 77 81 /** Initialize window parameters structure. … … 223 227 } 224 228 229 /** Get UI resource from window. 230 * 231 * @param window Window 232 * @return UI resource 233 */ 225 234 ui_resource_t *ui_window_get_res(ui_window_t *window) 226 235 { … … 228 237 } 229 238 239 /** Get window GC. 240 * 241 * @param window Window 242 * @return GC (relative to window) 243 */ 230 244 gfx_context_t *ui_window_get_gc(ui_window_t *window) 231 245 { … … 233 247 } 234 248 249 /** Get window application area GC 250 * 251 * @param window Window 252 * @param rgc Place to store GC (relative to application area) 253 * @return EOK on success or an error code 254 */ 255 errno_t ui_window_get_app_gc(ui_window_t *window, gfx_context_t **rgc) 256 { 257 gfx_bitmap_params_t params; 258 gfx_bitmap_alloc_t alloc; 259 gfx_rect_t rect; 260 mem_gc_t *memgc; 261 errno_t rc; 262 263 if (window->app_gc == NULL) { 264 assert(window->app_bmp == NULL); 265 266 gfx_bitmap_params_init(¶ms); 267 268 /* 269 * The bitmap will have the same dimensions as the 270 * application rectangle, but start at 0,0. 271 */ 272 ui_window_get_app_rect(window, &rect); 273 gfx_rect_rtranslate(&rect.p0, &rect, ¶ms.rect); 274 275 rc = gfx_bitmap_create(window->gc, ¶ms, NULL, 276 &window->app_bmp); 277 if (rc != EOK) 278 return rc; 279 280 rc = gfx_bitmap_get_alloc(window->app_bmp, &alloc); 281 if (rc != EOK) { 282 gfx_bitmap_destroy(window->app_bmp); 283 return rc; 284 } 285 286 rc = mem_gc_create(¶ms.rect, &alloc, ui_window_app_update, 287 (void *) window, &memgc); 288 if (rc != EOK) { 289 gfx_bitmap_destroy(window->app_bmp); 290 return rc; 291 } 292 293 window->app_gc = mem_gc_get_ctx(memgc); 294 } 295 296 *rgc = window->app_gc; 297 return EOK; 298 } 299 300 /** Get window application rectangle 301 * 302 * @param window Window 303 * @param rect Place to store application rectangle 304 */ 235 305 void ui_window_get_app_rect(ui_window_t *window, gfx_rect_t *rect) 236 306 { … … 241 311 } 242 312 313 /** Paint window 314 * 315 * @param window Window 316 * @return EOK on success or an error code 317 */ 243 318 errno_t ui_window_paint(ui_window_t *window) 244 319 { … … 428 503 } 429 504 505 /** Application area update callback 506 * 507 * @param arg Argument (ui_window_t *) 508 * @param rect Rectangle to update 509 */ 510 static void ui_window_app_update(void *arg, gfx_rect_t *rect) 511 { 512 ui_window_t *window = (ui_window_t *) arg; 513 gfx_rect_t arect; 514 515 ui_window_get_app_rect(window, &arect); 516 517 /* Render bitmap rectangle inside the application area */ 518 (void) gfx_bitmap_render(window->app_bmp, rect, &arect.p0); 519 } 520 430 521 /** @} 431 522 */ -
uspace/lib/ui/test/window.c
rd942ca4 r66a2becf 29 29 #include <gfx/context.h> 30 30 #include <gfx/coord.h> 31 #include <gfx/render.h> 31 32 #include <io/kbd_event.h> 32 33 #include <io/pos_event.h> … … 207 208 } 208 209 210 /** ui_window_get_app_gc() return valid GC */ 211 PCUT_TEST(get_app_gc) 212 { 213 errno_t rc; 214 ui_t *ui = NULL; 215 ui_wnd_params_t params; 216 ui_window_t *window = NULL; 217 gfx_context_t *gc; 218 219 rc = ui_create_disp(NULL, &ui); 220 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 221 222 ui_wnd_params_init(¶ms); 223 params.caption = "Hello"; 224 params.rect.p0.x = 0; 225 params.rect.p0.y = 0; 226 params.rect.p0.x = 10; 227 params.rect.p0.y = 10; 228 229 rc = ui_window_create(ui, ¶ms, &window); 230 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 231 PCUT_ASSERT_NOT_NULL(window); 232 233 rc = ui_window_get_app_gc(window, &gc); 234 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 235 PCUT_ASSERT_NOT_NULL(gc); 236 237 rc = gfx_fill_rect(gc, ¶ms.rect); 238 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 239 240 ui_window_destroy(window); 241 ui_destroy(ui); 242 } 243 209 244 /** Test ui_window_paint() */ 210 245 PCUT_TEST(paint)
Note:
See TracChangeset
for help on using the changeset viewer.