1 /*
2  * This source file is part of libRocket, the HTML/CSS Interface Middleware
3  *
4  * For the latest information, see http://www.librocket.com
5  *
6  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  *
26  */
27 
28 #include "../../Include/Rocket/Controls/Clipboard.h"
29 #include "../../Include/Rocket/Core/Types.h"
30 #include "../../Include/Rocket/Core/WString.h"
31 #if defined ROCKET_PLATFORM_WIN32
32 #include <windows.h>
33 #endif
34 
35 namespace Rocket {
36 namespace Controls {
37 
38 #if defined ROCKET_PLATFORM_WIN32
39 static HWND application_hwnd = NULL;
40 
FindApplicationWindow(HWND hwnd,LPARAM process_id)41 static BOOL CALLBACK FindApplicationWindow(HWND hwnd, LPARAM process_id)
42 {
43 	DWORD hwnd_pid;
44 	GetWindowThreadProcessId(hwnd, &hwnd_pid);
45 	if (hwnd_pid == (DWORD) process_id)
46 	{
47 		application_hwnd = hwnd;
48 		return FALSE;
49 	}
50 
51 	return TRUE;
52 }
53 
GetHWND()54 static HWND GetHWND()
55 {
56 	if (application_hwnd != NULL)
57 		return application_hwnd;
58 
59 	EnumWindows(FindApplicationWindow, GetCurrentProcessId());
60 	return application_hwnd;
61 }
62 #endif
63 
64 static Core::WString content;
65 
66 // Get the current contents of the clipboard.
Get()67 Core::WString Clipboard::Get()
68 {
69 	#if defined ROCKET_PLATFORM_WIN32
70 	if (GetHWND())
71 	{
72 		Core::WString clipboard_content;
73 
74 		if (!OpenClipboard(GetHWND()))
75 			return clipboard_content;
76 
77 		HANDLE clipboard_data = GetClipboardData(CF_UNICODETEXT);
78 		if (clipboard_data == NULL)
79 		{
80 			CloseClipboard();
81 			return clipboard_content;
82 		}
83 
84 		const Rocket::Core::word* clipboard_text = (const Rocket::Core::word*) GlobalLock(clipboard_data);
85 		if (clipboard_text)
86 			clipboard_content.Assign(clipboard_text);
87 		GlobalUnlock(clipboard_data);
88 
89 		CloseClipboard();
90 		return clipboard_content;
91 	}
92 	else
93 		return content;
94 	#else
95 	return content;
96 	#endif
97 }
98 
99 // Set the contents of the clipboard.
Set(const Core::WString & _content)100 void Clipboard::Set(const Core::WString& _content)
101 {
102 	#if defined ROCKET_PLATFORM_WIN32
103 	if (GetHWND())
104 	{
105 		if (!OpenClipboard(GetHWND()))
106 			return;
107 
108 		EmptyClipboard();
109 
110 		Rocket::Core::String win32_content;
111 		_content.ToUTF8(win32_content);
112 
113 		HGLOBAL clipboard_data = GlobalAlloc(GMEM_FIXED, win32_content.Length() + 1);
114 		// Replaced strcpy_s with a simple strcpy, because we know for sure it's big enough.
115 		strcpy((char*) clipboard_data, win32_content.CString());
116 
117 		if (SetClipboardData(CF_TEXT, clipboard_data) == NULL)
118 		{
119 			CloseClipboard();
120 			GlobalFree(clipboard_data);
121 		}
122 		else
123 			CloseClipboard();
124 	}
125 	else
126 		content = _content;
127 	#else
128 	content = _content;
129 	#endif
130 }
131 
132 #if defined ROCKET_PLATFORM_WIN32
133 // Set the window handle of the application.
SetHWND(void * hwnd)134 void Clipboard::SetHWND(void* hwnd)
135 {
136 	application_hwnd = (HWND) hwnd;
137 }
138 #endif
139 
140 }
141 }
142