1 /*
2 * WIN32 clipboard implementation
3 *
4 * Copyright 1994 Martin Ayotte
5 * 1996 Alex Korobka
6 * 1999 Noel Borthwick
7 * 2003 Ulrich Czekalla for CodeWeavers
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 *
23 * NOTES:
24 * This file contains the implementation for the WIN32 Clipboard API
25 * and Wine's internal clipboard cache.
26 * The actual contents of the clipboard are held in the clipboard cache.
27 * The internal implementation talks to a "clipboard driver" to fill or
28 * expose the cache to the native device. (Currently only the X11 and
29 * TTY clipboard driver are available)
30 */
31
32 #include "config.h"
33 #include "wine/port.h"
34
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <sys/types.h>
38 #include <fcntl.h>
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42 #include <string.h>
43
44 #include "windef.h"
45 #include "winbase.h"
46 #include "wingdi.h"
47 #include "winuser.h"
48 #include "winerror.h"
49 #include "wine/winbase16.h"
50 #include "user_private.h"
51 #include "win.h"
52
53 #include "wine/debug.h"
54 #include "wine/unicode.h"
55 #include "wine/server.h"
56
57 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
58
59 #define CF_REGFORMATBASE 0xC000
60
61 typedef struct
62 {
63 HWND hWndOpen;
64 HWND hWndOwner;
65 HWND hWndViewer;
66 UINT seqno;
67 UINT flags;
68 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
69
70 /*
71 * Indicates if data has changed since open.
72 */
73 static BOOL bCBHasChanged = FALSE;
74
75
76 /**************************************************************************
77 * CLIPBOARD_SetClipboardOwner
78 *
79 * Set the global wineserver clipboard owner. The current process will
80 * be the owner and <hWnd> will get the render notifications.
81 */
82 static BOOL CLIPBOARD_SetClipboardOwner(HWND hWnd)
83 {
84 BOOL bRet;
85
86 TRACE(" hWnd(%p)\n", hWnd);
87
88 SERVER_START_REQ( set_clipboard_info )
89 {
90 req->flags = SET_CB_OWNER;
91 req->owner = wine_server_user_handle( hWnd );
92 bRet = !wine_server_call_err( req );
93 }
94 SERVER_END_REQ;
95
96 return bRet;
97 }
98
99
100 /**************************************************************************
101 * CLIPBOARD_GetClipboardInfo
102 */
103 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
104 {
105 BOOL bRet;
106
107 SERVER_START_REQ( set_clipboard_info )
108 {
109 req->flags = 0;
110
111 if (((bRet = !wine_server_call_err( req ))))
112 {
113 cbInfo->hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
114 cbInfo->hWndOwner = wine_server_ptr_handle( reply->old_owner );
115 cbInfo->hWndViewer = wine_server_ptr_handle( reply->old_viewer );
116 cbInfo->seqno = reply->seqno;
117 cbInfo->flags = reply->flags;
118 }
119 }
120 SERVER_END_REQ;
121
122 return bRet;
123 }
124
125
126 /**************************************************************************
127 * CLIPBOARD_ReleaseOwner
128 */
129 BOOL CLIPBOARD_ReleaseOwner(void)
130 {
131 BOOL bRet = FALSE;
132
133 SERVER_START_REQ( set_clipboard_info )
134 {
135 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
136
137 if (wine_server_call_err( req ))
138 {
139 ERR("Failed to set clipboard.\n");
140 }
141 else
142 {
143 bRet = TRUE;
144 }
145 }
146 SERVER_END_REQ;
147
148 return bRet;
149 }
150
151
152 /**************************************************************************
153 * CLIPBOARD_OpenClipboard
154 */
155 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
156 {
157 BOOL bRet;
158
159 SERVER_START_REQ( set_clipboard_info )
160 {
161 req->flags = SET_CB_OPEN;
162 req->clipboard = wine_server_user_handle( hWnd );
163 bRet = !wine_server_call( req );
164 }
165 SERVER_END_REQ;
166
167 return bRet;
168 }
169
170
171 /**************************************************************************
172 * CLIPBOARD_CloseClipboard
173 */
174 static BOOL CLIPBOARD_CloseClipboard(void)
175 {
176 BOOL bRet;
177
178 TRACE(" Changed=%d\n", bCBHasChanged);
179
180 SERVER_START_REQ( set_clipboard_info )
181 {
182 req->flags = SET_CB_CLOSE;
183 if (bCBHasChanged) req->flags |= SET_CB_SEQNO;
184 bRet = !wine_server_call_err( req );
185 }
186 SERVER_END_REQ;
187
188 return bRet;
189 }
190
191 /**************************************************************************
192 * CLIPBOARD_SetClipboardViewer
193 */
194 static HWND CLIPBOARD_SetClipboardViewer( HWND hWnd )
195 {
196 HWND hwndPrev = 0;
197
198 SERVER_START_REQ( set_clipboard_info )
199 {
200 req->flags = SET_CB_VIEWER;
201 req->viewer = wine_server_user_handle( hWnd );
202 if (!wine_server_call_err( req ))
203 hwndPrev = wine_server_ptr_handle( reply->old_viewer );
204 }
205 SERVER_END_REQ;
206
207 return hwndPrev;
208 }
209
210 /**************************************************************************
211 * WIN32 Clipboard implementation
212 **************************************************************************/
213
214 /**************************************************************************
215 * RegisterClipboardFormatW (USER32.@)
216 */
217 UINT WINAPI RegisterClipboardFormatW(LPCWSTR FormatName)
218 {
219 return USER_Driver->pRegisterClipboardFormat(FormatName);
220 }
221
222
223 /**************************************************************************
224 * RegisterClipboardFormatA (USER32.@)
225 */
226 UINT WINAPI RegisterClipboardFormatA(LPCSTR formatName)
227 {
228 int len;
229 LPWSTR wFormat;
230 UINT ret;
231
232 len = MultiByteToWideChar(CP_ACP, 0, formatName, -1, NULL, 0);
233 wFormat = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
234 MultiByteToWideChar(CP_ACP, 0, formatName, -1, wFormat, len);
235
236 ret = RegisterClipboardFormatW(wFormat);
237 HeapFree(GetProcessHeap(), 0, wFormat);
238 return ret;
239 }
240
241
242 /**************************************************************************
243 * GetClipboardFormatNameW (USER32.@)
244 */
245 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
246 {
247 return USER_Driver->pGetClipboardFormatName(wFormat, retStr, maxlen);
248 }
249
250
251 /**************************************************************************
252 * GetClipboardFormatNameA (USER32.@)
253 */
254 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
255 {
256 INT ret;
257 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen*sizeof(WCHAR) );
258 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
259
260 ret = GetClipboardFormatNameW( wFormat, p, maxlen );
261
262 if (ret && maxlen > 0 && !WideCharToMultiByte( CP_ACP, 0, p, -1, retStr, maxlen, 0, 0))
263 retStr[maxlen-1] = 0;
264 HeapFree( GetProcessHeap(), 0, p );
265 return ret;
266 }
267
268
269 /**************************************************************************
270 * OpenClipboard (USER32.@)
271 *
272 * Note: Netscape uses NULL hWnd to open the clipboard.
273 */
274 BOOL WINAPI OpenClipboard( HWND hWnd )
275 {
276 BOOL bRet;
277
278 TRACE("(%p)...\n", hWnd);
279
280 bRet = CLIPBOARD_OpenClipboard(hWnd);
281
282 TRACE(" returning %i\n", bRet);
283
284 return bRet;
285 }
286
287
288 /**************************************************************************
289 * CloseClipboard (USER32.@)
290 */
291 BOOL WINAPI CloseClipboard(void)
292 {
293 BOOL bRet = FALSE;
294
295 TRACE("(%d)\n", bCBHasChanged);
296
297 if (CLIPBOARD_CloseClipboard())
298 {
299 if (bCBHasChanged)
300 {
301 HWND hWndViewer = GetClipboardViewer();
302
303 USER_Driver->pEndClipboardUpdate();
304
305 if (hWndViewer)
306 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
307
308 bCBHasChanged = FALSE;
309 }
310
311 bRet = TRUE;
312 }
313
314 return bRet;
315 }
316
317
318 /**************************************************************************
319 * EmptyClipboard (USER32.@)
320 * Empties and acquires ownership of the clipboard
321 */
322 BOOL WINAPI EmptyClipboard(void)
323 {
324 CLIPBOARDINFO cbinfo;
325
326 TRACE("()\n");
327
328 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
329 ~cbinfo.flags & CB_OPEN)
330 {
331 WARN("Clipboard not opened by calling task!\n");
332 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
333 return FALSE;
334 }
335
336 /* Destroy private objects */
337 if (cbinfo.hWndOwner)
338 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
339
340 /* Tell the driver to acquire the selection. The current owner
341 * will be signaled to delete it's own cache. */
342
343 /* Assign ownership of the clipboard to the current client. We do
344 * this before acquiring the selection so that when we do acquire the
345 * selection and the selection loser gets notified, it can check if
346 * it has lost the Wine clipboard ownership. If it did then it knows
347 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
348 * lost the selection to a X app and it should send the
349 * WM_DESTROYCLIPBOARD itself. */
350 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
351
352 /* Acquire the selection. This will notify the previous owner
353 * to clear it's cache. */
354 USER_Driver->pAcquireClipboard(cbinfo.hWndOpen);
355
356 /* Empty the local cache */
357 USER_Driver->pEmptyClipboard(FALSE);
358
359 bCBHasChanged = TRUE;
360
361 return TRUE;
362 }
363
364
365 /**************************************************************************
366 * GetClipboardOwner (USER32.@)
367 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
368 */
369 HWND WINAPI GetClipboardOwner(void)
370 {
371 HWND hWndOwner = 0;
372
373 SERVER_START_REQ( set_clipboard_info )
374 {
375 req->flags = 0;
376 if (!wine_server_call_err( req )) hWndOwner = wine_server_ptr_handle( reply->old_owner );
377 }
378 SERVER_END_REQ;
379
380 TRACE(" hWndOwner(%p)\n", hWndOwner);
381
382 return hWndOwner;
383 }
384
385
386 /**************************************************************************
387 * GetOpenClipboardWindow (USER32.@)
388 */
389 HWND WINAPI GetOpenClipboardWindow(void)
390 {
391 HWND hWndOpen = 0;
392
393 SERVER_START_REQ( set_clipboard_info )
394 {
395 req->flags = 0;
396 if (!wine_server_call_err( req )) hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
397 }
398 SERVER_END_REQ;
399
400 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
401
402 return hWndOpen;
403 }
404
405
406 /**************************************************************************
407 * SetClipboardViewer (USER32.@)
408 */
409 HWND WINAPI SetClipboardViewer( HWND hWnd )
410 {
411 HWND hwndPrev = CLIPBOARD_SetClipboardViewer(hWnd);
412
413 if (hWnd)
414 SendMessageW(hWnd, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
415 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
416
417 return hwndPrev;
418 }
419
420
421 /**************************************************************************
422 * GetClipboardViewer (USER32.@)
423 */
424 HWND WINAPI GetClipboardViewer(void)
425 {
426 HWND hWndViewer = 0;
427
428 SERVER_START_REQ( set_clipboard_info )
429 {
430 req->flags = 0;
431 if (!wine_server_call_err( req )) hWndViewer = wine_server_ptr_handle( reply->old_viewer );
432 }
433 SERVER_END_REQ;
434
435 TRACE(" hWndViewer=%p\n", hWndViewer);
436
437 return hWndViewer;
438 }
439
440
441 /**************************************************************************
442 * ChangeClipboardChain (USER32.@)
443 */
444 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
445 {
446 BOOL bRet = TRUE;
447 HWND hWndViewer = GetClipboardViewer();
448
449 if (hWndViewer)
450 {
451 if (WIN_GetFullHandle(hWnd) == hWndViewer)
452 CLIPBOARD_SetClipboardViewer(WIN_GetFullHandle(hWndNext));
453 else
454 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
455 }
456 else
457 ERR("hWndViewer is lost\n");
458
459 return bRet;
460 }
461
462
463 /**************************************************************************
464 * SetClipboardData (USER.141)
465 */
466 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
467 {
468 CLIPBOARDINFO cbinfo;
469 HANDLE16 hResult = 0;
470
471 TRACE("(%04X, %04x) !\n", wFormat, hData);
472
473 /* If it's not owned, data can only be set if the format doesn't exists
474 and its rendering is not delayed */
475 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
476 (!(cbinfo.flags & CB_OWNER) && !hData))
477 {
478 WARN("Clipboard not owned by calling task. Operation failed.\n");
479 return 0;
480 }
481
482 if (USER_Driver->pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
483 {
484 hResult = hData;
485 bCBHasChanged = TRUE;
486 }
487
488 return hResult;
489 }
490
491
492 /**************************************************************************
493 * SetClipboardData (USER32.@)
494 */
495 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
496 {
497 CLIPBOARDINFO cbinfo;
498 HANDLE hResult = 0;
499
500 TRACE("(%04X, %p) !\n", wFormat, hData);
501
502 /* If it's not owned, data can only be set if the format isn't
503 available and its rendering is not delayed */
504 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
505 (!(cbinfo.flags & CB_OWNER) && !hData))
506 {
507 WARN("Clipboard not owned by calling task. Operation failed.\n");
508 return 0;
509 }
510
511 if (USER_Driver->pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
512 {
513 hResult = hData;
514 bCBHasChanged = TRUE;
515 }
516
517 return hResult;
518 }
519
520
521 /**************************************************************************
522 * CountClipboardFormats (USER32.@)
523 */
524 INT WINAPI CountClipboardFormats(void)
525 {
526 INT count = USER_Driver->pCountClipboardFormats();
527 TRACE("returning %d\n", count);
528 return count;
529 }
530
531
532 /**************************************************************************
533 * EnumClipboardFormats (USER32.@)
534 */
535 UINT WINAPI EnumClipboardFormats(UINT wFormat)
536 {
537 CLIPBOARDINFO cbinfo;
538
539 TRACE("(%04X)\n", wFormat);
540
541 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
542 (~cbinfo.flags & CB_OPEN))
543 {
544 WARN("Clipboard not opened by calling task.\n");
545 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
546 return 0;
547 }
548 return USER_Driver->pEnumClipboardFormats(wFormat);
549 }
550
551
552 /**************************************************************************
553 * IsClipboardFormatAvailable (USER32.@)
554 */
555 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
556 {
557 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
558 TRACE("%04x, returning %d\n", wFormat, bret);
559 return bret;
560 }
561
562
563 /**************************************************************************
564 * GetClipboardData (USER.142)
565 */
566 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
567 {
568 HANDLE16 hData = 0;
569 CLIPBOARDINFO cbinfo;
570
571 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
572 (~cbinfo.flags & CB_OPEN))
573 {
574 WARN("Clipboard not opened by calling task.\n");
575 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
576 return 0;
577 }
578
579 if (!USER_Driver->pGetClipboardData(wFormat, &hData, NULL)) hData = 0;
580
581 return hData;
582 }
583
584
585 /**************************************************************************
586 * GetClipboardData (USER32.@)
587 */
588 HANDLE WINAPI GetClipboardData(UINT wFormat)
589 {
590 HANDLE hData = 0;
591 CLIPBOARDINFO cbinfo;
592
593 TRACE("%04x\n", wFormat);
594
595 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
596 (~cbinfo.flags & CB_OPEN))
597 {
598 WARN("Clipboard not opened by calling task.\n");
599 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
600 return 0;
601 }
602
603 if (!USER_Driver->pGetClipboardData(wFormat, NULL, &hData)) hData = 0;
604
605 TRACE("returning %p\n", hData);
606 return hData;
607 }
608
609
610 /**************************************************************************
611 * GetPriorityClipboardFormat (USER32.@)
612 */
613 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
614 {
615 int i;
616
617 TRACE("()\n");
618
619 if(CountClipboardFormats() == 0)
620 return 0;
621
622 for (i = 0; i < nCount; i++)
623 if (IsClipboardFormatAvailable(list[i]))
624 return list[i];
625
626 return -1;
627 }
628
629
630 /**************************************************************************
631 * GetClipboardSequenceNumber (USER32.@)
632 * Supported on Win2k/Win98
633 * MSDN: Windows clipboard code keeps a serial number for the clipboard
634 * for each window station. The number is incremented whenever the
635 * contents change or are emptied.
636 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
637 */
638 DWORD WINAPI GetClipboardSequenceNumber(VOID)
639 {
640 DWORD seqno = 0;
641
642 SERVER_START_REQ( set_clipboard_info )
643 {
644 req->flags = 0;
645 if (!wine_server_call_err( req )) seqno = reply->seqno;
646 }
647 SERVER_END_REQ;
648
649 TRACE("returning %x\n", seqno);
650 return seqno;
651 }
652
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.