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 | |
parent | 13248acdefcaa88d88cb9ef493ab3e9602abc658 (diff) |
Text insertion machinery
Diffstat (limited to 'src')
-rw-r--r-- | src/mainwindow.c | 11 | ||||
-rw-r--r-- | src/objects.c | 50 | ||||
-rw-r--r-- | src/objects.h | 4 | ||||
-rw-r--r-- | src/slide_render.c | 4 |
4 files changed, 65 insertions, 4 deletions
diff --git a/src/mainwindow.c b/src/mainwindow.c index 904c69f..362bd92 100644 --- a/src/mainwindow.c +++ b/src/mainwindow.c @@ -142,6 +142,14 @@ static gint close_sig(GtkWidget *window, struct presentation *p) static gboolean im_commit_sig(GtkIMContext *im, gchar *str, struct presentation *p) { + if ( p->editing_object == NULL ) return FALSE; + if ( p->editing_object->type != TEXT ) return FALSE; + + insert_text(p->editing_object, str); + + /* FIXME: Invalidate only the necessary region */ + gdk_window_invalidate_rect(p->drawingarea->window, NULL, FALSE); + return FALSE; } @@ -156,9 +164,6 @@ static gboolean key_press_sig(GtkWidget *da, GdkEventKey *event, /* Throw the event to the IM context and let it sort things out */ gtk_im_context_filter_keypress(GTK_IM_CONTEXT(p->im_context), event); - /* FIXME: Invalidate only the necessary region */ - gdk_window_invalidate_rect(p->drawingarea->window, NULL, FALSE); - return FALSE; } 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); diff --git a/src/objects.h b/src/objects.h index c220b00..e6e457e 100644 --- a/src/objects.h +++ b/src/objects.h @@ -52,10 +52,14 @@ struct object /* For type TEXT */ char *text; + size_t text_len; + int insertion_point; }; extern struct object *add_text_object(struct slide *s, double x, double y); +extern void insert_text(struct object *o, char *t); + extern void delete_object(struct object *o); diff --git a/src/slide_render.c b/src/slide_render.c index 33eee36..ab656be 100644 --- a/src/slide_render.c +++ b/src/slide_render.c @@ -47,6 +47,10 @@ static void render_text_object(cairo_t *cr, struct object *o) pango_cairo_update_layout(cr, l); pango_layout_get_size(l, &width, &height); + + o->bb_width = width/PANGO_SCALE; + o->bb_height = height/PANGO_SCALE; + cairo_move_to(cr, o->x - (width/PANGO_SCALE)/2.0, o->y - (height/PANGO_SCALE)/2.0); |