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
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.
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 /*---------------------------------------------------------------------------
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 /*---------------------------------------------------------------------------
69 ----------------------------------------------------------------------------*/
70 nsDocShellEditorData::~nsDocShellEditorData()
76 nsDocShellEditorData::TearDownEditor()
79 mEditor
->PreDestroy(PR_FALSE
);
82 mEditingSession
= nsnull
;
83 mIsDetached
= PR_FALSE
;
87 /*---------------------------------------------------------------------------
91 ----------------------------------------------------------------------------*/
93 nsDocShellEditorData::MakeEditable(PRBool inWaitForUriLoad
)
98 // if we are already editable, and are getting turned off,
102 NS_WARNING("Destroying existing editor on frame");
104 mEditor
->PreDestroy(PR_FALSE
);
108 if (inWaitForUriLoad
)
109 mMakeEditable
= PR_TRUE
;
114 /*---------------------------------------------------------------------------
118 ----------------------------------------------------------------------------*/
120 nsDocShellEditorData::GetEditable()
122 return mMakeEditable
|| (mEditor
!= nsnull
);
125 /*---------------------------------------------------------------------------
129 ----------------------------------------------------------------------------*/
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
;
145 /*---------------------------------------------------------------------------
149 ----------------------------------------------------------------------------*/
151 nsDocShellEditorData::GetEditingSession(nsIEditingSession
**outEditingSession
)
153 nsresult rv
= EnsureEditingSession();
154 NS_ENSURE_SUCCESS(rv
, rv
);
156 NS_ADDREF(*outEditingSession
= mEditingSession
);
162 /*---------------------------------------------------------------------------
166 ----------------------------------------------------------------------------*/
168 nsDocShellEditorData::GetEditor(nsIEditor
**outEditor
)
170 NS_ENSURE_ARG_POINTER(outEditor
);
171 NS_IF_ADDREF(*outEditor
= mEditor
);
176 /*---------------------------------------------------------------------------
180 ----------------------------------------------------------------------------*/
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
)
191 mEditor
->PreDestroy(PR_FALSE
);
195 mEditor
= inEditor
; // owning addref
197 mMakeEditable
= PR_FALSE
;
204 /*---------------------------------------------------------------------------
208 This creates the editing session on the content docShell that owns
211 ----------------------------------------------------------------------------*/
213 nsDocShellEditorData::EnsureEditingSession()
215 NS_ASSERTION(mDocShell
, "Should have docShell here");
216 NS_ASSERTION(!mIsDetached
, "This will stomp editing session!");
220 if (!mEditingSession
)
223 do_CreateInstance("@mozilla.org/editor/editingsession;1", &rv
);
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
);
247 mDetachedEditingState
= htmlDoc
->GetEditingState();
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
);
270 htmlDoc
->SetEditingState(mDetachedEditingState
);