mirror of
https://github.com/glfw/glfw.git
synced 2026-09-18 22:55:46 +00:00
Merged clipboard code into input.
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <windowsx.h>
|
||||
#include <shellapi.h>
|
||||
|
||||
@@ -1372,6 +1373,95 @@ void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
|
||||
}
|
||||
}
|
||||
|
||||
void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string)
|
||||
{
|
||||
WCHAR* wideString;
|
||||
HANDLE stringHandle;
|
||||
size_t wideSize;
|
||||
|
||||
wideString = _glfwCreateWideStringFromUTF8(string);
|
||||
if (!wideString)
|
||||
{
|
||||
_glfwInputError(GLFW_PLATFORM_ERROR,
|
||||
"Win32: Failed to convert clipboard string to "
|
||||
"wide string");
|
||||
return;
|
||||
}
|
||||
|
||||
wideSize = (wcslen(wideString) + 1) * sizeof(WCHAR);
|
||||
|
||||
stringHandle = GlobalAlloc(GMEM_MOVEABLE, wideSize);
|
||||
if (!stringHandle)
|
||||
{
|
||||
free(wideString);
|
||||
|
||||
_glfwInputError(GLFW_PLATFORM_ERROR,
|
||||
"Win32: Failed to allocate global handle for clipboard");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(GlobalLock(stringHandle), wideString, wideSize);
|
||||
GlobalUnlock(stringHandle);
|
||||
|
||||
if (!OpenClipboard(window->win32.handle))
|
||||
{
|
||||
GlobalFree(stringHandle);
|
||||
free(wideString);
|
||||
|
||||
_glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to open clipboard");
|
||||
return;
|
||||
}
|
||||
|
||||
EmptyClipboard();
|
||||
SetClipboardData(CF_UNICODETEXT, stringHandle);
|
||||
CloseClipboard();
|
||||
|
||||
free(wideString);
|
||||
}
|
||||
|
||||
const char* _glfwPlatformGetClipboardString(_GLFWwindow* window)
|
||||
{
|
||||
HANDLE stringHandle;
|
||||
|
||||
if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
|
||||
{
|
||||
_glfwInputError(GLFW_FORMAT_UNAVAILABLE, NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!OpenClipboard(window->win32.handle))
|
||||
{
|
||||
_glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to open clipboard");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
stringHandle = GetClipboardData(CF_UNICODETEXT);
|
||||
if (!stringHandle)
|
||||
{
|
||||
CloseClipboard();
|
||||
|
||||
_glfwInputError(GLFW_PLATFORM_ERROR,
|
||||
"Win32: Failed to retrieve clipboard data");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
free(_glfw.win32.clipboardString);
|
||||
_glfw.win32.clipboardString =
|
||||
_glfwCreateUTF8FromWideString(GlobalLock(stringHandle));
|
||||
|
||||
GlobalUnlock(stringHandle);
|
||||
CloseClipboard();
|
||||
|
||||
if (!_glfw.win32.clipboardString)
|
||||
{
|
||||
_glfwInputError(GLFW_PLATFORM_ERROR,
|
||||
"Win32: Failed to convert wide string to UTF-8");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return _glfw.win32.clipboardString;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
////// GLFW native API //////
|
||||
|
||||
Reference in New Issue
Block a user