changeset 4500:eff4e88cc1e8

Added Windows clipboard support
author Sam Lantinga <slouken@libsdl.org>
date Thu, 08 Jul 2010 05:43:34 -0700
parents c2ebe3e020c6
children 0cf025066b6f
files VisualC/SDL/SDL_VS2005.vcproj VisualC/SDL/SDL_VS2008.vcproj VisualC/SDLmain/SDLmain_VS2008.vcproj src/video/SDL_clipboard.c src/video/win32/SDL_win32clipboard.c src/video/win32/SDL_win32clipboard.h src/video/win32/SDL_win32events.c src/video/win32/SDL_win32events.h src/video/win32/SDL_win32video.c src/video/win32/SDL_win32video.h src/video/win32/SDL_win32window.c
diffstat 11 files changed, 235 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/VisualC/SDL/SDL_VS2005.vcproj	Thu Jul 08 00:35:58 2010 -0700
+++ b/VisualC/SDL/SDL_VS2005.vcproj	Thu Jul 08 05:43:34 2010 -0700
@@ -236,6 +236,10 @@
 				>
 			</File>
 			<File
+				RelativePath="..\..\include\SDL_clipboard.h"
+				>
+			</File>
+			<File
 				RelativePath="..\..\include\SDL_compat.h"
 				>
 			</File>
@@ -561,6 +565,10 @@
 			>
 		</File>
 		<File
+			RelativePath="..\..\src\video\SDL_clipboard.c"
+			>
+		</File>
+		<File
 			RelativePath="..\..\src\SDL_compat.c"
 			>
 		</File>
@@ -941,6 +949,14 @@
 			>
 		</File>
 		<File
+			RelativePath="..\..\src\video\win32\SDL_win32clipboard.c"
+			>
+		</File>
+		<File
+			RelativePath="..\..\src\video\win32\SDL_win32clipboard.h"
+			>
+		</File>
+		<File
 			RelativePath="..\..\src\video\win32\SDL_win32events.c"
 			>
 		</File>
--- a/VisualC/SDL/SDL_VS2008.vcproj	Thu Jul 08 00:35:58 2010 -0700
+++ b/VisualC/SDL/SDL_VS2008.vcproj	Thu Jul 08 05:43:34 2010 -0700
@@ -5,9 +5,6 @@
 	Name="SDL"
 	ProjectGUID="{81CE8DAF-EBB2-4761-8E45-B71ABCCA8C68}"
 	RootNamespace="SDL"
-	SccProjectName="Perforce Project"
-	SccLocalPath="..\.."
-	SccProvider="MSSCCI:Perforce SCM"
 	TargetFrameworkVersion="131072"
 	>
 	<Platforms>
@@ -386,6 +383,10 @@
 				>
 			</File>
 			<File
+				RelativePath="..\..\include\SDL_clipboard.h"
+				>
+			</File>
+			<File
 				RelativePath="..\..\include\SDL_compat.h"
 				>
 			</File>
@@ -719,6 +720,10 @@
 			>
 		</File>
 		<File
+			RelativePath="..\..\src\video\SDL_clipboard.c"
+			>
+		</File>
+		<File
 			RelativePath="..\..\src\SDL_compat.c"
 			>
 		</File>
@@ -1099,6 +1104,14 @@
 			>
 		</File>
 		<File
+			RelativePath="..\..\src\video\win32\SDL_win32clipboard.c"
+			>
+		</File>
+		<File
+			RelativePath="..\..\src\video\win32\SDL_win32clipboard.h"
+			>
+		</File>
+		<File
 			RelativePath="..\..\src\video\win32\SDL_win32events.c"
 			>
 		</File>
--- a/VisualC/SDLmain/SDLmain_VS2008.vcproj	Thu Jul 08 00:35:58 2010 -0700
+++ b/VisualC/SDLmain/SDLmain_VS2008.vcproj	Thu Jul 08 05:43:34 2010 -0700
@@ -4,9 +4,6 @@
 	Version="9.00"
 	Name="SDLmain"
 	ProjectGUID="{DA956FD3-E142-46F2-9DD5-C78BEBB56B7A}"
-	SccProjectName="Perforce Project"
-	SccLocalPath="..\.."
-	SccProvider="MSSCCI:Perforce SCM"
 	TargetFrameworkVersion="131072"
 	>
 	<Platforms>
