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