| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2006, 2007 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 "Pasteboard.h" | |
| 28 | |
| 29 #include "BitmapInfo.h" | |
| 30 #include "CachedImage.h" | |
| 31 #include "ClipboardUtilitiesWin.h" | |
| 32 #include "Document.h" | |
| 33 #include "DocumentFragment.h" | |
| 34 #include "Element.h" | |
| 35 #include "Frame.h" | |
| 36 #include "HWndDC.h" | |
| 37 #include "HitTestResult.h" | |
| 38 #include "Image.h" | |
| 39 #include "KURL.h" | |
| 40 #include "NotImplemented.h" | |
| 41 #include "Page.h" | |
| 42 #include "Range.h" | |
| 43 #include "RenderImage.h" | |
| 44 #include "TextEncoding.h" | |
| 45 #include "WebCoreInstanceHandle.h" | |
| 46 #include "WindowsExtras.h" | |
| 47 #include "markup.h" | |
| 48 #include <wtf/text/CString.h> | |
| 49 | |
| 50 namespace WebCore { | |
| 51 | |
| 52 static UINT HTMLClipboardFormat = 0; | |
| 53 static UINT BookmarkClipboardFormat = 0; | |
| 54 static UINT WebSmartPasteFormat = 0; | |
| 55 | |
| 56 static LRESULT CALLBACK PasteboardOwnerWndProc(HWND hWnd, UINT message, WPARAM w
Param, LPARAM lParam) | |
| 57 { | |
| 58 LRESULT lresult = 0; | |
| 59 | |
| 60 switch (message) { | |
| 61 case WM_RENDERFORMAT: | |
| 62 // This message comes when SetClipboardData was sent a null data handle | |
| 63 // and now it's come time to put the data on the clipboard. | |
| 64 break; | |
| 65 case WM_RENDERALLFORMATS: | |
| 66 // This message comes when SetClipboardData was sent a null data handle | |
| 67 // and now this application is about to quit, so it must put data on | |
| 68 // the clipboard before it exits. | |
| 69 break; | |
| 70 case WM_DESTROY: | |
| 71 break; | |
| 72 #if !OS(WINCE) | |
| 73 case WM_DRAWCLIPBOARD: | |
| 74 break; | |
| 75 case WM_CHANGECBCHAIN: | |
| 76 break; | |
| 77 #endif | |
| 78 default: | |
| 79 lresult = DefWindowProc(hWnd, message, wParam, lParam); | |
| 80 break; | |
| 81 } | |
| 82 return lresult; | |
| 83 } | |
| 84 | |
| 85 Pasteboard* Pasteboard::generalPasteboard() | |
| 86 { | |
| 87 static Pasteboard* pasteboard = new Pasteboard; | |
| 88 return pasteboard; | |
| 89 } | |
| 90 | |
| 91 Pasteboard::Pasteboard() | |
| 92 { | |
| 93 WNDCLASS wc; | |
| 94 memset(&wc, 0, sizeof(WNDCLASS)); | |
| 95 wc.lpfnWndProc = PasteboardOwnerWndProc; | |
| 96 wc.hInstance = WebCore::instanceHandle(); | |
| 97 wc.lpszClassName = L"PasteboardOwnerWindowClass"; | |
| 98 RegisterClass(&wc); | |
| 99 | |
| 100 m_owner = ::CreateWindow(L"PasteboardOwnerWindowClass", L"PasteboardOwnerWin
dow", 0, 0, 0, 0, 0, | |
| 101 HWND_MESSAGE, 0, 0, 0); | |
| 102 | |
| 103 HTMLClipboardFormat = ::RegisterClipboardFormat(L"HTML Format"); | |
| 104 BookmarkClipboardFormat = ::RegisterClipboardFormat(L"UniformResourceLocator
W"); | |
| 105 WebSmartPasteFormat = ::RegisterClipboardFormat(L"WebKit Smart Paste Format"
); | |
| 106 } | |
| 107 | |
| 108 void Pasteboard::clear() | |
| 109 { | |
| 110 if (::OpenClipboard(m_owner)) { | |
| 111 ::EmptyClipboard(); | |
| 112 ::CloseClipboard(); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete,
Frame* frame, ShouldSerializeSelectedTextForClipboard shouldSerializeSelectedTe
xtForClipboard) | |
| 117 { | |
| 118 clear(); | |
| 119 | |
| 120 // Put CF_HTML format on the pasteboard | |
| 121 if (::OpenClipboard(m_owner)) { | |
| 122 Vector<char> data; | |
| 123 markupToCFHTML(createMarkup(selectedRange, 0, AnnotateForInterchange), | |
| 124 selectedRange->startContainer()->document()->url().string(), data); | |
| 125 HGLOBAL cbData = createGlobalData(data); | |
| 126 if (!::SetClipboardData(HTMLClipboardFormat, cbData)) | |
| 127 ::GlobalFree(cbData); | |
| 128 ::CloseClipboard(); | |
| 129 } | |
| 130 | |
| 131 // Put plain string on the pasteboard. CF_UNICODETEXT covers CF_TEXT as well | |
| 132 String str = shouldSerializeSelectedTextForClipboard == IncludeImageAltTextF
orClipboard ? frame->editor()->selectedTextForClipboard() : frame->editor()->sel
ectedText(); | |
| 133 replaceNewlinesWithWindowsStyleNewlines(str); | |
| 134 replaceNBSPWithSpace(str); | |
| 135 if (::OpenClipboard(m_owner)) { | |
| 136 HGLOBAL cbData = createGlobalData(str); | |
| 137 if (!::SetClipboardData(CF_UNICODETEXT, cbData)) | |
| 138 ::GlobalFree(cbData); | |
| 139 ::CloseClipboard(); | |
| 140 } | |
| 141 | |
| 142 // enable smart-replacing later on by putting dummy data on the pasteboard | |
| 143 if (canSmartCopyOrDelete) { | |
| 144 if (::OpenClipboard(m_owner)) { | |
| 145 ::SetClipboardData(WebSmartPasteFormat, 0); | |
| 146 ::CloseClipboard(); | |
| 147 } | |
| 148 | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 void Pasteboard::writePlainText(const String& text, SmartReplaceOption smartRepl
aceOption) | |
| 153 { | |
| 154 clear(); | |
| 155 | |
| 156 // Put plain string on the pasteboard. CF_UNICODETEXT covers CF_TEXT as well | |
| 157 String str = text; | |
| 158 replaceNewlinesWithWindowsStyleNewlines(str); | |
| 159 if (::OpenClipboard(m_owner)) { | |
| 160 HGLOBAL cbData = createGlobalData(str); | |
| 161 if (!::SetClipboardData(CF_UNICODETEXT, cbData)) | |
| 162 ::GlobalFree(cbData); | |
| 163 ::CloseClipboard(); | |
| 164 } | |
| 165 | |
| 166 // enable smart-replacing later on by putting dummy data on the pasteboard | |
| 167 if (smartReplaceOption == CanSmartReplace) { | |
| 168 if (::OpenClipboard(m_owner)) { | |
| 169 ::SetClipboardData(WebSmartPasteFormat, 0); | |
| 170 ::CloseClipboard(); | |
| 171 } | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 void Pasteboard::writeURL(const KURL& url, const String& titleStr, Frame* frame) | |
| 176 { | |
| 177 ASSERT(!url.isEmpty()); | |
| 178 | |
| 179 clear(); | |
| 180 | |
| 181 String title(titleStr); | |
| 182 if (title.isEmpty()) { | |
| 183 title = url.lastPathComponent(); | |
| 184 if (title.isEmpty()) | |
| 185 title = url.host(); | |
| 186 } | |
| 187 | |
| 188 // write to clipboard in format com.apple.safari.bookmarkdata to be able to
paste into the bookmarks view with appropriate title | |
| 189 if (::OpenClipboard(m_owner)) { | |
| 190 HGLOBAL cbData = createGlobalData(url, title); | |
| 191 if (!::SetClipboardData(BookmarkClipboardFormat, cbData)) | |
| 192 ::GlobalFree(cbData); | |
| 193 ::CloseClipboard(); | |
| 194 } | |
| 195 | |
| 196 // write to clipboard in format CF_HTML to be able to paste into contentedit
able areas as a link | |
| 197 if (::OpenClipboard(m_owner)) { | |
| 198 Vector<char> data; | |
| 199 markupToCFHTML(urlToMarkup(url, title), "", data); | |
| 200 HGLOBAL cbData = createGlobalData(data); | |
| 201 if (!::SetClipboardData(HTMLClipboardFormat, cbData)) | |
| 202 ::GlobalFree(cbData); | |
| 203 ::CloseClipboard(); | |
| 204 } | |
| 205 | |
| 206 // bare-bones CF_UNICODETEXT support | |
| 207 if (::OpenClipboard(m_owner)) { | |
| 208 HGLOBAL cbData = createGlobalData(url.string()); | |
| 209 if (!::SetClipboardData(CF_UNICODETEXT, cbData)) | |
| 210 ::GlobalFree(cbData); | |
| 211 ::CloseClipboard(); | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 void Pasteboard::writeImage(Node* node, const KURL&, const String&) | |
| 216 { | |
| 217 ASSERT(node); | |
| 218 | |
| 219 if (!(node->renderer() && node->renderer()->isImage())) | |
| 220 return; | |
| 221 | |
| 222 RenderImage* renderer = toRenderImage(node->renderer()); | |
| 223 CachedImage* cachedImage = renderer->cachedImage(); | |
| 224 if (!cachedImage || cachedImage->errorOccurred()) | |
| 225 return; | |
| 226 Image* image = cachedImage->imageForRenderer(renderer); | |
| 227 ASSERT(image); | |
| 228 | |
| 229 clear(); | |
| 230 | |
| 231 HWndDC dc(0); | |
| 232 HDC compatibleDC = CreateCompatibleDC(0); | |
| 233 HDC sourceDC = CreateCompatibleDC(0); | |
| 234 OwnPtr<HBITMAP> resultBitmap = adoptPtr(CreateCompatibleBitmap(dc, image->wi
dth(), image->height())); | |
| 235 HGDIOBJ oldBitmap = SelectObject(compatibleDC, resultBitmap.get()); | |
| 236 | |
| 237 BitmapInfo bmInfo = BitmapInfo::create(image->size()); | |
| 238 | |
| 239 HBITMAP coreBitmap = CreateDIBSection(dc, &bmInfo, DIB_RGB_COLORS, 0, 0, 0); | |
| 240 HGDIOBJ oldSource = SelectObject(sourceDC, coreBitmap); | |
| 241 image->getHBITMAP(coreBitmap); | |
| 242 | |
| 243 BitBlt(compatibleDC, 0, 0, image->width(), image->height(), sourceDC, 0, 0,
SRCCOPY); | |
| 244 | |
| 245 SelectObject(sourceDC, oldSource); | |
| 246 DeleteObject(coreBitmap); | |
| 247 | |
| 248 SelectObject(compatibleDC, oldBitmap); | |
| 249 DeleteDC(sourceDC); | |
| 250 DeleteDC(compatibleDC); | |
| 251 | |
| 252 if (::OpenClipboard(m_owner)) { | |
| 253 ::SetClipboardData(CF_BITMAP, resultBitmap.leakPtr()); | |
| 254 ::CloseClipboard(); | |
| 255 } | |
| 256 } | |
| 257 | |
| 258 void Pasteboard::writeClipboard(Clipboard*) | |
| 259 { | |
| 260 notImplemented(); | |
| 261 } | |
| 262 | |
| 263 bool Pasteboard::canSmartReplace() | |
| 264 { | |
| 265 return ::IsClipboardFormatAvailable(WebSmartPasteFormat); | |
| 266 } | |
| 267 | |
| 268 String Pasteboard::plainText(Frame* frame) | |
| 269 { | |
| 270 if (::IsClipboardFormatAvailable(CF_UNICODETEXT) && ::OpenClipboard(m_owner)
) { | |
| 271 HANDLE cbData = ::GetClipboardData(CF_UNICODETEXT); | |
| 272 if (cbData) { | |
| 273 UChar* buffer = static_cast<UChar*>(GlobalLock(cbData)); | |
| 274 String fromClipboard(buffer); | |
| 275 GlobalUnlock(cbData); | |
| 276 ::CloseClipboard(); | |
| 277 return fromClipboard; | |
| 278 } | |
| 279 ::CloseClipboard(); | |
| 280 } | |
| 281 | |
| 282 if (::IsClipboardFormatAvailable(CF_TEXT) && ::OpenClipboard(m_owner)) { | |
| 283 HANDLE cbData = ::GetClipboardData(CF_TEXT); | |
| 284 if (cbData) { | |
| 285 char* buffer = static_cast<char*>(GlobalLock(cbData)); | |
| 286 String fromClipboard(buffer); | |
| 287 GlobalUnlock(cbData); | |
| 288 ::CloseClipboard(); | |
| 289 return fromClipboard; | |
| 290 } | |
| 291 ::CloseClipboard(); | |
| 292 } | |
| 293 | |
| 294 return String(); | |
| 295 } | |
| 296 | |
| 297 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefP
tr<Range> context, bool allowPlainText, bool& chosePlainText) | |
| 298 { | |
| 299 chosePlainText = false; | |
| 300 | |
| 301 if (::IsClipboardFormatAvailable(HTMLClipboardFormat) && ::OpenClipboard(m_o
wner)) { | |
| 302 // get data off of clipboard | |
| 303 HANDLE cbData = ::GetClipboardData(HTMLClipboardFormat); | |
| 304 if (cbData) { | |
| 305 SIZE_T dataSize = ::GlobalSize(cbData); | |
| 306 String cfhtml(UTF8Encoding().decode(static_cast<char*>(GlobalLock(cb
Data)), dataSize)); | |
| 307 GlobalUnlock(cbData); | |
| 308 ::CloseClipboard(); | |
| 309 | |
| 310 PassRefPtr<DocumentFragment> fragment = fragmentFromCFHTML(frame->do
cument(), cfhtml); | |
| 311 if (fragment) | |
| 312 return fragment; | |
| 313 } else | |
| 314 ::CloseClipboard(); | |
| 315 } | |
| 316 | |
| 317 if (allowPlainText && ::IsClipboardFormatAvailable(CF_UNICODETEXT)) { | |
| 318 chosePlainText = true; | |
| 319 if (::OpenClipboard(m_owner)) { | |
| 320 HANDLE cbData = ::GetClipboardData(CF_UNICODETEXT); | |
| 321 if (cbData) { | |
| 322 UChar* buffer = static_cast<UChar*>(GlobalLock(cbData)); | |
| 323 String str(buffer); | |
| 324 GlobalUnlock(cbData); | |
| 325 ::CloseClipboard(); | |
| 326 RefPtr<DocumentFragment> fragment = createFragmentFromText(conte
xt.get(), str); | |
| 327 if (fragment) | |
| 328 return fragment.release(); | |
| 329 } else | |
| 330 ::CloseClipboard(); | |
| 331 } | |
| 332 } | |
| 333 | |
| 334 if (allowPlainText && ::IsClipboardFormatAvailable(CF_TEXT)) { | |
| 335 chosePlainText = true; | |
| 336 if (::OpenClipboard(m_owner)) { | |
| 337 HANDLE cbData = ::GetClipboardData(CF_TEXT); | |
| 338 if (cbData) { | |
| 339 char* buffer = static_cast<char*>(GlobalLock(cbData)); | |
| 340 String str(buffer); | |
| 341 GlobalUnlock(cbData); | |
| 342 ::CloseClipboard(); | |
| 343 RefPtr<DocumentFragment> fragment = createFragmentFromText(conte
xt.get(), str); | |
| 344 if (fragment) | |
| 345 return fragment.release(); | |
| 346 } else | |
| 347 ::CloseClipboard(); | |
| 348 } | |
| 349 } | |
| 350 | |
| 351 return 0; | |
| 352 } | |
| 353 | |
| 354 } // namespace WebCore | |
| OLD | NEW |