--- a/src/video/SDL_clipboard.c	Thu Jul 08 00:35:58 2010 -0700
+++ b/src/video/SDL_clipboard.c	Thu Jul 08 05:43:34 2010 -0700
@@ -30,6 +30,9 @@
 {
     SDL_VideoDevice *_this = SDL_GetVideoDevice();
 
+    if (!text) {
+        text = "";
+    }
     if (_this->SetClipboardText) {
         return _this->SetClipboardText(_this, text);
     } else {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/video/win32/SDL_win32clipboard.c	Thu Jul 08 05:43:34 2010 -0700
@@ -0,0 +1,144 @@
+/*
+    SDL - Simple DirectMedia Layer
+    Copyright (C) 1997-2010 Sam Lantinga
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+    Sam Lantinga
+    slouken@libsdl.org
+*/
+#include "SDL_config.h"
+
+#include "SDL_win32video.h"
+#include "SDL_win32window.h"
+
+
+#ifdef UNICODE
+#define TEXT_FORMAT  CF_UNICODETEXT
+#else
+#define TEXT_FORMAT  CF_TEXT
+#endif
+
+
+/* Get any application owned window handle for clipboard association */
+static HWND
+GetWindowHandle(_THIS)
+{
+    SDL_VideoDisplay *display;
+    SDL_Window *window;
+
+    display = _this->displays;
+    if (display) {
+        window = display->windows;
+        if (window) {
+            return ((SDL_WindowData *) window->driverdata)->hwnd;
+        }
+    }
+    return NULL;
+}
+
+int
+WIN_SetClipboardText(_THIS, const char *text)
+{
+    int result = 0;
+
+    if (OpenClipboard(GetWindowHandle(_this))) {
+        HANDLE hMem;
+        LPTSTR tstr;
+        SIZE_T i, size;
+
+        /* Convert the text from UTF-8 to Windows Unicode */
+        tstr = WIN_UTF8ToString(text);
+        if (!tstr) {
+            return -1;
+        }
+
+        /* Find out the size of the data */
+        for (size = 0, i = 0; tstr[i]; ++i, ++size) {
+            if (tstr[i] == '\n' && (i == 0 || tstr[i-1] != '\r')) {
+                /* We're going to insert a carriage return */
+                ++size;
+            }
+        }
+        size = (size+1)*sizeof(*tstr);
+
+        /* Save the data to the clipboard */
+        hMem = GlobalAlloc(GMEM_MOVEABLE, size);
+        if (hMem) {
+            LPTSTR dst = (LPTSTR)GlobalLock(hMem);
+            /* Copy the text over, adding carriage returns as necessary */
+            for (i = 0; tstr[i]; ++i) {
+                if (tstr[i] == '\n' && (i == 0 || tstr[i-1] != '\r')) {
+                    *dst++ = '\r';
+                }
+                *dst++ = tstr[i];
+            }
+            *dst = 0;
+            GlobalUnlock(hMem);
+
+            EmptyClipboard();
+            if (!SetClipboardData(TEXT_FORMAT, hMem)) {
+                WIN_SetError("Couldn't set clipboard data");
+                result = -1;
+            }
+        }
+        SDL_free(tstr);
+
+        CloseClipboard();
+    } else {
+        WIN_SetError("Couldn't open clipboard");
+        result = -1;
+    }
+    return result;
+}
+
+char *
+WIN_GetClipboardText(_THIS)
+{
+    char *text;
+
+    text = NULL;
+    if (IsClipboardFormatAvailable(TEXT_FORMAT) &&
+        OpenClipboard(GetWindowHandle(_this))) {
+        HANDLE hMem;
+        LPTSTR tstr;
+
+        hMem = GetClipboardData(TEXT_FORMAT);
+        if (hMem) {
+            tstr = (LPTSTR)GlobalLock(hMem);
+            text = WIN_StringToUTF8(tstr);
+            GlobalUnlock(hMem);
+        } else {
+            WIN_SetError("Couldn't get clipboard data");
+        }
+        CloseClipboard();
+    }
+    if (!text) {
+        text = SDL_strdup("");
+    }
+    return text;
+}
+
+SDL_bool
+WIN_HasClipboardText(_THIS)
+{
+    if (IsClipboardFormatAvailable(TEXT_FORMAT)) {
+        return SDL_TRUE;
+    } else {
+        return SDL_FALSE;
+    }
+}
+
+/* vi: set ts=4 sw=4 expandtab: */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/video/win32/SDL_win32clipboard.h	Thu Jul 08 05:43:34 2010 -0700
@@ -0,0 +1,33 @@
+/*
+    SDL - Simple DirectMedia Layer
+    Copyright (C) 1997-2010 Sam Lantinga
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+    Sam Lantinga
+    slouken@libsdl.org
+*/
+#include "SDL_config.h"
+
+#ifndef _SDL_win32clipboard_h
+#define _SDL_win32clipboard_h
+
+extern int WIN_SetClipboardText(_THIS, const char *text);
+extern char *WIN_GetClipboardText(_THIS);
+extern SDL_bool WIN_HasClipboardText(_THIS);
+
+#endif /* _SDL_win32clipboard_h */
+
+/* vi: set ts=4 sw=4 expandtab: */
--- a/src/video/win32/SDL_win32events.c	Thu Jul 08 00:35:58 2010 -0700
+++ b/src/video/win32/SDL_win32events.c	Thu Jul 08 05:43:34 2010 -0700
@@ -585,17 +585,4 @@
     }
 }
 
