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