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.  * Textual input handling (implementation)
  25.  */
  26.  
  27. #include <assert.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include <dom/dom.h>
  31.  
  32. #include "desktop/browser_private.h"
  33. #include "desktop/gui.h"
  34. #include "desktop/mouse.h"
  35. #include "desktop/scrollbar.h"
  36. #include "desktop/selection.h"
  37. #include "desktop/textinput.h"
  38. #include "render/box.h"
  39. #include "render/font.h"
  40. #include "render/form.h"
  41. #include "render/html_internal.h"
  42. #include "render/layout.h"
  43. #include "utils/log.h"
  44. #include "utils/talloc.h"
  45. #include "utils/utf8.h"
  46. #include "utils/utils.h"
  47.  
  48. /* Define to enable textinput debug */
  49. #undef TEXTINPUT_DEBUG
  50.  
  51.  
  52. /**
  53.  * Position the caret and assign a callback for key presses.
  54.  *
  55.  * \param bw  The browser window in which to place the caret
  56.  * \param x   X coordinate of the caret
  57.  * \param y   Y coordinate
  58.  * \param height    Height of caret
  59.  * \param caret_cb  Callback function for keypresses
  60.  * \param paste_cb  Callback function for pasting text
  61.  * \param move_cb   Callback function for caret movement
  62.  * \param p1  Callback private data pointer, passed to callback function
  63.  * \param p2  Callback private data pointer, passed to callback function
  64.  */
  65. void browser_window_place_caret(struct browser_window *bw,
  66.                 int x, int y, int height,
  67.                 browser_caret_callback caret_cb,
  68.                 browser_paste_callback paste_cb,
  69.                 browser_move_callback move_cb,
  70.                 void *p1, void *p2)
  71. {
  72.         struct browser_window *root_bw;
  73.         int pos_x = 0;
  74.         int pos_y = 0;
  75.  
  76.         /* Find top level browser window */
  77.         root_bw = browser_window_get_root(bw);
  78.         browser_window_get_position(bw, true, &pos_x, &pos_y);
  79.  
  80.         x = x * bw->scale + pos_x;
  81.         y = y * bw->scale + pos_y;
  82.  
  83.         gui_window_place_caret(root_bw->window, x, y, height * bw->scale);
  84.         bw->caret_callback = caret_cb;
  85.         bw->paste_callback = paste_cb;
  86.         bw->move_callback = move_cb;
  87.         bw->caret_p1 = p1;
  88.         bw->caret_p2 = p2;
  89.  
  90.         /* Set focus browser window */
  91.         root_bw->focus = bw;
  92. }
  93.  
  94.  
  95. /**
  96.  * Removes the caret and callback for key process.
  97.  *
  98.  * \param bw  The browser window from which to remove caret
  99.  */
  100. void browser_window_remove_caret(struct browser_window *bw)
  101. {
  102.         struct browser_window *root_bw;
  103.  
  104.         root_bw = browser_window_get_root(bw);
  105.  
  106.         if (root_bw && root_bw->window)
  107.                 gui_window_remove_caret(root_bw->window);
  108.  
  109.         bw->caret_callback = NULL;
  110.         bw->paste_callback = NULL;
  111.         bw->move_callback = NULL;
  112.         bw->caret_p1 = NULL;
  113.         bw->caret_p2 = NULL;
  114. }
  115.  
  116.  
  117. /**
  118.  * Handle key presses in a browser window.
  119.  *
  120.  * \param bw   The root browser window
  121.  * \param key  The UCS4 character codepoint
  122.  * \return true if key handled, false otherwise
  123.  */
  124. bool browser_window_key_press(struct browser_window *bw, uint32_t key)
  125. {
  126.         struct browser_window *focus = bw->focus;
  127.  
  128.         assert(bw->window != NULL);
  129.  
  130.         /* safe keys that can be handled whether input claimed or not */
  131.         switch (key) {
  132.                 case KEY_COPY_SELECTION:
  133.                         selection_copy_to_clipboard(bw->cur_sel);
  134.                         return true;
  135.  
  136.                 case KEY_CLEAR_SELECTION:
  137.                         selection_clear(bw->cur_sel, true);
  138.                         return true;
  139.  
  140.                 case KEY_ESCAPE:
  141.                         if (bw->cur_sel && selection_defined(bw->cur_sel)) {
  142.                                 selection_clear(bw->cur_sel, true);
  143.                                 return true;
  144.                         }
  145.                         /* if there's no selection,
  146.                          * leave Escape for the caller */
  147.                         return false;
  148.         }
  149.  
  150.         if (focus->caret_callback) {
  151.                 /* Pass keypress onto anything that has claimed input focus */
  152.                 return focus->caret_callback(focus, key,
  153.                                 focus->caret_p1, focus->caret_p2);
  154.         }
  155.  
  156.         /* keys we can't handle here if cursor is in form */
  157.         switch (key) {
  158.                 case KEY_SELECT_ALL:
  159.                         selection_select_all(bw->cur_sel);
  160.                         return true;
  161.         }
  162.  
  163.         return false;
  164. }
  165.  
  166.  
  167. /**
  168.  * Paste a block of text into a browser window at the caret position.
  169.  *
  170.  * \param  bw        browser window
  171.  * \param  utf8      pointer to block of text
  172.  * \param  utf8_len  length (bytes) of text block
  173.  * \param  last      true iff this is the last chunk (update screen too)
  174.  * \return true iff successful
  175.  *
  176.  * TODO: Remove this function.
  177.  */
  178.  
  179. bool browser_window_paste_text(struct browser_window *bw, const char *utf8,
  180.                 unsigned utf8_len, bool last)
  181. {
  182.         if (!bw->focus || !bw->focus->paste_callback)
  183.                 return false;
  184.  
  185.         return bw->focus->paste_callback(bw->focus, utf8, utf8_len, last,
  186.                         bw->focus->caret_p1, bw->focus->caret_p2);
  187. }
  188.  
  189.