2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "ClipboardUtilitiesWin.h"
32 #include "DocumentFragment.h"
33 #include "PlatformString.h"
35 #include "TextEncoding.h"
39 #include <wtf/Forward.h>
40 #include <wtf/Hashmap.h>
41 #include <wtf/PassRefPtr.h>
42 #include <wtf/RefPtr.h>
46 DragData::DragData(const DragDataMap& data, const IntPoint& clientPosition, const IntPoint& globalPosition,
47 DragOperation sourceOperationMask, DragApplicationFlags flags)
48 : m_clientPosition(clientPosition)
49 , m_globalPosition(globalPosition)
50 , m_platformDragData(0)
51 , m_draggingSourceOperationMask(sourceOperationMask)
52 , m_applicationFlags(flags)
57 bool DragData::containsURL(Frame*, FilenameConversionPolicy filenamePolicy) const
59 if (m_platformDragData)
60 return SUCCEEDED(m_platformDragData->QueryGetData(urlWFormat()))
61 || SUCCEEDED(m_platformDragData->QueryGetData(urlFormat()))
62 || (filenamePolicy == ConvertFilenames
63 && (SUCCEEDED(m_platformDragData->QueryGetData(filenameWFormat()))
64 || SUCCEEDED(m_platformDragData->QueryGetData(filenameFormat()))));
65 return m_dragDataMap.contains(urlWFormat()->cfFormat) || m_dragDataMap.contains(urlFormat()->cfFormat)
66 || (filenamePolicy == ConvertFilenames && (m_dragDataMap.contains(filenameWFormat()->cfFormat) || m_dragDataMap.contains(filenameFormat()->cfFormat)));
69 const DragDataMap& DragData::dragDataMap()
71 if (!m_dragDataMap.isEmpty() || !m_platformDragData)
73 // Enumerate clipboard content and load it in the map.
74 COMPtr<IEnumFORMATETC> itr;
76 if (FAILED(m_platformDragData->EnumFormatEtc(DATADIR_GET, &itr)) || !itr)
80 while (itr->Next(1, &dataFormat, 0) == S_OK) {
81 Vector<String> dataStrings;
82 getClipboardData(m_platformDragData, &dataFormat, dataStrings);
83 if (!dataStrings.isEmpty())
84 m_dragDataMap.set(dataFormat.cfFormat, dataStrings);
89 void DragData::getDragFileDescriptorData(int& size, String& pathname)
92 if (m_platformDragData)
93 getFileDescriptorData(m_platformDragData, size, pathname);
96 void DragData::getDragFileContentData(int size, void* dataBlob)
98 if (m_platformDragData)
99 getFileContentData(m_platformDragData, size, dataBlob);
102 String DragData::asURL(Frame*, FilenameConversionPolicy filenamePolicy, String* title) const
105 return (m_platformDragData) ? getURL(m_platformDragData, filenamePolicy, success, title) : getURL(&m_dragDataMap, filenamePolicy, title);
108 bool DragData::containsFiles() const
110 return (m_platformDragData) ? SUCCEEDED(m_platformDragData->QueryGetData(cfHDropFormat())) : m_dragDataMap.contains(cfHDropFormat()->cfFormat);
113 unsigned DragData::numberOfFiles() const
118 void DragData::asFilenames(Vector<String>& result) const
120 if (m_platformDragData) {
121 WCHAR filename[MAX_PATH];
124 if (FAILED(m_platformDragData->GetData(cfHDropFormat(), &medium)))
127 HDROP hdrop = (HDROP)GlobalLock(medium.hGlobal);
132 const unsigned numFiles = DragQueryFileW(hdrop, 0xFFFFFFFF, 0, 0);
133 for (unsigned i = 0; i < numFiles; i++) {
134 if (!DragQueryFileW(hdrop, 0, filename, WTF_ARRAY_LENGTH(filename)))
136 result.append((UChar*)filename);
139 // Free up memory from drag
142 GlobalUnlock(medium.hGlobal);
145 result = m_dragDataMap.get(cfHDropFormat()->cfFormat);
148 bool DragData::containsPlainText() const
150 if (m_platformDragData)
151 return SUCCEEDED(m_platformDragData->QueryGetData(plainTextWFormat()))
152 || SUCCEEDED(m_platformDragData->QueryGetData(plainTextFormat()));
153 return m_dragDataMap.contains(plainTextWFormat()->cfFormat) || m_dragDataMap.contains(plainTextFormat()->cfFormat);
156 String DragData::asPlainText(Frame*) const
159 return (m_platformDragData) ? getPlainText(m_platformDragData, success) : getPlainText(&m_dragDataMap);
162 bool DragData::containsColor() const
167 bool DragData::canSmartReplace() const
169 if (m_platformDragData)
170 return SUCCEEDED(m_platformDragData->QueryGetData(smartPasteFormat()));
171 return m_dragDataMap.contains(smartPasteFormat()->cfFormat);
174 bool DragData::containsCompatibleContent() const
176 return containsPlainText() || containsURL(0)
177 || ((m_platformDragData) ? (containsHTML(m_platformDragData) || containsFilenames(m_platformDragData))
178 : (containsHTML(&m_dragDataMap) || containsFilenames(&m_dragDataMap)))
182 PassRefPtr<DocumentFragment> DragData::asFragment(Frame* frame, PassRefPtr<Range>, bool, bool&) const
185 * Order is richest format first. On OSX this is:
194 if (m_platformDragData) {
195 if (containsFilenames(m_platformDragData)) {
196 if (PassRefPtr<DocumentFragment> fragment = fragmentFromFilenames(frame->document(), m_platformDragData))
200 if (containsHTML(m_platformDragData)) {
201 if (PassRefPtr<DocumentFragment> fragment = fragmentFromHTML(frame->document(), m_platformDragData))
205 if (containsFilenames(&m_dragDataMap)) {
206 if (PassRefPtr<DocumentFragment> fragment = fragmentFromFilenames(frame->document(), &m_dragDataMap))
210 if (containsHTML(&m_dragDataMap)) {
211 if (PassRefPtr<DocumentFragment> fragment = fragmentFromHTML(frame->document(), &m_dragDataMap))
218 Color DragData::asColor() const