Changeset c9722c1 in mainline


Ignore:
Timestamp:
2021-07-20T00:18:59Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
282c86d
Parents:
9eb8d12
Message:

Cut, copy and paste entry text

Using Ctrl-X, Ctrl-C and Ctrl-V.

Location:
uspace/lib/ui
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ui/include/ui/entry.h

    r9eb8d12 rc9722c1  
    5555extern void ui_entry_backspace(ui_entry_t *);
    5656extern void ui_entry_delete(ui_entry_t *);
     57extern void ui_entry_copy(ui_entry_t *);
     58extern void ui_entry_cut(ui_entry_t *);
     59extern void ui_entry_paste(ui_entry_t *);
    5760extern void ui_entry_seek_start(ui_entry_t *, bool);
    5861extern void ui_entry_seek_end(ui_entry_t *, bool);
  • uspace/lib/ui/private/entry.h

    r9eb8d12 rc9722c1  
    8080
    8181extern errno_t ui_entry_insert_str(ui_entry_t *, const char *);
     82extern ui_evclaim_t ui_entry_key_press_ctrl(ui_entry_t *, kbd_event_t *);
    8283extern ui_evclaim_t ui_entry_key_press_shift(ui_entry_t *, kbd_event_t *);
    8384extern ui_evclaim_t ui_entry_key_press_unmod(ui_entry_t *, kbd_event_t *);
  • uspace/lib/ui/src/entry.c

    r9eb8d12 rc9722c1  
    3737 */
    3838
     39#include <clipboard.h>
    3940#include <errno.h>
    4041#include <gfx/context.h>
     
    553554}
    554555
     556/** Copy selected text to clipboard.
     557 *
     558 * @param entry Text entry
     559 */
     560void ui_entry_copy(ui_entry_t *entry)
     561{
     562        unsigned off1;
     563        unsigned off2;
     564        char c;
     565
     566        off1 = min(entry->pos, entry->sel_start);
     567        off2 = max(entry->pos, entry->sel_start);
     568
     569        c = entry->text[off2];
     570        entry->text[off2] = '\0';
     571
     572        (void) clipboard_put_str(entry->text + off1);
     573
     574        entry->text[off2] = c;
     575}
     576
     577/** Cut selected text to clipboard.
     578 *
     579 * @param entry Text entry
     580 */
     581void ui_entry_cut(ui_entry_t *entry)
     582{
     583        ui_entry_copy(entry);
     584        ui_entry_delete_sel(entry);
     585}
     586
     587/** Paste text from clipboard.
     588 *
     589 * @param entry Text entry
     590 */
     591void ui_entry_paste(ui_entry_t *entry)
     592{
     593        char *str;
     594        errno_t rc;
     595
     596        rc = clipboard_get_str(&str);
     597        if (rc != EOK)
     598                return;
     599
     600        ui_entry_insert_str(entry, str);
     601        free(str);
     602}
     603
    555604/** Handle text entry key press without modifiers.
    556605 *
     
    626675                break;
    627676
     677        default:
     678                break;
     679        }
     680
     681        return ui_claimed;
     682}
     683
     684/** Handle text entry key press with control modifier.
     685 *
     686 * @param entry Text entry
     687 * @param kbd_event Keyboard event
     688 * @return @c ui_claimed iff the event is claimed
     689 */
     690ui_evclaim_t ui_entry_key_press_ctrl(ui_entry_t *entry, kbd_event_t *event)
     691{
     692        assert(event->type == KEY_PRESS);
     693
     694        switch (event->key) {
     695        case KC_C:
     696                ui_entry_copy(entry);
     697                break;
     698        case KC_V:
     699                ui_entry_paste(entry);
     700                break;
     701        case KC_X:
     702                ui_entry_cut(entry);
     703                break;
    628704        default:
    629705                break;
     
    665741            (event->mods & (KM_CTRL | KM_ALT)) == 0)
    666742                return ui_entry_key_press_shift(entry, event);
     743
     744        if (event->type == KEY_PRESS &&
     745            (event->mods & KM_CTRL) != 0 &&
     746            (event->mods & (KM_ALT | KM_SHIFT)) == 0)
     747                return ui_entry_key_press_ctrl(entry, event);
    667748
    668749        return ui_claimed;
  • uspace/lib/ui/test/entry.c

    r9eb8d12 rc9722c1  
    2727 */
    2828
     29#include <clipboard.h>
    2930#include <gfx/context.h>
    3031#include <gfx/coord.h>
     
    487488}
    488489
     490/** ui_entry_copy() copies selected text to clipboard. */
     491PCUT_TEST(copy)
     492{
     493        errno_t rc;
     494        ui_t *ui = NULL;
     495        ui_window_t *window = NULL;
     496        ui_wnd_params_t params;
     497        ui_entry_t *entry;
     498        char *str;
     499
     500        rc = ui_create_disp(NULL, &ui);
     501        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     502
     503        ui_wnd_params_init(&params);
     504        params.caption = "Hello";
     505
     506        rc = ui_window_create(ui, &params, &window);
     507        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     508        PCUT_ASSERT_NOT_NULL(window);
     509
     510        rc = ui_entry_create(window, "ABCDEF", &entry);
     511        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     512
     513        ui_entry_activate(entry);
     514        ui_entry_seek_start(entry, false);
     515        ui_entry_seek_next_char(entry, false);
     516        ui_entry_seek_end(entry, true);
     517        ui_entry_seek_prev_char(entry, true);
     518
     519        // FIXME: This is not safe unless we could create a private
     520        // test clipboard
     521
     522        ui_entry_copy(entry);
     523        rc = clipboard_get_str(&str);
     524        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     525        PCUT_ASSERT_STR_EQUALS("BCDE", str);
     526        PCUT_ASSERT_STR_EQUALS("ABCDEF", entry->text);
     527        free(str);
     528
     529        ui_entry_destroy(entry);
     530        ui_window_destroy(window);
     531        ui_destroy(ui);
     532}
     533
     534/** ui_entry_cut() cuts selected text to clipboard. */
     535PCUT_TEST(cut)
     536{
     537        errno_t rc;
     538        ui_t *ui = NULL;
     539        ui_window_t *window = NULL;
     540        ui_wnd_params_t params;
     541        ui_entry_t *entry;
     542        char *str;
     543
     544        rc = ui_create_disp(NULL, &ui);
     545        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     546
     547        ui_wnd_params_init(&params);
     548        params.caption = "Hello";
     549
     550        rc = ui_window_create(ui, &params, &window);
     551        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     552        PCUT_ASSERT_NOT_NULL(window);
     553
     554        rc = ui_entry_create(window, "ABCDEF", &entry);
     555        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     556
     557        ui_entry_activate(entry);
     558        ui_entry_seek_start(entry, false);
     559        ui_entry_seek_next_char(entry, false);
     560        ui_entry_seek_end(entry, true);
     561        ui_entry_seek_prev_char(entry, true);
     562
     563        // FIXME: This is not safe unless we could create a private
     564        // test clipboard
     565
     566        ui_entry_cut(entry);
     567        rc = clipboard_get_str(&str);
     568        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     569        PCUT_ASSERT_STR_EQUALS("BCDE", str);
     570        PCUT_ASSERT_STR_EQUALS("AF", entry->text);
     571        free(str);
     572
     573        ui_entry_destroy(entry);
     574        ui_window_destroy(window);
     575        ui_destroy(ui);
     576}
     577
     578/** ui_entry_paste() pastes text from clipboard. */
     579PCUT_TEST(paste)
     580{
     581        errno_t rc;
     582        ui_t *ui = NULL;
     583        ui_window_t *window = NULL;
     584        ui_wnd_params_t params;
     585        ui_entry_t *entry;
     586
     587        rc = ui_create_disp(NULL, &ui);
     588        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     589
     590        ui_wnd_params_init(&params);
     591        params.caption = "Hello";
     592
     593        rc = ui_window_create(ui, &params, &window);
     594        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     595        PCUT_ASSERT_NOT_NULL(window);
     596
     597        rc = ui_entry_create(window, "AB", &entry);
     598        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     599
     600        ui_entry_activate(entry);
     601        ui_entry_seek_start(entry, false);
     602        ui_entry_seek_next_char(entry, false);
     603
     604        // FIXME: This is not safe unless we could create a private
     605        // test clipboard
     606
     607        rc = clipboard_put_str("123");
     608        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     609
     610        ui_entry_paste(entry);
     611        PCUT_ASSERT_STR_EQUALS("A123B", entry->text);
     612
     613        ui_entry_destroy(entry);
     614        ui_window_destroy(window);
     615        ui_destroy(ui);
     616}
     617
    489618/** ui_entry_seek_start() moves cursor to beginning of text */
    490619PCUT_TEST(seek_start)
     
    506635        PCUT_ASSERT_NOT_NULL(window);
    507636
    508         rc = ui_entry_create(window, "ABCD", &entry);
    509         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    510 
    511         PCUT_ASSERT_STR_EQUALS("ABCD", entry->text);
     637        rc = ui_entry_create(window, "ABCDEF", &entry);
     638        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     639
     640        ui_entry_activate(entry);
     641
    512642        entry->pos = 2;
    513643        entry->sel_start = 2;
Note: See TracChangeset for help on using the changeset viewer.