NetSurf
clipboard.c
Go to the documentation of this file.
1/*
2 * Copyright 2019 Vincent Sanders <vince@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/**
20 * \file
21 * win32 clipboard implementation.
22 */
23
24#include <windows.h>
25
26#include "utils/log.h"
27#include "netsurf/clipboard.h"
28
29#include "windows/clipboard.h"
30
31/**
32 * Core asks front end for clipboard contents.
33 *
34 * \param buffer UTF-8 text, allocated by front end, ownership yeilded to core
35 * \param length Byte length of UTF-8 text in buffer
36 */
37static void gui_get_clipboard(char **buffer, size_t *length)
38{
39 HANDLE clipboard_handle;
40 wchar_t *content;
41
42 if (OpenClipboard(NULL)) {
43 clipboard_handle = GetClipboardData(CF_UNICODETEXT);
44 if (clipboard_handle != NULL) {
45 content = GlobalLock(clipboard_handle);
46 if (content != NULL) {
47 int required_len;
48 size_t content_len;
49
50 content_len = wcslen(content);
51
52 /* compute length */
53 required_len = WideCharToMultiByte(
54 CP_UTF8,
55 WC_NO_BEST_FIT_CHARS,
56 content,
57 content_len,
58 NULL,
59 0,
60 NULL,
61 NULL);
62 /* allocate buffer and do conversion */
63 *buffer = malloc(required_len);
64 *length = WideCharToMultiByte(
65 CP_UTF8,
66 WC_NO_BEST_FIT_CHARS,
67 content,
68 content_len,
69 *buffer,
70 required_len,
71 NULL,
72 NULL);
73
74 GlobalUnlock(clipboard_handle);
75 }
76 }
77 CloseClipboard();
78 }
79}
80
81
82/**
83 * Core tells front end to put given text in clipboard
84 *
85 * \param buffer UTF-8 text, owned by core
86 * \param length Byte length of UTF-8 text in buffer
87 * \param styles Array of styles given to text runs, owned by core, or NULL
88 * \param n_styles Number of text run styles in array
89 */
90static void
92 size_t length,
93 nsclipboard_styles styles[],
94 int n_styles)
95{
96 HGLOBAL hglbCopy;
97 wchar_t *content; /* clipboard content */
98 int content_len; /* characters in content */
99
100 if (OpenClipboard(NULL)) {
101 EmptyClipboard();
102 content_len = MultiByteToWideChar(CP_UTF8,
103 MB_PRECOMPOSED,
104 buffer, length,
105 NULL, 0);
106
107 hglbCopy = GlobalAlloc(GMEM_MOVEABLE,
108 ((content_len + 1) * sizeof(wchar_t)));
109 if (hglbCopy != NULL) {
110 content = GlobalLock(hglbCopy);
111 MultiByteToWideChar(CP_UTF8,
112 MB_PRECOMPOSED,
113 buffer, length,
114 content, content_len);
115 content[content_len] = 0; /* null terminate */
116
117 GlobalUnlock(hglbCopy);
118 SetClipboardData(CF_UNICODETEXT, hglbCopy);
119 }
120 CloseClipboard();
121 }
122}
123
124
125
128 .set = gui_set_clipboard,
129};
130
static osspriteop_area * buffer
The buffer characteristics.
Definition: buffer.c:55
Interface to platform-specific clipboard operations.
Content which corresponds to a single URL.
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
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: clipboard.c:91
static void gui_get_clipboard(char **buffer, size_t *length)
Core asks front end for clipboard contents.
Definition: clipboard.c:37
static struct gui_clipboard_table clipboard_table
Definition: clipboard.c:126
struct gui_clipboard_table * win32_clipboard_table
The clipboard operation function table for win32.
Definition: clipboard.c:131