| 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 #include "base/string_util.h" | 6 #include "base/string_util.h" |
| 7 | 7 |
| 8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 static const char* kMimeHtml = "text/html"; | 16 static const char* kMimeHtml = "text/html"; |
| 17 static const char* kMimeText = "text/plain"; | 17 static const char* kMimeText = "text/plain"; |
| 18 static const char* kMimeWebkitSmartPaste = "chrome-internal/webkit-paste"; | 18 static const char* kMimeWebkitSmartPaste = "chrome-internal/webkit-paste"; |
| 19 | 19 |
| 20 std::string GdkAtomToString(const GdkAtom& atom) { |
| 21 gchar* name = gdk_atom_name(atom); |
| 22 std::string rv(name); |
| 23 g_free(name); |
| 24 return rv; |
| 25 } |
| 26 |
| 27 GdkAtom StringToGdkAtom(const std::string& str) { |
| 28 return gdk_atom_intern(str.c_str(), false); |
| 29 } |
| 30 |
| 20 // GtkClipboardGetFunc callback. | 31 // GtkClipboardGetFunc callback. |
| 21 // GTK will call this when an application wants data we copied to the clipboard. | 32 // GTK will call this when an application wants data we copied to the clipboard. |
| 22 void GetData(GtkClipboard* clipboard, | 33 void GetData(GtkClipboard* clipboard, |
| 23 GtkSelectionData* selection_data, | 34 GtkSelectionData* selection_data, |
| 24 guint info, | 35 guint info, |
| 25 gpointer user_data) { | 36 gpointer user_data) { |
| 26 Clipboard::TargetMap* data_map = | 37 Clipboard::TargetMap* data_map = |
| 27 reinterpret_cast<Clipboard::TargetMap*>(user_data); | 38 reinterpret_cast<Clipboard::TargetMap*>(user_data); |
| 28 | 39 |
| 29 Clipboard::TargetMap::iterator iter = | 40 Clipboard::TargetMap::iterator iter = |
| 30 data_map->find(std::string(gdk_atom_name(selection_data->target))); | 41 data_map->find(GdkAtomToString(selection_data->target)); |
| 31 | 42 |
| 32 if (iter == data_map->end()) | 43 if (iter == data_map->end()) |
| 33 return; | 44 return; |
| 34 | 45 |
| 35 gtk_selection_data_set(selection_data, selection_data->target, 8, | 46 gtk_selection_data_set(selection_data, selection_data->target, 8, |
| 36 reinterpret_cast<guchar*>(iter->second.first), | 47 reinterpret_cast<guchar*>(iter->second.first), |
| 37 iter->second.second); | 48 iter->second.second); |
| 38 } | 49 } |
| 39 | 50 |
| 40 // GtkClipboardClearFunc callback. | 51 // GtkClipboardClearFunc callback. |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 NOTIMPLEMENTED(); | 178 NOTIMPLEMENTED(); |
| 168 } | 179 } |
| 169 | 180 |
| 170 // We do not use gtk_clipboard_wait_is_target_available because of | 181 // We do not use gtk_clipboard_wait_is_target_available because of |
| 171 // a bug with the gtk clipboard. It caches the available targets | 182 // a bug with the gtk clipboard. It caches the available targets |
| 172 // and does not always refresh the cache when it is appropriate. | 183 // and does not always refresh the cache when it is appropriate. |
| 173 // TODO(estade): When gnome bug 557315 is resolved, change this function | 184 // TODO(estade): When gnome bug 557315 is resolved, change this function |
| 174 // to use gtk_clipboard_wait_is_target_available. Also, catch requests | 185 // to use gtk_clipboard_wait_is_target_available. Also, catch requests |
| 175 // for plain text and change them to gtk_clipboard_wait_is_text_available | 186 // for plain text and change them to gtk_clipboard_wait_is_text_available |
| 176 // (which checks for several standard text targets). | 187 // (which checks for several standard text targets). |
| 177 bool Clipboard::IsFormatAvailable(Clipboard::FormatType format) const { | 188 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format) const { |
| 178 bool retval = false; | 189 bool retval = false; |
| 179 GdkAtom* targets = NULL; | 190 GdkAtom* targets = NULL; |
| 180 GtkSelectionData* data = | 191 GtkSelectionData* data = |
| 181 gtk_clipboard_wait_for_contents(clipboard_, | 192 gtk_clipboard_wait_for_contents(clipboard_, |
| 182 gdk_atom_intern("TARGETS", false)); | 193 gdk_atom_intern("TARGETS", false)); |
| 183 | 194 |
| 184 if (!data) | 195 if (!data) |
| 185 return false; | 196 return false; |
| 186 | 197 |
| 187 int num = 0; | 198 int num = 0; |
| 188 gtk_selection_data_get_targets(data, &targets, &num); | 199 gtk_selection_data_get_targets(data, &targets, &num); |
| 189 | 200 |
| 201 GdkAtom format_atom = StringToGdkAtom(format); |
| 202 |
| 190 for (int i = 0; i < num; i++) { | 203 for (int i = 0; i < num; i++) { |
| 191 if (targets[i] == format) { | 204 if (targets[i] == format_atom) { |
| 192 retval = true; | 205 retval = true; |
| 193 break; | 206 break; |
| 194 } | 207 } |
| 195 } | 208 } |
| 196 | 209 |
| 197 gtk_selection_data_free(data); | 210 gtk_selection_data_free(data); |
| 198 g_free(targets); | 211 g_free(targets); |
| 199 | 212 |
| 200 return retval; | 213 return retval; |
| 201 } | 214 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 225 | 238 |
| 226 void Clipboard::ReadFile(FilePath* file) const { | 239 void Clipboard::ReadFile(FilePath* file) const { |
| 227 *file = FilePath(); | 240 *file = FilePath(); |
| 228 } | 241 } |
| 229 | 242 |
| 230 // TODO(estade): handle different charsets. | 243 // TODO(estade): handle different charsets. |
| 231 void Clipboard::ReadHTML(string16* markup, std::string* src_url) const { | 244 void Clipboard::ReadHTML(string16* markup, std::string* src_url) const { |
| 232 markup->clear(); | 245 markup->clear(); |
| 233 | 246 |
| 234 GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard_, | 247 GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard_, |
| 235 GetHtmlFormatType()); | 248 StringToGdkAtom(GetHtmlFormatType())); |
| 236 | 249 |
| 237 if (!data) | 250 if (!data) |
| 238 return; | 251 return; |
| 239 | 252 |
| 240 UTF8ToUTF16(reinterpret_cast<char*>(data->data), | 253 UTF8ToUTF16(reinterpret_cast<char*>(data->data), |
| 241 strlen(reinterpret_cast<char*>(data->data)), | 254 strlen(reinterpret_cast<char*>(data->data)), |
| 242 markup); | 255 markup); |
| 243 gtk_selection_data_free(data); | 256 gtk_selection_data_free(data); |
| 244 } | 257 } |
| 245 | 258 |
| 246 // static | 259 // static |
| 247 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { | 260 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { |
| 248 return GDK_TARGET_STRING; | 261 return GdkAtomToString(GDK_TARGET_STRING); |
| 249 } | 262 } |
| 250 | 263 |
| 251 // static | 264 // static |
| 252 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { | 265 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { |
| 253 return GetPlainTextFormatType(); | 266 return GetPlainTextFormatType(); |
| 254 } | 267 } |
| 255 | 268 |
| 256 // static | 269 // static |
| 257 Clipboard::FormatType Clipboard::GetHtmlFormatType() { | 270 Clipboard::FormatType Clipboard::GetHtmlFormatType() { |
| 258 return gdk_atom_intern(kMimeHtml, false); | 271 return std::string(kMimeHtml); |
| 259 } | 272 } |
| 260 | 273 |
| 261 // static | 274 // static |
| 262 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { | 275 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { |
| 263 return gdk_atom_intern(kMimeWebkitSmartPaste, false); | 276 return std::string(kMimeWebkitSmartPaste); |
| 264 } | 277 } |
| 265 | 278 |
| 266 // Insert the key/value pair in the clipboard_data structure. If | 279 // Insert the key/value pair in the clipboard_data structure. If |
| 267 // the mapping already exists, it frees the associated data. Don't worry | 280 // the mapping already exists, it frees the associated data. Don't worry |
| 268 // about double freeing because if the same key is inserted into the | 281 // about double freeing because if the same key is inserted into the |
| 269 // map twice, it must have come from different Write* functions and the | 282 // map twice, it must have come from different Write* functions and the |
| 270 // data pointer cannot be the same. | 283 // data pointer cannot be the same. |
| 271 void Clipboard::InsertMapping(const char* key, | 284 void Clipboard::InsertMapping(const char* key, |
| 272 char* data, | 285 char* data, |
| 273 size_t data_len) { | 286 size_t data_len) { |
| 274 TargetMap::iterator iter = clipboard_data_->find(key); | 287 TargetMap::iterator iter = clipboard_data_->find(key); |
| 275 | 288 |
| 276 if (iter != clipboard_data_->end()) | 289 if (iter != clipboard_data_->end()) |
| 277 delete[] iter->second.first; | 290 delete[] iter->second.first; |
| 278 | 291 |
| 279 (*clipboard_data_)[key] = std::make_pair(data, data_len); | 292 (*clipboard_data_)[key] = std::make_pair(data, data_len); |
| 280 } | 293 } |
| OLD | NEW |