From 27f3d73d16b6b041e06a39451d2cda51562ce1c6 Mon Sep 17 00:00:00 2001 From: "estade@chromium.org" Date: Tue, 21 Jul 2009 01:21:54 +0000 Subject: Support pasting text from a clipboard that has no published targets. BUG=16887 TEST=paste from intellij into chrome render view Review URL: http://codereview.chromium.org/159107 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21142 0039d316-1c4b-4281-b951-d872f2087c98 --- base/clipboard.h | 2 +- base/clipboard_linux.cc | 28 +++++++++++++++++++++----- tools/gtk_clipboard_dump/gtk_clipboard_dump.cc | 21 +++++++++++++++++-- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/base/clipboard.h b/base/clipboard.h index a404418..a0a7827 100644 --- a/base/clipboard.h +++ b/base/clipboard.h @@ -40,7 +40,7 @@ class Clipboard { CBF_FILES, CBF_WEBKIT, CBF_BITMAP, - CBF_SMBITMAP // bitmap from shared memory + CBF_SMBITMAP // bitmap from shared memory }; // ObjectMap is a map from ObjectType to associated data. diff --git a/base/clipboard_linux.cc b/base/clipboard_linux.cc index 10c0deb..eb5012c 100644 --- a/base/clipboard_linux.cc +++ b/base/clipboard_linux.cc @@ -214,11 +214,14 @@ void Clipboard::WriteFiles(const char* file_data, size_t file_len) { // We do not use gtk_clipboard_wait_is_target_available because of // a bug with the gtk clipboard. It caches the available targets // and does not always refresh the cache when it is appropriate. -// TODO(estade): When gnome bug 557315 is resolved, change this function -// to use gtk_clipboard_wait_is_target_available. Also, catch requests -// for plain text and change them to gtk_clipboard_wait_is_text_available -// (which checks for several standard text targets). bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format) const { + bool format_is_plain_text = GetPlainTextFormatType() == format; + if (format_is_plain_text) { + // This tries a number of common text targets. + if (gtk_clipboard_wait_is_text_available(clipboard_)) + return true; + } + bool retval = false; GdkAtom* targets = NULL; GtkSelectionData* data = @@ -231,6 +234,21 @@ bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format) const { int num = 0; gtk_selection_data_get_targets(data, &targets, &num); + // Some programs post data to the clipboard without any targets. If this is + // the case we attempt to make sense of the contents as text. This is pretty + // unfortunate since it means we have to actually copy the data to see if it + // is available, but at least this path shouldn't be hit for conforming + // programs. + if (num <= 0) { + if (format_is_plain_text) { + gchar* text = gtk_clipboard_wait_for_text(clipboard_); + if (text) { + g_free(text); + retval = true; + } + } + } + GdkAtom format_atom = StringToGdkAtom(format); for (int i = 0; i < num; i++) { @@ -240,8 +258,8 @@ bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format) const { } } - gtk_selection_data_free(data); g_free(targets); + gtk_selection_data_free(data); return retval; } diff --git a/tools/gtk_clipboard_dump/gtk_clipboard_dump.cc b/tools/gtk_clipboard_dump/gtk_clipboard_dump.cc index f0f6016..a064792 100644 --- a/tools/gtk_clipboard_dump/gtk_clipboard_dump.cc +++ b/tools/gtk_clipboard_dump/gtk_clipboard_dump.cc @@ -11,9 +11,17 @@ namespace { void PrintClipboardContents(GtkClipboard* clip) { GdkAtom* targets; int num_targets = 0; - gtk_clipboard_wait_for_targets(clip, &targets, &num_targets); - printf("Available targets:\n---------------\n"); + // This call is bugged, the cache it checks is often stale; see + // . + // gtk_clipboard_wait_for_targets(clip, &targets, &num_targets); + + GtkSelectionData* target_data = + gtk_clipboard_wait_for_contents(clip, + gdk_atom_intern("TARGETS", false)); + gtk_selection_data_get_targets(target_data, &targets, &num_targets); + + printf("%d available targets:\n---------------\n", num_targets); for (int i = 0; i < num_targets; i++) { printf(" [format: %s", gdk_atom_name(targets[i])); @@ -41,6 +49,15 @@ void PrintClipboardContents(GtkClipboard* clip) { } printf("\n\n"); } + + if (num_targets <= 0) { + printf("No targets advertised. Text is: "); + gchar* text = gtk_clipboard_wait_for_text(clip); + printf("%s\n", text ? text : "NULL"); + g_free(text); + } + + g_free(targets); } } -- cgit v1.1