Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
wclipboard.cpp
Go to the documentation of this file.
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"
28 
29 
30 // caller is responsible for freeing 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)
37 
38  wchar_t* lockedText = (wchar_t*)GlobalLock(hMem);
39  if(!lockedText)
41  wcscpy_s(lockedText, numChars+1, text);
42  GlobalUnlock(hMem);
43 
44  HANDLE hData = SetClipboardData(CF_UNICODETEXT, hMem);
45  if(!hData) // failed
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.
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)
64 
65  const wchar_t* lockedText = (const wchar_t*)GlobalLock(hMem);
66  if(!lockedText)
68 
69  const size_t size = GlobalSize(hMem);
70  text = (wchar_t*)malloc(size);
71  if(!text)
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 
85 Status sys_clipboard_set(const wchar_t* text)
86 {
87  if(!OpenClipboard(hWndNewOwner))
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 
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 
120 {
121  free(text);
122  return INFO::OK;
123 }
const Status OK
Definition: status.h:386
Status sys_clipboard_set(const wchar_t *text)
Definition: android.cpp:30
Status sys_clipboard_free(wchar_t *copy)
Definition: android.cpp:40
const Status CANNOT_HANDLE
Definition: status.h:396
static const HWND hWndNewOwner
Definition: wclipboard.cpp:83
static Status SetClipboardText(const wchar_t *text, HGLOBAL &hMem)
Definition: wclipboard.cpp:31
int wcscpy_s(wchar_t *dst, size_t max_dst_chars, const wchar_t *src)
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
void * HANDLE
Definition: wgl.h:62
i64 Status
Error handling system.
Definition: status.h:171
wchar_t * sys_clipboard_get()
Definition: android.cpp:35
#define WARN_IF_FALSE(expression)
Definition: status.h:360
static Status GetClipboardText(wchar_t *&text)
Definition: wclipboard.cpp:54
#define WARN_RETURN(status)
Definition: status.h:255
const Status FAIL
Definition: status.h:406
const Status NO_MEM
Definition: status.h:430