Changes in uspace/lib/ui/src/entry.c [d0dfbba:a106037] in mainline
- File:
-
- 1 edited
-
uspace/lib/ui/src/entry.c (modified) (29 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ui/src/entry.c
rd0dfbba ra106037 1 1 /* 2 * Copyright (c) 202 6Jiri Svoboda2 * Copyright (c) 2021 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 37 37 */ 38 38 39 #include <assert.h>40 39 #include <clipboard.h> 41 40 #include <errno.h> … … 45 44 #include <gfx/text.h> 46 45 #include <macros.h> 47 #include <stdio.h>48 46 #include <stdlib.h> 49 47 #include <str.h> … … 69 67 ui_entry_cursor_width = 2, 70 68 ui_entry_sel_hpad = 0, 71 ui_entry_sel_vpad = 2, 72 /** Additional amount to scroll to the left after revealing cursor */ 73 ui_entry_left_scroll_margin = 30 69 ui_entry_sel_vpad = 2 74 70 }; 75 71 … … 160 156 { 161 157 entry->halign = halign; 162 ui_entry_scroll_update(entry, true);163 ui_entry_paint(entry);164 158 } 165 159 … … 193 187 entry->sel_start = entry->pos; 194 188 195 ui_entry_scroll_update(entry, false);196 ui_entry_paint(entry);197 198 189 return EOK; 199 }200 201 /** Get entry text.202 *203 * @return Pointer to entry text.204 */205 const char *ui_entry_get_text(ui_entry_t *entry)206 {207 return entry->text;208 190 } 209 191 … … 316 298 317 299 gfx_text_fmt_init(&fmt); 318 fmt.font = res->font;319 300 fmt.color = res->entry_fg_color; 320 301 fmt.halign = gfx_halign_left; … … 332 313 entry->text[off1] = '\0'; 333 314 334 rc = gfx_puttext( &pos, &fmt, entry->text);315 rc = gfx_puttext(res->font, &pos, &fmt, entry->text); 335 316 if (rc != EOK) { 336 317 (void) gfx_set_clip_rect(res->gc, NULL); … … 338 319 } 339 320 340 gfx_text_cont( &pos, &fmt, entry->text, &cpos, &cfmt);321 gfx_text_cont(res->font, &pos, &fmt, entry->text, &cpos, &cfmt); 341 322 entry->text[off1] = c; 342 323 … … 346 327 c = entry->text[off2]; 347 328 entry->text[off2] = '\0'; 348 cfmt.color = res->entry_ sel_text_fg_color;349 350 gfx_text_rect( &cpos, &cfmt, entry->text + off1, &sel);329 cfmt.color = res->entry_bg_color; 330 331 gfx_text_rect(res->font, &cpos, &cfmt, entry->text + off1, &sel); 351 332 sel.p0.x -= ui_entry_sel_hpad; 352 333 sel.p0.y -= ui_entry_sel_vpad; … … 354 335 sel.p1.y += ui_entry_sel_vpad; 355 336 356 rc = gfx_set_color(res->gc, res->entry_ sel_text_bg_color);337 rc = gfx_set_color(res->gc, res->entry_fg_color); 357 338 if (rc != EOK) 358 339 goto error; … … 362 343 goto error; 363 344 364 rc = gfx_puttext( &cpos, &cfmt, entry->text + off1);345 rc = gfx_puttext(res->font, &cpos, &cfmt, entry->text + off1); 365 346 if (rc != EOK) { 366 347 (void) gfx_set_clip_rect(res->gc, NULL); … … 368 349 } 369 350 370 gfx_text_cont(&cpos, &cfmt, entry->text + off1, &cpos, &cfmt); 351 gfx_text_cont(res->font, &cpos, &cfmt, entry->text + off1, 352 &cpos, &cfmt); 371 353 372 354 entry->text[off2] = c; … … 376 358 cfmt.color = res->entry_fg_color; 377 359 378 rc = gfx_puttext( &cpos, &cfmt, entry->text + off2);360 rc = gfx_puttext(res->font, &cpos, &cfmt, entry->text + off2); 379 361 if (rc != EOK) { 380 362 (void) gfx_set_clip_rect(res->gc, NULL); … … 423 405 424 406 gfx_text_fmt_init(&fmt); 425 fmt.font = res->font;426 407 fmt.halign = gfx_halign_left; 427 408 fmt.valign = gfx_valign_top; 428 409 429 return gfx_text_find_pos(&geom.text_pos, &fmt, entry->text, fpos); 410 return gfx_text_find_pos(res->font, &geom.text_pos, &fmt, 411 entry->text, fpos); 430 412 } 431 413 … … 470 452 entry->pos = off1; 471 453 entry->sel_start = off1; 472 ui_entry_scroll_update(entry, false);473 454 ui_entry_paint(entry); 474 455 } … … 513 494 514 495 entry->sel_start = entry->pos; 515 ui_entry_scroll_update(entry, false);516 496 ui_entry_paint(entry); 517 497 … … 546 526 entry->sel_start = off; 547 527 548 ui_entry_scroll_update(entry, false);549 528 ui_entry_paint(entry); 550 529 } … … 572 551 str_size(entry->text + off) + 1); 573 552 574 ui_entry_scroll_update(entry, false);575 553 ui_entry_paint(entry); 576 554 } … … 666 644 break; 667 645 } 646 668 647 return ui_claimed; 669 648 } … … 744 723 if (!entry->active) 745 724 return ui_unclaimed; 725 726 if (event->type == KEY_PRESS && event->c >= ' ') { 727 off = 0; 728 rc = chr_encode(event->c, buf, &off, sizeof(buf)); 729 if (rc == EOK) { 730 buf[off] = '\0'; 731 (void) ui_entry_insert_str(entry, buf); 732 } 733 } 746 734 747 735 /* … … 760 748 if (event->type == KEY_RELEASE && event->key == KC_RSHIFT) 761 749 entry->rshift_held = false; 762 763 if (event->type == KEY_PRESS &&764 (event->mods & (KM_CTRL | KM_ALT)) == 0 && event->c >= ' ') {765 off = 0;766 rc = chr_encode(event->c, buf, &off, sizeof(buf));767 if (rc == EOK) {768 buf[off] = '\0';769 (void) ui_entry_insert_str(entry, buf);770 }771 }772 750 773 751 if (event->type == KEY_PRESS && … … 897 875 gfx_coord_t hpad; 898 876 gfx_coord_t vpad; 877 gfx_coord_t width; 899 878 ui_resource_t *res; 900 879 … … 916 895 } 917 896 918 geom->text_rect.p0.x = geom->interior_rect.p0.x + hpad; 919 geom->text_rect.p0.y = geom->interior_rect.p0.y + vpad; 920 geom->text_rect.p1.x = geom->interior_rect.p1.x - hpad; 921 geom->text_rect.p1.y = geom->interior_rect.p1.y - vpad; 922 923 geom->text_pos.x = geom->interior_rect.p0.x + hpad + 924 entry->scroll_pos; 925 geom->text_pos.y = geom->interior_rect.p0.y + vpad; 897 width = gfx_text_width(res->font, entry->text); 926 898 927 899 switch (entry->halign) { 928 900 case gfx_halign_left: 929 901 case gfx_halign_justify: 930 geom-> anchor_x = geom->text_rect.p0.x;902 geom->text_pos.x = geom->interior_rect.p0.x + hpad; 931 903 break; 932 904 case gfx_halign_center: 933 geom-> anchor_x = (geom->text_rect.p0.x +934 geom-> text_rect.p1.x)/ 2;905 geom->text_pos.x = (geom->interior_rect.p0.x + 906 geom->interior_rect.p1.x) / 2 - width / 2; 935 907 break; 936 908 case gfx_halign_right: 937 geom->anchor_x = geom->text_rect.p1.x; 938 break; 939 } 909 geom->text_pos.x = geom->interior_rect.p1.x - hpad - 1 - width; 910 break; 911 } 912 913 geom->text_pos.y = geom->interior_rect.p0.y + vpad; 940 914 } 941 915 … … 971 945 if (!shift) 972 946 entry->sel_start = entry->pos; 973 974 ui_entry_scroll_update(entry, false);975 947 (void) ui_entry_paint(entry); 976 948 } … … 987 959 if (!shift) 988 960 entry->sel_start = entry->pos; 989 990 ui_entry_scroll_update(entry, false);991 961 (void) ui_entry_paint(entry); 992 962 } … … 1008 978 if (!shift) 1009 979 entry->sel_start = entry->pos; 1010 1011 ui_entry_scroll_update(entry, false);1012 980 (void) ui_entry_paint(entry); 1013 981 } … … 1029 997 if (!shift) 1030 998 entry->sel_start = entry->pos; 1031 1032 ui_entry_scroll_update(entry, false);1033 999 (void) ui_entry_paint(entry); 1034 1000 } … … 1055 1021 } 1056 1022 1057 /** Update text entry scroll position.1058 *1059 * @param entry Text entry1060 * @param realign @c true iff we should left-align short text.1061 * This should be only used when changing text alignment,1062 * because left-aligned text entries should not realign1063 * the text to the left side under normal circumstances.1064 */1065 void ui_entry_scroll_update(ui_entry_t *entry, bool realign)1066 {1067 ui_entry_geom_t geom;1068 gfx_coord_t x;1069 gfx_coord_t width;1070 gfx_coord2_t tpos;1071 gfx_coord2_t anchor;1072 gfx_text_fmt_t fmt;1073 ui_resource_t *res;1074 1075 res = ui_window_get_res(entry->window);1076 1077 ui_entry_get_geom(entry, &geom);1078 1079 /* Compute position where cursor is currently displayed at */1080 x = geom.text_pos.x + ui_entry_lwidth(entry);1081 1082 /* Is cursor off to the left? */1083 if (x < geom.text_rect.p0.x) {1084 /*1085 * Scroll to make cursor visible and put some space between it1086 * and the left edge of the text rectangle.1087 */1088 entry->scroll_pos += geom.text_rect.p0.x - x +1089 ui_entry_left_scroll_margin;1090 1091 /*1092 * We don't want to scroll further than what's needed1093 * to reveal the beginning of the text.1094 */1095 if (entry->scroll_pos > 0)1096 entry->scroll_pos = 0;1097 }1098 1099 /*1100 * Is cursor off to the right? Note that the width of the cursor1101 * is deliberately not taken into account (i.e. we only care1102 * about the left edge of the cursor).1103 */1104 if (x > geom.text_rect.p1.x)1105 entry->scroll_pos -= x - geom.text_rect.p1.x;1106 1107 width = gfx_text_width(res->font, entry->text);1108 1109 if (width < geom.text_rect.p1.x - geom.text_rect.p0.x &&1110 (realign || entry->halign != gfx_halign_left)) {1111 /* Text fits inside entry, so we need to align it */1112 anchor.x = geom.anchor_x;1113 anchor.y = 0;1114 gfx_text_fmt_init(&fmt);1115 fmt.font = res->font;1116 fmt.halign = entry->halign;1117 gfx_text_start_pos(&anchor, &fmt, entry->text, &tpos);1118 entry->scroll_pos = tpos.x - geom.text_rect.p0.x;1119 } else if (geom.text_pos.x + width < geom.text_rect.p1.x &&1120 entry->halign != gfx_halign_left) {1121 /* Text is long, unused space on the right */1122 entry->scroll_pos += geom.text_rect.p1.x -1123 geom.text_pos.x - width;1124 }1125 }1126 1127 1023 /** @} 1128 1024 */
Note:
See TracChangeset
for help on using the changeset viewer.
