1 /* Copyright (C) 2010 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include "precompiled.h"
24 #include "lib/sysdep/clipboard.h"
25
26 #include "lib/sysdep/os/win/win.h"
27 #include "lib/sysdep/os/win/wutil.h"
28
29
30 // caller is responsible for freeing hMem.
SetClipboardText(const wchar_t * text,HGLOBAL & hMem)31 static Status SetClipboardText(const wchar_t* text, HGLOBAL& hMem)
32 {
33 const size_t numChars = wcslen(text);
34 hMem = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT, (numChars + 1) * sizeof(wchar_t));
35 if(!hMem)
36 WARN_RETURN(ERR::NO_MEM);
37
38 wchar_t* lockedText = (wchar_t*)GlobalLock(hMem);
39 if(!lockedText)
40 WARN_RETURN(ERR::NO_MEM);
41 wcscpy_s(lockedText, numChars+1, text);
42 GlobalUnlock(hMem);
43
44 HANDLE hData = SetClipboardData(CF_UNICODETEXT, hMem);
45 if(!hData) // failed
46 WARN_RETURN(ERR::FAIL);
47
48 return INFO::OK;
49 }
50
51
52 // @return INFO::OK iff text has been assigned a pointer (which the
53 // caller must free via sys_clipboard_free) to the clipboard text.
GetClipboardText(wchar_t * & text)54 static Status GetClipboardText(wchar_t*& text)
55 {
56 // NB: Windows NT/2000+ auto convert CF_UNICODETEXT <-> CF_TEXT.
57
58 if(!IsClipboardFormatAvailable(CF_UNICODETEXT))
59 return INFO::CANNOT_HANDLE;
60
61 HGLOBAL hMem = GetClipboardData(CF_UNICODETEXT);
62 if(!hMem)
63 WARN_RETURN(ERR::FAIL);
64
65 const wchar_t* lockedText = (const wchar_t*)GlobalLock(hMem);
66 if(!lockedText)
67 WARN_RETURN(ERR::NO_MEM);
68
69 const size_t size = GlobalSize(hMem);
70 text = (wchar_t*)malloc(size);
71 if(!text)
72 WARN_RETURN(ERR::NO_MEM);
73 wcscpy_s(text, size/sizeof(wchar_t), lockedText);
74
75 (void)GlobalUnlock(hMem);
76
77 return INFO::OK;
78 }
79
80
81 // OpenClipboard parameter.
82 // NB: using wutil_AppWindow() causes GlobalLock to fail.
83 static const HWND hWndNewOwner = 0; // MSDN: associate with "current task"
84
sys_clipboard_set(const wchar_t * text)85 Status sys_clipboard_set(const wchar_t* text)
86 {
87 if(!OpenClipboard(hWndNewOwner))
88 WARN_RETURN(ERR::FAIL);
89
90 WARN_IF_FALSE(EmptyClipboard());
91
92 // NB: to enable copy/pasting something other than text, add
93 // message handlers for WM_RENDERFORMAT and WM_RENDERALLFORMATS.
94 HGLOBAL hMem;
95 Status ret = SetClipboardText(text, hMem);
96
97 WARN_IF_FALSE(CloseClipboard()); // must happen before GlobalFree
98
99 ENSURE(GlobalFree(hMem) == 0); // (0 indicates success)
100
101 return ret;
102 }
103
104
sys_clipboard_get()105 wchar_t* sys_clipboard_get()
106 {
107 if(!OpenClipboard(hWndNewOwner))
108 return 0;
109
110 wchar_t* text;
111 Status ret = GetClipboardText(text);
112
113 WARN_IF_FALSE(CloseClipboard());
114
115 return (ret == INFO::OK)? text : 0;
116 }
117
118
sys_clipboard_free(wchar_t * text)119 Status sys_clipboard_free(wchar_t* text)
120 {
121 free(text);
122 return INFO::OK;
123 }
124