From fcf60a1d85095955b634614fed57e90949032696 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Fri, 3 May 2019 23:07:03 +0200 Subject: Improve behaviour of paragraph splitting (when inserting a slide) --- libstorycode/gtk/gtknarrativeview.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/libstorycode/gtk/gtknarrativeview.c b/libstorycode/gtk/gtknarrativeview.c index 48ef91f..9db5629 100644 --- a/libstorycode/gtk/gtknarrativeview.c +++ b/libstorycode/gtk/gtknarrativeview.c @@ -815,18 +815,24 @@ 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) +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); + 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); + if ( (off > 0) && (off < strlen(n->items[pos->para].text)) ) { + narrative_split_item(n, pos->para, off); + } else if ( off == 0 ) { + pos->para--; + pos->pos = 0; + pos->trail = 0; + } } @@ -841,7 +847,7 @@ static void insert_text(char *t, GtkNarrativeView *e) item = &e->n->items[e->cpos.para]; if ( strcmp(t, "\n") == 0 ) { - split_paragraph_at_cursor(e->n, e->cpos); + split_paragraph_at_cursor(e->n, &e->cpos); rewrap_range(e, e->cpos.para, e->cpos.para+1); update_size(e); cursor_moveh(e->n, &e->cpos, +1); @@ -1216,7 +1222,7 @@ void gtk_narrative_view_add_slide_at_cursor(GtkNarrativeView *e) s = slide_new(); if ( s == NULL ) return; - split_paragraph_at_cursor(e->n, e->cpos); + split_paragraph_at_cursor(e->n, &e->cpos); narrative_insert_slide(e->n, s, e->cpos.para+1); rewrap_range(e, e->cpos.para, e->cpos.para+2); -- cgit v1.2.3