Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
  3.  * Copyright 2004 James Bursa <bursa@users.sourceforge.net>
  4.  * Copyright 2004 Andrew Timmins <atimmins@blueyonder.co.uk>
  5.  * Copyright 2004 John Tytgat <joty@netsurf-browser.org>
  6.  * Copyright 2005 Adrian Lees <adrianl@users.sourceforge.net>
  7.  *
  8.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  9.  *
  10.  * NetSurf is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; version 2 of the License.
  13.  *
  14.  * NetSurf is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.  */
  22.  
  23. /** \file
  24.  * HTML form text input handling (implementation)
  25.  */
  26.  
  27. #include <assert.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30.  
  31. #include <dom/dom.h>
  32.  
  33. #include "desktop/browser.h"
  34. #include "desktop/gui.h"
  35. #include "desktop/mouse.h"
  36. #include "desktop/scrollbar.h"
  37. #include "desktop/selection.h"
  38. #include "desktop/textinput.h"
  39. #include "render/box.h"
  40. #include "render/font.h"
  41. #include "render/form.h"
  42. #include "render/html_internal.h"
  43. #include "render/layout.h"
  44. #include "render/textinput.h"
  45. #include "utils/log.h"
  46. #include "utils/talloc.h"
  47. #include "utils/utf8.h"
  48. #include "utils/utils.h"
  49.  
  50. /* Define to enable textinput debug */
  51. #undef TEXTINPUT_DEBUG
  52.  
  53.  
  54. static bool textinput_textbox_delete(struct content *c,
  55.                 struct box *text_box, unsigned char_offset,
  56.                 unsigned utf8_len);
  57.  
  58. /* Textarea callbacks */
  59. static bool textinput_textarea_callback(struct browser_window *bw,
  60.                 uint32_t key, void *p1, void *p2);
  61. static void textinput_textarea_move_caret(struct browser_window *bw,
  62.                 void *p1, void *p2);
  63. static bool textinput_textarea_paste_text(struct browser_window *bw,
  64.                 const char *utf8, unsigned utf8_len, bool last,
  65.                 void *p1, void *p2);
  66.  
  67. /* Text input callbacks */
  68. static bool textinput_input_callback(struct browser_window *bw,
  69.                 uint32_t key, void *p1, void *p2);
  70. static void textinput_input_move_caret(struct browser_window *bw,
  71.                 void *p1, void *p2);
  72. static bool textinput_input_paste_text(struct browser_window *bw,
  73.                 const char *utf8, unsigned utf8_len, bool last,
  74.                 void *p1, void *p2);
  75.  
  76. #define SPACE_LEN(b) ((b->space == 0) ? 0 : 1)
  77.  
  78.  
  79. static struct textinput_buffer {
  80.         char *buffer;
  81.         size_t buffer_len;
  82.         size_t length;
  83. } textinput_buffer;
  84.  
  85.  
  86.  
  87. /**
  88.  * Given the x,y co-ordinates of a point within a textarea, return the
  89.  * TEXT box pointer, and the character and pixel offsets within that
  90.  * box at which the caret should be positioned. (eg. for mouse clicks,
  91.  * drag-and-drop insertions etc)
  92.  *
  93.  * \param  textarea       the textarea being considered
  94.  * \param  x              x ordinate of point
  95.  * \param  y              y ordinate of point
  96.  * \param  pchar_offset   receives the char offset within the TEXT box
  97.  * \param  ppixel_offset  receives the pixel offset within the TEXT box
  98.  * \return pointer to TEXT box
  99.  */
  100.  
  101. static struct box *textinput_textarea_get_position(struct box *textarea,
  102.                 int x, int y, int *pchar_offset, int *ppixel_offset)
  103. {
  104.         /* A textarea is an INLINE_BLOCK containing a single
  105.          * INLINE_CONTAINER, which contains the text as runs of TEXT
  106.          * separated by BR. There is at least one TEXT. The first and
  107.          * last boxes are TEXT. Consecutive BR may not be present. These
  108.          * constraints are satisfied by using a 0-length TEXT for blank
  109.          * lines. */
  110.  
  111.         struct box *inline_container, *text_box;
  112.         plot_font_style_t fstyle;
  113.         size_t char_offset = 0;
  114.  
  115.         inline_container = textarea->children;
  116.  
  117.         if (inline_container->y + inline_container->height < y) {
  118.                 /* below the bottom of the textarea: place caret at end */
  119.                 text_box = inline_container->last;
  120.                 assert(text_box->type == BOX_TEXT);
  121.                 assert(text_box->text);
  122.                 font_plot_style_from_css(text_box->style, &fstyle);
  123.                 /** \todo handle errors */
  124.                 nsfont.font_position_in_string(&fstyle, text_box->text,
  125.                                 text_box->length,
  126.                                 (unsigned int)(x - text_box->x),
  127.                                 &char_offset, ppixel_offset);
  128.         } else {
  129.                 /* find the relevant text box */
  130.                 y -= inline_container->y;
  131.                 for (text_box = inline_container->children;
  132.                                 text_box &&
  133.                                 text_box->y + text_box->height < y;
  134.                                 text_box = text_box->next)
  135.                         ;
  136.                 for (; text_box && text_box->type != BOX_BR &&
  137.                                 text_box->y <= y &&
  138.                                 text_box->x + text_box->width < x;
  139.                                 text_box = text_box->next)
  140.                         ;
  141.                 if (!text_box) {
  142.                         /* past last text box */
  143.                         text_box = inline_container->last;
  144.                         assert(text_box->type == BOX_TEXT);
  145.                         assert(text_box->text);
  146.                         font_plot_style_from_css(text_box->style, &fstyle);
  147.                         nsfont.font_position_in_string(&fstyle,
  148.                                         text_box->text,
  149.                                         text_box->length,
  150.                                         textarea->width,
  151.                                         &char_offset,
  152.                                         ppixel_offset);
  153.                 } else {
  154.                         /* in a text box */
  155.                         if (text_box->type == BOX_BR)
  156.                                 text_box = text_box->prev;
  157.                         else if (y < text_box->y && text_box->prev) {
  158.                                 if (text_box->prev->type == BOX_BR) {
  159.                                         assert(text_box->prev->prev);
  160.                                         text_box = text_box->prev->prev;
  161.                                 }
  162.                                 else
  163.                                         text_box = text_box->prev;
  164.                         }
  165.                         assert(text_box->type == BOX_TEXT);
  166.                         assert(text_box->text);
  167.                         font_plot_style_from_css(text_box->style, &fstyle);
  168.                         nsfont.font_position_in_string(&fstyle,
  169.                                         text_box->text,
  170.                                         text_box->length,
  171.                                         (unsigned int)(x - text_box->x),
  172.                                         &char_offset,
  173.                                         ppixel_offset);
  174.                 }
  175.         }
  176.  
  177.         *pchar_offset = char_offset;
  178.  
  179.         assert(text_box);
  180.         return text_box;
  181. }
  182.  
  183.  
  184. /**
  185.  * Delete some text from a box, or delete the box in its entirety
  186.  *
  187.  * \param  c       html content
  188.  * \param  b       box
  189.  * \param  offset  start offset of text to be deleted (in bytes)
  190.  * \param  length  length of text to be deleted
  191.  * \return true iff successful
  192.  */
  193.  
  194. static bool textinput_delete_handler(struct content *c, struct box *b,
  195.                 int offset, size_t length)
  196. {
  197.         size_t text_length = b->length + SPACE_LEN(b);
  198.  
  199.         /* only remove if its not the first box */
  200.         if (offset <= 0 && length >= text_length && b->prev != NULL) {
  201.                 /* remove the entire box */
  202.                 box_unlink_and_free(b);
  203.  
  204.                 return true;
  205.         } else
  206.                 return textinput_textbox_delete(c, b, offset,
  207.                                 min(length, text_length - offset));
  208. }
  209.  
  210.  
  211. /**
  212.  * Remove the selected text from a text box and gadget (if applicable)
  213.  *
  214.  * \param  c    The content containing the selection
  215.  * \param  s    The selection to be removed
  216.  */
  217.  
  218. static void textinput_delete_selection(struct content *c, struct selection *s)
  219. {
  220.         size_t start_offset, end_offset;
  221.         struct box *text_box;
  222.         struct box *end_box;
  223.         struct box *next;
  224.         size_t sel_len;
  225.         int beginning = 0;
  226.  
  227.         assert(s->defined);
  228.  
  229.         text_box = selection_get_start(s, &start_offset);
  230.         end_box = selection_get_end(s, &end_offset);
  231.         sel_len = s->end_idx - s->start_idx;
  232.  
  233.         /* Clear selection so that deletion from textboxes proceeds */
  234.         selection_clear(s, true);
  235.  
  236.         /* handle first box */
  237.         textinput_delete_handler(c, text_box, start_offset, sel_len);
  238.         if (text_box == end_box)
  239.                 return;
  240.  
  241.         for (text_box = text_box->next; text_box != end_box; text_box = next) {
  242.                 next = text_box->next;
  243.                 box_unlink_and_free(text_box);
  244.         }
  245.  
  246.         textinput_delete_handler(c, end_box, beginning, end_offset);
  247. }
  248.  
  249.  
  250. /**
  251.  * Insert a number of chars into a text box
  252.  *
  253.  * \param  c            html_content
  254.  * \param  text_box     text box
  255.  * \param  char_offset  offset (bytes) at which to insert text
  256.  * \param  utf8         UTF-8 text to insert
  257.  * \param  utf8_len     length (bytes) of UTF-8 text to insert
  258.  * \return true iff successful
  259.  */
  260.  
  261. static bool textinput_textbox_insert(struct content *c,
  262.                 struct box *text_box, unsigned char_offset, const char *utf8,
  263.                 unsigned utf8_len)
  264. {
  265.         html_content *html = (html_content *)c;
  266.         char *text;
  267.         struct box *input = text_box->parent->parent;
  268.         bool hide;
  269.  
  270.         if (html->bw && html->sel.defined)
  271.                 textinput_delete_selection(c, &html->sel);
  272.  
  273.         /* insert into form gadget (text and password inputs only) */
  274.         if (input->gadget && (input->gadget->type == GADGET_TEXTBOX ||
  275.                         input->gadget->type == GADGET_PASSWORD) &&
  276.                         input->gadget->value) {
  277.                 size_t form_offset = input->gadget->caret_form_offset;
  278.                 char *value = realloc(input->gadget->value,
  279.                                 input->gadget->length + utf8_len + 1);
  280.                 if (!value) {
  281.                         warn_user("NoMemory", 0);
  282.                         return true;
  283.                 }
  284.                 input->gadget->value = value;
  285.  
  286.                 memmove(input->gadget->value + form_offset + utf8_len,
  287.                                 input->gadget->value + form_offset,
  288.                                 input->gadget->length - form_offset);
  289.                 memcpy(input->gadget->value + form_offset, utf8, utf8_len);
  290.                 input->gadget->length += utf8_len;
  291.                 input->gadget->value[input->gadget->length] = 0;
  292.         }
  293.  
  294.         hide = (input->gadget && input->gadget->type == GADGET_PASSWORD);
  295.         if (hide) {
  296.                 /* determine the number of '*'s to be inserted */
  297.                 const char *eutf8 = utf8 + utf8_len;
  298.                 utf8_len = 0;
  299.                 while (utf8 < eutf8) {
  300.                         utf8 += utf8_next(utf8, eutf8 - utf8, 0);
  301.                         utf8_len++;
  302.                 }
  303.         }
  304.  
  305.         /* insert in text box */
  306.         text = talloc_realloc(html->bctx, text_box->text,
  307.                         char,
  308.                         text_box->length + SPACE_LEN(text_box) + utf8_len + 1);
  309.         if (!text) {
  310.                 warn_user("NoMemory", 0);
  311.                 return false;
  312.         }
  313.         text_box->text = text;
  314.  
  315.         if (text_box->space != 0 &&
  316.                         char_offset == text_box->length + SPACE_LEN(text_box)) {
  317.                 if (hide)
  318.                         text_box->space = 0;
  319.                 else {
  320.                         unsigned int last_off = utf8_prev(utf8, utf8_len);
  321.                         if (utf8[last_off] != ' ')
  322.                                 text_box->space = 0;
  323.                         else
  324.                                 utf8_len = last_off;
  325.                 }
  326.                 text_box->text[text_box->length++] = ' ';
  327.         } else {
  328.                 memmove(text_box->text + char_offset + utf8_len,
  329.                                 text_box->text + char_offset,
  330.                                 text_box->length - char_offset);
  331.         }
  332.  
  333.         if (hide)
  334.                 memset(text_box->text + char_offset, '*', utf8_len);
  335.         else
  336.                 memcpy(text_box->text + char_offset, utf8, utf8_len);
  337.         text_box->length += utf8_len;
  338.  
  339.         /* nothing should assume that the text is terminated,
  340.          * but just in case */
  341.         text_box->text[text_box->length] = 0;
  342.  
  343.         text_box->width = UNKNOWN_WIDTH;
  344.  
  345.         return true;
  346. }
  347.  
  348. /**
  349.  * Calculates the form_offset from the box_offset
  350.  *
  351.  * \param input         The root box containing both the textbox and gadget
  352.  * \param text_box      The textbox containing the caret
  353.  * \param char_offset   The caret offset within text_box
  354.  * \return the translated form_offset
  355.  */
  356.  
  357. static size_t textinput_get_form_offset(struct box* input, struct box* text_box,
  358.                 size_t char_offset)
  359. {
  360.         int uchars;
  361.         unsigned int offset;
  362.  
  363.         for (uchars = 0, offset = 0; offset < char_offset; uchars++) {
  364.                 if ((text_box->text[offset] & 0x80) == 0x00) {
  365.                         offset++;
  366.                         continue;
  367.                 }
  368.                 assert((text_box->text[offset] & 0xC0) == 0xC0);
  369.                 for (++offset; offset < char_offset &&
  370.                                 (text_box->text[offset] & 0xC0) == 0x80;
  371.                                 offset++)
  372.                         /* do nothing */;
  373.         }
  374.         /* uchars is the number of real Unicode characters at the left
  375.          * side of the caret.
  376.          */
  377.         for (offset = 0; uchars > 0 && offset < input->gadget->length;
  378.                         uchars--) {
  379.                 if ((input->gadget->value[offset] & 0x80) == 0x00) {
  380.                         offset++;
  381.                         continue;
  382.                 }
  383.                 assert((input->gadget->value[offset] & 0xC0) == 0xC0);
  384.                 for (++offset; offset < input->gadget->length &&
  385.                         (input->gadget->value[offset] & 0xC0) == 0x80;
  386.                                 offset++)
  387.                         /* do nothing */;
  388.         }
  389.         assert(uchars == 0);
  390.         return offset;
  391. }
  392.  
  393.  
  394. /**
  395.  * Delete a number of chars from a text box
  396.  *
  397.  * \param  c            html content
  398.  * \param  text_box     text box
  399.  * \param  char_offset  offset within text box (bytes) of first char to delete
  400.  * \param  utf8_len     length (bytes) of chars to be deleted
  401.  * \return true on success, false otherwise
  402.  *
  403.  * ::char_offset and ::utf8_len are only considered when there is no selection.
  404.  * If there is a selection, the entire selected area is deleted.
  405.  */
  406.  
  407. bool textinput_textbox_delete(struct content *c, struct box *text_box,
  408.                 unsigned char_offset, unsigned utf8_len)
  409. {
  410.         html_content *html = (html_content *)c;
  411.         unsigned next_offset = char_offset + utf8_len;
  412.         struct box *form = text_box->parent->parent;
  413.  
  414.         if (html->bw && html->sel.defined) {
  415.                 textinput_delete_selection(c, &html->sel);
  416.                 return true;
  417.         }
  418.  
  419.         /* delete from form gadget (text and password inputs only) */
  420.         if (form->gadget && (form->gadget->type == GADGET_TEXTBOX ||
  421.                         form->gadget->type == GADGET_PASSWORD) &&
  422.                         form->gadget->value) {
  423.                 size_t form_offset = textinput_get_form_offset(form, text_box,
  424.                                                 char_offset);
  425.                 size_t next_offset = textinput_get_form_offset(form, text_box,
  426.                                                 char_offset + utf8_len);
  427.  
  428.                 memmove(form->gadget->value + form_offset,
  429.                                 form->gadget->value + next_offset,
  430.                                 form->gadget->length - next_offset);
  431.                 form->gadget->length -= (next_offset - form_offset);
  432.                 form->gadget->value[form->gadget->length] = 0;
  433.         }
  434.  
  435.         /* delete from visible textbox */
  436.         if (next_offset <= text_box->length + SPACE_LEN(text_box)) {
  437.                 /* handle removal of trailing space */
  438.                 if (text_box->space != 0 && next_offset > text_box->length) {
  439.                         if (char_offset > 0) {
  440.                                 /* is the trailing character still a space? */
  441.                                 int tmp = utf8_prev(text_box->text, char_offset);
  442.                                 if (isspace(text_box->text[tmp]))
  443.                                         char_offset = tmp;
  444.                                 else
  445.                                         text_box->space = 0;
  446.                         } else {
  447.                                 text_box->space = 0;
  448.                         }
  449.  
  450.                         text_box->length = char_offset;
  451.                 } else {
  452.                         memmove(text_box->text + char_offset,
  453.                                         text_box->text + next_offset,
  454.                                         text_box->length - next_offset);
  455.                         text_box->length -= utf8_len;
  456.                 }
  457.  
  458.                 /* nothing should assume that the text is terminated,
  459.                  * but just in case */
  460.                 text_box->text[text_box->length] = 0;
  461.  
  462.                 text_box->width = UNKNOWN_WIDTH;
  463.  
  464.                 return true;
  465.         }
  466.  
  467.         return false;
  468. }
  469.  
  470. /**
  471.  * Locate the first inline box at the start of this line
  472.  *
  473.  * \param  text_box  text box from which to start searching
  474.  */
  475.  
  476. static struct box *textinput_line_start(struct box *text_box)
  477. {
  478.         while (text_box->prev && text_box->prev->type == BOX_TEXT)
  479.                 text_box = text_box->prev;
  480.         return text_box;
  481. }
  482.  
  483.  
  484. /**
  485.  * Locate the last inline box in this line
  486.  *
  487.  * \param  text_box  text box from which to start searching
  488.  */
  489.  
  490. static struct box *textinput_line_end(struct box *text_box)
  491. {
  492.         while (text_box->next && text_box->next->type == BOX_TEXT)
  493.                 text_box = text_box->next;
  494.         return text_box;
  495. }
  496.  
  497.  
  498. /**
  499.  * Backtrack to the start of the previous line, if there is one.
  500.  */
  501.  
  502. static struct box *textinput_line_above(struct box *text_box)
  503. {
  504.         struct box *prev;
  505.  
  506.         text_box = textinput_line_start(text_box);
  507.  
  508.         prev = text_box->prev;
  509.         while (prev && prev->type == BOX_BR)
  510.                 prev = prev->prev;
  511.  
  512.         return prev ? textinput_line_start(prev) : text_box;
  513. }
  514.  
  515.  
  516. /**
  517.  * Advance to the start of the next line, if there is one.
  518.  */
  519.  
  520. static struct box *textinput_line_below(struct box *text_box)
  521. {
  522.         struct box *next;
  523.  
  524.         text_box = textinput_line_end(text_box);
  525.  
  526.         next = text_box->next;
  527.         while (next && next->type == BOX_BR)
  528.                 next = next->next;
  529.  
  530.         return next ? next : text_box;
  531. }
  532.  
  533.  
  534. /**
  535.  * Add some text to the buffer, optionally appending a trailing space.
  536.  *
  537.  * \param text text to be added
  538.  * \param length length of text in bytes
  539.  * \param space indicates whether a trailing space should be appended
  540.  * \param fstyle The font style
  541.  * \return true if successful
  542.  */
  543.  
  544. static bool textinput_add_to_buffer(const char *text, size_t length, bool space,
  545.                 const plot_font_style_t *fstyle)
  546. {
  547.         size_t new_length = textinput_buffer.length + length + (space ? 1 : 0) + 1;
  548.  
  549.         if (new_length > textinput_buffer.buffer_len) {
  550.                 size_t new_alloc = new_length + (new_length / 4);
  551.                 char *new_buff;
  552.  
  553.                 new_buff = realloc(textinput_buffer.buffer, new_alloc);
  554.                 if (new_buff == NULL)
  555.                         return false;
  556.  
  557.                 textinput_buffer.buffer = new_buff;
  558.                 textinput_buffer.buffer_len = new_alloc;
  559.         }
  560.  
  561.         memcpy(textinput_buffer.buffer + textinput_buffer.length, text, length);
  562.         textinput_buffer.length += length;
  563.  
  564.         if (space)
  565.                 textinput_buffer.buffer[textinput_buffer.length++] = ' ';
  566.  
  567.         textinput_buffer.buffer[textinput_buffer.length] = '\0';
  568.  
  569.         return true;
  570. }
  571.  
  572.  
  573. /**
  574.  * Empty the buffer, called prior to textinput_add_to_buffer sequence
  575.  *
  576.  * \return true iff successful
  577.  */
  578.  
  579. static bool textinput_empty_buffer(void)
  580. {
  581.         const size_t init_size = 1024;
  582.  
  583.         if (textinput_buffer.buffer_len == 0) {
  584.                 textinput_buffer.buffer = malloc(init_size);
  585.                 if (textinput_buffer.buffer == NULL)
  586.                         return false;
  587.  
  588.                 textinput_buffer.buffer_len = init_size;
  589.         }
  590.  
  591.         textinput_buffer.length = 0;
  592.  
  593.         return true;
  594. }
  595.  
  596.  
  597. /**
  598.  * Cut a range of text from a text box,
  599.  * possibly placing it on the global clipboard.
  600.  *
  601.  * \param  c          html content
  602.  * \param  start_box  text box at start of range
  603.  * \param  start_idx  index (bytes) within start box
  604.  * \param  end_box    text box at end of range
  605.  * \param  end_idx    index (bytes) within end box
  606.  * \param  clipboard  whether to place text on the clipboard
  607.  * \return true iff successful
  608.  */
  609.  
  610. static bool textinput_textarea_cut(struct content *c,
  611.                 struct box *start_box, unsigned start_idx,
  612.                 struct box *end_box, unsigned end_idx,
  613.                 bool clipboard)
  614. {
  615.         struct box *box = start_box;
  616.         bool success = true;
  617.         bool del = false;       /* caller expects start_box to persist */
  618.  
  619.         if (textinput_empty_buffer() == false) {
  620.                 return false;
  621.         }
  622.  
  623.         while (box && box != end_box) {
  624.                 /* read before deletion, in case the whole box goes */
  625.                 struct box *next = box->next;
  626.  
  627.                 if (box->type == BOX_BR) {
  628.                         if (clipboard && !textinput_add_to_buffer("\n", 1,
  629.                                         false, plot_style_font)) {
  630.                                 return false;
  631.                         }
  632.                         box_unlink_and_free(box);
  633.                 } else {
  634.                         /* append box text to clipboard and then delete it */
  635.                         if (clipboard &&
  636.                                 !textinput_add_to_buffer(box->text + start_idx,
  637.                                         box->length - start_idx,
  638.                                         SPACE_LEN(box), plot_style_font)) {
  639.                                 return false;
  640.                         }
  641.  
  642.                         if (del) {
  643.                                 if (!textinput_delete_handler(c, box,
  644.                                                 start_idx,
  645.                                                 (box->length + SPACE_LEN(box)) -
  646.                                                 start_idx) && clipboard) {
  647.                                         return false;
  648.                                 }
  649.                         } else {
  650.                                 textinput_textbox_delete(c, box, start_idx,
  651.                                         (box->length + SPACE_LEN(box)) -
  652.                                         start_idx);
  653.                         }
  654.                 }
  655.  
  656.                 del = true;
  657.                 start_idx = 0;
  658.                 box = next;
  659.         }
  660.  
  661.         /* and the last box */
  662.         if (box) {
  663.                 if (clipboard && !textinput_add_to_buffer(box->text + start_idx,
  664.                                 end_idx - start_idx, end_idx > box->length,
  665.                                 plot_style_font)) {
  666.                         success = false;
  667.                 } else {
  668.                         if (del) {
  669.                                 if (!textinput_delete_handler(c, box,
  670.                                                 start_idx, end_idx - start_idx))
  671.                                         success = false;
  672.                         } else {
  673.                                 textinput_textbox_delete(c, box, start_idx,
  674.                                                 end_idx - start_idx);
  675.                         }
  676.                 }
  677.         }
  678.  
  679.         if (clipboard) {
  680.                 gui_set_clipboard(textinput_buffer.buffer,
  681.                                 textinput_buffer.length, NULL, 0);
  682.         }
  683.  
  684.         return true;
  685. }
  686.  
  687.  
  688. /**
  689.  * Break a text box into two
  690.  *
  691.  * \param  c            html content
  692.  * \param  text_box     text box to be split
  693.  * \param  char_offset  offset (in bytes) at which text box is to be split
  694.  */
  695.  
  696. static struct box *textinput_textarea_insert_break(struct content *c,
  697.                 struct box *text_box, size_t char_offset)
  698. {
  699.         html_content *html = (html_content *)c;
  700.         struct box *new_br, *new_text;
  701.         char *text;
  702.  
  703.         text = talloc_array(html->bctx, char, text_box->length + 1);
  704.         if (!text) {
  705.                 warn_user("NoMemory", 0);
  706.                 return NULL;
  707.         }
  708.  
  709.         new_br = box_create(NULL, text_box->style, false, 0, 0, text_box->title,
  710.                         0, html->bctx);
  711.         new_text = talloc(html->bctx, struct box);
  712.         if (!new_text) {
  713.                 warn_user("NoMemory", 0);
  714.                 return NULL;
  715.         }
  716.  
  717.         new_br->type = BOX_BR;
  718.         box_insert_sibling(text_box, new_br);
  719.  
  720.         memcpy(new_text, text_box, sizeof (struct box));
  721.         new_text->flags |= CLONE;
  722.         new_text->text = text;
  723.         memcpy(new_text->text, text_box->text + char_offset,
  724.                         text_box->length - char_offset);
  725.         new_text->length = text_box->length - char_offset;
  726.         text_box->length = char_offset;
  727.         text_box->width = new_text->width = UNKNOWN_WIDTH;
  728.         box_insert_sibling(new_br, new_text);
  729.  
  730.         return new_text;
  731. }
  732.  
  733.  
  734. /**
  735.  * Reflow textarea preserving width and height
  736.  *
  737.  * \param  c                 html content
  738.  * \param  textarea          text area box
  739.  * \param  inline_container  container holding text box
  740.  */
  741.  
  742. static void textinput_textarea_reflow(struct content *c,
  743.                 struct box *textarea, struct box *inline_container)
  744. {
  745.         int width = textarea->width;
  746.         int height = textarea->height;
  747.  
  748.         assert(c != NULL);
  749.  
  750.         if (!layout_inline_container(inline_container, width,
  751.                         textarea, 0, 0, (struct html_content *) c))
  752.                 warn_user("NoMemory", 0);
  753.         textarea->width = width;
  754.         textarea->height = height;
  755.         layout_calculate_descendant_bboxes(textarea);
  756.         box_handle_scrollbars(c, textarea,
  757.                         box_hscrollbar_present(textarea),
  758.                         box_vscrollbar_present(textarea));
  759. }
  760.  
  761.  
  762. /**
  763.  * Move to the start of the word containing the given character position,
  764.  * or the start of the preceding word if already at the start of this one.
  765.  *
  766.  * \param  text     UTF-8 text string
  767.  * \param  poffset  offset of caret within string (updated on exit)
  768.  * \param  pchars   receives the number of characters skipped
  769.  * \return true iff the start of a word was found before/at the string start
  770.  */
  771.  
  772. static bool textinput_word_left(const char *text,
  773.                 size_t *poffset, size_t *pchars)
  774. {
  775.         size_t offset = *poffset;
  776.         bool success = false;
  777.         size_t nchars = 0;
  778.  
  779.         /* Skip any spaces immediately prior to the offset */
  780.         while (offset > 0) {
  781.                 offset = utf8_prev(text, offset);
  782.                 nchars++;
  783.                 if (!isspace(text[offset])) break;
  784.         }
  785.  
  786.         /* Now skip all non-space characters */
  787.         while (offset > 0) {
  788.                 size_t prev = utf8_prev(text, offset);
  789.                 success = true;
  790.                 if (isspace(text[prev]))
  791.                         break;
  792.                 offset = prev;
  793.                 nchars++;
  794.         }
  795.  
  796.         *poffset = offset;
  797.         if (pchars) *pchars = nchars;
  798.  
  799.         return success;
  800. }
  801.  
  802.  
  803. /**
  804.  * Move to the start of the first word following the given character position.
  805.  *
  806.  * \param  text     UTF-8 text string
  807.  * \param  len      length of string in bytes
  808.  * \param  poffset  offset of caret within string (updated on exit)
  809.  * \param  pchars   receives the number of characters skipped
  810.  * \return true iff the start of a word was found before the string end
  811.  */
  812.  
  813. static bool textinput_word_right(const char *text, size_t len,
  814.                 size_t *poffset, size_t *pchars)
  815. {
  816.         size_t offset = *poffset;
  817.         bool success = false;
  818.         size_t nchars = 0;
  819.  
  820.         /* Skip all non-space characters after the offset */
  821.         while (offset < len) {
  822.                 if (isspace(text[offset])) break;
  823.                 offset = utf8_next(text, len, offset);
  824.                 nchars++;
  825.         }
  826.  
  827.         /* Now skip all space characters */
  828.         while (offset < len) {
  829.                 offset = utf8_next(text, len, offset);
  830.                 nchars++;
  831.                 if (offset < len && !isspace(text[offset])) {
  832.                         success = true;
  833.                         break;
  834.                 }
  835.         }
  836.  
  837.         *poffset = offset;
  838.         if (pchars) *pchars = nchars;
  839.  
  840.         return success;
  841. }
  842.  
  843. /**
  844.  * Adjust scroll offsets so that the caret is visible
  845.  *
  846.  * \param c         html content where click ocurred
  847.  * \param textarea  textarea box
  848.  * \return true if a change in scroll offsets has occurred
  849. */
  850.  
  851. static bool textinput_ensure_caret_visible(struct content *c,
  852.                 struct box *textarea)
  853. {
  854.         html_content *html = (html_content *)c;
  855.         int cx, cy;
  856.         int scrollx, scrolly;
  857.  
  858.         assert(textarea->gadget);
  859.  
  860.         scrollx = scrollbar_get_offset(textarea->scroll_x);
  861.         scrolly = scrollbar_get_offset(textarea->scroll_y);
  862.  
  863.         /* Calculate the caret coordinates */
  864.         cx = textarea->gadget->caret_pixel_offset +
  865.                         textarea->gadget->caret_text_box->x;
  866.         cy = textarea->gadget->caret_text_box->y;
  867.  
  868.         /* Ensure they are visible */
  869.         if (textarea->scroll_x == NULL) {
  870.                 scrollx = 0;
  871.         } else if (cx - scrollbar_get_offset(textarea->scroll_x) < 0) {
  872.                 scrollx = cx;
  873.         } else if (cx > scrollbar_get_offset(textarea->scroll_x) +
  874.                         textarea->width) {
  875.                 scrollx = cx - textarea->width;
  876.         }
  877.  
  878.         if (textarea->scroll_y == NULL) {
  879.                 scrolly = 0;
  880.         } else if (cy - scrollbar_get_offset(textarea->scroll_y) < 0) {
  881.                 scrolly = cy;
  882.         } else if (cy + textarea->gadget->caret_text_box->height >
  883.                         scrollbar_get_offset(textarea->scroll_y) +
  884.                         textarea->height) {
  885.                 scrolly = (cy + textarea->gadget->caret_text_box->height) -
  886.                                 textarea->height;
  887.         }
  888.  
  889.         if ((scrollx == scrollbar_get_offset(textarea->scroll_x)) &&
  890.                         (scrolly == scrollbar_get_offset(textarea->scroll_y)))
  891.                 return false;
  892.  
  893.         if (textarea->scroll_x != NULL) {
  894.                 html->scrollbar = textarea->scroll_x;
  895.                 scrollbar_set(textarea->scroll_x, scrollx, false);
  896.                 html->scrollbar = NULL;
  897.         }
  898.         if (textarea->scroll_y != NULL) {
  899.                 html->scrollbar = textarea->scroll_x;
  900.                 scrollbar_set(textarea->scroll_y, scrolly, false);
  901.                 html->scrollbar = NULL;
  902.         }
  903.  
  904.         return true;
  905. }
  906.  
  907.  
  908. /**
  909.  * Paste a block of text into a textarea at the
  910.  * current caret position.
  911.  *
  912.  * \param  bw        browser window
  913.  * \param  utf8      pointer to block of text
  914.  * \param  utf8_len  length (bytes) of text block
  915.  * \param  last      true iff this is the last chunk (update screen too)
  916.  * \param  p1        pointer to textarea
  917.  * \param  p2        html content with the text area box
  918.  * \return true iff successful
  919.  */
  920.  
  921. bool textinput_textarea_paste_text(struct browser_window *bw,
  922.                 const char *utf8, unsigned utf8_len, bool last,
  923.                 void *p1, void *p2)
  924. {
  925.         struct box *textarea = p1;
  926.         struct content *c = p2;
  927.         struct box *inline_container =
  928.                         textarea->gadget->caret_inline_container;
  929.         struct box *text_box = textarea->gadget->caret_text_box;
  930.         size_t char_offset = textarea->gadget->caret_box_offset;
  931.         int pixel_offset = textarea->gadget->caret_pixel_offset;
  932.         const char *ep = utf8 + utf8_len;
  933.         const char *p = utf8;
  934.         bool success = true;
  935.         bool update = last;
  936.  
  937.         while (p < ep) {
  938.                 struct box *new_text;
  939.                 unsigned utf8_len;
  940.  
  941.                 while (p < ep) {
  942.                         if (*p == '\n' || *p == '\r') break;
  943.                         p++;
  944.                 }
  945.  
  946.                 utf8_len = p - utf8;
  947.                 if (!textinput_textbox_insert(c, text_box, char_offset,
  948.                                 utf8, utf8_len))
  949.                         return false;
  950.  
  951.                 char_offset += utf8_len;
  952.                 if (p == ep)
  953.                         break;
  954.  
  955.                 new_text = textinput_textarea_insert_break(c, text_box,
  956.                                 char_offset);
  957.                 if (!new_text) {
  958.                         /* we still need to update the screen */
  959.                         update = true;
  960.                         success = false;
  961.                         break;
  962.                 }
  963.  
  964.                 /* place caret at start of new text box */
  965.                 text_box = new_text;
  966.                 char_offset = 0;
  967.  
  968.                 /* handle CR/LF and LF/CR terminations */
  969.                 if ((*p == '\n' && p[1] == '\r') ||
  970.                                 (*p == '\r' && p[1] == '\n'))
  971.                          p++;
  972.                 utf8 = ++p;
  973.         }
  974.  
  975. //      textarea->gadget->caret_inline_container = inline_container;
  976.         textarea->gadget->caret_text_box = text_box;
  977.         textarea->gadget->caret_box_offset = char_offset;
  978.  
  979.         if (update) {
  980.                 int box_x, box_y;
  981.                 plot_font_style_t fstyle;
  982.  
  983.                 /* reflow textarea preserving width and height */
  984.                 textinput_textarea_reflow(c, textarea, inline_container);
  985.                 /* reflowing may have broken our caret offset
  986.                  * this bit should hopefully continue to work if
  987.                  * textarea_reflow is fixed to update the caret itself */
  988.                 char_offset = textarea->gadget->caret_box_offset;
  989.                 text_box = textarea->gadget->caret_text_box;
  990.  
  991.                 while ((char_offset > text_box->length + SPACE_LEN(text_box)) &&
  992.                                 (text_box->next) &&
  993.                                 (text_box->next->type == BOX_TEXT)) {
  994. #ifdef TEXTINPUT_DEBUG
  995.                         LOG(("Caret out of range: Was %d in boxlen %d "
  996.                                         "space %d", char_offset,
  997.                                         text_box->length, SPACE_LEN(text_box)));
  998. #endif
  999.                         char_offset -= text_box->length + SPACE_LEN(text_box);
  1000.                         text_box = text_box->next;
  1001.                 }
  1002.  
  1003.                 /* not sure if this will happen or not...
  1004.                  * but won't stick an assert here as we can recover from it */
  1005.                 if (char_offset > text_box->length) {
  1006. #ifdef TEXTINPUT_DEBUG
  1007.                         LOG(("Caret moved beyond end of line: "
  1008.                                 "Was %d in boxlen %d", char_offset,
  1009.                                 text_box->length));
  1010. #endif
  1011.                         char_offset = text_box->length;
  1012.                 }
  1013.  
  1014.                 textarea->gadget->caret_text_box = text_box;
  1015.                 textarea->gadget->caret_box_offset = char_offset;
  1016.  
  1017.                 font_plot_style_from_css(text_box->style, &fstyle);
  1018.  
  1019.                 nsfont.font_width(&fstyle, text_box->text,
  1020.                                 char_offset, &pixel_offset);
  1021.  
  1022.                 textarea->gadget->caret_pixel_offset = pixel_offset;
  1023.  
  1024.                 box_coords(textarea, &box_x, &box_y);
  1025.                 box_x += scrollbar_get_offset(textarea->scroll_x);
  1026.                 box_y += scrollbar_get_offset(textarea->scroll_y);
  1027.                 textinput_ensure_caret_visible(c, textarea);
  1028.                 box_x -= scrollbar_get_offset(textarea->scroll_x);
  1029.                 box_y -= scrollbar_get_offset(textarea->scroll_y);
  1030.  
  1031.                 browser_window_place_caret(bw,
  1032.                                 box_x + inline_container->x + text_box->x +
  1033.                                 pixel_offset,
  1034.                                 box_y + inline_container->y + text_box->y,
  1035.                                 text_box->height,
  1036.                                 textinput_textarea_callback,
  1037.                                 textinput_textarea_paste_text,
  1038.                                 textinput_textarea_move_caret,
  1039.                                 textarea, c);
  1040.  
  1041.                 html__redraw_a_box((html_content *)c, textarea);
  1042.         }
  1043.  
  1044.         return success;
  1045. }
  1046.  
  1047.  
  1048. /**
  1049.  * Move caret to new position after reformatting
  1050.  *
  1051.  * \param  bw   browser window
  1052.  * \param  p1   pointer textarea box
  1053.  * \param  p2   html content with the text area box
  1054.  * \return none
  1055.  */
  1056.  
  1057. void textinput_textarea_move_caret(struct browser_window *bw,
  1058.                 void *p1, void *p2)
  1059. {
  1060.         struct box *textarea = p1;
  1061.         struct content *c = p2;
  1062.         struct box *inline_container = textarea->gadget->caret_inline_container;
  1063.         struct box *text_box = textarea->gadget->caret_text_box;
  1064.         size_t char_offset = textarea->gadget->caret_box_offset;
  1065.         int pixel_offset;
  1066.         int box_x, box_y;
  1067.         plot_font_style_t fstyle;
  1068.  
  1069.         font_plot_style_from_css(text_box->style, &fstyle);
  1070.  
  1071.         box_coords(textarea, &box_x, &box_y);
  1072.         box_x -= scrollbar_get_offset(textarea->scroll_x);
  1073.         box_y -= scrollbar_get_offset(textarea->scroll_y);
  1074.  
  1075.         nsfont.font_width(&fstyle, text_box->text,
  1076.                         char_offset, &pixel_offset);
  1077.  
  1078.         browser_window_place_caret(bw,
  1079.                         box_x + inline_container->x + text_box->x +
  1080.                         pixel_offset,
  1081.                         box_y + inline_container->y + text_box->y,
  1082.                         text_box->height,
  1083.                         textinput_textarea_callback,
  1084.                         textinput_textarea_paste_text,
  1085.                         textinput_textarea_move_caret,
  1086.                         textarea, c);
  1087. }
  1088.  
  1089.  
  1090. /**
  1091.  * Update display to reflect modified input field
  1092.  *
  1093.  * \param  bw           browser window
  1094.  * \param  input        input field
  1095.  * \param  form_offset
  1096.  * \param  box_offset   offset of caret within text box
  1097.  * \param  to_textarea  caret is to be moved to a textarea
  1098.  * \param  redraw       force redraw even if field hasn't scrolled
  1099.  */
  1100.  
  1101. static void textinput_input_update_display(struct content *c, struct box *input,
  1102.                 unsigned box_offset, bool to_textarea, bool redraw)
  1103. {
  1104.         struct box *text_box = input->children->children;
  1105.         unsigned pixel_offset;
  1106.         int box_x, box_y;
  1107.         int dx;
  1108.         plot_font_style_t fstyle;
  1109.         html_content *html = (html_content *)c;
  1110.  
  1111.         font_plot_style_from_css(text_box->style, &fstyle);
  1112.  
  1113.         if (redraw)
  1114.                 nsfont.font_width(&fstyle, text_box->text, text_box->length,
  1115.                         &text_box->width);
  1116.  
  1117.         box_coords(input, &box_x, &box_y);
  1118.  
  1119.         nsfont.font_width(&fstyle, text_box->text, box_offset,
  1120.                         (int *) &pixel_offset);
  1121.  
  1122.         /* Shift text box horizontally, so caret is visible */
  1123.         dx = text_box->x;
  1124.         text_box->x = 0;
  1125.         if (input->width < text_box->width &&
  1126.                         input->width / 2 < (int) pixel_offset) {
  1127.                 /* Make caret appear in centre of text input */
  1128.                 text_box->x = input->width / 2 - pixel_offset;
  1129.                 /* Clamp if we've shifted too far left */
  1130.                 if (text_box->x < input->width - text_box->width)
  1131.                         text_box->x = input->width - text_box->width;
  1132.         }
  1133.         dx -= text_box->x;
  1134.         input->gadget->caret_pixel_offset = pixel_offset;
  1135.  
  1136.         if (to_textarea) {
  1137.                 /* moving to textarea so need to set these up */
  1138.                 input->gadget->caret_inline_container = input->children;
  1139.                 input->gadget->caret_text_box = text_box;
  1140.         }
  1141.  
  1142.         input->gadget->caret_box_offset = box_offset;
  1143.  
  1144.         browser_window_place_caret(html->bw,
  1145.                         box_x + input->children->x +
  1146.                                         text_box->x + pixel_offset,
  1147.                         box_y + input->children->y + text_box->y,
  1148.                         text_box->height,
  1149.                         /* use the appropriate callback */
  1150.                         to_textarea ? textinput_textarea_callback
  1151.                                         : textinput_input_callback,
  1152.                         to_textarea ? textinput_textarea_paste_text
  1153.                                         : textinput_input_paste_text,
  1154.                         to_textarea ? textinput_textarea_move_caret
  1155.                                         : textinput_input_move_caret,
  1156.                         input, c);
  1157.  
  1158.         if (dx || redraw)
  1159.                 html__redraw_a_box(html, input);
  1160. }
  1161.  
  1162.  
  1163. /**
  1164.  * Key press callback for text areas.
  1165.  *
  1166.  * \param bw   The browser window containing the text area
  1167.  * \param key  The ucs4 character codepoint
  1168.  * \param p1   The text area box
  1169.  * \param p2   The html content with the text area box
  1170.  * \return     true if the keypress is dealt with, false otherwise.  It can
  1171.  *             return true even if it ran out of memory; this just means that
  1172.  *             it would have claimed it if it could.
  1173.  */
  1174. bool textinput_textarea_callback(struct browser_window *bw, uint32_t key,
  1175.                 void *p1, void *p2)
  1176. {
  1177.         struct box *textarea = p1;
  1178.         struct content *c = p2;
  1179.         html_content *html = (html_content *)c;
  1180.         struct box *inline_container =
  1181.                                 textarea->gadget->caret_inline_container;
  1182.         struct box *text_box = textarea->gadget->caret_text_box;
  1183.         struct box *new_text;
  1184.         size_t char_offset = textarea->gadget->caret_box_offset;
  1185.         int pixel_offset = textarea->gadget->caret_pixel_offset;
  1186.         int box_x, box_y;
  1187.         char utf8[6];
  1188.         unsigned int utf8_len;
  1189.         bool scrolled, reflow = false;
  1190.         bool selection_exists = html->sel.defined;
  1191.         plot_font_style_t fstyle;
  1192.  
  1193.         /* box_dump(textarea, 0); */
  1194. #ifdef TEXTINPUT_DEBUG
  1195.         LOG(("key %i at %i in '%.*s'", key, char_offset,
  1196.                         (int) text_box->length, text_box->text));
  1197. #endif
  1198.  
  1199.         box_coords(textarea, &box_x, &box_y);
  1200.         box_x -= scrollbar_get_offset(textarea->scroll_x);
  1201.         box_y -= scrollbar_get_offset(textarea->scroll_y);
  1202.  
  1203.         if (!(key <= 0x001F || (0x007F <= key && key <= 0x009F))) {
  1204.                 /* normal character insertion */
  1205.                 utf8_len = utf8_from_ucs4(key, utf8);
  1206.  
  1207.                 if (!textinput_textbox_insert(c, text_box, char_offset,
  1208.                                 utf8, utf8_len))
  1209.                         return true;
  1210.  
  1211.                 char_offset += utf8_len;
  1212.                 reflow = true;
  1213.  
  1214.         } else switch (key) {
  1215.         case KEY_DELETE_LEFT:
  1216.                 if (selection_exists) {
  1217.                         /* Have a selection; delete it */
  1218.                         textinput_textbox_delete(c, text_box, 0, 0);
  1219.                 } else if (char_offset == 0) {
  1220.                         /* at the start of a text box */
  1221.                         struct box *prev;
  1222.  
  1223.                         if (text_box->prev && text_box->prev->type == BOX_BR) {
  1224.                                 /* previous box is BR: remove it */
  1225.                                 box_unlink_and_free(text_box->prev);
  1226.                         }
  1227.  
  1228.                         /* This needs to be after the BR removal, as that may
  1229.                          * result in no previous box existing */
  1230.                         if (!text_box->prev)
  1231.                                 /* at very beginning of text area: ignore */
  1232.                                 return true;
  1233.  
  1234.                         /* delete space by merging with previous text box */
  1235.                         prev = text_box->prev;
  1236.                         assert(prev->type == BOX_TEXT);
  1237.                         assert(prev->text);
  1238.  
  1239.                         char_offset = prev->length;     /* caret at join */
  1240.  
  1241.                         if (!textinput_textbox_insert(c, prev, prev->length,
  1242.                                         text_box->text, text_box->length))
  1243.                                 return true;
  1244.  
  1245.                         box_unlink_and_free(text_box);
  1246.  
  1247.                         /* place caret at join (see above) */
  1248.                         text_box = prev;
  1249.  
  1250.                 } else {
  1251.                         /* delete a character */
  1252.                         size_t prev_offset = char_offset;
  1253.                         size_t new_offset =
  1254.                                         utf8_prev(text_box->text, char_offset);
  1255.  
  1256.                         if (textinput_textbox_delete(c, text_box, new_offset,
  1257.                                         prev_offset - new_offset))
  1258.                                 char_offset = new_offset;
  1259.                 }
  1260.                 reflow = true;
  1261.                 break;
  1262.  
  1263.         case KEY_DELETE_LINE_START:
  1264.         {
  1265.                 struct box *start_box = textinput_line_start(text_box);
  1266.  
  1267.                 /* Clear the selection, if one exists */
  1268.                 if (selection_exists)
  1269.                         selection_clear(&html->sel, false);
  1270.  
  1271.                 textinput_textarea_cut(c, start_box, 0, text_box,
  1272.                                 char_offset, false);
  1273.                 text_box = start_box;
  1274.                 char_offset = 0;
  1275.                 reflow = true;
  1276.         }
  1277.                 break;
  1278.  
  1279.         case KEY_DELETE_LINE_END:
  1280.         {
  1281.                 struct box *end_box = textinput_line_end(text_box);
  1282.  
  1283.                 /* Clear the selection, if one exists */
  1284.                 if (selection_exists)
  1285.                         selection_clear(&html->sel, false);
  1286.  
  1287.                 if (end_box != text_box ||
  1288.                         char_offset < text_box->length + SPACE_LEN(text_box)) {
  1289.                         /* there's something at the end of the line to delete */
  1290.                         textinput_textarea_cut(c, text_box,
  1291.                                         char_offset, end_box,
  1292.                                         end_box->length + SPACE_LEN(end_box),
  1293.                                         false);
  1294.                         reflow = true;
  1295.                         break;
  1296.                 }
  1297.         }
  1298.         /* no break */
  1299.         case KEY_DELETE_RIGHT:  /* delete to right */
  1300.                 if (selection_exists) {
  1301.                         /* Delete selection */
  1302.                         textinput_textbox_delete(c, text_box, 0, 0);
  1303.                 } else if (char_offset >= text_box->length) {
  1304.                         /* at the end of a text box */
  1305.                         struct box *next;
  1306.  
  1307.                         if (text_box->next && text_box->next->type == BOX_BR) {
  1308.                                 /* next box is a BR: remove it */
  1309.                                 box_unlink_and_free(text_box->next);
  1310.                         }
  1311.  
  1312.                         /* This test is after the BR removal, as that may
  1313.                          * result in no subsequent box being present */
  1314.                         if (!text_box->next)
  1315.                                 /* at very end of text area: ignore */
  1316.                                 return true;
  1317.  
  1318.                         /* delete space by merging with next text box */
  1319.  
  1320.                         next = text_box->next;
  1321.                         assert(next->type == BOX_TEXT);
  1322.                         assert(next->text);
  1323.  
  1324.                         if (!textinput_textbox_insert(c, text_box,
  1325.                                         text_box->length,
  1326.                                         next->text, next->length))
  1327.                                 return true;
  1328.  
  1329.                         box_unlink_and_free(next);
  1330.  
  1331.                         /* leave caret at join */
  1332.                 } else {
  1333.                         /* delete a character */
  1334.                         size_t next_offset = utf8_next(text_box->text,
  1335.                                         text_box->length, char_offset);
  1336.  
  1337.                         textinput_textbox_delete(c, text_box, char_offset,
  1338.                                         next_offset - char_offset);
  1339.                 }
  1340.                 reflow = true;
  1341.                 break;
  1342.  
  1343.         case KEY_NL:
  1344.         case KEY_CR:    /* paragraph break */
  1345.                 if (selection_exists) {
  1346.                         /* If we have a selection, then delete it,
  1347.                          * so it's replaced by the break */
  1348.                         textinput_textbox_delete(c, text_box, 0, 0);
  1349.                 }
  1350.  
  1351.                 new_text = textinput_textarea_insert_break(c, text_box,
  1352.                                 char_offset);
  1353.                 if (!new_text)
  1354.                         return true;
  1355.  
  1356.                 /* place caret at start of new text box */
  1357.                 text_box = new_text;
  1358.                 char_offset = 0;
  1359.  
  1360.                 reflow = true;
  1361.                 break;
  1362.  
  1363.         case KEY_CUT_LINE:
  1364.         {
  1365.                 struct box *start_box = textinput_line_start(text_box);
  1366.                 struct box *end_box = textinput_line_end(text_box);
  1367.  
  1368.                 /* Clear the selection, if one exists */
  1369.                 if (selection_exists)
  1370.                         selection_clear(&html->sel, false);
  1371.  
  1372.                 textinput_textarea_cut(c, start_box, 0, end_box,
  1373.                                 end_box->length, false);
  1374.  
  1375.                 text_box = start_box;
  1376.                 char_offset = 0;
  1377.                 reflow = true;
  1378.         }
  1379.                 break;
  1380.  
  1381.         case KEY_PASTE:
  1382.         {
  1383.                 char *buff = NULL;
  1384.                 size_t buff_len;
  1385.                 bool success;
  1386.  
  1387.                 gui_get_clipboard(&buff, &buff_len);
  1388.                 if (buff == NULL)
  1389.                         return false;
  1390.  
  1391.                 success = browser_window_paste_text(bw, buff, buff_len, true);
  1392.                 free(buff);
  1393.  
  1394.                 return success;
  1395.         }
  1396.  
  1397.         case KEY_CUT_SELECTION:
  1398.         {
  1399.                 size_t start_idx, end_idx;
  1400.                 struct box *start_box =
  1401.                                 selection_get_start(&html->sel, &start_idx);
  1402.                 struct box *end_box = selection_get_end(&html->sel, &end_idx);
  1403.  
  1404.                 if (start_box && end_box) {
  1405.                         selection_clear(&html->sel, false);
  1406.                         textinput_textarea_cut(c, start_box, start_idx,
  1407.                                         end_box, end_idx, true);
  1408.                         text_box = start_box;
  1409.                         char_offset = start_idx;
  1410.                         reflow = true;
  1411.                 }
  1412.         }
  1413.                 break;
  1414.  
  1415.         case KEY_RIGHT:
  1416.                 if (selection_exists) {
  1417.                         /* In selection, move caret to end */
  1418.                         text_box = selection_get_end(&html->sel, &char_offset);
  1419.                 } else if (char_offset < text_box->length) {
  1420.                         /* Within-box movement */
  1421.                         char_offset = utf8_next(text_box->text,
  1422.                                         text_box->length, char_offset);
  1423.                 } else {
  1424.                         /* Between-box movement */
  1425.                         if (!text_box->next)
  1426.                                 /* at end of text area: ignore */
  1427.                                 return true;
  1428.  
  1429.                         text_box = text_box->next;
  1430.                         if (text_box->type == BOX_BR)
  1431.                                 text_box = text_box->next;
  1432.                         char_offset = 0;
  1433.                 }
  1434.                 break;
  1435.  
  1436.         case KEY_LEFT:
  1437.                 if (selection_exists) {
  1438.                         /* In selection, move caret to start */
  1439.                         text_box = selection_get_start(&html->sel, &char_offset);
  1440.                 } else if (char_offset > 0) {
  1441.                         /* Within-box movement */
  1442.                         char_offset = utf8_prev(text_box->text, char_offset);
  1443.                 } else {
  1444.                         /* Between-box movement */
  1445.                         if (!text_box->prev)
  1446.                                 /* at start of text area: ignore */
  1447.                                 return true;
  1448.  
  1449.                         text_box = text_box->prev;
  1450.                         if (text_box->type == BOX_BR)
  1451.                                 text_box = text_box->prev;
  1452.                         char_offset = text_box->length;
  1453.                 }
  1454.                 break;
  1455.  
  1456.         case KEY_UP:
  1457.                 selection_clear(&html->sel, true);
  1458.                 textinput_textarea_click(c, BROWSER_MOUSE_CLICK_1,
  1459.                                 textarea,
  1460.                                 box_x, box_y,
  1461.                                 text_box->x + pixel_offset,
  1462.                                 inline_container->y + text_box->y - 1);
  1463.                 return true;
  1464.  
  1465.         case KEY_DOWN:
  1466.                 selection_clear(&html->sel, true);
  1467.                 textinput_textarea_click(c, BROWSER_MOUSE_CLICK_1,
  1468.                                 textarea,
  1469.                                 box_x, box_y,
  1470.                                 text_box->x + pixel_offset,
  1471.                                 inline_container->y + text_box->y +
  1472.                                 text_box->height + 1);
  1473.                 return true;
  1474.  
  1475.         case KEY_LINE_START:
  1476.                 text_box = textinput_line_start(text_box);
  1477.                 char_offset = 0;
  1478.                 break;
  1479.  
  1480.         case KEY_LINE_END:
  1481.                 text_box = textinput_line_end(text_box);
  1482.                 char_offset = text_box->length;
  1483.                 break;
  1484.  
  1485.         case KEY_TEXT_START:
  1486.                 assert(text_box->parent);
  1487.  
  1488.                 /* place caret at start of first box */
  1489.                 text_box = text_box->parent->children;
  1490.                 char_offset = 0;
  1491.                 break;
  1492.  
  1493.         case KEY_TEXT_END:
  1494.                 assert(text_box->parent);
  1495.  
  1496.                 /* place caret at end of last box */
  1497.                 text_box = text_box->parent->last;
  1498.                 char_offset = text_box->length;
  1499.                 break;
  1500.  
  1501.         case KEY_WORD_LEFT:
  1502.         {
  1503.                 bool start_of_word;
  1504.                 /* if there is a selection, caret should stay at beginning */
  1505.                 if (selection_exists)
  1506.                         break;
  1507.  
  1508.                 start_of_word = (char_offset <= 0 ||
  1509.                                 isspace(text_box->text[char_offset - 1]));
  1510.  
  1511.                 while (!textinput_word_left(text_box->text,
  1512.                                 &char_offset, NULL)) {
  1513.                         struct box *prev = NULL;
  1514.  
  1515.                         assert(char_offset == 0);
  1516.  
  1517.                         if (start_of_word) {
  1518.                                 /* find the preceding non-BR box */
  1519.                                 prev = text_box->prev;
  1520.                                 if (prev && prev->type == BOX_BR)
  1521.                                         prev = prev->prev;
  1522.                         }
  1523.  
  1524.                         if (!prev) {
  1525.                                 /* just stay at the start of this box */
  1526.                                 break;
  1527.                         }
  1528.  
  1529.                         assert(prev->type == BOX_TEXT);
  1530.  
  1531.                         text_box = prev;
  1532.                         char_offset = prev->length;
  1533.                 }
  1534.         }
  1535.                 break;
  1536.  
  1537.         case KEY_WORD_RIGHT:
  1538.         {
  1539.                 bool in_word;
  1540.                 /* if there is a selection, caret should move to the end */
  1541.                 if (selection_exists) {
  1542.                         text_box = selection_get_end(&html->sel, &char_offset);
  1543.                         break;
  1544.                 }
  1545.  
  1546.                 in_word = (char_offset < text_box->length &&
  1547.                                 !isspace(text_box->text[char_offset]));
  1548.  
  1549.                 while (!textinput_word_right(text_box->text, text_box->length,
  1550.                                 &char_offset, NULL)) {
  1551.                         struct box *next = text_box->next;
  1552.  
  1553.                         /* find the next non-BR box */
  1554.                         if (next && next->type == BOX_BR)
  1555.                                 next = next->next;
  1556.  
  1557.                         if (!next) {
  1558.                                 /* just stay at the end of this box */
  1559.                                 char_offset = text_box->length;
  1560.                                 break;
  1561.                         }
  1562.  
  1563.                         assert(next->type == BOX_TEXT);
  1564.  
  1565.                         text_box = next;
  1566.                         char_offset = 0;
  1567.  
  1568.                         if (in_word && text_box->length > 0 &&
  1569.                                         !isspace(text_box->text[0])) {
  1570.                                 /* just stay at the start of this box */
  1571.                                 break;
  1572.                         }
  1573.                 }
  1574.         }
  1575.                 break;
  1576.  
  1577.         case KEY_PAGE_UP:
  1578.         {
  1579.                 int nlines = (textarea->height / text_box->height) - 1;
  1580.  
  1581.                 while (nlines-- > 0)
  1582.                         text_box = textinput_line_above(text_box);
  1583.  
  1584.                 if (char_offset > text_box->length)
  1585.                         char_offset = text_box->length;
  1586.         }
  1587.                 break;
  1588.  
  1589.         case KEY_PAGE_DOWN:
  1590.         {
  1591.                 int nlines = (textarea->height / text_box->height) - 1;
  1592.  
  1593.                 while (nlines-- > 0)
  1594.                         text_box = textinput_line_below(text_box);
  1595.  
  1596.                 /* vague attempt to keep the caret at the same horizontal
  1597.                  * position, given that the code currently cannot support it
  1598.                  * being beyond the end of a line */
  1599.                 if (char_offset > text_box->length)
  1600.                         char_offset = text_box->length;
  1601.         }
  1602.                 break;
  1603.  
  1604.         default:
  1605.                 return false;
  1606.         }
  1607.  
  1608.         /*
  1609.          box_dump(textarea, 0);
  1610.          for (struct box *t = inline_container->children; t; t = t->next) {
  1611.                 assert(t->type == BOX_TEXT);
  1612.                 assert(t->text);
  1613.                 assert(t->parent == inline_container);
  1614.                 if (t->next) assert(t->next->prev == t);
  1615.                 if (t->prev) assert(t->prev->next == t);
  1616.                 if (!t->next) {
  1617.                         assert(inline_container->last == t);
  1618.                         break;
  1619.                 }
  1620.                 if (t->next->type == BOX_BR) {
  1621.                         assert(t->next->next);
  1622.                         t = t->next;
  1623.                 }
  1624.         } */
  1625.  
  1626.         if (reflow)
  1627.                 textinput_textarea_reflow(c, textarea, inline_container);
  1628.  
  1629.         if (text_box->length + SPACE_LEN(text_box) <= char_offset) {
  1630.                 if (text_box->next && text_box->next->type == BOX_TEXT) {
  1631.                         /* the text box has been split when reflowing and
  1632.                            the caret is in the second part */
  1633.                         char_offset -= (text_box->length + SPACE_LEN(text_box));
  1634.                         text_box = text_box->next;
  1635.                         assert(text_box);
  1636.                         assert(char_offset <= text_box->length);
  1637.                         /* Scroll back to the left */
  1638.                         if (textarea->scroll_x != NULL) {
  1639.                                 box_x += scrollbar_get_offset(
  1640.                                                 textarea->scroll_x);
  1641.                                 scrollbar_set(textarea->scroll_x, 0, false);
  1642.                         }
  1643.                 } else {
  1644.                         assert(!text_box->next ||
  1645.                                         (text_box->next &&
  1646.                                         text_box->next->type == BOX_BR));
  1647.  
  1648.                         char_offset = text_box->length + SPACE_LEN(text_box);
  1649.                 }
  1650.         }
  1651.  
  1652.         font_plot_style_from_css(text_box->style, &fstyle);
  1653.  
  1654.         nsfont.font_width(&fstyle, text_box->text, char_offset, &pixel_offset);
  1655.  
  1656.         selection_clear(&html->sel, true);
  1657.  
  1658.         textarea->gadget->caret_inline_container = inline_container;
  1659.         textarea->gadget->caret_text_box = text_box;
  1660.         textarea->gadget->caret_box_offset = char_offset;
  1661.         textarea->gadget->caret_pixel_offset = pixel_offset;
  1662.  
  1663.         box_x += scrollbar_get_offset(textarea->scroll_x);
  1664.         box_y += scrollbar_get_offset(textarea->scroll_y);
  1665.         scrolled = textinput_ensure_caret_visible(c, textarea);
  1666.         box_x -= scrollbar_get_offset(textarea->scroll_x);
  1667.         box_y -= scrollbar_get_offset(textarea->scroll_y);
  1668.  
  1669.         browser_window_place_caret(bw,
  1670.                         box_x + inline_container->x + text_box->x +
  1671.                         pixel_offset,
  1672.                         box_y + inline_container->y + text_box->y,
  1673.                         text_box->height,
  1674.                         textinput_textarea_callback,
  1675.                         textinput_textarea_paste_text,
  1676.                         textinput_textarea_move_caret,
  1677.                         textarea, c);
  1678.  
  1679.         if (scrolled || reflow)
  1680.                 html__redraw_a_box(html, textarea);
  1681.  
  1682.         return true;
  1683. }
  1684.  
  1685.  
  1686. /**
  1687.  * Handle clicks in a text area by placing the caret.
  1688.  *
  1689.  * \param  c         html content where click occurred
  1690.  * \param  mouse     state of mouse buttons and modifier keys
  1691.  * \param  textarea  textarea box
  1692.  * \param  box_x     position of textarea in global document coordinates
  1693.  * \param  box_y     position of textarea in global document coordinates
  1694.  * \param  x         coordinate of click relative to textarea
  1695.  * \param  y         coordinate of click relative to textarea
  1696.  */
  1697. void textinput_textarea_click(struct content *c, browser_mouse_state mouse,
  1698.                 struct box *textarea, int box_x, int box_y, int x, int y)
  1699. {
  1700.         /* A textarea is an INLINE_BLOCK containing a single
  1701.          * INLINE_CONTAINER, which contains the text as runs of TEXT
  1702.          * separated by BR. There is at least one TEXT. The first and
  1703.          * last boxes are TEXT. Consecutive BR may not be present. These
  1704.          * constraints are satisfied by using a 0-length TEXT for blank
  1705.          * lines. */
  1706.  
  1707.         int char_offset = 0, pixel_offset = 0;
  1708.         struct box *inline_container = textarea->children;
  1709.         struct box *text_box;
  1710.         bool scrolled;
  1711.         html_content *html = (html_content *)c;
  1712.  
  1713.         text_box = textinput_textarea_get_position(textarea, x, y,
  1714.                         &char_offset, &pixel_offset);
  1715.  
  1716.         textarea->gadget->caret_inline_container = inline_container;
  1717.         textarea->gadget->caret_text_box = text_box;
  1718.         textarea->gadget->caret_box_offset = char_offset;
  1719.         textarea->gadget->caret_pixel_offset = pixel_offset;
  1720.  
  1721.         box_x += scrollbar_get_offset(textarea->scroll_x);
  1722.         box_y += scrollbar_get_offset(textarea->scroll_y);
  1723.         scrolled = textinput_ensure_caret_visible(c, textarea);
  1724.         box_x -= scrollbar_get_offset(textarea->scroll_x);
  1725.         box_y -= scrollbar_get_offset(textarea->scroll_y);
  1726.  
  1727.         browser_window_place_caret(html->bw,
  1728.                         box_x + inline_container->x + text_box->x +
  1729.                         pixel_offset,
  1730.                         box_y + inline_container->y + text_box->y,
  1731.                         text_box->height,
  1732.                         textinput_textarea_callback,
  1733.                         textinput_textarea_paste_text,
  1734.                         textinput_textarea_move_caret,
  1735.                         textarea, c);
  1736.  
  1737.         if (scrolled)
  1738.                 html__redraw_a_box(html, textarea);
  1739. }
  1740.  
  1741.  
  1742. /**
  1743.  * Paste a block of text into an input field at the caret position.
  1744.  *
  1745.  * \param  bw        browser window
  1746.  * \param  utf8      pointer to block of text
  1747.  * \param  utf8_len  length (bytes) of text block
  1748.  * \param  last      true iff this is the last chunk (update screen too)
  1749.  * \param  p1        pointer to input box
  1750.  * \param  p2        html content with the input box
  1751.  * \return true iff successful
  1752.  */
  1753.  
  1754. bool textinput_input_paste_text(struct browser_window *bw,
  1755.                 const char *utf8, unsigned utf8_len, bool last,
  1756.                 void *p1, void *p2)
  1757. {
  1758.         struct box *input = p1;
  1759.         struct content *c = p2;
  1760.         struct box *text_box = input->children->children;
  1761.         size_t box_offset = input->gadget->caret_box_offset;
  1762.         unsigned int nchars = utf8_length(input->gadget->value);
  1763.         const char *ep = utf8 + utf8_len;
  1764.         const char *p = utf8;
  1765.         bool success = true;
  1766.         bool update = last;
  1767.  
  1768.         /* keep adding chars until we've run out or would exceed
  1769.                 the maximum length of the field (in which we silently
  1770.                 ignore all others)
  1771.         */
  1772.         while (p < ep && nchars < input->gadget->maxlength) {
  1773.                 char buf[80 + 6];
  1774.                 int nbytes = 0;
  1775.  
  1776.                 /* how many more chars can we insert in one go? */
  1777.                 while (p < ep && nbytes < 80 &&
  1778.                                 nchars < input->gadget->maxlength &&
  1779.                                 *p != '\n' && *p != '\r') {
  1780.                         unsigned len = utf8_next(p, ep - p, 0);
  1781.                         if (*p == ' ')
  1782.                                 nbytes += utf8_from_ucs4(160, &buf[nbytes]);
  1783.                         else {
  1784.                                 memcpy(&buf[nbytes], p, len);
  1785.                                 nbytes += len;
  1786.                         }
  1787.  
  1788.                         p += len;
  1789.                         nchars++;
  1790.                 }
  1791.  
  1792.                 if (!textinput_textbox_insert(c, text_box, box_offset,
  1793.                                 buf, nbytes)) {
  1794.                         /* we still need to update the screen */
  1795.                         update = true;
  1796.                         success = false;
  1797.                         break;
  1798.                 }
  1799.                 box_offset += nbytes;
  1800.                 /* Keep caret_form_offset in sync -- textbox_insert uses this
  1801.                  * to determine where to insert into the gadget's value */
  1802.                 input->gadget->caret_form_offset += nbytes;
  1803.  
  1804.                 /* handle CR/LF and LF/CR terminations */
  1805.                 if (*p == '\n') {
  1806.                         p++;
  1807.                         if (*p == '\r') p++;
  1808.                 }
  1809.                 else if (*p == '\r') {
  1810.                         p++;
  1811.                         if (*p == '\n') p++;
  1812.                 }
  1813.         }
  1814.  
  1815.         if (update)
  1816.                 textinput_input_update_display(c, input, box_offset,
  1817.                                 false, true);
  1818.  
  1819.         return success;
  1820. }
  1821.  
  1822.  
  1823. /**
  1824.  * Move caret to new position after reformatting
  1825.  *
  1826.  * \param  bw   browser window
  1827.  * \param  p1   pointer to text input box
  1828.  * \param  p2   html content with the input box
  1829.  * \return none
  1830.  */
  1831.  
  1832. void textinput_input_move_caret(struct browser_window *bw,
  1833.                 void *p1, void *p2)
  1834. {
  1835.         struct box *input = (struct box *)p1;
  1836.         struct content *c = p2;
  1837.         struct box *text_box = input->children->children;
  1838.         unsigned int box_offset = input->gadget->caret_box_offset;
  1839.         int pixel_offset;
  1840.         int box_x, box_y;
  1841.         plot_font_style_t fstyle;
  1842.  
  1843.         font_plot_style_from_css(text_box->style, &fstyle);
  1844.  
  1845.         box_coords(input, &box_x, &box_y);
  1846.  
  1847.         nsfont.font_width(&fstyle, text_box->text, box_offset,
  1848.                         &pixel_offset);
  1849.  
  1850.         browser_window_place_caret(bw,
  1851.                         box_x + input->children->x +
  1852.                                         text_box->x + pixel_offset,
  1853.                         box_y + input->children->y + text_box->y,
  1854.                         text_box->height,
  1855.                         textinput_input_callback,
  1856.                         textinput_input_paste_text,
  1857.                         textinput_input_move_caret,
  1858.                         input, c);
  1859. }
  1860.  
  1861. /**
  1862.  * Key press callback for text or password input boxes.
  1863.  *
  1864.  * \param bw   The browser window containing the input box
  1865.  * \param key  The UCS4 character codepoint
  1866.  * \param p1   The input box
  1867.  * \param p2   The html content with the input box
  1868.  * \return     true if the keypress is dealt with, false otherwise.  It can
  1869.  *             return true even if it ran out of memory; this just means that
  1870.  *             it would have claimed it if it could.
  1871.  */
  1872. bool textinput_input_callback(struct browser_window *bw, uint32_t key,
  1873.                 void *p1, void *p2)
  1874. {
  1875.         struct box *input = (struct box *)p1;
  1876.         struct content *c = p2;
  1877.         html_content *html = (html_content *)c;
  1878.         struct box *text_box = input->children->children;
  1879.         size_t box_offset = input->gadget->caret_box_offset;
  1880.         size_t end_offset;
  1881.         int box_x, box_y;
  1882.         struct form* form = input->gadget->form;
  1883.         bool changed = false;
  1884.         char utf8[6];
  1885.         unsigned int utf8_len;
  1886.         bool to_textarea = false;
  1887.         bool selection_exists = html->sel.defined;
  1888.  
  1889.         input->gadget->caret_form_offset =
  1890.                         textinput_get_form_offset(input, text_box, box_offset);
  1891.  
  1892.         /* update the form offset */
  1893.         input->gadget->caret_form_offset =
  1894.                         textinput_get_form_offset(input, text_box, box_offset);
  1895.  
  1896.         selection_get_end(&html->sel, &end_offset);
  1897.  
  1898.         box_coords(input, &box_x, &box_y);
  1899.  
  1900.         /* normal character insertion */
  1901.         if (!(key <= 0x001F || (0x007F <= key && key <= 0x009F))) {
  1902.                 /* have we exceeded max length of input? */
  1903.                 utf8_len = utf8_length(input->gadget->value);
  1904.                 if (utf8_len >= input->gadget->maxlength)
  1905.                         return true;
  1906.  
  1907.                 utf8_len = utf8_from_ucs4(key, utf8);
  1908.  
  1909.                 if (!textinput_textbox_insert(c, text_box, box_offset,
  1910.                                 utf8, utf8_len))
  1911.                         return true;
  1912.  
  1913.                 box_offset += utf8_len;
  1914.  
  1915.                 changed = true;
  1916.  
  1917.         } else switch (key) {
  1918.         case KEY_DELETE_LEFT:
  1919.         {
  1920.                 int prev_offset, new_offset;
  1921.  
  1922.                 if (selection_exists) {
  1923.                         textinput_textbox_delete(c, text_box, 0, 0);
  1924.                 } else {
  1925.                         /* Can't delete left from text box start */
  1926.                         if (box_offset == 0)
  1927.                                 return true;
  1928.  
  1929.                         prev_offset = box_offset;
  1930.                         new_offset = utf8_prev(text_box->text, box_offset);
  1931.  
  1932.                         if (textinput_textbox_delete(c, text_box, new_offset,
  1933.                                         prev_offset - new_offset))
  1934.                                 box_offset = new_offset;
  1935.                 }
  1936.  
  1937.                 changed = true;
  1938.         }
  1939.                 break;
  1940.  
  1941.         case KEY_DELETE_RIGHT:
  1942.         {
  1943.                 unsigned next_offset;
  1944.  
  1945.                 if (selection_exists) {
  1946.                         textinput_textbox_delete(c, text_box, 0, 0);
  1947.                 } else {
  1948.                         /* Can't delete right from text box end */
  1949.                         if (box_offset >= text_box->length)
  1950.                                 return true;
  1951.  
  1952.                         /* Go to the next valid UTF-8 character */
  1953.                         next_offset = utf8_next(text_box->text,
  1954.                                         text_box->length, box_offset);
  1955.  
  1956.                         textinput_textbox_delete(c, text_box, box_offset,
  1957.                                         next_offset - box_offset);
  1958.                 }
  1959.  
  1960.                 changed = true;
  1961.         }
  1962.                 break;
  1963.  
  1964.         case KEY_TAB:
  1965.         {
  1966.                 struct form_control *next_input;
  1967.                 /* Find next text entry field that is actually
  1968.                  * displayed (i.e. has an associated box) */
  1969.                 for (next_input = input->gadget->next;
  1970.                                 next_input &&
  1971.                                 ((next_input->type != GADGET_TEXTBOX &&
  1972.                                 next_input->type != GADGET_TEXTAREA &&
  1973.                                 next_input->type != GADGET_PASSWORD) ||
  1974.                                 !next_input->box);
  1975.                                 next_input = next_input->next)
  1976.                         ;
  1977.                 if (!next_input)
  1978.                         return true;
  1979.  
  1980.                 input = next_input->box;
  1981.                 box_offset = 0;
  1982.                 to_textarea = next_input->type == GADGET_TEXTAREA;
  1983.         }
  1984.                 break;
  1985.  
  1986.         case KEY_NL:
  1987.         case KEY_CR:    /* Return/Enter hit */
  1988.                 selection_clear(&html->sel, true);
  1989.  
  1990.                 if (form)
  1991.                         form_submit(content_get_url(c), bw, form, 0);
  1992.                 return true;
  1993.  
  1994.         case KEY_SHIFT_TAB:
  1995.         {
  1996.                 struct form_control *prev_input;
  1997.                 /* Find previous text entry field that is actually
  1998.                  * displayed (i.e. has an associated box) */
  1999.                 for (prev_input = input->gadget->prev;
  2000.                                 prev_input &&
  2001.                                 ((prev_input->type != GADGET_TEXTBOX &&
  2002.                                 prev_input->type != GADGET_TEXTAREA &&
  2003.                                 prev_input->type != GADGET_PASSWORD) ||
  2004.                                 !prev_input->box);
  2005.                                 prev_input = prev_input->prev)
  2006.                         ;
  2007.                 if (!prev_input)
  2008.                         return true;
  2009.  
  2010.                 input = prev_input->box;
  2011.                 box_offset = 0;
  2012.                 to_textarea = prev_input->type == GADGET_TEXTAREA;
  2013.         }
  2014.                 break;
  2015.  
  2016.         case KEY_CUT_LINE:
  2017.                 /* Clear the selection, if one exists */
  2018.                 if (selection_exists)
  2019.                         selection_clear(&html->sel, false);
  2020.  
  2021.                 textinput_textarea_cut(c, text_box, 0, text_box,
  2022.                                 text_box->length, false);
  2023.                 box_offset = 0;
  2024.  
  2025.                 changed = true;
  2026.                 break;
  2027.  
  2028.         case KEY_PASTE:
  2029.         {
  2030.                 char *buff = NULL;
  2031.                 size_t buff_len;
  2032.                 bool success;
  2033.  
  2034.                 gui_get_clipboard(&buff, &buff_len);
  2035.                 if (buff == NULL)
  2036.                         return false;
  2037.  
  2038.                 success = browser_window_paste_text(bw, buff, buff_len, true);
  2039.                 free(buff);
  2040.  
  2041.                 return success;
  2042.         }
  2043.  
  2044.         case KEY_CUT_SELECTION:
  2045.         {
  2046.                 size_t start_idx, end_idx;
  2047.                 struct box *start_box =
  2048.                                 selection_get_start(&html->sel, &start_idx);
  2049.                 struct box *end_box = selection_get_end(&html->sel, &end_idx);
  2050.  
  2051.                 if (start_box && end_box) {
  2052.                         selection_clear(&html->sel, false);
  2053.                         textinput_textarea_cut(c, start_box, start_idx,
  2054.                                         end_box, end_idx, true);
  2055.  
  2056.                         box_offset = start_idx;
  2057.                         changed = true;
  2058.                 }
  2059.         }
  2060.                 break;
  2061.  
  2062.         case KEY_RIGHT:
  2063.                 if (selection_exists) {
  2064.                         box_offset = end_offset;
  2065.                         break;
  2066.                 }
  2067.  
  2068.                 if (box_offset < text_box->length) {
  2069.                         /* Go to the next valid UTF-8 character */
  2070.                         box_offset = utf8_next(text_box->text,
  2071.                                         text_box->length, box_offset);
  2072.                 }
  2073.  
  2074.                 break;
  2075.  
  2076.         case KEY_LEFT:
  2077.                 /* If there is a selection, caret should remain at start */
  2078.                 if (selection_exists)
  2079.                         break;
  2080.  
  2081.                 /* Go to the previous valid UTF-8 character */
  2082.                 box_offset = utf8_prev(text_box->text, box_offset);
  2083.                 break;
  2084.  
  2085.         case KEY_LINE_START:
  2086.                 box_offset = 0;
  2087.                 break;
  2088.  
  2089.         case KEY_LINE_END:
  2090.                 box_offset = text_box->length;
  2091.                 break;
  2092.  
  2093.         case KEY_WORD_LEFT:
  2094.                 /* If there is a selection, caret should remain at start */
  2095.                 if (selection_exists)
  2096.                         break;
  2097.  
  2098.                 if (!textinput_word_left(text_box->text, &box_offset, NULL))
  2099.                         box_offset = 0;
  2100.  
  2101.                 break;
  2102.  
  2103.         case KEY_WORD_RIGHT:
  2104.                 if (selection_exists) {
  2105.                         box_offset = end_offset;
  2106.                         break;
  2107.                 }
  2108.  
  2109.                 if (!textinput_word_right(text_box->text, text_box->length,
  2110.                                 &box_offset, NULL))
  2111.                         box_offset = text_box->length;
  2112.  
  2113.                 break;
  2114.  
  2115.         case KEY_DELETE_LINE_START:
  2116.                 if (selection_exists)
  2117.                         selection_clear(&html->sel, true);
  2118.  
  2119.                 if (box_offset == 0)
  2120.                         return true;
  2121.  
  2122.                 textinput_textarea_cut(c, text_box, 0, text_box,
  2123.                                 box_offset, false);
  2124.                 box_offset = 0;
  2125.  
  2126.                 changed = true;
  2127.                 break;
  2128.  
  2129.         case KEY_DELETE_LINE_END:
  2130.                 if (selection_exists)
  2131.                         selection_clear(&html->sel, true);
  2132.  
  2133.                 if (box_offset >= text_box->length)
  2134.                         return true;
  2135.  
  2136.                 textinput_textarea_cut(c, text_box, box_offset,
  2137.                                 text_box, text_box->length, false);
  2138.  
  2139.                 changed = true;
  2140.                 break;
  2141.  
  2142.         default:
  2143.                 return false;
  2144.         }
  2145.  
  2146.         selection_clear(&html->sel, true);
  2147.         textinput_input_update_display(c, input, box_offset,
  2148.                         to_textarea, changed);
  2149.  
  2150.         return true;
  2151. }
  2152.  
  2153.  
  2154. /**
  2155.  * Handle clicks in a text or password input box by placing the caret.
  2156.  *
  2157.  * \param  bw     browser window where click occurred
  2158.  * \param  input  input box
  2159.  * \param  box_x  position of input in global document coordinates
  2160.  * \param  box_y  position of input in global document coordinates
  2161.  * \param  x      coordinate of click relative to input
  2162.  * \param  y      coordinate of click relative to input
  2163.  */
  2164. void textinput_input_click(struct content *c, struct box *input,
  2165.                 int box_x, int box_y, int x, int y)
  2166. {
  2167.         size_t char_offset = 0;
  2168.         int pixel_offset = 0, dx = 0;
  2169.         struct box *text_box = input->children->children;
  2170.         plot_font_style_t fstyle;
  2171.         html_content *html = (html_content *)c;
  2172.  
  2173.         font_plot_style_from_css(text_box->style, &fstyle);
  2174.  
  2175.         nsfont.font_position_in_string(&fstyle, text_box->text,
  2176.                         text_box->length, x - text_box->x,
  2177.                         &char_offset, &pixel_offset);
  2178.         assert(char_offset <= text_box->length);
  2179.  
  2180.         /* Shift the text box horizontally to ensure that the
  2181.          * caret position is visible, and ideally centred */
  2182.         text_box->x = 0;
  2183.         if ((input->width < text_box->width) &&
  2184.                         (input->width / 2 < pixel_offset)) {
  2185.                 dx = text_box->x;
  2186.                 /* Move left so caret is centred */
  2187.                 text_box->x = input->width / 2 - pixel_offset;
  2188.                 /* Clamp, so text box's right hand edge coincides
  2189.                  * with the input's right hand edge */
  2190.                 if (text_box->x < input->width - text_box->width)
  2191.                         text_box->x = input->width - text_box->width;
  2192.                 dx -= text_box->x;
  2193.         }
  2194.         input->gadget->caret_box_offset = char_offset;
  2195.         input->gadget->caret_form_offset =
  2196.                         textinput_get_form_offset(input, text_box, char_offset);
  2197.         input->gadget->caret_pixel_offset = pixel_offset;
  2198.  
  2199.         browser_window_place_caret(html->bw,
  2200.                         box_x + input->children->x +
  2201.                                         text_box->x + pixel_offset,
  2202.                         box_y + input->children->y + text_box->y,
  2203.                         text_box->height,
  2204.                         textinput_input_callback,
  2205.                         textinput_input_paste_text,
  2206.                         textinput_input_move_caret,
  2207.                         input, c);
  2208.  
  2209.         if (dx)
  2210.                 html__redraw_a_box(html, input);
  2211. }
  2212.  
  2213.  
  2214.