1. more drag and drop and clipboard changes:
[wxWidgets.git] / src / msw / clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: clipbrd.cpp
3 // Purpose: Clipboard functionality
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "clipbrd.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/setup.h"
33 #endif
34
35 #if wxUSE_CLIPBOARD
36
37 #ifndef WX_PRECOMP
38 #include "wx/object.h"
39 #include "wx/event.h"
40 #include "wx/app.h"
41 #include "wx/frame.h"
42 #include "wx/bitmap.h"
43 #include "wx/utils.h"
44 #include "wx/intl.h"
45 #endif
46
47 #if wxUSE_METAFILE
48 #include "wx/metafile.h"
49 #endif
50
51 #include "wx/log.h"
52 #include "wx/clipbrd.h"
53
54 #include <string.h>
55 #include <windows.h>
56
57 #include "wx/msw/private.h"
58 #include "wx/msw/dib.h"
59
60 // wxDataObject is tied to OLE/drag and drop implementation, therefore so are
61 // the functions using wxDataObject in wxClipboard
62 #define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
63
64 #if wxUSE_DATAOBJ
65 #include "wx/dataobj.h"
66
67 // use OLE clipboard
68 #define wxUSE_OLE_CLIPBOARD 1
69 #else // !wxUSE_DATAOBJ
70 // use Win clipboard API
71 #define wxUSE_OLE_CLIPBOARD 0
72 #endif
73
74 #if wxUSE_OLE_CLIPBOARD
75 #include <ole2.h>
76 #endif // wxUSE_OLE_CLIPBOARD
77
78 #ifdef __WIN16__
79 #define memcpy hmemcpy
80 #endif
81
82 // ===========================================================================
83 // implementation
84 // ===========================================================================
85
86 // ---------------------------------------------------------------------------
87 // old-style clipboard functions using Windows API
88 // ---------------------------------------------------------------------------
89
90 static bool gs_wxClipboardIsOpen = FALSE;
91
92 bool wxOpenClipboard()
93 {
94 wxCHECK_MSG( !gs_wxClipboardIsOpen, TRUE, wxT("clipboard already opened.") );
95
96 wxWindow *win = wxTheApp->GetTopWindow();
97 if ( win )
98 {
99 gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0;
100
101 if ( !gs_wxClipboardIsOpen )
102 wxLogSysError(_("Failed to open the clipboard."));
103
104 return gs_wxClipboardIsOpen;
105 }
106 else
107 {
108 wxLogDebug(wxT("Can not open clipboard without a main window."));
109
110 return FALSE;
111 }
112 }
113
114 bool wxCloseClipboard()
115 {
116 wxCHECK_MSG( gs_wxClipboardIsOpen, FALSE, wxT("clipboard is not opened") );
117
118 gs_wxClipboardIsOpen = FALSE;
119
120 if ( ::CloseClipboard() == 0 )
121 {
122 wxLogSysError(_("Failed to close the clipboard."));
123
124 return FALSE;
125 }
126
127 return TRUE;
128 }
129
130 bool wxEmptyClipboard()
131 {
132 if ( ::EmptyClipboard() == 0 )
133 {
134 wxLogSysError(_("Failed to empty the clipboard."));
135
136 return FALSE;
137 }
138
139 return TRUE;
140 }
141
142 bool wxIsClipboardOpened()
143 {
144 return gs_wxClipboardIsOpen;
145 }
146
147 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat)
148 {
149 // for bitmaps, DIBs will also do
150 return (::IsClipboardFormatAvailable(dataFormat) != 0) ||
151 (dataFormat.GetFormatId() == CF_BITMAP &&
152 ::IsClipboardFormatAvailable(CF_DIB));
153 }
154
155 bool wxSetClipboardData(wxDataFormat dataFormat,
156 const void *data,
157 int width, int height)
158 {
159 HANDLE handle = 0; // return value of SetClipboardData
160
161 switch (dataFormat)
162 {
163 case wxDF_BITMAP:
164 {
165 wxBitmap *bitmap = (wxBitmap *)data;
166
167 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
168 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
169 HBITMAP old = (HBITMAP)
170 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
171 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
172 bitmap->GetWidth(),
173 bitmap->GetHeight());
174 if (!hBitmap)
175 {
176 SelectObject(hdcSrc, old);
177 DeleteDC(hdcMem);
178 DeleteDC(hdcSrc);
179 return FALSE;
180 }
181
182 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
183 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
184 hdcSrc, 0, 0, SRCCOPY);
185
186 // Select new bitmap out of memory DC
187 SelectObject(hdcMem, old1);
188
189 // Set the data
190 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
191
192 // Clean up
193 SelectObject(hdcSrc, old);
194 DeleteDC(hdcSrc);
195 DeleteDC(hdcMem);
196 break;
197 }
198
199 case wxDF_DIB:
200 {
201 #if wxUSE_IMAGE_LOADING_IN_MSW
202 wxBitmap *bitmap = (wxBitmap *)data;
203 HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP();
204 // NULL palette means to use the system one
205 HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL);
206 handle = SetClipboardData(CF_DIB, hDIB);
207 #endif // wxUSE_IMAGE_LOADING_IN_MSW
208 break;
209 }
210
211 #if wxUSE_METAFILE
212 case wxDF_METAFILE:
213 {
214 wxMetafile *wxMF = (wxMetafile *)data;
215 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
216 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
217
218 mf->mm = wxMF->GetWindowsMappingMode();
219 mf->xExt = width;
220 mf->yExt = height;
221 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
222 GlobalUnlock(data);
223 wxMF->SetHMETAFILE((WXHANDLE) NULL);
224
225 handle = SetClipboardData(CF_METAFILEPICT, data);
226 break;
227 }
228 #endif
229 case CF_SYLK:
230 case CF_DIF:
231 case CF_TIFF:
232 case CF_PALETTE:
233 default:
234 {
235 wxLogError(_("Unsupported clipboard format."));
236 return FALSE;
237 }
238
239 case wxDF_OEMTEXT:
240 dataFormat = wxDF_TEXT;
241 // fall through
242
243 case wxDF_TEXT:
244 {
245 char *s = (char *)data;
246
247 width = strlen(s) + 1;
248 height = 1;
249 DWORD l = (width * height);
250 HANDLE hGlobalMemory = GlobalAlloc(GHND, l);
251 if ( hGlobalMemory )
252 {
253 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
254
255 memcpy(lpGlobalMemory, s, l);
256
257 GlobalUnlock(hGlobalMemory);
258 }
259
260 handle = SetClipboardData(dataFormat, hGlobalMemory);
261 break;
262 }
263 }
264
265 if ( handle == 0 )
266 {
267 wxLogSysError(_("Failed to set clipboard data."));
268
269 return FALSE;
270 }
271
272 return TRUE;
273 }
274
275 void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
276 {
277 void *retval = NULL;
278
279 switch ( dataFormat )
280 {
281 case wxDF_BITMAP:
282 {
283 BITMAP bm;
284 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
285 if (!hBitmap)
286 break;
287
288 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
289 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
290
291 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
292 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
293
294 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
295
296 if (!hNewBitmap)
297 {
298 SelectObject(hdcSrc, old);
299 DeleteDC(hdcMem);
300 DeleteDC(hdcSrc);
301 break;
302 }
303
304 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
305 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
306 hdcSrc, 0, 0, SRCCOPY);
307
308 // Select new bitmap out of memory DC
309 SelectObject(hdcMem, old1);
310
311 // Clean up
312 SelectObject(hdcSrc, old);
313 DeleteDC(hdcSrc);
314 DeleteDC(hdcMem);
315
316 // Create and return a new wxBitmap
317 wxBitmap *wxBM = new wxBitmap;
318 wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
319 wxBM->SetWidth(bm.bmWidth);
320 wxBM->SetHeight(bm.bmHeight);
321 wxBM->SetDepth(bm.bmPlanes);
322 wxBM->SetOk(TRUE);
323 retval = wxBM;
324 break;
325 }
326
327 case wxDF_METAFILE:
328 case CF_SYLK:
329 case CF_DIF:
330 case CF_TIFF:
331 case CF_PALETTE:
332 case wxDF_DIB:
333 {
334 wxLogError(_("Unsupported clipboard format."));
335 return FALSE;
336 }
337
338 case wxDF_OEMTEXT:
339 dataFormat = wxDF_TEXT;
340 // fall through
341
342 case wxDF_TEXT:
343 {
344 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
345 if (!hGlobalMemory)
346 break;
347
348 DWORD hsize = ::GlobalSize(hGlobalMemory);
349 if (len)
350 *len = hsize;
351
352 char *s = new char[hsize];
353 if (!s)
354 break;
355
356 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
357
358 memcpy(s, lpGlobalMemory, hsize);
359
360 ::GlobalUnlock(hGlobalMemory);
361
362 retval = s;
363 break;
364 }
365
366 default:
367 {
368 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
369 if ( !hGlobalMemory )
370 break;
371
372 DWORD size = ::GlobalSize(hGlobalMemory);
373 if ( len )
374 *len = size;
375
376 void *buf = malloc(size);
377 if ( !buf )
378 break;
379
380 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
381
382 memcpy(buf, lpGlobalMemory, size);
383
384 ::GlobalUnlock(hGlobalMemory);
385
386 retval = buf;
387 break;
388 }
389 }
390
391 if ( !retval )
392 {
393 wxLogSysError(_("Failed to retrieve data from the clipboard."));
394 }
395
396 return retval;
397 }
398
399 wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
400 {
401 return ::EnumClipboardFormats(dataFormat);
402 }
403
404 int wxRegisterClipboardFormat(wxChar *formatName)
405 {
406 return ::RegisterClipboardFormat(formatName);
407 }
408
409 bool wxGetClipboardFormatName(wxDataFormat dataFormat,
410 wxChar *formatName,
411 int maxCount)
412 {
413 return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
414 }
415
416 // ---------------------------------------------------------------------------
417 // wxClipboard
418 // ---------------------------------------------------------------------------
419
420 IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
421
422 wxClipboard* wxTheClipboard = (wxClipboard *)NULL;
423
424 wxClipboard::wxClipboard()
425 {
426 m_clearOnExit = FALSE;
427 }
428
429 wxClipboard::~wxClipboard()
430 {
431 if ( m_clearOnExit )
432 {
433 Clear();
434 }
435 }
436
437 void wxClipboard::Clear()
438 {
439 #if wxUSE_OLE_CLIPBOARD
440 if ( FAILED(OleSetClipboard(NULL)) )
441 {
442 wxLogLastError("OleSetClipboard(NULL)");
443 }
444 #endif
445 }
446
447 bool wxClipboard::Flush()
448 {
449 if ( FAILED(OleFlushClipboard()) )
450 {
451 wxLogLastError("OleFlushClipboard");
452
453 return FALSE;
454 }
455 else
456 {
457 m_clearOnExit = FALSE;
458
459 return TRUE;
460 }
461 }
462
463 bool wxClipboard::Open()
464 {
465 // OLE opens clipboard for us
466 #if wxUSE_OLE_CLIPBOARD
467 return TRUE;
468 #else
469 return wxOpenClipboard();
470 #endif
471 }
472
473 bool wxClipboard::SetData( wxDataObject *data )
474 {
475 (void)wxEmptyClipboard();
476
477 if ( data )
478 return AddData(data);
479 else
480 return TRUE;
481 }
482
483 bool wxClipboard::AddData( wxDataObject *data )
484 {
485 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
486
487 #if wxUSE_OLE_CLIPBOARD
488 HRESULT hr = OleSetClipboard(data->GetInterface());
489 if ( FAILED(hr) )
490 {
491 wxLogSysError(hr, _("Failed to put data on the clipboard"));
492
493 // don't free anything in this case
494
495 return FALSE;
496 }
497
498 // we have a problem here because we should delete wxDataObject, but we
499 // can't do it because IDataObject which we just gave to the clipboard
500 // would try to use it when it will need the data. IDataObject is ref
501 // counted and so doesn't suffer from such problem, so we release it now
502 // and tell it to delete wxDataObject when it is deleted itself.
503 data->SetAutoDelete();
504
505 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
506 // using OLE clipboard when the app terminates - by default, we call
507 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
508 // wxClipboard::Flush() to chaneg this
509 m_clearOnExit = TRUE;
510
511 return TRUE;
512 #elif wxUSE_DATAOBJ
513 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
514
515 wxDataFormat format = data->GetFormat();
516
517 switch ( format )
518 {
519 case wxDF_TEXT:
520 case wxDF_OEMTEXT:
521 {
522 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
523 wxString str(textDataObject->GetText());
524 return wxSetClipboardData(format, str.c_str());
525 }
526
527 case wxDF_BITMAP:
528 case wxDF_DIB:
529 {
530 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
531 wxBitmap bitmap(bitmapDataObject->GetBitmap());
532 return wxSetClipboardData(data->GetFormat(), &bitmap);
533 }
534
535 #if wxUSE_METAFILE
536 case wxDF_METAFILE:
537 {
538 wxMetafileDataObject* metaFileDataObject =
539 (wxMetafileDataObject*) data;
540 wxMetafile metaFile = metaFileDataObject->GetMetafile();
541 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
542 metaFileDataObject->GetWidth(),
543 metaFileDataObject->GetHeight());
544 }
545 #endif // wxUSE_METAFILE
546
547 default:
548 return wxSetClipboardData(data);
549 }
550 #else // !wxUSE_DATAOBJ
551 return FALSE;
552 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
553 }
554
555 void wxClipboard::Close()
556 {
557 // OLE closes clipboard for us
558 #if !wxUSE_OLE_CLIPBOARD
559 wxCloseClipboard();
560 #endif
561 }
562
563 bool wxClipboard::IsSupported( wxDataFormat format )
564 {
565 return wxIsClipboardFormatAvailable(format);
566 }
567
568 bool wxClipboard::GetData( wxDataObject *data )
569 {
570 wxCHECK_MSG( data, FALSE, wxT("invalid data object") );
571
572 #if wxUSE_OLE_CLIPBOARD
573 IDataObject *pDataObject = NULL;
574 HRESULT hr = OleGetClipboard(&pDataObject);
575 if ( FAILED(hr) || !pDataObject )
576 {
577 wxLogSysError(hr, _("Failed to get data from the clipboard"));
578
579 return FALSE;
580 }
581
582 // build the list of supported formats
583 size_t nFormats = data->GetFormatCount(FALSE /* for SetData() */);
584 wxDataFormat format, *formats;
585 if ( nFormats == 1 )
586 {
587 // the most common case
588 formats = &format;
589 }
590 else
591 {
592 // bad luck, need to alloc mem
593 formats = new wxDataFormat[nFormats];
594 }
595
596 data->GetAllFormats(formats, FALSE);
597
598 // get the format enumerator
599 bool result = FALSE;
600 wxArrayInt supportedFormats;
601 IEnumFORMATETC *pEnumFormatEtc = NULL;
602 hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc);
603 if ( FAILED(hr) || !pEnumFormatEtc )
604 {
605 wxLogSysError(hr,
606 _("Failed to retrieve the supported clipboard formats"));
607 }
608 else
609 {
610 // ask for the supported formats and see if there are any we support
611 FORMATETC formatEtc;
612 for ( ;; )
613 {
614 ULONG nCount;
615 hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount);
616
617 // don't use FAILED() because S_FALSE would pass it
618 if ( hr != S_OK )
619 {
620 // no more formats
621 break;
622 }
623
624 CLIPFORMAT cf = formatEtc.cfFormat;
625
626 #ifdef __WXDEBUG__
627 wxLogTrace(wxTRACE_OleCalls,
628 wxT("Object on the clipboard supports format %s."),
629 wxDataObject::GetFormatName(cf));
630 #endif // Debug
631
632 // is supported?
633 for ( size_t n = 0; n < nFormats; n++ )
634 {
635 if ( formats[n].GetFormatId() == cf )
636 {
637 if ( supportedFormats.Index(cf) == wxNOT_FOUND )
638 {
639 supportedFormats.Add(cf);
640 }
641 }
642 }
643 }
644
645 pEnumFormatEtc->Release();
646 }
647
648 if ( formats != &format )
649 {
650 delete [] formats;
651 }
652 //else: we didn't allocate any memory
653
654 if ( !supportedFormats.IsEmpty() )
655 {
656 FORMATETC formatEtc;
657 formatEtc.ptd = NULL;
658 formatEtc.dwAspect = DVASPECT_CONTENT;
659 formatEtc.lindex = -1;
660 formatEtc.tymed = TYMED_HGLOBAL;
661
662 size_t nSupportedFormats = supportedFormats.GetCount();
663 for ( size_t n = 0; !result && (n < nSupportedFormats); n++ )
664 {
665 STGMEDIUM medium;
666 formatEtc.cfFormat = supportedFormats[n];
667
668 // try to get data
669 hr = pDataObject->GetData(&formatEtc, &medium);
670 if ( FAILED(hr) )
671 {
672 // try other tymed for GDI objects
673 if ( formatEtc.cfFormat == CF_BITMAP )
674 {
675 formatEtc.tymed = TYMED_HGLOBAL;
676 hr = pDataObject->GetData(&formatEtc, &medium);
677 }
678 }
679
680 if ( SUCCEEDED(hr) )
681 {
682 // pass the data to the data object
683 hr = data->GetInterface()->SetData(&formatEtc, &medium, TRUE);
684 if ( FAILED(hr) )
685 {
686 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
687
688 // IDataObject only takes the ownership of data if it
689 // successfully got it - which is not the case here
690 ReleaseStgMedium(&medium);
691 }
692 else
693 {
694 result = TRUE;
695 }
696 }
697 //else: unsupported tymed?
698 }
699 }
700 //else: unsupported format
701
702 // clean up and return
703 pDataObject->Release();
704
705 return result;
706 #elif wxUSE_DATAOBJ
707 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
708
709 wxDataFormat format = data->GetFormat();
710 switch ( format )
711 {
712 case wxDF_TEXT:
713 case wxDF_OEMTEXT:
714 {
715 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
716 char* s = (char*) wxGetClipboardData(format);
717 if ( s )
718 {
719 textDataObject->SetText(s);
720 delete[] s;
721 return TRUE;
722 }
723 else
724 return FALSE;
725 }
726
727 case wxDF_BITMAP:
728 case wxDF_DIB:
729 {
730 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject *)data;
731 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetFormat());
732 if (bitmap)
733 {
734 bitmapDataObject->SetBitmap(* bitmap);
735 delete bitmap;
736 return TRUE;
737 }
738 else
739 return FALSE;
740 }
741 #if wxUSE_METAFILE
742 case wxDF_METAFILE:
743 {
744 wxMetafileDataObject* metaFileDataObject = (wxMetafileDataObject *)data;
745 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
746 if (metaFile)
747 {
748 metaFileDataObject->SetMetafile(*metaFile);
749 delete metaFile;
750 return TRUE;
751 }
752 else
753 return FALSE;
754 }
755 #endif
756 default:
757 {
758 long len;
759 void *buf = wxGetClipboardData(format, &len);
760 if ( buf )
761 {
762 // FIXME this is for testing only!!
763 ((wxPrivateDataObject *)data)->SetData(buf, len);
764 free(buf);
765
766 return TRUE;
767 }
768 }
769
770 return FALSE;
771 }
772 #else // !wxUSE_DATAOBJ
773 return FALSE;
774 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
775 }
776
777 //-----------------------------------------------------------------------------
778 // wxClipboardModule
779 //-----------------------------------------------------------------------------
780
781 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule,wxModule)
782
783 bool wxClipboardModule::OnInit()
784 {
785 wxTheClipboard = new wxClipboard();
786
787 return TRUE;
788 }
789
790 void wxClipboardModule::OnExit()
791 {
792 if (wxTheClipboard) delete wxTheClipboard;
793 wxTheClipboard = (wxClipboard*) NULL;
794 }
795
796 #else
797 #error "Please turn wxUSE_CLIPBOARD on to compile this file."
798 #endif // wxUSE_CLIPBOARD
799