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