Added ApplyStyleSheet() method to the various editor interfaces.

This commit is contained in:
kin%netscape.com 1999-07-01 19:32:35 +00:00
parent bd73f68e8b
commit 3b5b4d3b48
21 changed files with 370 additions and 5 deletions

View File

@ -51,12 +51,20 @@
#include "nsIStyleContext.h"
#include "nsIEditActionListener.h"
#include "nsICSSLoader.h"
#include "nsICSSStyleSheet.h"
#include "nsIHTMLContentContainer.h"
#include "nsIStyleSet.h"
#include "nsIDocumentObserver.h"
#ifdef NECKO
#include "nsIIOService.h"
#include "nsIURL.h"
#include "nsIServiceManager.h"
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
#else
#include "nsIURL.h"
#endif // NECKO
#include "nsEditorShell.h"
@ -1332,6 +1340,117 @@ NS_IMETHODIMP nsEditor::InsertAsQuotation(const nsString& aQuotedText)
return InsertText(aQuotedText);
}
extern "C" void
ApplyStyleSheetToPresShellDocument(nsICSSStyleSheet* aSheet, void *aData)
{
nsresult rv;
nsIPresShell *presShell = (nsIPresShell *)aData;
if (presShell) {
nsCOMPtr<nsIStyleSet> styleSet;
rv = presShell->GetStyleSet(getter_AddRefs(styleSet));
if (NS_SUCCEEDED(rv) && styleSet) {
styleSet->AppendOverrideStyleSheet(aSheet);
nsCOMPtr<nsIDocumentObserver> observer = do_QueryInterface(presShell);
nsCOMPtr<nsIStyleSheet> styleSheet = do_QueryInterface(aSheet);
nsCOMPtr<nsIDocument> document;
rv = presShell->GetDocument(getter_AddRefs(document));
if (NS_SUCCEEDED(rv) && document && observer && styleSheet)
rv = observer->StyleSheetAdded(document, styleSheet);
}
}
}
NS_IMETHODIMP nsEditor::ApplyStyleSheet(const nsString& aURL)
{
#ifdef ENABLE_JS_EDITOR_LOG
nsAutoJSEditorLogLock logLock(mJSEditorLog);
if (mJSEditorLog)
mJSEditorLog->ApplyStyleSheet(aURL);
#endif // ENABLE_JS_EDITOR_LOG
// XXX: Note that this is not an undo-able action yet!
nsresult rv = NS_OK;
nsIURI* uaURL = 0;
#ifndef NECKO
rv = NS_NewURL(&uaURL, aURL);
#else
NS_WITH_SERVICE(nsIIOService, service, kIOServiceCID, &rv);
if (NS_FAILED(rv)) return rv;
nsIURI *uri = nsnull;
rv = service->NewURI(aURL, nsnull, &uri);
if (NS_FAILED(rv)) return rv;
rv = uri->QueryInterface(nsIURI::GetIID(), (void**)&uaURL);
NS_RELEASE(uri);
#endif // NECKO
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsIDocument> document;
rv = mPresShell->GetDocument(getter_AddRefs(document));
if (NS_SUCCEEDED(rv)) {
if (document) {
nsCOMPtr<nsIHTMLContentContainer> container = do_QueryInterface(document);
if (container) {
nsICSSLoader *cssLoader = 0;
nsICSSStyleSheet *cssStyleSheet = 0;
rv = container->GetCSSLoader(cssLoader);
if (NS_SUCCEEDED(rv)) {
if (cssLoader) {
PRBool complete;
rv = cssLoader->LoadAgentSheet(uaURL, cssStyleSheet, complete,
ApplyStyleSheetToPresShellDocument,
mPresShell);
if (NS_SUCCEEDED(rv)) {
if (complete) {
if (cssStyleSheet) {
ApplyStyleSheetToPresShellDocument(cssStyleSheet,
mPresShell);
}
else
rv = NS_ERROR_NULL_POINTER;
}
//
// If not complete, we will be notified later
// with a call to AddStyleSheetToEditorDocument().
//
}
}
else
rv = NS_ERROR_NULL_POINTER;
}
}
else
rv = NS_ERROR_NULL_POINTER;
}
else
rv = NS_ERROR_NULL_POINTER;
}
NS_RELEASE(uaURL);
}
return rv;
}
NS_IMETHODIMP
nsEditor::AddEditActionListener(nsIEditActionListener *aListener)
{

View File

@ -229,6 +229,8 @@ public:
NS_IMETHOD InsertAsQuotation(const nsString& aQuotedText);
NS_IMETHOD ApplyStyleSheet(const nsString& aURL);
NS_IMETHOD AddEditActionListener(nsIEditActionListener *aListener);
NS_IMETHOD RemoveEditActionListener(nsIEditActionListener *aListener);

View File

@ -90,7 +90,6 @@
#include "nsITransferable.h"
#include "nsISupportsArray.h"
/* Define Class IDs */
static NS_DEFINE_IID(kAppShellServiceCID, NS_APPSHELL_SERVICE_CID);
static NS_DEFINE_IID(kEditorAppCoreCID, NS_EDITORAPPCORE_CID);
@ -616,6 +615,35 @@ NS_IMETHODIMP nsEditorShell::SetBackgroundColor(const PRUnichar *color)
return result;
}
NS_IMETHODIMP nsEditorShell::ApplyStyleSheet(const PRUnichar *url)
{
nsresult result = NS_NOINTERFACE;
nsAutoString aURL(url);
switch (mEditorType)
{
case ePlainTextEditorType:
{
nsCOMPtr<nsITextEditor> textEditor = do_QueryInterface(mEditor);
if (textEditor)
result = textEditor->ApplyStyleSheet(aURL);
}
break;
case eHTMLTextEditorType:
{
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
if (htmlEditor)
result = htmlEditor->ApplyStyleSheet(aURL);
}
break;
default:
result = NS_ERROR_NOT_IMPLEMENTED;
}
return result;
}
NS_IMETHODIMP nsEditorShell::SetBodyAttribute(const PRUnichar *attr, const PRUnichar *value)
{
nsresult result = NS_NOINTERFACE;

View File

@ -161,6 +161,8 @@ class nsEditorShell : public nsIEditorShell,
NS_IMETHOD SetBodyAttribute(const PRUnichar *attr, const PRUnichar *value);
NS_IMETHOD SetBackgroundColor(const PRUnichar *color);
NS_IMETHOD ApplyStyleSheet(const PRUnichar *url);
/* string GetLocalFileURL (in nsIDOMWindow parent, in string filterType); */
NS_IMETHOD GetLocalFileURL(nsIDOMWindow *parent, const PRUnichar *filterType, PRUnichar **_retval);

View File

@ -708,6 +708,12 @@ nsHTMLEditor::CopyAttributes(nsIDOMNode *aDestNode, nsIDOMNode *aSourceNode)
return nsTextEditor::CopyAttributes(aDestNode, aSourceNode);
}
NS_IMETHODIMP
nsHTMLEditor::ApplyStyleSheet(const nsString& aURL)
{
return nsTextEditor::ApplyStyleSheet(aURL);
}
//================================================================
// HTML Editor methods
//

