2013-02-07 02:39:45 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Michael Drake <tlsa@netsurf-browser.org>
|
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Box tree treeview box replacement (implementation).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dom/dom.h>
|
|
|
|
|
2016-06-06 10:59:23 +03:00
|
|
|
#include "utils/config.h"
|
|
|
|
#include "utils/log.h"
|
2016-07-01 16:27:04 +03:00
|
|
|
#include "netsurf/keypress.h"
|
2013-02-07 02:39:45 +04:00
|
|
|
#include "desktop/textarea.h"
|
2014-10-13 14:56:31 +04:00
|
|
|
|
2013-02-07 02:39:45 +04:00
|
|
|
#include "render/box_textarea.h"
|
|
|
|
#include "render/font.h"
|
2014-11-11 20:11:59 +03:00
|
|
|
#include "render/form_internal.h"
|
2013-02-07 02:39:45 +04:00
|
|
|
|
|
|
|
|
2013-02-22 16:19:35 +04:00
|
|
|
bool box_textarea_keypress(html_content *html, struct box *box, uint32_t key)
|
2013-02-07 02:39:45 +04:00
|
|
|
{
|
|
|
|
struct form_control *gadget = box->gadget;
|
|
|
|
struct textarea *ta = gadget->data.text.ta;
|
|
|
|
struct form* form = box->gadget->form;
|
|
|
|
struct content *c = (struct content *) html;
|
|
|
|
|
|
|
|
assert(ta != NULL);
|
|
|
|
|
|
|
|
if (gadget->type != GADGET_TEXTAREA) {
|
|
|
|
switch (key) {
|
2015-03-27 12:36:19 +03:00
|
|
|
case NS_KEY_NL:
|
|
|
|
case NS_KEY_CR:
|
2013-02-07 02:39:45 +04:00
|
|
|
if (form)
|
|
|
|
form_submit(content_get_url(c), html->bw,
|
|
|
|
form, 0);
|
|
|
|
return true;
|
|
|
|
|
2015-03-27 12:36:19 +03:00
|
|
|
case NS_KEY_TAB:
|
2013-02-07 02:39:45 +04:00
|
|
|
{
|
|
|
|
struct form_control *next_input;
|
|
|
|
/* Find next text entry field that is actually
|
|
|
|
* displayed (i.e. has an associated box) */
|
|
|
|
for (next_input = gadget->next;
|
|
|
|
next_input &&
|
|
|
|
((next_input->type != GADGET_TEXTBOX &&
|
|
|
|
next_input->type != GADGET_TEXTAREA &&
|
|
|
|
next_input->type != GADGET_PASSWORD) ||
|
|
|
|
!next_input->box);
|
|
|
|
next_input = next_input->next)
|
|
|
|
;
|
|
|
|
if (!next_input)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
textarea_set_caret(ta, -1);
|
|
|
|
textarea_set_caret(next_input->data.text.ta, 0);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
2015-03-27 12:36:19 +03:00
|
|
|
case NS_KEY_SHIFT_TAB:
|
2013-02-07 02:39:45 +04:00
|
|
|
{
|
|
|
|
struct form_control *prev_input;
|
|
|
|
/* Find previous text entry field that is actually
|
|
|
|
* displayed (i.e. has an associated box) */
|
|
|
|
for (prev_input = gadget->prev;
|
|
|
|
prev_input &&
|
|
|
|
((prev_input->type != GADGET_TEXTBOX &&
|
|
|
|
prev_input->type != GADGET_TEXTAREA &&
|
|
|
|
prev_input->type != GADGET_PASSWORD) ||
|
|
|
|
!prev_input->box);
|
|
|
|
prev_input = prev_input->prev)
|
|
|
|
;
|
|
|
|
if (!prev_input)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
textarea_set_caret(ta, -1);
|
|
|
|
textarea_set_caret(prev_input->data.text.ta, 0);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Pass to textarea widget */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return textarea_keypress(ta, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for html form textareas.
|
|
|
|
*/
|
|
|
|
static void box_textarea_callback(void *data, struct textarea_msg *msg)
|
|
|
|
{
|
|
|
|
struct form_textarea_data *d = data;
|
|
|
|
struct form_control *gadget = d->gadget;
|
2014-02-17 18:38:53 +04:00
|
|
|
struct html_content *html = d->gadget->html;
|
2013-02-07 02:39:45 +04:00
|
|
|
struct box *box = gadget->box;
|
|
|
|
|
|
|
|
switch (msg->type) {
|
|
|
|
case TEXTAREA_MSG_DRAG_REPORT:
|
|
|
|
if (msg->data.drag == TEXTAREA_DRAG_NONE) {
|
|
|
|
/* Textarea drag finished */
|
2013-02-08 17:22:53 +04:00
|
|
|
html_drag_type drag_type = HTML_DRAG_NONE;
|
|
|
|
union html_drag_owner drag_owner;
|
|
|
|
drag_owner.no_owner = true;
|
2013-02-07 02:39:45 +04:00
|
|
|
|
2014-02-17 18:38:53 +04:00
|
|
|
html_set_drag_type(html, drag_type, drag_owner,
|
2013-02-08 17:22:53 +04:00
|
|
|
NULL);
|
2013-02-07 02:39:45 +04:00
|
|
|
} else {
|
|
|
|
/* Textarea drag started */
|
|
|
|
struct rect rect = {
|
|
|
|
.x0 = INT_MIN,
|
|
|
|
.y0 = INT_MIN,
|
|
|
|
.x1 = INT_MAX,
|
|
|
|
.y1 = INT_MAX
|
|
|
|
};
|
2013-02-08 17:22:53 +04:00
|
|
|
union html_drag_owner drag_owner;
|
|
|
|
drag_owner.textarea = box;
|
|
|
|
|
|
|
|
switch (msg->data.drag) {
|
|
|
|
case TEXTAREA_DRAG_SCROLLBAR:
|
2015-05-03 19:47:41 +03:00
|
|
|
html_set_drag_type(html,
|
|
|
|
HTML_DRAG_TEXTAREA_SCROLLBAR,
|
|
|
|
drag_owner,
|
|
|
|
&rect);
|
2013-02-08 17:22:53 +04:00
|
|
|
break;
|
2015-05-03 19:47:41 +03:00
|
|
|
|
2013-02-08 17:22:53 +04:00
|
|
|
case TEXTAREA_DRAG_SELECTION:
|
2015-05-03 19:47:41 +03:00
|
|
|
html_set_drag_type(html,
|
|
|
|
HTML_DRAG_TEXTAREA_SELECTION,
|
|
|
|
drag_owner,
|
|
|
|
&rect);
|
2013-02-08 17:22:53 +04:00
|
|
|
break;
|
2015-05-03 19:47:41 +03:00
|
|
|
|
2013-02-08 17:22:53 +04:00
|
|
|
default:
|
2015-05-28 18:08:46 +03:00
|
|
|
LOG("Drag type %d not handled.", msg->data.drag);
|
2015-05-08 17:30:41 +03:00
|
|
|
/* This is a logic faliure in the
|
|
|
|
* front end code so abort.
|
|
|
|
*/
|
|
|
|
assert(0);
|
2013-02-08 17:22:53 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-02-07 02:39:45 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEXTAREA_MSG_REDRAW_REQUEST:
|
2013-03-19 16:44:03 +04:00
|
|
|
{
|
|
|
|
/* Request redraw of the required textarea rectangle */
|
|
|
|
int x, y;
|
2014-09-27 12:57:26 +04:00
|
|
|
|
|
|
|
if (html->reflowing == true) {
|
|
|
|
/* Can't redraw during layout, and it will
|
|
|
|
* be redrawn after layout anyway. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-03-19 16:44:03 +04:00
|
|
|
box_coords(box, &x, &y);
|
|
|
|
|
|
|
|
content__request_redraw((struct content *)html,
|
|
|
|
x + msg->data.redraw.x0,
|
|
|
|
y + msg->data.redraw.y0,
|
|
|
|
msg->data.redraw.x1 - msg->data.redraw.x0,
|
|
|
|
msg->data.redraw.y1 - msg->data.redraw.y0);
|
|
|
|
}
|
2013-02-07 02:39:45 +04:00
|
|
|
break;
|
|
|
|
|
2013-02-22 16:19:35 +04:00
|
|
|
case TEXTAREA_MSG_SELECTION_REPORT:
|
|
|
|
if (msg->data.selection.have_selection) {
|
|
|
|
/* Textarea now has a selection */
|
|
|
|
union html_selection_owner sel_owner;
|
|
|
|
sel_owner.textarea = box;
|
|
|
|
|
2014-02-17 18:38:53 +04:00
|
|
|
html_set_selection(html, HTML_SELECTION_TEXTAREA,
|
2013-02-22 16:19:35 +04:00
|
|
|
sel_owner,
|
|
|
|
msg->data.selection.read_only);
|
|
|
|
} else {
|
|
|
|
/* The textarea now has no selection */
|
|
|
|
union html_selection_owner sel_owner;
|
|
|
|
sel_owner.none = true;
|
|
|
|
|
2014-02-17 18:38:53 +04:00
|
|
|
html_set_selection(html, HTML_SELECTION_NONE,
|
2013-02-22 16:19:35 +04:00
|
|
|
sel_owner, true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEXTAREA_MSG_CARET_UPDATE:
|
2013-02-07 02:39:45 +04:00
|
|
|
if (html->bw == NULL)
|
|
|
|
break;
|
|
|
|
|
2013-02-22 16:19:35 +04:00
|
|
|
if (msg->data.caret.type == TEXTAREA_CARET_HIDE) {
|
|
|
|
union html_focus_owner focus_owner;
|
|
|
|
focus_owner.textarea = box;
|
2014-02-17 18:38:53 +04:00
|
|
|
html_set_focus(html, HTML_FOCUS_TEXTAREA,
|
2013-02-22 16:19:35 +04:00
|
|
|
focus_owner, true, 0, 0, 0, NULL);
|
2013-02-07 02:39:45 +04:00
|
|
|
} else {
|
2013-02-22 16:19:35 +04:00
|
|
|
union html_focus_owner focus_owner;
|
|
|
|
focus_owner.textarea = box;
|
2014-02-17 18:38:53 +04:00
|
|
|
html_set_focus(html, HTML_FOCUS_TEXTAREA,
|
2013-02-22 16:19:35 +04:00
|
|
|
focus_owner, false,
|
|
|
|
msg->data.caret.pos.x,
|
|
|
|
msg->data.caret.pos.y,
|
|
|
|
msg->data.caret.pos.height,
|
|
|
|
msg->data.caret.pos.clip);
|
2013-02-07 02:39:45 +04:00
|
|
|
}
|
|
|
|
break;
|
2014-01-04 18:48:58 +04:00
|
|
|
|
|
|
|
case TEXTAREA_MSG_TEXT_MODIFIED:
|
2014-02-08 21:11:55 +04:00
|
|
|
form_gadget_update_value(gadget,
|
2014-01-04 20:30:09 +04:00
|
|
|
strndup(msg->data.modified.text,
|
|
|
|
msg->data.modified.len));
|
2014-01-04 18:48:58 +04:00
|
|
|
break;
|
2013-02-07 02:39:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Exported interface, documented in box_textarea.h */
|
|
|
|
bool box_textarea_create_textarea(html_content *html,
|
|
|
|
struct box *box, struct dom_node *node)
|
|
|
|
{
|
|
|
|
dom_string *dom_text = NULL;
|
|
|
|
dom_exception err;
|
|
|
|
textarea_setup ta_setup;
|
|
|
|
textarea_flags ta_flags;
|
|
|
|
plot_font_style_t fstyle;
|
2013-02-23 21:44:20 +04:00
|
|
|
bool read_only = false;
|
2017-02-11 20:26:03 +03:00
|
|
|
bool disabled = false;
|
2013-02-07 02:39:45 +04:00
|
|
|
struct form_control *gadget = box->gadget;
|
|
|
|
const char *text;
|
|
|
|
|
|
|
|
assert(gadget != NULL);
|
|
|
|
assert(gadget->type == GADGET_TEXTAREA ||
|
|
|
|
gadget->type == GADGET_TEXTBOX ||
|
|
|
|
gadget->type == GADGET_PASSWORD);
|
|
|
|
|
|
|
|
if (gadget->type == GADGET_TEXTAREA) {
|
2013-02-23 21:44:20 +04:00
|
|
|
dom_html_text_area_element *textarea =
|
|
|
|
(dom_html_text_area_element *) node;
|
2013-03-09 23:44:25 +04:00
|
|
|
ta_flags = TEXTAREA_MULTILINE;
|
2013-02-23 21:44:20 +04:00
|
|
|
|
|
|
|
err = dom_html_text_area_element_get_read_only(
|
|
|
|
textarea, &read_only);
|
|
|
|
if (err != DOM_NO_ERR)
|
|
|
|
return false;
|
2013-02-07 02:39:45 +04:00
|
|
|
|
2017-02-11 20:26:03 +03:00
|
|
|
err = dom_html_text_area_element_get_disabled(
|
|
|
|
textarea, &disabled);
|
|
|
|
if (err != DOM_NO_ERR)
|
|
|
|
return false;
|
|
|
|
|
2013-02-07 02:39:45 +04:00
|
|
|
/* Get the textarea's initial content */
|
2014-01-04 19:37:51 +04:00
|
|
|
err = dom_html_text_area_element_get_value(textarea, &dom_text);
|
2013-02-07 02:39:45 +04:00
|
|
|
if (err != DOM_NO_ERR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
dom_html_input_element *input = (dom_html_input_element *) node;
|
|
|
|
|
2013-02-23 21:44:20 +04:00
|
|
|
err = dom_html_input_element_get_read_only(
|
|
|
|
input, &read_only);
|
|
|
|
if (err != DOM_NO_ERR)
|
|
|
|
return false;
|
|
|
|
|
2017-02-11 20:26:03 +03:00
|
|
|
err = dom_html_input_element_get_disabled(
|
|
|
|
input, &disabled);
|
|
|
|
if (err != DOM_NO_ERR)
|
|
|
|
return false;
|
|
|
|
|
2013-02-07 02:39:45 +04:00
|
|
|
if (gadget->type == GADGET_PASSWORD)
|
|
|
|
ta_flags = TEXTAREA_PASSWORD;
|
|
|
|
else
|
|
|
|
ta_flags = TEXTAREA_DEFAULT;
|
|
|
|
|
|
|
|
/* Get initial text */
|
|
|
|
err = dom_html_input_element_get_value(input, &dom_text);
|
|
|
|
if (err != DOM_NO_ERR)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dom_text != NULL) {
|
|
|
|
text = dom_string_data(dom_text);
|
|
|
|
} else {
|
|
|
|
/* No initial text, or failed reading it;
|
|
|
|
* use a blank string */
|
|
|
|
text = "";
|
|
|
|
}
|
|
|
|
|
2017-02-11 20:26:03 +03:00
|
|
|
if (read_only || disabled)
|
2013-02-23 21:44:20 +04:00
|
|
|
ta_flags |= TEXTAREA_READONLY;
|
|
|
|
|
2013-02-07 02:39:45 +04:00
|
|
|
gadget->data.text.data.gadget = gadget;
|
|
|
|
|
|
|
|
font_plot_style_from_css(gadget->box->style, &fstyle);
|
|
|
|
|
|
|
|
/* Reset to correct values by layout */
|
|
|
|
ta_setup.width = 200;
|
|
|
|
ta_setup.height = 20;
|
|
|
|
ta_setup.pad_top = 4;
|
|
|
|
ta_setup.pad_right = 4;
|
|
|
|
ta_setup.pad_bottom = 4;
|
|
|
|
ta_setup.pad_left = 4;
|
2013-02-10 02:55:31 +04:00
|
|
|
|
|
|
|
/* Set remaining data */
|
2013-02-07 02:39:45 +04:00
|
|
|
ta_setup.border_width = 0;
|
|
|
|
ta_setup.border_col = 0x000000;
|
|
|
|
ta_setup.text = fstyle;
|
|
|
|
ta_setup.text.background = NS_TRANSPARENT;
|
2013-02-10 02:55:31 +04:00
|
|
|
/* Make selected text either black or white, as gives greatest contrast
|
2013-02-11 04:41:22 +04:00
|
|
|
* with background colour. */
|
2013-02-10 02:55:31 +04:00
|
|
|
ta_setup.selected_bg = fstyle.foreground;
|
2013-02-11 04:41:22 +04:00
|
|
|
ta_setup.selected_text = colour_to_bw_furthest(ta_setup.selected_bg);
|
2013-02-07 02:39:45 +04:00
|
|
|
|
|
|
|
/* Hand reference to dom text over to gadget */
|
|
|
|
gadget->data.text.initial = dom_text;
|
|
|
|
|
|
|
|
gadget->data.text.ta = textarea_create(ta_flags, &ta_setup,
|
|
|
|
box_textarea_callback, &gadget->data.text.data);
|
|
|
|
|
|
|
|
if (gadget->data.text.ta == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!textarea_set_text(gadget->data.text.ta, text))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|