Changeset c9722c1 in mainline for uspace/lib/ui/src/entry.c


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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;
Note: See TracChangeset for help on using the changeset viewer.