Back to index

lightning-sunbird  0.9+nobinonly
nsXBLEventHandler.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Brendan Eich (brendan@mozilla.org)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCOMPtr.h"
00040 #include "nsIAtom.h"
00041 #include "nsIContent.h"
00042 #include "nsIDOMEventGroup.h"
00043 #include "nsIDOMEventReceiver.h"
00044 #include "nsIDOMKeyEvent.h"
00045 #include "nsIDOMMouseEvent.h"
00046 #include "nsIDOMText.h"
00047 #include "nsIDOM3EventTarget.h"
00048 #include "nsXBLAtoms.h"
00049 #include "nsXBLPrototypeHandler.h"
00050 #include "nsIDOMNSEvent.h"
00051 
00052 nsXBLEventHandler::nsXBLEventHandler(nsXBLPrototypeHandler* aHandler)
00053   : mProtoHandler(aHandler)
00054 {
00055 }
00056 
00057 nsXBLEventHandler::~nsXBLEventHandler()
00058 {
00059 }
00060 
00061 NS_IMPL_ISUPPORTS1(nsXBLEventHandler, nsIDOMEventListener)
00062 
00063 NS_IMETHODIMP
00064 nsXBLEventHandler::HandleEvent(nsIDOMEvent* aEvent)
00065 {
00066   if (!mProtoHandler)
00067     return NS_ERROR_FAILURE;
00068 
00069   PRUint8 phase = mProtoHandler->GetPhase();
00070   if (phase == NS_PHASE_TARGET) {
00071     PRUint16 eventPhase;
00072     aEvent->GetEventPhase(&eventPhase);
00073     if (eventPhase != nsIDOMEvent::AT_TARGET)
00074       return NS_OK;
00075   }
00076 
00077   if (!EventMatched(aEvent))
00078     return NS_OK;
00079 
00080   nsCOMPtr<nsIDOMEventTarget> target;
00081   aEvent->GetCurrentTarget(getter_AddRefs(target));
00082   nsCOMPtr<nsIDOMEventReceiver> receiver = do_QueryInterface(target);
00083 
00084   mProtoHandler->ExecuteHandler(receiver, aEvent);
00085 
00086   return NS_OK;
00087 }
00088 
00089 nsXBLMouseEventHandler::nsXBLMouseEventHandler(nsXBLPrototypeHandler* aHandler)
00090   : nsXBLEventHandler(aHandler)
00091 {
00092 }
00093 
00094 nsXBLMouseEventHandler::~nsXBLMouseEventHandler()
00095 {
00096 }
00097 
00098 PRBool
00099 nsXBLMouseEventHandler::EventMatched(nsIDOMEvent* aEvent)
00100 {
00101   nsCOMPtr<nsIDOMMouseEvent> mouse(do_QueryInterface(aEvent));
00102   return mProtoHandler->MouseEventMatched(mouse);
00103 }
00104 
00105 nsXBLKeyEventHandler::nsXBLKeyEventHandler(nsIAtom* aEventType, PRUint8 aPhase,
00106                                            PRUint8 aType)
00107   : mEventType(aEventType),
00108     mPhase(aPhase),
00109     mType(aType)
00110 {
00111 }
00112 
00113 nsXBLKeyEventHandler::~nsXBLKeyEventHandler()
00114 {
00115 }
00116 
00117 NS_IMPL_ISUPPORTS1(nsXBLKeyEventHandler, nsIDOMEventListener)
00118 
00119 NS_IMETHODIMP
00120 nsXBLKeyEventHandler::HandleEvent(nsIDOMEvent* aEvent)
00121 {
00122   PRUint32 count = mProtoHandlers.Count();
00123   if (count == 0)
00124     return NS_ERROR_FAILURE;
00125 
00126   if (mPhase == NS_PHASE_TARGET) {
00127     PRUint16 eventPhase;
00128     aEvent->GetEventPhase(&eventPhase);
00129     if (eventPhase != nsIDOMEvent::AT_TARGET)
00130       return NS_OK;
00131   }
00132 
00133   nsCOMPtr<nsIDOMEventTarget> target;
00134   aEvent->GetCurrentTarget(getter_AddRefs(target));
00135   nsCOMPtr<nsIDOMEventReceiver> receiver = do_QueryInterface(target);
00136 
00137   nsCOMPtr<nsIDOMKeyEvent> key(do_QueryInterface(aEvent));
00138 
00139   nsCOMPtr<nsIDOMNSEvent> domNSEvent = do_QueryInterface(aEvent);
00140   PRBool trustedEvent = PR_FALSE;
00141   if (domNSEvent) {
00142     domNSEvent->GetIsTrusted(&trustedEvent);
00143   }
00144 
00145   PRUint32 i;
00146   for (i = 0; i < count; ++i) {
00147     nsXBLPrototypeHandler* handler = NS_STATIC_CAST(nsXBLPrototypeHandler*,
00148                                                     mProtoHandlers[i]);
00149     if ((trustedEvent || handler->AllowUntrustedEvents()) &&
00150         handler->KeyEventMatched(key)) {
00151       handler->ExecuteHandler(receiver, aEvent);
00152     }
00153   }
00154 
00155   return NS_OK;
00156 }
00157 
00159 
00160 nsresult
00161 NS_NewXBLEventHandler(nsXBLPrototypeHandler* aHandler,
00162                       nsIAtom* aEventType,
00163                       nsXBLEventHandler** aResult)
00164 {
00165   if (aEventType == nsXBLAtoms::mousedown ||
00166       aEventType == nsXBLAtoms::mouseup ||
00167       aEventType == nsXBLAtoms::click ||
00168       aEventType == nsXBLAtoms::dblclick ||
00169       aEventType == nsXBLAtoms::mouseover ||
00170       aEventType == nsXBLAtoms::mouseout ||
00171       aEventType == nsXBLAtoms::mousemove ||
00172       aEventType == nsXBLAtoms::contextmenu ||
00173       aEventType == nsXBLAtoms::dragenter ||
00174       aEventType == nsXBLAtoms::dragover ||
00175       aEventType == nsXBLAtoms::dragdrop ||
00176       aEventType == nsXBLAtoms::dragexit ||
00177       aEventType == nsXBLAtoms::draggesture) {
00178     *aResult = new nsXBLMouseEventHandler(aHandler);
00179   }
00180   else {
00181     *aResult = new nsXBLEventHandler(aHandler);
00182   }
00183 
00184   if (!*aResult)
00185     return NS_ERROR_OUT_OF_MEMORY;
00186 
00187   NS_ADDREF(*aResult);
00188 
00189   return NS_OK;
00190 }
00191 
00192 nsresult
00193 NS_NewXBLKeyEventHandler(nsIAtom* aEventType, PRUint8 aPhase, PRUint8 aType,
00194                          nsXBLKeyEventHandler** aResult)
00195 {
00196   *aResult = new nsXBLKeyEventHandler(aEventType, aPhase, aType);
00197 
00198   if (!*aResult)
00199     return NS_ERROR_OUT_OF_MEMORY;
00200 
00201   NS_ADDREF(*aResult);
00202 
00203   return NS_OK;
00204 }