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