-/* Sets an error message based on GetLastError() */
-void
-WIN_SetError(const char *prefix)
-{
-    TCHAR buffer[1024];
-    char *message;
-    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
-                  buffer, SDL_arraysize(buffer), NULL);
-    message = WIN_StringToUTF8(buffer);
-    SDL_SetError("%s%s%s", prefix ? prefix : "", prefix ? ": " : "", message);
-    SDL_free(message);
-}
-
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/video/win32/SDL_win32events.h	Thu Jul 08 00:35:58 2010 -0700
+++ b/src/video/win32/SDL_win32events.h	Thu Jul 08 05:43:34 2010 -0700
@@ -31,7 +31,6 @@
 extern LRESULT CALLBACK WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam,
                                        LPARAM lParam);
 extern void WIN_PumpEvents(_THIS);
-extern void WIN_SetError(const char *prefix);
 
 #endif /* _SDL_win32events_h */
 
--- a/src/video/win32/SDL_win32video.c	Thu Jul 08 00:35:58 2010 -0700
+++ b/src/video/win32/SDL_win32video.c	Thu Jul 08 05:43:34 2010 -0700
@@ -28,6 +28,7 @@
 #include "../SDL_pixels_c.h"
 
 #include "SDL_win32video.h"
+#include "SDL_win32clipboard.h"
 #include "SDL_d3drender.h"
 #include "SDL_gdirender.h"
 
@@ -35,6 +36,19 @@
 static int WIN_VideoInit(_THIS);
 static void WIN_VideoQuit(_THIS);
 
+/* Sets an error message based on GetLastError() */
+void
+WIN_SetError(const char *prefix)
+{
+    TCHAR buffer[1024];
+    char *message;
+    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
+                  buffer, SDL_arraysize(buffer), NULL);
+    message = WIN_StringToUTF8(buffer);
+    SDL_SetError("%s%s%s", prefix ? prefix : "", prefix ? ": " : "", message);
+    SDL_free(message);
+}
+
 /* WIN32 driver bootstrap functions */
 
 static int
@@ -163,6 +177,10 @@
     device->GL_DeleteContext = WIN_GL_DeleteContext;
 #endif
 
+    device->SetClipboardText = WIN_SetClipboardText;
+    device->GetClipboardText = WIN_GetClipboardText;
+    device->HasClipboardText = WIN_HasClipboardText;
+
     device->free = WIN_DeleteDevice;
 
     return device;
--- a/src/video/win32/SDL_win32video.h	Thu Jul 08 00:35:58 2010 -0700
+++ b/src/video/win32/SDL_win32video.h	Thu Jul 08 05:43:34 2010 -0700
@@ -60,6 +60,7 @@
 #define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "ASCII", (char *)S, (SDL_strlen(S)+1))
 #define WIN_UTF8ToString(S) SDL_iconv_string("ASCII", "UTF-8", (char *)S, SDL_strlen(S)+1)
 #endif
+extern void WIN_SetError(const char *prefix);
 
 /* Private display data */
 
--- a/src/video/win32/SDL_win32window.c	Thu Jul 08 00:35:58 2010 -0700
+++ b/src/video/win32/SDL_win32window.c	Thu Jul 08 05:43:34 2010 -0700
@@ -633,8 +633,7 @@
     /* Register the class. */
     SDL_HelperWindowClass = RegisterClass(&wce);
     if (SDL_HelperWindowClass == 0) {
-        SDL_SetError("Unable to create Helper Window Class: error %d.",
-                     GetLastError());
+        WIN_SetError("Unable to create Helper Window Class");
         return -1;
     }
 
@@ -652,8 +651,7 @@
                                       hInstance, NULL);
     if (SDL_HelperWindow == NULL) {
         UnregisterClass(SDL_HelperWindowClassName, hInstance);
-        SDL_SetError("Unable to create Helper Window: error %d.",
-                     GetLastError());
+        WIN_SetError("Unable to create Helper Window");
         return -1;
     }
 
@@ -672,8 +670,7 @@
     /* Destroy the window. */
     if (SDL_HelperWindow != NULL) {
         if (DestroyWindow(SDL_HelperWindow) == 0) {
-            SDL_SetError("Unable to destroy Helper Window: error %d.",
-                         GetLastError());
+            WIN_SetError("Unable to destroy Helper Window");
             return;
         }
         SDL_HelperWindow = NULL;
@@ -682,8 +679,7 @@
     /* Unregister the class. */
     if (SDL_HelperWindowClass != 0) {
         if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
-            SDL_SetError("Unable to destroy Helper Window Class: error %d.",
-                         GetLastError());
+            WIN_SetError("Unable to destroy Helper Window Class");
             return;
         }
         SDL_HelperWindowClass = 0;