Simplified clipboard API.

This commit is contained in:
Camilla Berglund 2012-04-12 00:51:58 +02:00
parent 721e0a7fd0
commit f868712f02
9 changed files with 33 additions and 56 deletions

View File

@ -581,7 +581,7 @@ GLFWAPI int glfwGetJoystickButtons(int joy, unsigned char* buttons, int numbutto
/* Clipboard */
GLFWAPI void glfwSetClipboardString(GLFWwindow window, const char* string);
GLFWAPI size_t glfwGetClipboardString(GLFWwindow window, char* string, size_t size);
GLFWAPI const char* glfwGetClipboardString(GLFWwindow window);
/* Time */
GLFWAPI double glfwGetTime(void);

View File

@ -59,19 +59,16 @@ GLFWAPI void glfwSetClipboardString(GLFWwindow handle, const char* string)
// Return the current clipboard contents
//========================================================================
GLFWAPI size_t glfwGetClipboardString(GLFWwindow handle, char* string, size_t size)
GLFWAPI const char* glfwGetClipboardString(GLFWwindow handle)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED, NULL);
return 0;
return NULL;
}
if (!string || !size)
return 0;
return _glfwPlatformGetClipboardString(window, string, size);
return _glfwPlatformGetClipboardString(window);
}

View File

@ -56,7 +56,7 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string)
// Return the current clipboard contents
//========================================================================
size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t size)
const char* _glfwPlatformGetClipboardString(_GLFWwindow* window)
{
const char* source;
size_t targetSize;
@ -65,7 +65,7 @@ size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t
if (![[pasteboard types] containsObject:NSStringPboardType])
{
_glfwSetError(GLFW_FORMAT_UNAVAILABLE, NULL);
return 0;
return NULL;
}
NSString* object = [pasteboard stringForType:NSStringPboardType];
@ -73,15 +73,12 @@ size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t
{
_glfwSetError(GLFW_PLATFORM_ERROR,
"Cocoa/NSGL: Failed to retrieve object from pasteboard");
return 0;
return NULL;
}
source = [object UTF8String];
targetSize = strlen(source) + 1;
if (targetSize > size)
targetSize = size;
free(_glfwLibrary.NS.clipboardString);
_glfwLibrary.NS.clipboardString = strdup([object UTF8String]);
strlcpy(string, source, targetSize);
return 0;
return _glfwLibrary.NS.clipboardString;
}

View File

@ -95,6 +95,8 @@ typedef struct _GLFWlibraryNS
CGEventSourceRef eventSource;
id delegate;
id autoreleasePool;
char* clipboardString;
} _GLFWlibraryNS;

View File

@ -290,7 +290,7 @@ void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp);
// Clipboard
void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string);
size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char *data, size_t size);
const char* _glfwPlatformGetClipboardString(_GLFWwindow* window);
// Joystick
int _glfwPlatformGetJoystickParam(int joy, int param);

View File

@ -94,23 +94,21 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string)
// Return the current clipboard contents
//========================================================================
size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t size)
const char* _glfwPlatformGetClipboardString(_GLFWwindow* window)
{
HANDLE stringHandle;
char* utf8String;
size_t utf8Size;
if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
{
_glfwSetError(GLFW_FORMAT_UNAVAILABLE, NULL);
return 0;
return NULL;
}
if (!OpenClipboard(window->Win32.handle))
{
_glfwSetError(GLFW_PLATFORM_ERROR,
"Win32/WGL: Failed to open clipboard");
return 0;
return NULL;
}
stringHandle = GetClipboardData(CF_UNICODETEXT);
@ -120,30 +118,23 @@ size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t
_glfwSetError(GLFW_PLATFORM_ERROR,
"Win32/WGL: Failed to retrieve clipboard data");
return 0;
return NULL;
}
utf8String = _glfwCreateUTF8FromWideString(GlobalLock(stringHandle));
free(_glfwLibrary.Win32.clipboardString);
_glfwLibrary.Win32.clipboardString =
_glfwCreateUTF8FromWideString(GlobalLock(stringHandle));
GlobalUnlock(stringHandle);
CloseClipboard();
if (!utf8String)
if (!_glfwLibrary.Win32.clipboardString)
{
_glfwSetError(GLFW_PLATFORM_ERROR,
"Win32/WGL: Failed to convert wide string to UTF-8");
return 0;
return NULL;
}
utf8Size = strlen(utf8String) + 1;
if (utf8Size > size)
{
memcpy(string, utf8String, size);
string[size - 1] = '\0';
}
else
memcpy(string, utf8String, utf8Size);
free(utf8String);
return utf8Size;
return _glfwLibrary.Win32.clipboardString;
}

View File

@ -177,6 +177,7 @@ typedef struct _GLFWlibraryWin32
ATOM classAtom; // Window class atom
HHOOK keyboardHook; // Keyboard hook handle
DWORD foregroundLockTimeout;
char* clipboardString;
// Default monitor
struct {

View File

@ -152,10 +152,9 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string)
// Return the current clipboard contents
//========================================================================
size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t size)
const char* _glfwPlatformGetClipboardString(_GLFWwindow* window)
{
int i;
size_t sourceSize, targetSize;
_glfwLibrary.X11.selection.status = _GLFW_CONVERSION_INACTIVE;
@ -184,18 +183,9 @@ size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t
{
_glfwSetError(GLFW_FORMAT_UNAVAILABLE,
"X11/GLX: Failed to convert selection to string");
return 0;
return NULL;
}
sourceSize = strlen(_glfwLibrary.X11.selection.string) + 1;
targetSize = sourceSize;
if (targetSize > size)
targetSize = size;
memcpy(string, _glfwLibrary.X11.selection.string, targetSize);
string[targetSize - 1] = '\0';
return sourceSize;
return _glfwLibrary.X11.selection.string;
}

View File

@ -59,16 +59,15 @@ static void key_callback(GLFWwindow window, int key, int action)
case GLFW_KEY_V:
if (control_is_down(window))
{
char buffer[4096];
size_t size;
const char* string;
printf("Paste test.\n");
size = glfwGetClipboardString(window, buffer, sizeof(buffer));
if (size >= sizeof(buffer))
printf("Buffer wasn't big enough to hold clipboard data.\n");
string = glfwGetClipboardString(window);
if (!string)
printf("Failed to retrieve clipboard string\n");
printf("[%lu]: %s\n", (unsigned long) size, buffer);
printf("%s\n", string);
}
break;