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