From 44f874ed89b364bc0b60b35572f64fae44154733 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sun, 10 Mar 2019 22:23:21 +0100 Subject: Implement enter press / paragraph split --- libstorycode/gtk/gtknarrativeview.c | 30 +++++++++++++++++++----- libstorycode/narrative.c | 43 +++++++++++++++++++++++++++++++---- libstorycode/narrative.h | 1 + libstorycode/narrative_render_cairo.c | 2 ++ 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/libstorycode/gtk/gtknarrativeview.c b/libstorycode/gtk/gtknarrativeview.c index da3d049..ec34778 100644 --- a/libstorycode/gtk/gtknarrativeview.c +++ b/libstorycode/gtk/gtknarrativeview.c @@ -528,6 +528,11 @@ static void get_cursor_pos(Narrative *n, struct edit_pos cpos, return; } + if ( item->layout == NULL ) { + fprintf(stderr, "get_cursor_pos: No layout\n"); + return; + } + offs = pos_trail_to_offset(item, cpos.pos, cpos.trail); pango_layout_get_cursor_pos(item->layout, offs, &rect, NULL); *x = pango_units_to_double(rect.x) + n->space_l + item->space_l; @@ -680,6 +685,7 @@ static void cursor_moveh(Narrative *n, struct edit_pos *cp, signed int dir) cp->trail = 0; } } else { + if ( item->layout == NULL ) return; pango_layout_move_cursor_visually(item->layout, 1, cp->pos, cp->trail, dir, &np, &cp->trail); @@ -804,6 +810,21 @@ static void insert_text_in_paragraph(struct narrative_item *item, size_t offs, } +static void split_paragraph_at_cursor(Narrative *n, struct edit_pos pos) +{ + size_t off; + + if ( n->items[pos.para].type != NARRATIVE_ITEM_SLIDE ) { + off = pos_trail_to_offset(&n->items[pos.para], + pos.pos, pos.trail); + } else { + off = 0; + } + + narrative_split_item(n, pos.para, off); +} + + static void insert_text(char *t, GtkNarrativeView *e) { Narrative *n; @@ -816,7 +837,8 @@ static void insert_text(char *t, GtkNarrativeView *e) item = get_current_item(e, &n); if ( strcmp(t, "\n") == 0 ) { - //split_paragraph_at_cursor(e); FIXME + split_paragraph_at_cursor(n, e->cpos); + rewrap_range(e, e->cpos.para, e->cpos.para+1); update_size(e); cursor_moveh(n, &e->cpos, +1); check_cursor_visible(e); @@ -835,11 +857,7 @@ static void insert_text(char *t, GtkNarrativeView *e) update_size(e); cursor_moveh(n, &e->cpos, +1); - } else { - - /* FIXME: Add text after slide */ - - } + } /* else do nothing: pressing enter is OK, though */ emit_change_sig(e); check_cursor_visible(e); diff --git a/libstorycode/narrative.c b/libstorycode/narrative.c index caf9bc9..81b55f0 100644 --- a/libstorycode/narrative.c +++ b/libstorycode/narrative.c @@ -70,6 +70,15 @@ void narrative_free(Narrative *n) } +static void init_item(struct narrative_item *item) +{ + item->layout = NULL; + item->text = NULL; + item->slide = NULL; + item->slide_thumbnail = NULL; +} + + static struct narrative_item *add_item(Narrative *n) { struct narrative_item *new_items; @@ -78,14 +87,21 @@ static struct narrative_item *add_item(Narrative *n) if ( new_items == NULL ) return NULL; n->items = new_items; item = &n->items[n->n_items++]; - item->layout = NULL; - item->text = NULL; - item->slide = NULL; - item->slide_thumbnail = NULL; + init_item(item); return item; } +static struct narrative_item *insert_item(Narrative *n, int pos) +{ + add_item(n); + memmove(&n->items[pos+1], &n->items[pos], + (n->n_items-pos-1)*sizeof(struct narrative_item)); + init_item(&n->items[pos+1]); + return &n->items[pos+1]; +} + + void narrative_add_prestitle(Narrative *n, char *text) { struct narrative_item *item; @@ -213,3 +229,22 @@ void narrative_delete_block(Narrative *n, int i1, size_t o1, int i2, size_t o2) delete_item(n, i2); } } + + +void narrative_split_item(Narrative *n, int i1, size_t o1) +{ + struct narrative_item *item1; + struct narrative_item *item2; + + item1 = &n->items[i1]; + item2 = insert_item(n, i1); + + if ( item1->type != NARRATIVE_ITEM_SLIDE ) { + item2->text = strdup(&item1->text[o1]); + item1->text[o1] = '\0'; + } else { + item2->text = strdup(""); + } + + item2->type = NARRATIVE_ITEM_TEXT; +} diff --git a/libstorycode/narrative.h b/libstorycode/narrative.h index 412bc6b..5b8af4a 100644 --- a/libstorycode/narrative.h +++ b/libstorycode/narrative.h @@ -40,6 +40,7 @@ extern void narrative_add_text(Narrative *n, char *text); extern void narrative_add_slide(Narrative *n, Slide *slide); extern void narrative_delete_block(Narrative *n, int i1, size_t o1, int i2, size_t o2); +extern void narrative_split_item(Narrative *n, int i1, size_t o1); #endif /* NARRATIVE_H */ diff --git a/libstorycode/narrative_render_cairo.c b/libstorycode/narrative_render_cairo.c index 9852db8..b4f29cb 100644 --- a/libstorycode/narrative_render_cairo.c +++ b/libstorycode/narrative_render_cairo.c @@ -369,6 +369,8 @@ static void draw_slide(struct narrative_item *item, cairo_t *cr) static void draw_text(struct narrative_item *item, cairo_t *cr) { + if ( item->layout == NULL ) return; + cairo_save(cr); cairo_translate(cr, item->space_l, item->space_t); -- cgit v1.2.3