Changeset 0902edfe in mainline for uspace/app/edit/edit.c


Ignore:
Timestamp:
2009-12-04T20:53:50Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0b772f5
Parents:
0f24c57
Message:

Editor copy and paste. Clipboard API with task-local implementation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/edit/edit.c

    r0f24c57 r0902edfe  
    4545#include <align.h>
    4646#include <macros.h>
     47#include <clipboard.h>
    4748#include <bool.h>
    4849
     
    119120    spt_t const *epos);
    120121static char *filename_prompt(char const *prompt, char const *init_value);
     122static char *range_get_str(spt_t const *spos, spt_t const *epos);
    121123
    122124static void pane_text_display(void);
     
    133135
    134136static bool selection_active(void);
     137static void selection_get_points(spt_t *pa, spt_t *pb);
    135138static void selection_delete(void);
     139static void selection_copy(void);
     140static void insert_clipboard_data(void);
    136141
    137142static void pt_get_sof(spt_t *pt);
     
    321326        case KC_E:
    322327                file_save_as();
     328                break;
     329        case KC_C:
     330                selection_copy();
     331                break;
     332        case KC_V:
     333                selection_delete();
     334                insert_clipboard_data();
     335                pane.rflags |= REDRAW_TEXT;
     336                caret_update();
    323337                break;
    324338        default:
     
    579593}
    580594
     595/** Return contents of range as a new string. */
     596static char *range_get_str(spt_t const *spos, spt_t const *epos)
     597{
     598        char *buf;
     599        spt_t sp, bep;
     600        size_t bytes;
     601        size_t buf_size, bpos;
     602
     603        buf_size = 1;
     604
     605        buf = malloc(buf_size);
     606        if (buf == NULL)
     607                return NULL;
     608
     609        bpos = 0;
     610        sp = *spos;
     611
     612        while (true) {
     613                sheet_copy_out(&doc.sh, &sp, epos, &buf[bpos], buf_size - bpos,
     614                    &bep);
     615                bytes = str_size(&buf[bpos]);
     616                bpos += bytes;
     617                sp = bep;
     618
     619                if (spt_equal(&bep, epos))
     620                        break;
     621
     622                buf_size *= 2;
     623                buf = realloc(buf, buf_size);
     624                if (buf == NULL)
     625                        return NULL;
     626        }
     627
     628        return buf;
     629}
     630
    581631static void pane_text_display(void)
    582632{
     
    913963}
    914964
     965static void selection_get_points(spt_t *pa, spt_t *pb)
     966{
     967        spt_t pt;
     968
     969        tag_get_pt(&pane.sel_start, pa);
     970        tag_get_pt(&pane.caret_pos, pb);
     971
     972        if (spt_cmp(pa, pb) > 0) {
     973                pt = *pa;
     974                *pa = *pb;
     975                *pb = pt;
     976        }
     977}
     978
    915979/** Delete selected text. */
    916980static void selection_delete(void)
     
    9401004}
    9411005
     1006static void selection_copy(void)
     1007{
     1008        spt_t pa, pb;
     1009        char *str;
     1010
     1011        selection_get_points(&pa, &pb);
     1012        str = range_get_str(&pa, &pb);
     1013        if (str == NULL || clipboard_put_str(str) != EOK) {
     1014                status_display("Copying to clipboard failed!");
     1015        }
     1016        free(str);
     1017}
     1018
     1019static void insert_clipboard_data(void)
     1020{
     1021        char *str;
     1022        size_t off;
     1023        wchar_t c;
     1024        int rc;
     1025
     1026        rc = clipboard_get_str(&str);
     1027        if (rc != EOK || str == NULL)
     1028                return;
     1029
     1030        off = 0;
     1031
     1032        while (true) {
     1033                c = str_decode(str, &off, STR_NO_LIMIT);
     1034                if (c == '\0')
     1035                        break;
     1036
     1037                insert_char(c);
     1038        }
     1039
     1040        free(str);
     1041}
     1042
    9421043/** Get start-of-file s-point. */
    9431044static void pt_get_sof(spt_t *pt)
Note: See TracChangeset for help on using the changeset viewer.