From b017dba2f888d7f98b1ca85b62227d78a5ce742a Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 21 Sep 2016 13:48:56 +0200 Subject: Keep the cursor inside the window on edit --- src/sc_editor.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/sc_editor.c b/src/sc_editor.c index 2627b5a..f00ad28 100644 --- a/src/sc_editor.c +++ b/src/sc_editor.c @@ -598,6 +598,26 @@ SCBlock *split_paragraph_at_cursor(SCEditor *e) } +static void check_cursor_visible(SCEditor *e) +{ + double x, y, h; + + get_cursor_pos(e->cursor_frame, e->cursor_para, + e->cursor_pos+e->cursor_trail, &x, &y, &h); + + /* Off the bottom? */ + if ( y - e->scroll_pos + h > e->visible_height ) { + e->scroll_pos = y + h - e->visible_height; + e->scroll_pos += e->cursor_frame->pad_b; + } + + /* Off the top? */ + if ( y < e->scroll_pos ) { + e->scroll_pos = y - e->cursor_frame->pad_t; + } +} + + static void insert_text(char *t, SCEditor *e) { Paragraph *para; @@ -627,8 +647,6 @@ static void insert_text(char *t, SCEditor *e) cursor_moveh(e->cursor_frame, &e->cursor_para, &e->cursor_pos, &e->cursor_trail, +1); - sc_editor_redraw(e); - } else { SCBlock *ad; @@ -653,9 +671,10 @@ static void insert_text(char *t, SCEditor *e) /* FIXME: Find the cursor again */ - sc_editor_redraw(e); } + check_cursor_visible(e); + sc_editor_redraw(e); } -- cgit v1.2.3