gecko-dev/editor/base/nsEditorCommands.cpp

374 lines
11 KiB
C++
Raw Normal View History

2000-02-16 01:33:03 +00:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape 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/NPL/
*
* 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.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*
*/
#include "nsCRT.h"
#include "nsIEditor.h"
#include "nsIEditorMailSupport.h"
#include "nsISelectionController.h"
#include "nsIPresShell.h"
#include "nsIClipboard.h"
2000-02-16 01:33:03 +00:00
#include "nsEditorCommands.h"
nsBaseCommand::nsBaseCommand()
{
NS_INIT_REFCNT();
}
nsBaseCommand::~nsBaseCommand()
{
}
NS_IMPL_ISUPPORTS(nsBaseCommand, NS_GET_IID(nsIControllerCommand));
NS_IMETHODIMP
nsUndoCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (aEditor)
{
PRBool isEnabled;
return aEditor->CanUndo(isEnabled, *outCmdEnabled);
}
return NS_OK;
}
NS_IMETHODIMP
nsUndoCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
if (aEditor)
return aEditor->Undo(1);
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsRedoCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (aEditor)
{
PRBool isEnabled;
return aEditor->CanRedo(isEnabled, *outCmdEnabled);
}
return NS_OK;
}
NS_IMETHODIMP
nsRedoCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
if (aEditor)
return aEditor->Redo(1);
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsCutCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (aEditor)
return aEditor->CanCut(*outCmdEnabled);
return NS_OK;
}
NS_IMETHODIMP
nsCutCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
if (aEditor)
return aEditor->Cut();
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsCopyCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (aEditor)
return aEditor->CanCopy(*outCmdEnabled);
return NS_OK;
}
NS_IMETHODIMP
nsCopyCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
if (aEditor)
return aEditor->Copy();
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsPasteCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (aEditor)
return aEditor->CanPaste(nsIClipboard::kGlobalClipboard, *outCmdEnabled);
2000-02-16 01:33:03 +00:00
return NS_OK;
}
NS_IMETHODIMP
nsPasteCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
if (!aEditor)
return NS_ERROR_FAILURE;
nsresult rv = NS_OK;
nsAutoString cmdString(aCommand);
2000-04-18 07:44:58 +00:00
if (cmdString.EqualsWithConversion("cmd_paste"))
rv = aEditor->Paste(nsIClipboard::kGlobalClipboard);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_pasteQuote"))
2000-02-16 01:33:03 +00:00
{
nsCOMPtr<nsIEditorMailSupport> mailEditor = do_QueryInterface(aEditor, &rv);
if (mailEditor)
rv = mailEditor->PasteAsQuotation(nsIClipboard::kGlobalClipboard);
2000-02-16 01:33:03 +00:00
}
return rv;
}
NS_IMETHODIMP
nsDeleteCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
// we can delete when we can cut
if (!aEditor)
return NS_OK;
nsresult rv = NS_OK;
nsAutoString cmdString(aCommand);
2000-04-18 07:44:58 +00:00
if (cmdString.EqualsWithConversion("cmd_delete"))
2000-02-16 01:33:03 +00:00
rv = aEditor->CanCut(*outCmdEnabled);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_deleteCharBackward"))
2000-02-16 01:33:03 +00:00
*outCmdEnabled = PR_TRUE;
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_deleteCharForward"))
2000-02-16 01:33:03 +00:00
*outCmdEnabled = PR_TRUE;
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_deleteWordBackward"))
2000-02-16 01:33:03 +00:00
*outCmdEnabled = PR_TRUE;
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_deleteWordForward"))
2000-02-16 01:33:03 +00:00
*outCmdEnabled = PR_TRUE;
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_deleteToBeginningOfLine"))
2000-02-16 01:33:03 +00:00
*outCmdEnabled = PR_TRUE;
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_deleteToEndOfLine"))
2000-02-16 01:33:03 +00:00
*outCmdEnabled = PR_TRUE;
return rv;
}
NS_IMETHODIMP
nsDeleteCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
if (!aEditor)
return NS_ERROR_FAILURE;
nsAutoString cmdString(aCommand);
nsIEditor::EDirection deleteDir = nsIEditor::eNone;
2000-04-18 07:44:58 +00:00
if (cmdString.EqualsWithConversion("cmd_delete"))
2000-02-16 01:33:03 +00:00
deleteDir = nsIEditor::ePrevious;
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_deleteCharBackward"))
2000-02-16 01:33:03 +00:00
deleteDir = nsIEditor::ePrevious;
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_deleteCharForward"))
2000-02-16 01:33:03 +00:00
deleteDir = nsIEditor::eNext;
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_deleteWordBackward"))
2000-02-16 01:33:03 +00:00
deleteDir = nsIEditor::ePreviousWord;
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_deleteWordForward"))
2000-02-16 01:33:03 +00:00
deleteDir = nsIEditor::eNextWord;
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_deleteToBeginningOfLine"))
2000-02-16 01:33:03 +00:00
deleteDir = nsIEditor::eToBeginningOfLine;
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_deleteToEndOfLine"))
2000-02-16 01:33:03 +00:00
deleteDir = nsIEditor::eToEndOfLine;
return aEditor->DeleteSelection(deleteDir);
}
NS_IMETHODIMP
nsSelectAllCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (aEditor)
*outCmdEnabled = PR_TRUE; // you can always select all
return NS_OK;
}
NS_IMETHODIMP
nsSelectAllCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
if (aEditor)
return aEditor->SelectAll();
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsSelectionMoveCommands::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (!aEditor)
return NS_ERROR_FAILURE;
*outCmdEnabled = PR_TRUE;
return NS_OK;
}
NS_IMETHODIMP
nsSelectionMoveCommands::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditor> aEditor = do_QueryInterface(refCon);
if (!aEditor)
return NS_ERROR_FAILURE;
nsresult rv;
nsCOMPtr<nsIPresShell> presShell;
rv = aEditor->GetPresShell(getter_AddRefs(presShell));
if (NS_FAILED(rv) || !presShell)
return rv ? rv : NS_ERROR_FAILURE;
nsCOMPtr<nsISelectionController> selCont = do_QueryInterface(presShell);
if (!selCont)
return NS_ERROR_FAILURE;
nsAutoString cmdString(aCommand);
// complete scroll commands
2000-04-18 07:44:58 +00:00
if (cmdString.EqualsWithConversion("cmd_scrollTop"))
2000-02-16 01:33:03 +00:00
return selCont->CompleteScroll(PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_scrollBottom"))
2000-02-16 01:33:03 +00:00
return selCont->CompleteScroll(PR_TRUE);
// complete move commands
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_moveTop"))
2000-02-16 01:33:03 +00:00
return selCont->CompleteMove(PR_FALSE, PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_moveBottom"))
2000-02-16 01:33:03 +00:00
return selCont->CompleteMove(PR_TRUE, PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_selectTop"))
2000-02-16 01:33:03 +00:00
return selCont->CompleteMove(PR_FALSE, PR_TRUE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_selectBottom"))
2000-02-16 01:33:03 +00:00
return selCont->CompleteMove(PR_TRUE, PR_TRUE);
// line move commands
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_lineNext"))
2000-02-16 01:33:03 +00:00
return selCont->LineMove(PR_TRUE, PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_linePrevious"))
2000-02-16 01:33:03 +00:00
return selCont->LineMove(PR_FALSE, PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_selectLineNext"))
2000-02-16 01:33:03 +00:00
return selCont->LineMove(PR_TRUE, PR_TRUE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_selectLinePrevious"))
2000-02-16 01:33:03 +00:00
return selCont->LineMove(PR_FALSE, PR_TRUE);
// character move commands
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_charPrevious"))
2000-02-16 01:33:03 +00:00
return selCont->CharacterMove(PR_FALSE, PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_charNext"))
2000-02-16 01:33:03 +00:00
return selCont->CharacterMove(PR_TRUE, PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_selectCharPrevious"))
2000-02-16 01:33:03 +00:00
return selCont->CharacterMove(PR_FALSE, PR_TRUE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_selectCharNext"))
2000-02-16 01:33:03 +00:00
return selCont->CharacterMove(PR_TRUE, PR_TRUE);
// intra line move commands
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_beginLine"))
2000-02-16 01:33:03 +00:00
return selCont->IntraLineMove(PR_FALSE, PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_endLine"))
2000-02-16 01:33:03 +00:00
return selCont->IntraLineMove(PR_TRUE, PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_selectBeginLine"))
2000-02-16 01:33:03 +00:00
return selCont->IntraLineMove(PR_FALSE, PR_TRUE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_selectEndLine"))
2000-02-16 01:33:03 +00:00
return selCont->IntraLineMove(PR_TRUE, PR_TRUE);
// word move commands
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_wordPrevious"))
2000-02-16 01:33:03 +00:00
return selCont->WordMove(PR_FALSE, PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_wordNext"))
2000-02-16 01:33:03 +00:00
return selCont->WordMove(PR_TRUE, PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_selectWordPrevious"))
2000-02-16 01:33:03 +00:00
return selCont->WordMove(PR_FALSE, PR_TRUE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_selectWordNext"))
2000-02-16 01:33:03 +00:00
return selCont->WordMove(PR_TRUE, PR_TRUE);
// scroll page commands
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_scrollPageUp"))
2000-02-16 01:33:03 +00:00
return selCont->ScrollPage(PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_scrollPageDown"))
2000-02-16 01:33:03 +00:00
return selCont->ScrollPage(PR_TRUE);
// scroll line commands
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_scrollLineUp"))
2000-02-16 01:33:03 +00:00
return selCont->ScrollLine(PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_scrollLineDown"))
2000-02-16 01:33:03 +00:00
return selCont->ScrollLine(PR_TRUE);
// page move commands
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_scrollPageUp"))
2000-02-16 01:33:03 +00:00
return selCont->PageMove(PR_FALSE, PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_scrollPageDown"))
2000-02-16 01:33:03 +00:00
return selCont->PageMove(PR_TRUE, PR_FALSE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_selectPageUp"))
2000-02-16 01:33:03 +00:00
return selCont->PageMove(PR_FALSE, PR_TRUE);
2000-04-18 07:44:58 +00:00
else if (cmdString.EqualsWithConversion("cmd_selectPageDown"))
2000-02-16 01:33:03 +00:00
return selCont->PageMove(PR_TRUE, PR_TRUE);
return NS_ERROR_FAILURE;
}