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)
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld [DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld if[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp local skip1(dst_w_bpp<=(lowbit *8)) &&((lowbit *8)<(pixblock_size *dst_w_bpp)) .if lowbit< 16 tst DST_R
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:317
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:161
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)