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) <torisugari@gmail.com>,
r=bzbarsky, sr=jst
This commit is contained in:
bzbarsky%mit.edu 2005-01-12 04:11:26 +00:00
Родитель 1f30c3397a
Коммит 2fc5bf37c3
3 изменённых файлов: 65 добавлений и 37 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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<nsIImage> image = nsContentUtils::GetImageFromContent(imageElement);
if (!image) return NS_ERROR_FAILURE;
// create a transferable for putting data on the Clipboard
nsCOMPtr<nsITransferable> trans(do_CreateInstance(kCTransferableCID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
// Get the Clipboard
// get the location from the element
nsCOMPtr<nsIURI> 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<nsISupportsString>
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<nsIImage> image =
nsContentUtils::GetImageFromContent(aImageElement);
NS_ENSURE_TRUE(image, NS_ERROR_FAILURE);
nsCOMPtr<nsISupportsInterfacePointer>
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<nsIClipboard> 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<nsITransferable> trans = do_CreateInstance(kCTransferableCID, &rv);
if (NS_FAILED(rv)) return rv;
if (!trans) return NS_ERROR_FAILURE;
nsCOMPtr<nsISupportsInterfacePointer> 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);
}

Просмотреть файл

@ -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<nsIURI> uri;
node->GetCurrentURI(getter_AddRefs(uri));
if (!uri)
return NS_ERROR_FAILURE;
nsCAutoString spec;
uri->GetSpec(spec);
NS_ConvertUTF8toUTF16 locationText(spec);
nsresult rv;
nsCOMPtr<nsIClipboardHelper> 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)