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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : * Brendan Eich (brendan@mozilla.org)
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsCOMPtr.h"
40 : #include "nsIAtom.h"
41 : #include "nsIContent.h"
42 : #include "nsIDOMEventListener.h"
43 : #include "nsIDOMEventTarget.h"
44 : #include "nsIDOMKeyEvent.h"
45 : #include "nsIDOMMouseEvent.h"
46 : #include "nsGkAtoms.h"
47 : #include "nsXBLPrototypeHandler.h"
48 : #include "nsIDOMNSEvent.h"
49 : #include "nsGUIEvent.h"
50 : #include "nsContentUtils.h"
51 : #include "nsUnicharUtils.h"
52 :
53 0 : nsXBLEventHandler::nsXBLEventHandler(nsXBLPrototypeHandler* aHandler)
54 0 : : mProtoHandler(aHandler)
55 : {
56 0 : }
57 :
58 0 : nsXBLEventHandler::~nsXBLEventHandler()
59 : {
60 0 : }
61 :
62 0 : NS_IMPL_ISUPPORTS1(nsXBLEventHandler, nsIDOMEventListener)
63 :
64 : NS_IMETHODIMP
65 0 : nsXBLEventHandler::HandleEvent(nsIDOMEvent* aEvent)
66 : {
67 0 : if (!mProtoHandler)
68 0 : return NS_ERROR_FAILURE;
69 :
70 0 : PRUint8 phase = mProtoHandler->GetPhase();
71 0 : if (phase == NS_PHASE_TARGET) {
72 : PRUint16 eventPhase;
73 0 : aEvent->GetEventPhase(&eventPhase);
74 0 : if (eventPhase != nsIDOMEvent::AT_TARGET)
75 0 : return NS_OK;
76 : }
77 :
78 0 : if (!EventMatched(aEvent))
79 0 : return NS_OK;
80 :
81 0 : nsCOMPtr<nsIDOMEventTarget> target;
82 0 : aEvent->GetCurrentTarget(getter_AddRefs(target));
83 :
84 0 : mProtoHandler->ExecuteHandler(target, aEvent);
85 :
86 0 : return NS_OK;
87 : }
88 :
89 0 : nsXBLMouseEventHandler::nsXBLMouseEventHandler(nsXBLPrototypeHandler* aHandler)
90 0 : : nsXBLEventHandler(aHandler)
91 : {
92 0 : }
93 :
94 0 : nsXBLMouseEventHandler::~nsXBLMouseEventHandler()
95 : {
96 0 : }
97 :
98 : bool
99 0 : nsXBLMouseEventHandler::EventMatched(nsIDOMEvent* aEvent)
100 : {
101 0 : nsCOMPtr<nsIDOMMouseEvent> mouse(do_QueryInterface(aEvent));
102 0 : return mouse && mProtoHandler->MouseEventMatched(mouse);
103 : }
104 :
105 0 : nsXBLKeyEventHandler::nsXBLKeyEventHandler(nsIAtom* aEventType, PRUint8 aPhase,
106 : PRUint8 aType)
107 : : mEventType(aEventType),
108 : mPhase(aPhase),
109 : mType(aType),
110 0 : mIsBoundToChrome(false)
111 : {
112 0 : }
113 :
114 0 : nsXBLKeyEventHandler::~nsXBLKeyEventHandler()
115 : {
116 0 : }
117 :
118 0 : NS_IMPL_ISUPPORTS1(nsXBLKeyEventHandler, nsIDOMEventListener)
119 :
120 : bool
121 0 : nsXBLKeyEventHandler::ExecuteMatchedHandlers(nsIDOMKeyEvent* aKeyEvent,
122 : PRUint32 aCharCode,
123 : bool aIgnoreShiftKey)
124 : {
125 0 : nsCOMPtr<nsIDOMNSEvent> domNSEvent = do_QueryInterface(aKeyEvent);
126 0 : bool trustedEvent = false;
127 0 : if (domNSEvent)
128 0 : domNSEvent->GetIsTrusted(&trustedEvent);
129 :
130 0 : nsCOMPtr<nsIDOMEventTarget> target;
131 0 : aKeyEvent->GetCurrentTarget(getter_AddRefs(target));
132 :
133 0 : bool executed = false;
134 0 : for (PRUint32 i = 0; i < mProtoHandlers.Length(); ++i) {
135 0 : nsXBLPrototypeHandler* handler = mProtoHandlers[i];
136 0 : bool hasAllowUntrustedAttr = handler->HasAllowUntrustedAttr();
137 0 : if ((trustedEvent ||
138 0 : (hasAllowUntrustedAttr && handler->AllowUntrustedEvents()) ||
139 0 : (!hasAllowUntrustedAttr && !mIsBoundToChrome)) &&
140 0 : handler->KeyEventMatched(aKeyEvent, aCharCode, aIgnoreShiftKey)) {
141 0 : handler->ExecuteHandler(target, aKeyEvent);
142 0 : executed = true;
143 : }
144 : }
145 0 : return executed;
146 : }
147 :
148 : NS_IMETHODIMP
149 0 : nsXBLKeyEventHandler::HandleEvent(nsIDOMEvent* aEvent)
150 : {
151 0 : PRUint32 count = mProtoHandlers.Length();
152 0 : if (count == 0)
153 0 : return NS_ERROR_FAILURE;
154 :
155 0 : if (mPhase == NS_PHASE_TARGET) {
156 : PRUint16 eventPhase;
157 0 : aEvent->GetEventPhase(&eventPhase);
158 0 : if (eventPhase != nsIDOMEvent::AT_TARGET)
159 0 : return NS_OK;
160 : }
161 :
162 0 : nsCOMPtr<nsIDOMKeyEvent> key(do_QueryInterface(aEvent));
163 0 : if (!key)
164 0 : return NS_OK;
165 :
166 0 : nsAutoTArray<nsShortcutCandidate, 10> accessKeys;
167 0 : nsContentUtils::GetAccelKeyCandidates(key, accessKeys);
168 :
169 0 : if (accessKeys.IsEmpty()) {
170 0 : ExecuteMatchedHandlers(key, 0, false);
171 0 : return NS_OK;
172 : }
173 :
174 0 : for (PRUint32 i = 0; i < accessKeys.Length(); ++i) {
175 0 : if (ExecuteMatchedHandlers(key, accessKeys[i].mCharCode,
176 0 : accessKeys[i].mIgnoreShift))
177 0 : return NS_OK;
178 : }
179 0 : return NS_OK;
180 : }
181 :
182 : ///////////////////////////////////////////////////////////////////////////////////
183 :
184 : nsresult
185 0 : NS_NewXBLEventHandler(nsXBLPrototypeHandler* aHandler,
186 : nsIAtom* aEventType,
187 : nsXBLEventHandler** aResult)
188 : {
189 0 : switch (nsContentUtils::GetEventCategory(nsDependentAtomString(aEventType))) {
190 : case NS_DRAG_EVENT:
191 : case NS_MOUSE_EVENT:
192 : case NS_MOUSE_SCROLL_EVENT:
193 : case NS_SIMPLE_GESTURE_EVENT:
194 0 : *aResult = new nsXBLMouseEventHandler(aHandler);
195 0 : break;
196 : default:
197 0 : *aResult = new nsXBLEventHandler(aHandler);
198 0 : break;
199 : }
200 :
201 0 : if (!*aResult)
202 0 : return NS_ERROR_OUT_OF_MEMORY;
203 :
204 0 : NS_ADDREF(*aResult);
205 :
206 0 : return NS_OK;
207 : }
208 :
209 : nsresult
210 0 : NS_NewXBLKeyEventHandler(nsIAtom* aEventType, PRUint8 aPhase, PRUint8 aType,
211 : nsXBLKeyEventHandler** aResult)
212 : {
213 0 : *aResult = new nsXBLKeyEventHandler(aEventType, aPhase, aType);
214 :
215 0 : if (!*aResult)
216 0 : return NS_ERROR_OUT_OF_MEMORY;
217 :
218 0 : NS_ADDREF(*aResult);
219 :
220 0 : return NS_OK;
221 : }
|