NetSurf
selection.c
Go to the documentation of this file.
1/*
2 * Copyright 2008 Mike Lester <element3260@gmail.com>
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#include <string.h>
20#include <gtk/gtk.h>
21#include <stdlib.h>
22
23#include "utils/log.h"
25#include "netsurf/clipboard.h"
26
27#include "gtk/toolbar_items.h"
28#include "gtk/window.h"
29
30static GString *current_selection = NULL;
31static GtkClipboard *clipboard;
32
33
34/**
35 * Core asks front end for clipboard contents.
36 *
37 * \param buffer UTF-8 text, allocated by front end, ownership yeilded to core
38 * \param length Byte length of UTF-8 text in buffer
39 */
40static void gui_get_clipboard(char **buffer, size_t *length)
41{
42 gchar *gtext;
43
44 *buffer = NULL;
45 *length = 0;
46
47 /* get clipboard contents from gtk */
48 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
49 gtext = gtk_clipboard_wait_for_text(clipboard); /* conv to utf-8 */
50
51 if (gtext == NULL)
52 return;
53
54 *length = strlen(gtext);
55 *buffer = malloc(*length);
56 if (*buffer == NULL) {
57 *length = 0;
58 g_free(gtext);
59 return;
60 }
61
62 memcpy(*buffer, gtext, *length);
63
64 g_free(gtext);
65}
66
67
68/**
69 * Core tells front end to put given text in clipboard
70 *
71 * \param buffer UTF-8 text, owned by core
72 * \param length Byte length of UTF-8 text in buffer
73 * \param styles Array of styles given to text runs, owned by core, or NULL
74 * \param n_styles Number of text run styles in array
75 */
76static void gui_set_clipboard(const char *buffer, size_t length,
77 nsclipboard_styles styles[], int n_styles)
78{
79 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
80
82 current_selection = g_string_new(NULL);
83 else
84 g_string_set_size(current_selection, 0);
85
86 current_selection = g_string_append_len(current_selection,
87 buffer, length);
88
89 gtk_clipboard_set_text(clipboard, current_selection->str, -1);
90}
91
94 .set = gui_set_clipboard,
95};
96
Browser window creation and manipulation interface.
static osspriteop_area * buffer
The buffer characteristics.
Definition: buffer.c:55
static void gui_set_clipboard(const char *buffer, size_t length, nsclipboard_styles styles[], int n_styles)
Core tells front end to put given text in clipboard.
Definition: selection.c:76
static void gui_get_clipboard(char **buffer, size_t *length)
Core asks front end for clipboard contents.
Definition: selection.c:40
static GString * current_selection
Definition: selection.c:30
static struct gui_clipboard_table clipboard_table
Definition: selection.c:92
static GtkClipboard * clipboard
Definition: selection.c:31
struct gui_clipboard_table * nsgtk_clipboard_table
Definition: selection.c:97
Interface to platform-specific clipboard operations.
Interface to utility string handling.
function table for clipboard operations.
Definition: clipboard.h:42
void(* get)(char **buffer, size_t *length)
Core asks front end for clipboard contents.
Definition: clipboard.h:49