Changeset db3895d in mainline for uspace/lib/ui


Ignore:
Timestamp:
2021-06-10T17:10:11Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
af5d62eb
Parents:
90f1f19
Message:

Set cursor shape to I-beam when hovering over text entry

Location:
uspace/lib/ui
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ui/include/types/ui/cursor.h

    r90f1f19 rdb3895d  
    11/*
    2  * Copyright (c) 2020 Jiri Svoboda
     2 * Copyright (c) 2021 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4747        ui_curs_size_uldr,
    4848        /** Double arrow pointing up-right nad down-left */
    49         ui_curs_size_urdl
     49        ui_curs_size_urdl,
     50        /** I-beam (suggests editable text) */
     51        ui_curs_ibeam
    5052} ui_stock_cursor_t;
    5153
  • uspace/lib/ui/include/ui/entry.h

    r90f1f19 rdb3895d  
    11/*
    2  * Copyright (c) 2020 Jiri Svoboda
     2 * Copyright (c) 2021 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4242#include <types/ui/control.h>
    4343#include <types/ui/entry.h>
    44 #include <types/ui/resource.h>
     44#include <types/ui/window.h>
    4545
    46 extern errno_t ui_entry_create(ui_resource_t *, const char *,
     46extern errno_t ui_entry_create(ui_window_t *, const char *,
    4747    ui_entry_t **);
    4848extern void ui_entry_destroy(ui_entry_t *);
  • uspace/lib/ui/include/ui/window.h

    r90f1f19 rdb3895d  
    6060extern errno_t ui_window_get_app_gc(ui_window_t *, gfx_context_t **);
    6161extern void ui_window_get_app_rect(ui_window_t *, gfx_rect_t *);
     62extern void ui_window_set_ctl_cursor(ui_window_t *, ui_stock_cursor_t);
    6263extern errno_t ui_window_paint(ui_window_t *);
    6364extern errno_t ui_window_def_paint(ui_window_t *);
  • uspace/lib/ui/private/entry.h

    r90f1f19 rdb3895d  
    11/*
    2  * Copyright (c) 2020 Jiri Svoboda
     2 * Copyright (c) 2021 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4848        /** Base control object */
    4949        struct ui_control *control;
    50         /** UI resource */
    51         struct ui_resource *res;
     50        /** UI window */
     51        struct ui_window *window;
    5252        /** Entry rectangle */
    5353        gfx_rect_t rect;
     
    5656        /** Text */
    5757        char *text;
     58        /** Pointer is currently inside */
     59        bool pointer_inside;
    5860};
    5961
  • uspace/lib/ui/private/window.h

    r90f1f19 rdb3895d  
    9292};
    9393
     94extern display_stock_cursor_t wnd_dcursor_from_cursor(ui_stock_cursor_t);
    9495extern void ui_window_send_close(ui_window_t *);
    9596extern void ui_window_send_focus(ui_window_t *);
  • uspace/lib/ui/src/entry.c

    r90f1f19 rdb3895d  
    4747#include <ui/entry.h>
    4848#include <ui/ui.h>
     49#include <ui/window.h>
    4950#include "../private/entry.h"
    5051#include "../private/resource.h"
     
    7071/** Create new text entry.
    7172 *
    72  * @param resource UI resource
     73 * @param window UI window
    7374 * @param text Text
    7475 * @param rentry Place to store pointer to new text entry
    7576 * @return EOK on success, ENOMEM if out of memory
    7677 */
    77 errno_t ui_entry_create(ui_resource_t *resource, const char *text,
     78errno_t ui_entry_create(ui_window_t *window, const char *text,
    7879    ui_entry_t **rentry)
    7980{
     
    9899        }
    99100
    100         entry->res = resource;
     101        entry->window = window;
    101102        entry->halign = gfx_halign_left;
    102103        *rentry = entry;
     
    174175errno_t ui_entry_paint(ui_entry_t *entry)
    175176{
     177        ui_resource_t *res;
    176178        gfx_text_fmt_t fmt;
    177179        gfx_coord2_t pos;
     
    181183        errno_t rc;
    182184
    183         if (entry->res->textmode) {
     185        res = ui_window_get_res(entry->window);
     186
     187        if (res->textmode) {
    184188                hpad = ui_entry_hpad_text;
    185189                vpad = ui_entry_vpad_text;
     
    189193        }
    190194
    191         if (entry->res->textmode == false) {
     195        if (res->textmode == false) {
    192196                /* Paint inset frame */
    193                 rc = ui_paint_inset_frame(entry->res, &entry->rect, &inside);
     197                rc = ui_paint_inset_frame(res, &entry->rect, &inside);
    194198                if (rc != EOK)
    195199                        goto error;
     
    200204        /* Paint entry background */
    201205
    202         rc = gfx_set_color(entry->res->gc, entry->res->entry_bg_color);
     206        rc = gfx_set_color(res->gc, res->entry_bg_color);
    203207        if (rc != EOK)
    204208                goto error;
    205209
    206         rc = gfx_fill_rect(entry->res->gc, &inside);
     210        rc = gfx_fill_rect(res->gc, &inside);
    207211        if (rc != EOK)
    208212                goto error;
     
    224228
    225229        gfx_text_fmt_init(&fmt);
    226         fmt.color = entry->res->entry_fg_color;
     230        fmt.color = res->entry_fg_color;
    227231        fmt.halign = entry->halign;
    228232        fmt.valign = gfx_valign_top;
    229233
    230         rc = gfx_puttext(entry->res->font, &pos, &fmt, entry->text);
     234        rc = gfx_puttext(res->font, &pos, &fmt, entry->text);
    231235        if (rc != EOK)
    232236                goto error;
    233237
    234         rc = gfx_update(entry->res->gc);
     238        rc = gfx_update(res->gc);
    235239        if (rc != EOK)
    236240                goto error;
     
    273277{
    274278        ui_entry_t *entry = (ui_entry_t *) arg;
    275 
    276         (void) entry;
     279        gfx_coord2_t pos;
     280
     281        if (event->type == POS_UPDATE) {
     282                pos.x = event->hpos;
     283                pos.y = event->vpos;
     284
     285                if (gfx_pix_inside_rect(&pos, &entry->rect)) {
     286                        if (!entry->pointer_inside) {
     287                                ui_window_set_ctl_cursor(entry->window,
     288                                    ui_curs_ibeam);
     289                                entry->pointer_inside = true;
     290                        }
     291                } else {
     292                        if (entry->pointer_inside) {
     293                                ui_window_set_ctl_cursor(entry->window,
     294                                    ui_curs_arrow);
     295                                entry->pointer_inside = false;
     296                        }
     297                }
     298        }
     299
    277300        return ui_unclaimed;
    278301}
  • uspace/lib/ui/src/window.c

    r90f1f19 rdb3895d  
    646646}
    647647
     648/** Set cursor when pointer is hovering over a control.
     649 *
     650 * @param window Window
     651 * @param cursor Cursor
     652 */
     653void ui_window_set_ctl_cursor(ui_window_t *window, ui_stock_cursor_t cursor)
     654{
     655        display_stock_cursor_t dcursor;
     656
     657        dcursor = wnd_dcursor_from_cursor(cursor);
     658
     659        if (window->dwindow != NULL)
     660                (void) display_window_set_cursor(window->dwindow, dcursor);
     661}
     662
    648663/** Paint window
    649664 *
     
    770785}
    771786
    772 /** Window decoration requested changing cursor.
    773  *
    774  * @param wdecor Window decoration
    775  * @param arg Argument (window)
    776  * @param cursor Cursor to set
    777  */
    778 static void wd_set_cursor(ui_wdecor_t *wdecor, void *arg,
    779     ui_stock_cursor_t cursor)
    780 {
    781         ui_window_t *window = (ui_window_t *) arg;
     787/** Get display stock cursor from UI stock cursor.
     788 *
     789 * @param cursor UI stock cursor
     790 * @return Display stock cursor
     791 */
     792display_stock_cursor_t wnd_dcursor_from_cursor(ui_stock_cursor_t cursor)
     793{
    782794        display_stock_cursor_t dcursor;
    783 
    784         if (cursor == window->cursor)
    785                 return;
    786795
    787796        dcursor = dcurs_arrow;
     
    803812                dcursor = dcurs_size_urdl;
    804813                break;
    805         }
     814        case ui_curs_ibeam:
     815                dcursor = dcurs_ibeam;
     816                break;
     817        }
     818
     819        return dcursor;
     820}
     821
     822/** Window decoration requested changing cursor.
     823 *
     824 * @param wdecor Window decoration
     825 * @param arg Argument (window)
     826 * @param cursor Cursor to set
     827 */
     828static void wd_set_cursor(ui_wdecor_t *wdecor, void *arg,
     829    ui_stock_cursor_t cursor)
     830{
     831        ui_window_t *window = (ui_window_t *) arg;
     832        display_stock_cursor_t dcursor;
     833
     834        if (cursor == window->cursor)
     835                return;
     836
     837        dcursor = wnd_dcursor_from_cursor(cursor);
    806838
    807839        if (window->dwindow != NULL)
    808840                (void) display_window_set_cursor(window->dwindow, dcursor);
     841
    809842        window->cursor = cursor;
    810843}
  • uspace/lib/ui/test/entry.c

    r90f1f19 rdb3895d  
    3535#include <ui/entry.h>
    3636#include <ui/resource.h>
     37#include <ui/ui.h>
     38#include <ui/window.h>
    3739#include "../private/entry.h"
    3840
     
    4042
    4143PCUT_TEST_SUITE(entry);
    42 
    43 static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
    44 static errno_t testgc_set_color(void *, gfx_color_t *);
    45 static errno_t testgc_fill_rect(void *, gfx_rect_t *);
    46 static errno_t testgc_update(void *);
    47 static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
    48     gfx_bitmap_alloc_t *, void **);
    49 static errno_t testgc_bitmap_destroy(void *);
    50 static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
    51 static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
    52 
    53 static gfx_context_ops_t ops = {
    54         .set_clip_rect = testgc_set_clip_rect,
    55         .set_color = testgc_set_color,
    56         .fill_rect = testgc_fill_rect,
    57         .update = testgc_update,
    58         .bitmap_create = testgc_bitmap_create,
    59         .bitmap_destroy = testgc_bitmap_destroy,
    60         .bitmap_render = testgc_bitmap_render,
    61         .bitmap_get_alloc = testgc_bitmap_get_alloc
    62 };
    63 
    64 typedef struct {
    65         bool bm_created;
    66         bool bm_destroyed;
    67         gfx_bitmap_params_t bm_params;
    68         void *bm_pixels;
    69         gfx_rect_t bm_srect;
    70         gfx_coord2_t bm_offs;
    71         bool bm_rendered;
    72         bool bm_got_alloc;
    73 } test_gc_t;
    74 
    75 typedef struct {
    76         test_gc_t *tgc;
    77         gfx_bitmap_alloc_t alloc;
    78         bool myalloc;
    79 } testgc_bitmap_t;
    80 
    81 typedef struct {
    82         bool clicked;
    83 } test_cb_resp_t;
    8444
    8545/** Create and destroy text entry */
     
    187147{
    188148        errno_t rc;
    189         gfx_context_t *gc = NULL;
    190         test_gc_t tgc;
    191         ui_resource_t *resource = NULL;
     149        ui_t *ui = NULL;
     150        ui_window_t *window = NULL;
     151        ui_wnd_params_t params;
    192152        ui_entry_t *entry;
    193153
    194         memset(&tgc, 0, sizeof(tgc));
    195         rc = gfx_context_new(&ops, &tgc, &gc);
     154        rc = ui_create_disp(NULL, &ui);
    196155        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    197156
    198         rc = ui_resource_create(gc, false, &resource);
     157        ui_wnd_params_init(&params);
     158        params.caption = "Hello";
     159
     160        rc = ui_window_create(ui, &params, &window);
    199161        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    200         PCUT_ASSERT_NOT_NULL(resource);
     162        PCUT_ASSERT_NOT_NULL(window);
    201163
    202         rc = ui_entry_create(resource, "Hello", &entry);
     164        rc = ui_entry_create(window, "Hello", &entry);
    203165        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    204166
     
    207169
    208170        ui_entry_destroy(entry);
    209         ui_resource_destroy(resource);
    210 
    211         rc = gfx_context_delete(gc);
    212         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    213 }
    214 
    215 static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
    216 {
    217         (void) arg;
    218         (void) rect;
    219         return EOK;
    220 }
    221 
    222 static errno_t testgc_set_color(void *arg, gfx_color_t *color)
    223 {
    224         (void) arg;
    225         (void) color;
    226         return EOK;
    227 }
    228 
    229 static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
    230 {
    231         (void) arg;
    232         (void) rect;
    233         return EOK;
    234 }
    235 
    236 static errno_t testgc_update(void *arg)
    237 {
    238         (void) arg;
    239         return EOK;
    240 }
    241 
    242 static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
    243     gfx_bitmap_alloc_t *alloc, void **rbm)
    244 {
    245         test_gc_t *tgc = (test_gc_t *) arg;
    246         testgc_bitmap_t *tbm;
    247 
    248         tbm = calloc(1, sizeof(testgc_bitmap_t));
    249         if (tbm == NULL)
    250                 return ENOMEM;
    251 
    252         if (alloc == NULL) {
    253                 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
    254                     sizeof(uint32_t);
    255                 tbm->alloc.off0 = 0;
    256                 tbm->alloc.pixels = calloc(sizeof(uint32_t),
    257                     (params->rect.p1.x - params->rect.p0.x) *
    258                     (params->rect.p1.y - params->rect.p0.y));
    259                 tbm->myalloc = true;
    260                 if (tbm->alloc.pixels == NULL) {
    261                         free(tbm);
    262                         return ENOMEM;
    263                 }
    264         } else {
    265                 tbm->alloc = *alloc;
    266         }
    267 
    268         tbm->tgc = tgc;
    269         tgc->bm_created = true;
    270         tgc->bm_params = *params;
    271         tgc->bm_pixels = tbm->alloc.pixels;
    272         *rbm = (void *)tbm;
    273         return EOK;
    274 }
    275 
    276 static errno_t testgc_bitmap_destroy(void *bm)
    277 {
    278         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    279         if (tbm->myalloc)
    280                 free(tbm->alloc.pixels);
    281         tbm->tgc->bm_destroyed = true;
    282         free(tbm);
    283         return EOK;
    284 }
    285 
    286 static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
    287     gfx_coord2_t *offs)
    288 {
    289         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    290         tbm->tgc->bm_rendered = true;
    291         tbm->tgc->bm_srect = *srect;
    292         tbm->tgc->bm_offs = *offs;
    293         return EOK;
    294 }
    295 
    296 static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
    297 {
    298         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    299         *alloc = tbm->alloc;
    300         tbm->tgc->bm_got_alloc = true;
    301         return EOK;
     171        ui_window_destroy(window);
     172        ui_destroy(ui);
    302173}
    303174
  • uspace/lib/ui/test/window.c

    r90f1f19 rdb3895d  
    291291}
    292292
     293/** ui_window_set_ctl_cursor() */
     294PCUT_TEST(set_ctl_cursor)
     295{
     296        errno_t rc;
     297        ui_t *ui = NULL;
     298        ui_wnd_params_t params;
     299        ui_window_t *window = NULL;
     300
     301        rc = ui_create_disp(NULL, &ui);
     302        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     303
     304        ui_wnd_params_init(&params);
     305        params.caption = "Hello";
     306
     307        rc = ui_window_create(ui, &params, &window);
     308        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     309        PCUT_ASSERT_NOT_NULL(window);
     310
     311        ui_window_set_ctl_cursor(window, ui_curs_ibeam);
     312
     313        ui_window_destroy(window);
     314        ui_destroy(ui);
     315}
     316
    293317/** ui_window_get_app_gc() return valid GC */
    294318PCUT_TEST(get_app_gc)
Note: See TracChangeset for help on using the changeset viewer.