split nsComposerCommands.cpp to separate out document commands from content commands; add commands to set "useCSS" flag and readonly flag (bugs 184718, 184719), r=mkaply, sr=sfraser

This commit is contained in:
brade%netscape.com 2003-01-16 14:59:09 +00:00
Родитель fa159104bb
Коммит 4fd79914da
3 изменённых файлов: 312 добавлений и 218 удалений

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

@ -65,6 +65,7 @@ CPPSRCS = \
nsEditorParserObserver.cpp \
nsComposerController.cpp \
nsComposerCommands.cpp \
nsComposerDocumentCommands.cpp \
nsComposerRegistration.cpp \
nsEditingSession.cpp \
nsComposerCommandsUpdater.cpp \

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

@ -46,17 +46,17 @@
#include "nsIEditingSession.h"
#include "nsIDOMElement.h"
#include "nsIDOMWindowInternal.h"
#include "nsIDOMDocument.h"
#include "nsIDocument.h"
#include "nsIURI.h"
//#include "nsIDOMWindowInternal.h"
//#include "nsIDOMDocument.h"
//#include "nsIDocument.h"
//#include "nsIURI.h"
#include "nsIClipboard.h"
#include "nsCOMPtr.h"
#include "nsComposerCommands.h"
#include "nsIEditorMailSupport.h"
//#include "nsIEditorMailSupport.h"
#include "nsReadableUtils.h"
#include "nsICommandParams.h"
#include "nsComponentManagerUtils.h"
@ -1403,219 +1403,6 @@ nsInsertTagCommand::GetCommandStateParams(const char *aCommandName,
#pragma mark -
#endif
/**
* Commands for document state that may be changed via doCommandParams
* As of 11/11/02, this is just "cmd_setDocumentModified"
* Note that you can use the same command class, nsSetDocumentStateCommand,
* for more than one of this type of command
* We check the input command param for different behavior
*/
NS_IMETHODIMP
nsSetDocumentStateCommand::IsCommandEnabled(const char * aCommandName,
nsISupports *refCon,
PRBool *outCmdEnabled)
{
NS_ENSURE_ARG_POINTER(outCmdEnabled);
nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
*outCmdEnabled = editor ? PR_TRUE : PR_FALSE;
return NS_OK;
}
NS_IMETHODIMP
nsSetDocumentStateCommand::DoCommand(const char *aCommandName,
nsISupports *refCon)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsSetDocumentStateCommand::DoCommandParams(const char *aCommandName,
nsICommandParams *aParams,
nsISupports *refCon)
{
nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
if (!editor)
return NS_ERROR_INVALID_ARG;
if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentModified"))
{
NS_ENSURE_ARG_POINTER(aParams);
PRBool modified;
nsresult rv = aParams->GetBooleanValue(STATE_ATTRIBUTE, &modified);
// Should we fail if this param wasn't set?
// I'm not sure we should be that strict
if (NS_FAILED(rv))
return NS_OK;
if (modified)
return editor->IncrementModificationCount(1);
else
return editor->ResetModificationCount();
}
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsSetDocumentStateCommand::GetCommandStateParams(const char *aCommandName,
nsICommandParams *aParams,
nsISupports *refCon)
{
NS_ENSURE_ARG_POINTER(aParams);
NS_ENSURE_ARG_POINTER(refCon);
// The base editor owns most state info
nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
if (!editor)
return NS_ERROR_INVALID_ARG;
// Always get the enabled state
PRBool outCmdEnabled = PR_FALSE;
IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
nsresult rv = aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
NS_ENSURE_SUCCESS(rv, rv);
if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentModified"))
{
PRBool modified;
rv = editor->GetDocumentModified(&modified);
NS_ENSURE_SUCCESS(rv, rv);
return aParams->SetBooleanValue(STATE_ATTRIBUTE, modified);
}
return NS_ERROR_NOT_IMPLEMENTED;
}
/**
* Commands just for state notification
* As of 11/21/02, possible commands are:
* "obs_documentCreated"
* "obs_documentWillBeDestroyed"
* "obs_documentLocationChanged"
* Note that you can use the same command class, nsDocumentStateCommand
* for these or future observer commands.
* We check the input command param for different behavior
*
* How to use:
* 1. Get the nsICommandManager for the current editor
* 2. Implement an nsIObserve object, e.g:
*
* void Observe(
* in nsISupports aSubject, // The nsICommandManager calling this Observer
* in string aTopic, // command name, e.g.:"obs_documentCreated"
* // or "obs_documentWillBeDestroyed"
in wstring aData ); // ignored (set to "command_status_changed")
*
* 3. Add the observer by:
* commandManager.addObserver(observeobject, obs_documentCreated);
* 4. In the appropriate location in editorSession, editor, or commands code,
* trigger the notification of this observer by something like:
*
* nsCOMPtr<nsICommandManager> commandManager = do_GetInterface(mDocShell);
* nsCOMPtr<nsPICommandUpdater> commandUpdater = do_QueryInterface(commandManager);
* if (!commandUpdater) return NS_ERROR_FAILURE;
* commandUpdater->CommandStatusChanged(obs_documentCreated);
*
* 5. Use GetCommandStateParams() to obtain state information
* e.g., any creation state codes when creating an editor are
* supplied for "obs_documentCreated" command in the
* "state_data" param's value
*
*/
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsDocumentStateCommand::IsCommandEnabled(const char* aCommandName,
nsISupports *refCon,
PRBool *outCmdEnabled)
{
NS_ENSURE_ARG_POINTER(outCmdEnabled);
// Always return false to discourage callers from using DoCommand()
*outCmdEnabled = PR_FALSE;
return NS_OK;
}
NS_IMETHODIMP
nsDocumentStateCommand::DoCommand(const char *aCommandName,
nsISupports *refCon)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsDocumentStateCommand::DoCommandParams(const char *aCommandName,
nsICommandParams *aParams,
nsISupports *refCon)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsDocumentStateCommand::GetCommandStateParams(const char *aCommandName,
nsICommandParams *aParams,
nsISupports *refCon)
{
NS_ENSURE_ARG_POINTER(aParams);
NS_ENSURE_ARG_POINTER(aCommandName);
nsCOMPtr<nsIEditingSession> editingSession = do_QueryInterface(refCon);
nsresult rv;
if (!nsCRT::strcmp(aCommandName, "obs_documentCreated"))
{
PRUint32 editorStatus = nsIEditingSession::eEditorErrorUnknown;
if (editingSession)
{
// refCon is initially set to nsIEditingSession until editor
// is successfully created and source doc is loaded
// Embedder gets error status if this fails
// If called before startup is finished,
// status = eEditorCreationInProgress
rv = editingSession->GetEditorStatus(&editorStatus);
NS_ENSURE_SUCCESS(rv, rv);
}
else
{
// If refCon is an editor, then everything started up OK!
nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
if (editor)
editorStatus = nsIEditingSession::eEditorOK;
}
// Note that if refCon is not-null, but is neither
// an nsIEditingSession or nsIEditor, we return "eEditorErrorUnknown"
aParams->SetLongValue(STATE_DATA, editorStatus);
return NS_OK;
}
else if (!nsCRT::strcmp(aCommandName, "obs_documentLocationChanged"))
{
nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
if (editor)
{
nsCOMPtr<nsIDOMDocument> domDoc;
editor->GetDocument(getter_AddRefs(domDoc));
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
if (!doc) return NS_ERROR_FAILURE;
nsCOMPtr<nsIURI> uri;
rv = doc->GetDocumentURL(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
if (!uri) return NS_ERROR_FAILURE;
return aParams->SetISupportsValue(STATE_DATA, (nsISupports*)uri);
}
return NS_OK;
}
return NS_ERROR_NOT_IMPLEMENTED;
}
/****************************/
//HELPER METHODS
/****************************/

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

@ -0,0 +1,306 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* 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 the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Ryan Cassin <rcassin@supernova.org>
* Daniel Glazman <glazman@netscape.com>
* Charles Manske <cmanske@netscape.com>
* Kathleen Brade <brade@netscape.com>
*
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsIEditor.h"
#include "nsIEditingSession.h"
#include "nsIPlaintextEditor.h"
#include "nsIHTMLEditor.h"
#include "nsIDOMDocument.h"
#include "nsIDocument.h"
#include "nsIURI.h"
#include "nsCOMPtr.h"
#include "nsComposerCommands.h"
#include "nsICommandParams.h"
#include "nsCRT.h"
//defines
#define STATE_ENABLED "state_enabled"
#define STATE_ATTRIBUTE "state_attribute"
#define STATE_DATA "state_data"
/**
* Commands for document state that may be changed via doCommandParams
* As of 11/11/02, this is just "cmd_setDocumentModified"
* Note that you can use the same command class, nsSetDocumentStateCommand,
* for more than one of this type of command
* We check the input command param for different behavior
*/
NS_IMETHODIMP
nsSetDocumentStateCommand::IsCommandEnabled(const char * aCommandName,
nsISupports *refCon,
PRBool *outCmdEnabled)
{
NS_ENSURE_ARG_POINTER(outCmdEnabled);
nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
*outCmdEnabled = editor ? PR_TRUE : PR_FALSE;
return NS_OK;
}
NS_IMETHODIMP
nsSetDocumentStateCommand::DoCommand(const char *aCommandName,
nsISupports *refCon)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsSetDocumentStateCommand::DoCommandParams(const char *aCommandName,
nsICommandParams *aParams,
nsISupports *refCon)
{
nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
if (!editor)
return NS_ERROR_INVALID_ARG;
if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentModified"))
{
NS_ENSURE_ARG_POINTER(aParams);
PRBool modified;
nsresult rv = aParams->GetBooleanValue(STATE_ATTRIBUTE, &modified);
// Should we fail if this param wasn't set?
// I'm not sure we should be that strict
if (NS_FAILED(rv))
return rv;
if (modified)
return editor->IncrementModificationCount(1);
else
return editor->ResetModificationCount();
}
else if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentReadOnly"))
{
NS_ENSURE_ARG_POINTER(aParams);
PRBool isReadOnly;
nsresult rvRO = aParams->GetBooleanValue(STATE_ATTRIBUTE, &isReadOnly);
if (NS_FAILED(rvRO))
return rvRO;
PRUint32 flags;
editor->GetFlags(&flags);
if (isReadOnly)
flags |= nsIPlaintextEditor::eEditorReadonlyMask;
else
flags &= ~(nsIPlaintextEditor::eEditorReadonlyMask);
return editor->SetFlags(flags);
}
else if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentUseCSS"))
{
NS_ENSURE_ARG_POINTER(aParams);
nsCOMPtr<nsIHTMLEditor> htmleditor = do_QueryInterface(refCon);
if (!htmleditor)
return NS_ERROR_INVALID_ARG;
PRBool desireCSS;
nsresult rvCSS = aParams->GetBooleanValue(STATE_ATTRIBUTE, &desireCSS);
if (NS_FAILED(rvCSS))
return rvCSS;
return htmleditor->SetIsCSSEnabled(desireCSS);
}
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsSetDocumentStateCommand::GetCommandStateParams(const char *aCommandName,
nsICommandParams *aParams,
nsISupports *refCon)
{
NS_ENSURE_ARG_POINTER(aParams);
NS_ENSURE_ARG_POINTER(refCon);
// The base editor owns most state info
nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
if (!editor)
return NS_ERROR_INVALID_ARG;
// Always get the enabled state
PRBool outCmdEnabled = PR_FALSE;
IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
nsresult rv = aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
NS_ENSURE_SUCCESS(rv, rv);
if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentModified"))
{
PRBool modified;
rv = editor->GetDocumentModified(&modified);
NS_ENSURE_SUCCESS(rv, rv);
return aParams->SetBooleanValue(STATE_ATTRIBUTE, modified);
}
return NS_ERROR_NOT_IMPLEMENTED;
}
/**
* Commands just for state notification
* As of 11/21/02, possible commands are:
* "obs_documentCreated"
* "obs_documentWillBeDestroyed"
* "obs_documentLocationChanged"
* Note that you can use the same command class, nsDocumentStateCommand
* for these or future observer commands.
* We check the input command param for different behavior
*
* How to use:
* 1. Get the nsICommandManager for the current editor
* 2. Implement an nsIObserve object, e.g:
*
* void Observe(
* in nsISupports aSubject, // The nsICommandManager calling this Observer
* in string aTopic, // command name, e.g.:"obs_documentCreated"
* // or "obs_documentWillBeDestroyed"
in wstring aData ); // ignored (set to "command_status_changed")
*
* 3. Add the observer by:
* commandManager.addObserver(observeobject, obs_documentCreated);
* 4. In the appropriate location in editorSession, editor, or commands code,
* trigger the notification of this observer by something like:
*
* nsCOMPtr<nsICommandManager> commandManager = do_GetInterface(mDocShell);
* nsCOMPtr<nsPICommandUpdater> commandUpdater = do_QueryInterface(commandManager);
* if (!commandUpdater) return NS_ERROR_FAILURE;
* commandUpdater->CommandStatusChanged(obs_documentCreated);
*
* 5. Use GetCommandStateParams() to obtain state information
* e.g., any creation state codes when creating an editor are
* supplied for "obs_documentCreated" command in the
* "state_data" param's value
*
*/
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsDocumentStateCommand::IsCommandEnabled(const char* aCommandName,
nsISupports *refCon,
PRBool *outCmdEnabled)
{
NS_ENSURE_ARG_POINTER(outCmdEnabled);
// Always return false to discourage callers from using DoCommand()
*outCmdEnabled = PR_FALSE;
return NS_OK;
}
NS_IMETHODIMP
nsDocumentStateCommand::DoCommand(const char *aCommandName,
nsISupports *refCon)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsDocumentStateCommand::DoCommandParams(const char *aCommandName,
nsICommandParams *aParams,
nsISupports *refCon)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsDocumentStateCommand::GetCommandStateParams(const char *aCommandName,
nsICommandParams *aParams,
nsISupports *refCon)
{
NS_ENSURE_ARG_POINTER(aParams);
NS_ENSURE_ARG_POINTER(aCommandName);
nsresult rv;
if (!nsCRT::strcmp(aCommandName, "obs_documentCreated"))
{
PRUint32 editorStatus = nsIEditingSession::eEditorErrorUnknown;
nsCOMPtr<nsIEditingSession> editingSession = do_QueryInterface(refCon);
if (editingSession)
{
// refCon is initially set to nsIEditingSession until editor
// is successfully created and source doc is loaded
// Embedder gets error status if this fails
// If called before startup is finished,
// status = eEditorCreationInProgress
rv = editingSession->GetEditorStatus(&editorStatus);
NS_ENSURE_SUCCESS(rv, rv);
}
else
{
// If refCon is an editor, then everything started up OK!
nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
if (editor)
editorStatus = nsIEditingSession::eEditorOK;
}
// Note that if refCon is not-null, but is neither
// an nsIEditingSession or nsIEditor, we return "eEditorErrorUnknown"
aParams->SetLongValue(STATE_DATA, editorStatus);
return NS_OK;
}
else if (!nsCRT::strcmp(aCommandName, "obs_documentLocationChanged"))
{
nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
if (editor)
{
nsCOMPtr<nsIDOMDocument> domDoc;
editor->GetDocument(getter_AddRefs(domDoc));
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
if (!doc) return NS_ERROR_FAILURE;
nsCOMPtr<nsIURI> uri;
rv = doc->GetDocumentURL(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
if (!uri) return NS_ERROR_FAILURE;
return aParams->SetISupportsValue(STATE_DATA, (nsISupports*)uri);
}
return NS_OK;
}
return NS_ERROR_NOT_IMPLEMENTED;
}