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 "ClipboardUtilitiesWin.h" | |
30 #include "CString.h" | |
31 #include "DocumentFragment.h" | |
32 #include "Document.h" | |
33 #include "Element.h" | |
34 #include "Frame.h" | |
35 #include "HitTestResult.h" | |
36 #include "Image.h" | |
37 #include "KURL.h" | |
38 #include "NativeImageSkia.h" | |
39 #include "NotImplemented.h" | |
40 #include "Page.h" | |
41 #include "Range.h" | |
42 #include "RenderImage.h" | |
43 #include "TextEncoding.h" | |
44 #include "markup.h" | |
45 | |
46 #include "base/clipboard_util.h" | |
47 #include "webkit/glue/glue_util.h" | |
48 #include "webkit/glue/webkit_glue.h" | |
49 | |
50 namespace WebCore { | |
51 | |
52 static UINT HTMLClipboardFormat = 0; | |
53 static UINT BookmarkClipboardFormat = 0; | |
54 static UINT WebSmartPasteFormat = 0; | |
55 | |
56 Pasteboard* Pasteboard::generalPasteboard() | |
57 { | |
58 static Pasteboard* pasteboard = new Pasteboard; | |
59 return pasteboard; | |
60 } | |
61 | |
62 Pasteboard::Pasteboard() | |
63 { | |
64 HTMLClipboardFormat = ClipboardUtil::GetHtmlFormat()->cfFormat; | |
65 | |
66 BookmarkClipboardFormat = ClipboardUtil::GetUrlWFormat()->cfFormat; | |
67 | |
68 WebSmartPasteFormat = ClipboardUtil::GetWebKitSmartPasteFormat()->cfFormat; | |
69 } | |
70 | |
71 void Pasteboard::clear() | |
72 { | |
73 webkit_glue::ClipboardClear(); | |
74 } | |
75 | |
76 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete,
Frame* frame) | |
77 { | |
78 clear(); | |
79 | |
80 ExceptionCode ec = 0; | |
81 webkit_glue::ClipboardWriteHTML( | |
82 webkit_glue::StringToStdWString( | |
83 createMarkup(selectedRange, 0, AnnotateForInterchange)), | |
84 GURL(webkit_glue::StringToStdWString( | |
85 selectedRange->startContainer(ec)->document()->url()))); | |
86 | |
87 // Put plain string on the pasteboard. CF_UNICODETEXT covers CF_TEXT as well | |
88 String str = frame->selectedText(); | |
89 replaceNewlinesWithWindowsStyleNewlines(str); | |
90 replaceNBSPWithSpace(str); | |
91 webkit_glue::ClipboardWriteText(webkit_glue::StringToStdWString(str)); | |
92 | |
93 if (canSmartCopyOrDelete) | |
94 webkit_glue::ClipboardWriteWebSmartPaste(); | |
95 } | |
96 | |
97 void Pasteboard::writeURL(const KURL& url, const String& titleStr, Frame* frame) | |
98 { | |
99 ASSERT(!url.isEmpty()); | |
100 | |
101 clear(); | |
102 | |
103 String title(titleStr); | |
104 if (title.isEmpty()) { | |
105 title = url.lastPathComponent(); | |
106 if (title.isEmpty()) | |
107 title = url.host(); | |
108 } | |
109 | |
110 // write to clipboard in format com.apple.safari.bookmarkdata to be able to
paste into the bookmarks view with appropriate title | |
111 webkit_glue::ClipboardWriteBookmark(webkit_glue::StringToStdWString(titleStr
), | |
112 webkit_glue::KURLToGURL(url)); | |
113 | |
114 // write to clipboard in format CF_HTML to be able to paste into contentedit
able areas as a link | |
115 std::wstring link(webkit_glue::StringToStdWString(urlToMarkup(url, title))); | |
116 webkit_glue::ClipboardWriteHTML(link, GURL()); | |
117 | |
118 // bare-bones CF_UNICODETEXT support | |
119 std::wstring spec(webkit_glue::StringToStdWString(url)); | |
120 webkit_glue::ClipboardWriteText(spec); | |
121 } | |
122 | |
123 void Pasteboard::writeImage(Node* node, const KURL& url, const String& title) | |
124 { | |
125 ASSERT(node && node->renderer() && node->renderer()->isImage()); | |
126 RenderImage* renderer = static_cast<RenderImage*>(node->renderer()); | |
127 CachedImage* cachedImage = static_cast<CachedImage*>(renderer->cachedImage()
); | |
128 ASSERT(cachedImage); | |
129 Image* image = cachedImage->image(); | |
130 ASSERT(image); | |
131 | |
132 clear(); | |
133 NativeImageSkia* bitmap = image->nativeImageForCurrentFrame(); | |
134 if (bitmap) | |
135 webkit_glue::ClipboardWriteBitmap(*bitmap); | |
136 if (!url.isEmpty()) { | |
137 webkit_glue::ClipboardWriteBookmark(webkit_glue::StringToStdWString(titl
e), | |
138 webkit_glue::KURLToGURL(url)); | |
139 // write to clipboard in format com.apple.safari.bookmarkdata to be able
to paste into the bookmarks view with appropriate title | |
140 webkit_glue::ClipboardWriteBookmark(webkit_glue::StringToStdWString(titl
e), | |
141 webkit_glue::KURLToGURL(url)); | |
142 | |
143 // write to clipboard in format CF_HTML to be able to paste into content
editable areas as a an image | |
144 std::wstring markup(webkit_glue::StringToStdWString(urlToImageMarkup(url
, title))); | |
145 webkit_glue::ClipboardWriteHTML(markup, GURL()); | |
146 | |
147 // bare-bones CF_UNICODETEXT support | |
148 std::wstring spec(webkit_glue::StringToStdWString(url.string())); | |
149 webkit_glue::ClipboardWriteText(spec); | |
150 } | |
151 } | |
152 | |
153 bool Pasteboard::canSmartReplace() | |
154 { | |
155 return webkit_glue::ClipboardIsFormatAvailable(WebSmartPasteFormat); | |
156 } | |
157 | |
158 String Pasteboard::plainText(Frame* frame) | |
159 { | |
160 if (webkit_glue::ClipboardIsFormatAvailable(CF_UNICODETEXT)) { | |
161 std::wstring text; | |
162 webkit_glue::ClipboardReadText(&text); | |
163 if (!text.empty()) | |
164 return webkit_glue::StdWStringToString(text); | |
165 } | |
166 | |
167 if (webkit_glue::ClipboardIsFormatAvailable(CF_TEXT)) { | |
168 std::string text; | |
169 webkit_glue::ClipboardReadAsciiText(&text); | |
170 if (!text.empty()) | |
171 return webkit_glue::StdStringToString(text); | |
172 } | |
173 | |
174 return String(); | |
175 } | |
176 | |
177 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefP
tr<Range> context, bool allowPlainText, bool& chosePlainText) | |
178 { | |
179 chosePlainText = false; | |
180 | |
181 if (webkit_glue::ClipboardIsFormatAvailable(HTMLClipboardFormat)) { | |
182 std::wstring markup; | |
183 GURL src_url; | |
184 webkit_glue::ClipboardReadHTML(&markup, &src_url); | |
185 | |
186 PassRefPtr<DocumentFragment> fragment = | |
187 createFragmentFromMarkup(frame->document(), | |
188 webkit_glue::StdWStringToString(markup), | |
189 webkit_glue::StdStringToString(src_url.spec())); | |
190 | |
191 if (fragment) | |
192 return fragment; | |
193 } | |
194 | |
195 if (allowPlainText && | |
196 webkit_glue::ClipboardIsFormatAvailable(CF_UNICODETEXT)) { | |
197 | |
198 chosePlainText = true; | |
199 std::wstring markup; | |
200 webkit_glue::ClipboardReadText(&markup); | |
201 RefPtr<DocumentFragment> fragment = | |
202 createFragmentFromText(context.get(), | |
203 webkit_glue::StdWStringToString(markup)); | |
204 | |
205 if (fragment) | |
206 return fragment.release(); | |
207 } | |
208 | |
209 if (allowPlainText && webkit_glue::ClipboardIsFormatAvailable(CF_TEXT)) { | |
210 chosePlainText = true; | |
211 std::string markup; | |
212 webkit_glue::ClipboardReadAsciiText(&markup); | |
213 RefPtr<DocumentFragment> fragment = | |
214 createFragmentFromText(context.get(), | |
215 webkit_glue::StdStringToString(markup)); | |
216 if (fragment) | |
217 return fragment.release(); | |
218 } | |
219 | |
220 return 0; | |
221 } | |
222 | |
223 } // namespace WebCore | |
OLD | NEW |