From 59d24187f757463b1fef6c8c9ba4ebbc1952f74d Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Fri, 8 Feb 2013 16:05:44 +0000 Subject: Support dropping text file on textarea widget. --- desktop/textarea.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ desktop/textarea.h | 10 ++++++++++ render/html.c | 14 +++++++------- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/desktop/textarea.c b/desktop/textarea.c index 25630e464..7324b821b 100644 --- a/desktop/textarea.c +++ b/desktop/textarea.c @@ -1110,6 +1110,52 @@ bool textarea_set_text(struct textarea *ta, const char *text) } +/* exported interface, documented in textarea.h */ +bool textarea_drop_text(struct textarea *ta, const char *text, + size_t text_length) +{ + struct textarea_msg msg; + unsigned int caret_pos; + size_t text_chars; + + if (ta->flags & TEXTAREA_READONLY) + return false; + + if (text == NULL) + return false; + + text_chars = utf8_bounded_length(text, text_length); + caret_pos = textarea_get_caret(ta); + + if (ta->sel_start != -1) { + if (!textarea_replace_text(ta, ta->sel_start, ta->sel_end, + text, text_length, false)) + return false; + + caret_pos = ta->sel_start + text_chars; + ta->sel_start = ta->sel_end = -1; + } else { + if (!textarea_replace_text(ta, caret_pos, caret_pos, + text, text_length, false)) + return false; + caret_pos += text_chars; + } + + textarea_set_caret(ta, caret_pos); + + msg.ta = ta; + msg.type = TEXTAREA_MSG_REDRAW_REQUEST; + msg.data.redraw.x0 = 0; + msg.data.redraw.y0 = 0; + msg.data.redraw.x1 = ta->vis_width; + msg.data.redraw.y1 = ta->vis_height; + + ta->callback(ta->data, &msg); + + return true; +} + + /* exported interface, documented in textarea.h */ int textarea_get_text(struct textarea *ta, char *buf, unsigned int len) { diff --git a/desktop/textarea.h b/desktop/textarea.h index 560f1ca28..566e0cf6c 100644 --- a/desktop/textarea.h +++ b/desktop/textarea.h @@ -123,6 +123,16 @@ void textarea_destroy(struct textarea *ta); */ bool textarea_set_text(struct textarea *ta, const char *text); +/** + * Insert the text in a text area at the caret, replacing any selection. + * + * \param ta Text area + * \param text UTF-8 text to set text area's contents to + * \return true on success, false on memory exhaustion or if ta lacks caret + */ +bool textarea_drop_text(struct textarea *ta, const char *text, + size_t text_length); + /** * Extract the text from a text area * diff --git a/render/html.c b/render/html.c index 1eb0f91f1..4fc152a84 100644 --- a/render/html.c +++ b/render/html.c @@ -2796,7 +2796,7 @@ static bool html_drop_file_at_point(struct content *c, int x, int y, char *file) /* Redraw box. */ html__redraw_a_box(html, file_box); - } else if (html->bw != NULL) { + } else { /* File dropped on text input */ size_t file_len; @@ -2805,7 +2805,7 @@ static bool html_drop_file_at_point(struct content *c, int x, int y, char *file) char *utf8_buff; utf8_convert_ret ret; unsigned int size; - struct browser_window *bw; + int bx, by; /* Open file */ fp = fopen(file, "rb"); @@ -2861,13 +2861,13 @@ static bool html_drop_file_at_point(struct content *c, int x, int y, char *file) size = strlen(utf8_buff); /* Simulate a click over the input box, to place caret */ - browser_window_mouse_click(html->bw, - BROWSER_MOUSE_PRESS_1, x, y); - - bw = browser_window_get_root(html->bw); + box_coords(text_box, &bx, &by); + textarea_mouse_action(text_box->gadget->data.text.ta, + BROWSER_MOUSE_PRESS_1, x - bx, y - by); /* Paste the file as text */ - browser_window_paste_text(bw, utf8_buff, size, true); + textarea_drop_text(text_box->gadget->data.text.ta, + utf8_buff, size); free(utf8_buff); } -- cgit v1.2.3