Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / content / xbl / src / nsXBLEventHandler.cpp
blobb85be41cb8a45b5fd339f41ea7ea40382f7feeb5
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
13 * License.
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.
22 * Contributor(s):
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 ***** */
39 #include "nsCOMPtr.h"
40 #include "nsIAtom.h"
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)
67 NS_IMETHODIMP
68 nsXBLEventHandler::HandleEvent(nsIDOMEvent* aEvent)
70 if (!mProtoHandler)
71 return NS_ERROR_FAILURE;
73 PRUint8 phase = mProtoHandler->GetPhase();
74 if (phase == NS_PHASE_TARGET) {
75 PRUint16 eventPhase;
76 aEvent->GetEventPhase(&eventPhase);
77 if (eventPhase != nsIDOMEvent::AT_TARGET)
78 return NS_OK;
81 if (!EventMatched(aEvent))
82 return NS_OK;
84 nsCOMPtr<nsIDOMEventTarget> target;
85 aEvent->GetCurrentTarget(getter_AddRefs(target));
86 nsCOMPtr<nsPIDOMEventTarget> piTarget = do_QueryInterface(target);
88 mProtoHandler->ExecuteHandler(piTarget, aEvent);
90 return NS_OK;
93 nsXBLMouseEventHandler::nsXBLMouseEventHandler(nsXBLPrototypeHandler* aHandler)
94 : nsXBLEventHandler(aHandler)
98 nsXBLMouseEventHandler::~nsXBLMouseEventHandler()
102 PRBool
103 nsXBLMouseEventHandler::EventMatched(nsIDOMEvent* aEvent)
105 nsCOMPtr<nsIDOMMouseEvent> mouse(do_QueryInterface(aEvent));
106 return mProtoHandler->MouseEventMatched(mouse);
109 nsXBLKeyEventHandler::nsXBLKeyEventHandler(nsIAtom* aEventType, PRUint8 aPhase,
110 PRUint8 aType)
111 : mEventType(aEventType),
112 mPhase(aPhase),
113 mType(aType),
114 mIsBoundToChrome(PR_FALSE)
118 nsXBLKeyEventHandler::~nsXBLKeyEventHandler()
122 NS_IMPL_ISUPPORTS1(nsXBLKeyEventHandler, nsIDOMEventListener)
124 PRBool
125 nsXBLKeyEventHandler::ExecuteMatchedHandlers(nsIDOMKeyEvent* aKeyEvent,
126 PRUint32 aCharCode,
127 PRBool aIgnoreShiftKey)
129 nsCOMPtr<nsIDOMNSEvent> domNSEvent = do_QueryInterface(aKeyEvent);
130 PRBool trustedEvent = PR_FALSE;
131 if (domNSEvent)
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*>
141 (mProtoHandlers[i]);
142 PRBool hasAllowUntrustedAttr = handler->HasAllowUntrustedAttr();
143 if ((trustedEvent ||
144 (hasAllowUntrustedAttr && handler->AllowUntrustedEvents()) ||
145 (!hasAllowUntrustedAttr && !mIsBoundToChrome)) &&
146 handler->KeyEventMatched(aKeyEvent, aCharCode, aIgnoreShiftKey)) {
147 handler->ExecuteHandler(piTarget, aKeyEvent);
148 executed = PR_TRUE;
151 return executed;
154 NS_IMETHODIMP
155 nsXBLKeyEventHandler::HandleEvent(nsIDOMEvent* aEvent)
157 PRUint32 count = mProtoHandlers.Count();
158 if (count == 0)
159 return NS_ERROR_FAILURE;
161 if (mPhase == NS_PHASE_TARGET) {
162 PRUint16 eventPhase;
163 aEvent->GetEventPhase(&eventPhase);
164 if (eventPhase != nsIDOMEvent::AT_TARGET)
165 return NS_OK;
168 nsCOMPtr<nsIDOMKeyEvent> key(do_QueryInterface(aEvent));
169 if (!key)
170 return NS_OK;
172 nsAutoTArray<nsShortcutCandidate, 10> accessKeys;
173 nsContentUtils::GetAccelKeyCandidates(key, accessKeys);
175 if (accessKeys.IsEmpty()) {
176 ExecuteMatchedHandlers(key, 0, PR_FALSE);
177 return NS_OK;
180 for (PRUint32 i = 0; i < accessKeys.Length(); ++i) {
181 if (ExecuteMatchedHandlers(key, accessKeys[i].mCharCode,
182 accessKeys[i].mIgnoreShift))
183 return NS_OK;
185 return NS_OK;
188 ///////////////////////////////////////////////////////////////////////////////////
190 nsresult
191 NS_NewXBLEventHandler(nsXBLPrototypeHandler* aHandler,
192 nsIAtom* aEventType,
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);
210 else {
211 *aResult = new nsXBLEventHandler(aHandler);
214 if (!*aResult)
215 return NS_ERROR_OUT_OF_MEMORY;
217 NS_ADDREF(*aResult);
219 return NS_OK;
222 nsresult
223 NS_NewXBLKeyEventHandler(nsIAtom* aEventType, PRUint8 aPhase, PRUint8 aType,
224 nsXBLKeyEventHandler** aResult)
226 *aResult = new nsXBLKeyEventHandler(aEventType, aPhase, aType);
228 if (!*aResult)
229 return NS_ERROR_OUT_OF_MEMORY;
231 NS_ADDREF(*aResult);
233 return NS_OK;