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