1 /* 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "DragData.h" 28 29 #include "ClipboardWin.h" 30 #include "ClipboardUtilitiesWin.h" 31 #include "ClipboardAccessPolicy.h" 32 #include "DocumentFragment.h" 33 #include "PlatformString.h" 34 #include "Markup.h" 35 #include "TextEncoding.h" 36 #include <objidl.h> 37 #include <shlwapi.h> 38 #include <wininet.h> 39 40 namespace WebCore { 41 42 PassRefPtr<Clipboard> DragData::createClipboard(ClipboardAccessPolicy policy) const 43 { 44 return ClipboardWin::create(true, m_platformDragData, policy); 45 } 46 47 bool DragData::containsURL() const 48 { 49 return SUCCEEDED(m_platformDragData->QueryGetData(urlWFormat())) 50 || SUCCEEDED(m_platformDragData->QueryGetData(urlFormat())) 51 || SUCCEEDED(m_platformDragData->QueryGetData(filenameWFormat())) 52 || SUCCEEDED(m_platformDragData->QueryGetData(filenameFormat())); 53 } 54 55 String DragData::asURL(String* title) const 56 { 57 bool success; 58 return getURL(m_platformDragData, success, title); 59 } 60 61 bool DragData::containsFiles() const 62 { 63 return SUCCEEDED(m_platformDragData->QueryGetData(cfHDropFormat())); 64 } 65 66 void DragData::asFilenames(Vector<String>& result) const 67 { 68 WCHAR filename[MAX_PATH]; 69 70 STGMEDIUM medium; 71 if (FAILED(m_platformDragData->GetData(cfHDropFormat(), &medium))) 72 return; 73 74 HDROP hdrop = (HDROP)GlobalLock(medium.hGlobal); 75 76 if (!hdrop) 77 return; 78 79 const unsigned numFiles = DragQueryFileW(hdrop, 0xFFFFFFFF, 0, 0); 80 for (unsigned i = 0; i < numFiles; i++) { 81 if (!DragQueryFileW(hdrop, 0, filename, ARRAYSIZE(filename))) 82 continue; 83 result.append((UChar*)filename); 84 } 85 86 // Free up memory from drag 87 DragFinish(hdrop); 88 89 GlobalUnlock(medium.hGlobal); 90 } 91 92 bool DragData::containsPlainText() const 93 { 94 return SUCCEEDED(m_platformDragData->QueryGetData(plainTextWFormat())) 95 || SUCCEEDED(m_platformDragData->QueryGetData(plainTextFormat())); 96 } 97 98 String DragData::asPlainText() const 99 { 100 bool success; 101 return getPlainText(m_platformDragData, success); 102 } 103 104 bool DragData::containsColor() const 105 { 106 return false; 107 } 108 109 bool DragData::canSmartReplace() const 110 { 111 return SUCCEEDED(m_platformDragData->QueryGetData(smartPasteFormat())); 112 } 113 114 bool DragData::containsCompatibleContent() const 115 { 116 return containsPlainText() || containsURL() 117 || containsHTML(m_platformDragData) 118 || containsFilenames(m_platformDragData) 119 || containsColor(); 120 } 121 122 PassRefPtr<DocumentFragment> DragData::asFragment(Document* doc) const 123 { 124 /* 125 * Order is richest format first. On OSX this is: 126 * * Web Archive 127 * * Filenames 128 * * HTML 129 * * RTF 130 * * TIFF 131 * * PICT 132 */ 133 134 if (containsFilenames(m_platformDragData)) 135 if (PassRefPtr<DocumentFragment> fragment = fragmentFromFilenames(doc, m_platformDragData)) 136 return fragment; 137 138 if (containsHTML(m_platformDragData)) 139 if (PassRefPtr<DocumentFragment> fragment = fragmentFromHTML(doc, m_platformDragData)) 140 return fragment; 141 142 return 0; 143 } 144 145 Color DragData::asColor() const 146 { 147 return Color(); 148 } 149 150 } 151 152