diff options
author | Thomas White <taw@bitwiz.org.uk> | 2011-05-24 08:24:36 +0200 |
---|---|---|
committer | Thomas White <taw@bitwiz.org.uk> | 2011-05-24 08:24:36 +0200 |
commit | 7e9854982c560a11aa7fa0eee6aa2a9f9673a9d6 (patch) | |
tree | e84c910d21f4155a3e9e5190a05fea1d772beae3 /src/objects.c | |
parent | 13248acdefcaa88d88cb9ef493ab3e9602abc658 (diff) |
Text insertion machinery
Diffstat (limited to 'src/objects.c')
-rw-r--r-- | src/objects.c | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/src/objects.c b/src/objects.c index 576eeb8..189241c 100644 --- a/src/objects.c +++ b/src/objects.c @@ -27,6 +27,7 @@ #include <stdlib.h> #include <string.h> +#include <assert.h> #include "presentation.h" #include "objects.h" @@ -66,7 +67,10 @@ struct object *add_text_object(struct slide *s, double x, double y) new->x = x; new->y = y; new->bb_width = 10.0; new->bb_height = 40.0; - new->text = "Hello"; + new->text = malloc(1); + new->text[0] = '\0'; + new->text_len = 1; + new->insertion_point = 0; s->object_seq++; @@ -74,6 +78,50 @@ struct object *add_text_object(struct slide *s, double x, double y) } +void insert_text(struct object *o, char *t) +{ + char *tmp; + size_t tlen, olen; + int i; + + assert(o->type == TEXT); + tlen = strlen(t); + olen = strlen(o->text); + + if ( tlen + olen + 1 > o->text_len ) { + + char *try; + + try = realloc(o->text, o->text_len + tlen + 64); + if ( try == NULL ) return; /* Failed to insert */ + o->text = try; + o->text_len += 64; + o->text_len += tlen; + + } + + tmp = malloc(o->text_len); + if ( tmp == NULL ) return; + + for ( i=0; i<o->insertion_point; i++ ) { + tmp[i] = o->text[i]; + } + for ( i=0; i<tlen; i++ ) { + tmp[i+o->insertion_point] = t[i]; + } + for ( i=0; i<olen-o->insertion_point; i++ ) { + tmp[i+o->insertion_point+tlen] = o->text[i+o->insertion_point]; + } + tmp[olen+tlen] = '\0'; + memcpy(o->text, tmp, o->text_len); + free(tmp); + + o->insertion_point += tlen; + o->parent->object_seq++; + o->empty = 0; +} + + void delete_object(struct object *o) { remove_object_from_slide(o->parent, o); |