diff options
-rw-r--r-- | desktop/textarea.c | 55 | ||||
-rw-r--r-- | desktop/textarea.h | 8 |
2 files changed, 30 insertions, 33 deletions
diff --git a/desktop/textarea.c b/desktop/textarea.c index b73ebbfc1..b16d78381 100644 --- a/desktop/textarea.c +++ b/desktop/textarea.c @@ -169,6 +169,36 @@ static void textarea_normalise_text(struct textarea *ta, /** + * Get the caret's position + * + * \param ta Text area + * \return 0-based character index of caret location, or -1 on error + */ +static int textarea_get_caret(struct textarea *ta) +{ + unsigned int c_off = 0, b_off; + + /* Ensure caret isn't hidden */ + if (ta->caret_pos.char_off < 0) + textarea_set_caret(ta, 0); + + /* if the text is a trailing NUL only */ + if (ta->text.utf8_len == 0) + return 0; + + if (ta->caret_pos.line >= ta->line_count) + return ta->text.utf8_len; + + /* Calculate character offset of this line's start */ + for (b_off = 0; b_off < ta->lines[ta->caret_pos.line].b_start; + b_off = utf8_next(ta->text.data, ta->text.len, b_off)) + c_off++; + + return c_off + ta->caret_pos.char_off; +} + + +/** * Selects a character range in the textarea and redraws it * * \param ta Text area @@ -1501,31 +1531,6 @@ bool textarea_set_caret(struct textarea *ta, int caret) /* exported interface, documented in textarea.h */ -int textarea_get_caret(struct textarea *ta) -{ - unsigned int c_off = 0, b_off; - - /* Ensure caret isn't hidden */ - if (ta->caret_pos.char_off < 0) - textarea_set_caret(ta, 0); - - /* if the text is a trailing NUL only */ - if (ta->text.utf8_len == 0) - return 0; - - if (ta->caret_pos.line >= ta->line_count) - return ta->text.utf8_len; - - /* Calculate character offset of this line's start */ - for (b_off = 0; b_off < ta->lines[ta->caret_pos.line].b_start; - b_off = utf8_next(ta->text.data, ta->text.len, b_off)) - c_off++; - - return c_off + ta->caret_pos.char_off; -} - - -/* exported interface, documented in textarea.h */ void textarea_redraw(struct textarea *ta, int x, int y, colour bg, float scale, const struct rect *clip, const struct redraw_context *ctx) { diff --git a/desktop/textarea.h b/desktop/textarea.h index 8652c5436..016f15a10 100644 --- a/desktop/textarea.h +++ b/desktop/textarea.h @@ -166,14 +166,6 @@ int textarea_get_text(struct textarea *ta, char *buf, unsigned int len); bool textarea_set_caret(struct textarea *ta, int caret); /** - * Get the caret's position - * - * \param ta Text area - * \return 0-based character index of caret location, or -1 on error - */ -int textarea_get_caret(struct textarea *ta); - -/** * Handle redraw requests for text areas * * \param ta textarea to render |