OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Many of these functions are based on those found in | 5 // Many of these functions are based on those found in |
6 // webkit/port/platform/PasteboardWin.cpp | 6 // webkit/port/platform/PasteboardWin.cpp |
7 | 7 |
8 #include "ui/base/clipboard/clipboard.h" | 8 #include "ui/base/clipboard/clipboard.h" |
9 | 9 |
10 #include <shellapi.h> | 10 #include <shellapi.h> |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 void MakeBitmapOpaque(const SkBitmap& bitmap) { | 154 void MakeBitmapOpaque(const SkBitmap& bitmap) { |
155 for (int x = 0; x < bitmap.width(); ++x) { | 155 for (int x = 0; x < bitmap.width(); ++x) { |
156 for (int y = 0; y < bitmap.height(); ++y) { | 156 for (int y = 0; y < bitmap.height(); ++y) { |
157 *bitmap.getAddr32(x, y) = SkColorSetA(*bitmap.getAddr32(x, y), 0xFF); | 157 *bitmap.getAddr32(x, y) = SkColorSetA(*bitmap.getAddr32(x, y), 0xFF); |
158 } | 158 } |
159 } | 159 } |
160 } | 160 } |
161 | 161 |
162 } // namespace | 162 } // namespace |
163 | 163 |
| 164 // Clipboard::FormatType implementation. |
164 Clipboard::FormatType::FormatType() : data_() {} | 165 Clipboard::FormatType::FormatType() : data_() {} |
165 | 166 |
166 Clipboard::FormatType::FormatType(UINT native_format) : data_() { | 167 Clipboard::FormatType::FormatType(UINT native_format) : data_() { |
167 // There's no good way to actually initialize this in the constructor in | 168 // There's no good way to actually initialize this in the constructor in |
168 // C++03. | 169 // C++03. |
169 data_.cfFormat = native_format; | 170 data_.cfFormat = native_format; |
170 data_.dwAspect = DVASPECT_CONTENT; | 171 data_.dwAspect = DVASPECT_CONTENT; |
171 data_.lindex = -1; | 172 data_.lindex = -1; |
172 data_.tymed = TYMED_HGLOBAL; | 173 data_.tymed = TYMED_HGLOBAL; |
173 } | 174 } |
(...skipping 26 matching lines...) Expand all Loading... |
200 } | 201 } |
201 | 202 |
202 bool Clipboard::FormatType::operator<(const FormatType& other) const { | 203 bool Clipboard::FormatType::operator<(const FormatType& other) const { |
203 return ToUINT() < other.ToUINT(); | 204 return ToUINT() < other.ToUINT(); |
204 } | 205 } |
205 | 206 |
206 bool Clipboard::FormatType::Equals(const FormatType& other) const { | 207 bool Clipboard::FormatType::Equals(const FormatType& other) const { |
207 return ToUINT() == other.ToUINT(); | 208 return ToUINT() == other.ToUINT(); |
208 } | 209 } |
209 | 210 |
| 211 // Various predefined FormatTypes. |
| 212 // static |
| 213 Clipboard::FormatType Clipboard::GetFormatType( |
| 214 const std::string& format_string) { |
| 215 return FormatType( |
| 216 ::RegisterClipboardFormat(base::ASCIIToWide(format_string).c_str())); |
| 217 } |
| 218 |
| 219 // static |
| 220 const Clipboard::FormatType& Clipboard::GetUrlFormatType() { |
| 221 CR_DEFINE_STATIC_LOCAL( |
| 222 FormatType, type, (::RegisterClipboardFormat(CFSTR_INETURLA))); |
| 223 return type; |
| 224 } |
| 225 |
| 226 // static |
| 227 const Clipboard::FormatType& Clipboard::GetUrlWFormatType() { |
| 228 CR_DEFINE_STATIC_LOCAL( |
| 229 FormatType, type, (::RegisterClipboardFormat(CFSTR_INETURLW))); |
| 230 return type; |
| 231 } |
| 232 |
| 233 // static |
| 234 const Clipboard::FormatType& Clipboard::GetMozUrlFormatType() { |
| 235 CR_DEFINE_STATIC_LOCAL( |
| 236 FormatType, type, (::RegisterClipboardFormat(L"text/x-moz-url"))); |
| 237 return type; |
| 238 } |
| 239 |
| 240 // static |
| 241 const Clipboard::FormatType& Clipboard::GetPlainTextFormatType() { |
| 242 CR_DEFINE_STATIC_LOCAL(FormatType, type, (CF_TEXT)); |
| 243 return type; |
| 244 } |
| 245 |
| 246 // static |
| 247 const Clipboard::FormatType& Clipboard::GetPlainTextWFormatType() { |
| 248 CR_DEFINE_STATIC_LOCAL(FormatType, type, (CF_UNICODETEXT)); |
| 249 return type; |
| 250 } |
| 251 |
| 252 // static |
| 253 const Clipboard::FormatType& Clipboard::GetFilenameFormatType() { |
| 254 CR_DEFINE_STATIC_LOCAL( |
| 255 FormatType, type, (::RegisterClipboardFormat(CFSTR_FILENAMEA))); |
| 256 return type; |
| 257 } |
| 258 |
| 259 // static |
| 260 const Clipboard::FormatType& Clipboard::GetFilenameWFormatType() { |
| 261 CR_DEFINE_STATIC_LOCAL( |
| 262 FormatType, type, (::RegisterClipboardFormat(CFSTR_FILENAMEW))); |
| 263 return type; |
| 264 } |
| 265 |
| 266 // MS HTML Format |
| 267 // static |
| 268 const Clipboard::FormatType& Clipboard::GetHtmlFormatType() { |
| 269 CR_DEFINE_STATIC_LOCAL( |
| 270 FormatType, type, (::RegisterClipboardFormat(L"HTML Format"))); |
| 271 return type; |
| 272 } |
| 273 |
| 274 // MS RTF Format |
| 275 // static |
| 276 const Clipboard::FormatType& Clipboard::GetRtfFormatType() { |
| 277 CR_DEFINE_STATIC_LOCAL( |
| 278 FormatType, type, (::RegisterClipboardFormat(L"Rich Text Format"))); |
| 279 return type; |
| 280 } |
| 281 |
| 282 // static |
| 283 const Clipboard::FormatType& Clipboard::GetBitmapFormatType() { |
| 284 CR_DEFINE_STATIC_LOCAL(FormatType, type, (CF_BITMAP)); |
| 285 return type; |
| 286 } |
| 287 |
| 288 // Firefox text/html |
| 289 // static |
| 290 const Clipboard::FormatType& Clipboard::GetTextHtmlFormatType() { |
| 291 CR_DEFINE_STATIC_LOCAL( |
| 292 FormatType, type, (::RegisterClipboardFormat(L"text/html"))); |
| 293 return type; |
| 294 } |
| 295 |
| 296 // static |
| 297 const Clipboard::FormatType& Clipboard::GetCFHDropFormatType() { |
| 298 CR_DEFINE_STATIC_LOCAL(FormatType, type, (CF_HDROP)); |
| 299 return type; |
| 300 } |
| 301 |
| 302 // static |
| 303 const Clipboard::FormatType& Clipboard::GetFileDescriptorFormatType() { |
| 304 CR_DEFINE_STATIC_LOCAL( |
| 305 FormatType, type, (::RegisterClipboardFormat(CFSTR_FILEDESCRIPTOR))); |
| 306 return type; |
| 307 } |
| 308 |
| 309 // static |
| 310 const Clipboard::FormatType& Clipboard::GetFileContentZeroFormatType() { |
| 311 CR_DEFINE_STATIC_LOCAL( |
| 312 FormatType, type, (::RegisterClipboardFormat(CFSTR_FILECONTENTS), 0)); |
| 313 return type; |
| 314 } |
| 315 |
| 316 // static |
| 317 const Clipboard::FormatType& Clipboard::GetIDListFormatType() { |
| 318 CR_DEFINE_STATIC_LOCAL( |
| 319 FormatType, type, (::RegisterClipboardFormat(CFSTR_SHELLIDLIST))); |
| 320 return type; |
| 321 } |
| 322 |
| 323 // static |
| 324 const Clipboard::FormatType& Clipboard::GetWebKitSmartPasteFormatType() { |
| 325 CR_DEFINE_STATIC_LOCAL( |
| 326 FormatType, |
| 327 type, |
| 328 (::RegisterClipboardFormat(L"WebKit Smart Paste Format"))); |
| 329 return type; |
| 330 } |
| 331 |
| 332 // static |
| 333 const Clipboard::FormatType& Clipboard::GetWebCustomDataFormatType() { |
| 334 // TODO(dcheng): This name is temporary. See http://crbug.com/106449. |
| 335 CR_DEFINE_STATIC_LOCAL( |
| 336 FormatType, |
| 337 type, |
| 338 (::RegisterClipboardFormat(L"Chromium Web Custom MIME Data Format"))); |
| 339 return type; |
| 340 } |
| 341 |
| 342 // static |
| 343 const Clipboard::FormatType& Clipboard::GetPepperCustomDataFormatType() { |
| 344 CR_DEFINE_STATIC_LOCAL( |
| 345 FormatType, |
| 346 type, |
| 347 (::RegisterClipboardFormat(L"Chromium Pepper MIME Data Format"))); |
| 348 return type; |
| 349 } |
| 350 |
| 351 // Clipboard implementation. |
210 Clipboard::Clipboard() { | 352 Clipboard::Clipboard() { |
211 if (base::MessageLoopForUI::IsCurrent()) | 353 if (base::MessageLoopForUI::IsCurrent()) |
212 clipboard_owner_.reset(new base::win::MessageWindow()); | 354 clipboard_owner_.reset(new base::win::MessageWindow()); |
213 } | 355 } |
214 | 356 |
215 Clipboard::~Clipboard() { | 357 Clipboard::~Clipboard() { |
216 } | 358 } |
217 | 359 |
218 void Clipboard::WriteObjects(ClipboardType type, const ObjectMap& objects) { | |
219 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); | |
220 | |
221 ScopedClipboard clipboard; | |
222 if (!clipboard.Acquire(GetClipboardWindow())) | |
223 return; | |
224 | |
225 ::EmptyClipboard(); | |
226 | |
227 for (ObjectMap::const_iterator iter = objects.begin(); | |
228 iter != objects.end(); ++iter) { | |
229 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); | |
230 } | |
231 } | |
232 | |
233 void Clipboard::WriteText(const char* text_data, size_t text_len) { | |
234 base::string16 text; | |
235 base::UTF8ToUTF16(text_data, text_len, &text); | |
236 HGLOBAL glob = CreateGlobalData(text); | |
237 | |
238 WriteToClipboard(CF_UNICODETEXT, glob); | |
239 } | |
240 | |
241 void Clipboard::WriteHTML(const char* markup_data, | |
242 size_t markup_len, | |
243 const char* url_data, | |
244 size_t url_len) { | |
245 std::string markup(markup_data, markup_len); | |
246 std::string url; | |
247 | |
248 if (url_len > 0) | |
249 url.assign(url_data, url_len); | |
250 | |
251 std::string html_fragment = ClipboardUtil::HtmlToCFHtml(markup, url); | |
252 HGLOBAL glob = CreateGlobalData(html_fragment); | |
253 | |
254 WriteToClipboard(Clipboard::GetHtmlFormatType().ToUINT(), glob); | |
255 } | |
256 | |
257 void Clipboard::WriteRTF(const char* rtf_data, size_t data_len) { | |
258 WriteData(GetRtfFormatType(), rtf_data, data_len); | |
259 } | |
260 | |
261 void Clipboard::WriteBookmark(const char* title_data, | |
262 size_t title_len, | |
263 const char* url_data, | |
264 size_t url_len) { | |
265 std::string bookmark(title_data, title_len); | |
266 bookmark.append(1, L'\n'); | |
267 bookmark.append(url_data, url_len); | |
268 | |
269 base::string16 wide_bookmark = base::UTF8ToWide(bookmark); | |
270 HGLOBAL glob = CreateGlobalData(wide_bookmark); | |
271 | |
272 WriteToClipboard(GetUrlWFormatType().ToUINT(), glob); | |
273 } | |
274 | |
275 void Clipboard::WriteWebSmartPaste() { | |
276 DCHECK(clipboard_owner_->hwnd() != NULL); | |
277 ::SetClipboardData(GetWebKitSmartPasteFormatType().ToUINT(), NULL); | |
278 } | |
279 | |
280 void Clipboard::WriteBitmap(const SkBitmap& bitmap) { | |
281 HDC dc = ::GetDC(NULL); | |
282 | |
283 // This doesn't actually cost us a memcpy when the bitmap comes from the | |
284 // renderer as we load it into the bitmap using setPixels which just sets a | |
285 // pointer. Someone has to memcpy it into GDI, it might as well be us here. | |
286 | |
287 // TODO(darin): share data in gfx/bitmap_header.cc somehow | |
288 BITMAPINFO bm_info = {0}; | |
289 bm_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | |
290 bm_info.bmiHeader.biWidth = bitmap.width(); | |
291 bm_info.bmiHeader.biHeight = -bitmap.height(); // sets vertical orientation | |
292 bm_info.bmiHeader.biPlanes = 1; | |
293 bm_info.bmiHeader.biBitCount = 32; | |
294 bm_info.bmiHeader.biCompression = BI_RGB; | |
295 | |
296 // ::CreateDIBSection allocates memory for us to copy our bitmap into. | |
297 // Unfortunately, we can't write the created bitmap to the clipboard, | |
298 // (see http://msdn2.microsoft.com/en-us/library/ms532292.aspx) | |
299 void *bits; | |
300 HBITMAP source_hbitmap = | |
301 ::CreateDIBSection(dc, &bm_info, DIB_RGB_COLORS, &bits, NULL, 0); | |
302 | |
303 if (bits && source_hbitmap) { | |
304 { | |
305 SkAutoLockPixels bitmap_lock(bitmap); | |
306 // Copy the bitmap out of shared memory and into GDI | |
307 memcpy(bits, bitmap.getPixels(), bitmap.getSize()); | |
308 } | |
309 | |
310 // Now we have an HBITMAP, we can write it to the clipboard | |
311 WriteBitmapFromHandle(source_hbitmap, | |
312 gfx::Size(bitmap.width(), bitmap.height())); | |
313 } | |
314 | |
315 ::DeleteObject(source_hbitmap); | |
316 ::ReleaseDC(NULL, dc); | |
317 } | |
318 | |
319 void Clipboard::WriteBitmapFromHandle(HBITMAP source_hbitmap, | |
320 const gfx::Size& size) { | |
321 // We would like to just call ::SetClipboardData on the source_hbitmap, | |
322 // but that bitmap might not be of a sort we can write to the clipboard. | |
323 // For this reason, we create a new bitmap, copy the bits over, and then | |
324 // write that to the clipboard. | |
325 | |
326 HDC dc = ::GetDC(NULL); | |
327 HDC compatible_dc = ::CreateCompatibleDC(NULL); | |
328 HDC source_dc = ::CreateCompatibleDC(NULL); | |
329 | |
330 // This is the HBITMAP we will eventually write to the clipboard | |
331 HBITMAP hbitmap = ::CreateCompatibleBitmap(dc, size.width(), size.height()); | |
332 if (!hbitmap) { | |
333 // Failed to create the bitmap | |
334 ::DeleteDC(compatible_dc); | |
335 ::DeleteDC(source_dc); | |
336 ::ReleaseDC(NULL, dc); | |
337 return; | |
338 } | |
339 | |
340 HBITMAP old_hbitmap = (HBITMAP)SelectObject(compatible_dc, hbitmap); | |
341 HBITMAP old_source = (HBITMAP)SelectObject(source_dc, source_hbitmap); | |
342 | |
343 // Now we need to blend it into an HBITMAP we can place on the clipboard | |
344 BLENDFUNCTION bf = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA}; | |
345 ::GdiAlphaBlend(compatible_dc, 0, 0, size.width(), size.height(), | |
346 source_dc, 0, 0, size.width(), size.height(), bf); | |
347 | |
348 // Clean up all the handles we just opened | |
349 ::SelectObject(compatible_dc, old_hbitmap); | |
350 ::SelectObject(source_dc, old_source); | |
351 ::DeleteObject(old_hbitmap); | |
352 ::DeleteObject(old_source); | |
353 ::DeleteDC(compatible_dc); | |
354 ::DeleteDC(source_dc); | |
355 ::ReleaseDC(NULL, dc); | |
356 | |
357 WriteToClipboard(CF_BITMAP, hbitmap); | |
358 } | |
359 | |
360 void Clipboard::WriteData(const FormatType& format, | |
361 const char* data_data, | |
362 size_t data_len) { | |
363 HGLOBAL hdata = ::GlobalAlloc(GMEM_MOVEABLE, data_len); | |
364 if (!hdata) | |
365 return; | |
366 | |
367 char* data = static_cast<char*>(::GlobalLock(hdata)); | |
368 memcpy(data, data_data, data_len); | |
369 ::GlobalUnlock(data); | |
370 WriteToClipboard(format.ToUINT(), hdata); | |
371 } | |
372 | |
373 void Clipboard::WriteToClipboard(unsigned int format, HANDLE handle) { | |
374 DCHECK(clipboard_owner_->hwnd() != NULL); | |
375 if (handle && !::SetClipboardData(format, handle)) { | |
376 DCHECK(ERROR_CLIPBOARD_NOT_OPEN != GetLastError()); | |
377 FreeData(format, handle); | |
378 } | |
379 } | |
380 | |
381 uint64 Clipboard::GetSequenceNumber(ClipboardType type) { | 360 uint64 Clipboard::GetSequenceNumber(ClipboardType type) { |
382 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); | 361 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); |
383 return ::GetClipboardSequenceNumber(); | 362 return ::GetClipboardSequenceNumber(); |
384 } | 363 } |
385 | 364 |
386 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format, | 365 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format, |
387 ClipboardType type) const { | 366 ClipboardType type) const { |
388 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); | 367 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); |
389 return ::IsClipboardFormatAvailable(format.ToUINT()) != FALSE; | 368 return ::IsClipboardFormatAvailable(format.ToUINT()) != FALSE; |
390 } | 369 } |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 | 652 |
674 if (url) { | 653 if (url) { |
675 const size_t url_start = bookmark.find_first_not_of(kDelim, title_end); | 654 const size_t url_start = bookmark.find_first_not_of(kDelim, title_end); |
676 if (url_start != base::string16::npos) { | 655 if (url_start != base::string16::npos) { |
677 *url = base::UTF16ToUTF8( | 656 *url = base::UTF16ToUTF8( |
678 bookmark.substr(url_start, base::string16::npos)); | 657 bookmark.substr(url_start, base::string16::npos)); |
679 } | 658 } |
680 } | 659 } |
681 } | 660 } |
682 | 661 |
| 662 void Clipboard::WriteObjects(ClipboardType type, const ObjectMap& objects) { |
| 663 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); |
| 664 |
| 665 ScopedClipboard clipboard; |
| 666 if (!clipboard.Acquire(GetClipboardWindow())) |
| 667 return; |
| 668 |
| 669 ::EmptyClipboard(); |
| 670 |
| 671 for (ObjectMap::const_iterator iter = objects.begin(); |
| 672 iter != objects.end(); ++iter) { |
| 673 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); |
| 674 } |
| 675 } |
| 676 |
| 677 void Clipboard::WriteText(const char* text_data, size_t text_len) { |
| 678 base::string16 text; |
| 679 base::UTF8ToUTF16(text_data, text_len, &text); |
| 680 HGLOBAL glob = CreateGlobalData(text); |
| 681 |
| 682 WriteToClipboard(CF_UNICODETEXT, glob); |
| 683 } |
| 684 |
| 685 void Clipboard::WriteHTML(const char* markup_data, |
| 686 size_t markup_len, |
| 687 const char* url_data, |
| 688 size_t url_len) { |
| 689 std::string markup(markup_data, markup_len); |
| 690 std::string url; |
| 691 |
| 692 if (url_len > 0) |
| 693 url.assign(url_data, url_len); |
| 694 |
| 695 std::string html_fragment = ClipboardUtil::HtmlToCFHtml(markup, url); |
| 696 HGLOBAL glob = CreateGlobalData(html_fragment); |
| 697 |
| 698 WriteToClipboard(Clipboard::GetHtmlFormatType().ToUINT(), glob); |
| 699 } |
| 700 |
| 701 void Clipboard::WriteRTF(const char* rtf_data, size_t data_len) { |
| 702 WriteData(GetRtfFormatType(), rtf_data, data_len); |
| 703 } |
| 704 |
| 705 void Clipboard::WriteBookmark(const char* title_data, |
| 706 size_t title_len, |
| 707 const char* url_data, |
| 708 size_t url_len) { |
| 709 std::string bookmark(title_data, title_len); |
| 710 bookmark.append(1, L'\n'); |
| 711 bookmark.append(url_data, url_len); |
| 712 |
| 713 base::string16 wide_bookmark = base::UTF8ToWide(bookmark); |
| 714 HGLOBAL glob = CreateGlobalData(wide_bookmark); |
| 715 |
| 716 WriteToClipboard(GetUrlWFormatType().ToUINT(), glob); |
| 717 } |
| 718 |
| 719 void Clipboard::WriteWebSmartPaste() { |
| 720 DCHECK(clipboard_owner_->hwnd() != NULL); |
| 721 ::SetClipboardData(GetWebKitSmartPasteFormatType().ToUINT(), NULL); |
| 722 } |
| 723 |
| 724 void Clipboard::WriteBitmap(const SkBitmap& bitmap) { |
| 725 HDC dc = ::GetDC(NULL); |
| 726 |
| 727 // This doesn't actually cost us a memcpy when the bitmap comes from the |
| 728 // renderer as we load it into the bitmap using setPixels which just sets a |
| 729 // pointer. Someone has to memcpy it into GDI, it might as well be us here. |
| 730 |
| 731 // TODO(darin): share data in gfx/bitmap_header.cc somehow |
| 732 BITMAPINFO bm_info = {0}; |
| 733 bm_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
| 734 bm_info.bmiHeader.biWidth = bitmap.width(); |
| 735 bm_info.bmiHeader.biHeight = -bitmap.height(); // sets vertical orientation |
| 736 bm_info.bmiHeader.biPlanes = 1; |
| 737 bm_info.bmiHeader.biBitCount = 32; |
| 738 bm_info.bmiHeader.biCompression = BI_RGB; |
| 739 |
| 740 // ::CreateDIBSection allocates memory for us to copy our bitmap into. |
| 741 // Unfortunately, we can't write the created bitmap to the clipboard, |
| 742 // (see http://msdn2.microsoft.com/en-us/library/ms532292.aspx) |
| 743 void *bits; |
| 744 HBITMAP source_hbitmap = |
| 745 ::CreateDIBSection(dc, &bm_info, DIB_RGB_COLORS, &bits, NULL, 0); |
| 746 |
| 747 if (bits && source_hbitmap) { |
| 748 { |
| 749 SkAutoLockPixels bitmap_lock(bitmap); |
| 750 // Copy the bitmap out of shared memory and into GDI |
| 751 memcpy(bits, bitmap.getPixels(), bitmap.getSize()); |
| 752 } |
| 753 |
| 754 // Now we have an HBITMAP, we can write it to the clipboard |
| 755 WriteBitmapFromHandle(source_hbitmap, |
| 756 gfx::Size(bitmap.width(), bitmap.height())); |
| 757 } |
| 758 |
| 759 ::DeleteObject(source_hbitmap); |
| 760 ::ReleaseDC(NULL, dc); |
| 761 } |
| 762 |
| 763 void Clipboard::WriteBitmapFromHandle(HBITMAP source_hbitmap, |
| 764 const gfx::Size& size) { |
| 765 // We would like to just call ::SetClipboardData on the source_hbitmap, |
| 766 // but that bitmap might not be of a sort we can write to the clipboard. |
| 767 // For this reason, we create a new bitmap, copy the bits over, and then |
| 768 // write that to the clipboard. |
| 769 |
| 770 HDC dc = ::GetDC(NULL); |
| 771 HDC compatible_dc = ::CreateCompatibleDC(NULL); |
| 772 HDC source_dc = ::CreateCompatibleDC(NULL); |
| 773 |
| 774 // This is the HBITMAP we will eventually write to the clipboard |
| 775 HBITMAP hbitmap = ::CreateCompatibleBitmap(dc, size.width(), size.height()); |
| 776 if (!hbitmap) { |
| 777 // Failed to create the bitmap |
| 778 ::DeleteDC(compatible_dc); |
| 779 ::DeleteDC(source_dc); |
| 780 ::ReleaseDC(NULL, dc); |
| 781 return; |
| 782 } |
| 783 |
| 784 HBITMAP old_hbitmap = (HBITMAP)SelectObject(compatible_dc, hbitmap); |
| 785 HBITMAP old_source = (HBITMAP)SelectObject(source_dc, source_hbitmap); |
| 786 |
| 787 // Now we need to blend it into an HBITMAP we can place on the clipboard |
| 788 BLENDFUNCTION bf = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA}; |
| 789 ::GdiAlphaBlend(compatible_dc, 0, 0, size.width(), size.height(), |
| 790 source_dc, 0, 0, size.width(), size.height(), bf); |
| 791 |
| 792 // Clean up all the handles we just opened |
| 793 ::SelectObject(compatible_dc, old_hbitmap); |
| 794 ::SelectObject(source_dc, old_source); |
| 795 ::DeleteObject(old_hbitmap); |
| 796 ::DeleteObject(old_source); |
| 797 ::DeleteDC(compatible_dc); |
| 798 ::DeleteDC(source_dc); |
| 799 ::ReleaseDC(NULL, dc); |
| 800 |
| 801 WriteToClipboard(CF_BITMAP, hbitmap); |
| 802 } |
| 803 |
| 804 void Clipboard::WriteData(const FormatType& format, |
| 805 const char* data_data, |
| 806 size_t data_len) { |
| 807 HGLOBAL hdata = ::GlobalAlloc(GMEM_MOVEABLE, data_len); |
| 808 if (!hdata) |
| 809 return; |
| 810 |
| 811 char* data = static_cast<char*>(::GlobalLock(hdata)); |
| 812 memcpy(data, data_data, data_len); |
| 813 ::GlobalUnlock(data); |
| 814 WriteToClipboard(format.ToUINT(), hdata); |
| 815 } |
| 816 |
| 817 void Clipboard::WriteToClipboard(unsigned int format, HANDLE handle) { |
| 818 DCHECK(clipboard_owner_->hwnd() != NULL); |
| 819 if (handle && !::SetClipboardData(format, handle)) { |
| 820 DCHECK(ERROR_CLIPBOARD_NOT_OPEN != GetLastError()); |
| 821 FreeData(format, handle); |
| 822 } |
| 823 } |
| 824 |
683 // static | 825 // static |
684 Clipboard::FormatType Clipboard::GetFormatType( | |
685 const std::string& format_string) { | |
686 return FormatType( | |
687 ::RegisterClipboardFormat(base::ASCIIToWide(format_string).c_str())); | |
688 } | |
689 | |
690 // static | |
691 const Clipboard::FormatType& Clipboard::GetUrlFormatType() { | |
692 CR_DEFINE_STATIC_LOCAL( | |
693 FormatType, type, (::RegisterClipboardFormat(CFSTR_INETURLA))); | |
694 return type; | |
695 } | |
696 | |
697 // static | |
698 const Clipboard::FormatType& Clipboard::GetUrlWFormatType() { | |
699 CR_DEFINE_STATIC_LOCAL( | |
700 FormatType, type, (::RegisterClipboardFormat(CFSTR_INETURLW))); | |
701 return type; | |
702 } | |
703 | |
704 // static | |
705 const Clipboard::FormatType& Clipboard::GetMozUrlFormatType() { | |
706 CR_DEFINE_STATIC_LOCAL( | |
707 FormatType, type, (::RegisterClipboardFormat(L"text/x-moz-url"))); | |
708 return type; | |
709 } | |
710 | |
711 // static | |
712 const Clipboard::FormatType& Clipboard::GetPlainTextFormatType() { | |
713 CR_DEFINE_STATIC_LOCAL(FormatType, type, (CF_TEXT)); | |
714 return type; | |
715 } | |
716 | |
717 // static | |
718 const Clipboard::FormatType& Clipboard::GetPlainTextWFormatType() { | |
719 CR_DEFINE_STATIC_LOCAL(FormatType, type, (CF_UNICODETEXT)); | |
720 return type; | |
721 } | |
722 | |
723 // static | |
724 const Clipboard::FormatType& Clipboard::GetFilenameFormatType() { | |
725 CR_DEFINE_STATIC_LOCAL( | |
726 FormatType, type, (::RegisterClipboardFormat(CFSTR_FILENAMEA))); | |
727 return type; | |
728 } | |
729 | |
730 // static | |
731 const Clipboard::FormatType& Clipboard::GetFilenameWFormatType() { | |
732 CR_DEFINE_STATIC_LOCAL( | |
733 FormatType, type, (::RegisterClipboardFormat(CFSTR_FILENAMEW))); | |
734 return type; | |
735 } | |
736 | |
737 // MS HTML Format | |
738 // static | |
739 const Clipboard::FormatType& Clipboard::GetHtmlFormatType() { | |
740 CR_DEFINE_STATIC_LOCAL( | |
741 FormatType, type, (::RegisterClipboardFormat(L"HTML Format"))); | |
742 return type; | |
743 } | |
744 | |
745 // MS RTF Format | |
746 // static | |
747 const Clipboard::FormatType& Clipboard::GetRtfFormatType() { | |
748 CR_DEFINE_STATIC_LOCAL( | |
749 FormatType, type, (::RegisterClipboardFormat(L"Rich Text Format"))); | |
750 return type; | |
751 } | |
752 | |
753 // static | |
754 const Clipboard::FormatType& Clipboard::GetBitmapFormatType() { | |
755 CR_DEFINE_STATIC_LOCAL(FormatType, type, (CF_BITMAP)); | |
756 return type; | |
757 } | |
758 | |
759 // Firefox text/html | |
760 // static | |
761 const Clipboard::FormatType& Clipboard::GetTextHtmlFormatType() { | |
762 CR_DEFINE_STATIC_LOCAL( | |
763 FormatType, type, (::RegisterClipboardFormat(L"text/html"))); | |
764 return type; | |
765 } | |
766 | |
767 // static | |
768 const Clipboard::FormatType& Clipboard::GetCFHDropFormatType() { | |
769 CR_DEFINE_STATIC_LOCAL(FormatType, type, (CF_HDROP)); | |
770 return type; | |
771 } | |
772 | |
773 // static | |
774 const Clipboard::FormatType& Clipboard::GetFileDescriptorFormatType() { | |
775 CR_DEFINE_STATIC_LOCAL( | |
776 FormatType, type, (::RegisterClipboardFormat(CFSTR_FILEDESCRIPTOR))); | |
777 return type; | |
778 } | |
779 | |
780 // static | |
781 const Clipboard::FormatType& Clipboard::GetFileContentZeroFormatType() { | |
782 CR_DEFINE_STATIC_LOCAL( | |
783 FormatType, type, (::RegisterClipboardFormat(CFSTR_FILECONTENTS), 0)); | |
784 return type; | |
785 } | |
786 | |
787 // static | |
788 const Clipboard::FormatType& Clipboard::GetIDListFormatType() { | |
789 CR_DEFINE_STATIC_LOCAL( | |
790 FormatType, type, (::RegisterClipboardFormat(CFSTR_SHELLIDLIST))); | |
791 return type; | |
792 } | |
793 | |
794 // static | |
795 const Clipboard::FormatType& Clipboard::GetWebKitSmartPasteFormatType() { | |
796 CR_DEFINE_STATIC_LOCAL( | |
797 FormatType, | |
798 type, | |
799 (::RegisterClipboardFormat(L"WebKit Smart Paste Format"))); | |
800 return type; | |
801 } | |
802 | |
803 // static | |
804 const Clipboard::FormatType& Clipboard::GetWebCustomDataFormatType() { | |
805 // TODO(dcheng): This name is temporary. See http://crbug.com/106449. | |
806 CR_DEFINE_STATIC_LOCAL( | |
807 FormatType, | |
808 type, | |
809 (::RegisterClipboardFormat(L"Chromium Web Custom MIME Data Format"))); | |
810 return type; | |
811 } | |
812 | |
813 // static | |
814 const Clipboard::FormatType& Clipboard::GetPepperCustomDataFormatType() { | |
815 CR_DEFINE_STATIC_LOCAL( | |
816 FormatType, | |
817 type, | |
818 (::RegisterClipboardFormat(L"Chromium Pepper MIME Data Format"))); | |
819 return type; | |
820 } | |
821 | |
822 // static | |
823 void Clipboard::FreeData(unsigned int format, HANDLE data) { | 826 void Clipboard::FreeData(unsigned int format, HANDLE data) { |
824 if (format == CF_BITMAP) | 827 if (format == CF_BITMAP) |
825 ::DeleteObject(static_cast<HBITMAP>(data)); | 828 ::DeleteObject(static_cast<HBITMAP>(data)); |
826 else | 829 else |
827 ::GlobalFree(data); | 830 ::GlobalFree(data); |
828 } | 831 } |
829 | 832 |
830 HWND Clipboard::GetClipboardWindow() const { | 833 HWND Clipboard::GetClipboardWindow() const { |
831 if (!clipboard_owner_) | 834 if (!clipboard_owner_) |
832 return NULL; | 835 return NULL; |
833 | 836 |
834 if (clipboard_owner_->hwnd() == NULL) | 837 if (clipboard_owner_->hwnd() == NULL) |
835 clipboard_owner_->Create(base::Bind(&ClipboardOwnerWndProc)); | 838 clipboard_owner_->Create(base::Bind(&ClipboardOwnerWndProc)); |
836 | 839 |
837 return clipboard_owner_->hwnd(); | 840 return clipboard_owner_->hwnd(); |
838 } | 841 } |
839 | 842 |
840 } // namespace ui | 843 } // namespace ui |
OLD | NEW |