2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsComposerCommandsUpdater.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsISelection.h"
|
|
|
|
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
#include "nsICommandManager.h"
|
|
|
|
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsITransactionManager.h"
|
|
|
|
|
|
|
|
nsComposerCommandsUpdater::nsComposerCommandsUpdater()
|
2009-01-14 04:24:10 -08:00
|
|
|
: mDirtyState(eStateUninitialized)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mSelectionCollapsed(eStateUninitialized)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mFirstDoOfFirstUndo(true)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsComposerCommandsUpdater::~nsComposerCommandsUpdater()
|
|
|
|
{
|
2009-01-14 04:24:10 -08:00
|
|
|
// cancel any outstanding update timer
|
|
|
|
if (mUpdateTimer)
|
|
|
|
{
|
|
|
|
mUpdateTimer->Cancel();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS4(nsComposerCommandsUpdater, nsISelectionListener,
|
|
|
|
nsIDocumentStateListener, nsITransactionListener, nsITimerCallback)
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::NotifyDocumentCreated()
|
|
|
|
{
|
|
|
|
// Trigger an nsIObserve notification that the document has been created
|
|
|
|
UpdateOneCommand("obs_documentCreated");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::NotifyDocumentWillBeDestroyed()
|
|
|
|
{
|
|
|
|
// cancel any outstanding update timer
|
|
|
|
if (mUpdateTimer)
|
|
|
|
{
|
|
|
|
mUpdateTimer->Cancel();
|
|
|
|
mUpdateTimer = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't call this right now; it is too late in some cases and the window
|
|
|
|
// is already partially destructed (e.g. JS objects may be gone).
|
|
|
|
#if 0
|
|
|
|
// Trigger an nsIObserve notification that the document will be destroyed
|
|
|
|
UpdateOneCommand("obs_documentWillBeDestroyed");
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsComposerCommandsUpdater::NotifyDocumentStateChanged(bool aNowDirty)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// update document modified. We should have some other notifications for this too.
|
|
|
|
return UpdateDirtyState(aNowDirty);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::NotifySelectionChanged(nsIDOMDocument *,
|
|
|
|
nsISelection *, PRInt16)
|
|
|
|
{
|
|
|
|
return PrimeUpdateTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::WillDo(nsITransactionManager *aManager,
|
2011-09-28 23:19:26 -07:00
|
|
|
nsITransaction *aTransaction, bool *aInterrupt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aInterrupt = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::DidDo(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTransaction, nsresult aDoResult)
|
|
|
|
{
|
|
|
|
// only need to update if the status of the Undo menu item changes.
|
|
|
|
PRInt32 undoCount;
|
|
|
|
aManager->GetNumberOfUndoItems(&undoCount);
|
|
|
|
if (undoCount == 1)
|
|
|
|
{
|
|
|
|
if (mFirstDoOfFirstUndo)
|
|
|
|
UpdateCommandGroup(NS_LITERAL_STRING("undo"));
|
2011-10-17 07:59:28 -07:00
|
|
|
mFirstDoOfFirstUndo = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::WillUndo(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTransaction,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aInterrupt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aInterrupt = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::DidUndo(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTransaction,
|
|
|
|
nsresult aUndoResult)
|
|
|
|
{
|
|
|
|
PRInt32 undoCount;
|
|
|
|
aManager->GetNumberOfUndoItems(&undoCount);
|
|
|
|
if (undoCount == 0)
|
2011-10-17 07:59:28 -07:00
|
|
|
mFirstDoOfFirstUndo = true; // reset the state for the next do
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
UpdateCommandGroup(NS_LITERAL_STRING("undo"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::WillRedo(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTransaction,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aInterrupt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aInterrupt = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::DidRedo(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTransaction,
|
|
|
|
nsresult aRedoResult)
|
|
|
|
{
|
|
|
|
UpdateCommandGroup(NS_LITERAL_STRING("undo"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::WillBeginBatch(nsITransactionManager *aManager,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aInterrupt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aInterrupt = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::DidBeginBatch(nsITransactionManager *aManager,
|
|
|
|
nsresult aResult)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::WillEndBatch(nsITransactionManager *aManager,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aInterrupt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aInterrupt = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::DidEndBatch(nsITransactionManager *aManager,
|
|
|
|
nsresult aResult)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::WillMerge(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTopTransaction,
|
|
|
|
nsITransaction *aTransactionToMerge,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aInterrupt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aInterrupt = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsComposerCommandsUpdater::DidMerge(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTopTransaction,
|
|
|
|
nsITransaction *aTransactionToMerge,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aDidMerge, nsresult aMergeResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsComposerCommandsUpdater::Init(nsIDOMWindow* aDOMWindow)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aDOMWindow);
|
2009-01-14 04:24:10 -08:00
|
|
|
mDOMWindow = do_GetWeakReference(aDOMWindow);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aDOMWindow));
|
|
|
|
if (window)
|
|
|
|
{
|
2007-05-27 13:57:13 -07:00
|
|
|
mDocShell = do_GetWeakReference(window->GetDocShell());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsComposerCommandsUpdater::PrimeUpdateTimer()
|
|
|
|
{
|
|
|
|
if (!mUpdateTimer)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
mUpdateTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const PRUint32 kUpdateTimerDelay = 150;
|
2007-07-08 00:08:04 -07:00
|
|
|
return mUpdateTimer->InitWithCallback(static_cast<nsITimerCallback*>(this),
|
2007-03-22 10:30:00 -07:00
|
|
|
kUpdateTimerDelay,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nsComposerCommandsUpdater::TimerCallback()
|
|
|
|
{
|
|
|
|
// if the selection state has changed, update stuff
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isCollapsed = SelectionIsCollapsed();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (isCollapsed != mSelectionCollapsed)
|
|
|
|
{
|
|
|
|
UpdateCommandGroup(NS_LITERAL_STRING("select"));
|
|
|
|
mSelectionCollapsed = isCollapsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// isn't this redundant with the UpdateCommandGroup above?
|
|
|
|
// can we just nuke the above call? or create a meta command group?
|
|
|
|
UpdateCommandGroup(NS_LITERAL_STRING("style"));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsComposerCommandsUpdater::UpdateDirtyState(bool aNowDirty)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mDirtyState != aNowDirty)
|
|
|
|
{
|
|
|
|
UpdateCommandGroup(NS_LITERAL_STRING("save"));
|
|
|
|
UpdateCommandGroup(NS_LITERAL_STRING("undo"));
|
|
|
|
mDirtyState = aNowDirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsComposerCommandsUpdater::UpdateCommandGroup(const nsAString& aCommandGroup)
|
|
|
|
{
|
2007-05-27 13:57:13 -07:00
|
|
|
nsCOMPtr<nsPICommandUpdater> commandUpdater = GetCommandUpdater();
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(commandUpdater, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
// This hardcoded list of commands is temporary.
|
|
|
|
// This code should use nsIControllerCommandGroup.
|
|
|
|
if (aCommandGroup.EqualsLiteral("undo"))
|
|
|
|
{
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_undo");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_redo");
|
|
|
|
}
|
|
|
|
else if (aCommandGroup.EqualsLiteral("select") ||
|
|
|
|
aCommandGroup.EqualsLiteral("style"))
|
|
|
|
{
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_bold");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_italic");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_underline");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_tt");
|
|
|
|
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_strikethrough");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_superscript");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_subscript");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_nobreak");
|
|
|
|
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_em");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_strong");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_cite");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_abbr");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_acronym");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_code");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_samp");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_var");
|
|
|
|
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_increaseFont");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_decreaseFont");
|
|
|
|
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_paragraphState");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_fontFace");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_fontColor");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_backgroundColor");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_highlight");
|
|
|
|
}
|
|
|
|
else if (aCommandGroup.EqualsLiteral("save"))
|
|
|
|
{
|
|
|
|
// save commands (most are not in C++)
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_setDocumentModified");
|
|
|
|
commandUpdater->CommandStatusChanged("cmd_save");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsComposerCommandsUpdater::UpdateOneCommand(const char *aCommand)
|
|
|
|
{
|
2007-05-27 13:57:13 -07:00
|
|
|
nsCOMPtr<nsPICommandUpdater> commandUpdater = GetCommandUpdater();
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(commandUpdater, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
commandUpdater->CommandStatusChanged(aCommand);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsComposerCommandsUpdater::SelectionIsCollapsed()
|
|
|
|
{
|
2009-01-14 04:24:10 -08:00
|
|
|
nsCOMPtr<nsIDOMWindow> domWindow = do_QueryReferent(mDOMWindow);
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_TRUE(domWindow, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> domSelection;
|
2009-01-14 04:24:10 -08:00
|
|
|
if (NS_SUCCEEDED(domWindow->GetSelection(getter_AddRefs(domSelection))) && domSelection)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool selectionCollapsed = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
domSelection->GetIsCollapsed(&selectionCollapsed);
|
|
|
|
return selectionCollapsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("nsComposerCommandsUpdater::SelectionIsCollapsed - no domSelection");
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-05-27 13:57:13 -07:00
|
|
|
already_AddRefed<nsPICommandUpdater>
|
|
|
|
nsComposerCommandsUpdater::GetCommandUpdater()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocShell> docShell = do_QueryReferent(mDocShell);
|
|
|
|
NS_ENSURE_TRUE(docShell, nsnull);
|
|
|
|
nsCOMPtr<nsICommandManager> manager = do_GetInterface(docShell);
|
|
|
|
nsCOMPtr<nsPICommandUpdater> updater = do_QueryInterface(manager);
|
|
|
|
nsPICommandUpdater* retVal = nsnull;
|
|
|
|
updater.swap(retVal);
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if 0
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsComposerCommandsUpdater::Notify(nsITimer *timer)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(timer == mUpdateTimer.get(), "Hey, this ain't my timer!");
|
|
|
|
TimerCallback();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewComposerCommandsUpdater(nsISelectionListener** aInstancePtrResult)
|
|
|
|
{
|
|
|
|
nsComposerCommandsUpdater* newThang = new nsComposerCommandsUpdater;
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(newThang, NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return newThang->QueryInterface(NS_GET_IID(nsISelectionListener),
|
|
|
|
(void **)aInstancePtrResult);
|
|
|
|
}
|