2 * Copyright (C) 2006, 2007 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.
27 #include "Pasteboard.h"
29 #include "BitmapInfo.h"
30 #include "ClipboardUtilitiesWin.h"
32 #include "DocumentFragment.h"
35 #include "HitTestResult.h"
40 #include "RenderImage.h"
41 #include "TextEncoding.h"
42 #include "WebCoreInstanceHandle.h"
44 #include <wtf/text/CString.h>
48 static UINT HTMLClipboardFormat = 0;
49 static UINT BookmarkClipboardFormat = 0;
50 static UINT WebSmartPasteFormat = 0;
52 static LRESULT CALLBACK PasteboardOwnerWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
58 // This message comes when SetClipboardData was sent a null data handle
59 // and now it's come time to put the data on the clipboard.
61 case WM_RENDERALLFORMATS:
62 // This message comes when SetClipboardData was sent a null data handle
63 // and now this application is about to quit, so it must put data on
64 // the clipboard before it exits.
69 case WM_DRAWCLIPBOARD:
71 case WM_CHANGECBCHAIN:
75 lresult = DefWindowProc(hWnd, message, wParam, lParam);
81 Pasteboard* Pasteboard::generalPasteboard()
83 static Pasteboard* pasteboard = new Pasteboard;
87 Pasteboard::Pasteboard()
91 hWndParent = HWND_MESSAGE;
95 memset(&wc, 0, sizeof(WNDCLASS));
96 wc.lpfnWndProc = PasteboardOwnerWndProc;
97 wc.hInstance = WebCore::instanceHandle();
98 wc.lpszClassName = L"PasteboardOwnerWindowClass";
101 m_owner = ::CreateWindow(L"PasteboardOwnerWindowClass", L"PasteboardOwnerWindow", 0, 0, 0, 0, 0,
102 hWndParent, 0, 0, 0);
104 HTMLClipboardFormat = ::RegisterClipboardFormat(L"HTML Format");
105 BookmarkClipboardFormat = ::RegisterClipboardFormat(L"UniformResourceLocatorW");
106 WebSmartPasteFormat = ::RegisterClipboardFormat(L"WebKit Smart Paste Format");
109 void Pasteboard::clear()
111 if (::OpenClipboard(m_owner)) {
117 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
121 // Put CF_HTML format on the pasteboard
122 if (::OpenClipboard(m_owner)) {
123 ExceptionCode ec = 0;
125 markupToCFHTML(createMarkup(selectedRange, 0, AnnotateForInterchange),
126 selectedRange->startContainer(ec)->document()->url().string(), data);
127 HGLOBAL cbData = createGlobalData(data);
128 if (!::SetClipboardData(HTMLClipboardFormat, cbData))
129 ::GlobalFree(cbData);
133 // Put plain string on the pasteboard. CF_UNICODETEXT covers CF_TEXT as well
134 String str = frame->editor()->selectedText();
135 replaceNewlinesWithWindowsStyleNewlines(str);
136 replaceNBSPWithSpace(str);
137 if (::OpenClipboard(m_owner)) {
138 HGLOBAL cbData = createGlobalData(str);
139 if (!::SetClipboardData(CF_UNICODETEXT, cbData))
140 ::GlobalFree(cbData);
144 // enable smart-replacing later on by putting dummy data on the pasteboard
145 if (canSmartCopyOrDelete) {
146 if (::OpenClipboard(m_owner)) {
147 ::SetClipboardData(WebSmartPasteFormat, 0);
154 void Pasteboard::writePlainText(const String& text)
158 // Put plain string on the pasteboard. CF_UNICODETEXT covers CF_TEXT as well
160 replaceNewlinesWithWindowsStyleNewlines(str);
161 if (::OpenClipboard(m_owner)) {
162 HGLOBAL cbData = createGlobalData(str);
163 if (!::SetClipboardData(CF_UNICODETEXT, cbData))
164 ::GlobalFree(cbData);
169 void Pasteboard::writeURL(const KURL& url, const String& titleStr, Frame* frame)
171 ASSERT(!url.isEmpty());
175 String title(titleStr);
176 if (title.isEmpty()) {
177 title = url.lastPathComponent();
182 // write to clipboard in format com.apple.safari.bookmarkdata to be able to paste into the bookmarks view with appropriate title
183 if (::OpenClipboard(m_owner)) {
184 HGLOBAL cbData = createGlobalData(url, title);
185 if (!::SetClipboardData(BookmarkClipboardFormat, cbData))
186 ::GlobalFree(cbData);
190 // write to clipboard in format CF_HTML to be able to paste into contenteditable areas as a link
191 if (::OpenClipboard(m_owner)) {
193 markupToCFHTML(urlToMarkup(url, title), "", data);
194 HGLOBAL cbData = createGlobalData(data);
195 if (!::SetClipboardData(HTMLClipboardFormat, cbData))
196 ::GlobalFree(cbData);
200 // bare-bones CF_UNICODETEXT support
201 if (::OpenClipboard(m_owner)) {
202 HGLOBAL cbData = createGlobalData(url.string());
203 if (!::SetClipboardData(CF_UNICODETEXT, cbData))
204 ::GlobalFree(cbData);
209 void Pasteboard::writeImage(Node* node, const KURL&, const String&)
213 if (!(node->renderer() && node->renderer()->isImage()))
216 RenderImage* renderer = toRenderImage(node->renderer());
217 CachedImage* cachedImage = renderer->cachedImage();
218 if (!cachedImage || cachedImage->errorOccurred())
220 Image* image = cachedImage->image();
226 HDC compatibleDC = CreateCompatibleDC(0);
227 HDC sourceDC = CreateCompatibleDC(0);
228 OwnPtr<HBITMAP> resultBitmap = adoptPtr(CreateCompatibleBitmap(dc, image->width(), image->height()));
229 HGDIOBJ oldBitmap = SelectObject(compatibleDC, resultBitmap.get());
231 BitmapInfo bmInfo = BitmapInfo::create(image->size());
233 HBITMAP coreBitmap = CreateDIBSection(dc, &bmInfo, DIB_RGB_COLORS, 0, 0, 0);
234 HGDIOBJ oldSource = SelectObject(sourceDC, coreBitmap);
235 image->getHBITMAP(coreBitmap);
237 BitBlt(compatibleDC, 0, 0, image->width(), image->height(), sourceDC, 0, 0, SRCCOPY);
239 SelectObject(sourceDC, oldSource);
240 DeleteObject(coreBitmap);
242 SelectObject(compatibleDC, oldBitmap);
244 DeleteDC(compatibleDC);
247 if (::OpenClipboard(m_owner)) {
248 ::SetClipboardData(CF_BITMAP, resultBitmap.leakPtr());
253 bool Pasteboard::canSmartReplace()
255 return ::IsClipboardFormatAvailable(WebSmartPasteFormat);
258 String Pasteboard::plainText(Frame* frame)
260 if (::IsClipboardFormatAvailable(CF_UNICODETEXT) && ::OpenClipboard(m_owner)) {
261 HANDLE cbData = ::GetClipboardData(CF_UNICODETEXT);
263 UChar* buffer = static_cast<UChar*>(GlobalLock(cbData));
264 String fromClipboard(buffer);
265 GlobalUnlock(cbData);
267 return fromClipboard;
272 if (::IsClipboardFormatAvailable(CF_TEXT) && ::OpenClipboard(m_owner)) {
273 HANDLE cbData = ::GetClipboardData(CF_TEXT);
275 char* buffer = static_cast<char*>(GlobalLock(cbData));
276 String fromClipboard(buffer);
277 GlobalUnlock(cbData);
279 return fromClipboard;
287 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context, bool allowPlainText, bool& chosePlainText)
289 chosePlainText = false;
291 if (::IsClipboardFormatAvailable(HTMLClipboardFormat) && ::OpenClipboard(m_owner)) {
292 // get data off of clipboard
293 HANDLE cbData = ::GetClipboardData(HTMLClipboardFormat);
295 SIZE_T dataSize = ::GlobalSize(cbData);
296 String cfhtml(UTF8Encoding().decode(static_cast<char*>(GlobalLock(cbData)), dataSize));
297 GlobalUnlock(cbData);
300 PassRefPtr<DocumentFragment> fragment = fragmentFromCFHTML(frame->document(), cfhtml);
307 if (allowPlainText && ::IsClipboardFormatAvailable(CF_UNICODETEXT)) {
308 chosePlainText = true;
309 if (::OpenClipboard(m_owner)) {
310 HANDLE cbData = ::GetClipboardData(CF_UNICODETEXT);
312 UChar* buffer = static_cast<UChar*>(GlobalLock(cbData));
314 GlobalUnlock(cbData);
316 RefPtr<DocumentFragment> fragment = createFragmentFromText(context.get(), str);
318 return fragment.release();
324 if (allowPlainText && ::IsClipboardFormatAvailable(CF_TEXT)) {
325 chosePlainText = true;
326 if (::OpenClipboard(m_owner)) {
327 HANDLE cbData = ::GetClipboardData(CF_TEXT);
329 char* buffer = static_cast<char*>(GlobalLock(cbData));
331 GlobalUnlock(cbData);
333 RefPtr<DocumentFragment> fragment = createFragmentFromText(context.get(), str);
335 return fragment.release();
344 } // namespace WebCore