SDL  2.0
SDL_windowsclipboard.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #if SDL_VIDEO_DRIVER_WINDOWS
24 
25 #include "SDL_windowsvideo.h"
26 #include "SDL_windowswindow.h"
27 #include "../../events/SDL_clipboardevents_c.h"
28 
29 
30 #ifdef UNICODE
31 #define TEXT_FORMAT CF_UNICODETEXT
32 #else
33 #define TEXT_FORMAT CF_TEXT
34 #endif
35 
36 
37 /* Get any application owned window handle for clipboard association */
38 static HWND
39 GetWindowHandle(_THIS)
40 {
42 
43  window = _this->windows;
44  if (window) {
45  return ((SDL_WindowData *) window->driverdata)->hwnd;
46  }
47  return NULL;
48 }
49 
50 int
51 WIN_SetClipboardText(_THIS, const char *text)
52 {
54  int result = 0;
55 
56  if (OpenClipboard(GetWindowHandle(_this))) {
57  HANDLE hMem;
58  LPTSTR tstr;
59  SIZE_T i, size;
60 
61  /* Convert the text from UTF-8 to Windows Unicode */
62  tstr = WIN_UTF8ToString(text);
63  if (!tstr) {
64  return -1;
65  }
66 
67  /* Find out the size of the data */
68  for (size = 0, i = 0; tstr[i]; ++i, ++size) {
69  if (tstr[i] == '\n' && (i == 0 || tstr[i-1] != '\r')) {
70  /* We're going to insert a carriage return */
71  ++size;
72  }
73  }
74  size = (size+1)*sizeof(*tstr);
75 
76  /* Save the data to the clipboard */
77  hMem = GlobalAlloc(GMEM_MOVEABLE, size);
78  if (hMem) {
79  LPTSTR dst = (LPTSTR)GlobalLock(hMem);
80  if (dst) {
81  /* Copy the text over, adding carriage returns as necessary */
82  for (i = 0; tstr[i]; ++i) {
83  if (tstr[i] == '\n' && (i == 0 || tstr[i-1] != '\r')) {
84  *dst++ = '\r';
85  }
86  *dst++ = tstr[i];
87  }
88  *dst = 0;
89  GlobalUnlock(hMem);
90  }
91 
92  EmptyClipboard();
93  if (!SetClipboardData(TEXT_FORMAT, hMem)) {
94  result = WIN_SetError("Couldn't set clipboard data");
95  }
96  data->clipboard_count = GetClipboardSequenceNumber();
97  }
98  SDL_free(tstr);
99 
100  CloseClipboard();
101  } else {
102  result = WIN_SetError("Couldn't open clipboard");
103  }
104  return result;
105 }
106 
107 char *
109 {
110  char *text;
111 
112  text = NULL;
113  if (IsClipboardFormatAvailable(TEXT_FORMAT) &&
114  OpenClipboard(GetWindowHandle(_this))) {
115  HANDLE hMem;
116  LPTSTR tstr;
117 
118  hMem = GetClipboardData(TEXT_FORMAT);
119  if (hMem) {
120  tstr = (LPTSTR)GlobalLock(hMem);
121  text = WIN_StringToUTF8(tstr);
122  GlobalUnlock(hMem);
123  } else {
124  WIN_SetError("Couldn't get clipboard data");
125  }
126  CloseClipboard();
127  }
128  if (!text) {
129  text = SDL_strdup("");
130  }
131  return text;
132 }
133 
134 SDL_bool
136 {
138  char *text = WIN_GetClipboardText(_this);
139  if (text) {
140  result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
141  SDL_free(text);
142  }
143  return result;
144 }
145 
146 void
148 {
149  const DWORD count = GetClipboardSequenceNumber();
150  if (count != data->clipboard_count) {
151  if (data->clipboard_count) {
153  }
154  data->clipboard_count = count;
155  }
156 }
157 
158 #endif /* SDL_VIDEO_DRIVER_WINDOWS */
159 
160 /* vi: set ts=4 sw=4 expandtab: */
NSInteger clipboard_count
#define WIN_UTF8ToString(S)
Definition: SDL_windows.h:47
GLuint64EXT * result
GLenum GLenum dst
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
char * WIN_GetClipboardText(_THIS)
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
#define _THIS
int SDL_SendClipboardUpdate(void)
#define SDL_free
#define WIN_StringToUTF8(S)
Definition: SDL_windows.h:46
SDL_bool WIN_HasClipboardText(_THIS)
SDL_Window * windows
Definition: SDL_sysvideo.h:313
GLsizeiptr size
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:164
SDL_bool
Definition: SDL_stdinc.h:139
int WIN_SetError(const char *prefix)
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
IUnknown DWORD DWORD
Definition: SDL_msctf.h:234
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
#define SDL_strdup
The type used to identify a window.
Definition: SDL_sysvideo.h:73
void * driverdata
Definition: SDL_sysvideo.h:111
void WIN_CheckClipboardUpdate(struct SDL_VideoData *data)
int WIN_SetClipboardText(_THIS, const char *text)