gecko-dev/content/xbl/src/nsXBLEventHandler.cpp

183 lines
5.3 KiB
C++
Raw Normal View History

2000-03-21 13:14:34 +00:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Original Author: David W. Hyatt (hyatt@netscape.com)
*
* Contributor(s): Brendan Eich (brendan@mozilla.org)
2000-03-21 13:14:34 +00:00
*/
#include "nsCOMPtr.h"
2000-09-02 01:09:47 +00:00
#include "nsIXBLPrototypeHandler.h"
#include "nsXBLEventHandler.h"
#include "nsIContent.h"
2000-01-26 10:25:55 +00:00
#include "nsIAtom.h"
#include "nsIDOMKeyEvent.h"
2000-05-21 06:58:15 +00:00
#include "nsIDOMMouseEvent.h"
2000-01-26 10:25:55 +00:00
#include "nsINameSpaceManager.h"
2000-01-26 11:05:44 +00:00
#include "nsIScriptContext.h"
#include "nsIScriptGlobalObject.h"
#include "nsIDocument.h"
#include "nsIDOMDocument.h"
#include "nsIJSEventListener.h"
2000-01-27 07:49:50 +00:00
#include "nsIController.h"
#include "nsIControllers.h"
2000-01-27 07:57:29 +00:00
#include "nsIDOMXULElement.h"
#include "nsIDOMNSHTMLTextAreaElement.h"
#include "nsIDOMNSHTMLInputElement.h"
#include "nsIDOMText.h"
2000-06-22 00:36:19 +00:00
#include "nsIEventListenerManager.h"
#include "nsIDOMEventReceiver.h"
#include "nsIDOMEventListener.h"
2000-06-22 00:36:19 +00:00
#include "nsXBLBinding.h"
#include "nsIPrivateDOMEvent.h"
#include "nsIDOMWindowInternal.h"
#include "nsIPref.h"
#include "nsIServiceManager.h"
#include "nsIURI.h"
#include "nsXPIDLString.h"
#include "nsIDOMFocusListener.h"
#include "nsIDOMKeyListener.h"
2001-08-06 21:49:35 +00:00
#include "nsIDOMXULListener.h"
#include "nsIDOMMouseListener.h"
#include "nsIDOMDragListener.h"
#include "nsIDOMScrollListener.h"
#include "nsIDOMFormListener.h"
2000-01-26 10:25:55 +00:00
PRUint32 nsXBLEventHandler::gRefCnt = 0;
nsIAtom* nsXBLEventHandler::kKeyCodeAtom = nsnull;
nsIAtom* nsXBLEventHandler::kCharCodeAtom = nsnull;
nsIAtom* nsXBLEventHandler::kKeyAtom = nsnull;
2000-09-01 01:38:04 +00:00
nsIAtom* nsXBLEventHandler::kActionAtom = nsnull;
2000-01-27 07:49:50 +00:00
nsIAtom* nsXBLEventHandler::kCommandAtom = nsnull;
2000-05-21 06:58:15 +00:00
nsIAtom* nsXBLEventHandler::kClickCountAtom = nsnull;
nsIAtom* nsXBLEventHandler::kButtonAtom = nsnull;
2000-09-01 08:07:07 +00:00
nsIAtom* nsXBLEventHandler::kModifiersAtom = nsnull;
2000-09-22 05:02:20 +00:00
nsXBLEventHandler::nsXBLEventHandler(nsIDOMEventReceiver* aEventReceiver, nsIXBLPrototypeHandler* aHandler)
{
NS_INIT_REFCNT();
2000-09-01 08:07:07 +00:00
mEventReceiver = aEventReceiver;
2000-09-02 01:09:47 +00:00
mProtoHandler = aHandler;
2000-06-22 00:36:19 +00:00
mNextHandler = nsnull;
2000-01-26 10:25:55 +00:00
gRefCnt++;
if (gRefCnt == 1) {
kKeyCodeAtom = NS_NewAtom("keycode");
kKeyAtom = NS_NewAtom("key");
kCharCodeAtom = NS_NewAtom("charcode");
2000-09-01 08:07:07 +00:00
kModifiersAtom = NS_NewAtom("modifiers");
2000-09-01 01:38:04 +00:00
kActionAtom = NS_NewAtom("action");
2000-01-27 07:49:50 +00:00
kCommandAtom = NS_NewAtom("command");
2000-05-21 06:58:15 +00:00
kClickCountAtom = NS_NewAtom("clickcount");
kButtonAtom = NS_NewAtom("button");
2000-01-26 10:25:55 +00:00
}
}
nsXBLEventHandler::~nsXBLEventHandler()
{
2000-01-26 10:25:55 +00:00
gRefCnt--;
if (gRefCnt == 0) {
NS_RELEASE(kKeyAtom);
NS_RELEASE(kKeyCodeAtom);
NS_RELEASE(kCharCodeAtom);
2000-09-01 08:07:07 +00:00
NS_RELEASE(kModifiersAtom);
2000-09-01 01:38:04 +00:00
NS_RELEASE(kActionAtom);
2000-01-27 07:49:50 +00:00
NS_RELEASE(kCommandAtom);
2000-05-21 06:58:15 +00:00
NS_RELEASE(kButtonAtom);
NS_RELEASE(kClickCountAtom);
2000-01-26 10:25:55 +00:00
}
}
NS_IMPL_ISUPPORTS1(nsXBLEventHandler, nsISupports)
2000-06-22 00:36:19 +00:00
void
nsXBLEventHandler::RemoveEventHandlers()
{
2000-09-01 08:07:07 +00:00
if (mNextHandler)
mNextHandler->RemoveEventHandlers();
2000-07-28 00:35:02 +00:00
2000-09-03 06:00:09 +00:00
// Figure out if we're using capturing or not.
if (!mProtoHandler)
return;
2000-09-22 05:02:20 +00:00
nsCOMPtr<nsIAtom> eventName;
mProtoHandler->GetEventName(getter_AddRefs(eventName));
2000-09-02 01:09:47 +00:00
nsCOMPtr<nsIContent> handlerElement;
mProtoHandler->GetHandlerElement(getter_AddRefs(handlerElement));
2000-09-03 06:00:09 +00:00
mProtoHandler = nsnull;
2000-09-02 01:09:47 +00:00
if (!handlerElement)
return;
2000-09-01 08:07:07 +00:00
PRBool useCapture = PR_FALSE;
nsAutoString capturer;
handlerElement->GetAttr(kNameSpaceID_None, nsXBLBinding::kPhaseAtom, capturer);
2000-09-01 08:07:07 +00:00
if (capturer == NS_LITERAL_STRING("capturing"))
useCapture = PR_TRUE;
nsAutoString type;
handlerElement->GetAttr(kNameSpaceID_None, nsXBLBinding::kEventAtom, type);
2000-09-01 08:07:07 +00:00
PRBool found = PR_FALSE;
nsIID iid;
2000-09-22 05:02:20 +00:00
nsXBLBinding::GetEventHandlerIID(eventName, &iid, &found);
nsCOMPtr<nsIDOMEventListener> listener(do_QueryInterface(this));
if (found && listener)
mEventReceiver->RemoveEventListener(type, listener, useCapture);
2000-06-22 00:36:19 +00:00
}
/// Helpers that are relegated to the end of the file /////////////////////////////
nsresult
nsXBLEventHandler::GetTextData(nsIContent *aParent, nsAWritableString& aResult)
{
aResult.Truncate(0);
nsCOMPtr<nsIContent> textChild;
PRInt32 textCount;
aParent->ChildCount(textCount);
for (PRInt32 j = 0; j < textCount; j++) {
// Get the child.
aParent->ChildAt(j, *getter_AddRefs(textChild));
nsCOMPtr<nsIDOMText> text(do_QueryInterface(textChild));
if (text) {
nsAutoString data;
text->GetData(data);
aResult.Append(data);
}
}
return NS_OK;
}
///////////////////////////////////////////////////////////////////////////////////
nsresult
2000-09-02 01:09:47 +00:00
NS_NewXBLEventHandler(nsIDOMEventReceiver* aRec, nsIXBLPrototypeHandler* aHandler,
nsXBLEventHandler** aResult)
{
2000-09-22 05:02:20 +00:00
*aResult = new nsXBLEventHandler(aRec, aHandler);
if (!*aResult)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(*aResult);
return NS_OK;
}