Automated checkin: version bump remove "pre" from version number for firefox 3.7a1...
[mozilla-central.git] / docshell / base / nsDocShellEditorData.cpp
blob26d2510bc7b9382a5f3215174f998851aaa0a031
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is the Mozilla browser.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications, Inc.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Simon Fraser <sfraser@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 #include "nsIComponentManager.h"
42 #include "nsIInterfaceRequestorUtils.h"
43 #include "nsIDOMWindow.h"
44 #include "nsIDocShellTreeItem.h"
45 #include "nsIDOMDocument.h"
46 #include "nsDocShellEditorData.h"
48 /*---------------------------------------------------------------------------
50 nsDocShellEditorData
52 ----------------------------------------------------------------------------*/
54 nsDocShellEditorData::nsDocShellEditorData(nsIDocShell* inOwningDocShell)
55 : mDocShell(inOwningDocShell)
56 , mMakeEditable(PR_FALSE)
57 , mIsDetached(PR_FALSE)
58 , mDetachedMakeEditable(PR_FALSE)
59 , mDetachedEditingState(nsIHTMLDocument::eOff)
61 NS_ASSERTION(mDocShell, "Where is my docShell?");
65 /*---------------------------------------------------------------------------
67 ~nsDocShellEditorData
69 ----------------------------------------------------------------------------*/
70 nsDocShellEditorData::~nsDocShellEditorData()
72 TearDownEditor();
75 void
76 nsDocShellEditorData::TearDownEditor()
78 if (mEditor) {
79 mEditor->PreDestroy(PR_FALSE);
80 mEditor = nsnull;
82 mEditingSession = nsnull;
83 mIsDetached = PR_FALSE;
87 /*---------------------------------------------------------------------------
89 MakeEditable
91 ----------------------------------------------------------------------------*/
92 nsresult
93 nsDocShellEditorData::MakeEditable(PRBool inWaitForUriLoad)
95 if (mMakeEditable)
96 return NS_OK;
98 // if we are already editable, and are getting turned off,
99 // nuke the editor.
100 if (mEditor)
102 NS_WARNING("Destroying existing editor on frame");
104 mEditor->PreDestroy(PR_FALSE);
105 mEditor = nsnull;
108 if (inWaitForUriLoad)
109 mMakeEditable = PR_TRUE;
110 return NS_OK;
114 /*---------------------------------------------------------------------------
116 GetEditable
118 ----------------------------------------------------------------------------*/
119 PRBool
120 nsDocShellEditorData::GetEditable()
122 return mMakeEditable || (mEditor != nsnull);
125 /*---------------------------------------------------------------------------
127 CreateEditor
129 ----------------------------------------------------------------------------*/
130 nsresult
131 nsDocShellEditorData::CreateEditor()
133 nsCOMPtr<nsIEditingSession> editingSession;
134 nsresult rv = GetEditingSession(getter_AddRefs(editingSession));
135 if (NS_FAILED(rv)) return rv;
137 nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
138 rv = editingSession->SetupEditorOnWindow(domWindow);
139 if (NS_FAILED(rv)) return rv;
141 return NS_OK;
145 /*---------------------------------------------------------------------------
147 GetEditingSession
149 ----------------------------------------------------------------------------*/
150 nsresult
151 nsDocShellEditorData::GetEditingSession(nsIEditingSession **outEditingSession)
153 nsresult rv = EnsureEditingSession();
154 NS_ENSURE_SUCCESS(rv, rv);
156 NS_ADDREF(*outEditingSession = mEditingSession);
158 return NS_OK;
162 /*---------------------------------------------------------------------------
164 GetEditor
166 ----------------------------------------------------------------------------*/
167 nsresult
168 nsDocShellEditorData::GetEditor(nsIEditor **outEditor)
170 NS_ENSURE_ARG_POINTER(outEditor);
171 NS_IF_ADDREF(*outEditor = mEditor);
172 return NS_OK;
176 /*---------------------------------------------------------------------------
178 SetEditor
180 ----------------------------------------------------------------------------*/
181 nsresult
182 nsDocShellEditorData::SetEditor(nsIEditor *inEditor)
184 // destroy any editor that we have. Checks for equality are
185 // necessary to ensure that assigment into the nsCOMPtr does
186 // not temporarily reduce the refCount of the editor to zero
187 if (mEditor.get() != inEditor)
189 if (mEditor)
191 mEditor->PreDestroy(PR_FALSE);
192 mEditor = nsnull;
195 mEditor = inEditor; // owning addref
196 if (!mEditor)
197 mMakeEditable = PR_FALSE;
200 return NS_OK;
204 /*---------------------------------------------------------------------------
206 EnsureEditingSession
208 This creates the editing session on the content docShell that owns
209 'this'.
211 ----------------------------------------------------------------------------*/
212 nsresult
213 nsDocShellEditorData::EnsureEditingSession()
215 NS_ASSERTION(mDocShell, "Should have docShell here");
216 NS_ASSERTION(!mIsDetached, "This will stomp editing session!");
218 nsresult rv = NS_OK;
220 if (!mEditingSession)
222 mEditingSession =
223 do_CreateInstance("@mozilla.org/editor/editingsession;1", &rv);
226 return rv;
229 nsresult
230 nsDocShellEditorData::DetachFromWindow()
232 NS_ASSERTION(mEditingSession,
233 "Can't detach when we don't have a session to detach!");
235 nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
236 nsresult rv = mEditingSession->DetachFromWindow(domWindow);
237 NS_ENSURE_SUCCESS(rv, rv);
239 mIsDetached = PR_TRUE;
240 mDetachedMakeEditable = mMakeEditable;
241 mMakeEditable = PR_FALSE;
243 nsCOMPtr<nsIDOMDocument> domDoc;
244 domWindow->GetDocument(getter_AddRefs(domDoc));
245 nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
246 if (htmlDoc)
247 mDetachedEditingState = htmlDoc->GetEditingState();
249 mDocShell = nsnull;
251 return NS_OK;
254 nsresult
255 nsDocShellEditorData::ReattachToWindow(nsIDocShell* aDocShell)
257 mDocShell = aDocShell;
259 nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
260 nsresult rv = mEditingSession->ReattachToWindow(domWindow);
261 NS_ENSURE_SUCCESS(rv, rv);
263 mIsDetached = PR_FALSE;
264 mMakeEditable = mDetachedMakeEditable;
266 nsCOMPtr<nsIDOMDocument> domDoc;
267 domWindow->GetDocument(getter_AddRefs(domDoc));
268 nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
269 if (htmlDoc)
270 htmlDoc->SetEditingState(mDetachedEditingState);
272 return NS_OK;