Back to index

lightning-sunbird  0.9+nobinonly
nsStyleSheetTxns.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 
00039 #include "nsEditor.h"
00040 
00041 #include "nsIPresShell.h"
00042 #include "nsICSSStyleSheet.h"
00043 #include "nsIDocument.h"
00044 #include "nsIDocumentObserver.h"
00045 #include "nsISelectionController.h"
00046 
00047 
00048 #include "nsStyleSheetTxns.h"
00049 
00050 static void
00051 AddStyleSheet(nsIEditor* aEditor, nsIStyleSheet* aSheet)
00052 {
00053   nsCOMPtr<nsIDOMDocument> domDoc;
00054   aEditor->GetDocument(getter_AddRefs(domDoc));
00055   nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
00056   if (doc) {
00057     doc->BeginUpdate(UPDATE_STYLE);
00058     doc->AddStyleSheet(aSheet);
00059     doc->EndUpdate(UPDATE_STYLE);
00060   }
00061 }
00062 
00063 static void
00064 RemoveStyleSheet(nsIEditor *aEditor, nsIStyleSheet *aSheet)
00065 {
00066   nsCOMPtr<nsIDOMDocument> domDoc;
00067   aEditor->GetDocument(getter_AddRefs(domDoc));
00068   nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
00069   if (doc) {
00070     doc->BeginUpdate(UPDATE_STYLE);
00071     doc->RemoveStyleSheet(aSheet);
00072     doc->EndUpdate(UPDATE_STYLE);
00073   }
00074 }
00075 
00076 AddStyleSheetTxn::AddStyleSheetTxn()
00077 :  EditTxn()
00078 ,  mEditor(NULL)
00079 {
00080 }
00081 
00082 AddStyleSheetTxn::~AddStyleSheetTxn()
00083 {
00084 }
00085 
00086 NS_IMETHODIMP
00087 AddStyleSheetTxn::Init(nsIEditor *aEditor, nsICSSStyleSheet *aSheet)
00088 {
00089   if (!aEditor)
00090     return NS_ERROR_INVALID_ARG;
00091 
00092   if (!aSheet)
00093     return NS_ERROR_INVALID_ARG;
00094 
00095   mEditor = aEditor;
00096   mSheet = do_QueryInterface(aSheet);
00097   
00098   return NS_OK;
00099 }
00100 
00101 
00102 NS_IMETHODIMP
00103 AddStyleSheetTxn::DoTransaction()
00104 {
00105   if (!mEditor || !mSheet)
00106     return NS_ERROR_NOT_INITIALIZED;
00107   
00108   AddStyleSheet(mEditor, mSheet);
00109   return NS_OK;
00110 }
00111 
00112 NS_IMETHODIMP
00113 AddStyleSheetTxn::UndoTransaction()
00114 {
00115   if (!mEditor || !mSheet)
00116     return NS_ERROR_NOT_INITIALIZED;
00117   
00118   RemoveStyleSheet(mEditor, mSheet);
00119   return NS_OK;
00120 }
00121 
00122 NS_IMETHODIMP
00123 AddStyleSheetTxn::RedoTransaction()
00124 {
00125    return DoTransaction();
00126 }
00127 
00128 NS_IMETHODIMP
00129 AddStyleSheetTxn::Merge(nsITransaction *aTransaction, PRBool *aDidMerge)
00130 {
00131   // set out param default value
00132   if (!aDidMerge)
00133     return NS_ERROR_NULL_POINTER;
00134     
00135   *aDidMerge = PR_FALSE;
00136   return NS_OK;
00137 }
00138 
00139 NS_IMETHODIMP
00140 AddStyleSheetTxn::GetTxnDescription(nsAString& aString)
00141 {
00142   aString.AssignLiteral("AddStyleSheetTxn");
00143   return NS_OK;
00144 }
00145 
00146 #ifdef XP_MAC
00147 #pragma mark -
00148 #endif
00149 
00150 
00151 RemoveStyleSheetTxn::RemoveStyleSheetTxn()
00152 :  EditTxn()
00153 ,  mEditor(NULL)
00154 {
00155 }
00156 
00157 RemoveStyleSheetTxn::~RemoveStyleSheetTxn()
00158 {
00159 }
00160 
00161 NS_IMETHODIMP
00162 RemoveStyleSheetTxn::Init(nsIEditor *aEditor, nsICSSStyleSheet *aSheet)
00163 {
00164   if (!aEditor)
00165     return NS_ERROR_INVALID_ARG;
00166 
00167   if (!aSheet)
00168     return NS_ERROR_INVALID_ARG;
00169 
00170   mEditor = aEditor;
00171   mSheet = do_QueryInterface(aSheet);
00172   
00173   return NS_OK;
00174 }
00175 
00176 
00177 NS_IMETHODIMP
00178 RemoveStyleSheetTxn::DoTransaction()
00179 {
00180   if (!mEditor || !mSheet)
00181     return NS_ERROR_NOT_INITIALIZED;
00182 
00183   RemoveStyleSheet(mEditor, mSheet);
00184   return NS_OK;
00185 }
00186 
00187 NS_IMETHODIMP
00188 RemoveStyleSheetTxn::UndoTransaction()
00189 {
00190   if (!mEditor || !mSheet)
00191     return NS_ERROR_NOT_INITIALIZED;
00192 
00193   AddStyleSheet(mEditor, mSheet);
00194   return NS_OK;
00195 }
00196 
00197 NS_IMETHODIMP
00198 RemoveStyleSheetTxn::RedoTransaction()
00199 {
00200    return DoTransaction();
00201 }
00202 
00203 NS_IMETHODIMP
00204 RemoveStyleSheetTxn::Merge(nsITransaction *aTransaction, PRBool *aDidMerge)
00205 {
00206   // set out param default value
00207   if (!aDidMerge)
00208     return NS_ERROR_NULL_POINTER;
00209     
00210   *aDidMerge = PR_FALSE;
00211   return NS_OK;
00212 }
00213 
00214 NS_IMETHODIMP
00215 RemoveStyleSheetTxn::GetTxnDescription(nsAString& aString)
00216 {
00217   aString.AssignLiteral("RemoveStyleSheetTxn");
00218   return NS_OK;
00219 }