View File

@ -113,6 +113,9 @@ public:
NS_IMETHOD OutputTextToStream(nsIOutputStream* aOutputStream,nsString* aCharsetOverride, PRBool aSelectionOnly);
NS_IMETHOD OutputHTMLToStream(nsIOutputStream* aOutputStream,nsString* aCharsetOverride, PRBool aSelectionOnly);
// Miscellaneous
NS_IMETHOD ApplyStyleSheet(const nsString& aURL);
// Logging methods
NS_IMETHOD StartLogging(nsIFileSpec *aLogFile);

View File

@ -839,6 +839,18 @@ nsJSEditorLog::EndComposition(void)
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsJSEditorLog::ApplyStyleSheet(const nsString& aURL)
{
Write("window.editorShell.ApplyStyleSheet(\"");
PrintUnicode(aURL);
Write("\");\n");
Flush();
return NS_OK;
}
NS_IMETHODIMP
nsJSEditorLog::StartLogging(nsIFileSpec *aLogFile)
{

View File

@ -104,6 +104,8 @@ public:
NS_IMETHOD OutputTextToStream(nsIOutputStream* aOutputStream, nsString* aCharsetOverride, PRBool aSelectionOnly);
NS_IMETHOD OutputHTMLToStream(nsIOutputStream* aOutputStream, nsString* aCharsetOverride, PRBool aSelectionOnly);
NS_IMETHOD ApplyStyleSheet(const nsString& aURL);
NS_IMETHOD GetLocalFileURL(nsIDOMWindow* aParent, const nsString& aFilterType, nsString& aReturn);
NS_IMETHOD SetBackgroundColor(const nsString& aColor);
NS_IMETHOD SetBodyAttribute(const nsString& aAttr, const nsString& aValue);

View File

@ -1505,6 +1505,11 @@ NS_IMETHODIMP nsTextEditor::SetBodyWrapWidth(PRInt32 aWrapColumn)
return res;
}
NS_IMETHODIMP nsTextEditor::ApplyStyleSheet(const nsString& aURL)
{
return nsEditor::ApplyStyleSheet(aURL);
}
NS_IMETHODIMP nsTextEditor::OutputTextToString(nsString& aOutputString, PRBool aSelectionOnly)
{
PRBool cancel;
@ -1705,7 +1710,6 @@ NS_IMETHODIMP nsTextEditor::OutputHTMLToStream(nsIOutputStream* aOutputStream,ns
return encoder->EncodeToStream(aOutputStream);
}
nsCOMPtr<nsIDOMElement>
nsTextEditor::FindPreElement()
{

View File

@ -122,6 +122,9 @@ public:
NS_IMETHOD GetBodyWrapWidth(PRInt32 *aWrapColumn);
NS_IMETHOD SetBodyWrapWidth(PRInt32 aWrapColumn);
// Miscellaneous
NS_IMETHOD ApplyStyleSheet(const nsString& aURL);
// Logging methods
NS_IMETHOD StartLogging(nsIFileSpec *aLogFile);

View File

@ -90,7 +90,6 @@
#include "nsITransferable.h"
#include "nsISupportsArray.h"
/* Define Class IDs */
static NS_DEFINE_IID(kAppShellServiceCID, NS_APPSHELL_SERVICE_CID);
static NS_DEFINE_IID(kEditorAppCoreCID, NS_EDITORAPPCORE_CID);
@ -616,6 +615,35 @@ NS_IMETHODIMP nsEditorShell::SetBackgroundColor(const PRUnichar *color)
return result;
}
NS_IMETHODIMP nsEditorShell::ApplyStyleSheet(const PRUnichar *url)
{
nsresult result = NS_NOINTERFACE;
nsAutoString aURL(url);
switch (mEditorType)
{
case ePlainTextEditorType:
{
nsCOMPtr<nsITextEditor> textEditor = do_QueryInterface(mEditor);
if (textEditor)
result = textEditor->ApplyStyleSheet(aURL);
}
break;
case eHTMLTextEditorType:
{
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
if (htmlEditor)
result = htmlEditor->ApplyStyleSheet(aURL);
}
break;
default:
result = NS_ERROR_NOT_IMPLEMENTED;
}
return result;
}
NS_IMETHODIMP nsEditorShell::SetBodyAttribute(const PRUnichar *attr, const PRUnichar *value)
{
nsresult result = NS_NOINTERFACE;

View File

@ -161,6 +161,8 @@ class nsEditorShell : public nsIEditorShell,
NS_IMETHOD SetBodyAttribute(const PRUnichar *attr, const PRUnichar *value);
NS_IMETHOD SetBackgroundColor(const PRUnichar *color);
NS_IMETHOD ApplyStyleSheet(const PRUnichar *url);
/* string GetLocalFileURL (in nsIDOMWindow parent, in string filterType); */
NS_IMETHOD GetLocalFileURL(nsIDOMWindow *parent, const PRUnichar *filterType, PRUnichar **_retval);

View File

@ -107,6 +107,8 @@ interface nsIEditorShell : nsISupports
void GetTextProperty(in wstring prop, in wstring attr, in wstring value, out wstring firstHas, out wstring anyHas, out wstring allHas);
void SetBodyAttribute(in wstring attr, in wstring value);
void SetBackgroundColor(in wstring color);
void ApplyStyleSheet(in wstring url);
/* Utility */
wstring GetLocalFileURL(in nsIDOMWindow parent, in wstring filterType);

View File

@ -51,12 +51,20 @@
#include "nsIStyleContext.h"
#include "nsIEditActionListener.h"
#include "nsICSSLoader.h"
#include "nsICSSStyleSheet.h"
#include "nsIHTMLContentContainer.h"
#include "nsIStyleSet.h"
#include "nsIDocumentObserver.h"
#ifdef NECKO
#include "nsIIOService.h"
#include "nsIURL.h"
#include "nsIServiceManager.h"
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
#else
#include "nsIURL.h"
#endif // NECKO
#include "nsEditorShell.h"
@ -1332,6 +1340,117 @@ NS_IMETHODIMP nsEditor::InsertAsQuotation(const nsString& aQuotedText)
return InsertText(aQuotedText);
}
extern "C" void
ApplyStyleSheetToPresShellDocument(nsICSSStyleSheet* aSheet, void *aData)
{
nsresult rv;
nsIPresShell *presShell = (nsIPresShell *)aData;
if (presShell) {
nsCOMPtr<nsIStyleSet> styleSet;
rv = presShell->GetStyleSet(getter_AddRefs(styleSet));
if (NS_SUCCEEDED(rv) && styleSet) {
styleSet->AppendOverrideStyleSheet(aSheet);
nsCOMPtr<nsIDocumentObserver> observer = do_QueryInterface(presShell);
nsCOMPtr<nsIStyleSheet> styleSheet = do_QueryInterface(aSheet);
nsCOMPtr<nsIDocument> document;
rv = presShell->GetDocument(getter_AddRefs(document));
if (NS_SUCCEEDED(rv) && document && observer && styleSheet)
rv = observer->StyleSheetAdded(document, styleSheet);
}
}
}
NS_IMETHODIMP nsEditor::ApplyStyleSheet(const nsString& aURL)
{
#ifdef ENABLE_JS_EDITOR_LOG
nsAutoJSEditorLogLock logLock(mJSEditorLog);
if (mJSEditorLog)
mJSEditorLog->ApplyStyleSheet(aURL);
#endif // ENABLE_JS_EDITOR_LOG
// XXX: Note that this is not an undo-able action yet!
nsresult rv = NS_OK;
nsIURI* uaURL = 0;
#ifndef NECKO
rv = NS_NewURL(&uaURL, aURL);
#else
NS_WITH_SERVICE(nsIIOService, service, kIOServiceCID, &rv);
if (NS_FAILED(rv)) return rv;
nsIURI *uri = nsnull;
rv = service->NewURI(aURL, nsnull, &uri);
if (NS_FAILED(rv)) return rv;
rv = uri->QueryInterface(nsIURI::GetIID(), (void**)&uaURL);
NS_RELEASE(uri);
#endif // NECKO
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsIDocument> document;
rv = mPresShell->GetDocument(getter_AddRefs(document));
if (NS_SUCCEEDED(rv)) {
if (document) {
nsCOMPtr<nsIHTMLContentContainer> container = do_QueryInterface(document);
if (container) {
nsICSSLoader *cssLoader = 0;
nsICSSStyleSheet *cssStyleSheet = 0;
rv = container->GetCSSLoader(cssLoader);
if (NS_SUCCEEDED(rv)) {
if (cssLoader) {
PRBool complete;
rv = cssLoader->LoadAgentSheet(uaURL, cssStyleSheet, complete,
ApplyStyleSheetToPresShellDocument,
mPresShell);
if (NS_SUCCEEDED(rv)) {
if (complete) {
if (cssStyleSheet) {
ApplyStyleSheetToPresShellDocument(cssStyleSheet,
mPresShell);
}
else
rv = NS_ERROR_NULL_POINTER;
}
//
// If not complete, we will be notified later
// with a call to AddStyleSheetToEditorDocument().
//
}
}
else
rv = NS_ERROR_NULL_POINTER;
}
}
else
rv = NS_ERROR_NULL_POINTER;
}
else
rv = NS_ERROR_NULL_POINTER;
}
NS_RELEASE(uaURL);
}
return rv;
}
NS_IMETHODIMP
nsEditor::AddEditActionListener(nsIEditActionListener *aListener)
{

View File

@ -229,6 +229,8 @@ public:
NS_IMETHOD InsertAsQuotation(const nsString& aQuotedText);
NS_IMETHOD ApplyStyleSheet(const nsString& aURL);
NS_IMETHOD AddEditActionListener(nsIEditActionListener *aListener);
NS_IMETHOD RemoveEditActionListener(nsIEditActionListener *aListener);

View File

@ -708,6 +708,12 @@ nsHTMLEditor::CopyAttributes(nsIDOMNode *aDestNode, nsIDOMNode *aSourceNode)
return nsTextEditor::CopyAttributes(aDestNode, aSourceNode);
}
NS_IMETHODIMP
nsHTMLEditor::ApplyStyleSheet(const nsString& aURL)
{
return nsTextEditor::ApplyStyleSheet(aURL);
}
//================================================================
// HTML Editor methods
//

