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