Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / dom / base / nsWindowRoot.cpp
blob3bfb62a17ca9cf5ab70f7269b5afa4f92b0744e6
1 /* -*- Mode: C++; tab-width: 3; 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 * David W. Hyatt <hyatt@netscape.com> (Original Author)
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 ***** */
40 #include "nsCOMPtr.h"
41 #include "nsWindowRoot.h"
42 #include "nsPIDOMWindow.h"
43 #include "nsIDOMWindow.h"
44 #include "nsIDOMDocument.h"
45 #include "nsIDocument.h"
46 #include "nsIEventListenerManager.h"
47 #include "nsPresContext.h"
48 #include "nsLayoutCID.h"
49 #include "nsContentCID.h"
50 #include "nsIEventStateManager.h"
51 #include "nsIPrivateDOMEvent.h"
52 #include "nsIDOMWindowInternal.h"
53 #include "nsString.h"
54 #include "nsEventDispatcher.h"
55 #include "nsIProgrammingLanguage.h"
56 #include "nsGUIEvent.h"
57 #include "nsGlobalWindow.h"
58 #include "nsFocusManager.h"
59 #include "nsIDOMHTMLInputElement.h"
60 #include "nsIDOMNSHTMLTextAreaElement.h"
61 #include "nsIControllers.h"
63 #include "nsCycleCollectionParticipant.h"
65 #ifdef MOZ_XUL
66 #include "nsIDOMXULElement.h"
67 #endif
69 static NS_DEFINE_CID(kEventListenerManagerCID, NS_EVENTLISTENERMANAGER_CID);
71 nsWindowRoot::nsWindowRoot(nsPIDOMWindow* aWindow)
73 mWindow = aWindow;
76 nsWindowRoot::~nsWindowRoot()
78 if (mListenerManager) {
79 mListenerManager->Disconnect();
83 NS_IMPL_CYCLE_COLLECTION_3(nsWindowRoot, mListenerManager, mPopupNode,
84 mParent)
86 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsWindowRoot)
87 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMEventTarget)
88 NS_INTERFACE_MAP_ENTRY(nsPIDOMEventTarget)
89 NS_INTERFACE_MAP_ENTRY(nsPIWindowRoot)
90 NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget)
91 NS_INTERFACE_MAP_ENTRY(nsIDOM3EventTarget)
92 NS_INTERFACE_MAP_ENTRY(nsIDOMNSEventTarget)
93 NS_INTERFACE_MAP_END
95 NS_IMPL_CYCLE_COLLECTING_ADDREF_AMBIGUOUS(nsWindowRoot, nsIDOMEventTarget)
96 NS_IMPL_CYCLE_COLLECTING_RELEASE_AMBIGUOUS(nsWindowRoot, nsIDOMEventTarget)
98 NS_IMETHODIMP
99 nsWindowRoot::AddEventListener(const nsAString& aType, nsIDOMEventListener* aListener, PRBool aUseCapture)
101 return AddEventListener(aType, aListener, aUseCapture, PR_FALSE, 0);
104 NS_IMETHODIMP
105 nsWindowRoot::RemoveEventListener(const nsAString& aType, nsIDOMEventListener* aListener, PRBool aUseCapture)
107 return RemoveGroupedEventListener(aType, aListener, aUseCapture, nsnull);
110 NS_IMETHODIMP
111 nsWindowRoot::DispatchEvent(nsIDOMEvent* aEvt, PRBool *_retval)
113 nsEventStatus status = nsEventStatus_eIgnore;
114 nsresult rv = nsEventDispatcher::DispatchDOMEvent(
115 static_cast<nsPIDOMEventTarget*>(this), nsnull, aEvt, nsnull, &status);
116 *_retval = (status != nsEventStatus_eConsumeNoDefault);
117 return rv;
120 nsresult
121 nsWindowRoot::DispatchDOMEvent(nsEvent* aEvent,
122 nsIDOMEvent* aDOMEvent,
123 nsPresContext* aPresContext,
124 nsEventStatus* aEventStatus)
126 return nsEventDispatcher::DispatchDOMEvent(static_cast<nsPIDOMEventTarget*>(this),
127 aEvent, aDOMEvent,
128 aPresContext, aEventStatus);
131 NS_IMETHODIMP
132 nsWindowRoot::AddGroupedEventListener(const nsAString & aType, nsIDOMEventListener *aListener,
133 PRBool aUseCapture, nsIDOMEventGroup *aEvtGrp)
135 nsCOMPtr<nsIEventListenerManager> manager = GetListenerManager(PR_TRUE);
136 NS_ENSURE_STATE(manager);
137 PRInt32 flags = aUseCapture ? NS_EVENT_FLAG_CAPTURE : NS_EVENT_FLAG_BUBBLE;
138 return manager->AddEventListenerByType(aListener, aType, flags, aEvtGrp);
141 NS_IMETHODIMP
142 nsWindowRoot::RemoveGroupedEventListener(const nsAString & aType, nsIDOMEventListener *aListener,
143 PRBool aUseCapture, nsIDOMEventGroup *aEvtGrp)
145 if (mListenerManager) {
146 PRInt32 flags = aUseCapture ? NS_EVENT_FLAG_CAPTURE : NS_EVENT_FLAG_BUBBLE;
147 return mListenerManager->RemoveEventListenerByType(aListener, aType, flags,
148 aEvtGrp);
150 return NS_OK;
153 NS_IMETHODIMP
154 nsWindowRoot::CanTrigger(const nsAString & type, PRBool *_retval)
156 return NS_ERROR_NOT_IMPLEMENTED;
159 NS_IMETHODIMP
160 nsWindowRoot::IsRegisteredHere(const nsAString & type, PRBool *_retval)
162 return NS_ERROR_NOT_IMPLEMENTED;
165 NS_IMETHODIMP
166 nsWindowRoot::AddEventListener(const nsAString& aType,
167 nsIDOMEventListener *aListener,
168 PRBool aUseCapture, PRBool aWantsUntrusted,
169 PRUint8 optional_argc)
171 NS_ASSERTION(!aWantsUntrusted || optional_argc > 0,
172 "Won't check if this is chrome, you want to set "
173 "aWantsUntrusted to PR_FALSE or make the aWantsUntrusted "
174 "explicit by making optional_argc non-zero.");
176 nsIEventListenerManager* manager = GetListenerManager(PR_TRUE);
177 NS_ENSURE_STATE(manager);
179 PRInt32 flags = aUseCapture ? NS_EVENT_FLAG_CAPTURE : NS_EVENT_FLAG_BUBBLE;
181 if (aWantsUntrusted) {
182 flags |= NS_PRIV_EVENT_UNTRUSTED_PERMITTED;
185 return manager->AddEventListenerByType(aListener, aType, flags, nsnull);
188 nsresult
189 nsWindowRoot::AddEventListenerByIID(nsIDOMEventListener *aListener, const nsIID& aIID)
191 nsIEventListenerManager* manager = GetListenerManager(PR_TRUE);
192 NS_ENSURE_STATE(manager);
193 return manager->AddEventListenerByIID(aListener, aIID, NS_EVENT_FLAG_BUBBLE);
196 nsresult
197 nsWindowRoot::RemoveEventListenerByIID(nsIDOMEventListener *aListener, const nsIID& aIID)
199 nsIEventListenerManager* manager = GetListenerManager(PR_TRUE);
200 if (manager) {
201 return manager->RemoveEventListenerByIID(aListener, aIID,
202 NS_EVENT_FLAG_BUBBLE);
204 return NS_OK;
207 nsIEventListenerManager*
208 nsWindowRoot::GetListenerManager(PRBool aCreateIfNotFound)
210 if (!mListenerManager) {
211 if (!aCreateIfNotFound) {
212 return nsnull;
215 mListenerManager = do_CreateInstance(kEventListenerManagerCID);
216 if (mListenerManager) {
217 mListenerManager->SetListenerTarget(
218 static_cast<nsPIDOMEventTarget*>(this));
222 return mListenerManager;
225 nsresult
226 nsWindowRoot::GetSystemEventGroup(nsIDOMEventGroup **aGroup)
228 nsIEventListenerManager* manager = GetListenerManager(PR_TRUE);
229 NS_ENSURE_STATE(manager);
230 return manager->GetSystemEventGroupLM(aGroup);
234 nsresult
235 nsWindowRoot::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
237 aVisitor.mCanHandle = PR_TRUE;
238 aVisitor.mForceContentDispatch = PR_TRUE; //FIXME! Bug 329119
239 // To keep mWindow alive
240 aVisitor.mItemData = static_cast<nsISupports *>(mWindow);
241 aVisitor.mParentTarget = mParent;
242 return NS_OK;
245 nsresult
246 nsWindowRoot::PostHandleEvent(nsEventChainPostVisitor& aVisitor)
248 return NS_OK;
251 nsPIDOMWindow*
252 nsWindowRoot::GetWindow()
254 return mWindow;
257 NS_IMETHODIMP
258 nsWindowRoot::GetScriptTypeID(PRUint32 *aScriptType)
260 NS_ERROR("No default script type here - ask some element");
261 return nsIProgrammingLanguage::UNKNOWN;
264 NS_IMETHODIMP
265 nsWindowRoot::SetScriptTypeID(PRUint32 aScriptType)
267 NS_ERROR("Can't change default script type for a document");
268 return NS_ERROR_NOT_IMPLEMENTED;
271 nsresult
272 nsWindowRoot::GetControllers(nsIControllers** aResult)
274 *aResult = nsnull;
276 // XXX: we should fix this so there's a generic interface that
277 // describes controllers, so this code would have no special
278 // knowledge of what object might have controllers.
280 nsCOMPtr<nsPIDOMWindow> focusedWindow;
281 nsIContent* focusedContent =
282 nsFocusManager::GetFocusedDescendant(mWindow, PR_TRUE, getter_AddRefs(focusedWindow));
283 if (focusedContent) {
284 #ifdef MOZ_XUL
285 nsCOMPtr<nsIDOMXULElement> xulElement(do_QueryInterface(focusedContent));
286 if (xulElement)
287 return xulElement->GetControllers(aResult);
288 #endif
290 nsCOMPtr<nsIDOMNSHTMLTextAreaElement> htmlTextArea =
291 do_QueryInterface(focusedContent);
292 if (htmlTextArea)
293 return htmlTextArea->GetControllers(aResult);
295 nsCOMPtr<nsIDOMHTMLInputElement> htmlInputElement =
296 do_QueryInterface(focusedContent);
297 if (htmlInputElement)
298 return htmlInputElement->GetControllers(aResult);
300 if (focusedContent->IsEditable() && focusedWindow)
301 return focusedWindow->GetControllers(aResult);
303 else {
304 nsCOMPtr<nsIDOMWindowInternal> domWindow = do_QueryInterface(focusedWindow);
305 if (domWindow)
306 return domWindow->GetControllers(aResult);
309 return NS_OK;
312 nsresult
313 nsWindowRoot::GetControllerForCommand(const char * aCommand,
314 nsIController** _retval)
316 NS_ENSURE_ARG_POINTER(_retval);
317 *_retval = nsnull;
319 nsCOMPtr<nsIControllers> controllers;
320 nsCOMPtr<nsIController> controller;
322 GetControllers(getter_AddRefs(controllers));
323 if (controllers) {
324 controllers->GetControllerForCommand(aCommand, getter_AddRefs(controller));
325 if (controller) {
326 controller.swap(*_retval);
327 return NS_OK;
331 nsCOMPtr<nsPIDOMWindow> focusedWindow;
332 nsFocusManager::GetFocusedDescendant(mWindow, PR_TRUE, getter_AddRefs(focusedWindow));
333 while (focusedWindow) {
334 nsCOMPtr<nsIDOMWindowInternal> domWindow(do_QueryInterface(focusedWindow));
336 nsCOMPtr<nsIControllers> controllers2;
337 domWindow->GetControllers(getter_AddRefs(controllers2));
338 if (controllers2) {
339 controllers2->GetControllerForCommand(aCommand,
340 getter_AddRefs(controller));
341 if (controller) {
342 controller.swap(*_retval);
343 return NS_OK;
347 // XXXndeakin P3 is this casting safe?
348 nsCOMPtr<nsPIDOMWindow> piWindow = do_QueryInterface(focusedWindow);
349 nsGlobalWindow *win =
350 static_cast<nsGlobalWindow *>
351 (static_cast<nsIDOMWindowInternal *>(piWindow));
352 focusedWindow = win->GetPrivateParent();
355 return NS_OK;
358 nsIDOMNode*
359 nsWindowRoot::GetPopupNode()
361 return mPopupNode;
364 void
365 nsWindowRoot::SetPopupNode(nsIDOMNode* aNode)
367 mPopupNode = aNode;
370 ///////////////////////////////////////////////////////////////////////////////////
372 nsresult
373 NS_NewWindowRoot(nsPIDOMWindow* aWindow, nsPIDOMEventTarget** aResult)
375 *aResult = new nsWindowRoot(aWindow);
376 if (!*aResult)
377 return NS_ERROR_OUT_OF_MEMORY;
378 NS_ADDREF(*aResult);
379 return NS_OK;