1999-07-28 06:49:12 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 06:43:54 +03: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 06:49:12 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03: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 06:49:12 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-07-28 06:49:12 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2000-01-11 23:49:15 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
1999-07-28 06:49:12 +04: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"
|
1999-08-03 04:57:23 +04:00
|
|
|
#include "nsIDOMSelection.h"
|
1999-08-26 07:22:03 +04:00
|
|
|
#include "nsIDOMAttr.h"
|
2000-02-16 04:38:02 +03:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
2000-09-01 05:54:35 +04:00
|
|
|
#include "nsIDOMWindowInternal.h"
|
2000-03-04 02:35:11 +03:00
|
|
|
#include "nsITimer.h"
|
1999-07-28 06:49:12 +04:00
|
|
|
|
1999-08-09 05:37:50 +04:00
|
|
|
#include "nsIEditor.h"
|
1999-07-28 06:49:12 +04:00
|
|
|
#include "nsIHTMLEditor.h"
|
2000-02-16 04:38:02 +03:00
|
|
|
#include "nsITransactionManager.h"
|
1999-07-28 06:49:12 +04:00
|
|
|
|
|
|
|
#include "nsInterfaceState.h"
|
|
|
|
|
2000-05-02 01:49:52 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
static PRBool StringHashDestroyFunc(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
nsString* stringData = NS_REINTERPRET_CAST(nsString*, aData);
|
|
|
|
delete stringData;
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* StringHashCloneElementFunc(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
nsString* stringData = NS_REINTERPRET_CAST(nsString*, aData);
|
|
|
|
if (stringData)
|
|
|
|
{
|
|
|
|
nsString* newString = new nsString(*stringData);
|
|
|
|
return newString;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
1999-07-28 06:49:12 +04:00
|
|
|
nsInterfaceState::nsInterfaceState()
|
|
|
|
: mEditor(nsnull)
|
2000-01-19 00:50:15 +03:00
|
|
|
, mChromeDoc(nsnull)
|
2000-02-16 04:38:02 +03:00
|
|
|
, mDOMWindow(nsnull)
|
2000-03-17 04:59:10 +03:00
|
|
|
, mUpdateParagraph(PR_FALSE)
|
|
|
|
, mUpdateFont(PR_FALSE)
|
|
|
|
, mUpdateList(PR_FALSE)
|
|
|
|
, mUpdateBold(PR_FALSE)
|
|
|
|
, mUpdateItalics(PR_FALSE)
|
|
|
|
, mUpdateUnderline(PR_FALSE)
|
1999-07-28 06:49:12 +04:00
|
|
|
, mBoldState(eStateUninitialized)
|
|
|
|
, mItalicState(eStateUninitialized)
|
|
|
|
, mUnderlineState(eStateUninitialized)
|
|
|
|
, mDirtyState(eStateUninitialized)
|
2000-01-19 00:50:15 +03:00
|
|
|
, mSelectionCollapsed(eStateUninitialized)
|
2000-02-16 04:38:02 +03:00
|
|
|
, mFirstDoOfFirstUndo(PR_TRUE)
|
1999-07-28 06:49:12 +04:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsInterfaceState::~nsInterfaceState()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsInterfaceState);
|
|
|
|
NS_IMPL_RELEASE(nsInterfaceState);
|
2000-03-04 02:35:11 +03:00
|
|
|
NS_IMPL_QUERY_INTERFACE4(nsInterfaceState, nsIDOMSelectionListener, nsIDocumentStateListener, nsITransactionListener, nsITimerCallback);
|
1999-07-28 06:49:12 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-06-24 05:29:58 +04:00
|
|
|
nsInterfaceState::Init(nsIHTMLEditor* aEditor, nsIDOMDocument *aChromeDoc)
|
1999-07-28 06:49:12 +04:00
|
|
|
{
|
|
|
|
if (!aEditor)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2000-01-19 00:50:15 +03:00
|
|
|
if (!aChromeDoc)
|
1999-07-28 06:49:12 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
mEditor = aEditor; // no addreffing here
|
2000-01-19 00:50:15 +03:00
|
|
|
mChromeDoc = aChromeDoc;
|
2000-03-17 04:59:10 +03:00
|
|
|
|
|
|
|
// it sucks explicitly naming XUL nodes here. Would be better to have
|
|
|
|
// some way to register things that we want to observe from JS
|
|
|
|
mUpdateParagraph = XULNodeExists("ParagraphSelect");
|
|
|
|
mUpdateFont = XULNodeExists("FontFaceSelect");
|
|
|
|
mUpdateList = XULNodeExists("ulButton") || XULNodeExists("olButton");
|
|
|
|
|
|
|
|
mUpdateBold = XULNodeExists("boldButton");
|
|
|
|
mUpdateItalics = XULNodeExists("italicButton");
|
|
|
|
mUpdateUnderline = XULNodeExists("underlineButton");
|
|
|
|
|
1999-07-28 06:49:12 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-08-03 04:57:23 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsInterfaceState::NotifyDocumentCreated()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsInterfaceState::NotifyDocumentWillBeDestroyed()
|
|
|
|
{
|
2000-03-10 01:20:56 +03:00
|
|
|
// cancel any outstanding udpate timer
|
|
|
|
if (mUpdateTimer)
|
|
|
|
mUpdateTimer->Cancel();
|
2000-03-17 04:59:10 +03:00
|
|
|
|
1999-08-03 04:57:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-07-28 06:49:12 +04: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-04-15 03:01:09 +04:00
|
|
|
nsInterfaceState::NotifySelectionChanged(nsIDOMDocument *, nsIDOMSelection *, short)
|
1999-09-22 05:22:35 +04:00
|
|
|
{
|
2000-03-04 02:35:11 +03:00
|
|
|
return PrimeUpdateTimer();
|
1999-09-22 05:22:35 +04:00
|
|
|
}
|
|
|
|
|
2000-03-04 02:35:11 +03:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
1999-09-22 05:22:35 +04:00
|
|
|
|
2000-02-16 04:38:02 +03: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 11:52:02 +04:00
|
|
|
CallUpdateCommands(NS_ConvertASCIItoUCS2("undo"));
|
2000-02-16 04:38:02 +03:00
|
|
|
mFirstDoOfFirstUndo = PR_FALSE;
|
|
|
|
}
|
2000-03-04 02:35:11 +03:00
|
|
|
|
2000-02-16 04:38:02 +03: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 11:52:02 +04:00
|
|
|
CallUpdateCommands(NS_ConvertASCIItoUCS2("undo"));
|
2000-02-16 04:38:02 +03: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 11:52:02 +04:00
|
|
|
CallUpdateCommands(NS_ConvertASCIItoUCS2("undo"));
|
2000-02-16 04:38:02 +03: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-04 02:35:11 +03: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-14 03:57:52 +04:00
|
|
|
mUpdateTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
|
2000-03-04 02:35:11 +03: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 11:52:02 +04:00
|
|
|
CallUpdateCommands(NS_ConvertASCIItoUCS2("select"));
|
2000-03-04 02:35:11 +03:00
|
|
|
mSelectionCollapsed = isCollapsed;
|
|
|
|
}
|
|
|
|
|
2000-05-02 01:49:52 +04:00
|
|
|
// (void)ForceUpdate();
|
|
|
|
CallUpdateCommands(NS_ConvertASCIItoUCS2("style"));
|
2000-03-04 02:35:11 +03:00
|
|
|
}
|
|
|
|
|
2000-02-16 04:38:02 +03: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 05:54:35 +04:00
|
|
|
nsCOMPtr<nsIDOMWindowInternal> domWindow = do_QueryInterface(scriptGlobalObject);
|
2000-02-16 04:38:02 +03:00
|
|
|
if (!domWindow) return NS_ERROR_FAILURE;
|
|
|
|
mDOMWindow = domWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mDOMWindow->UpdateCommands(aCommand);
|
|
|
|
}
|
|
|
|
|
1999-09-22 05:22:35 +04:00
|
|
|
NS_IMETHODIMP
|
2000-05-02 01:49:52 +04:00
|
|
|
nsInterfaceState::ForceUpdate(const PRUnichar *tagToUpdate)
|
1999-07-28 06:49:12 +04:00
|
|
|
{
|
2000-05-02 01:49:52 +04:00
|
|
|
PRBool updateEverything = !tagToUpdate || !*tagToUpdate;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// This code is obsolete now that commanders are doing the work
|
1999-07-28 06:49:12 +04:00
|
|
|
nsresult rv;
|
2000-05-02 01:49:52 +04:00
|
|
|
|
1999-07-28 06:49:12 +04:00
|
|
|
// update bold
|
2000-05-02 01:49:52 +04:00
|
|
|
if (mUpdateBold && (updateEverything || nsAutoString("b").Equals(tagToUpdate)))
|
2000-03-17 04:59:10 +03:00
|
|
|
{
|
2000-05-02 01:49:52 +04:00
|
|
|
rv = UpdateTextState("b", "cmd_bold", "bold", mBoldState);
|
2000-03-17 04:59:10 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to update state");
|
|
|
|
}
|
2000-03-04 02:35:11 +03:00
|
|
|
|
1999-07-28 06:49:12 +04:00
|
|
|
// update italic
|
2000-05-02 01:49:52 +04:00
|
|
|
if (mUpdateItalics && (updateEverything || nsAutoString("i").Equals(tagToUpdate)))
|
2000-03-17 04:59:10 +03:00
|
|
|
{
|
2000-05-02 01:49:52 +04:00
|
|
|
rv = UpdateTextState("i", "cmd_italic", "italic", mItalicState);
|
2000-03-17 04:59:10 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to update state");
|
|
|
|
}
|
2000-03-04 02:35:11 +03:00
|
|
|
|
1999-07-28 06:49:12 +04:00
|
|
|
// update underline
|
2000-05-02 01:49:52 +04:00
|
|
|
if (mUpdateUnderline && (updateEverything || nsAutoString("u").Equals(tagToUpdate)))
|
2000-03-17 04:59:10 +03:00
|
|
|
{
|
2000-05-02 01:49:52 +04:00
|
|
|
rv = UpdateTextState("u", "cmd_underline", "underline", mUnderlineState);
|
2000-03-17 04:59:10 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to update state");
|
|
|
|
}
|
1999-07-28 06:49:12 +04:00
|
|
|
|
|
|
|
// update the paragraph format popup
|
2000-05-02 01:49:52 +04:00
|
|
|
if (mUpdateParagraph && (updateEverything || nsAutoString("format").Equals(tagToUpdate)))
|
2000-03-17 04:59:10 +03:00
|
|
|
{
|
2000-04-05 04:06:53 +04:00
|
|
|
rv = UpdateParagraphState("Editor:Paragraph:Format", "format");
|
2000-03-17 04:59:10 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to update state");
|
|
|
|
}
|
2000-03-04 02:35:11 +03:00
|
|
|
|
1999-11-03 03:48:26 +03:00
|
|
|
// udpate the font face
|
2000-05-02 01:49:52 +04:00
|
|
|
if (mUpdateFont && (updateEverything || nsAutoString("font").Equals(tagToUpdate)))
|
2000-03-17 04:59:10 +03:00
|
|
|
{
|
2000-04-05 04:06:53 +04:00
|
|
|
rv = UpdateFontFace("Editor:Font:Face", "font");
|
2000-03-17 04:59:10 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to update state");
|
|
|
|
}
|
1999-07-28 06:49:12 +04:00
|
|
|
|
1999-11-13 19:37:58 +03:00
|
|
|
// TODO: FINISH FONT FACE AND ADD FONT SIZE ("Editor:Font:Size", "fontsize", mFontSize)
|
1999-11-03 03:48:26 +03:00
|
|
|
|
1999-08-03 04:57:23 +04:00
|
|
|
// update the list buttons
|
2000-05-02 01:49:52 +04:00
|
|
|
if (mUpdateList && (updateEverything ||
|
|
|
|
nsAutoString("ol").Equals(tagToUpdate) ||
|
|
|
|
nsAutoString("ul").Equals(tagToUpdate)))
|
2000-03-17 04:59:10 +03:00
|
|
|
{
|
|
|
|
rv = UpdateListState("Editor:Paragraph:ListType");
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to update state");
|
|
|
|
}
|
2000-05-02 01:49:52 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-09-22 05:22:35 +04: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))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMSelection> domSelection;
|
|
|
|
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 06:49:12 +04:00
|
|
|
|
|
|
|
nsresult
|
2000-04-05 04:06:53 +04:00
|
|
|
nsInterfaceState::UpdateParagraphState(const char* observerName, const char* attributeName)
|
1999-07-28 06:49:12 +04:00
|
|
|
{
|
2000-03-17 02:45:41 +03:00
|
|
|
nsCOMPtr<nsIDOMSelection> domSelection;
|
|
|
|
nsCOMPtr<nsIEditor> editor = do_QueryInterface(mEditor);
|
|
|
|
// Get the nsIEditor pointer (mEditor is nsIHTMLEditor)
|
|
|
|
if (!editor) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsresult rv = editor->GetSelection(getter_AddRefs(domSelection));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
PRBool selectionCollapsed = PR_FALSE;
|
|
|
|
rv = domSelection->GetIsCollapsed(&selectionCollapsed);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-03-17 04:59:10 +03:00
|
|
|
|
2000-03-17 02:45:41 +03:00
|
|
|
// Get anchor and focus nodes:
|
|
|
|
nsCOMPtr<nsIDOMNode> anchorNode;
|
|
|
|
rv = domSelection->GetAnchorNode(getter_AddRefs(anchorNode));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (!anchorNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> focusNode;
|
|
|
|
rv = domSelection->GetFocusNode(getter_AddRefs(focusNode));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (!focusNode) return NS_ERROR_NULL_POINTER;
|
1999-07-28 06:49:12 +04:00
|
|
|
|
2000-03-17 02:45:41 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> anchorNodeBlockParent;
|
|
|
|
PRBool isBlock;
|
|
|
|
rv = editor->NodeIsBlock(anchorNode, isBlock);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (isBlock)
|
|
|
|
{
|
|
|
|
anchorNodeBlockParent = anchorNode;
|
2000-03-17 04:59:10 +03:00
|
|
|
}
|
2000-03-17 02:45:41 +03:00
|
|
|
else
|
1999-07-28 06:49:12 +04:00
|
|
|
{
|
2000-03-17 02:45:41 +03:00
|
|
|
// Get block parent of anchorNode node
|
|
|
|
// We could simply use this if it was in nsIEditor interface!
|
|
|
|
//rv = editor->GetBlockParent(anchorNode, getter_AddRefs(anchorNodeBlockParent));
|
|
|
|
// if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode>parent;
|
|
|
|
nsCOMPtr<nsIDOMNode>temp;
|
|
|
|
rv = anchorNode->GetParentNode(getter_AddRefs(parent));
|
|
|
|
while (NS_SUCCEEDED(rv) && parent)
|
|
|
|
{
|
|
|
|
rv = editor->NodeIsBlock(parent, isBlock);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (isBlock)
|
|
|
|
{
|
|
|
|
anchorNodeBlockParent = parent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rv = parent->GetParentNode(getter_AddRefs(temp));
|
|
|
|
parent = do_QueryInterface(temp);
|
|
|
|
}
|
1999-07-28 06:49:12 +04:00
|
|
|
}
|
2000-04-04 18:51:26 +04:00
|
|
|
if (!anchorNodeBlockParent) // return NS_ERROR_NULL_POINTER;
|
|
|
|
anchorNodeBlockParent = anchorNode;
|
2000-03-17 02:45:41 +03:00
|
|
|
|
|
|
|
nsAutoString tagName;
|
2000-03-17 04:59:10 +03:00
|
|
|
|
2000-03-17 02:45:41 +03:00
|
|
|
// Check if we have a selection that extends into multiple nodes,
|
|
|
|
// so we can check for "mixed" selection state
|
|
|
|
if (selectionCollapsed || focusNode == anchorNode)
|
|
|
|
{
|
|
|
|
// Entire selection is within one block
|
|
|
|
anchorNodeBlockParent->GetNodeName(tagName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We may have different block parent node types WITHIN the selection,
|
|
|
|
// even if the anchor and focus parents are the same type.
|
|
|
|
// Getting the list of all block, e.g., by using GetParagraphTags(&tagList)
|
|
|
|
// is too inefficient for long documents.
|
|
|
|
// TODO: Change to use selection iterator to detect each block node in the
|
|
|
|
// selection and if different from the anchorNodeBlockParent, use "mixed" state
|
|
|
|
// *** Not doing this now reduces risk for Beta1 -- simply assume mixed state
|
|
|
|
// Note that "mixed" displays as "normal" in UI as of 3/6.
|
2000-04-18 11:52:02 +04:00
|
|
|
tagName.AssignWithConversion("mixed");
|
2000-03-17 02:45:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tagName != mParagraphFormat)
|
1999-11-03 03:48:26 +03:00
|
|
|
{
|
2000-03-17 02:45:41 +03:00
|
|
|
rv = SetNodeAttribute(observerName, attributeName, tagName);
|
1999-11-03 03:48:26 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-03-17 02:45:41 +03:00
|
|
|
mParagraphFormat = tagName;
|
1999-11-03 03:48:26 +03:00
|
|
|
}
|
1999-07-28 06:49:12 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-08-03 04:57:23 +04:00
|
|
|
nsresult
|
1999-08-11 03:16:43 +04:00
|
|
|
nsInterfaceState::UpdateListState(const char* observerName)
|
1999-08-03 04:57:23 +04:00
|
|
|
{
|
|
|
|
nsresult rv = NS_ERROR_NO_INTERFACE;
|
2000-05-03 04:14:28 +04:00
|
|
|
nsAutoString tagStr; // empty by default.
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2000-06-02 11:47:53 +04:00
|
|
|
PRBool bMixed, bOL, bUL, bDL;
|
|
|
|
rv = mEditor->GetListState(bMixed, bOL, bUL, bDL);
|
1999-08-11 03:16:43 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-05-03 04:14:28 +04:00
|
|
|
if (bMixed)
|
|
|
|
{} // leave tagStr empty
|
|
|
|
else if (bOL)
|
|
|
|
tagStr.AssignWithConversion("ol");
|
|
|
|
else if (bUL)
|
2000-04-18 11:52:02 +04:00
|
|
|
tagStr.AssignWithConversion("ul");
|
2000-06-02 11:47:53 +04:00
|
|
|
else if (bDL)
|
|
|
|
tagStr.AssignWithConversion("dl");
|
2000-05-03 04:14:28 +04:00
|
|
|
// else leave tagStr empty
|
1999-08-11 03:16:43 +04:00
|
|
|
|
2000-05-03 04:14:28 +04:00
|
|
|
rv = SetNodeAttribute(observerName, "format", tagStr);
|
|
|
|
|
|
|
|
mListTag = tagStr;
|
1999-08-03 04:57:23 +04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-07-28 06:49:12 +04:00
|
|
|
nsresult
|
2000-04-05 04:06:53 +04:00
|
|
|
nsInterfaceState::UpdateFontFace(const char* observerName, const char* attributeName)
|
1999-07-28 06:49:12 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
PRBool firstOfSelectionHasProp = PR_FALSE;
|
|
|
|
PRBool anyOfSelectionHasProp = PR_FALSE;
|
|
|
|
PRBool allOfSelectionHasProp = PR_FALSE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> styleAtom = getter_AddRefs(NS_NewAtom("font"));
|
2000-04-18 11:52:02 +04:00
|
|
|
nsAutoString faceStr; faceStr.AssignWithConversion("face");
|
1999-11-03 03:48:26 +03:00
|
|
|
nsAutoString thisFace;
|
|
|
|
|
|
|
|
// Use to test for "Default Fixed Width"
|
|
|
|
nsCOMPtr<nsIAtom> fixedStyleAtom = getter_AddRefs(NS_NewAtom("tt"));
|
|
|
|
|
|
|
|
PRBool testBoolean;
|
1999-07-28 06:49:12 +04:00
|
|
|
|
2000-01-31 13:30:12 +03:00
|
|
|
rv = mEditor->GetInlineProperty(styleAtom, &faceStr, &thisFace, firstOfSelectionHasProp, anyOfSelectionHasProp, allOfSelectionHasProp);
|
|
|
|
if( !anyOfSelectionHasProp )
|
1999-09-22 05:22:35 +04:00
|
|
|
{
|
2000-03-17 04:59:10 +03:00
|
|
|
// No font face set -- check for "tt". This can return an error if the selection isn't in a node
|
2000-01-31 13:30:12 +03:00
|
|
|
rv = mEditor->GetInlineProperty(fixedStyleAtom, nsnull, nsnull, firstOfSelectionHasProp, anyOfSelectionHasProp, allOfSelectionHasProp);
|
2000-03-17 04:59:10 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
testBoolean = anyOfSelectionHasProp;
|
|
|
|
if (anyOfSelectionHasProp)
|
2000-04-18 11:52:02 +04:00
|
|
|
thisFace.AssignWithConversion("tt");
|
2000-03-17 04:59:10 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
rv = NS_OK; // we don't want to propagate this error
|
1999-11-03 03:48:26 +03:00
|
|
|
}
|
1999-11-13 19:37:58 +03:00
|
|
|
|
1999-11-03 03:48:26 +03:00
|
|
|
// TODO: HANDLE "MIXED" STATE
|
|
|
|
if (thisFace != mFontString)
|
|
|
|
{
|
2000-03-31 06:13:00 +04:00
|
|
|
rv = SetNodeAttribute(observerName, "face", thisFace);
|
1999-11-22 17:52:21 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
mFontString = thisFace;
|
1999-09-22 05:22:35 +04:00
|
|
|
}
|
1999-07-28 06:49:12 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsInterfaceState::UpdateTextState(const char* tagName, const char* observerName, const char* attributeName, PRInt8& ioState)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
1999-09-22 05:22:35 +04:00
|
|
|
|
1999-07-28 06:49:12 +04:00
|
|
|
nsCOMPtr<nsIAtom> styleAtom = getter_AddRefs(NS_NewAtom(tagName));
|
|
|
|
|
1999-09-22 05:22:35 +04:00
|
|
|
PRBool testBoolean;
|
2000-01-31 13:30:12 +03:00
|
|
|
PRBool firstOfSelectionHasProp = PR_FALSE;
|
|
|
|
PRBool anyOfSelectionHasProp = PR_FALSE;
|
|
|
|
PRBool allOfSelectionHasProp = PR_FALSE;
|
|
|
|
|
|
|
|
rv = mEditor->GetInlineProperty(styleAtom, nsnull, nsnull, firstOfSelectionHasProp, anyOfSelectionHasProp, allOfSelectionHasProp);
|
|
|
|
testBoolean = allOfSelectionHasProp; // change this to alter the behaviour
|
1999-09-22 05:22:35 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (testBoolean != ioState)
|
1999-07-28 06:49:12 +04:00
|
|
|
{
|
2000-04-18 11:52:02 +04:00
|
|
|
rv = SetNodeAttribute(observerName, attributeName, NS_ConvertASCIItoUCS2(testBoolean ? "true" : "false"));
|
1999-07-28 06:49:12 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
1999-09-22 05:22:35 +04:00
|
|
|
ioState = testBoolean;
|
1999-07-28 06:49:12 +04:00
|
|
|
}
|
|
|
|
|
2000-03-17 04:59:10 +03:00
|
|
|
return NS_OK;
|
1999-07-28 06:49:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsInterfaceState::UpdateDirtyState(PRBool aNowDirty)
|
|
|
|
{
|
|
|
|
if (mDirtyState != aNowDirty)
|
|
|
|
{
|
2000-05-02 01:49:52 +04:00
|
|
|
CallUpdateCommands(NS_ConvertASCIItoUCS2("save"));
|
1999-08-26 07:22:03 +04:00
|
|
|
|
1999-07-28 06:49:12 +04:00
|
|
|
mDirtyState = aNowDirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-17 04:59:10 +03:00
|
|
|
PRBool
|
|
|
|
nsInterfaceState::XULNodeExists(const char* nodeID)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mChromeDoc)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> elem;
|
2000-04-18 11:52:02 +04:00
|
|
|
rv = mChromeDoc->GetElementById( NS_ConvertASCIItoUCS2(nodeID), getter_AddRefs(elem) );
|
2000-03-17 04:59:10 +03:00
|
|
|
|
|
|
|
return NS_SUCCEEDED(rv) && elem;
|
|
|
|
}
|
|
|
|
|
1999-07-28 06:49:12 +04:00
|
|
|
nsresult
|
|
|
|
nsInterfaceState::SetNodeAttribute(const char* nodeID, const char* attributeName, const nsString& newValue)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2000-01-19 00:50:15 +03:00
|
|
|
if (!mChromeDoc)
|
1999-07-28 06:49:12 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> elem;
|
2000-04-18 11:52:02 +04:00
|
|
|
rv = mChromeDoc->GetElementById( NS_ConvertASCIItoUCS2(nodeID), getter_AddRefs(elem) );
|
1999-07-28 06:49:12 +04:00
|
|
|
if (NS_FAILED(rv) || !elem) return rv;
|
|
|
|
|
2000-04-18 11:52:02 +04:00
|
|
|
return elem->SetAttribute(NS_ConvertASCIItoUCS2(attributeName), newValue);
|
1999-07-28 06:49:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-26 07:22:03 +04:00
|
|
|
nsresult
|
|
|
|
nsInterfaceState::UnsetNodeAttribute(const char* nodeID, const char* attributeName)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2000-01-19 00:50:15 +03:00
|
|
|
if (!mChromeDoc)
|
1999-08-26 07:22:03 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> elem;
|
2000-04-18 11:52:02 +04:00
|
|
|
rv = mChromeDoc->GetElementById( NS_ConvertASCIItoUCS2(nodeID), getter_AddRefs(elem) );
|
1999-08-26 07:22:03 +04:00
|
|
|
if (NS_FAILED(rv) || !elem) return rv;
|
|
|
|
|
2000-04-18 11:52:02 +04:00
|
|
|
return elem->RemoveAttribute(NS_ConvertASCIItoUCS2(attributeName));
|
1999-08-26 07:22:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-04 02:35:11 +03: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-06-24 05:29:58 +04:00
|
|
|
nsresult NS_NewInterfaceState(nsIHTMLEditor* aEditor, nsIDOMDocument* aChromeDoc, nsIDOMSelectionListener** aInstancePtrResult)
|
1999-07-28 06:49:12 +04:00
|
|
|
{
|
|
|
|
nsInterfaceState* newThang = new nsInterfaceState;
|
|
|
|
if (!newThang)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
*aInstancePtrResult = nsnull;
|
2000-01-19 00:50:15 +03:00
|
|
|
nsresult rv = newThang->Init(aEditor, aChromeDoc);
|
1999-07-28 06:49:12 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
delete newThang;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-01-11 23:49:15 +03:00
|
|
|
return newThang->QueryInterface(NS_GET_IID(nsIDOMSelectionListener), (void **)aInstancePtrResult);
|
1999-07-28 06:49:12 +04:00
|
|
|
}
|