ReactOS  0.4.13-dev-257-gfabbd7c
clipboard.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS user32.dll
3  * FILE: win32ss/user/user32/windows/clipboard.c
4  * PURPOSE: Input
5  * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
6  * Pablo Borobia <pborobia@gmail.com>
7  * UPDATE HISTORY:
8  * 09-05-2001 CSH Created
9  *
10  */
11 
12 #include <user32.h>
13 
14 #define NDEBUG
15 
17 
18 /*
19  * @implemented
20  */
21 BOOL
22 WINAPI
23 OpenClipboard(HWND hWndNewOwner)
24 {
25  return NtUserOpenClipboard(hWndNewOwner, 0);
26 }
27 
28 /*
29  * @implemented
30  */
31 UINT
32 WINAPI
34 {
37 }
38 
39 /*
40  * @implemented
41  */
42 INT
43 WINAPI
45  LPSTR lpszFormatName,
46  int cchMaxCount)
47 {
49  INT Length;
50 
51  lpBuffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, cchMaxCount * sizeof(WCHAR));
52  if (!lpBuffer)
53  {
55  return 0;
56  }
57 
58  /* we need a UNICODE string */
60 
61  if (Length != 0)
62  {
63  if (!WideCharToMultiByte(CP_ACP, 0, lpBuffer, Length, lpszFormatName, cchMaxCount, NULL, NULL))
64  {
65  /* clear result string */
66  Length = 0;
67  }
68  lpszFormatName[Length] = '\0';
69  }
70 
71  RtlFreeHeap(RtlGetProcessHeap(), 0, lpBuffer);
72  return Length;
73 }
74 
75 /*
76  * @implemented
77  */
78 INT
79 WINAPI
81  LPWSTR lpszFormatName,
82  INT cchMaxCount)
83 {
84  return NtUserGetClipboardFormatName(uFormat, lpszFormatName, cchMaxCount);
85 }
86 
87 /*
88  * @implemented
89  */
90 UINT
91 WINAPI
93 {
94  UINT ret = 0;
95  UNICODE_STRING usFormat = {0};
96 
97  if (lpszFormat == NULL)
98  {
100  return 0;
101  }
102 
103  /* check for "" */
104  if (*lpszFormat == 0) //NULL
105  {
107  return 0;
108  }
109 
110  ret = RtlCreateUnicodeStringFromAsciiz(&usFormat, lpszFormat);
111  if (ret)
112  {
113  ret = NtUserRegisterWindowMessage(&usFormat); //(LPCWSTR)
114  RtlFreeUnicodeString(&usFormat);
115  }
116 
117  return ret;
118 }
119 
120 /*
121  * @implemented
122  */
123 UINT
124 WINAPI
126 {
127  UINT ret = 0;
128  UNICODE_STRING usFormat = {0};
129 
130  if (lpszFormat == NULL)
131  {
133  return 0;
134  }
135 
136  /* check for "" */
137  if (*lpszFormat == 0) //NULL
138  {
140  return 0;
141  }
142 
143  RtlInitUnicodeString(&usFormat, lpszFormat);
144  ret = NtUserRegisterWindowMessage(&usFormat);
145 
146  return ret;
147 }
148 
149 static PVOID WINAPI
151 {
152  HANDLE hGlobal;
153  PVOID pGlobal;
154  INT cbGlobal;
155 
156  cbGlobal = WideCharToMultiByte(bOem ? CP_OEMCP : CP_ACP,
157  0, pwStr, cbStr / sizeof(WCHAR),
158  NULL, 0, NULL, NULL);
159  hGlobal = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, cbGlobal);
160  if (!hGlobal)
161  return NULL;
162 
163  pGlobal = GlobalLock(hGlobal);
165  0, pwStr, cbStr / sizeof(WCHAR),
166  pGlobal, cbGlobal, NULL, NULL);
167  return pGlobal;
168 }
169 
170 static PVOID WINAPI
172 {
173  HANDLE hGlobal;
174  PVOID pGlobal;
175  INT cbGlobal;
176 
177  cbGlobal = MultiByteToWideChar(bOem ? CP_OEMCP : CP_ACP,
178  0, pwStr, cbStr, NULL, 0) * sizeof(WCHAR);
179  hGlobal = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, cbGlobal);
180  if (!hGlobal)
181  return NULL;
182 
183  pGlobal = GlobalLock(hGlobal);
185  0, pwStr, cbStr, pGlobal, cbGlobal);
186  return pGlobal;
187 }
188 
189 /*
190  * @implemented
191  */
192 HANDLE
193 WINAPI
195 {
196  HANDLE hData = NULL;
197  PVOID pData = NULL;
198  DWORD cbData = 0;
200 
201  hData = NtUserGetClipboardData(uFormat, &gcd);
202  if (!hData)
203  return NULL;
204 
205  if (gcd.fGlobalHandle)
206  {
207  HANDLE hGlobal;
208 
209  NtUserCreateLocalMemHandle(hData, NULL, 0, &cbData);
210  hGlobal = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, cbData);
211  pData = GlobalLock(hGlobal);
212  NtUserCreateLocalMemHandle(hData, pData, cbData, NULL);
213  hData = hGlobal;
214  }
215 
216  if (gcd.uFmtRet != uFormat)
217  {
218  SETCLIPBDATA scd = {FALSE, FALSE};
219  HANDLE hNewData = NULL;
220  PVOID pNewData = NULL;
221 
222  /* Synthesize requested format */
223  switch (uFormat)
224  {
225  case CF_TEXT:
226  if (gcd.uFmtRet == CF_UNICODETEXT)
227  pNewData = IntSynthesizeMultiByte(pData, cbData, uFormat == CF_OEMTEXT);
228  else // CF_OEMTEXT
229  OemToCharBuffA(pData, pData, cbData);
230  break;
231  case CF_OEMTEXT:
232  if (gcd.uFmtRet == CF_UNICODETEXT)
233  pNewData = IntSynthesizeMultiByte(pData, cbData, uFormat == CF_OEMTEXT);
234  else
235  CharToOemBuffA(pData, pData, cbData);
236  break;
237  case CF_UNICODETEXT:
238  pNewData = IntSynthesizeWideChar(pData, cbData, gcd.uFmtRet == CF_OEMTEXT);
239  break;
240  default:
241  FIXME("Format: %u != %u\n", uFormat, gcd.uFmtRet);
242  }
243 
244  /* Is it a global handle? */
245  if (pNewData)
246  hNewData = GlobalHandle(pNewData);
247 
248  if (hNewData)
249  {
250  /* Free old data */
251  if (pData)
252  {
253  GlobalUnlock(hData);
254  GlobalFree(hData);
255  }
256  hData = hNewData;
257  pData = pNewData;
258  }
259 
260  /* Save synthesized format in clipboard */
261  if (pData)
262  {
263  HANDLE hMem;
264 
265  scd.fGlobalHandle = TRUE;
266  hMem = NtUserConvertMemHandle(pData, GlobalSize(hData));
267  NtUserSetClipboardData(uFormat, hMem, &scd);
268  }
269  else if (hData)
270  NtUserSetClipboardData(uFormat, hData, &scd);
271  }
272 
273  /* Unlock global handle */
274  if (pData)
275  GlobalUnlock(hData);
276 
277  return hData;
278 }
279 
280 /*
281  * @implemented
282  */
283 HANDLE
284 WINAPI
286 {
287  DWORD dwSize;
288  HANDLE hGlobal;
289  LPVOID pMem;
290  HANDLE hRet = NULL;
291  SETCLIPBDATA scd = {FALSE, FALSE};
292 
293  /* Check if this is a delayed rendering */
294  if (hMem == NULL)
295  return NtUserSetClipboardData(uFormat, NULL, &scd);
296 
297  if (hMem <= (HANDLE)4)
299  /* Bitmaps and palette does not use global handles */
300  else if (uFormat == CF_BITMAP || uFormat == CF_DSPBITMAP || uFormat == CF_PALETTE)
301  hRet = NtUserSetClipboardData(uFormat, hMem, &scd);
302  /* Meta files are probably checked for validity */
303  else if (uFormat == CF_DSPMETAFILEPICT || uFormat == CF_METAFILEPICT ||
304  uFormat == CF_DSPENHMETAFILE || uFormat == CF_ENHMETAFILE)
305  {
307  hRet = NULL; // not supported yet
308  }
309  else
310  {
311  /* Some formats accept only global handles, other accept global handles or integer values */
312  pMem = GlobalLock(hMem);
313  dwSize = GlobalSize(hMem);
314 
315  if (pMem || uFormat == CF_DIB || uFormat == CF_DIBV5 ||
316  uFormat == CF_DSPTEXT || uFormat == CF_LOCALE ||
317  uFormat == CF_OEMTEXT || uFormat == CF_TEXT ||
318  uFormat == CF_UNICODETEXT)
319  {
320  if (pMem)
321  {
322  /* This is a local memory. Make global memory object */
323  hGlobal = NtUserConvertMemHandle(pMem, dwSize);
324 
325  /* Unlock memory */
326  GlobalUnlock(hMem);
327  /* FIXME: free hMem when CloseClipboard is called */
328 
329  if (hGlobal)
330  {
331  /* Save data */
332  scd.fGlobalHandle = TRUE;
333  hRet = NtUserSetClipboardData(uFormat, hGlobal, &scd);
334  }
335 
336  /* On success NtUserSetClipboardData returns pMem
337  however caller expects us to return hMem */
338  if (hRet == hGlobal)
339  hRet = hMem;
340  }
341  else
343  }
344  else
345  {
346  /* Save a number */
347  hRet = NtUserSetClipboardData(uFormat, hMem, &scd);
348  }
349  }
350 
351  if (!hRet)
352  ERR("SetClipboardData(%u, %p) failed\n", uFormat, hMem);
353 
354  return hRet;
355 }
356 
357 /*
358  * @unimplemented
359  */
360 BOOL
361 WINAPI
363 {
365  return FALSE;
366 }
367 /*
368  * @unimplemented
369  */
370 BOOL
371 WINAPI
373 {
375  return FALSE;
376 }
377 
378 /*
379  * @unimplemented
380  */
381 BOOL
382 WINAPI
384  UINT cFormats,
385  PUINT pcFormatsOut)
386 {
388  return FALSE;
389 }
INT APIENTRY NtUserGetClipboardFormatName(UINT fmt, LPWSTR lpszFormatName, INT cchMaxCount)
Definition: clipboard.c:718
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
BOOL WINAPI AddClipboardFormatListener(HWND hwnd)
Definition: clipboard.c:362
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
#define TRUE
Definition: types.h:120
HGLOBAL NTAPI GlobalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:368
#define WideCharToMultiByte
Definition: compat.h:101
#define CF_ENHMETAFILE
Definition: constants.h:409
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define CP_ACP
Definition: compat.h:99
#define CF_DSPMETAFILEPICT
Definition: constants.h:416
#define ERROR_INVALID_HANDLE
Definition: compat.h:88
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:603
HANDLE WINAPI GetClipboardData(UINT uFormat)
Definition: clipboard.c:194
HGLOBAL NTAPI GlobalHandle(LPCVOID pMem)
Definition: heapmem.c:705
#define CF_METAFILEPICT
Definition: constants.h:398
char * LPSTR
Definition: xmlstorage.h:182
#define NO_ERROR
Definition: dderror.h:5
int32_t INT
Definition: typedefs.h:56
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl.h:1546
SIZE_T NTAPI GlobalSize(HGLOBAL hMem)
Definition: heapmem.c:1090
BOOL WINAPI GetUpdatedClipboardFormats(PUINT lpuiFormats, UINT cFormats, PUINT pcFormatsOut)
Definition: clipboard.c:383
BOOL WINAPI CharToOemBuffA(_In_ LPCSTR lpszSrc, _Out_writes_(cchDstLength) LPSTR lpszDst, _In_ DWORD cchDstLength)
unsigned int BOOL
Definition: ntddk_ex.h:94
HANDLE APIENTRY NtUserGetClipboardData(UINT fmt, PGETCLIPBDATA pgcd)
Definition: clipboard.c:879
#define FIXME(fmt,...)
Definition: debug.h:110
UINT WINAPI RegisterClipboardFormatW(LPCWSTR lpszFormat)
Definition: clipboard.c:125
static TAGREF LPCWSTR LPDWORD LPVOID lpBuffer
Definition: db.cpp:173
#define CF_DSPENHMETAFILE
Definition: constants.h:417
BOOL fGlobalHandle
Definition: ntuser.h:1117
#define CF_UNICODETEXT
Definition: constants.h:408
#define CF_BITMAP
Definition: constants.h:397
smooth NULL
Definition: ftsmooth.c:416
WINE_DEFAULT_DEBUG_CHANNEL(ole)
NTSTATUS APIENTRY NtUserCreateLocalMemHandle(HANDLE hMem, PVOID pData, DWORD cbData, DWORD *pcbData)
Definition: clipboard.c:1204
NTSYSAPI BOOLEAN NTAPI RtlCreateUnicodeStringFromAsciiz(_Out_ PUNICODE_STRING Destination, _In_ PCSZ Source)
const char * LPCSTR
Definition: xmlstorage.h:183
#define CF_TEXT
Definition: constants.h:396
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:585
INT WINAPI GetClipboardFormatNameW(UINT uFormat, LPWSTR lpszFormatName, INT cchMaxCount)
Definition: clipboard.c:80
static PVOID WINAPI IntSynthesizeMultiByte(PVOID pwStr, DWORD cbStr, BOOL bOem)
Definition: clipboard.c:150
HANDLE WINAPI SetClipboardData(UINT uFormat, HANDLE hMem)
Definition: clipboard.c:285
BOOL WINAPI OpenClipboard(HWND hWndNewOwner)
Definition: clipboard.c:23
static PVOID WINAPI IntSynthesizeWideChar(PVOID pwStr, DWORD cbStr, BOOL bOem)
Definition: clipboard.c:171
HANDLE APIENTRY NtUserSetClipboardData(UINT fmt, HANDLE hData, PSETCLIPBDATA pUnsafeScd)
Definition: clipboard.c:1056
__wchar_t WCHAR
Definition: xmlstorage.h:180
HANDLE APIENTRY NtUserConvertMemHandle(PVOID pData, DWORD cbData)
Definition: clipboard.c:1159
#define CF_DSPBITMAP
Definition: constants.h:415
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
HGLOBAL NTAPI GlobalFree(HGLOBAL hMem)
Definition: heapmem.c:611
#define SetLastError(x)
Definition: compat.h:409
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
int ret
UINT NTAPI NtUserRegisterWindowMessage(PUNICODE_STRING MessageName)
Definition: window.c:4069
LPVOID NTAPI GlobalLock(HGLOBAL hMem)
Definition: heapmem.c:755
#define CF_PALETTE
Definition: constants.h:404
UINT WINAPI RegisterClipboardFormatA(LPCSTR lpszFormat)
Definition: clipboard.c:92
#define CF_LOCALE
Definition: constants.h:411
#define ERR(fmt,...)
Definition: debug.h:109
#define CF_OEMTEXT
Definition: constants.h:402
INT WINAPI GetClipboardFormatNameA(UINT format, LPSTR lpszFormatName, int cchMaxCount)
Definition: clipboard.c:44
BOOL APIENTRY NtUserOpenClipboard(HWND hWnd, DWORD Unknown1)
Definition: clipboard.c:515
unsigned int UINT
Definition: ndis.h:50
BOOL NTAPI GlobalUnlock(HGLOBAL hMem)
Definition: heapmem.c:1190
BOOL WINAPI RemoveClipboardFormatListener(HWND hwnd)
Definition: clipboard.c:372
#define MultiByteToWideChar
Definition: compat.h:100
EXTINLINE UINT NtUserxEnumClipboardFormats(UINT format)
Definition: ntwrapper.h:611
UINT WINAPI EnumClipboardFormats(UINT format)
Definition: clipboard.c:33
#define GMEM_DDESHARE
Definition: winbase.h:295
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define UNIMPLEMENTED
Definition: debug.h:114
#define ERROR_INVALID_NAME
Definition: compat.h:93
#define CF_DSPTEXT
Definition: constants.h:414
WCHAR * LPWSTR
Definition: xmlstorage.h:184
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1827
BOOL WINAPI OemToCharBuffA(_In_ LPCSTR lpszSrc, _Out_writes_(cchDstLength) LPSTR lpszDst, _In_ DWORD cchDstLength)
#define CP_OEMCP
Definition: winnls.h:228
#define GMEM_MOVEABLE
Definition: winbase.h:291
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
unsigned int * PUINT
Definition: ndis.h:50
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13
static int gcd(int, int)
Definition: getopt.c:86
#define CF_DIB
Definition: constants.h:403