1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is the Mozilla browser.
17 : *
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.
22 : *
23 : * Contributor(s):
24 : * Simon Fraser <sfraser@netscape.com>
25 : *
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.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 :
41 : #include "nsIComponentManager.h"
42 : #include "nsIInterfaceRequestorUtils.h"
43 : #include "nsIDOMWindow.h"
44 : #include "nsIDocShellTreeItem.h"
45 : #include "nsIDOMDocument.h"
46 : #include "nsDocShellEditorData.h"
47 :
48 : /*---------------------------------------------------------------------------
49 :
50 : nsDocShellEditorData
51 :
52 : ----------------------------------------------------------------------------*/
53 :
54 0 : nsDocShellEditorData::nsDocShellEditorData(nsIDocShell* inOwningDocShell)
55 : : mDocShell(inOwningDocShell)
56 : , mMakeEditable(false)
57 : , mIsDetached(false)
58 : , mDetachedMakeEditable(false)
59 0 : , mDetachedEditingState(nsIHTMLDocument::eOff)
60 : {
61 0 : NS_ASSERTION(mDocShell, "Where is my docShell?");
62 0 : }
63 :
64 :
65 : /*---------------------------------------------------------------------------
66 :
67 : ~nsDocShellEditorData
68 :
69 : ----------------------------------------------------------------------------*/
70 0 : nsDocShellEditorData::~nsDocShellEditorData()
71 : {
72 0 : TearDownEditor();
73 0 : }
74 :
75 : void
76 0 : nsDocShellEditorData::TearDownEditor()
77 : {
78 0 : if (mEditor) {
79 0 : mEditor->PreDestroy(false);
80 0 : mEditor = nsnull;
81 : }
82 0 : mEditingSession = nsnull;
83 0 : mIsDetached = false;
84 0 : }
85 :
86 :
87 : /*---------------------------------------------------------------------------
88 :
89 : MakeEditable
90 :
91 : ----------------------------------------------------------------------------*/
92 : nsresult
93 0 : nsDocShellEditorData::MakeEditable(bool inWaitForUriLoad)
94 : {
95 0 : if (mMakeEditable)
96 0 : return NS_OK;
97 :
98 : // if we are already editable, and are getting turned off,
99 : // nuke the editor.
100 0 : if (mEditor)
101 : {
102 0 : NS_WARNING("Destroying existing editor on frame");
103 :
104 0 : mEditor->PreDestroy(false);
105 0 : mEditor = nsnull;
106 : }
107 :
108 0 : if (inWaitForUriLoad)
109 0 : mMakeEditable = true;
110 0 : return NS_OK;
111 : }
112 :
113 :
114 : /*---------------------------------------------------------------------------
115 :
116 : GetEditable
117 :
118 : ----------------------------------------------------------------------------*/
119 : bool
120 0 : nsDocShellEditorData::GetEditable()
121 : {
122 0 : return mMakeEditable || (mEditor != nsnull);
123 : }
124 :
125 : /*---------------------------------------------------------------------------
126 :
127 : CreateEditor
128 :
129 : ----------------------------------------------------------------------------*/
130 : nsresult
131 0 : nsDocShellEditorData::CreateEditor()
132 : {
133 0 : nsCOMPtr<nsIEditingSession> editingSession;
134 0 : nsresult rv = GetEditingSession(getter_AddRefs(editingSession));
135 0 : if (NS_FAILED(rv)) return rv;
136 :
137 0 : nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
138 0 : rv = editingSession->SetupEditorOnWindow(domWindow);
139 0 : if (NS_FAILED(rv)) return rv;
140 :
141 0 : return NS_OK;
142 : }
143 :
144 :
145 : /*---------------------------------------------------------------------------
146 :
147 : GetEditingSession
148 :
149 : ----------------------------------------------------------------------------*/
150 : nsresult
151 0 : nsDocShellEditorData::GetEditingSession(nsIEditingSession **outEditingSession)
152 : {
153 0 : nsresult rv = EnsureEditingSession();
154 0 : NS_ENSURE_SUCCESS(rv, rv);
155 :
156 0 : NS_ADDREF(*outEditingSession = mEditingSession);
157 :
158 0 : return NS_OK;
159 : }
160 :
161 :
162 : /*---------------------------------------------------------------------------
163 :
164 : GetEditor
165 :
166 : ----------------------------------------------------------------------------*/
167 : nsresult
168 0 : nsDocShellEditorData::GetEditor(nsIEditor **outEditor)
169 : {
170 0 : NS_ENSURE_ARG_POINTER(outEditor);
171 0 : NS_IF_ADDREF(*outEditor = mEditor);
172 0 : return NS_OK;
173 : }
174 :
175 :
176 : /*---------------------------------------------------------------------------
177 :
178 : SetEditor
179 :
180 : ----------------------------------------------------------------------------*/
181 : nsresult
182 0 : nsDocShellEditorData::SetEditor(nsIEditor *inEditor)
183 : {
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 0 : if (mEditor.get() != inEditor)
188 : {
189 0 : if (mEditor)
190 : {
191 0 : mEditor->PreDestroy(false);
192 0 : mEditor = nsnull;
193 : }
194 :
195 0 : mEditor = inEditor; // owning addref
196 0 : if (!mEditor)
197 0 : mMakeEditable = false;
198 : }
199 :
200 0 : return NS_OK;
201 : }
202 :
203 :
204 : /*---------------------------------------------------------------------------
205 :
206 : EnsureEditingSession
207 :
208 : This creates the editing session on the content docShell that owns
209 : 'this'.
210 :
211 : ----------------------------------------------------------------------------*/
212 : nsresult
213 0 : nsDocShellEditorData::EnsureEditingSession()
214 : {
215 0 : NS_ASSERTION(mDocShell, "Should have docShell here");
216 0 : NS_ASSERTION(!mIsDetached, "This will stomp editing session!");
217 :
218 0 : nsresult rv = NS_OK;
219 :
220 0 : if (!mEditingSession)
221 : {
222 : mEditingSession =
223 0 : do_CreateInstance("@mozilla.org/editor/editingsession;1", &rv);
224 : }
225 :
226 0 : return rv;
227 : }
228 :
229 : nsresult
230 0 : nsDocShellEditorData::DetachFromWindow()
231 : {
232 0 : NS_ASSERTION(mEditingSession,
233 : "Can't detach when we don't have a session to detach!");
234 :
235 0 : nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
236 0 : nsresult rv = mEditingSession->DetachFromWindow(domWindow);
237 0 : NS_ENSURE_SUCCESS(rv, rv);
238 :
239 0 : mIsDetached = true;
240 0 : mDetachedMakeEditable = mMakeEditable;
241 0 : mMakeEditable = false;
242 :
243 0 : nsCOMPtr<nsIDOMDocument> domDoc;
244 0 : domWindow->GetDocument(getter_AddRefs(domDoc));
245 0 : nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
246 0 : if (htmlDoc)
247 0 : mDetachedEditingState = htmlDoc->GetEditingState();
248 :
249 0 : mDocShell = nsnull;
250 :
251 0 : return NS_OK;
252 : }
253 :
254 : nsresult
255 0 : nsDocShellEditorData::ReattachToWindow(nsIDocShell* aDocShell)
256 : {
257 0 : mDocShell = aDocShell;
258 :
259 0 : nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
260 0 : nsresult rv = mEditingSession->ReattachToWindow(domWindow);
261 0 : NS_ENSURE_SUCCESS(rv, rv);
262 :
263 0 : mIsDetached = false;
264 0 : mMakeEditable = mDetachedMakeEditable;
265 :
266 0 : nsCOMPtr<nsIDOMDocument> domDoc;
267 0 : domWindow->GetDocument(getter_AddRefs(domDoc));
268 0 : nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
269 0 : if (htmlDoc)
270 0 : htmlDoc->SetEditingState(mDetachedEditingState);
271 :
272 0 : return NS_OK;
273 : }
|