mirror of
https://github.com/netsurf-browser/netsurf
synced 2024-12-22 20:16:54 +03:00
Support dropping text file on textarea widget.
This commit is contained in:
parent
0a4e1a05e3
commit
59d24187f7
@ -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)
|
||||
{
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user