2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 14:30:37 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2001-02-15 13:22:26 +00:00
|
|
|
*
|
2004-04-18 14:30:37 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2001-02-15 13:22:26 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
2001-02-15 13:22:26 +00:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 14:30:37 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-28 20:14:13 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2001-02-15 13:22:26 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Contributor(s):
|
2003-04-17 13:16:36 +00:00
|
|
|
* Kathleen Brade <brade@netscape.com>
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 14:30:37 +00:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-28 20:14:13 +00:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 14:30:37 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-28 20:14:13 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 14:30:37 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-02-15 13:22:26 +00:00
|
|
|
|
|
|
|
#include "nsCopySupport.h"
|
|
|
|
#include "nsIDocumentEncoder.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIClipboard.h"
|
2001-07-16 02:40:48 +00:00
|
|
|
#include "nsISelection.h"
|
2001-02-15 13:22:26 +00:00
|
|
|
#include "nsWidgetsCID.h"
|
2002-09-03 23:36:13 +00:00
|
|
|
#include "nsXPCOM.h"
|
2001-02-15 13:22:26 +00:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2001-07-25 05:16:32 +00:00
|
|
|
#include "nsIDOMRange.h"
|
|
|
|
|
2003-04-17 13:16:36 +00:00
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIClipboardDragDropHooks.h"
|
|
|
|
#include "nsIClipboardDragDropHookList.h"
|
|
|
|
|
2001-03-21 01:16:22 +00:00
|
|
|
#include "nsIDocument.h"
|
2001-07-13 13:38:10 +00:00
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIHTMLDocument.h"
|
|
|
|
#include "nsHTMLAtoms.h"
|
2001-02-15 13:22:26 +00:00
|
|
|
|
2003-04-18 20:20:04 +00:00
|
|
|
// image copy stuff
|
|
|
|
#include "nsIImageLoadingContent.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsIImage.h"
|
2004-07-30 06:04:57 +00:00
|
|
|
#include "nsContentUtils.h"
|
2003-04-18 20:20:04 +00:00
|
|
|
|
2001-02-15 13:22:26 +00:00
|
|
|
static NS_DEFINE_CID(kCClipboardCID, NS_CLIPBOARD_CID);
|
|
|
|
static NS_DEFINE_CID(kCTransferableCID, NS_TRANSFERABLE_CID);
|
|
|
|
static NS_DEFINE_CID(kHTMLConverterCID, NS_HTMLFORMATCONVERTER_CID);
|
|
|
|
|
|
|
|
// private clipboard data flavors for html copy, used by editor when pasting
|
|
|
|
#define kHTMLContext "text/_moz_htmlcontext"
|
|
|
|
#define kHTMLInfo "text/_moz_htmlinfo"
|
|
|
|
|
|
|
|
|
|
|
|
nsresult nsCopySupport::HTMLCopy(nsISelection *aSel, nsIDocument *aDoc, PRInt16 aClipboardID)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocumentEncoder> docEncoder;
|
|
|
|
|
|
|
|
docEncoder = do_CreateInstance(NS_HTMLCOPY_ENCODER_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(docEncoder, NS_ERROR_FAILURE);
|
|
|
|
|
2001-07-13 13:38:10 +00:00
|
|
|
PRBool bIsPlainTextContext = PR_FALSE;
|
|
|
|
|
|
|
|
rv = IsPlainTextContext(aSel, aDoc, &bIsPlainTextContext);
|
2001-02-15 13:22:26 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-07-13 13:38:10 +00:00
|
|
|
|
|
|
|
PRBool bIsHTMLCopy = !bIsPlainTextContext;
|
|
|
|
PRUint32 flags = 0;
|
2001-02-15 13:22:26 +00:00
|
|
|
nsAutoString mimeType;
|
|
|
|
|
2001-07-13 13:38:10 +00:00
|
|
|
if (bIsHTMLCopy)
|
2004-06-17 00:13:25 +00:00
|
|
|
mimeType.AssignLiteral(kHTMLMime);
|
2001-07-13 13:38:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
flags |= nsIDocumentEncoder::OutputBodyOnly | nsIDocumentEncoder::OutputPreformatted;
|
2004-06-17 00:13:25 +00:00
|
|
|
mimeType.AssignLiteral(kUnicodeMime);
|
2001-07-13 13:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = docEncoder->Init(aDoc, mimeType, flags);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = docEncoder->SetSelection(aSel);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-02-15 13:22:26 +00:00
|
|
|
|
2001-07-13 13:38:10 +00:00
|
|
|
nsAutoString buffer, parents, info;
|
|
|
|
|
2001-02-15 13:22:26 +00:00
|
|
|
if (bIsHTMLCopy)
|
|
|
|
{
|
|
|
|
// encode the selection as html with contextual info
|
|
|
|
rv = docEncoder->EncodeToStringWithContext(buffer, parents, info);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// encode the selection
|
|
|
|
rv = docEncoder->EncodeToString(buffer);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the Clipboard
|
2001-07-25 07:54:28 +00:00
|
|
|
nsCOMPtr<nsIClipboard> clipboard(do_GetService(kCClipboardCID, &rv));
|
2001-02-15 13:22:26 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if ( clipboard )
|
|
|
|
{
|
|
|
|
// Create a transferable for putting data on the Clipboard
|
|
|
|
nsCOMPtr<nsITransferable> trans = do_CreateInstance(kCTransferableCID);
|
|
|
|
if ( trans )
|
|
|
|
{
|
|
|
|
if (bIsHTMLCopy)
|
|
|
|
{
|
|
|
|
// set up the data converter
|
|
|
|
nsCOMPtr<nsIFormatConverter> htmlConverter = do_CreateInstance(kHTMLConverterCID);
|
|
|
|
NS_ENSURE_TRUE(htmlConverter, NS_ERROR_FAILURE);
|
|
|
|
trans->SetConverter(htmlConverter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get wStrings to hold clip data
|
2002-08-06 00:53:19 +00:00
|
|
|
nsCOMPtr<nsISupportsString> dataWrapper, contextWrapper, infoWrapper;
|
|
|
|
dataWrapper = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
|
2001-02-15 13:22:26 +00:00
|
|
|
NS_ENSURE_TRUE(dataWrapper, NS_ERROR_FAILURE);
|
|
|
|
if (bIsHTMLCopy)
|
|
|
|
{
|
2002-08-06 00:53:19 +00:00
|
|
|
contextWrapper = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
|
2001-02-15 13:22:26 +00:00
|
|
|
NS_ENSURE_TRUE(contextWrapper, NS_ERROR_FAILURE);
|
2002-08-06 00:53:19 +00:00
|
|
|
infoWrapper = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
|
2001-02-15 13:22:26 +00:00
|
|
|
NS_ENSURE_TRUE(infoWrapper, NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// populate the strings
|
2002-04-16 19:16:33 +00:00
|
|
|
nsresult data_rv = NS_OK, context_rv = NS_OK, info_rv = NS_OK;
|
|
|
|
data_rv =
|
2002-08-26 21:20:34 +00:00
|
|
|
dataWrapper->SetData(buffer);
|
2001-02-15 13:22:26 +00:00
|
|
|
if (bIsHTMLCopy)
|
|
|
|
{
|
2002-04-16 19:16:33 +00:00
|
|
|
context_rv =
|
2002-08-26 21:20:34 +00:00
|
|
|
contextWrapper->SetData(parents);
|
2002-04-16 19:16:33 +00:00
|
|
|
info_rv =
|
2002-08-26 21:20:34 +00:00
|
|
|
infoWrapper->SetData(info);
|
2001-02-15 13:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// QI the data object an |nsISupports| so that when the transferable holds
|
|
|
|
// onto it, it will addref the correct interface.
|
|
|
|
nsCOMPtr<nsISupports> genericDataObj ( do_QueryInterface(dataWrapper) );
|
|
|
|
if (bIsHTMLCopy)
|
|
|
|
{
|
2002-04-16 19:16:33 +00:00
|
|
|
if (!buffer.IsEmpty() && NS_SUCCEEDED(data_rv))
|
2001-04-02 14:11:01 +00:00
|
|
|
{
|
|
|
|
// Add the html DataFlavor to the transferable
|
|
|
|
trans->AddDataFlavor(kHTMLMime);
|
|
|
|
trans->SetTransferData(kHTMLMime, genericDataObj, buffer.Length()*2);
|
|
|
|
}
|
2002-04-16 19:16:33 +00:00
|
|
|
if (!parents.IsEmpty() && NS_SUCCEEDED(context_rv))
|
2001-04-02 14:11:01 +00:00
|
|
|
{
|
|
|
|
// Add the htmlcontext DataFlavor to the transferable
|
|
|
|
trans->AddDataFlavor(kHTMLContext);
|
|
|
|
genericDataObj = do_QueryInterface(contextWrapper);
|
|
|
|
trans->SetTransferData(kHTMLContext, genericDataObj, parents.Length()*2);
|
|
|
|
}
|
2002-04-16 19:16:33 +00:00
|
|
|
if (!info.IsEmpty() && NS_SUCCEEDED(info_rv))
|
2001-04-02 14:11:01 +00:00
|
|
|
{
|
|
|
|
// Add the htmlinfo DataFlavor to the transferable
|
|
|
|
trans->AddDataFlavor(kHTMLInfo);
|
|
|
|
genericDataObj = do_QueryInterface(infoWrapper);
|
|
|
|
trans->SetTransferData(kHTMLInfo, genericDataObj, info.Length()*2);
|
|
|
|
}
|
2001-02-15 13:22:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-04-16 19:16:33 +00:00
|
|
|
if (!buffer.IsEmpty() && NS_SUCCEEDED(data_rv))
|
2001-04-02 14:11:01 +00:00
|
|
|
{
|
|
|
|
// Add the unicode DataFlavor to the transferable
|
|
|
|
trans->AddDataFlavor(kUnicodeMime);
|
|
|
|
trans->SetTransferData(kUnicodeMime, genericDataObj, buffer.Length()*2);
|
|
|
|
}
|
2001-02-15 13:22:26 +00:00
|
|
|
}
|
2003-04-17 13:16:36 +00:00
|
|
|
|
|
|
|
PRBool doPutOnClipboard = PR_TRUE;
|
|
|
|
DoHooks(aDoc, trans, &doPutOnClipboard);
|
|
|
|
|
2001-02-15 13:22:26 +00:00
|
|
|
// put the transferable on the clipboard
|
2003-04-17 13:16:36 +00:00
|
|
|
if (doPutOnClipboard)
|
|
|
|
clipboard->SetData(trans, nsnull, aClipboardID);
|
2001-02-15 13:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2001-07-13 13:38:10 +00:00
|
|
|
|
2003-04-17 13:16:36 +00:00
|
|
|
nsresult nsCopySupport::DoHooks(nsIDocument *aDoc, nsITransferable *aTrans,
|
|
|
|
PRBool *aDoPutOnClipboard)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aDoc);
|
|
|
|
|
|
|
|
*aDoPutOnClipboard = PR_TRUE;
|
|
|
|
|
2003-10-22 06:09:48 +00:00
|
|
|
nsCOMPtr<nsISupports> container = aDoc->GetContainer();
|
|
|
|
nsCOMPtr<nsIClipboardDragDropHookList> hookObj = do_GetInterface(container);
|
2003-04-17 13:16:36 +00:00
|
|
|
if (!hookObj) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
|
|
|
hookObj->GetHookEnumerator(getter_AddRefs(enumerator));
|
|
|
|
if (!enumerator) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// the logic here should follow the behavior specified in
|
|
|
|
// nsIClipboardDragDropHooks.h
|
|
|
|
|
|
|
|
nsCOMPtr<nsIClipboardDragDropHooks> override;
|
2003-10-22 06:09:48 +00:00
|
|
|
nsCOMPtr<nsISupports> isupp;
|
2003-04-17 13:16:36 +00:00
|
|
|
PRBool hasMoreHooks = PR_FALSE;
|
2003-10-22 06:09:48 +00:00
|
|
|
nsresult rv = NS_OK;
|
2003-04-17 13:16:36 +00:00
|
|
|
while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMoreHooks))
|
|
|
|
&& hasMoreHooks)
|
|
|
|
{
|
|
|
|
rv = enumerator->GetNext(getter_AddRefs(isupp));
|
|
|
|
if (NS_FAILED(rv)) break;
|
|
|
|
override = do_QueryInterface(isupp);
|
|
|
|
if (override)
|
|
|
|
{
|
2003-09-27 04:18:26 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult hookResult =
|
|
|
|
#endif
|
|
|
|
override->OnCopyOrDrag(nsnull, aTrans, aDoPutOnClipboard);
|
2003-04-17 13:16:36 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(hookResult), "OnCopyOrDrag hook failed");
|
|
|
|
if (!*aDoPutOnClipboard)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-07-13 13:38:10 +00:00
|
|
|
nsresult nsCopySupport::IsPlainTextContext(nsISelection *aSel, nsIDocument *aDoc, PRBool *aIsPlainTextContext)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!aSel || !aIsPlainTextContext)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*aIsPlainTextContext = PR_FALSE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
nsCOMPtr<nsIDOMNode> commonParent;
|
|
|
|
PRInt32 count = 0;
|
|
|
|
|
|
|
|
rv = aSel->GetRangeCount(&count);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// if selection is uninitialized return
|
|
|
|
if (!count)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// we'll just use the common parent of the first range. Implicit assumption
|
|
|
|
// here that multi-range selections are table cell selections, in which case
|
|
|
|
// the common parent is somewhere in the table and we don't really care where.
|
|
|
|
rv = aSel->GetRangeAt(0, getter_AddRefs(range));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!range)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
range->GetCommonAncestorContainer(getter_AddRefs(commonParent));
|
|
|
|
|
2003-07-28 21:25:13 +00:00
|
|
|
for (nsCOMPtr<nsIContent> selContent(do_QueryInterface(commonParent));
|
|
|
|
selContent;
|
|
|
|
selContent = selContent->GetParent())
|
2001-07-13 13:38:10 +00:00
|
|
|
{
|
|
|
|
// checking for selection inside a plaintext form widget
|
2003-11-19 01:20:56 +00:00
|
|
|
|
|
|
|
if (!selContent->IsContentOfType(nsIContent::eHTML)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom *atom = selContent->Tag();
|
2001-07-13 13:38:10 +00:00
|
|
|
|
2003-07-28 21:25:13 +00:00
|
|
|
if (atom == nsHTMLAtoms::input ||
|
|
|
|
atom == nsHTMLAtoms::textarea)
|
2001-07-13 13:38:10 +00:00
|
|
|
{
|
|
|
|
*aIsPlainTextContext = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-07-28 21:25:13 +00:00
|
|
|
if (atom == nsHTMLAtoms::body)
|
2001-07-13 13:38:10 +00:00
|
|
|
{
|
|
|
|
// check for moz prewrap style on body. If it's there we are
|
|
|
|
// in a plaintext editor. This is pretty cheezy but I haven't
|
|
|
|
// found a good way to tell if we are in a plaintext editor.
|
|
|
|
nsCOMPtr<nsIDOMElement> bodyElem = do_QueryInterface(selContent);
|
|
|
|
nsAutoString wsVal;
|
|
|
|
rv = bodyElem->GetAttribute(NS_LITERAL_STRING("style"), wsVal);
|
2002-03-19 06:46:56 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && (kNotFound != wsVal.Find(NS_LITERAL_STRING("-moz-pre-wrap"))))
|
2001-07-13 13:38:10 +00:00
|
|
|
{
|
|
|
|
*aIsPlainTextContext = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-01 22:43:56 +00:00
|
|
|
// also consider ourselves in a text widget if we can't find an html
|
|
|
|
// document. Note that XHTML is not counted as HTML here, because we can't
|
|
|
|
// copy it properly (all the copy code for non-plaintext assumes using HTML
|
|
|
|
// serializers and parsers is OK, and those mess up XHTML).
|
2001-07-13 13:38:10 +00:00
|
|
|
nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(aDoc);
|
2004-12-01 22:43:56 +00:00
|
|
|
if (!htmlDoc || aDoc->IsCaseSensitive())
|
|
|
|
*aIsPlainTextContext = PR_TRUE;
|
2001-07-13 13:38:10 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-04-18 20:20:04 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCopySupport::GetContents(const nsACString& aMimeType, PRUint32 aFlags, nsISelection *aSel, nsIDocument *aDoc, nsAString& outdata)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocumentEncoder> docEncoder;
|
|
|
|
|
|
|
|
nsCAutoString encoderContractID(NS_DOC_ENCODER_CONTRACTID_BASE);
|
|
|
|
encoderContractID.Append(aMimeType);
|
|
|
|
|
|
|
|
docEncoder = do_CreateInstance(encoderContractID.get());
|
|
|
|
NS_ENSURE_TRUE(docEncoder, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
PRUint32 flags = aFlags;
|
|
|
|
|
|
|
|
if (aMimeType.Equals("text/plain"))
|
|
|
|
flags |= nsIDocumentEncoder::OutputPreformatted;
|
|
|
|
|
|
|
|
NS_ConvertASCIItoUCS2 unicodeMimeType(aMimeType);
|
|
|
|
rv = docEncoder->Init(aDoc, unicodeMimeType, flags);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (aSel)
|
|
|
|
{
|
|
|
|
rv = docEncoder->SetSelection(aSel);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode the selection
|
|
|
|
return docEncoder->EncodeToString(outdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2004-06-05 17:57:00 +00:00
|
|
|
nsCopySupport::ImageCopy(nsIImageLoadingContent* imageElement, PRInt16 aClipboardID)
|
2003-04-18 20:20:04 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2004-06-05 17:57:00 +00:00
|
|
|
|
2004-07-30 06:04:57 +00:00
|
|
|
nsCOMPtr<nsIImage> image = nsContentUtils::GetImageFromContent(imageElement);
|
2003-04-18 20:20:04 +00:00
|
|
|
if (!image) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Get the Clipboard
|
|
|
|
nsCOMPtr<nsIClipboard> clipboard(do_GetService(kCClipboardCID, &rv));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (!clipboard) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// 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*));
|
|
|
|
|
|
|
|
// put the transferable on the clipboard
|
|
|
|
return clipboard->SetData(trans, nsnull, aClipboardID);
|
|
|
|
}
|