1999-07-28 02:49:12 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* 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/
|
1999-07-28 02:49:12 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* 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.
|
1999-07-28 02:49:12 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-07-28 02:49:12 +00:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 03:43:54 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2000-01-11 20:49:15 +00:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
1999-07-28 02:49:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsVoidArray.h"
|
|
|
|
|
|
|
|
#include "nsIContentViewer.h"
|
|
|
|
#include "nsIDocumentViewer.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDiskDocument.h"
|
|
|
|
#include "nsIDOMElement.h"
|
2000-09-14 11:45:01 +00:00
|
|
|
#include "nsISelection.h"
|
1999-08-26 03:22:03 +00:00
|
|
|
#include "nsIDOMAttr.h"
|
2000-02-16 01:38:02 +00:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
2000-09-01 01:54:35 +00:00
|
|
|
#include "nsIDOMWindowInternal.h"
|
2000-03-03 23:35:11 +00:00
|
|
|
#include "nsITimer.h"
|
1999-07-28 02:49:12 +00:00
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
#include "nsIEditor.h"
|
1999-07-28 02:49:12 +00:00
|
|
|
#include "nsIHTMLEditor.h"
|
2000-02-16 01:38:02 +00:00
|
|
|
#include "nsITransactionManager.h"
|
1999-07-28 02:49:12 +00:00
|
|
|
|
|
|
|
#include "nsInterfaceState.h"
|
|
|
|
|
|
|
|
nsInterfaceState::nsInterfaceState()
|
|
|
|
: mEditor(nsnull)
|
2000-01-18 21:50:15 +00:00
|
|
|
, mChromeDoc(nsnull)
|
2000-02-16 01:38:02 +00:00
|
|
|
, mDOMWindow(nsnull)
|
1999-07-28 02:49:12 +00:00
|
|
|
, mDirtyState(eStateUninitialized)
|
2000-01-18 21:50:15 +00:00
|
|
|
, mSelectionCollapsed(eStateUninitialized)
|
2000-02-16 01:38:02 +00:00
|
|
|
, mFirstDoOfFirstUndo(PR_TRUE)
|
1999-07-28 02:49:12 +00:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsInterfaceState::~nsInterfaceState()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsInterfaceState);
|
|
|
|
NS_IMPL_RELEASE(nsInterfaceState);
|
2000-09-14 11:45:01 +00:00
|
|
|
NS_IMPL_QUERY_INTERFACE4(nsInterfaceState, nsISelectionListener, nsIDocumentStateListener, nsITransactionListener, nsITimerCallback);
|
1999-07-28 02:49:12 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-06-24 01:29:58 +00:00
|
|
|
nsInterfaceState::Init(nsIHTMLEditor* aEditor, nsIDOMDocument *aChromeDoc)
|
1999-07-28 02:49:12 +00:00
|
|
|
{
|
|
|
|
if (!aEditor)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2000-01-18 21:50:15 +00:00
|
|
|
if (!aChromeDoc)
|
1999-07-28 02:49:12 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
mEditor = aEditor; // no addreffing here
|
2000-01-18 21:50:15 +00:00
|
|
|
mChromeDoc = aChromeDoc;
|
2000-03-17 01:59:10 +00:00
|
|
|
|
1999-07-28 02:49:12 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-08-03 00:57:23 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsInterfaceState::NotifyDocumentCreated()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsInterfaceState::NotifyDocumentWillBeDestroyed()
|
|
|
|
{
|
2000-03-09 22:20:56 +00:00
|
|
|
// cancel any outstanding udpate timer
|
|
|
|
if (mUpdateTimer)
|
|
|
|
mUpdateTimer->Cancel();
|
2000-03-17 01:59:10 +00:00
|
|
|
|
1999-08-03 00:57:23 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-07-28 02:49:12 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsInterfaceState::NotifyDocumentStateChanged(PRBool aNowDirty)
|
|
|
|
{
|
|
|
|
// update document modified. We should have some other notifications for this too.
|
|
|
|
return UpdateDirtyState(aNowDirty);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-09-14 11:45:01 +00:00
|
|
|
nsInterfaceState::NotifySelectionChanged(nsIDOMDocument *, nsISelection *, short)
|
1999-09-22 01:22:35 +00:00
|
|
|
{
|
2000-03-03 23:35:11 +00:00
|
|
|
return PrimeUpdateTimer();
|
1999-09-22 01:22:35 +00:00
|
|
|
}
|
|
|
|
|
2000-03-03 23:35:11 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
1999-09-22 01:22:35 +00:00
|
|
|
|
2000-02-16 01:38:02 +00:00
|
|
|
NS_IMETHODIMP nsInterfaceState::WillDo(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTransaction, PRBool *aInterrupt)
|
|
|
|
{
|
|
|
|
*aInterrupt = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsInterfaceState::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)
|
2000-04-18 07:52:02 +00:00
|
|
|
CallUpdateCommands(NS_ConvertASCIItoUCS2("undo"));
|
2000-02-16 01:38:02 +00:00
|
|
|
mFirstDoOfFirstUndo = PR_FALSE;
|
|
|
|
}
|
2000-03-03 23:35:11 +00:00
|
|
|
|
2000-02-16 01:38:02 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsInterfaceState::WillUndo(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTransaction, PRBool *aInterrupt)
|
|
|
|
{
|
|
|
|
*aInterrupt = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsInterfaceState::DidUndo(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTransaction, nsresult aUndoResult)
|
|
|
|
{
|
|
|
|
PRInt32 undoCount;
|
|
|
|
aManager->GetNumberOfUndoItems(&undoCount);
|
|
|
|
if (undoCount == 0)
|
|
|
|
mFirstDoOfFirstUndo = PR_TRUE; // reset the state for the next do
|
|
|
|
|
2000-04-18 07:52:02 +00:00
|
|
|
CallUpdateCommands(NS_ConvertASCIItoUCS2("undo"));
|
2000-02-16 01:38:02 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsInterfaceState::WillRedo(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTransaction, PRBool *aInterrupt)
|
|
|
|
{
|
|
|
|
*aInterrupt = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsInterfaceState::DidRedo(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTransaction, nsresult aRedoResult)
|
|
|
|
{
|
2000-04-18 07:52:02 +00:00
|
|
|
CallUpdateCommands(NS_ConvertASCIItoUCS2("undo"));
|
2000-02-16 01:38:02 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsInterfaceState::WillBeginBatch(nsITransactionManager *aManager, PRBool *aInterrupt)
|
|
|
|
{
|
|
|
|
*aInterrupt = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsInterfaceState::DidBeginBatch(nsITransactionManager *aManager, nsresult aResult)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsInterfaceState::WillEndBatch(nsITransactionManager *aManager, PRBool *aInterrupt)
|
|
|
|
{
|
|
|
|
*aInterrupt = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsInterfaceState::DidEndBatch(nsITransactionManager *aManager, nsresult aResult)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsInterfaceState::WillMerge(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTopTransaction, nsITransaction *aTransactionToMerge, PRBool *aInterrupt)
|
|
|
|
{
|
|
|
|
*aInterrupt = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsInterfaceState::DidMerge(nsITransactionManager *aManager,
|
|
|
|
nsITransaction *aTopTransaction, nsITransaction *aTransactionToMerge,
|
|
|
|
PRBool aDidMerge, nsresult aMergeResult)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-03-03 23:35:11 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
nsresult nsInterfaceState::PrimeUpdateTimer()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (mUpdateTimer)
|
|
|
|
{
|
|
|
|
// i'd love to be able to just call SetDelay on the existing timer, but
|
|
|
|
// i think i have to tear it down and make a new one.
|
|
|
|
mUpdateTimer->Cancel();
|
|
|
|
mUpdateTimer = NULL; // free it
|
|
|
|
}
|
|
|
|
|
2000-09-13 23:57:52 +00:00
|
|
|
mUpdateTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
|
2000-03-03 23:35:11 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
const PRUint32 kUpdateTimerDelay = 150;
|
|
|
|
return mUpdateTimer->Init(NS_STATIC_CAST(nsITimerCallback*, this), kUpdateTimerDelay);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nsInterfaceState::TimerCallback()
|
|
|
|
{
|
|
|
|
// if the selection state has changed, update stuff
|
|
|
|
PRBool isCollapsed = SelectionIsCollapsed();
|
|
|
|
if (isCollapsed != mSelectionCollapsed)
|
|
|
|
{
|
2000-04-18 07:52:02 +00:00
|
|
|
CallUpdateCommands(NS_ConvertASCIItoUCS2("select"));
|
2000-03-03 23:35:11 +00:00
|
|
|
mSelectionCollapsed = isCollapsed;
|
|
|
|
}
|
|
|
|
|
2000-05-01 21:49:52 +00:00
|
|
|
CallUpdateCommands(NS_ConvertASCIItoUCS2("style"));
|
2000-03-03 23:35:11 +00:00
|
|
|
}
|
|
|
|
|
2000-11-17 01:25:28 +00:00
|
|
|
nsresult
|
|
|
|
nsInterfaceState::UpdateDirtyState(PRBool aNowDirty)
|
|
|
|
{
|
|
|
|
if (mDirtyState != aNowDirty)
|
|
|
|
{
|
|
|
|
CallUpdateCommands(NS_ConvertASCIItoUCS2("save"));
|
|
|
|
|
|
|
|
mDirtyState = aNowDirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-02-16 01:38:02 +00:00
|
|
|
|
|
|
|
nsresult nsInterfaceState::CallUpdateCommands(const nsString& aCommand)
|
|
|
|
{
|
|
|
|
if (!mDOMWindow)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEditor> editor = do_QueryInterface(mEditor);
|
|
|
|
if (!editor) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc;
|
|
|
|
editor->GetDocument(getter_AddRefs(domDoc));
|
|
|
|
if (!domDoc) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> theDoc = do_QueryInterface(domDoc);
|
|
|
|
if (!theDoc) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> scriptGlobalObject;
|
|
|
|
theDoc->GetScriptGlobalObject(getter_AddRefs(scriptGlobalObject));
|
|
|
|
|
2000-09-01 01:54:35 +00:00
|
|
|
nsCOMPtr<nsIDOMWindowInternal> domWindow = do_QueryInterface(scriptGlobalObject);
|
2000-02-16 01:38:02 +00:00
|
|
|
if (!domWindow) return NS_ERROR_FAILURE;
|
|
|
|
mDOMWindow = domWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mDOMWindow->UpdateCommands(aCommand);
|
|
|
|
}
|
|
|
|
|
1999-09-22 01:22:35 +00:00
|
|
|
PRBool
|
|
|
|
nsInterfaceState::SelectionIsCollapsed()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
// we don't care too much about failures here.
|
|
|
|
nsCOMPtr<nsIEditor> editor = do_QueryInterface(mEditor, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelection> domSelection;
|
1999-09-22 01:22:35 +00:00
|
|
|
rv = editor->GetSelection(getter_AddRefs(domSelection));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
PRBool selectionCollapsed = PR_FALSE;
|
|
|
|
rv = domSelection->GetIsCollapsed(&selectionCollapsed);
|
|
|
|
return selectionCollapsed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
1999-07-28 02:49:12 +00:00
|
|
|
|
1999-08-26 03:22:03 +00:00
|
|
|
|
2000-03-03 23:35:11 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
nsInterfaceState::Notify(nsITimer *timer)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(timer == mUpdateTimer.get(), "Hey, this ain't my timer!");
|
|
|
|
mUpdateTimer = NULL; // release my hold
|
|
|
|
TimerCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2000-09-14 11:45:01 +00:00
|
|
|
nsresult NS_NewInterfaceState(nsIHTMLEditor* aEditor, nsIDOMDocument* aChromeDoc, nsISelectionListener** aInstancePtrResult)
|
1999-07-28 02:49:12 +00:00
|
|
|
{
|
|
|
|
nsInterfaceState* newThang = new nsInterfaceState;
|
|
|
|
if (!newThang)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
*aInstancePtrResult = nsnull;
|
2000-01-18 21:50:15 +00:00
|
|
|
nsresult rv = newThang->Init(aEditor, aChromeDoc);
|
1999-07-28 02:49:12 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
delete newThang;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-09-14 11:45:01 +00:00
|
|
|
return newThang->QueryInterface(NS_GET_IID(nsISelectionListener), (void **)aInstancePtrResult);
|
1999-07-28 02:49:12 +00:00
|
|
|
}
|