diff options
author | Thomas White <taw@bitwiz.org.uk> | 2011-05-26 23:01:13 +0200 |
---|---|---|
committer | Thomas White <taw@bitwiz.org.uk> | 2011-05-26 23:01:13 +0200 |
commit | 91c2dcb3509f13c2f8aed925ebcec2381af692da (patch) | |
tree | 16a0355fcb3c43db8f9d5f9fa44af031a2d1f508 /src/objects.c | |
parent | 8bcb4aa14d5e02b8c7ccab0ceb03f8876bb61e5b (diff) |
Add caret and basic keyboard editing
Diffstat (limited to 'src/objects.c')
-rw-r--r-- | src/objects.c | 75 |
1 files changed, 61 insertions, 14 deletions
diff --git a/src/objects.c b/src/objects.c index 372fac1..a66c3f5 100644 --- a/src/objects.c +++ b/src/objects.c @@ -37,19 +37,24 @@ static struct object *new_object(enum objtype t) { struct object *new; - new = malloc(sizeof(struct object)); + new = calloc(1, sizeof(struct object)); if ( new == NULL ) return NULL; new->type = t; new->empty = 1; new->parent = NULL; + new->layout = NULL; + new->fontdesc = NULL; + return new; } static void free_object(struct object *o) { + if ( o->layout != NULL ) g_object_unref(o->layout); + if ( o->fontdesc != NULL ) pango_font_description_free(o->fontdesc); free(o); } @@ -122,30 +127,60 @@ void insert_text(struct object *o, char *t) } -void handle_text_backspace(struct object *o) +static int find_prev_index(const char *t, int p) { - size_t ndel; - int i; + int i, nback; - assert(o->type == TEXT); + if ( p == 0 ) return 0; - if ( o->insertion_point == 0 ) return; /* Nothing to delete */ + if ( !(t[p-1] & 0x80) ) { + nback = 1; + } else { + nback = 0; + for ( i=1; i<=6; i++ ) { + if ( p-i == 0 ) return 0; + if ( !(t[p-i] & 0xC0) ) nback++; + } + } - if ( !(o->text[o->insertion_point-1] & 0x80) ) { - /* Simple (ASCII-style) backspace */ - ndel = 1; + return p - nback; +} + + +static int find_next_index(const char *t, int p) +{ + int i, nfor; + + if ( t[p] == '\0' ) return p; + + if ( !(t[p+1] & 0x80) ) { + nfor = 1; } else { - ndel = 0; + nfor = 0; for ( i=1; i<=6; i++ ) { - if ( !(o->text[o->insertion_point-i] & 0xC0) ) ndel++; + if ( t[p+i] == '\0' ) return p+i; + if ( !(t[p+i] & 0xC0) ) nfor++; } } - memmove(o->text+o->insertion_point-ndel, - o->text+o->insertion_point, + return p + nfor; +} + + +void handle_text_backspace(struct object *o) +{ + int prev_index; + + assert(o->type == TEXT); + + if ( o->insertion_point == 0 ) return; /* Nothing to delete */ + + prev_index = find_prev_index(o->text, o->insertion_point); + + memmove(o->text+prev_index, o->text+o->insertion_point, o->text_len-o->insertion_point); - o->insertion_point -= ndel; + o->insertion_point = prev_index; if ( strlen(o->text) == 0 ) o->empty = 1; @@ -153,6 +188,18 @@ void handle_text_backspace(struct object *o) } +void move_cursor_left(struct object *o) +{ + o->insertion_point = find_prev_index(o->text, o->insertion_point); +} + + +void move_cursor_right(struct object *o) +{ + o->insertion_point = find_next_index(o->text, o->insertion_point); +} + + void delete_object(struct object *o) { remove_object_from_slide(o->parent, o); |