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