OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #include "base/clipboard.h" | 5 #include "base/clipboard.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 } | 25 } |
26 | 26 |
27 } // namespace | 27 } // namespace |
28 | 28 |
29 Clipboard::Clipboard() { | 29 Clipboard::Clipboard() { |
30 } | 30 } |
31 | 31 |
32 Clipboard::~Clipboard() { | 32 Clipboard::~Clipboard() { |
33 } | 33 } |
34 | 34 |
35 void Clipboard::Clear() { | 35 void Clipboard::WriteObjects(const ObjectMap& objects) { |
36 NSPasteboard* pb = GetPasteboard(); | 36 NSPasteboard* pb = GetPasteboard(); |
37 [pb declareTypes:[NSArray array] owner:nil]; | 37 [pb declareTypes:[NSArray array] owner:nil]; |
| 38 |
| 39 for (ObjectMap::const_iterator iter = objects.begin(); |
| 40 iter != objects.end(); ++iter) { |
| 41 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); |
| 42 } |
| 43 |
38 } | 44 } |
39 | 45 |
40 void Clipboard::WriteText(const std::wstring& text) { | 46 void Clipboard::WriteText(const char* text_data, size_t text_len) { |
| 47 std::string text_str(text_data, text_len); |
| 48 NSString *text = base::SysUTF8ToNSString(text_str); |
41 NSPasteboard* pb = GetPasteboard(); | 49 NSPasteboard* pb = GetPasteboard(); |
42 [pb addTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; | 50 [pb addTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; |
43 [pb setString:base::SysWideToNSString(text) forType:NSStringPboardType]; | 51 [pb setString:text forType:NSStringPboardType]; |
44 } | 52 } |
45 | 53 |
46 void Clipboard::WriteHTML(const std::wstring& markup, | 54 void Clipboard::WriteHTML(const char* markup_data, |
47 const std::string& src_url) { | 55 size_t markup_len, |
48 // TODO(avi): src_url? | 56 const char* url_data, |
| 57 size_t url_len) { |
| 58 std::string html_fragment_str(markup_data, markup_len); |
| 59 NSString *html_fragment = base::SysUTF8ToNSString(html_fragment_str); |
| 60 |
| 61 // TODO(avi): url_data? |
49 NSPasteboard* pb = GetPasteboard(); | 62 NSPasteboard* pb = GetPasteboard(); |
50 [pb addTypes:[NSArray arrayWithObject:NSHTMLPboardType] owner:nil]; | 63 [pb addTypes:[NSArray arrayWithObject:NSHTMLPboardType] owner:nil]; |
51 [pb setString:base::SysWideToNSString(markup) forType:NSHTMLPboardType]; | 64 [pb setString:html_fragment forType:NSHTMLPboardType]; |
52 } | 65 } |
53 | 66 |
54 void Clipboard::WriteBookmark(const std::wstring& title, | 67 void Clipboard::WriteBookmark(const char* title_data, |
55 const std::string& url) { | 68 size_t title_len, |
56 WriteHyperlink(title, url); | 69 const char* url_data, |
| 70 size_t url_len) { |
| 71 WriteHyperlink(title_data, title_len, url_data, url_len); |
57 } | 72 } |
58 | 73 |
59 void Clipboard::WriteHyperlink(const std::wstring& title, | 74 void Clipboard::WriteHyperlink(const char* title_data, |
60 const std::string& url) { | 75 size_t title_len, |
| 76 const char* url_data, |
| 77 size_t url_len) { |
| 78 std::string title_str(title_data, title_len); |
| 79 NSString *title = base::SysUTF8ToNSString(title_str); |
| 80 std::string url_str(url_data, url_len); |
| 81 NSString *url = base::SysUTF8ToNSString(url_str); |
| 82 |
61 // TODO(playmobil): In the Windows version of this function, an HTML | 83 // TODO(playmobil): In the Windows version of this function, an HTML |
62 // representation of the bookmark is also added to the clipboard, to support | 84 // representation of the bookmark is also added to the clipboard, to support |
63 // drag and drop of web shortcuts. I don't think we need to do this on the | 85 // drag and drop of web shortcuts. I don't think we need to do this on the |
64 // Mac, but we should double check later on. | 86 // Mac, but we should double check later on. |
65 NSURL* nsurl = [NSURL URLWithString: | 87 NSURL* nsurl = [NSURL URLWithString:url]; |
66 [NSString stringWithUTF8String:url.c_str()]]; | |
67 NSString* nstitle = base::SysWideToNSString(title); | |
68 | 88 |
69 NSPasteboard* pb = GetPasteboard(); | 89 NSPasteboard* pb = GetPasteboard(); |
70 // passing UTIs into the pasteboard methods is valid >= 10.5 | 90 // passing UTIs into the pasteboard methods is valid >= 10.5 |
71 [pb addTypes:[NSArray arrayWithObjects:NSURLPboardType, | 91 [pb addTypes:[NSArray arrayWithObjects:NSURLPboardType, |
72 kUTTypeURLName, | 92 kUTTypeURLName, |
73 nil] | 93 nil] |
74 owner:nil]; | 94 owner:nil]; |
75 [nsurl writeToPasteboard:pb]; | 95 [nsurl writeToPasteboard:pb]; |
76 [pb setString:nstitle forType:kUTTypeURLName]; | 96 [pb setString:title forType:kUTTypeURLName]; |
77 } | 97 } |
78 | 98 |
79 void Clipboard::WriteFile(const std::wstring& file) { | 99 void Clipboard::WriteFiles(const char* file_data, size_t file_len) { |
80 std::vector<std::wstring> files; | 100 NSMutableArray* fileList = [NSMutableArray arrayWithCapacity:1]; |
81 files.push_back(file); | |
82 WriteFiles(files); | |
83 } | |
84 | 101 |
85 void Clipboard::WriteFiles(const std::vector<std::wstring>& files) { | 102 // Offset of current filename from start of file_data array. |
86 NSMutableArray* fileList = [NSMutableArray arrayWithCapacity:files.size()]; | 103 size_t current_filename_offset = 0; |
87 for (size_t i = 0; i < files.size(); ++i) { | 104 |
88 [fileList addObject:base::SysWideToNSString(files[i])]; | 105 // file_data is double null terminated (see table at top of clipboard.h). |
| 106 // So this loop can ignore the second null terminator, thus file_len - 1. |
| 107 // TODO(playmobil): If we need a loop like this on other platforms then split |
| 108 // this out into a common function that outputs a std::vector<const char*>. |
| 109 for (size_t i = 0; i < file_len - 1; ++i) { |
| 110 if (file_data[i] == '\0') { |
| 111 const char* filename = &file_data[current_filename_offset]; |
| 112 [fileList addObject:[NSString stringWithUTF8String:filename]]; |
| 113 |
| 114 current_filename_offset = i + 1; |
| 115 continue; |
| 116 } |
89 } | 117 } |
90 | 118 |
91 NSPasteboard* pb = GetPasteboard(); | 119 NSPasteboard* pb = GetPasteboard(); |
92 [pb addTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:nil]; | 120 [pb addTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:nil]; |
93 [pb setPropertyList:fileList forType:NSFilenamesPboardType]; | 121 [pb setPropertyList:fileList forType:NSFilenamesPboardType]; |
94 } | 122 } |
95 | 123 |
96 bool Clipboard::IsFormatAvailable(NSString* format) const { | 124 bool Clipboard::IsFormatAvailable(NSString* format) const { |
97 NSPasteboard* pb = GetPasteboard(); | 125 NSPasteboard* pb = GetPasteboard(); |
98 NSArray* types = [pb types]; | 126 NSArray* types = [pb types]; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 | 243 |
216 // static | 244 // static |
217 Clipboard::FormatType Clipboard::GetFilenameWFormatType() { | 245 Clipboard::FormatType Clipboard::GetFilenameWFormatType() { |
218 return NSFilenamesPboardType; | 246 return NSFilenamesPboardType; |
219 } | 247 } |
220 | 248 |
221 // static | 249 // static |
222 Clipboard::FormatType Clipboard::GetHtmlFormatType() { | 250 Clipboard::FormatType Clipboard::GetHtmlFormatType() { |
223 return NSHTMLPboardType; | 251 return NSHTMLPboardType; |
224 } | 252 } |
OLD | NEW |