This commit is contained in:
sfraser%netscape.com 2000-05-01 21:37:38 +00:00
Родитель f5e1bee33e
Коммит 288d009794
5 изменённых файлов: 2406 добавлений и 0 удалений

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

@ -0,0 +1,773 @@
/* -*- 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 "nsIEditor.h"
#include "nsIHTMLEditor.h"
#include "nsIEditorShell.h"
#include "nsIDOMSelection.h"
#include "nsIDOMNode.h"
#include "nsIDOMElement.h"
#include "nsIDOMWindow.h"
#include "nsIDOMDocument.h"
#include "nsIClipboard.h"
#include "nsXPIDLString.h"
#include "nsComposerCommands.h"
// utility methods
//---------------------------------------------------------------------------
static PRBool XULNodeExists(nsIEditorShell* aEditorShell, const char* nodeID)
//---------------------------------------------------------------------------
{
nsCOMPtr<nsIDOMWindow> webshellWindow;
aEditorShell->GetWebShellWindow(getter_AddRefs(webshellWindow));
nsCOMPtr<nsIDOMDocument> chromeDoc;
webshellWindow->GetDocument(getter_AddRefs(chromeDoc));
if (!chromeDoc) return PR_FALSE;
nsCOMPtr<nsIDOMElement> elem;
nsresult rv = chromeDoc->GetElementById( NS_ConvertASCIItoUCS2(nodeID), getter_AddRefs(elem) );
return NS_SUCCEEDED(rv) && (elem.get() != nsnull);
}
//---------------------------------------------------------------------------
static nsresult SetNodeAttribute(nsIEditorShell* aEditorShell, const PRUnichar* nodeID,
const PRUnichar* attributeName, const nsString& newValue)
//---------------------------------------------------------------------------
{
nsCOMPtr<nsIDOMWindow> webshellWindow;
aEditorShell->GetWebShellWindow(getter_AddRefs(webshellWindow));
nsCOMPtr<nsIDOMDocument> chromeDoc;
webshellWindow->GetDocument(getter_AddRefs(chromeDoc));
if (!chromeDoc) return PR_FALSE;
nsCOMPtr<nsIDOMElement> elem;
nsresult rv = chromeDoc->GetElementById( nsAutoString(nodeID), getter_AddRefs(elem) );
if (NS_FAILED(rv) || !elem) return rv;
return elem->SetAttribute(nsAutoString(attributeName), newValue);
}
//---------------------------------------------------------------------------
static nsresult GetNodeAttribute(nsIEditorShell* aEditorShell, const PRUnichar* nodeID,
const PRUnichar* attributeName, nsString& outValue)
//---------------------------------------------------------------------------
{
nsCOMPtr<nsIDOMWindow> webshellWindow;
aEditorShell->GetWebShellWindow(getter_AddRefs(webshellWindow));
nsCOMPtr<nsIDOMDocument> chromeDoc;
webshellWindow->GetDocument(getter_AddRefs(chromeDoc));
if (!chromeDoc) return PR_FALSE;
nsCOMPtr<nsIDOMElement> elem;
nsresult rv = chromeDoc->GetElementById( nsAutoString(nodeID), getter_AddRefs(elem) );
if (NS_FAILED(rv) || !elem) return rv;
return elem->GetAttribute(nsAutoString(attributeName), outValue);
}
#ifdef XP_MAC
#pragma mark -
#endif
nsBaseStateUpdatingCommand::nsBaseStateUpdatingCommand(const char* aTagName)
: nsBaseCommand()
, mTagName(aTagName)
{
}
nsBaseStateUpdatingCommand::~nsBaseStateUpdatingCommand()
{
}
NS_IMPL_ISUPPORTS_INHERITED1(nsBaseStateUpdatingCommand, nsBaseCommand, nsIStateUpdatingControllerCommand);
NS_IMETHODIMP
nsBaseStateUpdatingCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (!editorShell) return NS_OK;
// check for plain text?
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (!editor) return NS_OK;
*outCmdEnabled = PR_TRUE;
// also udpate the command state
return UpdateCommandState(aCommand, refCon);
}
NS_IMETHODIMP
nsBaseStateUpdatingCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
if (!editorShell) return NS_ERROR_NOT_INITIALIZED;
return ToggleState(editorShell, mTagName);
}
NS_IMETHODIMP
nsBaseStateUpdatingCommand::UpdateCommandState(const PRUnichar *aCommandName, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
PRBool stateIsSet;
rv = GetCurrentState(editorShell, mTagName, stateIsSet);
if (NS_FAILED(rv)) return rv;
PRBool oldState = !stateIsSet;
// what was it last set to?
void* stateData;
if (NS_SUCCEEDED(editorShell->GetCommandStateData(aCommandName, &stateData)))
oldState = (PRBool)stateData;
if (stateIsSet != oldState)
{
// poke the UI
rv = SetNodeAttribute(editorShell, aCommandName, NS_ConvertASCIItoUCS2("state").GetUnicode(),
NS_ConvertASCIItoUCS2(stateIsSet ? "true" : "false"));
editorShell->SetCommandStateData(aCommandName, (void*)stateIsSet);
}
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsAlwaysEnabledCommands::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = (editorShell.get() != nsnull); // enabled if we have an editorShell
return NS_OK;
}
NS_IMETHODIMP
nsAlwaysEnabledCommands::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
if (!editorShell) return NS_ERROR_NULL_POINTER;
nsresult rv = NS_OK;
nsAutoString cmdString(aCommand);
/*
if (cmdString.EqualsWithConversion("cmd_scrollTop"))
return selCont->CompleteScroll(PR_FALSE);
else if (cmdString.EqualsWithConversion("cmd_scrollBottom"))
return selCont->CompleteScroll(PR_TRUE);
*/
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsCloseCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
*outCmdEnabled = PR_TRUE; // check if loading etc?
}
return NS_OK;
}
NS_IMETHODIMP
nsCloseCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
PRBool wasConfirmed;
rv = editorShell->CloseWindow(&wasConfirmed);
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsPrintingCommands::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (!editorShell) return NS_OK;
nsAutoString cmdString(aCommand);
if (cmdString.EqualsWithConversion("cmd_print"))
*outCmdEnabled = PR_TRUE;
else if (cmdString.EqualsWithConversion("cmd_printSetup"))
*outCmdEnabled = PR_FALSE; // not implemented yet
else if (cmdString.EqualsWithConversion("cmd_printPreview"))
*outCmdEnabled = PR_FALSE; // not implemented yet
return NS_OK;
}
NS_IMETHODIMP
nsPrintingCommands::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
if (!editorShell) return NS_ERROR_NULL_POINTER;
nsresult rv = NS_OK;
nsAutoString cmdString(aCommand);
if (cmdString.EqualsWithConversion("cmd_print"))
rv = editorShell->Print();
else if (cmdString.EqualsWithConversion("cmd_printSetup"))
rv = NS_ERROR_NOT_IMPLEMENTED;
else if (cmdString.EqualsWithConversion("cmd_printPreview"))
rv = NS_ERROR_NOT_IMPLEMENTED;
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsSaveCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
PRBool docModified;
if (NS_SUCCEEDED(editorShell->GetDocumentModified(&docModified)))
*outCmdEnabled = docModified;
}
return NS_OK;
}
NS_IMETHODIMP
nsSaveCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
PRBool wasSaved;
rv = editorShell->SaveDocument(PR_FALSE, PR_FALSE, &wasSaved);
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsSaveAsCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = (editorShell.get() != nsnull);
return NS_OK;
}
NS_IMETHODIMP
nsSaveAsCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
PRBool wasSaved;
rv = editorShell->SaveDocument(PR_TRUE, PR_FALSE, &wasSaved);
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsPasteQuotationCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
editor->CanPaste(nsIClipboard::kGlobalClipboard, *outCmdEnabled);
}
return NS_OK;
}
NS_IMETHODIMP
nsPasteQuotationCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
rv = editorShell->PasteAsQuotation(nsIClipboard::kGlobalClipboard);
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
nsStyleUpdatingCommand::nsStyleUpdatingCommand(const char* aTagName)
: nsBaseStateUpdatingCommand(aTagName)
{
}
nsresult
nsStyleUpdatingCommand::GetCurrentState(nsIEditorShell *aEditorShell, const char* aTagName, PRBool& outStyleSet)
{
NS_ASSERTION(aEditorShell, "Need editor shell here");
nsresult rv = NS_OK;
PRBool firstOfSelectionHasProp = PR_FALSE;
PRBool anyOfSelectionHasProp = PR_FALSE;
PRBool allOfSelectionHasProp = PR_FALSE;
nsCOMPtr<nsIEditor> editor;
aEditorShell->GetEditor(getter_AddRefs(editor));
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(editor);
if (!htmlEditor) return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIAtom> styleAtom = getter_AddRefs(NS_NewAtom(aTagName));
rv = htmlEditor->GetInlineProperty(styleAtom, nsnull, nsnull, firstOfSelectionHasProp, anyOfSelectionHasProp, allOfSelectionHasProp);
outStyleSet = allOfSelectionHasProp; // change this to alter the behaviour
return rv;
}
nsresult
nsStyleUpdatingCommand::ToggleState(nsIEditorShell *aEditorShell, const char* aTagName)
{
PRBool styleSet;
nsresult rv = GetCurrentState(aEditorShell, aTagName, styleSet);
if (NS_FAILED(rv)) return rv;
nsAutoString tagName; tagName.AssignWithConversion(aTagName);
if (styleSet)
rv = aEditorShell->RemoveTextProperty(tagName.GetUnicode(), nsnull);
else
rv = aEditorShell->SetTextProperty(tagName.GetUnicode(), nsnull, nsnull);
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
nsListCommand::nsListCommand(const char* aTagName)
: nsBaseStateUpdatingCommand(aTagName)
{
}
nsresult
nsListCommand::GetCurrentState(nsIEditorShell *aEditorShell, const char* aTagName, PRBool& outInList)
{
NS_ASSERTION(aEditorShell, "Need editorShell here");
nsCOMPtr<nsIEditor> editor;
aEditorShell->GetEditor(getter_AddRefs(editor));
if (!editor) return NS_ERROR_UNEXPECTED;
nsCOMPtr<nsIDOMSelection> domSelection;
editor->GetSelection(getter_AddRefs(domSelection));
if (!domSelection) return NS_ERROR_UNEXPECTED;
nsCOMPtr<nsIDOMNode> domNode;
domSelection->GetAnchorNode(getter_AddRefs(domNode));
if (!domNode) return NS_ERROR_UNEXPECTED;
// tagStr will hold the list state when we're done.
nsAutoString tagStr; tagStr.AssignWithConversion("ol");
nsCOMPtr<nsIDOMElement> parentElement;
nsresult rv = aEditorShell->GetElementOrParentByTagName(tagStr.GetUnicode(), domNode, getter_AddRefs(parentElement));
if (NS_FAILED(rv)) return rv;
if (!parentElement)
{
tagStr.AssignWithConversion("ul");
rv = aEditorShell->GetElementOrParentByTagName(tagStr.GetUnicode(), domNode, getter_AddRefs(parentElement));
if (NS_FAILED(rv)) return rv;
if (!parentElement)
tagStr.Truncate();
}
outInList = tagStr.EqualsWithConversion(mTagName);
return NS_OK;
}
nsresult
nsListCommand::ToggleState(nsIEditorShell *aEditorShell, const char* aTagName)
{
PRBool inList;
nsresult rv = GetCurrentState(aEditorShell, aTagName, inList);
if (NS_FAILED(rv)) return rv;
nsAutoString listType; listType.AssignWithConversion(aTagName);
if (inList)
rv = aEditorShell->RemoveList(listType.GetUnicode());
else
rv = aEditorShell->MakeOrChangeList(listType.GetUnicode());
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsIndentCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
*outCmdEnabled = PR_TRUE; // can always indent (I guess)
}
return NS_OK;
}
NS_IMETHODIMP
nsIndentCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
nsAutoString indentStr; indentStr.AssignWithConversion("indent");
rv = editorShell->Indent(indentStr.GetUnicode());
}
return rv;
}
NS_IMETHODIMP
nsOutdentCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
{
// XXX fix me. You can't outdent if you're already at the top level.
//PRBool canOutdent;
//editor->CanIndent("outdent", &canOutdent);
*outCmdEnabled = PR_TRUE;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsOutdentCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
nsAutoString indentStr; indentStr.AssignWithConversion("outdent");
rv = editorShell->Indent(indentStr.GetUnicode());
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsParagraphStateCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
{
*outCmdEnabled = PR_TRUE;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsParagraphStateCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
// we have to grab the state attribute on our command node to find out
// what format to set the paragraph to
nsAutoString stateAttribute;
rv = GetNodeAttribute(editorShell, aCommand, NS_ConvertASCIItoUCS2("state").GetUnicode(), stateAttribute);
if (NS_FAILED(rv)) return rv;
rv = editorShell->SetParagraphFormat(stateAttribute.GetUnicode());
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsAlignCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
{
*outCmdEnabled = PR_TRUE;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsAlignCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
// we have to grab the state attribute on our command node to find out
// what format to set the paragraph to
nsAutoString stateAttribute;
rv = GetNodeAttribute(editorShell, aCommand, NS_ConvertASCIItoUCS2("state").GetUnicode(), stateAttribute);
if (NS_FAILED(rv)) return rv;
rv = editorShell->Align(stateAttribute.GetUnicode());
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsRemoveStylesCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
{
// test if we have any styles?
*outCmdEnabled = PR_TRUE;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsRemoveStylesCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
rv = editorShell->RemoveTextProperty(NS_ConvertASCIItoUCS2("all").GetUnicode(), NS_ConvertASCIItoUCS2("").GetUnicode());
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsIncreaseFontSizeCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
{
// test if we are at the max size?
*outCmdEnabled = PR_TRUE;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsIncreaseFontSizeCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
rv = editorShell->IncreaseFontSize();
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsDecreaseFontSizeCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
{
// test if we are at the min size?
*outCmdEnabled = PR_TRUE;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsDecreaseFontSizeCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
rv = editorShell->DecreaseFontSize();
}
return rv;
}

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

@ -0,0 +1,127 @@
/* -*- 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):
*
*/
#ifndef nsComposerCommands_h_
#define nsComposerCommands_h_
#include "nsEditorCommands.h"
// virtual base class for commands that need to save and update state
class nsBaseStateUpdatingCommand : public nsBaseCommand,
public nsIStateUpdatingControllerCommand
{
public:
nsBaseStateUpdatingCommand(const char* aTagName);
virtual ~nsBaseStateUpdatingCommand();
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSICONTROLLERCOMMAND
NS_DECL_NSISTATEUPDATINGCONTROLLERCOMMAND
protected:
// get the current state (on or off) for this style or block format
virtual nsresult GetCurrentState(nsIEditorShell *aEditorShell, const char* aTagName, PRBool& outStateSet) = 0;
// add/remove the style
virtual nsresult ToggleState(nsIEditorShell *aEditorShell, const char* aTagName) = 0;
protected:
const char* mTagName;
const char* mAttributeName;
};
// shared class for the various style updating commands
class nsStyleUpdatingCommand : public nsBaseStateUpdatingCommand
{
public:
nsStyleUpdatingCommand(const char* aTagName);
protected:
// get the current state (on or off) for this style or block format
virtual nsresult GetCurrentState(nsIEditorShell *aEditorShell, const char* aTagName, PRBool& outStateSet);
// add/remove the style
virtual nsresult ToggleState(nsIEditorShell *aEditorShell, const char* aTagName);
};
class nsListCommand : public nsBaseStateUpdatingCommand
{
public:
nsListCommand(const char* aTagName);
protected:
// get the current state (on or off) for this style or block format
virtual nsresult GetCurrentState(nsIEditorShell *aEditorShell, const char* aTagName, PRBool& outStateSet);
// add/remove the style
virtual nsresult ToggleState(nsIEditorShell *aEditorShell, const char* aTagName);
};
// composer commands
NS_DECL_EDITOR_COMMAND(nsAlwaysEnabledCommands)
NS_DECL_EDITOR_COMMAND(nsCloseCommand)
NS_DECL_EDITOR_COMMAND(nsPrintingCommands)
// Generic commands
// File menu
NS_DECL_EDITOR_COMMAND(nsNewCommands) // handles 'new' anything
NS_DECL_EDITOR_COMMAND(nsSaveCommand)
NS_DECL_EDITOR_COMMAND(nsSaveAsCommand)
// Edit menu
NS_DECL_EDITOR_COMMAND(nsPasteQuotationCommand)
// Block transformations
NS_DECL_EDITOR_COMMAND(nsIndentCommand)
NS_DECL_EDITOR_COMMAND(nsOutdentCommand)
NS_DECL_EDITOR_COMMAND(nsParagraphStateCommand)
NS_DECL_EDITOR_COMMAND(nsAlignCommand)
NS_DECL_EDITOR_COMMAND(nsRemoveStylesCommand)
NS_DECL_EDITOR_COMMAND(nsIncreaseFontSizeCommand)
NS_DECL_EDITOR_COMMAND(nsDecreaseFontSizeCommand)
#endif // nsComposerCommands_h_

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

@ -0,0 +1,773 @@
/* -*- 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 "nsIEditor.h"
#include "nsIHTMLEditor.h"
#include "nsIEditorShell.h"
#include "nsIDOMSelection.h"
#include "nsIDOMNode.h"
#include "nsIDOMElement.h"
#include "nsIDOMWindow.h"
#include "nsIDOMDocument.h"
#include "nsIClipboard.h"
#include "nsXPIDLString.h"
#include "nsComposerCommands.h"
// utility methods
//---------------------------------------------------------------------------
static PRBool XULNodeExists(nsIEditorShell* aEditorShell, const char* nodeID)
//---------------------------------------------------------------------------
{
nsCOMPtr<nsIDOMWindow> webshellWindow;
aEditorShell->GetWebShellWindow(getter_AddRefs(webshellWindow));
nsCOMPtr<nsIDOMDocument> chromeDoc;
webshellWindow->GetDocument(getter_AddRefs(chromeDoc));
if (!chromeDoc) return PR_FALSE;
nsCOMPtr<nsIDOMElement> elem;
nsresult rv = chromeDoc->GetElementById( NS_ConvertASCIItoUCS2(nodeID), getter_AddRefs(elem) );
return NS_SUCCEEDED(rv) && (elem.get() != nsnull);
}
//---------------------------------------------------------------------------
static nsresult SetNodeAttribute(nsIEditorShell* aEditorShell, const PRUnichar* nodeID,
const PRUnichar* attributeName, const nsString& newValue)
//---------------------------------------------------------------------------
{
nsCOMPtr<nsIDOMWindow> webshellWindow;
aEditorShell->GetWebShellWindow(getter_AddRefs(webshellWindow));
nsCOMPtr<nsIDOMDocument> chromeDoc;
webshellWindow->GetDocument(getter_AddRefs(chromeDoc));
if (!chromeDoc) return PR_FALSE;
nsCOMPtr<nsIDOMElement> elem;
nsresult rv = chromeDoc->GetElementById( nsAutoString(nodeID), getter_AddRefs(elem) );
if (NS_FAILED(rv) || !elem) return rv;
return elem->SetAttribute(nsAutoString(attributeName), newValue);
}
//---------------------------------------------------------------------------
static nsresult GetNodeAttribute(nsIEditorShell* aEditorShell, const PRUnichar* nodeID,
const PRUnichar* attributeName, nsString& outValue)
//---------------------------------------------------------------------------
{
nsCOMPtr<nsIDOMWindow> webshellWindow;
aEditorShell->GetWebShellWindow(getter_AddRefs(webshellWindow));
nsCOMPtr<nsIDOMDocument> chromeDoc;
webshellWindow->GetDocument(getter_AddRefs(chromeDoc));
if (!chromeDoc) return PR_FALSE;
nsCOMPtr<nsIDOMElement> elem;
nsresult rv = chromeDoc->GetElementById( nsAutoString(nodeID), getter_AddRefs(elem) );
if (NS_FAILED(rv) || !elem) return rv;
return elem->GetAttribute(nsAutoString(attributeName), outValue);
}
#ifdef XP_MAC
#pragma mark -
#endif
nsBaseStateUpdatingCommand::nsBaseStateUpdatingCommand(const char* aTagName)
: nsBaseCommand()
, mTagName(aTagName)
{
}
nsBaseStateUpdatingCommand::~nsBaseStateUpdatingCommand()
{
}
NS_IMPL_ISUPPORTS_INHERITED1(nsBaseStateUpdatingCommand, nsBaseCommand, nsIStateUpdatingControllerCommand);
NS_IMETHODIMP
nsBaseStateUpdatingCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (!editorShell) return NS_OK;
// check for plain text?
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (!editor) return NS_OK;
*outCmdEnabled = PR_TRUE;
// also udpate the command state
return UpdateCommandState(aCommand, refCon);
}
NS_IMETHODIMP
nsBaseStateUpdatingCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
if (!editorShell) return NS_ERROR_NOT_INITIALIZED;
return ToggleState(editorShell, mTagName);
}
NS_IMETHODIMP
nsBaseStateUpdatingCommand::UpdateCommandState(const PRUnichar *aCommandName, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
PRBool stateIsSet;
rv = GetCurrentState(editorShell, mTagName, stateIsSet);
if (NS_FAILED(rv)) return rv;
PRBool oldState = !stateIsSet;
// what was it last set to?
void* stateData;
if (NS_SUCCEEDED(editorShell->GetCommandStateData(aCommandName, &stateData)))
oldState = (PRBool)stateData;
if (stateIsSet != oldState)
{
// poke the UI
rv = SetNodeAttribute(editorShell, aCommandName, NS_ConvertASCIItoUCS2("state").GetUnicode(),
NS_ConvertASCIItoUCS2(stateIsSet ? "true" : "false"));
editorShell->SetCommandStateData(aCommandName, (void*)stateIsSet);
}
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsAlwaysEnabledCommands::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = (editorShell.get() != nsnull); // enabled if we have an editorShell
return NS_OK;
}
NS_IMETHODIMP
nsAlwaysEnabledCommands::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
if (!editorShell) return NS_ERROR_NULL_POINTER;
nsresult rv = NS_OK;
nsAutoString cmdString(aCommand);
/*
if (cmdString.EqualsWithConversion("cmd_scrollTop"))
return selCont->CompleteScroll(PR_FALSE);
else if (cmdString.EqualsWithConversion("cmd_scrollBottom"))
return selCont->CompleteScroll(PR_TRUE);
*/
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsCloseCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
*outCmdEnabled = PR_TRUE; // check if loading etc?
}
return NS_OK;
}
NS_IMETHODIMP
nsCloseCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
PRBool wasConfirmed;
rv = editorShell->CloseWindow(&wasConfirmed);
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsPrintingCommands::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (!editorShell) return NS_OK;
nsAutoString cmdString(aCommand);
if (cmdString.EqualsWithConversion("cmd_print"))
*outCmdEnabled = PR_TRUE;
else if (cmdString.EqualsWithConversion("cmd_printSetup"))
*outCmdEnabled = PR_FALSE; // not implemented yet
else if (cmdString.EqualsWithConversion("cmd_printPreview"))
*outCmdEnabled = PR_FALSE; // not implemented yet
return NS_OK;
}
NS_IMETHODIMP
nsPrintingCommands::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
if (!editorShell) return NS_ERROR_NULL_POINTER;
nsresult rv = NS_OK;
nsAutoString cmdString(aCommand);
if (cmdString.EqualsWithConversion("cmd_print"))
rv = editorShell->Print();
else if (cmdString.EqualsWithConversion("cmd_printSetup"))
rv = NS_ERROR_NOT_IMPLEMENTED;
else if (cmdString.EqualsWithConversion("cmd_printPreview"))
rv = NS_ERROR_NOT_IMPLEMENTED;
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsSaveCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
PRBool docModified;
if (NS_SUCCEEDED(editorShell->GetDocumentModified(&docModified)))
*outCmdEnabled = docModified;
}
return NS_OK;
}
NS_IMETHODIMP
nsSaveCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
PRBool wasSaved;
rv = editorShell->SaveDocument(PR_FALSE, PR_FALSE, &wasSaved);
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsSaveAsCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = (editorShell.get() != nsnull);
return NS_OK;
}
NS_IMETHODIMP
nsSaveAsCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
PRBool wasSaved;
rv = editorShell->SaveDocument(PR_TRUE, PR_FALSE, &wasSaved);
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsPasteQuotationCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
editor->CanPaste(nsIClipboard::kGlobalClipboard, *outCmdEnabled);
}
return NS_OK;
}
NS_IMETHODIMP
nsPasteQuotationCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
rv = editorShell->PasteAsQuotation(nsIClipboard::kGlobalClipboard);
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
nsStyleUpdatingCommand::nsStyleUpdatingCommand(const char* aTagName)
: nsBaseStateUpdatingCommand(aTagName)
{
}
nsresult
nsStyleUpdatingCommand::GetCurrentState(nsIEditorShell *aEditorShell, const char* aTagName, PRBool& outStyleSet)
{
NS_ASSERTION(aEditorShell, "Need editor shell here");
nsresult rv = NS_OK;
PRBool firstOfSelectionHasProp = PR_FALSE;
PRBool anyOfSelectionHasProp = PR_FALSE;
PRBool allOfSelectionHasProp = PR_FALSE;
nsCOMPtr<nsIEditor> editor;
aEditorShell->GetEditor(getter_AddRefs(editor));
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(editor);
if (!htmlEditor) return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIAtom> styleAtom = getter_AddRefs(NS_NewAtom(aTagName));
rv = htmlEditor->GetInlineProperty(styleAtom, nsnull, nsnull, firstOfSelectionHasProp, anyOfSelectionHasProp, allOfSelectionHasProp);
outStyleSet = allOfSelectionHasProp; // change this to alter the behaviour
return rv;
}
nsresult
nsStyleUpdatingCommand::ToggleState(nsIEditorShell *aEditorShell, const char* aTagName)
{
PRBool styleSet;
nsresult rv = GetCurrentState(aEditorShell, aTagName, styleSet);
if (NS_FAILED(rv)) return rv;
nsAutoString tagName; tagName.AssignWithConversion(aTagName);
if (styleSet)
rv = aEditorShell->RemoveTextProperty(tagName.GetUnicode(), nsnull);
else
rv = aEditorShell->SetTextProperty(tagName.GetUnicode(), nsnull, nsnull);
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
nsListCommand::nsListCommand(const char* aTagName)
: nsBaseStateUpdatingCommand(aTagName)
{
}
nsresult
nsListCommand::GetCurrentState(nsIEditorShell *aEditorShell, const char* aTagName, PRBool& outInList)
{
NS_ASSERTION(aEditorShell, "Need editorShell here");
nsCOMPtr<nsIEditor> editor;
aEditorShell->GetEditor(getter_AddRefs(editor));
if (!editor) return NS_ERROR_UNEXPECTED;
nsCOMPtr<nsIDOMSelection> domSelection;
editor->GetSelection(getter_AddRefs(domSelection));
if (!domSelection) return NS_ERROR_UNEXPECTED;
nsCOMPtr<nsIDOMNode> domNode;
domSelection->GetAnchorNode(getter_AddRefs(domNode));
if (!domNode) return NS_ERROR_UNEXPECTED;
// tagStr will hold the list state when we're done.
nsAutoString tagStr; tagStr.AssignWithConversion("ol");
nsCOMPtr<nsIDOMElement> parentElement;
nsresult rv = aEditorShell->GetElementOrParentByTagName(tagStr.GetUnicode(), domNode, getter_AddRefs(parentElement));
if (NS_FAILED(rv)) return rv;
if (!parentElement)
{
tagStr.AssignWithConversion("ul");
rv = aEditorShell->GetElementOrParentByTagName(tagStr.GetUnicode(), domNode, getter_AddRefs(parentElement));
if (NS_FAILED(rv)) return rv;
if (!parentElement)
tagStr.Truncate();
}
outInList = tagStr.EqualsWithConversion(mTagName);
return NS_OK;
}
nsresult
nsListCommand::ToggleState(nsIEditorShell *aEditorShell, const char* aTagName)
{
PRBool inList;
nsresult rv = GetCurrentState(aEditorShell, aTagName, inList);
if (NS_FAILED(rv)) return rv;
nsAutoString listType; listType.AssignWithConversion(aTagName);
if (inList)
rv = aEditorShell->RemoveList(listType.GetUnicode());
else
rv = aEditorShell->MakeOrChangeList(listType.GetUnicode());
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsIndentCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
*outCmdEnabled = PR_TRUE; // can always indent (I guess)
}
return NS_OK;
}
NS_IMETHODIMP
nsIndentCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
nsAutoString indentStr; indentStr.AssignWithConversion("indent");
rv = editorShell->Indent(indentStr.GetUnicode());
}
return rv;
}
NS_IMETHODIMP
nsOutdentCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
{
// XXX fix me. You can't outdent if you're already at the top level.
//PRBool canOutdent;
//editor->CanIndent("outdent", &canOutdent);
*outCmdEnabled = PR_TRUE;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsOutdentCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
nsAutoString indentStr; indentStr.AssignWithConversion("outdent");
rv = editorShell->Indent(indentStr.GetUnicode());
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsParagraphStateCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
{
*outCmdEnabled = PR_TRUE;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsParagraphStateCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
// we have to grab the state attribute on our command node to find out
// what format to set the paragraph to
nsAutoString stateAttribute;
rv = GetNodeAttribute(editorShell, aCommand, NS_ConvertASCIItoUCS2("state").GetUnicode(), stateAttribute);
if (NS_FAILED(rv)) return rv;
rv = editorShell->SetParagraphFormat(stateAttribute.GetUnicode());
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsAlignCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
{
*outCmdEnabled = PR_TRUE;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsAlignCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
// we have to grab the state attribute on our command node to find out
// what format to set the paragraph to
nsAutoString stateAttribute;
rv = GetNodeAttribute(editorShell, aCommand, NS_ConvertASCIItoUCS2("state").GetUnicode(), stateAttribute);
if (NS_FAILED(rv)) return rv;
rv = editorShell->Align(stateAttribute.GetUnicode());
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsRemoveStylesCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
{
// test if we have any styles?
*outCmdEnabled = PR_TRUE;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsRemoveStylesCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
rv = editorShell->RemoveTextProperty(NS_ConvertASCIItoUCS2("all").GetUnicode(), NS_ConvertASCIItoUCS2("").GetUnicode());
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsIncreaseFontSizeCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
{
// test if we are at the max size?
*outCmdEnabled = PR_TRUE;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsIncreaseFontSizeCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
rv = editorShell->IncreaseFontSize();
}
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsDecreaseFontSizeCommand::IsCommandEnabled(const PRUnichar *aCommand, nsISupports * refCon, PRBool *outCmdEnabled)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
*outCmdEnabled = PR_FALSE;
if (editorShell)
{
nsCOMPtr<nsIEditor> editor;
editorShell->GetEditor(getter_AddRefs(editor));
if (editor)
{
// test if we are at the min size?
*outCmdEnabled = PR_TRUE;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsDecreaseFontSizeCommand::DoCommand(const PRUnichar *aCommand, nsISupports * refCon)
{
nsCOMPtr<nsIEditorShell> editorShell = do_QueryInterface(refCon);
nsresult rv = NS_OK;
if (editorShell)
{
rv = editorShell->DecreaseFontSize();
}
return rv;
}

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

@ -0,0 +1,127 @@
/* -*- 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):
*
*/
#ifndef nsComposerCommands_h_
#define nsComposerCommands_h_
#include "nsEditorCommands.h"
// virtual base class for commands that need to save and update state
class nsBaseStateUpdatingCommand : public nsBaseCommand,
public nsIStateUpdatingControllerCommand
{
public:
nsBaseStateUpdatingCommand(const char* aTagName);
virtual ~nsBaseStateUpdatingCommand();
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSICONTROLLERCOMMAND
NS_DECL_NSISTATEUPDATINGCONTROLLERCOMMAND
protected:
// get the current state (on or off) for this style or block format
virtual nsresult GetCurrentState(nsIEditorShell *aEditorShell, const char* aTagName, PRBool& outStateSet) = 0;
// add/remove the style
virtual nsresult ToggleState(nsIEditorShell *aEditorShell, const char* aTagName) = 0;
protected:
const char* mTagName;
const char* mAttributeName;
};
// shared class for the various style updating commands
class nsStyleUpdatingCommand : public nsBaseStateUpdatingCommand
{
public:
nsStyleUpdatingCommand(const char* aTagName);
protected:
// get the current state (on or off) for this style or block format
virtual nsresult GetCurrentState(nsIEditorShell *aEditorShell, const char* aTagName, PRBool& outStateSet);
// add/remove the style
virtual nsresult ToggleState(nsIEditorShell *aEditorShell, const char* aTagName);
};
class nsListCommand : public nsBaseStateUpdatingCommand
{
public:
nsListCommand(const char* aTagName);
protected:
// get the current state (on or off) for this style or block format
virtual nsresult GetCurrentState(nsIEditorShell *aEditorShell, const char* aTagName, PRBool& outStateSet);
// add/remove the style
virtual nsresult ToggleState(nsIEditorShell *aEditorShell, const char* aTagName);
};
// composer commands
NS_DECL_EDITOR_COMMAND(nsAlwaysEnabledCommands)
NS_DECL_EDITOR_COMMAND(nsCloseCommand)
NS_DECL_EDITOR_COMMAND(nsPrintingCommands)
// Generic commands
// File menu
NS_DECL_EDITOR_COMMAND(nsNewCommands) // handles 'new' anything
NS_DECL_EDITOR_COMMAND(nsSaveCommand)
NS_DECL_EDITOR_COMMAND(nsSaveAsCommand)
// Edit menu
NS_DECL_EDITOR_COMMAND(nsPasteQuotationCommand)
// Block transformations
NS_DECL_EDITOR_COMMAND(nsIndentCommand)
NS_DECL_EDITOR_COMMAND(nsOutdentCommand)
NS_DECL_EDITOR_COMMAND(nsParagraphStateCommand)
NS_DECL_EDITOR_COMMAND(nsAlignCommand)
NS_DECL_EDITOR_COMMAND(nsRemoveStylesCommand)
NS_DECL_EDITOR_COMMAND(nsIncreaseFontSizeCommand)
NS_DECL_EDITOR_COMMAND(nsDecreaseFontSizeCommand)
#endif // nsComposerCommands_h_

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

@ -0,0 +1,606 @@
/* -*- Mode: Java; tab-width: 4; 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.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Simon Fraser (sfraser@netscape.com)
*/
/* Implementations of nsIControllerCommand for composer commands */
var gComposerCommandManager = null;
//-----------------------------------------------------------------------------------
function PrintObject(obj)
{
dump("-----" + obj + "------\n");
var names = "";
for (var i in obj)
{
if (i == "value")
names += i + ": " + obj.value + "\n";
else if (i == "id")
names += i + ": " + obj.id + "\n";
else
names += i + "\n";
}
dump(names + "-----------\n");
}
//-----------------------------------------------------------------------------------
function PrintNodeID(id)
{
PrintObject(document.getElementById(id));
}
//-----------------------------------------------------------------------------------
var nsOpenCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return true; // we can always do this
},
doCommand: function(aCommand)
{
var fp = Components.classes["component://mozilla/filepicker"].createInstance(nsIFilePicker);
fp.init(window, window.editorShell.GetString("OpenHTMLFile"), nsIFilePicker.modeOpen);
// While we include "All", include filters that prefer HTML and Text files
fp.setFilters(nsIFilePicker.filterText | nsIFilePicker.filterHTML | nsIFilePicker.filterAll);
/* doesn't handle *.shtml files */
try {
fp.show();
/* need to handle cancel (uncaught exception at present) */
}
catch (ex) {
dump("filePicker.chooseInputFile threw an exception\n");
}
/* check for already open window and activate it...
* note that we have to test the native path length
* since fileURL.spec will be "file:///" if no filename picked (Cancel button used)
*/
if (fp.file && fp.file.path.length > 0) {
var found = FindAndSelectEditorWindowWithURL(fp.fileURL.spec);
if (!found)
{
// if the existing window is untouched, just load there
if (PageIsEmptyAndUntouched())
{
window.editorShell.LoadUrl(fp.fileURL.spec);
}
else
{
/* open new window */
window.openDialog("chrome://editor/content",
"_blank",
"chrome,dialog=no,all",
fp.fileURL.spec);
}
}
}
}
};
//-----------------------------------------------------------------------------------
var nsNewEditorCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return true; // we can always do this
},
doCommand: function(aCommand)
{
NewEditorWindow();
}
};
//-----------------------------------------------------------------------------------
var nsOpenRemoteCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return true; // we can always do this
},
doCommand: function(aCommand)
{
/* The last parameter is the current browser window.
Use 0 and the default checkbox will be to load into an editor
and loading into existing browser option is removed
*/
window.openDialog( "chrome://navigator/content/openLocation.xul", "_blank", "chrome,modal", 0);
}
};
//-----------------------------------------------------------------------------------
var nsPreviewCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
// maybe disable if we haven't saved?
// return (window.editorShell && !window.editorShell.documentModified);
return (window.editorShell != null);
},
doCommand: function(aCommand)
{
if (!editorShell.CheckAndSaveDocument(editorShell.GetString("BeforePreview")))
return;
var fileurl = "";
try {
fileurl = window.content.location;
} catch (e) {
return;
}
// CheckAndSave doesn't tell us if the user said "Don't Save",
// so make sure we have a url:
if (fileurl != "" && fileurl != "about:blank")
{
window.openDialog(getBrowserURL(), "EditorPreview", "chrome,all,dialog=no", fileurl);
}
}
};
//-----------------------------------------------------------------------------------
var nsFindCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return (window.editorShell != null);
},
doCommand: function(aCommand)
{
window.editorShell.Find();
}
};
//-----------------------------------------------------------------------------------
var nsFindNextCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
// we can only do this if the search pattern is non-empty. Not sure how
// to get that from here
return (window.editorShell != null);
},
doCommand: function(aCommand)
{
window.editorShell.FindNext();
}
};
//-----------------------------------------------------------------------------------
var nsSpellingCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return (window.editorShell != null) && IsSpellCheckerInstalled();
},
doCommand: function(aCommand)
{
var spellChecker = window.editorShell.QueryInterface(Components.interfaces.nsIEditorSpellCheck);
if (spellChecker)
{
// dump("Check Spelling starting...\n");
// Start the spell checker module. Return is first misspelled word
try {
firstMisspelledWord = spellChecker.StartSpellChecking();
}
catch(ex) {
dump("*** Exception error: StartSpellChecking\n");
return;
}
if( firstMisspelledWord == "")
{
try {
spellChecker.CloseSpellChecking();
}
catch(ex) {
dump("*** Exception error: CloseSpellChecking\n");
return;
}
// No misspelled word - tell user
window.editorShell.AlertWithTitle(window.editorShell.GetString("CheckSpelling"),
window.editorShell.GetString("NoMisspelledWord"));
} else {
// Set spellChecker variable on window
window.spellChecker = spellChecker;
try {
window.openDialog("chrome://editor/content/EdSpellCheck.xul", "_blank",
"chrome,close,titlebar,modal", "", firstMisspelledWord);
}
catch(ex) {
dump("*** Exception error: SpellChecker Dialog Closing\n");
return;
}
}
}
contentWindow.focus();
}
};
//-----------------------------------------------------------------------------------
var nsImageCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return (window.editorShell && window.editorShell.documentEditable);
},
doCommand: function(aCommand)
{
window.openDialog("chrome://editor/content/EdImageProps.xul","_blank", "chrome,close,titlebar,modal");
}
};
//-----------------------------------------------------------------------------------
var nsHLineCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return (window.editorShell && window.editorShell.documentEditable);
},
doCommand: function(aCommand)
{
// Inserting an HLine is different in that we don't use properties dialog
// unless we are editing an existing line's attributes
// We get the last-used attributes from the prefs and insert immediately
tagName = "hr";
hLine = window.editorShell.GetSelectedElement(tagName);
if (hLine) {
dump("HLine was found -- opening dialog...!\n");
// We only open the dialog for an existing HRule
window.openDialog("chrome://editor/content/EdHLineProps.xul", "_blank", "chrome,close,titlebar,modal");
} else {
hLine = window.editorShell.CreateElementWithDefaults(tagName);
if (hLine) {
// We change the default attributes to those saved in the user prefs
var prefs = Components.classes['component://netscape/preferences'];
if (prefs) {
prefs = prefs.getService();
}
if (prefs) {
prefs = prefs.QueryInterface(Components.interfaces.nsIPref);
}
if (prefs) {
dump(" We found the Prefs Service\n");
var percent;
var height;
var shading;
var ud = "undefined";
try {
var align = prefs.GetIntPref("editor.hrule.align");
dump("Align pref: "+align+"\n");
if (align == 0 ) {
hLine.setAttribute("align", "left");
} else if (align == 2) {
hLine.setAttribute("align", "right");
} else {
// Default is center
hLine.setAttribute("align", "center");
}
var width = prefs.GetIntPref("editor.hrule.width");
var percent = prefs.GetBoolPref("editor.hrule.width_percent");
dump("Width pref: "+width+", percent:"+percent+"\n");
if (percent)
width = width +"%";
hLine.setAttribute("width", width);
var height = prefs.GetIntPref("editor.hrule.height");
dump("Size pref: "+height+"\n");
hLine.setAttribute("size", String(height));
var shading = prefs.GetBoolPref("editor.hrule.shading");
dump("Shading pref:"+shading+"\n");
if (shading) {
hLine.removeAttribute("noshade");
} else {
hLine.setAttribute("noshade", "");
}
}
catch (ex) {
dump("failed to get HLine prefs\n");
}
}
try {
window.editorShell.InsertElementAtSelection(hLine, true);
} catch (e) {
dump("Exception occured in InsertElementAtSelection\n");
}
}
}
}
};
//-----------------------------------------------------------------------------------
var nsTableCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return (window.editorShell && window.editorShell.documentEditable);
},
doCommand: function(aCommand)
{
EditorInsertOrEditTable(true);
}
};
//-----------------------------------------------------------------------------------
var nsEditTableCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return (window.editorShell && window.editorShell.documentEditable);
},
doCommand: function(aCommand)
{
EditorInsertOrEditTable(false);
}
};
//-----------------------------------------------------------------------------------
var nsLinkCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return (window.editorShell && window.editorShell.documentEditable);
},
doCommand: function(aCommand)
{
window.openDialog("chrome://editor/content/EdLinkProps.xul","_blank", "chrome,close,titlebar,modal");
}
};
//-----------------------------------------------------------------------------------
var nsAnchorCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return (window.editorShell && window.editorShell.documentEditable);
},
doCommand: function(aCommand)
{
window.openDialog("chrome://editor/content/EdNamedAnchorProps.xul", "_blank", "chrome,close,titlebar,modal", "");
}
};
//-----------------------------------------------------------------------------------
var nsInsertHTMLCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return (window.editorShell && window.editorShell.documentEditable);
},
doCommand: function(aCommand)
{
window.openDialog("chrome://editor/content/EdInsSrc.xul","_blank", "chrome,close,titlebar,modal,resizeable", "");
}
};
//-----------------------------------------------------------------------------------
var nsInsertCharsCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return (window.editorShell && window.editorShell.documentEditable);
},
doCommand: function(aCommand)
{
window.openDialog("chrome://editor/content/EdInsertChars.xul", "_blank", "chrome,close,titlebar", "");
}
};
//-----------------------------------------------------------------------------------
var nsInsertBreakCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return false;
},
doCommand: function(aCommand)
{
_EditorNotImplemented();
}
};
//-----------------------------------------------------------------------------------
var nsInsertBreakAllCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return false;
},
doCommand: function(aCommand)
{
_EditorNotImplemented();
}
};
//-----------------------------------------------------------------------------------
var nsListPropertiesCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return (window.editorShell && window.editorShell.documentEditable);
},
doCommand: function(aCommand)
{
window.openDialog("chrome://editor/content/EdListProps.xul","_blank", "chrome,close,titlebar,modal");
}
};
//-----------------------------------------------------------------------------------
var nsPagePropertiesCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return (window.editorShell && window.editorShell.documentEditable);
},
doCommand: function(aCommand)
{
window.openDialog("chrome://editor/content/EdPageProps.xul","_blank", "chrome,close,titlebar,modal,resizable", "");
}
};
//-----------------------------------------------------------------------------------
var nsColorPropertiesCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return (window.editorShell && window.editorShell.documentEditable);
},
doCommand: function(aCommand)
{
window.openDialog("chrome://editor/content/EdColorProps.xul","_blank", "chrome,close,titlebar,modal", "");
}
};
//-----------------------------------------------------------------------------------
var nsEditHTMLCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return false;
},
doCommand: function(aCommand)
{
_EditorNotImplemented();
}
};
//-----------------------------------------------------------------------------------
var nsPreferencesCommand =
{
isCommandEnabled: function(aCommand, dummy)
{
return true;
},
doCommand: function(aCommand)
{
goPreferences('navigator.xul', 'chrome://communicator/content/pref/pref-composer.xul','editor');
}
};
//-----------------------------------------------------------------------------------
function GetComposerController()
{
var numControllers = window.content.controllers.getControllerCount();
// count down to find a controller that supplies a nsIControllerCommandManager interface
for (var i = numControllers; i >= 0 ; i --)
{
var commandManager = null;
try {
var controller = window.content.controllers.getControllerAt(i);
var interfaceRequestor = controller.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
if (!interfaceRequestor) continue;
commandManager = interfaceRequestor.getInterface(Components.interfaces.nsIControllerCommandManager);
} catch(ex) {
}
if (commandManager)
return commandManager;
}
dump("Failed to find a controller to register commands with\n");
return null;
}
//-----------------------------------------------------------------------------------
function SetupControllerCommands()
{
gComposerCommandManager = GetComposerController();
if (!gComposerCommandManager)
return;
dump("Registering commands\n");
gComposerCommandManager.registerCommand("cmd_newEditor", nsNewEditorCommand);
gComposerCommandManager.registerCommand("cmd_open", nsOpenCommand);
gComposerCommandManager.registerCommand("cmd_openRemote", nsOpenRemoteCommand);
gComposerCommandManager.registerCommand("cmd_preview", nsPreviewCommand);
gComposerCommandManager.registerCommand("cmd_find", nsFindCommand);
gComposerCommandManager.registerCommand("cmd_findNext", nsFindNextCommand);
gComposerCommandManager.registerCommand("cmd_spelling", nsSpellingCommand);
gComposerCommandManager.registerCommand("cmd_editHTML", nsEditHTMLCommand);
gComposerCommandManager.registerCommand("cmd_preferences", nsPreferencesCommand);
gComposerCommandManager.registerCommand("cmd_listProperties", nsListPropertiesCommand);
gComposerCommandManager.registerCommand("cmd_pageProperties", nsPagePropertiesCommand);
gComposerCommandManager.registerCommand("cmd_colorProperties", nsColorPropertiesCommand);
gComposerCommandManager.registerCommand("cmd_image", nsImageCommand);
gComposerCommandManager.registerCommand("cmd_hline", nsHLineCommand);
gComposerCommandManager.registerCommand("cmd_table", nsTableCommand); // insert or edit
gComposerCommandManager.registerCommand("cmd_editTable", nsEditTableCommand); // edit
gComposerCommandManager.registerCommand("cmd_link", nsLinkCommand);
gComposerCommandManager.registerCommand("cmd_anchor", nsAnchorCommand);
gComposerCommandManager.registerCommand("cmd_insertHTML", nsInsertHTMLCommand);
gComposerCommandManager.registerCommand("cmd_insertBreak", nsInsertBreakCommand);
gComposerCommandManager.registerCommand("cmd_insertBreakAll", nsInsertBreakAllCommand);
}
//-----------------------------------------------------------------------------------
function goUpdateComposerMenuItems(commandset)
{
// dump("Updating commands for " + commandset.id + "\n");
for (var i = 0; i < commandset.childNodes.length; i++)
{
var commandID = commandset.childNodes[i].getAttribute("id");
if (commandID)
{
goUpdateCommand(commandID);
}
}
}