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