Changes in uspace/lib/ui/src/entry.c [a106037:d0dfbba] in mainline
- File:
-
- 1 edited
-
uspace/lib/ui/src/entry.c (modified) (29 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ui/src/entry.c
ra106037 rd0dfbba 1 1 /* 2 * Copyright (c) 202 1Jiri Svoboda2 * Copyright (c) 2026 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 37 37 */ 38 38 39 #include <assert.h> 39 40 #include <clipboard.h> 40 41 #include <errno.h> … … 44 45 #include <gfx/text.h> 45 46 #include <macros.h> 47 #include <stdio.h> 46 48 #include <stdlib.h> 47 49 #include <str.h> … … 67 69 ui_entry_cursor_width = 2, 68 70 ui_entry_sel_hpad = 0, 69 ui_entry_sel_vpad = 2 71 ui_entry_sel_vpad = 2, 72 /** Additional amount to scroll to the left after revealing cursor */ 73 ui_entry_left_scroll_margin = 30 70 74 }; 71 75 … … 156 160 { 157 161 entry->halign = halign; 162 ui_entry_scroll_update(entry, true); 163 ui_entry_paint(entry); 158 164 } 159 165 … … 187 193 entry->sel_start = entry->pos; 188 194 195 ui_entry_scroll_update(entry, false); 196 ui_entry_paint(entry); 197 189 198 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; 190 208 } 191 209 … … 298 316 299 317 gfx_text_fmt_init(&fmt); 318 fmt.font = res->font; 300 319 fmt.color = res->entry_fg_color; 301 320 fmt.halign = gfx_halign_left; … … 313 332 entry->text[off1] = '\0'; 314 333 315 rc = gfx_puttext( res->font,&pos, &fmt, entry->text);334 rc = gfx_puttext(&pos, &fmt, entry->text); 316 335 if (rc != EOK) { 317 336 (void) gfx_set_clip_rect(res->gc, NULL); … … 319 338 } 320 339 321 gfx_text_cont( res->font,&pos, &fmt, entry->text, &cpos, &cfmt);340 gfx_text_cont(&pos, &fmt, entry->text, &cpos, &cfmt); 322 341 entry->text[off1] = c; 323 342 … … 327 346 c = entry->text[off2]; 328 347 entry->text[off2] = '\0'; 329 cfmt.color = res->entry_ bg_color;330 331 gfx_text_rect( res->font,&cpos, &cfmt, entry->text + off1, &sel);348 cfmt.color = res->entry_sel_text_fg_color; 349 350 gfx_text_rect(&cpos, &cfmt, entry->text + off1, &sel); 332 351 sel.p0.x -= ui_entry_sel_hpad; 333 352 sel.p0.y -= ui_entry_sel_vpad; … … 335 354 sel.p1.y += ui_entry_sel_vpad; 336 355 337 rc = gfx_set_color(res->gc, res->entry_ fg_color);356 rc = gfx_set_color(res->gc, res->entry_sel_text_bg_color); 338 357 if (rc != EOK) 339 358 goto error; … … 343 362 goto error; 344 363 345 rc = gfx_puttext( res->font,&cpos, &cfmt, entry->text + off1);364 rc = gfx_puttext(&cpos, &cfmt, entry->text + off1); 346 365 if (rc != EOK) { 347 366 (void) gfx_set_clip_rect(res->gc, NULL); … … 349 368 } 350 369 351 gfx_text_cont(res->font, &cpos, &cfmt, entry->text + off1, 352 &cpos, &cfmt); 370 gfx_text_cont(&cpos, &cfmt, entry->text + off1, &cpos, &cfmt); 353 371 354 372 entry->text[off2] = c; … … 358 376 cfmt.color = res->entry_fg_color; 359 377 360 rc = gfx_puttext( res->font,&cpos, &cfmt, entry->text + off2);378 rc = gfx_puttext(&cpos, &cfmt, entry->text + off2); 361 379 if (rc != EOK) { 362 380 (void) gfx_set_clip_rect(res->gc, NULL); … … 405 423 406 424 gfx_text_fmt_init(&fmt); 425 fmt.font = res->font; 407 426 fmt.halign = gfx_halign_left; 408 427 fmt.valign = gfx_valign_top; 409 428 410 return gfx_text_find_pos(res->font, &geom.text_pos, &fmt, 411 entry->text, fpos); 429 return gfx_text_find_pos(&geom.text_pos, &fmt, entry->text, fpos); 412 430 } 413 431 … … 452 470 entry->pos = off1; 453 471 entry->sel_start = off1; 472 ui_entry_scroll_update(entry, false); 454 473 ui_entry_paint(entry); 455 474 } … … 494 513 495 514 entry->sel_start = entry->pos; 515 ui_entry_scroll_update(entry, false); 496 516 ui_entry_paint(entry); 497 517 … … 526 546 entry->sel_start = off; 527 547 548 ui_entry_scroll_update(entry, false); 528 549 ui_entry_paint(entry); 529 550 } … … 551 572 str_size(entry->text + off) + 1); 552 573 574 ui_entry_scroll_update(entry, false); 553 575 ui_entry_paint(entry); 554 576 } … … 644 666 break; 645 667 } 646 647 668 return ui_claimed; 648 669 } … … 723 744 if (!entry->active) 724 745 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 }734 746 735 747 /* … … 748 760 if (event->type == KEY_RELEASE && event->key == KC_RSHIFT) 749 761 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 } 750 772 751 773 if (event->type == KEY_PRESS && … … 875 897 gfx_coord_t hpad; 876 898 gfx_coord_t vpad; 877 gfx_coord_t width;878 899 ui_resource_t *res; 879 900 … … 895 916 } 896 917 897 width = gfx_text_width(res->font, entry->text); 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; 898 926 899 927 switch (entry->halign) { 900 928 case gfx_halign_left: 901 929 case gfx_halign_justify: 902 geom-> text_pos.x = geom->interior_rect.p0.x + hpad;930 geom->anchor_x = geom->text_rect.p0.x; 903 931 break; 904 932 case gfx_halign_center: 905 geom-> text_pos.x = (geom->interior_rect.p0.x +906 geom-> interior_rect.p1.x) / 2 - width/ 2;933 geom->anchor_x = (geom->text_rect.p0.x + 934 geom->text_rect.p1.x) / 2; 907 935 break; 908 936 case gfx_halign_right: 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; 937 geom->anchor_x = geom->text_rect.p1.x; 938 break; 939 } 914 940 } 915 941 … … 945 971 if (!shift) 946 972 entry->sel_start = entry->pos; 973 974 ui_entry_scroll_update(entry, false); 947 975 (void) ui_entry_paint(entry); 948 976 } … … 959 987 if (!shift) 960 988 entry->sel_start = entry->pos; 989 990 ui_entry_scroll_update(entry, false); 961 991 (void) ui_entry_paint(entry); 962 992 } … … 978 1008 if (!shift) 979 1009 entry->sel_start = entry->pos; 1010 1011 ui_entry_scroll_update(entry, false); 980 1012 (void) ui_entry_paint(entry); 981 1013 } … … 997 1029 if (!shift) 998 1030 entry->sel_start = entry->pos; 1031 1032 ui_entry_scroll_update(entry, false); 999 1033 (void) ui_entry_paint(entry); 1000 1034 } … … 1021 1055 } 1022 1056 1057 /** Update text entry scroll position. 1058 * 1059 * @param entry Text entry 1060 * @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 realign 1063 * 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 it 1086 * 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 needed 1093 * 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 cursor 1101 * is deliberately not taken into account (i.e. we only care 1102 * 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 1023 1127 /** @} 1024 1128 */
Note:
See TracChangeset
for help on using the changeset viewer.
