Back to index

lightning-sunbird  0.9+nobinonly
nsDocShellEditorData.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications, Inc.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Simon Fraser <sfraser@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 
00041 #include "nsIComponentManager.h"
00042 #include "nsIInterfaceRequestorUtils.h"
00043 
00044 #include "nsIDOMWindow.h"
00045 #include "nsIDocShellTreeItem.h"
00046 
00047 #include "nsDocShellEditorData.h"
00048 
00049 
00050 /*---------------------------------------------------------------------------
00051 
00052   nsDocShellEditorData
00053 
00054 ----------------------------------------------------------------------------*/
00055 
00056 nsDocShellEditorData::nsDocShellEditorData(nsIDocShell* inOwningDocShell)
00057 : mDocShell(inOwningDocShell)
00058 , mMakeEditable(PR_FALSE)
00059 {
00060   NS_ASSERTION(mDocShell, "Where is my docShell?");
00061 }
00062 
00063 
00064 /*---------------------------------------------------------------------------
00065 
00066   ~nsDocShellEditorData
00067 
00068 ----------------------------------------------------------------------------*/
00069 nsDocShellEditorData::~nsDocShellEditorData()
00070 {
00071   TearDownEditor();
00072 }
00073 
00074 void
00075 nsDocShellEditorData::TearDownEditor()
00076 {
00077   if (mEditingSession)
00078   {
00079     nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
00080     // This will eventually call nsDocShellEditorData::SetEditor(nsnull)
00081     //   which will call mEditorPreDestroy() and delete the editor
00082     mEditingSession->TearDownEditorOnWindow(domWindow);
00083   }
00084   else if (mEditor) // Should never have this w/o nsEditingSession!
00085   {
00086     mEditor->PreDestroy();
00087     mEditor = nsnull;     // explicit clear to make destruction order predictable
00088   }
00089 }
00090 
00091 
00092 /*---------------------------------------------------------------------------
00093 
00094   MakeEditable
00095 
00096 ----------------------------------------------------------------------------*/
00097 nsresult
00098 nsDocShellEditorData::MakeEditable(PRBool inWaitForUriLoad /*, PRBool inEditable */)
00099 {
00100   if (mMakeEditable)
00101     return NS_OK;
00102   
00103   // if we are already editable, and are getting turned off,
00104   // nuke the editor.
00105   if (mEditor)
00106   {
00107     NS_WARNING("Destroying existing editor on frame");
00108     
00109     mEditor->PreDestroy();
00110     mEditor = nsnull;
00111   }
00112   
00113   mMakeEditable = PR_TRUE;
00114   return NS_OK;
00115 }
00116 
00117 
00118 /*---------------------------------------------------------------------------
00119 
00120   GetEditable
00121 
00122 ----------------------------------------------------------------------------*/
00123 PRBool
00124 nsDocShellEditorData::GetEditable()
00125 {
00126   return mMakeEditable || (mEditor != nsnull);
00127 }
00128 
00129 /*---------------------------------------------------------------------------
00130 
00131   CreateEditor
00132 
00133 ----------------------------------------------------------------------------*/
00134 nsresult
00135 nsDocShellEditorData::CreateEditor()
00136 {
00137   nsCOMPtr<nsIEditingSession>   editingSession;    
00138   nsresult rv = GetEditingSession(getter_AddRefs(editingSession));
00139   if (NS_FAILED(rv)) return rv;
00140   
00141   nsCOMPtr<nsIDOMWindow>    domWindow = do_GetInterface(mDocShell);
00142   rv = editingSession->SetupEditorOnWindow(domWindow);
00143   if (NS_FAILED(rv)) return rv;
00144   
00145   return NS_OK;
00146 }
00147 
00148 
00149 /*---------------------------------------------------------------------------
00150 
00151   GetEditingSession
00152 
00153 ----------------------------------------------------------------------------*/
00154 nsresult
00155 nsDocShellEditorData::GetEditingSession(nsIEditingSession **outEditingSession)
00156 {
00157   nsresult rv = EnsureEditingSession();
00158   NS_ENSURE_SUCCESS(rv, rv);
00159 
00160   NS_ADDREF(*outEditingSession = mEditingSession);
00161 
00162   return NS_OK;
00163 }
00164 
00165 
00166 /*---------------------------------------------------------------------------
00167 
00168   GetEditor
00169 
00170 ----------------------------------------------------------------------------*/
00171 nsresult
00172 nsDocShellEditorData::GetEditor(nsIEditor **outEditor)
00173 {
00174   NS_ENSURE_ARG_POINTER(outEditor);
00175   NS_IF_ADDREF(*outEditor = mEditor);
00176   return NS_OK;
00177 }
00178 
00179 
00180 /*---------------------------------------------------------------------------
00181 
00182   SetEditor
00183 
00184 ----------------------------------------------------------------------------*/
00185 nsresult
00186 nsDocShellEditorData::SetEditor(nsIEditor *inEditor)
00187 {
00188   // destroy any editor that we have. Checks for equality are
00189   // necessary to ensure that assigment into the nsCOMPtr does
00190   // not temporarily reduce the refCount of the editor to zero
00191   if (mEditor.get() != inEditor)
00192   {
00193     if (mEditor)
00194     {
00195       mEditor->PreDestroy();
00196       mEditor = nsnull;
00197     }
00198       
00199     mEditor = inEditor;    // owning addref
00200   }   
00201   
00202   return NS_OK;
00203 }
00204 
00205 
00206 /*---------------------------------------------------------------------------
00207 
00208   EnsureEditingSession
00209   
00210   This creates the editing session on the content docShell that owns
00211   'this'.
00212 
00213 ----------------------------------------------------------------------------*/
00214 nsresult
00215 nsDocShellEditorData::EnsureEditingSession()
00216 {
00217   NS_ASSERTION(mDocShell, "Should have docShell here");
00218   
00219   nsresult rv = NS_OK;
00220   
00221   if (!mEditingSession)
00222   {
00223     mEditingSession =
00224       do_CreateInstance("@mozilla.org/editor/editingsession;1", &rv);
00225   }
00226 
00227   return rv;
00228 }
00229