2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
#include "mozilla/Util.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsPlaintextEditor.h"
|
|
|
|
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIFormControl.h"
|
2007-05-14 02:11:38 -07:00
|
|
|
#include "nsIDOMEventTarget.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMNSEvent.h"
|
|
|
|
#include "nsIDOMMouseEvent.h"
|
2008-08-27 05:07:27 -07:00
|
|
|
#include "nsIDOMDragEvent.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsISelection.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2012-06-10 16:37:47 -07:00
|
|
|
#include "nsIPrivateDOMEvent.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsIDOMRange.h"
|
2012-02-17 06:16:59 -08:00
|
|
|
#include "nsIDOMDOMStringList.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDocumentEncoder.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
|
|
|
|
// Drag & Drop, Clipboard
|
|
|
|
#include "nsIClipboard.h"
|
|
|
|
#include "nsITransferable.h"
|
|
|
|
#include "nsIDragService.h"
|
2011-08-26 00:43:49 -07:00
|
|
|
#include "nsIDOMUIEvent.h"
|
2010-03-19 11:32:13 -07:00
|
|
|
#include "nsCopySupport.h"
|
2012-02-17 06:16:59 -08:00
|
|
|
#include "nsITransferable.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Misc
|
|
|
|
#include "nsEditorUtils.h"
|
|
|
|
#include "nsContentCID.h"
|
|
|
|
#include "nsISelectionPrivate.h"
|
|
|
|
#include "nsFrameSelection.h"
|
2007-07-25 21:14:33 -07:00
|
|
|
#include "nsEventDispatcher.h"
|
2011-09-15 12:32:42 -07:00
|
|
|
#include "nsContentUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP nsPlaintextEditor::PrepareTransferable(nsITransferable **transferable)
|
|
|
|
{
|
|
|
|
// Create generic Transferable for getting the data
|
|
|
|
nsresult rv = CallCreateInstance("@mozilla.org/widget/transferable;1", transferable);
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Get the nsITransferable interface for getting the data from the clipboard
|
2009-03-26 23:50:03 -07:00
|
|
|
if (transferable) {
|
|
|
|
(*transferable)->AddDataFlavor(kUnicodeMime);
|
|
|
|
(*transferable)->AddDataFlavor(kMozTextInternal);
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsPlaintextEditor::InsertTextAt(const nsAString &aStringToInsert,
|
|
|
|
nsIDOMNode *aDestinationNode,
|
|
|
|
PRInt32 aDestOffset,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aDoDeleteSelection)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (aDestinationNode)
|
|
|
|
{
|
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> targetNode = aDestinationNode;
|
|
|
|
PRInt32 targetOffset = aDestOffset;
|
|
|
|
|
|
|
|
if (aDoDeleteSelection)
|
|
|
|
{
|
|
|
|
// Use an auto tracker so that our drop point is correctly
|
|
|
|
// positioned after the delete.
|
|
|
|
nsAutoTrackDOMPoint tracker(mRangeUpdater, &targetNode, &targetOffset);
|
2012-05-10 07:54:33 -07:00
|
|
|
res = DeleteSelection(eNone, eStrip);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = selection->Collapse(targetNode, targetOffset);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return InsertText(aStringToInsert);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPlaintextEditor::InsertTextFromTransferable(nsITransferable *aTransferable,
|
|
|
|
nsIDOMNode *aDestinationNode,
|
|
|
|
PRInt32 aDestOffset,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aDoDeleteSelection)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-03-26 18:36:44 -07:00
|
|
|
HandlingTrustedAction trusted(this);
|
2011-01-15 20:16:45 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
char* bestFlavor = nsnull;
|
|
|
|
nsCOMPtr<nsISupports> genericDataObj;
|
|
|
|
PRUint32 len = 0;
|
|
|
|
if (NS_SUCCEEDED(aTransferable->GetAnyTransferData(&bestFlavor, getter_AddRefs(genericDataObj), &len))
|
2009-03-26 23:50:03 -07:00
|
|
|
&& bestFlavor && (0 == nsCRT::strcmp(bestFlavor, kUnicodeMime) ||
|
|
|
|
0 == nsCRT::strcmp(bestFlavor, kMozTextInternal)))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsAutoTxnsConserveSelection dontSpazMySelection(this);
|
|
|
|
nsCOMPtr<nsISupportsString> textDataObj ( do_QueryInterface(genericDataObj) );
|
|
|
|
if (textDataObj && len > 0)
|
|
|
|
{
|
|
|
|
nsAutoString stuffToPaste;
|
|
|
|
textDataObj->GetData(stuffToPaste);
|
|
|
|
NS_ASSERTION(stuffToPaste.Length() <= (len/2), "Invalid length!");
|
2011-09-15 12:32:42 -07:00
|
|
|
|
|
|
|
// Sanitize possible carriage returns in the string to be inserted
|
|
|
|
nsContentUtils::PlatformToDOMLineBreaks(stuffToPaste);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
rv = InsertTextAt(stuffToPaste, aDestinationNode, aDestOffset, aDoDeleteSelection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_Free(bestFlavor);
|
|
|
|
|
|
|
|
// Try to scroll the selection into view if the paste/drop succeeded
|
2008-02-28 07:28:37 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2011-10-17 07:59:28 -07:00
|
|
|
ScrollSelectionIntoView(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:16:59 -08:00
|
|
|
nsresult nsPlaintextEditor::InsertFromDataTransfer(nsIDOMDataTransfer *aDataTransfer,
|
|
|
|
PRInt32 aIndex,
|
|
|
|
nsIDOMDocument *aSourceDoc,
|
|
|
|
nsIDOMNode *aDestinationNode,
|
|
|
|
PRInt32 aDestOffset,
|
|
|
|
bool aDoDeleteSelection)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIVariant> data;
|
|
|
|
aDataTransfer->MozGetDataAt(NS_LITERAL_STRING("text/plain"), aIndex,
|
|
|
|
getter_AddRefs(data));
|
2012-02-23 12:59:45 -08:00
|
|
|
if (data) {
|
|
|
|
nsAutoString insertText;
|
|
|
|
data->GetAsAString(insertText);
|
|
|
|
nsContentUtils::PlatformToDOMLineBreaks(insertText);
|
2012-02-17 06:16:59 -08:00
|
|
|
|
2012-02-23 12:59:45 -08:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
return InsertTextAt(insertText, aDestinationNode, aDestOffset, aDoDeleteSelection);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-02-17 06:16:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsPlaintextEditor::InsertFromDrop(nsIDOMEvent* aDropEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
ForceCompositionEnd();
|
|
|
|
|
2012-02-17 06:16:59 -08:00
|
|
|
nsCOMPtr<nsIDOMDragEvent> dragEvent(do_QueryInterface(aDropEvent));
|
|
|
|
NS_ENSURE_TRUE(dragEvent, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDataTransfer> dataTransfer;
|
|
|
|
nsresult rv = dragEvent->GetDataTransfer(getter_AddRefs(dataTransfer));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-20 08:21:41 -07:00
|
|
|
nsCOMPtr<nsIDragSession> dragSession = nsContentUtils::GetDragSession();
|
|
|
|
NS_ASSERTION(dragSession, "No drag session");
|
|
|
|
|
2012-06-10 16:37:47 -07:00
|
|
|
nsCOMPtr<nsIPrivateDOMEvent> privateEvent(do_QueryInterface(aDropEvent));
|
|
|
|
nsDragEvent* dragEventInternal = static_cast<nsDragEvent *>(privateEvent->GetInternalNSEvent());
|
2012-03-20 08:21:41 -07:00
|
|
|
if (nsContentUtils::CheckForSubFrameDrop(dragSession, dragEventInternal)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Current doc is destination
|
2012-03-24 01:19:14 -07:00
|
|
|
nsCOMPtr<nsIDOMDocument> destdomdoc = GetDOMDocument();
|
|
|
|
NS_ENSURE_TRUE(destdomdoc, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-17 06:16:59 -08:00
|
|
|
PRUint32 numItems = 0;
|
2012-02-17 06:16:59 -08:00
|
|
|
rv = dataTransfer->GetMozItemCount(&numItems);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (numItems < 1) return NS_ERROR_FAILURE; // nothing to drop?
|
|
|
|
|
|
|
|
// Combine any deletion and drop insertion into one transaction
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool deleteSelection = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// We have to figure out whether to delete and relocate caret only once
|
|
|
|
// Parent and offset are under the mouse cursor
|
2011-08-26 00:43:49 -07:00
|
|
|
nsCOMPtr<nsIDOMUIEvent> uiEvent = do_QueryInterface(aDropEvent);
|
|
|
|
NS_ENSURE_TRUE(uiEvent, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> newSelectionParent;
|
2011-08-26 00:43:49 -07:00
|
|
|
rv = uiEvent->GetRangeParent(getter_AddRefs(newSelectionParent));
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(newSelectionParent, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PRInt32 newSelectionOffset;
|
2011-08-26 00:43:49 -07:00
|
|
|
rv = uiEvent->GetRangeOffset(&newSelectionOffset);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
rv = GetSelection(getter_AddRefs(selection));
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-18 01:29:39 -07:00
|
|
|
bool isCollapsed = selection->Collapsed();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-17 06:16:59 -08:00
|
|
|
nsCOMPtr<nsIDOMNode> sourceNode;
|
|
|
|
dataTransfer->GetMozSourceNode(getter_AddRefs(sourceNode));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> srcdomdoc;
|
|
|
|
if (sourceNode) {
|
|
|
|
sourceNode->GetOwnerDocument(getter_AddRefs(srcdomdoc));
|
|
|
|
NS_ENSURE_TRUE(sourceNode, NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only the nsHTMLEditor::FindUserSelectAllNode returns a node.
|
|
|
|
nsCOMPtr<nsIDOMNode> userSelectNode = FindUserSelectAllNode(newSelectionParent);
|
|
|
|
if (userSelectNode)
|
|
|
|
{
|
|
|
|
// The drop is happening over a "-moz-user-select: all"
|
|
|
|
// subtree so make sure the content we insert goes before
|
|
|
|
// the root of the subtree.
|
|
|
|
//
|
|
|
|
// XXX: Note that inserting before the subtree matches the
|
|
|
|
// current behavior when dropping on top of an image.
|
|
|
|
// The decision for dropping before or after the
|
|
|
|
// subtree should really be done based on coordinates.
|
|
|
|
|
|
|
|
rv = GetNodeLocation(userSelectNode, address_of(newSelectionParent),
|
|
|
|
&newSelectionOffset);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(newSelectionParent, NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Check if mouse is in the selection
|
|
|
|
// if so, jump through some hoops to determine if mouse is over selection (bail)
|
|
|
|
// and whether user wants to copy selection or delete it
|
|
|
|
if (!isCollapsed)
|
|
|
|
{
|
|
|
|
// We never have to delete if selection is already collapsed
|
2011-09-28 23:19:26 -07:00
|
|
|
bool cursorIsInSelection = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PRInt32 rangeCount;
|
|
|
|
rv = selection->GetRangeCount(&rangeCount);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (PRInt32 j = 0; j < rangeCount; j++)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
rv = selection->GetRangeAt(j, getter_AddRefs(range));
|
2012-01-10 06:19:54 -08:00
|
|
|
if (NS_FAILED(rv) || !range)
|
2007-03-22 10:30:00 -07:00
|
|
|
continue; // don't bail yet, iterate through them all
|
|
|
|
|
2012-01-10 06:19:54 -08:00
|
|
|
rv = range->IsPointInRange(newSelectionParent, newSelectionOffset, &cursorIsInSelection);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (cursorIsInSelection)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cursorIsInSelection)
|
|
|
|
{
|
|
|
|
// Dragging within same doc can't drop on itself -- leave!
|
|
|
|
if (srcdomdoc == destdomdoc)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Dragging from another window onto a selection
|
|
|
|
// XXX Decision made to NOT do this,
|
|
|
|
// note that 4.x does replace if dropped on
|
2011-10-17 07:59:28 -07:00
|
|
|
//deleteSelection = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We are NOT over the selection
|
|
|
|
if (srcdomdoc == destdomdoc)
|
|
|
|
{
|
|
|
|
// Within the same doc: delete if user doesn't want to copy
|
2012-02-17 06:16:59 -08:00
|
|
|
PRUint32 dropEffect;
|
|
|
|
dataTransfer->GetDropEffectInt(&dropEffect);
|
|
|
|
deleteSelection = !(dropEffect & nsIDragService::DRAGDROP_ACTION_COPY);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Different source doc: Don't delete
|
2011-10-17 07:59:28 -07:00
|
|
|
deleteSelection = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:16:59 -08:00
|
|
|
if (IsPlaintextEditor()) {
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(newSelectionParent);
|
|
|
|
while (content) {
|
|
|
|
nsCOMPtr<nsIFormControl> formControl(do_QueryInterface(content));
|
|
|
|
if (formControl && !formControl->AllowDrop()) {
|
|
|
|
// Don't allow dropping into a form control that doesn't allow being
|
|
|
|
// dropped into.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
content = content->GetParent();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:16:59 -08:00
|
|
|
for (PRUint32 i = 0; i < numItems; ++i) {
|
|
|
|
InsertFromDataTransfer(dataTransfer, i, srcdomdoc, newSelectionParent,
|
|
|
|
newSelectionOffset, deleteSelection);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:16:59 -08:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
ScrollSelectionIntoView(false);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPlaintextEditor::Paste(PRInt32 aSelectionType)
|
|
|
|
{
|
2010-03-19 11:32:13 -07:00
|
|
|
if (!FireClipboardEvent(NS_PASTE))
|
|
|
|
return NS_OK;
|
2007-07-25 21:14:33 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Get Clipboard Service
|
2010-03-19 11:32:13 -07:00
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIClipboard> clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
|
|
|
|
if ( NS_FAILED(rv) )
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// Get the nsITransferable interface for getting the data from the clipboard
|
|
|
|
nsCOMPtr<nsITransferable> trans;
|
|
|
|
rv = PrepareTransferable(getter_AddRefs(trans));
|
|
|
|
if (NS_SUCCEEDED(rv) && trans)
|
|
|
|
{
|
|
|
|
// Get the Data from the clipboard
|
|
|
|
if (NS_SUCCEEDED(clipboard->GetData(trans, aSelectionType)) && IsModifiable())
|
|
|
|
{
|
|
|
|
// handle transferable hooks
|
2012-03-24 01:19:14 -07:00
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc = GetDOMDocument();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!nsEditorHookUtils::DoInsertionHook(domdoc, nsnull, trans))
|
|
|
|
return NS_OK;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = InsertTextFromTransferable(trans, nsnull, nsnull, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-01-10 17:45:45 -08:00
|
|
|
NS_IMETHODIMP nsPlaintextEditor::PasteTransferable(nsITransferable *aTransferable)
|
|
|
|
{
|
2010-03-19 11:32:13 -07:00
|
|
|
if (!FireClipboardEvent(NS_PASTE))
|
|
|
|
return NS_OK;
|
2010-01-10 17:45:45 -08:00
|
|
|
|
|
|
|
if (!IsModifiable())
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// handle transferable hooks
|
2012-03-24 01:19:14 -07:00
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc = GetDOMDocument();
|
2010-01-10 17:45:45 -08:00
|
|
|
if (!nsEditorHookUtils::DoInsertionHook(domdoc, nsnull, aTransferable))
|
|
|
|
return NS_OK;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return InsertTextFromTransferable(aTransferable, nsnull, nsnull, true);
|
2010-01-10 17:45:45 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP nsPlaintextEditor::CanPaste(PRInt32 aSelectionType, bool *aCanPaste)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-25 21:14:33 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aCanPaste);
|
2011-10-17 07:59:28 -07:00
|
|
|
*aCanPaste = false;
|
2007-07-25 21:14:33 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// can't paste if readonly
|
|
|
|
if (!IsModifiable())
|
|
|
|
return NS_OK;
|
|
|
|
|
2008-02-24 02:55:37 -08:00
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIClipboard> clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// the flavors that we can deal with
|
2007-11-20 11:06:30 -08:00
|
|
|
const char* textEditorFlavors[] = { kUnicodeMime };
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool haveFlavors;
|
2007-11-20 11:06:30 -08:00
|
|
|
rv = clipboard->HasDataMatchingFlavors(textEditorFlavors,
|
2011-10-10 22:50:08 -07:00
|
|
|
ArrayLength(textEditorFlavors),
|
2007-11-20 11:06:30 -08:00
|
|
|
aSelectionType, &haveFlavors);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aCanPaste = haveFlavors;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-01-10 17:45:45 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP nsPlaintextEditor::CanPasteTransferable(nsITransferable *aTransferable, bool *aCanPaste)
|
2010-01-10 17:45:45 -08:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aCanPaste);
|
|
|
|
|
|
|
|
// can't paste if readonly
|
|
|
|
if (!IsModifiable()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*aCanPaste = false;
|
2010-01-10 17:45:45 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If |aTransferable| is null, assume that a paste will succeed.
|
|
|
|
if (!aTransferable) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*aCanPaste = true;
|
2010-01-10 17:45:45 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> data;
|
|
|
|
PRUint32 dataLen;
|
|
|
|
nsresult rv = aTransferable->GetTransferData(kUnicodeMime,
|
|
|
|
getter_AddRefs(data),
|
|
|
|
&dataLen);
|
|
|
|
if (NS_SUCCEEDED(rv) && data)
|
2011-10-17 07:59:28 -07:00
|
|
|
*aCanPaste = true;
|
2010-01-10 17:45:45 -08:00
|
|
|
else
|
2011-10-17 07:59:28 -07:00
|
|
|
*aCanPaste = false;
|
2010-01-10 17:45:45 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|