michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: #include "mozFlushType.h" michael@0: #include "mozilla/Assertions.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsCRT.h" michael@0: #include "nsDebug.h" michael@0: #include "nsEditorCommands.h" michael@0: #include "nsError.h" michael@0: #include "nsIClipboard.h" michael@0: #include "nsICommandParams.h" michael@0: #include "nsID.h" michael@0: #include "nsIDOMDocument.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIEditor.h" michael@0: #include "nsIEditorMailSupport.h" michael@0: #include "nsIPlaintextEditor.h" michael@0: #include "nsISelection.h" michael@0: #include "nsISelectionController.h" michael@0: #include "nsITransferable.h" michael@0: #include "nsString.h" michael@0: #include "nsAString.h" michael@0: michael@0: class nsISupports; michael@0: michael@0: michael@0: #define STATE_ENABLED "state_enabled" michael@0: #define STATE_DATA "state_data" michael@0: michael@0: michael@0: nsBaseEditorCommand::nsBaseEditorCommand() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsBaseEditorCommand, nsIControllerCommand) michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsUndoCommand::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *aCommandRefCon, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: { michael@0: bool isEnabled, isEditable = false; michael@0: nsresult rv = editor->GetIsSelectionEditable(&isEditable); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (isEditable) michael@0: return editor->CanUndo(&isEnabled, outCmdEnabled); michael@0: } michael@0: michael@0: *outCmdEnabled = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsUndoCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: return editor->Undo(1); michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsUndoCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: return DoCommand(aCommandName, aCommandRefCon); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsUndoCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: bool canUndo; michael@0: IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo); michael@0: return aParams->SetBooleanValue(STATE_ENABLED,canUndo); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsRedoCommand::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *aCommandRefCon, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: { michael@0: bool isEnabled, isEditable = false; michael@0: nsresult rv = editor->GetIsSelectionEditable(&isEditable); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (isEditable) michael@0: return editor->CanRedo(&isEnabled, outCmdEnabled); michael@0: } michael@0: michael@0: *outCmdEnabled = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsRedoCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: return editor->Redo(1); michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsRedoCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: return DoCommand(aCommandName, aCommandRefCon); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsRedoCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: bool canUndo; michael@0: IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo); michael@0: return aParams->SetBooleanValue(STATE_ENABLED,canUndo); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsClearUndoCommand::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *refCon, bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: nsCOMPtr editor = do_QueryInterface(refCon); michael@0: if (editor) michael@0: return editor->GetIsSelectionEditable(outCmdEnabled); michael@0: michael@0: *outCmdEnabled = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsClearUndoCommand::DoCommand(const char *aCommandName, nsISupports *refCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(refCon); michael@0: NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED); michael@0: michael@0: editor->EnableUndo(false); // Turning off undo clears undo/redo stacks. michael@0: editor->EnableUndo(true); // This re-enables undo/redo. michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsClearUndoCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *refCon) michael@0: { michael@0: return DoCommand(aCommandName, refCon); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsClearUndoCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *refCon) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aParams); michael@0: michael@0: bool enabled; michael@0: nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return aParams->SetBooleanValue(STATE_ENABLED, enabled); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCutCommand::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *aCommandRefCon, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: { michael@0: bool isEditable = false; michael@0: nsresult rv = editor->GetIsSelectionEditable(&isEditable); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (isEditable) michael@0: return editor->CanCut(outCmdEnabled); michael@0: } michael@0: michael@0: *outCmdEnabled = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsCutCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: return editor->Cut(); michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCutCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: return DoCommand(aCommandName, aCommandRefCon); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCutCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: bool canUndo; michael@0: IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo); michael@0: return aParams->SetBooleanValue(STATE_ENABLED,canUndo); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsCutOrDeleteCommand::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *aCommandRefCon, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: return editor->GetIsSelectionEditable(outCmdEnabled); michael@0: michael@0: *outCmdEnabled = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsCutOrDeleteCommand::DoCommand(const char *aCommandName, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: { michael@0: nsCOMPtr selection; michael@0: nsresult rv = editor->GetSelection(getter_AddRefs(selection)); michael@0: if (NS_SUCCEEDED(rv) && selection && selection->Collapsed()) { michael@0: return editor->DeleteSelection(nsIEditor::eNext, nsIEditor::eStrip); michael@0: } michael@0: return editor->Cut(); michael@0: } michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCutOrDeleteCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: return DoCommand(aCommandName, aCommandRefCon); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCutOrDeleteCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: bool canUndo; michael@0: IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo); michael@0: return aParams->SetBooleanValue(STATE_ENABLED,canUndo); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCopyCommand::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *aCommandRefCon, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: return editor->CanCopy(outCmdEnabled); michael@0: michael@0: *outCmdEnabled = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsCopyCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: return editor->Copy(); michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCopyCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: return DoCommand(aCommandName, aCommandRefCon); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCopyCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: bool canUndo; michael@0: IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo); michael@0: return aParams->SetBooleanValue(STATE_ENABLED,canUndo); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCopyOrDeleteCommand::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *aCommandRefCon, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: return editor->GetIsSelectionEditable(outCmdEnabled); michael@0: michael@0: *outCmdEnabled = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsCopyOrDeleteCommand::DoCommand(const char *aCommandName, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: { michael@0: nsCOMPtr selection; michael@0: nsresult rv = editor->GetSelection(getter_AddRefs(selection)); michael@0: if (NS_SUCCEEDED(rv) && selection && selection->Collapsed()) { michael@0: return editor->DeleteSelection(nsIEditor::eNextWord, nsIEditor::eStrip); michael@0: } michael@0: return editor->Copy(); michael@0: } michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCopyOrDeleteCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: return DoCommand(aCommandName, aCommandRefCon); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCopyOrDeleteCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: bool canUndo; michael@0: IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo); michael@0: return aParams->SetBooleanValue(STATE_ENABLED,canUndo); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPasteCommand::IsCommandEnabled(const char *aCommandName, michael@0: nsISupports *aCommandRefCon, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: { michael@0: bool isEditable = false; michael@0: nsresult rv = editor->GetIsSelectionEditable(&isEditable); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (isEditable) michael@0: return editor->CanPaste(nsIClipboard::kGlobalClipboard, outCmdEnabled); michael@0: } michael@0: michael@0: *outCmdEnabled = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsPasteCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE); michael@0: michael@0: return editor->Paste(nsIClipboard::kGlobalClipboard); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPasteCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: return DoCommand(aCommandName, aCommandRefCon); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPasteCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: bool canUndo; michael@0: IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo); michael@0: return aParams->SetBooleanValue(STATE_ENABLED,canUndo); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPasteTransferableCommand::IsCommandEnabled(const char *aCommandName, michael@0: nsISupports *aCommandRefCon, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: { michael@0: bool isEditable = false; michael@0: nsresult rv = editor->GetIsSelectionEditable(&isEditable); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (isEditable) michael@0: return editor->CanPasteTransferable(nullptr, outCmdEnabled); michael@0: } michael@0: michael@0: *outCmdEnabled = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPasteTransferableCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon) michael@0: { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPasteTransferableCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE); michael@0: michael@0: nsCOMPtr supports; michael@0: aParams->GetISupportsValue("transferable", getter_AddRefs(supports)); michael@0: NS_ENSURE_TRUE(supports, NS_ERROR_FAILURE); michael@0: michael@0: nsCOMPtr trans = do_QueryInterface(supports); michael@0: NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE); michael@0: michael@0: return editor->PasteTransferable(trans); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPasteTransferableCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE); michael@0: michael@0: nsCOMPtr trans; michael@0: michael@0: nsCOMPtr supports; michael@0: aParams->GetISupportsValue("transferable", getter_AddRefs(supports)); michael@0: if (supports) { michael@0: trans = do_QueryInterface(supports); michael@0: NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE); michael@0: } michael@0: michael@0: bool canPaste; michael@0: nsresult rv = editor->CanPasteTransferable(trans, &canPaste); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return aParams->SetBooleanValue(STATE_ENABLED, canPaste); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSwitchTextDirectionCommand::IsCommandEnabled(const char *aCommandName, michael@0: nsISupports *aCommandRefCon, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: return editor->GetIsSelectionEditable(outCmdEnabled); michael@0: michael@0: *outCmdEnabled = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSwitchTextDirectionCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE); michael@0: michael@0: return editor->SwitchTextDirection(); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSwitchTextDirectionCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: return DoCommand(aCommandName, aCommandRefCon); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSwitchTextDirectionCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: bool canSwitchTextDirection = true; michael@0: IsCommandEnabled(aCommandName, aCommandRefCon, &canSwitchTextDirection); michael@0: return aParams->SetBooleanValue(STATE_ENABLED, canSwitchTextDirection); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsDeleteCommand::IsCommandEnabled(const char* aCommandName, michael@0: nsISupports* aCommandRefCon, michael@0: bool* outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: *outCmdEnabled = false; michael@0: michael@0: NS_ENSURE_TRUE(editor, NS_OK); michael@0: michael@0: // We can generally delete whenever the selection is editable. However, michael@0: // cmd_delete doesn't make sense if the selection is collapsed because it's michael@0: // directionless, which is the same condition under which we can't cut. michael@0: nsresult rv = editor->GetIsSelectionEditable(outCmdEnabled); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (!nsCRT::strcmp("cmd_delete", aCommandName) && *outCmdEnabled) { michael@0: rv = editor->CanCut(outCmdEnabled); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsDeleteCommand::DoCommand(const char* aCommandName, michael@0: nsISupports* aCommandRefCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE); michael@0: michael@0: nsIEditor::EDirection deleteDir = nsIEditor::eNone; michael@0: michael@0: if (!nsCRT::strcmp("cmd_delete", aCommandName)) { michael@0: // Really this should probably be eNone, but it only makes a difference if michael@0: // the selection is collapsed, and then this command is disabled. So let's michael@0: // keep it as it always was to avoid breaking things. michael@0: deleteDir = nsIEditor::ePrevious; michael@0: } else if (!nsCRT::strcmp("cmd_deleteCharForward", aCommandName)) { michael@0: deleteDir = nsIEditor::eNext; michael@0: } else if (!nsCRT::strcmp("cmd_deleteCharBackward", aCommandName)) { michael@0: deleteDir = nsIEditor::ePrevious; michael@0: } else if (!nsCRT::strcmp("cmd_deleteWordBackward", aCommandName)) { michael@0: deleteDir = nsIEditor::ePreviousWord; michael@0: } else if (!nsCRT::strcmp("cmd_deleteWordForward", aCommandName)) { michael@0: deleteDir = nsIEditor::eNextWord; michael@0: } else if (!nsCRT::strcmp("cmd_deleteToBeginningOfLine", aCommandName)) { michael@0: deleteDir = nsIEditor::eToBeginningOfLine; michael@0: } else if (!nsCRT::strcmp("cmd_deleteToEndOfLine", aCommandName)) { michael@0: deleteDir = nsIEditor::eToEndOfLine; michael@0: } else { michael@0: MOZ_CRASH("Unrecognized nsDeleteCommand"); michael@0: } michael@0: michael@0: return editor->DeleteSelection(deleteDir, nsIEditor::eStrip); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsDeleteCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: return DoCommand(aCommandName, aCommandRefCon); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsDeleteCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: bool canUndo; michael@0: IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo); michael@0: return aParams->SetBooleanValue(STATE_ENABLED,canUndo); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSelectAllCommand::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *aCommandRefCon, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: michael@0: nsresult rv = NS_OK; michael@0: // You can always select all, unless the selection is editable, michael@0: // and the editable region is empty! michael@0: *outCmdEnabled = true; michael@0: bool docIsEmpty; michael@0: michael@0: // you can select all if there is an editor which is non-empty michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) { michael@0: rv = editor->GetDocumentIsEmpty(&docIsEmpty); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: *outCmdEnabled = !docIsEmpty; michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsSelectAllCommand::DoCommand(const char *aCommandName, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: return editor->SelectAll(); michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSelectAllCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: return DoCommand(aCommandName, aCommandRefCon); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSelectAllCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: bool canUndo; michael@0: IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo); michael@0: return aParams->SetBooleanValue(STATE_ENABLED,canUndo); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsSelectionMoveCommands::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *aCommandRefCon, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: if (editor) michael@0: return editor->GetIsSelectionEditable(outCmdEnabled); michael@0: michael@0: *outCmdEnabled = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSelectionMoveCommands::DoCommand(const char *aCommandName, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(aCommandRefCon); michael@0: NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE); michael@0: michael@0: nsCOMPtr domDoc; michael@0: editor->GetDocument(getter_AddRefs(domDoc)); michael@0: nsCOMPtr doc = do_QueryInterface(domDoc); michael@0: if (doc) { michael@0: // Most of the commands below (possibly all of them) need layout to michael@0: // be up to date. michael@0: doc->FlushPendingNotifications(Flush_Layout); michael@0: } michael@0: michael@0: nsCOMPtr selCont; michael@0: nsresult rv = editor->GetSelectionController(getter_AddRefs(selCont)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: NS_ENSURE_TRUE(selCont, NS_ERROR_FAILURE); michael@0: michael@0: // complete scroll commands michael@0: if (!nsCRT::strcmp(aCommandName,"cmd_scrollTop")) michael@0: return selCont->CompleteScroll(false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_scrollBottom")) michael@0: return selCont->CompleteScroll(true); michael@0: michael@0: // complete move commands michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_moveTop")) michael@0: return selCont->CompleteMove(false, false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_moveBottom")) michael@0: return selCont->CompleteMove(true, false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_selectTop")) michael@0: return selCont->CompleteMove(false, true); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_selectBottom")) michael@0: return selCont->CompleteMove(true, true); michael@0: michael@0: // line move commands michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_lineNext")) michael@0: return selCont->LineMove(true, false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_linePrevious")) michael@0: return selCont->LineMove(false, false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_selectLineNext")) michael@0: return selCont->LineMove(true, true); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_selectLinePrevious")) michael@0: return selCont->LineMove(false, true); michael@0: michael@0: // character move commands michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_charPrevious")) michael@0: return selCont->CharacterMove(false, false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_charNext")) michael@0: return selCont->CharacterMove(true, false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_selectCharPrevious")) michael@0: return selCont->CharacterMove(false, true); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_selectCharNext")) michael@0: return selCont->CharacterMove(true, true); michael@0: michael@0: // intra line move commands michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_beginLine")) michael@0: return selCont->IntraLineMove(false, false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_endLine")) michael@0: return selCont->IntraLineMove(true, false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_selectBeginLine")) michael@0: return selCont->IntraLineMove(false, true); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_selectEndLine")) michael@0: return selCont->IntraLineMove(true, true); michael@0: michael@0: // word move commands michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_wordPrevious")) michael@0: return selCont->WordMove(false, false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_wordNext")) michael@0: return selCont->WordMove(true, false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_selectWordPrevious")) michael@0: return selCont->WordMove(false, true); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_selectWordNext")) michael@0: return selCont->WordMove(true, true); michael@0: michael@0: // scroll page commands michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_scrollPageUp")) michael@0: return selCont->ScrollPage(false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_scrollPageDown")) michael@0: return selCont->ScrollPage(true); michael@0: michael@0: // scroll line commands michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_scrollLineUp")) michael@0: return selCont->ScrollLine(false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_scrollLineDown")) michael@0: return selCont->ScrollLine(true); michael@0: michael@0: // page move commands michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_movePageUp")) michael@0: return selCont->PageMove(false, false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_movePageDown")) michael@0: return selCont->PageMove(true, false); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_selectPageUp")) michael@0: return selCont->PageMove(false, true); michael@0: else if (!nsCRT::strcmp(aCommandName,"cmd_selectPageDown")) michael@0: return selCont->PageMove(true, true); michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSelectionMoveCommands::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: return DoCommand(aCommandName, aCommandRefCon); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSelectionMoveCommands::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandRefCon) michael@0: { michael@0: bool canUndo; michael@0: IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo); michael@0: return aParams->SetBooleanValue(STATE_ENABLED,canUndo); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsInsertPlaintextCommand::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *refCon, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: nsCOMPtr editor = do_QueryInterface(refCon); michael@0: if (editor) michael@0: return editor->GetIsSelectionEditable(outCmdEnabled); michael@0: michael@0: *outCmdEnabled = false; michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsInsertPlaintextCommand::DoCommand(const char *aCommandName, michael@0: nsISupports *refCon) michael@0: { michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsInsertPlaintextCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *refCon) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aParams); michael@0: michael@0: nsCOMPtr editor = do_QueryInterface(refCon); michael@0: NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED); michael@0: michael@0: // Get text to insert from command params michael@0: nsAutoString text; michael@0: nsresult rv = aParams->GetStringValue(STATE_DATA, text); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (!text.IsEmpty()) michael@0: return editor->InsertText(text); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsInsertPlaintextCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *refCon) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aParams); michael@0: michael@0: bool outCmdEnabled = false; michael@0: IsCommandEnabled(aCommandName, refCon, &outCmdEnabled); michael@0: return aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsPasteQuotationCommand::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *refCon, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: michael@0: nsCOMPtr editor = do_QueryInterface(refCon); michael@0: nsCOMPtr mailEditor = do_QueryInterface(refCon); michael@0: if (editor && mailEditor) { michael@0: uint32_t flags; michael@0: editor->GetFlags(&flags); michael@0: if (!(flags & nsIPlaintextEditor::eEditorSingleLineMask)) michael@0: return editor->CanPaste(nsIClipboard::kGlobalClipboard, outCmdEnabled); michael@0: } michael@0: michael@0: *outCmdEnabled = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsPasteQuotationCommand::DoCommand(const char *aCommandName, michael@0: nsISupports *refCon) michael@0: { michael@0: nsCOMPtr mailEditor = do_QueryInterface(refCon); michael@0: if (mailEditor) michael@0: return mailEditor->PasteAsQuotation(nsIClipboard::kGlobalClipboard); michael@0: michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPasteQuotationCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *refCon) michael@0: { michael@0: nsCOMPtr mailEditor = do_QueryInterface(refCon); michael@0: if (mailEditor) michael@0: return mailEditor->PasteAsQuotation(nsIClipboard::kGlobalClipboard); michael@0: michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPasteQuotationCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *refCon) michael@0: { michael@0: nsCOMPtr editor = do_QueryInterface(refCon); michael@0: if (editor) michael@0: { michael@0: bool enabled = false; michael@0: editor->CanPaste(nsIClipboard::kGlobalClipboard, &enabled); michael@0: aParams->SetBooleanValue(STATE_ENABLED, enabled); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: