From 18540085b3ea36bb09c05f5a65a5b52b74e99c10 Mon Sep 17 00:00:00 2001 From: "bzbarsky%mit.edu" Date: Wed, 12 Jan 2005 04:11:26 +0000 Subject: [PATCH] Make DocumentViewerImpl::CopyImageContents also put the image URI on the clipboard in the text flavor; a big step toward having a single "copy image" command that puts it on the clipboard in all relevant flavors in a meaningful way. Bug 135300, patch by O. Atsushi (Torisugari) , r=bzbarsky, sr=jst git-svn-id: svn://10.0.0.236/trunk@167578 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/content/base/public/nsCopySupport.h | 3 +- mozilla/content/base/src/nsCopySupport.cpp | 79 ++++++++++++++++----- mozilla/layout/base/nsDocumentViewer.cpp | 20 +----- 3 files changed, 65 insertions(+), 37 deletions(-) diff --git a/mozilla/content/base/public/nsCopySupport.h b/mozilla/content/base/public/nsCopySupport.h index c0cb76a2f0b..c1b92febff5 100644 --- a/mozilla/content/base/public/nsCopySupport.h +++ b/mozilla/content/base/public/nsCopySupport.h @@ -61,7 +61,8 @@ class nsCopySupport // doc. static nsresult GetContents(const nsACString& aMimeType, PRUint32 aFlags, nsISelection *aSel, nsIDocument *aDoc, nsAString& outdata); - static nsresult ImageCopy(nsIImageLoadingContent* imageElement, PRInt16 aClipboardID); + static nsresult ImageCopy(nsIImageLoadingContent* aImageElement, + PRBool aCopyImageData); }; #endif diff --git a/mozilla/content/base/src/nsCopySupport.cpp b/mozilla/content/base/src/nsCopySupport.cpp index 5dd273c8202..431cb300dae 100644 --- a/mozilla/content/base/src/nsCopySupport.cpp +++ b/mozilla/content/base/src/nsCopySupport.cpp @@ -425,30 +425,73 @@ nsCopySupport::GetContents(const nsACString& aMimeType, PRUint32 aFlags, nsISele nsresult -nsCopySupport::ImageCopy(nsIImageLoadingContent* imageElement, PRInt16 aClipboardID) +nsCopySupport::ImageCopy(nsIImageLoadingContent* aImageElement, + PRBool aCopyImageData) { nsresult rv; - nsCOMPtr image = nsContentUtils::GetImageFromContent(imageElement); - if (!image) return NS_ERROR_FAILURE; + // create a transferable for putting data on the Clipboard + nsCOMPtr trans(do_CreateInstance(kCTransferableCID, &rv)); + NS_ENSURE_SUCCESS(rv, rv); - // Get the Clipboard + // get the location from the element + nsCOMPtr uri; + rv = aImageElement->GetCurrentURI(getter_AddRefs(uri)); + NS_ENSURE_SUCCESS(rv, rv); + NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE); + + nsCAutoString locationUTF8; + rv = uri->GetSpec(locationUTF8); + NS_ENSURE_SUCCESS(rv, rv); + + // convert the string to nsISupportsString + NS_ConvertUTF8toUTF16 locationUTF16(locationUTF8); + + nsCOMPtr + location(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + rv = location->SetData(locationUTF16); + NS_ENSURE_SUCCESS(rv, rv); + + // append the string to the transferable + rv = trans->SetTransferData(kUnicodeMime, location, + (locationUTF16.Length() * 2)); + NS_ENSURE_SUCCESS(rv, rv); + + if (aCopyImageData) { + // get the image data from the element + nsCOMPtr image = + nsContentUtils::GetImageFromContent(aImageElement); + NS_ENSURE_TRUE(image, NS_ERROR_FAILURE); + + nsCOMPtr + imgPtr(do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + rv = imgPtr->SetData(image); + NS_ENSURE_SUCCESS(rv, rv); + + // copy the image data onto the transferable + rv = trans->SetTransferData(kNativeImageMime, imgPtr, + sizeof(nsISupports*)); + NS_ENSURE_SUCCESS(rv, rv); + } + + // get clipboard nsCOMPtr clipboard(do_GetService(kCClipboardCID, &rv)); - if (NS_FAILED(rv)) return rv; - if (!clipboard) return NS_ERROR_FAILURE; + NS_ENSURE_SUCCESS(rv, rv); - // Create a transferable for putting data on the Clipboard - nsCOMPtr trans = do_CreateInstance(kCTransferableCID, &rv); - if (NS_FAILED(rv)) return rv; - if (!trans) return NS_ERROR_FAILURE; - - nsCOMPtr ptrPrimitive(do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv)); - if (NS_FAILED(rv)) return rv; - if (!ptrPrimitive) return NS_ERROR_FAILURE; - ptrPrimitive->SetData(image); - - trans->SetTransferData(kNativeImageMime, ptrPrimitive, sizeof(nsISupports*)); + // check whether the system supports the selection clipboard or not. + PRBool selectionSupported; + rv = clipboard->SupportsSelectionClipboard(&selectionSupported); + NS_ENSURE_SUCCESS(rv, rv); // put the transferable on the clipboard - return clipboard->SetData(trans, nsnull, aClipboardID); + if (selectionSupported) { + rv = clipboard->SetData(trans, nsnull, nsIClipboard::kSelectionClipboard); + NS_ENSURE_SUCCESS(rv, rv); + } + + return clipboard->SetData(trans, nsnull, nsIClipboard::kGlobalClipboard); } diff --git a/mozilla/layout/base/nsDocumentViewer.cpp b/mozilla/layout/base/nsDocumentViewer.cpp index cfc8ba4df2b..49f2687da61 100644 --- a/mozilla/layout/base/nsDocumentViewer.cpp +++ b/mozilla/layout/base/nsDocumentViewer.cpp @@ -2052,23 +2052,7 @@ NS_IMETHODIMP DocumentViewerImpl::CopyImageLocation() // make noise if we're not in an image NS_ENSURE_TRUE(node, NS_ERROR_FAILURE); - nsCOMPtr uri; - node->GetCurrentURI(getter_AddRefs(uri)); - if (!uri) - return NS_ERROR_FAILURE; - - nsCAutoString spec; - uri->GetSpec(spec); - - NS_ConvertUTF8toUTF16 locationText(spec); - - nsresult rv; - nsCOMPtr clipboard = - do_GetService("@mozilla.org/widget/clipboardhelper;1", &rv); - NS_ENSURE_SUCCESS(rv, rv); - - // copy the href onto the clipboard - return clipboard->CopyString(locationText); + return nsCopySupport::ImageCopy(node, PR_FALSE); } NS_IMETHODIMP DocumentViewerImpl::CopyImageContents() @@ -2079,7 +2063,7 @@ NS_IMETHODIMP DocumentViewerImpl::CopyImageContents() // make noise if we're not in an image NS_ENSURE_TRUE(node, NS_ERROR_FAILURE); - return nsCopySupport::ImageCopy(node, nsIClipboard::kGlobalClipboard); + return nsCopySupport::ImageCopy(node, PR_TRUE); } NS_IMETHODIMP DocumentViewerImpl::GetCopyable(PRBool *aCopyable)