View File

@ -113,6 +113,9 @@ public:
NS_IMETHOD OutputTextToStream(nsIOutputStream* aOutputStream,nsString* aCharsetOverride, PRBool aSelectionOnly);
NS_IMETHOD OutputHTMLToStream(nsIOutputStream* aOutputStream,nsString* aCharsetOverride, PRBool aSelectionOnly);
// Miscellaneous
NS_IMETHOD ApplyStyleSheet(const nsString& aURL);
// Logging methods
NS_IMETHOD StartLogging(nsIFileSpec *aLogFile);

View File

@ -378,6 +378,12 @@ public:
*/
NS_IMETHOD InsertAsQuotation(const nsString& aQuotedText)=0;
/** load and apply the style sheet, specified by aURL, to
* the editor's document.
* @param aURL The style sheet to be loaded and applied.
*/
NS_IMETHOD ApplyStyleSheet(const nsString& aURL)=0;
/** add an EditActionListener to the editors list of listeners. */
NS_IMETHOD AddEditActionListener(nsIEditActionListener *aListener)=0;

View File

@ -108,6 +108,8 @@ public:
NS_IMETHOD OutputTextToStream(nsIOutputStream* aOutputStream, nsString* aCharsetOverride, PRBool aSelectionOnly)=0;
NS_IMETHOD OutputHTMLToStream(nsIOutputStream* aOutputStream, nsString* aCharsetOverride, PRBool aSelectionOnly)=0;
NS_IMETHOD ApplyStyleSheet(const nsString& aURL)=0;
// Miscellaneous Methods
/** Set the background color of the selected table cell, row, columne, or table,
* or the document background if not in a table

View File

@ -348,10 +348,16 @@ public:
* -1 = no wrap at all
*
*/
NS_IMETHOD GetBodyWrapWidth(PRUint32 *aWrapColumn)=0;
NS_IMETHOD SetBodyWrapWidth(PRUint32 aWrapColumn)=0;
NS_IMETHOD GetBodyWrapWidth(PRInt32 *aWrapColumn)=0;
NS_IMETHOD SetBodyWrapWidth(PRInt32 aWrapColumn)=0;
// Miscellaneous Methods
/** Apply the style sheet, specified by aURL, to the
* text editor's document.
*/
NS_IMETHOD ApplyStyleSheet(const nsString& aURL)=0;
/*
NS_IMETHOD CheckSpelling()=0;
NS_IMETHOD SpellingLanguage(nsIAtom *aLanguage)=0;

View File

@ -436,6 +436,14 @@ function OnCreateAlignmentPopup()
// --------------------------- Debug stuff ---------------------------
function EditorApplyStyleSheet(url)
{
if (window.editorShell)
{
window.editorShell.ApplyStyleSheet(url);
}
}
function EditorExecuteScript(fileSpec)
{
fileSpec.openStreamForReading();