Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Copyright 2012 Michael Drake <tlsa@netsurf-browser.org>
  3.  *
  4.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  5.  *
  6.  * NetSurf is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; version 2 of the License.
  9.  *
  10.  * NetSurf is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18.  
  19. /** \file
  20.   * nsfb internal clipboard handling
  21.   */
  22.  
  23. #include <assert.h>
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include "desktop/browser.h"
  27. #include "desktop/gui.h"
  28. #include "desktop/selection.h"
  29. #include "framebuffer/gui.h"
  30. #include "utils/log.h"
  31.  
  32.  
  33. static struct gui_clipboard {
  34.         char *buffer;
  35.         size_t buffer_len;
  36.         size_t length;
  37. } gui_clipboard;
  38.  
  39.  
  40.  
  41.  
  42. /**
  43.  * Core asks front end for clipboard contents.
  44.  *
  45.  * \param  buffer  UTF-8 text, allocated by front end, ownership yeilded to core
  46.  * \param  length  Byte length of UTF-8 text in buffer
  47.  */
  48. void gui_get_clipboard(char **buffer, size_t *length)
  49. {
  50.         *buffer = NULL;
  51.         *length = 0;
  52.  
  53.         if (gui_clipboard.length > 0) {
  54.                 assert(gui_clipboard.buffer != NULL);
  55.                 LOG(("Pasting %i bytes: \"%s\"\n", gui_clipboard.length,
  56.                                 gui_clipboard.buffer));
  57.  
  58.                 *buffer = malloc(gui_clipboard.length);
  59.  
  60.                 if (*buffer != NULL) {
  61.                         memcpy(*buffer, gui_clipboard.buffer,
  62.                                         gui_clipboard.length);
  63.                         *length = gui_clipboard.length;
  64.                 }
  65.         }
  66. }
  67.  
  68.  
  69. /**
  70.  * Core tells front end to put given text in clipboard
  71.  *
  72.  * \param  buffer    UTF-8 text, owned by core
  73.  * \param  length    Byte length of UTF-8 text in buffer
  74.  * \param  styles    Array of styles given to text runs, owned by core, or NULL
  75.  * \param  n_styles  Number of text run styles in array
  76.  */
  77. void gui_set_clipboard(const char *buffer, size_t length,
  78.                 nsclipboard_styles styles[], int n_styles)
  79. {
  80.         if (gui_clipboard.buffer_len < length + 1) {
  81.                 /* Make buffer big enough */
  82.                 char *new_buff;
  83.  
  84.                 new_buff = realloc(gui_clipboard.buffer, length + 1);
  85.                 if (new_buff == NULL)
  86.                         return;
  87.  
  88.                 gui_clipboard.buffer = new_buff;
  89.                 gui_clipboard.buffer_len = length + 1;
  90.         }
  91.  
  92.         gui_clipboard.length = 0;
  93.  
  94.         memcpy(gui_clipboard.buffer, buffer, length);
  95.         gui_clipboard.length = length;
  96.         gui_clipboard.buffer[gui_clipboard.length] = '\0';
  97. }
  98.  
  99.