Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ui/src/entry.c

    rd0dfbba ra106037  
    11/*
    2  * Copyright (c) 2026 Jiri Svoboda
     2 * Copyright (c) 2021 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3737 */
    3838
    39 #include <assert.h>
    4039#include <clipboard.h>
    4140#include <errno.h>
     
    4544#include <gfx/text.h>
    4645#include <macros.h>
    47 #include <stdio.h>
    4846#include <stdlib.h>
    4947#include <str.h>
     
    6967        ui_entry_cursor_width = 2,
    7068        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
    7470};
    7571
     
    160156{
    161157        entry->halign = halign;
    162         ui_entry_scroll_update(entry, true);
    163         ui_entry_paint(entry);
    164158}
    165159
     
    193187        entry->sel_start = entry->pos;
    194188
    195         ui_entry_scroll_update(entry, false);
    196         ui_entry_paint(entry);
    197 
    198189        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;
    208190}
    209191
     
    316298
    317299        gfx_text_fmt_init(&fmt);
    318         fmt.font = res->font;
    319300        fmt.color = res->entry_fg_color;
    320301        fmt.halign = gfx_halign_left;
     
    332313        entry->text[off1] = '\0';
    333314
    334         rc = gfx_puttext(&pos, &fmt, entry->text);
     315        rc = gfx_puttext(res->font, &pos, &fmt, entry->text);
    335316        if (rc != EOK) {
    336317                (void) gfx_set_clip_rect(res->gc, NULL);
     
    338319        }
    339320
    340         gfx_text_cont(&pos, &fmt, entry->text, &cpos, &cfmt);
     321        gfx_text_cont(res->font, &pos, &fmt, entry->text, &cpos, &cfmt);
    341322        entry->text[off1] = c;
    342323
     
    346327                c = entry->text[off2];
    347328                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);
    351332                sel.p0.x -= ui_entry_sel_hpad;
    352333                sel.p0.y -= ui_entry_sel_vpad;
     
    354335                sel.p1.y += ui_entry_sel_vpad;
    355336
    356                 rc = gfx_set_color(res->gc, res->entry_sel_text_bg_color);
     337                rc = gfx_set_color(res->gc, res->entry_fg_color);
    357338                if (rc != EOK)
    358339                        goto error;
     
    362343                        goto error;
    363344
    364                 rc = gfx_puttext(&cpos, &cfmt, entry->text + off1);
     345                rc = gfx_puttext(res->font, &cpos, &cfmt, entry->text + off1);
    365346                if (rc != EOK) {
    366347                        (void) gfx_set_clip_rect(res->gc, NULL);
     
    368349                }
    369350
    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);
    371353
    372354                entry->text[off2] = c;
     
    376358        cfmt.color = res->entry_fg_color;
    377359
    378         rc = gfx_puttext(&cpos, &cfmt, entry->text + off2);
     360        rc = gfx_puttext(res->font, &cpos, &cfmt, entry->text + off2);
    379361        if (rc != EOK) {
    380362                (void) gfx_set_clip_rect(res->gc, NULL);
     
    423405
    424406        gfx_text_fmt_init(&fmt);
    425         fmt.font = res->font;
    426407        fmt.halign = gfx_halign_left;
    427408        fmt.valign = gfx_valign_top;
    428409
    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);
    430412}
    431413
     
    470452        entry->pos = off1;
    471453        entry->sel_start = off1;
    472         ui_entry_scroll_update(entry, false);
    473454        ui_entry_paint(entry);
    474455}
     
    513494
    514495        entry->sel_start = entry->pos;
    515         ui_entry_scroll_update(entry, false);
    516496        ui_entry_paint(entry);
    517497
     
    546526        entry->sel_start = off;
    547527
    548         ui_entry_scroll_update(entry, false);
    549528        ui_entry_paint(entry);
    550529}
     
    572551            str_size(entry->text + off) + 1);
    573552
    574         ui_entry_scroll_update(entry, false);
    575553        ui_entry_paint(entry);
    576554}
     
    666644                break;
    667645        }
     646
    668647        return ui_claimed;
    669648}
     
    744723        if (!entry->active)
    745724                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        }
    746734
    747735        /*
     
    760748        if (event->type == KEY_RELEASE && event->key == KC_RSHIFT)
    761749                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         }
    772750
    773751        if (event->type == KEY_PRESS &&
     
    897875        gfx_coord_t hpad;
    898876        gfx_coord_t vpad;
     877        gfx_coord_t width;
    899878        ui_resource_t *res;
    900879
     
    916895        }
    917896
    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);
    926898
    927899        switch (entry->halign) {
    928900        case gfx_halign_left:
    929901        case gfx_halign_justify:
    930                 geom->anchor_x = geom->text_rect.p0.x;
     902                geom->text_pos.x = geom->interior_rect.p0.x + hpad;
    931903                break;
    932904        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;
    935907                break;
    936908        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;
    940914}
    941915
     
    971945        if (!shift)
    972946                entry->sel_start = entry->pos;
    973 
    974         ui_entry_scroll_update(entry, false);
    975947        (void) ui_entry_paint(entry);
    976948}
     
    987959        if (!shift)
    988960                entry->sel_start = entry->pos;
    989 
    990         ui_entry_scroll_update(entry, false);
    991961        (void) ui_entry_paint(entry);
    992962}
     
    1008978        if (!shift)
    1009979                entry->sel_start = entry->pos;
    1010 
    1011         ui_entry_scroll_update(entry, false);
    1012980        (void) ui_entry_paint(entry);
    1013981}
     
    1029997        if (!shift)
    1030998                entry->sel_start = entry->pos;
    1031 
    1032         ui_entry_scroll_update(entry, false);
    1033999        (void) ui_entry_paint(entry);
    10341000}
     
    10551021}
    10561022
    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 
    11271023/** @}
    11281024 */
Note: See TracChangeset for help on using the changeset viewer.