gecko-dev/dom/base/nsWindowRoot.cpp

282 lines
8.2 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2012-05-21 11:12:37 +00:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
2000-11-04 08:21:20 +00:00
#include "nsCOMPtr.h"
#include "nsWindowRoot.h"
#include "nsPIDOMWindow.h"
2000-11-04 08:21:20 +00:00
#include "nsIDOMDocument.h"
#include "nsIDocument.h"
#include "nsEventListenerManager.h"
#include "nsPresContext.h"
2000-11-04 08:21:20 +00:00
#include "nsLayoutCID.h"
#include "nsContentCID.h"
#include "nsString.h"
#include "nsEventDispatcher.h"
#include "nsGUIEvent.h"
#include "nsGlobalWindow.h"
#include "nsFocusManager.h"
#include "nsIDOMHTMLInputElement.h"
#include "nsIDOMHTMLTextAreaElement.h"
#include "nsIControllers.h"
#include "nsCycleCollectionParticipant.h"
#ifdef MOZ_XUL
#include "nsIDOMXULElement.h"
#endif
2000-11-04 08:21:20 +00:00
static NS_DEFINE_CID(kEventListenerManagerCID, NS_EVENTLISTENERMANAGER_CID);
nsWindowRoot::nsWindowRoot(nsPIDOMWindow* aWindow)
2000-11-04 08:21:20 +00:00
{
mWindow = aWindow;
}
nsWindowRoot::~nsWindowRoot()
{
if (mListenerManager) {
mListenerManager->Disconnect();
}
2000-11-04 08:21:20 +00:00
}
NS_IMPL_CYCLE_COLLECTION_CLASS(nsWindowRoot)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsWindowRoot)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NATIVE_MEMBER(mListenerManager,
nsEventListenerManager)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mPopupNode)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mParent)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsWindowRoot)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mListenerManager)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mPopupNode)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mParent)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsWindowRoot)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMEventTarget)
NS_INTERFACE_MAP_ENTRY(nsPIWindowRoot)
NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsWindowRoot)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsWindowRoot)
2000-11-04 08:21:20 +00:00
NS_IMPL_DOMTARGET_DEFAULTS(nsWindowRoot)
2000-11-04 08:21:20 +00:00
NS_IMETHODIMP
nsWindowRoot::RemoveEventListener(const nsAString& aType, nsIDOMEventListener* aListener, bool aUseCapture)
2000-11-04 08:21:20 +00:00
{
nsRefPtr<nsEventListenerManager> elm = GetListenerManager(false);
if (elm) {
elm->RemoveEventListener(aType, aListener, aUseCapture);
}
return NS_OK;
2000-11-04 08:21:20 +00:00
}
NS_IMPL_REMOVE_SYSTEM_EVENT_LISTENER(nsWindowRoot)
2000-11-04 08:21:20 +00:00
NS_IMETHODIMP
nsWindowRoot::DispatchEvent(nsIDOMEvent* aEvt, bool *aRetVal)
2000-11-04 08:21:20 +00:00
{
nsEventStatus status = nsEventStatus_eIgnore;
nsresult rv = nsEventDispatcher::DispatchDOMEvent(
static_cast<nsIDOMEventTarget*>(this), nsnull, aEvt, nsnull, &status);
*aRetVal = (status != nsEventStatus_eConsumeNoDefault);
return rv;
2000-11-04 08:21:20 +00:00
}
nsresult
nsWindowRoot::DispatchDOMEvent(nsEvent* aEvent,
nsIDOMEvent* aDOMEvent,
nsPresContext* aPresContext,
nsEventStatus* aEventStatus)
{
return nsEventDispatcher::DispatchDOMEvent(static_cast<nsIDOMEventTarget*>(this),
aEvent, aDOMEvent,
aPresContext, aEventStatus);
}
NS_IMETHODIMP
nsWindowRoot::AddEventListener(const nsAString& aType,
nsIDOMEventListener *aListener,
bool aUseCapture, bool aWantsUntrusted,
PRUint8 aOptionalArgc)
{
NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1,
"Won't check if this is chrome, you want to set "
"aWantsUntrusted to false or make the aWantsUntrusted "
"explicit by making optional_argc non-zero.");
nsEventListenerManager* elm = GetListenerManager(true);
NS_ENSURE_STATE(elm);
elm->AddEventListener(aType, aListener, aUseCapture, aWantsUntrusted);
return NS_OK;
}
NS_IMETHODIMP
nsWindowRoot::AddSystemEventListener(const nsAString& aType,
nsIDOMEventListener *aListener,
bool aUseCapture,
bool aWantsUntrusted,
PRUint8 aOptionalArgc)
{
NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1,
"Won't check if this is chrome, you want to set "
"aWantsUntrusted to false or make the aWantsUntrusted "
"explicit by making optional_argc non-zero.");
return NS_AddSystemEventListener(this, aType, aListener, aUseCapture,
aWantsUntrusted);
}
nsEventListenerManager*
nsWindowRoot::GetListenerManager(bool aCreateIfNotFound)
2000-11-04 08:21:20 +00:00
{
if (!mListenerManager && aCreateIfNotFound) {
mListenerManager =
new nsEventListenerManager(static_cast<nsIDOMEventTarget*>(this));
2000-11-04 08:21:20 +00:00
}
return mListenerManager;
2000-11-04 08:21:20 +00:00
}
nsIScriptContext*
nsWindowRoot::GetContextForEventHandlers(nsresult* aRv)
{
*aRv = NS_OK;
return nsnull;
}
2007-02-16 00:03:57 +00:00
nsresult
nsWindowRoot::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
{
aVisitor.mCanHandle = true;
aVisitor.mForceContentDispatch = true; //FIXME! Bug 329119
// To keep mWindow alive
aVisitor.mItemData = static_cast<nsISupports *>(mWindow);
aVisitor.mParentTarget = mParent;
return NS_OK;
}
2007-02-16 00:03:57 +00:00
nsresult
nsWindowRoot::PostHandleEvent(nsEventChainPostVisitor& aVisitor)
{
return NS_OK;
}
nsPIDOMWindow*
nsWindowRoot::GetWindow()
{
return mWindow;
}
nsresult
nsWindowRoot::GetControllers(nsIControllers** aResult)
{
*aResult = nsnull;
// XXX: we should fix this so there's a generic interface that
// describes controllers, so this code would have no special
// knowledge of what object might have controllers.
nsCOMPtr<nsPIDOMWindow> focusedWindow;
nsIContent* focusedContent =
nsFocusManager::GetFocusedDescendant(mWindow, true, getter_AddRefs(focusedWindow));
if (focusedContent) {
#ifdef MOZ_XUL
nsCOMPtr<nsIDOMXULElement> xulElement(do_QueryInterface(focusedContent));
if (xulElement)
return xulElement->GetControllers(aResult);
#endif
nsCOMPtr<nsIDOMHTMLTextAreaElement> htmlTextArea =
do_QueryInterface(focusedContent);
if (htmlTextArea)
return htmlTextArea->GetControllers(aResult);
nsCOMPtr<nsIDOMHTMLInputElement> htmlInputElement =
do_QueryInterface(focusedContent);
if (htmlInputElement)
return htmlInputElement->GetControllers(aResult);
if (focusedContent->IsEditable() && focusedWindow)
return focusedWindow->GetControllers(aResult);
}
else {
nsCOMPtr<nsIDOMWindow> domWindow = do_QueryInterface(focusedWindow);
if (domWindow)
return domWindow->GetControllers(aResult);
}
return NS_OK;
}
nsresult
nsWindowRoot::GetControllerForCommand(const char * aCommand,
nsIController** _retval)
{
NS_ENSURE_ARG_POINTER(_retval);
*_retval = nsnull;
{
nsCOMPtr<nsIControllers> controllers;
GetControllers(getter_AddRefs(controllers));
if (controllers) {
nsCOMPtr<nsIController> controller;
controllers->GetControllerForCommand(aCommand, getter_AddRefs(controller));
if (controller) {
controller.forget(_retval);
return NS_OK;
}
}
}
nsCOMPtr<nsPIDOMWindow> focusedWindow;
nsFocusManager::GetFocusedDescendant(mWindow, true, getter_AddRefs(focusedWindow));
while (focusedWindow) {
nsCOMPtr<nsIControllers> controllers;
focusedWindow->GetControllers(getter_AddRefs(controllers));
if (controllers) {
nsCOMPtr<nsIController> controller;
controllers->GetControllerForCommand(aCommand,
getter_AddRefs(controller));
if (controller) {
controller.forget(_retval);
return NS_OK;
}
}
// XXXndeakin P3 is this casting safe?
nsGlobalWindow *win = static_cast<nsGlobalWindow*>(focusedWindow.get());
focusedWindow = win->GetPrivateParent();
}
return NS_OK;
}
nsIDOMNode*
nsWindowRoot::GetPopupNode()
{
return mPopupNode;
}
void
nsWindowRoot::SetPopupNode(nsIDOMNode* aNode)
{
mPopupNode = aNode;
}
2000-11-04 08:21:20 +00:00
///////////////////////////////////////////////////////////////////////////////////
nsresult
NS_NewWindowRoot(nsPIDOMWindow* aWindow, nsIDOMEventTarget** aResult)
2000-11-04 08:21:20 +00:00
{
*aResult = new nsWindowRoot(aWindow);
NS_ADDREF(*aResult);
return NS_OK;
}