]>
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 KB |
57 | #include "wx/msw/private.h" |
58 | #include "wx/msw/dib.h" | |
59 | ||
d59ceba5 VZ |
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 | |
26f86486 | 65 | #include "wx/dataobj.h" |
3f480da3 | 66 | |
d59ceba5 VZ |
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 | |
2bda0e17 KB |
72 | #endif |
73 | ||
d59ceba5 VZ |
74 | #if wxUSE_OLE_CLIPBOARD |
75 | #include <ole2.h> | |
76 | #endif // wxUSE_OLE_CLIPBOARD | |
77 | ||
3f480da3 VZ |
78 | #ifdef __WIN16__ |
79 | #define memcpy hmemcpy | |
80 | #endif | |
06e43511 | 81 | |
26f86486 VZ |
82 | // =========================================================================== |
83 | // implementation | |
84 | // =========================================================================== | |
85 | ||
86 | // --------------------------------------------------------------------------- | |
87 | // old-style clipboard functions using Windows API | |
88 | // --------------------------------------------------------------------------- | |
2bda0e17 | 89 | |
26f86486 VZ |
90 | static bool gs_wxClipboardIsOpen = FALSE; |
91 | ||
92 | bool wxOpenClipboard() | |
2bda0e17 | 93 | { |
223d09f6 | 94 | wxCHECK_MSG( !gs_wxClipboardIsOpen, TRUE, wxT("clipboard already opened.") ); |
26f86486 VZ |
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 | { | |
223d09f6 | 108 | wxLogDebug(wxT("Can not open clipboard without a main window.")); |
26f86486 VZ |
109 | |
110 | return FALSE; | |
111 | } | |
2bda0e17 KB |
112 | } |
113 | ||
26f86486 | 114 | bool wxCloseClipboard() |
2bda0e17 | 115 | { |
223d09f6 | 116 | wxCHECK_MSG( gs_wxClipboardIsOpen, FALSE, wxT("clipboard is not opened") ); |
26f86486 VZ |
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; | |
2bda0e17 KB |
128 | } |
129 | ||
26f86486 | 130 | bool wxEmptyClipboard() |
2bda0e17 | 131 | { |
26f86486 VZ |
132 | if ( ::EmptyClipboard() == 0 ) |
133 | { | |
134 | wxLogSysError(_("Failed to empty the clipboard.")); | |
135 | ||
136 | return FALSE; | |
137 | } | |
138 | ||
139 | return TRUE; | |
2bda0e17 KB |
140 | } |
141 | ||
26f86486 | 142 | bool wxIsClipboardOpened() |
2bda0e17 | 143 | { |
26f86486 | 144 | return gs_wxClipboardIsOpen; |
2bda0e17 KB |
145 | } |
146 | ||
06e43511 | 147 | bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat) |
2bda0e17 | 148 | { |
d59ceba5 VZ |
149 | // for bitmaps, DIBs will also do |
150 | return (::IsClipboardFormatAvailable(dataFormat) != 0) || | |
151 | (dataFormat.GetFormatId() == CF_BITMAP && | |
152 | ::IsClipboardFormatAvailable(CF_DIB)); | |
2bda0e17 KB |
153 | } |
154 | ||
26f86486 VZ |
155 | bool wxSetClipboardData(wxDataFormat dataFormat, |
156 | const void *data, | |
157 | int width, int height) | |
2bda0e17 | 158 | { |
26f86486 VZ |
159 | HANDLE handle = 0; // return value of SetClipboardData |
160 | ||
161 | switch (dataFormat) | |
2bda0e17 | 162 | { |
26f86486 VZ |
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 | { | |
47d67540 | 201 | #if wxUSE_IMAGE_LOADING_IN_MSW |
26f86486 VZ |
202 | wxBitmap *bitmap = (wxBitmap *)data; |
203 | HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP(); | |
204 | // NULL palette means to use the system one | |
ef3ab009 | 205 | HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL); |
26f86486 | 206 | handle = SetClipboardData(CF_DIB, hDIB); |
d59ceba5 | 207 | #endif // wxUSE_IMAGE_LOADING_IN_MSW |
26f86486 VZ |
208 | break; |
209 | } | |
210 | ||
47d67540 | 211 | #if wxUSE_METAFILE |
26f86486 VZ |
212 | case wxDF_METAFILE: |
213 | { | |
214 | wxMetafile *wxMF = (wxMetafile *)data; | |
215 | HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1); | |
26f86486 | 216 | METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data); |
2bda0e17 | 217 | |
26f86486 VZ |
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); | |
2bda0e17 | 224 | |
26f86486 VZ |
225 | handle = SetClipboardData(CF_METAFILEPICT, data); |
226 | break; | |
227 | } | |
2bda0e17 | 228 | #endif |
26f86486 VZ |
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 | } | |
2bda0e17 | 238 | |
26f86486 VZ |
239 | case wxDF_OEMTEXT: |
240 | dataFormat = wxDF_TEXT; | |
241 | // fall through | |
2bda0e17 | 242 | |
26f86486 VZ |
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 | { | |
26f86486 | 253 | LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory); |
2bda0e17 | 254 | |
26f86486 | 255 | memcpy(lpGlobalMemory, s, l); |
2bda0e17 | 256 | |
26f86486 VZ |
257 | GlobalUnlock(hGlobalMemory); |
258 | } | |
2bda0e17 | 259 | |
26f86486 VZ |
260 | handle = SetClipboardData(dataFormat, hGlobalMemory); |
261 | break; | |
262 | } | |
2bda0e17 | 263 | } |
26f86486 VZ |
264 | |
265 | if ( handle == 0 ) | |
2bda0e17 | 266 | { |
26f86486 VZ |
267 | wxLogSysError(_("Failed to set clipboard data.")); |
268 | ||
269 | return FALSE; | |
2bda0e17 | 270 | } |
26f86486 VZ |
271 | |
272 | return TRUE; | |
273 | } | |
274 | ||
275 | void *wxGetClipboardData(wxDataFormat dataFormat, long *len) | |
276 | { | |
277 | void *retval = NULL; | |
278 | ||
279 | switch ( dataFormat ) | |
2bda0e17 | 280 | { |
26f86486 VZ |
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); | |
6d167489 | 322 | #if WXWIN_COMPATIBILITY_2 |
26f86486 | 323 | wxBM->SetOk(TRUE); |
6d167489 | 324 | #endif // WXWIN_COMPATIBILITY_2 |
26f86486 VZ |
325 | retval = wxBM; |
326 | break; | |
327 | } | |
328 | ||
329 | case wxDF_METAFILE: | |
330 | case CF_SYLK: | |
331 | case CF_DIF: | |
332 | case CF_TIFF: | |
333 | case CF_PALETTE: | |
334 | case wxDF_DIB: | |
26f86486 VZ |
335 | { |
336 | wxLogError(_("Unsupported clipboard format.")); | |
337 | return FALSE; | |
338 | } | |
339 | ||
340 | case wxDF_OEMTEXT: | |
341 | dataFormat = wxDF_TEXT; | |
342 | // fall through | |
343 | ||
344 | case wxDF_TEXT: | |
345 | { | |
346 | HANDLE hGlobalMemory = ::GetClipboardData(dataFormat); | |
347 | if (!hGlobalMemory) | |
348 | break; | |
2bda0e17 | 349 | |
26f86486 VZ |
350 | DWORD hsize = ::GlobalSize(hGlobalMemory); |
351 | if (len) | |
352 | *len = hsize; | |
2bda0e17 | 353 | |
26f86486 VZ |
354 | char *s = new char[hsize]; |
355 | if (!s) | |
356 | break; | |
2bda0e17 | 357 | |
26f86486 | 358 | LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory); |
2bda0e17 | 359 | |
26f86486 | 360 | memcpy(s, lpGlobalMemory, hsize); |
2bda0e17 | 361 | |
26f86486 VZ |
362 | ::GlobalUnlock(hGlobalMemory); |
363 | ||
364 | retval = s; | |
365 | break; | |
366 | } | |
3f480da3 VZ |
367 | |
368 | default: | |
369 | { | |
370 | HANDLE hGlobalMemory = ::GetClipboardData(dataFormat); | |
371 | if ( !hGlobalMemory ) | |
372 | break; | |
373 | ||
374 | DWORD size = ::GlobalSize(hGlobalMemory); | |
375 | if ( len ) | |
376 | *len = size; | |
377 | ||
378 | void *buf = malloc(size); | |
379 | if ( !buf ) | |
380 | break; | |
381 | ||
382 | LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory); | |
383 | ||
384 | memcpy(buf, lpGlobalMemory, size); | |
385 | ||
386 | ::GlobalUnlock(hGlobalMemory); | |
387 | ||
388 | retval = buf; | |
389 | break; | |
390 | } | |
26f86486 | 391 | } |
2bda0e17 | 392 | |
26f86486 VZ |
393 | if ( !retval ) |
394 | { | |
395 | wxLogSysError(_("Failed to retrieve data from the clipboard.")); | |
2bda0e17 | 396 | } |
26f86486 VZ |
397 | |
398 | return retval; | |
2bda0e17 KB |
399 | } |
400 | ||
3f480da3 | 401 | wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat) |
2bda0e17 | 402 | { |
3f480da3 | 403 | return ::EnumClipboardFormats(dataFormat); |
2bda0e17 KB |
404 | } |
405 | ||
837e5743 | 406 | int wxRegisterClipboardFormat(wxChar *formatName) |
2bda0e17 KB |
407 | { |
408 | return ::RegisterClipboardFormat(formatName); | |
409 | } | |
410 | ||
26f86486 | 411 | bool wxGetClipboardFormatName(wxDataFormat dataFormat, |
837e5743 | 412 | wxChar *formatName, |
26f86486 | 413 | int maxCount) |
2bda0e17 | 414 | { |
26f86486 | 415 | return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0; |
2bda0e17 KB |
416 | } |
417 | ||
26f86486 | 418 | // --------------------------------------------------------------------------- |
06e43511 | 419 | // wxClipboard |
26f86486 | 420 | // --------------------------------------------------------------------------- |
2bda0e17 | 421 | |
26f86486 | 422 | IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject) |
2bda0e17 KB |
423 | |
424 | wxClipboard::wxClipboard() | |
425 | { | |
d59ceba5 | 426 | m_clearOnExit = FALSE; |
2bda0e17 KB |
427 | } |
428 | ||
429 | wxClipboard::~wxClipboard() | |
430 | { | |
d59ceba5 VZ |
431 | if ( m_clearOnExit ) |
432 | { | |
433 | Clear(); | |
434 | } | |
2bda0e17 KB |
435 | } |
436 | ||
06e43511 | 437 | void wxClipboard::Clear() |
2bda0e17 | 438 | { |
d59ceba5 VZ |
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 | { | |
7ffdaf81 | 449 | #if wxUSE_OLE_CLIPBOARD |
d59ceba5 VZ |
450 | if ( FAILED(OleFlushClipboard()) ) |
451 | { | |
452 | wxLogLastError("OleFlushClipboard"); | |
453 | ||
454 | return FALSE; | |
455 | } | |
456 | else | |
457 | { | |
458 | m_clearOnExit = FALSE; | |
459 | ||
460 | return TRUE; | |
461 | } | |
7ffdaf81 VZ |
462 | #else // !wxUSE_OLE_CLIPBOARD |
463 | return FALSE; | |
464 | #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD | |
2bda0e17 KB |
465 | } |
466 | ||
06e43511 | 467 | bool wxClipboard::Open() |
2bda0e17 | 468 | { |
d59ceba5 VZ |
469 | // OLE opens clipboard for us |
470 | #if wxUSE_OLE_CLIPBOARD | |
471 | return TRUE; | |
472 | #else | |
06e43511 | 473 | return wxOpenClipboard(); |
d59ceba5 | 474 | #endif |
2bda0e17 KB |
475 | } |
476 | ||
f536e0f2 VZ |
477 | bool wxClipboard::IsOpened() const |
478 | { | |
479 | #if wxUSE_OLE_CLIPBOARD | |
480 | return TRUE; | |
481 | #else | |
482 | return wxIsClipboardOpened(); | |
483 | #endif | |
484 | } | |
485 | ||
06e43511 | 486 | bool wxClipboard::SetData( wxDataObject *data ) |
2bda0e17 | 487 | { |
51edda6a | 488 | #if !wxUSE_OLE_CLIPBOARD |
26f86486 | 489 | (void)wxEmptyClipboard(); |
51edda6a | 490 | #endif // wxUSE_OLE_CLIPBOARD |
26f86486 VZ |
491 | |
492 | if ( data ) | |
493 | return AddData(data); | |
494 | else | |
495 | return TRUE; | |
496 | } | |
497 | ||
498 | bool wxClipboard::AddData( wxDataObject *data ) | |
499 | { | |
223d09f6 | 500 | wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); |
06e43511 | 501 | |
d59ceba5 VZ |
502 | #if wxUSE_OLE_CLIPBOARD |
503 | HRESULT hr = OleSetClipboard(data->GetInterface()); | |
504 | if ( FAILED(hr) ) | |
505 | { | |
506 | wxLogSysError(hr, _("Failed to put data on the clipboard")); | |
507 | ||
508 | // don't free anything in this case | |
509 | ||
510 | return FALSE; | |
511 | } | |
512 | ||
513 | // we have a problem here because we should delete wxDataObject, but we | |
514 | // can't do it because IDataObject which we just gave to the clipboard | |
515 | // would try to use it when it will need the data. IDataObject is ref | |
516 | // counted and so doesn't suffer from such problem, so we release it now | |
517 | // and tell it to delete wxDataObject when it is deleted itself. | |
518 | data->SetAutoDelete(); | |
519 | ||
520 | // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when | |
521 | // using OLE clipboard when the app terminates - by default, we call | |
522 | // OleSetClipboard(NULL) which won't waste RAM, but the app can call | |
523 | // wxClipboard::Flush() to chaneg this | |
524 | m_clearOnExit = TRUE; | |
525 | ||
526 | return TRUE; | |
527 | #elif wxUSE_DATAOBJ | |
223d09f6 | 528 | wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") ); |
26f86486 VZ |
529 | |
530 | wxDataFormat format = data->GetFormat(); | |
531 | ||
532 | switch ( format ) | |
06e43511 JS |
533 | { |
534 | case wxDF_TEXT: | |
535 | case wxDF_OEMTEXT: | |
536 | { | |
537 | wxTextDataObject* textDataObject = (wxTextDataObject*) data; | |
538 | wxString str(textDataObject->GetText()); | |
26f86486 | 539 | return wxSetClipboardData(format, str.c_str()); |
06e43511 | 540 | } |
26f86486 | 541 | |
06e43511 JS |
542 | case wxDF_BITMAP: |
543 | case wxDF_DIB: | |
544 | { | |
545 | wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data; | |
546 | wxBitmap bitmap(bitmapDataObject->GetBitmap()); | |
26f86486 | 547 | return wxSetClipboardData(data->GetFormat(), &bitmap); |
06e43511 | 548 | } |
26f86486 | 549 | |
06e43511 JS |
550 | #if wxUSE_METAFILE |
551 | case wxDF_METAFILE: | |
552 | { | |
26f86486 VZ |
553 | wxMetafileDataObject* metaFileDataObject = |
554 | (wxMetafileDataObject*) data; | |
06e43511 | 555 | wxMetafile metaFile = metaFileDataObject->GetMetafile(); |
26f86486 VZ |
556 | return wxSetClipboardData(wxDF_METAFILE, &metaFile, |
557 | metaFileDataObject->GetWidth(), | |
558 | metaFileDataObject->GetHeight()); | |
06e43511 | 559 | } |
26f86486 VZ |
560 | #endif // wxUSE_METAFILE |
561 | ||
06e43511 | 562 | default: |
3f480da3 | 563 | return wxSetClipboardData(data); |
06e43511 | 564 | } |
d59ceba5 | 565 | #else // !wxUSE_DATAOBJ |
06e43511 | 566 | return FALSE; |
d59ceba5 | 567 | #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ |
2bda0e17 KB |
568 | } |
569 | ||
06e43511 | 570 | void wxClipboard::Close() |
2bda0e17 | 571 | { |
d59ceba5 VZ |
572 | // OLE closes clipboard for us |
573 | #if !wxUSE_OLE_CLIPBOARD | |
06e43511 | 574 | wxCloseClipboard(); |
d59ceba5 | 575 | #endif |
2bda0e17 KB |
576 | } |
577 | ||
26f86486 | 578 | bool wxClipboard::IsSupported( wxDataFormat format ) |
2bda0e17 | 579 | { |
06e43511 | 580 | return wxIsClipboardFormatAvailable(format); |
2bda0e17 KB |
581 | } |
582 | ||
1e8335b0 | 583 | bool wxClipboard::GetData( wxDataObject& data ) |
2bda0e17 | 584 | { |
d59ceba5 VZ |
585 | #if wxUSE_OLE_CLIPBOARD |
586 | IDataObject *pDataObject = NULL; | |
587 | HRESULT hr = OleGetClipboard(&pDataObject); | |
588 | if ( FAILED(hr) || !pDataObject ) | |
589 | { | |
590 | wxLogSysError(hr, _("Failed to get data from the clipboard")); | |
591 | ||
592 | return FALSE; | |
593 | } | |
594 | ||
595 | // build the list of supported formats | |
1e8335b0 | 596 | size_t nFormats = data.GetFormatCount(wxDataObject::Set); |
d59ceba5 VZ |
597 | wxDataFormat format, *formats; |
598 | if ( nFormats == 1 ) | |
599 | { | |
600 | // the most common case | |
601 | formats = &format; | |
602 | } | |
603 | else | |
604 | { | |
605 | // bad luck, need to alloc mem | |
606 | formats = new wxDataFormat[nFormats]; | |
607 | } | |
608 | ||
1e8335b0 | 609 | data.GetAllFormats(formats, wxDataObject::Set); |
d59ceba5 VZ |
610 | |
611 | // get the format enumerator | |
612 | bool result = FALSE; | |
613 | wxArrayInt supportedFormats; | |
614 | IEnumFORMATETC *pEnumFormatEtc = NULL; | |
615 | hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc); | |
616 | if ( FAILED(hr) || !pEnumFormatEtc ) | |
617 | { | |
618 | wxLogSysError(hr, | |
619 | _("Failed to retrieve the supported clipboard formats")); | |
620 | } | |
621 | else | |
622 | { | |
623 | // ask for the supported formats and see if there are any we support | |
624 | FORMATETC formatEtc; | |
625 | for ( ;; ) | |
626 | { | |
627 | ULONG nCount; | |
628 | hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount); | |
629 | ||
630 | // don't use FAILED() because S_FALSE would pass it | |
631 | if ( hr != S_OK ) | |
632 | { | |
633 | // no more formats | |
634 | break; | |
635 | } | |
636 | ||
637 | CLIPFORMAT cf = formatEtc.cfFormat; | |
638 | ||
639 | #ifdef __WXDEBUG__ | |
640 | wxLogTrace(wxTRACE_OleCalls, | |
641 | wxT("Object on the clipboard supports format %s."), | |
642 | wxDataObject::GetFormatName(cf)); | |
643 | #endif // Debug | |
644 | ||
645 | // is supported? | |
646 | for ( size_t n = 0; n < nFormats; n++ ) | |
647 | { | |
648 | if ( formats[n].GetFormatId() == cf ) | |
649 | { | |
650 | if ( supportedFormats.Index(cf) == wxNOT_FOUND ) | |
651 | { | |
652 | supportedFormats.Add(cf); | |
653 | } | |
654 | } | |
655 | } | |
656 | } | |
657 | ||
658 | pEnumFormatEtc->Release(); | |
659 | } | |
660 | ||
661 | if ( formats != &format ) | |
662 | { | |
663 | delete [] formats; | |
664 | } | |
665 | //else: we didn't allocate any memory | |
666 | ||
667 | if ( !supportedFormats.IsEmpty() ) | |
668 | { | |
669 | FORMATETC formatEtc; | |
670 | formatEtc.ptd = NULL; | |
671 | formatEtc.dwAspect = DVASPECT_CONTENT; | |
672 | formatEtc.lindex = -1; | |
673 | formatEtc.tymed = TYMED_HGLOBAL; | |
674 | ||
675 | size_t nSupportedFormats = supportedFormats.GetCount(); | |
676 | for ( size_t n = 0; !result && (n < nSupportedFormats); n++ ) | |
677 | { | |
678 | STGMEDIUM medium; | |
679 | formatEtc.cfFormat = supportedFormats[n]; | |
680 | ||
681 | // try to get data | |
682 | hr = pDataObject->GetData(&formatEtc, &medium); | |
683 | if ( FAILED(hr) ) | |
684 | { | |
685 | // try other tymed for GDI objects | |
686 | if ( formatEtc.cfFormat == CF_BITMAP ) | |
687 | { | |
688 | formatEtc.tymed = TYMED_HGLOBAL; | |
689 | hr = pDataObject->GetData(&formatEtc, &medium); | |
690 | } | |
691 | } | |
692 | ||
693 | if ( SUCCEEDED(hr) ) | |
694 | { | |
695 | // pass the data to the data object | |
1e8335b0 | 696 | hr = data.GetInterface()->SetData(&formatEtc, &medium, TRUE); |
d59ceba5 VZ |
697 | if ( FAILED(hr) ) |
698 | { | |
699 | wxLogDebug(wxT("Failed to set data in wxIDataObject")); | |
700 | ||
701 | // IDataObject only takes the ownership of data if it | |
702 | // successfully got it - which is not the case here | |
703 | ReleaseStgMedium(&medium); | |
704 | } | |
705 | else | |
706 | { | |
707 | result = TRUE; | |
708 | } | |
709 | } | |
710 | //else: unsupported tymed? | |
711 | } | |
712 | } | |
713 | //else: unsupported format | |
714 | ||
715 | // clean up and return | |
716 | pDataObject->Release(); | |
717 | ||
718 | return result; | |
719 | #elif wxUSE_DATAOBJ | |
223d09f6 | 720 | wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") ); |
26f86486 | 721 | |
1e8335b0 | 722 | wxDataFormat format = data.GetFormat(); |
26f86486 | 723 | switch ( format ) |
06e43511 JS |
724 | { |
725 | case wxDF_TEXT: | |
726 | case wxDF_OEMTEXT: | |
727 | { | |
1e8335b0 VZ |
728 | wxTextDataObject& textDataObject = (wxTextDataObject &)data; |
729 | char* s = (char*)wxGetClipboardData(format); | |
730 | if ( !s ) | |
06e43511 | 731 | return FALSE; |
1e8335b0 VZ |
732 | |
733 | textDataObject.SetText(s); | |
734 | delete [] s; | |
735 | ||
736 | return TRUE; | |
06e43511 | 737 | } |
26f86486 | 738 | |
06e43511 JS |
739 | case wxDF_BITMAP: |
740 | case wxDF_DIB: | |
741 | { | |
1e8335b0 | 742 | wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data; |
26f86486 | 743 | wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetFormat()); |
1e8335b0 | 744 | if ( !bitmap ) |
06e43511 | 745 | return FALSE; |
1e8335b0 VZ |
746 | |
747 | bitmapDataObject.SetBitmap(*bitmap); | |
748 | delete bitmap; | |
749 | ||
750 | return TRUE; | |
06e43511 JS |
751 | } |
752 | #if wxUSE_METAFILE | |
753 | case wxDF_METAFILE: | |
754 | { | |
1e8335b0 | 755 | wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data; |
26f86486 | 756 | wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE); |
1e8335b0 | 757 | if ( !metaFile ) |
06e43511 | 758 | return FALSE; |
3f480da3 | 759 | |
1e8335b0 VZ |
760 | metaFileDataObject.SetMetafile(*metaFile); |
761 | delete metaFile; | |
26f86486 | 762 | |
1e8335b0 VZ |
763 | return TRUE; |
764 | } | |
765 | #endif // wxUSE_METAFILE | |
06e43511 | 766 | } |
d59ceba5 | 767 | #else // !wxUSE_DATAOBJ |
1e8335b0 VZ |
768 | wxFAIL_MSG( wxT("no clipboard implementation") ); |
769 | #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ | |
770 | ||
06e43511 | 771 | return FALSE; |
2bda0e17 KB |
772 | } |
773 | ||
26f86486 VZ |
774 | #else |
775 | #error "Please turn wxUSE_CLIPBOARD on to compile this file." | |
47d67540 | 776 | #endif // wxUSE_CLIPBOARD |
4ce81a75 | 777 |