annotate src/video/win32/SDL_win32clipboard.c @ 4500:eff4e88cc1e8

Added Windows clipboard support
author Sam Lantinga <slouken@libsdl.org>
date Thu, 08 Jul 2010 05:43:34 -0700
parents
children 9faebccfefb3
rev   line source
4500
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /*
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
3 Copyright (C) 1997-2010 Sam Lantinga
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 Lesser General Public License for more details.
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 Sam Lantinga
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 slouken@libsdl.org
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21 */
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 #include "SDL_config.h"
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 #include "SDL_win32video.h"
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25 #include "SDL_win32window.h"
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28 #ifdef UNICODE
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 #define TEXT_FORMAT CF_UNICODETEXT
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30 #else
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31 #define TEXT_FORMAT CF_TEXT
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 #endif
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35 /* Get any application owned window handle for clipboard association */
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 static HWND
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37 GetWindowHandle(_THIS)
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
38 {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
39 SDL_VideoDisplay *display;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
40 SDL_Window *window;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
41
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
42 display = _this->displays;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
43 if (display) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
44 window = display->windows;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45 if (window) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
46 return ((SDL_WindowData *) window->driverdata)->hwnd;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
48 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
49 return NULL;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52 int
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
53 WIN_SetClipboardText(_THIS, const char *text)
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54 {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
55 int result = 0;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
56
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
57 if (OpenClipboard(GetWindowHandle(_this))) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
58 HANDLE hMem;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59 LPTSTR tstr;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60 SIZE_T i, size;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
61
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62 /* Convert the text from UTF-8 to Windows Unicode */
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63 tstr = WIN_UTF8ToString(text);
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64 if (!tstr) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65 return -1;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68 /* Find out the size of the data */
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69 for (size = 0, i = 0; tstr[i]; ++i, ++size) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70 if (tstr[i] == '\n' && (i == 0 || tstr[i-1] != '\r')) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
71 /* We're going to insert a carriage return */
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
72 ++size;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
73 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75 size = (size+1)*sizeof(*tstr);
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
76
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77 /* Save the data to the clipboard */
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
78 hMem = GlobalAlloc(GMEM_MOVEABLE, size);
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
79 if (hMem) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
80 LPTSTR dst = (LPTSTR)GlobalLock(hMem);
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
81 /* Copy the text over, adding carriage returns as necessary */
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
82 for (i = 0; tstr[i]; ++i) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83 if (tstr[i] == '\n' && (i == 0 || tstr[i-1] != '\r')) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 *dst++ = '\r';
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86 *dst++ = tstr[i];
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
87 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88 *dst = 0;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89 GlobalUnlock(hMem);
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
90
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91 EmptyClipboard();
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92 if (!SetClipboardData(TEXT_FORMAT, hMem)) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
93 WIN_SetError("Couldn't set clipboard data");
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
94 result = -1;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
97 SDL_free(tstr);
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
98
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
99 CloseClipboard();
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100 } else {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101 WIN_SetError("Couldn't open clipboard");
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
102 result = -1;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
103 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
104 return result;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
105 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
107 char *
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 WIN_GetClipboardText(_THIS)
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109 {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110 char *text;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
111
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
112 text = NULL;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 if (IsClipboardFormatAvailable(TEXT_FORMAT) &&
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114 OpenClipboard(GetWindowHandle(_this))) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115 HANDLE hMem;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
116 LPTSTR tstr;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
117
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
118 hMem = GetClipboardData(TEXT_FORMAT);
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
119 if (hMem) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
120 tstr = (LPTSTR)GlobalLock(hMem);
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
121 text = WIN_StringToUTF8(tstr);
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
122 GlobalUnlock(hMem);
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
123 } else {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
124 WIN_SetError("Couldn't get clipboard data");
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
125 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
126 CloseClipboard();
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
127 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
128 if (!text) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
129 text = SDL_strdup("");
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
130 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
131 return text;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
132 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
133
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134 SDL_bool
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
135 WIN_HasClipboardText(_THIS)
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
136 {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
137 if (IsClipboardFormatAvailable(TEXT_FORMAT)) {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
138 return SDL_TRUE;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
139 } else {
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
140 return SDL_FALSE;
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
141 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
142 }
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
143
eff4e88cc1e8 Added Windows clipboard support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
144 /* vi: set ts=4 sw=4 expandtab: */