Ignore:
File:
1 edited

Legend:

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

    ra106037 rb987eb4  
    6767        ui_entry_cursor_width = 2,
    6868        ui_entry_sel_hpad = 0,
    69         ui_entry_sel_vpad = 2
     69        ui_entry_sel_vpad = 2,
     70        /** Additional amount to scroll to the left after revealing cursor */
     71        ui_entry_left_scroll_margin = 30
    7072};
    7173
     
    156158{
    157159        entry->halign = halign;
     160        ui_entry_scroll_update(entry, true);
     161        ui_entry_paint(entry);
    158162}
    159163
     
    187191        entry->sel_start = entry->pos;
    188192
     193        ui_entry_scroll_update(entry, false);
     194        ui_entry_paint(entry);
     195
    189196        return EOK;
     197}
     198
     199/** Get entry text.
     200 *
     201 * @return Pointer to entry text.
     202 */
     203const char *ui_entry_get_text(ui_entry_t *entry)
     204{
     205        return entry->text;
    190206}
    191207
     
    298314
    299315        gfx_text_fmt_init(&fmt);
     316        fmt.font = res->font;
    300317        fmt.color = res->entry_fg_color;
    301318        fmt.halign = gfx_halign_left;
     
    313330        entry->text[off1] = '\0';
    314331
    315         rc = gfx_puttext(res->font, &pos, &fmt, entry->text);
     332        rc = gfx_puttext(&pos, &fmt, entry->text);
    316333        if (rc != EOK) {
    317334                (void) gfx_set_clip_rect(res->gc, NULL);
     
    319336        }
    320337
    321         gfx_text_cont(res->font, &pos, &fmt, entry->text, &cpos, &cfmt);
     338        gfx_text_cont(&pos, &fmt, entry->text, &cpos, &cfmt);
    322339        entry->text[off1] = c;
    323340
     
    327344                c = entry->text[off2];
    328345                entry->text[off2] = '\0';
    329                 cfmt.color = res->entry_bg_color;
    330 
    331                 gfx_text_rect(res->font, &cpos, &cfmt, entry->text + off1, &sel);
     346                cfmt.color = res->entry_sel_text_fg_color;
     347
     348                gfx_text_rect(&cpos, &cfmt, entry->text + off1, &sel);
    332349                sel.p0.x -= ui_entry_sel_hpad;
    333350                sel.p0.y -= ui_entry_sel_vpad;
     
    335352                sel.p1.y += ui_entry_sel_vpad;
    336353
    337                 rc = gfx_set_color(res->gc, res->entry_fg_color);
     354                rc = gfx_set_color(res->gc, res->entry_sel_text_bg_color);
    338355                if (rc != EOK)
    339356                        goto error;
     
    343360                        goto error;
    344361
    345                 rc = gfx_puttext(res->font, &cpos, &cfmt, entry->text + off1);
     362                rc = gfx_puttext(&cpos, &cfmt, entry->text + off1);
    346363                if (rc != EOK) {
    347364                        (void) gfx_set_clip_rect(res->gc, NULL);
     
    349366                }
    350367
    351                 gfx_text_cont(res->font, &cpos, &cfmt, entry->text + off1,
    352                     &cpos, &cfmt);
     368                gfx_text_cont(&cpos, &cfmt, entry->text + off1, &cpos, &cfmt);
    353369
    354370                entry->text[off2] = c;
     
    358374        cfmt.color = res->entry_fg_color;
    359375
    360         rc = gfx_puttext(res->font, &cpos, &cfmt, entry->text + off2);
     376        rc = gfx_puttext(&cpos, &cfmt, entry->text + off2);
    361377        if (rc != EOK) {
    362378                (void) gfx_set_clip_rect(res->gc, NULL);
     
    405421
    406422        gfx_text_fmt_init(&fmt);
     423        fmt.font = res->font;
    407424        fmt.halign = gfx_halign_left;
    408425        fmt.valign = gfx_valign_top;
    409426
    410         return gfx_text_find_pos(res->font, &geom.text_pos, &fmt,
    411             entry->text, fpos);
     427        return gfx_text_find_pos(&geom.text_pos, &fmt, entry->text, fpos);
    412428}
    413429
     
    452468        entry->pos = off1;
    453469        entry->sel_start = off1;
     470        ui_entry_scroll_update(entry, false);
    454471        ui_entry_paint(entry);
    455472}
     
    494511
    495512        entry->sel_start = entry->pos;
     513        ui_entry_scroll_update(entry, false);
    496514        ui_entry_paint(entry);
    497515
     
    526544        entry->sel_start = off;
    527545
     546        ui_entry_scroll_update(entry, false);
    528547        ui_entry_paint(entry);
    529548}
     
    551570            str_size(entry->text + off) + 1);
    552571
     572        ui_entry_scroll_update(entry, false);
    553573        ui_entry_paint(entry);
    554574}
     
    644664                break;
    645665        }
    646 
    647666        return ui_claimed;
    648667}
     
    723742        if (!entry->active)
    724743                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         }
    734744
    735745        /*
     
    748758        if (event->type == KEY_RELEASE && event->key == KC_RSHIFT)
    749759                entry->rshift_held = false;
     760
     761        if (event->type == KEY_PRESS &&
     762            (event->mods & (KM_CTRL | KM_ALT)) == 0 && event->c >= ' ') {
     763                off = 0;
     764                rc = chr_encode(event->c, buf, &off, sizeof(buf));
     765                if (rc == EOK) {
     766                        buf[off] = '\0';
     767                        (void) ui_entry_insert_str(entry, buf);
     768                }
     769        }
    750770
    751771        if (event->type == KEY_PRESS &&
     
    875895        gfx_coord_t hpad;
    876896        gfx_coord_t vpad;
    877         gfx_coord_t width;
    878897        ui_resource_t *res;
    879898
     
    895914        }
    896915
    897         width = gfx_text_width(res->font, entry->text);
     916        geom->text_rect.p0.x = geom->interior_rect.p0.x + hpad;
     917        geom->text_rect.p0.y = geom->interior_rect.p0.y + vpad;
     918        geom->text_rect.p1.x = geom->interior_rect.p1.x - hpad;
     919        geom->text_rect.p1.y = geom->interior_rect.p1.y - vpad;
     920
     921        geom->text_pos.x = geom->interior_rect.p0.x + hpad +
     922            entry->scroll_pos;
     923        geom->text_pos.y = geom->interior_rect.p0.y + vpad;
    898924
    899925        switch (entry->halign) {
    900926        case gfx_halign_left:
    901927        case gfx_halign_justify:
    902                 geom->text_pos.x = geom->interior_rect.p0.x + hpad;
     928                geom->anchor_x = geom->text_rect.p0.x;
    903929                break;
    904930        case gfx_halign_center:
    905                 geom->text_pos.x = (geom->interior_rect.p0.x +
    906                     geom->interior_rect.p1.x) / 2 - width / 2;
     931                geom->anchor_x = (geom->text_rect.p0.x +
     932                    geom->text_rect.p1.x) / 2;
    907933                break;
    908934        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;
     935                geom->anchor_x = geom->text_rect.p1.x;
     936                break;
     937        }
    914938}
    915939
     
    945969        if (!shift)
    946970                entry->sel_start = entry->pos;
     971
     972        ui_entry_scroll_update(entry, false);
    947973        (void) ui_entry_paint(entry);
    948974}
     
    959985        if (!shift)
    960986                entry->sel_start = entry->pos;
     987
     988        ui_entry_scroll_update(entry, false);
    961989        (void) ui_entry_paint(entry);
    962990}
     
    9781006        if (!shift)
    9791007                entry->sel_start = entry->pos;
     1008
     1009        ui_entry_scroll_update(entry, false);
    9801010        (void) ui_entry_paint(entry);
    9811011}
     
    9971027        if (!shift)
    9981028                entry->sel_start = entry->pos;
     1029
     1030        ui_entry_scroll_update(entry, false);
    9991031        (void) ui_entry_paint(entry);
    10001032}
     
    10211053}
    10221054
     1055/** Update text entry scroll position.
     1056 *
     1057 * @param entry Text entry
     1058 * @param realign @c true iff we should left-align short text.
     1059 *                This should be only used when changing text alignment,
     1060 *                because left-aligned text entries should not realign
     1061 *                the text to the left side under normal circumstances.
     1062 */
     1063void ui_entry_scroll_update(ui_entry_t *entry, bool realign)
     1064{
     1065        ui_entry_geom_t geom;
     1066        gfx_coord_t x;
     1067        gfx_coord_t width;
     1068        gfx_coord2_t tpos;
     1069        gfx_coord2_t anchor;
     1070        gfx_text_fmt_t fmt;
     1071        ui_resource_t *res;
     1072
     1073        res = ui_window_get_res(entry->window);
     1074
     1075        ui_entry_get_geom(entry, &geom);
     1076
     1077        /* Compute position where cursor is currently displayed at */
     1078        x = geom.text_pos.x + ui_entry_lwidth(entry);
     1079
     1080        /* Is cursor off to the left? */
     1081        if (x < geom.text_rect.p0.x) {
     1082                /*
     1083                 * Scroll to make cursor visible and put some space between it
     1084                 * and the left edge of the text rectangle.
     1085                 */
     1086                entry->scroll_pos += geom.text_rect.p0.x - x +
     1087                    ui_entry_left_scroll_margin;
     1088
     1089                /*
     1090                 * We don't want to scroll further than what's needed
     1091                 * to reveal the beginning of the text.
     1092                 */
     1093                if (entry->scroll_pos > 0)
     1094                        entry->scroll_pos = 0;
     1095        }
     1096
     1097        /*
     1098         * Is cursor off to the right? Note that the width of the cursor
     1099         * is deliberately not taken into account (i.e. we only care
     1100         * about the left edge of the cursor).
     1101         */
     1102        if (x > geom.text_rect.p1.x)
     1103                entry->scroll_pos -= x - geom.text_rect.p1.x;
     1104
     1105        width = gfx_text_width(res->font, entry->text);
     1106
     1107        if (width < geom.text_rect.p1.x - geom.text_rect.p0.x &&
     1108            (realign || entry->halign != gfx_halign_left)) {
     1109                /* Text fits inside entry, so we need to align it */
     1110                anchor.x = geom.anchor_x;
     1111                anchor.y = 0;
     1112                gfx_text_fmt_init(&fmt);
     1113                fmt.font = res->font;
     1114                fmt.halign = entry->halign;
     1115                gfx_text_start_pos(&anchor, &fmt, entry->text, &tpos);
     1116                entry->scroll_pos = tpos.x - geom.text_rect.p0.x;
     1117        } else if (geom.text_pos.x + width < geom.text_rect.p1.x &&
     1118            entry->halign != gfx_halign_left) {
     1119                /* Text is long, unused space on the right */
     1120                entry->scroll_pos += geom.text_rect.p1.x -
     1121                    geom.text_pos.x - width;
     1122        }
     1123}
     1124
    10231125/** @}
    10241126 */
Note: See TracChangeset for help on using the changeset viewer.