1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Brendan Eich (brendan@mozilla.org)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
41 #include "nsIContent.h"
42 #include "nsIDOMEventGroup.h"
43 #include "nsIDOMEventListener.h"
44 #include "nsIDOMEventTarget.h"
45 #include "nsIDOMKeyEvent.h"
46 #include "nsIDOMMouseEvent.h"
47 #include "nsIDOMText.h"
48 #include "nsIDOM3EventTarget.h"
49 #include "nsGkAtoms.h"
50 #include "nsXBLPrototypeHandler.h"
51 #include "nsIDOMNSEvent.h"
52 #include "nsGUIEvent.h"
53 #include "nsContentUtils.h"
54 #include "nsUnicharUtils.h"
56 nsXBLEventHandler::nsXBLEventHandler(nsXBLPrototypeHandler
* aHandler
)
57 : mProtoHandler(aHandler
)
61 nsXBLEventHandler::~nsXBLEventHandler()
65 NS_IMPL_ISUPPORTS1(nsXBLEventHandler
, nsIDOMEventListener
)
68 nsXBLEventHandler::HandleEvent(nsIDOMEvent
* aEvent
)
71 return NS_ERROR_FAILURE
;
73 PRUint8 phase
= mProtoHandler
->GetPhase();
74 if (phase
== NS_PHASE_TARGET
) {
76 aEvent
->GetEventPhase(&eventPhase
);
77 if (eventPhase
!= nsIDOMEvent::AT_TARGET
)
81 if (!EventMatched(aEvent
))
84 nsCOMPtr
<nsIDOMEventTarget
> target
;
85 aEvent
->GetCurrentTarget(getter_AddRefs(target
));
86 nsCOMPtr
<nsPIDOMEventTarget
> piTarget
= do_QueryInterface(target
);
88 mProtoHandler
->ExecuteHandler(piTarget
, aEvent
);
93 nsXBLMouseEventHandler::nsXBLMouseEventHandler(nsXBLPrototypeHandler
* aHandler
)
94 : nsXBLEventHandler(aHandler
)
98 nsXBLMouseEventHandler::~nsXBLMouseEventHandler()
103 nsXBLMouseEventHandler::EventMatched(nsIDOMEvent
* aEvent
)
105 nsCOMPtr
<nsIDOMMouseEvent
> mouse(do_QueryInterface(aEvent
));
106 return mProtoHandler
->MouseEventMatched(mouse
);
109 nsXBLKeyEventHandler::nsXBLKeyEventHandler(nsIAtom
* aEventType
, PRUint8 aPhase
,
111 : mEventType(aEventType
),
114 mIsBoundToChrome(PR_FALSE
)
118 nsXBLKeyEventHandler::~nsXBLKeyEventHandler()
122 NS_IMPL_ISUPPORTS1(nsXBLKeyEventHandler
, nsIDOMEventListener
)
125 nsXBLKeyEventHandler::ExecuteMatchedHandlers(nsIDOMKeyEvent
* aKeyEvent
,
127 PRBool aIgnoreShiftKey
)
129 nsCOMPtr
<nsIDOMNSEvent
> domNSEvent
= do_QueryInterface(aKeyEvent
);
130 PRBool trustedEvent
= PR_FALSE
;
132 domNSEvent
->GetIsTrusted(&trustedEvent
);
134 nsCOMPtr
<nsIDOMEventTarget
> target
;
135 aKeyEvent
->GetCurrentTarget(getter_AddRefs(target
));
136 nsCOMPtr
<nsPIDOMEventTarget
> piTarget
= do_QueryInterface(target
);
138 PRBool executed
= PR_FALSE
;
139 for (PRUint32 i
= 0; i
< mProtoHandlers
.Count(); ++i
) {
140 nsXBLPrototypeHandler
* handler
= static_cast<nsXBLPrototypeHandler
*>
142 PRBool hasAllowUntrustedAttr
= handler
->HasAllowUntrustedAttr();
144 (hasAllowUntrustedAttr
&& handler
->AllowUntrustedEvents()) ||
145 (!hasAllowUntrustedAttr
&& !mIsBoundToChrome
)) &&
146 handler
->KeyEventMatched(aKeyEvent
, aCharCode
, aIgnoreShiftKey
)) {
147 handler
->ExecuteHandler(piTarget
, aKeyEvent
);
155 nsXBLKeyEventHandler::HandleEvent(nsIDOMEvent
* aEvent
)
157 PRUint32 count
= mProtoHandlers
.Count();
159 return NS_ERROR_FAILURE
;
161 if (mPhase
== NS_PHASE_TARGET
) {
163 aEvent
->GetEventPhase(&eventPhase
);
164 if (eventPhase
!= nsIDOMEvent::AT_TARGET
)
168 nsCOMPtr
<nsIDOMKeyEvent
> key(do_QueryInterface(aEvent
));
172 nsAutoTArray
<nsShortcutCandidate
, 10> accessKeys
;
173 nsContentUtils::GetAccelKeyCandidates(key
, accessKeys
);
175 if (accessKeys
.IsEmpty()) {
176 ExecuteMatchedHandlers(key
, 0, PR_FALSE
);
180 for (PRUint32 i
= 0; i
< accessKeys
.Length(); ++i
) {
181 if (ExecuteMatchedHandlers(key
, accessKeys
[i
].mCharCode
,
182 accessKeys
[i
].mIgnoreShift
))
188 ///////////////////////////////////////////////////////////////////////////////////
191 NS_NewXBLEventHandler(nsXBLPrototypeHandler
* aHandler
,
193 nsXBLEventHandler
** aResult
)
195 if (aEventType
== nsGkAtoms::mousedown
||
196 aEventType
== nsGkAtoms::mouseup
||
197 aEventType
== nsGkAtoms::click
||
198 aEventType
== nsGkAtoms::dblclick
||
199 aEventType
== nsGkAtoms::mouseover
||
200 aEventType
== nsGkAtoms::mouseout
||
201 aEventType
== nsGkAtoms::mousemove
||
202 aEventType
== nsGkAtoms::contextmenu
||
203 aEventType
== nsGkAtoms::dragenter
||
204 aEventType
== nsGkAtoms::dragover
||
205 aEventType
== nsGkAtoms::dragdrop
||
206 aEventType
== nsGkAtoms::dragexit
||
207 aEventType
== nsGkAtoms::draggesture
) {
208 *aResult
= new nsXBLMouseEventHandler(aHandler
);
211 *aResult
= new nsXBLEventHandler(aHandler
);
215 return NS_ERROR_OUT_OF_MEMORY
;
223 NS_NewXBLKeyEventHandler(nsIAtom
* aEventType
, PRUint8 aPhase
, PRUint8 aType
,
224 nsXBLKeyEventHandler
** aResult
)
226 *aResult
= new nsXBLKeyEventHandler(aEventType
, aPhase
, aType
);
229 return NS_ERROR_OUT_OF_MEMORY
;