Context menu listener code hooked into the window root object to receive DOM events even for subframes. b=60489, sr=hyatt

This commit is contained in:
locka%iol.ie 2000-12-01 22:13:45 +00:00
Родитель 060cd966fe
Коммит b41c0ecda4
2 изменённых файлов: 45 добавлений и 23 удалений

Просмотреть файл

@ -31,18 +31,21 @@
// Interfaces needed to be included // Interfaces needed to be included
#include "nsIContextMenuListener.h" #include "nsIContextMenuListener.h"
#include "nsIPrivateDOMEvent.h"
#include "nsIDOMNode.h" #include "nsIDOMNode.h"
#include "nsIDOMNodeList.h" #include "nsIDOMNodeList.h"
#include "nsIDOMDocument.h" #include "nsIDOMDocument.h"
#include "nsIDOMDocumentType.h" #include "nsIDOMDocumentType.h"
#include "nsIDOMElement.h" #include "nsIDOMElement.h"
#include "nsIDOMEvent.h" #include "nsIDOMEvent.h"
#include "nsIDOMEventReceiver.h"
#include "nsIDOMMouseEvent.h" #include "nsIDOMMouseEvent.h"
#include "nsIDOMEventReceiver.h" #include "nsIDOMEventReceiver.h"
#include "nsIDOMNamedNodeMap.h" #include "nsIDOMNamedNodeMap.h"
#include "nsIDOMHTMLElement.h" #include "nsIDOMHTMLElement.h"
#include "nsIPresShell.h" #include "nsIPresShell.h"
#include "nsPIDOMWindow.h"
#include "nsIFocusController.h"
#include "nsIDOMWindowInternal.h"
// CIDs // CIDs
@ -551,6 +554,10 @@ nsDocShellTreeOwner::OnSecurityChange(nsIWebProgress *aWebProgress,
void nsDocShellTreeOwner::WebBrowser(nsWebBrowser* aWebBrowser) void nsDocShellTreeOwner::WebBrowser(nsWebBrowser* aWebBrowser)
{ {
if (aWebBrowser == nsnull)
{
RemoveMouseListener();
}
mWebBrowser = aWebBrowser; mWebBrowser = aWebBrowser;
} }
@ -604,35 +611,44 @@ NS_IMETHODIMP nsDocShellTreeOwner::SetWebBrowserChrome(nsIWebBrowserChrome* aWeb
return NS_OK; return NS_OK;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP nsDocShellTreeOwner::AddMouseListener() NS_IMETHODIMP nsDocShellTreeOwner::AddMouseListener()
{ {
// Clear out the old listener
RemoveMouseListener();
NS_ENSURE_TRUE(mWebBrowser, NS_ERROR_FAILURE); NS_ENSURE_TRUE(mWebBrowser, NS_ERROR_FAILURE);
// Get the DOM document if (mMouseListenerActive)
nsCOMPtr<nsIDOMDocument> domDocument; {
mWebBrowser->GetDocument(getter_AddRefs(domDocument)); return NS_OK;
if (domDocument == nsnull)
{
return NS_ERROR_FAILURE;
} }
nsCOMPtr<nsIDOMWindow> domWindow;
mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
NS_ENSURE_TRUE(domWindow, NS_ERROR_FAILURE);
nsCOMPtr<nsPIDOMWindow> domWindowPrivate = do_QueryInterface(domWindow);
NS_ENSURE_TRUE(domWindowPrivate, NS_ERROR_FAILURE);
nsCOMPtr<nsIDOMWindowInternal> rootWindow;
domWindowPrivate->GetPrivateRoot(getter_AddRefs(rootWindow));
NS_ENSURE_TRUE(rootWindow, NS_ERROR_FAILURE);
nsCOMPtr<nsIChromeEventHandler> chromeHandler;
nsCOMPtr<nsPIDOMWindow> piWin(do_QueryInterface(rootWindow));
piWin->GetChromeEventHandler(getter_AddRefs(chromeHandler));
NS_ENSURE_TRUE(chromeHandler, NS_ERROR_FAILURE);
// Subscribe to mouse events // Subscribe to mouse events
nsCOMPtr<nsIDOMEventReceiver> eventReceiver; mEventReceiver = do_QueryInterface(chromeHandler);
domDocument->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), getter_AddRefs(eventReceiver)); if (mEventReceiver)
if (eventReceiver)
{ {
nsresult rv; nsresult rv;
nsIDOMMouseListener *pListener = NS_STATIC_CAST(nsIDOMMouseListener *, this); nsIDOMMouseListener *pListener = NS_STATIC_CAST(nsIDOMMouseListener *, this);
rv = eventReceiver->AddEventListenerByIID(pListener, NS_GET_IID(nsIDOMMouseListener)); rv = mEventReceiver->AddEventListenerByIID(pListener, NS_GET_IID(nsIDOMMouseListener));
if (NS_SUCCEEDED(rv)) if (NS_SUCCEEDED(rv))
{
mMouseListenerActive = PR_TRUE; mMouseListenerActive = PR_TRUE;
}
} }
mLastDOMDocument = domDocument;
return NS_OK; return NS_OK;
} }
@ -640,30 +656,33 @@ NS_IMETHODIMP nsDocShellTreeOwner::AddMouseListener()
NS_IMETHODIMP nsDocShellTreeOwner::RemoveMouseListener() NS_IMETHODIMP nsDocShellTreeOwner::RemoveMouseListener()
{ {
NS_ENSURE_TRUE(mWebBrowser, NS_ERROR_FAILURE);
// Unsubscribe from mouse events // Unsubscribe from mouse events
if (mLastDOMDocument) if (mEventReceiver)
{ {
// Removing the mouse listener may cause this instance to // Removing the mouse listener may cause this instance to
// be deleted... So, keep an extra reference to defer destruction.. // be deleted... So, keep an extra reference to defer destruction..
nsCOMPtr<nsIDocShellTreeOwner> kungFuDeathGrip(this); nsCOMPtr<nsIDocShellTreeOwner> kungFuDeathGrip(this);
if (mEventReceiver)
nsCOMPtr<nsIDOMEventReceiver> eventReceiver;
mLastDOMDocument->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), getter_AddRefs(eventReceiver));
if (eventReceiver)
{ {
nsresult rv; nsresult rv;
nsIDOMMouseListener *pListener = NS_STATIC_CAST(nsIDOMMouseListener *, this); nsIDOMMouseListener *pListener = NS_STATIC_CAST(nsIDOMMouseListener *, this);
rv = eventReceiver->RemoveEventListenerByIID(pListener, NS_GET_IID(nsIDOMMouseListener)); rv = mEventReceiver->RemoveEventListenerByIID(pListener, NS_GET_IID(nsIDOMMouseListener));
if (NS_SUCCEEDED(rv)) if (NS_SUCCEEDED(rv))
mMouseListenerActive = PR_FALSE; mMouseListenerActive = PR_FALSE;
} }
mLastDOMDocument = nsnull; mEventReceiver = nsnull;
} }
return NS_OK; return NS_OK;
} }
//*****************************************************************************
// nsDocShellTreeOwner::nsIDOMMouseListener
//*****************************************************************************
nsresult nsDocShellTreeOwner::HandleEvent(nsIDOMEvent* aEvent) nsresult nsDocShellTreeOwner::HandleEvent(nsIDOMEvent* aEvent)
{ {
return NS_OK; return NS_OK;

Просмотреть файл

@ -35,6 +35,8 @@
#include "nsIWebProgressListener.h" #include "nsIWebProgressListener.h"
#include "nsIDOMMouseListener.h" #include "nsIDOMMouseListener.h"
#include "nsIDOMDocument.h" #include "nsIDOMDocument.h"
#include "nsIChromeEventHandler.h"
#include "nsIDOMEventReceiver.h"
#include "nsCommandHandler.h" #include "nsCommandHandler.h"
@ -89,11 +91,12 @@ protected:
virtual ~nsDocShellTreeOwner(); virtual ~nsDocShellTreeOwner();
void WebBrowser(nsWebBrowser* aWebBrowser); void WebBrowser(nsWebBrowser* aWebBrowser);
nsWebBrowser* WebBrowser(); nsWebBrowser* WebBrowser();
NS_IMETHOD SetTreeOwner(nsIDocShellTreeOwner* aTreeOwner); NS_IMETHOD SetTreeOwner(nsIDocShellTreeOwner* aTreeOwner);
NS_IMETHOD SetWebBrowserChrome(nsIWebBrowserChrome* aWebBrowserChrome); NS_IMETHOD SetWebBrowserChrome(nsIWebBrowserChrome* aWebBrowserChrome);
nsCOMPtr<nsIDOMDocument> mLastDOMDocument; nsCOMPtr<nsIDOMEventReceiver> mEventReceiver;
NS_IMETHOD AddMouseListener(); NS_IMETHOD AddMouseListener();
NS_IMETHOD RemoveMouseListener(); NS_IMETHOD RemoveMouseListener();