Ignore:
File:
1 edited

Legend:

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

    rb987eb4 ra106037  
    6767        ui_entry_cursor_width = 2,
    6868        ui_entry_sel_hpad = 0,
    69         ui_entry_sel_vpad = 2,
    70         /** Additional amount to scroll to the left after revealing cursor */
    71         ui_entry_left_scroll_margin = 30
     69        ui_entry_sel_vpad = 2
    7270};
    7371
     
    158156{
    159157        entry->halign = halign;
    160         ui_entry_scroll_update(entry, true);
    161         ui_entry_paint(entry);
    162158}
    163159
     
    191187        entry->sel_start = entry->pos;
    192188
    193         ui_entry_scroll_update(entry, false);
    194         ui_entry_paint(entry);
    195 
    196189        return EOK;
    197 }
    198 
    199 /** Get entry text.
    200  *
    201  * @return Pointer to entry text.
    202  */
    203 const char *ui_entry_get_text(ui_entry_t *entry)
    204 {
    205         return entry->text;
    206190}
    207191
     
    314298
    315299        gfx_text_fmt_init(&fmt);
    316         fmt.font = res->font;
    317300        fmt.color = res->entry_fg_color;
    318301        fmt.halign = gfx_halign_left;
     
    330313        entry->text[off1] = '\0';
    331314
    332         rc = gfx_puttext(&pos, &fmt, entry->text);
     315        rc = gfx_puttext(res->font, &pos, &fmt, entry->text);
    333316        if (rc != EOK) {
    334317                (void) gfx_set_clip_rect(res->gc, NULL);
     
    336319        }
    337320
    338         gfx_text_cont(&pos, &fmt, entry->text, &cpos, &cfmt);
     321        gfx_text_cont(res->font, &pos, &fmt, entry->text, &cpos, &cfmt);
    339322        entry->text[off1] = c;
    340323
     
    344327                c = entry->text[off2];
    345328                entry->text[off2] = '\0';
    346                 cfmt.color = res->entry_sel_text_fg_color;
    347 
    348                 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);
    349332                sel.p0.x -= ui_entry_sel_hpad;
    350333                sel.p0.y -= ui_entry_sel_vpad;
     
    352335                sel.p1.y += ui_entry_sel_vpad;
    353336
    354                 rc = gfx_set_color(res->gc, res->entry_sel_text_bg_color);
     337                rc = gfx_set_color(res->gc, res->entry_fg_color);
    355338                if (rc != EOK)
    356339                        goto error;
     
    360343                        goto error;
    361344
    362                 rc = gfx_puttext(&cpos, &cfmt, entry->text + off1);
     345                rc = gfx_puttext(res->font, &cpos, &cfmt, entry->text + off1);
    363346                if (rc != EOK) {
    364347                        (void) gfx_set_clip_rect(res->gc, NULL);
     
    366349                }
    367350
    368                 gfx_text_cont(&cpos, &cfmt, entry->text + off1, &cpos, &cfmt);
     351                gfx_text_cont(res->font, &cpos, &cfmt, entry->text + off1,
     352                    &cpos, &cfmt);
    369353
    370354                entry->text[off2] = c;
     
    374358        cfmt.color = res->entry_fg_color;
    375359
    376         rc = gfx_puttext(&cpos, &cfmt, entry->text + off2);
     360        rc = gfx_puttext(res->font, &cpos, &cfmt, entry->text + off2);
    377361        if (rc != EOK) {
    378362                (void) gfx_set_clip_rect(res->gc, NULL);
     
    421405
    422406        gfx_text_fmt_init(&fmt);
    423         fmt.font = res->font;
    424407        fmt.halign = gfx_halign_left;
    425408        fmt.valign = gfx_valign_top;
    426409
    427         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);
    428412}
    429413
     
    468452        entry->pos = off1;
    469453        entry->sel_start = off1;
    470         ui_entry_scroll_update(entry, false);
    471454        ui_entry_paint(entry);
    472455}
     
    511494
    512495        entry->sel_start = entry->pos;
    513         ui_entry_scroll_update(entry, false);
    514496        ui_entry_paint(entry);
    515497
     
    544526        entry->sel_start = off;
    545527
    546         ui_entry_scroll_update(entry, false);
    547528        ui_entry_paint(entry);
    548529}
     
    570551            str_size(entry->text + off) + 1);
    571552
    572         ui_entry_scroll_update(entry, false);
    573553        ui_entry_paint(entry);
    574554}
     
    664644                break;
    665645        }
     646
    666647        return ui_claimed;
    667648}
     
    742723        if (!entry->active)
    743724                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        }
    744734
    745735        /*
     
    758748        if (event->type == KEY_RELEASE && event->key == KC_RSHIFT)
    759749                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         }
    770750
    771751        if (event->type == KEY_PRESS &&
     
    895875        gfx_coord_t hpad;
    896876        gfx_coord_t vpad;
     877        gfx_coord_t width;
    897878        ui_resource_t *res;
    898879
     
    914895        }
    915896
    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;
     897        width = gfx_text_width(res->font, entry->text);
    924898
    925899        switch (entry->halign) {
    926900        case gfx_halign_left:
    927901        case gfx_halign_justify:
    928                 geom->anchor_x = geom->text_rect.p0.x;
     902                geom->text_pos.x = geom->interior_rect.p0.x + hpad;
    929903                break;
    930904        case gfx_halign_center:
    931                 geom->anchor_x = (geom->text_rect.p0.x +
    932                     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;
    933907                break;
    934908        case gfx_halign_right:
    935                 geom->anchor_x = geom->text_rect.p1.x;
    936                 break;
    937         }
     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;
    938914}
    939915
     
    969945        if (!shift)
    970946                entry->sel_start = entry->pos;
    971 
    972         ui_entry_scroll_update(entry, false);
    973947        (void) ui_entry_paint(entry);
    974948}
     
    985959        if (!shift)
    986960                entry->sel_start = entry->pos;
    987 
    988         ui_entry_scroll_update(entry, false);
    989961        (void) ui_entry_paint(entry);
    990962}
     
    1006978        if (!shift)
    1007979                entry->sel_start = entry->pos;
    1008 
    1009         ui_entry_scroll_update(entry, false);
    1010980        (void) ui_entry_paint(entry);
    1011981}
     
    1027997        if (!shift)
    1028998                entry->sel_start = entry->pos;
    1029 
    1030         ui_entry_scroll_update(entry, false);
    1031999        (void) ui_entry_paint(entry);
    10321000}
     
    10531021}
    10541022
    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  */
    1063 void 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 
    11251023/** @}
    11261024 */
Note: See TracChangeset for help on using the changeset viewer.