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 : * Original Author: David W. Hyatt (hyatt@netscape.com)
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 : #ifndef nsXBLPrototypeHandler_h__
40 : #define nsXBLPrototypeHandler_h__
41 :
42 : #include "nsIAtom.h"
43 : #include "nsString.h"
44 : #include "nsCOMPtr.h"
45 : #include "nsIController.h"
46 : #include "nsAutoPtr.h"
47 : #include "nsXBLEventHandler.h"
48 : #include "nsIWeakReference.h"
49 : #include "nsIScriptGlobalObject.h"
50 : #include "nsDOMScriptObjectHolder.h"
51 : #include "nsCycleCollectionParticipant.h"
52 :
53 : class nsIDOMEvent;
54 : class nsIContent;
55 : class nsIDOMUIEvent;
56 : class nsIDOMKeyEvent;
57 : class nsIDOMMouseEvent;
58 : class nsIDOMEventTarget;
59 : class nsXBLPrototypeBinding;
60 :
61 : #define NS_HANDLER_TYPE_XBL_JS (1 << 0)
62 : #define NS_HANDLER_TYPE_XBL_COMMAND (1 << 1)
63 : #define NS_HANDLER_TYPE_XUL (1 << 2)
64 : #define NS_HANDLER_HAS_ALLOW_UNTRUSTED_ATTR (1 << 4)
65 : #define NS_HANDLER_ALLOW_UNTRUSTED (1 << 5)
66 : #define NS_HANDLER_TYPE_SYSTEM (1 << 6)
67 : #define NS_HANDLER_TYPE_PREVENTDEFAULT (1 << 7)
68 :
69 : // XXX Use nsIDOMEvent:: codes?
70 : #define NS_PHASE_CAPTURING 1
71 : #define NS_PHASE_TARGET 2
72 : #define NS_PHASE_BUBBLING 3
73 :
74 : class nsXBLPrototypeHandler
75 : {
76 : public:
77 : // This constructor is used by XBL handlers (both the JS and command shorthand variety)
78 : nsXBLPrototypeHandler(const PRUnichar* aEvent, const PRUnichar* aPhase,
79 : const PRUnichar* aAction, const PRUnichar* aCommand,
80 : const PRUnichar* aKeyCode, const PRUnichar* aCharCode,
81 : const PRUnichar* aModifiers, const PRUnichar* aButton,
82 : const PRUnichar* aClickCount, const PRUnichar* aGroup,
83 : const PRUnichar* aPreventDefault,
84 : const PRUnichar* aAllowUntrusted,
85 : nsXBLPrototypeBinding* aBinding,
86 : PRUint32 aLineNumber);
87 :
88 : // This constructor is used only by XUL key handlers (e.g., <key>)
89 : nsXBLPrototypeHandler(nsIContent* aKeyElement);
90 :
91 : // This constructor is used for handlers loaded from the cache
92 : nsXBLPrototypeHandler(nsXBLPrototypeBinding* aBinding);
93 :
94 : ~nsXBLPrototypeHandler();
95 :
96 : // if aCharCode is not zero, it is used instead of the charCode of aKeyEvent.
97 : bool KeyEventMatched(nsIDOMKeyEvent* aKeyEvent,
98 : PRUint32 aCharCode = 0,
99 : bool aIgnoreShiftKey = false);
100 0 : inline bool KeyEventMatched(nsIAtom* aEventType,
101 : nsIDOMKeyEvent* aEvent,
102 : PRUint32 aCharCode = 0,
103 : bool aIgnoreShiftKey = false)
104 : {
105 0 : if (aEventType != mEventName)
106 0 : return false;
107 :
108 0 : return KeyEventMatched(aEvent, aCharCode, aIgnoreShiftKey);
109 : }
110 :
111 : bool MouseEventMatched(nsIDOMMouseEvent* aMouseEvent);
112 : inline bool MouseEventMatched(nsIAtom* aEventType,
113 : nsIDOMMouseEvent* aEvent)
114 : {
115 : if (aEventType != mEventName)
116 : return false;
117 :
118 : return MouseEventMatched(aEvent);
119 : }
120 :
121 : already_AddRefed<nsIContent> GetHandlerElement();
122 :
123 : void AppendHandlerText(const nsAString& aText);
124 :
125 0 : PRUint8 GetPhase() { return mPhase; }
126 0 : PRUint8 GetType() { return mType; }
127 :
128 0 : nsXBLPrototypeHandler* GetNextHandler() { return mNextHandler; }
129 0 : void SetNextHandler(nsXBLPrototypeHandler* aHandler) { mNextHandler = aHandler; }
130 :
131 : nsresult ExecuteHandler(nsIDOMEventTarget* aTarget, nsIDOMEvent* aEvent);
132 :
133 : already_AddRefed<nsIAtom> GetEventName();
134 : void SetEventName(nsIAtom* aName) { mEventName = aName; }
135 :
136 0 : nsXBLEventHandler* GetEventHandler()
137 : {
138 0 : if (!mHandler) {
139 0 : NS_NewXBLEventHandler(this, mEventName, getter_AddRefs(mHandler));
140 : // XXX Need to signal out of memory?
141 : }
142 :
143 0 : return mHandler;
144 : }
145 :
146 0 : nsXBLEventHandler* GetCachedEventHandler()
147 : {
148 0 : return mHandler;
149 : }
150 :
151 0 : bool HasAllowUntrustedAttr()
152 : {
153 0 : return (mType & NS_HANDLER_HAS_ALLOW_UNTRUSTED_ATTR) != 0;
154 : }
155 :
156 : // This returns a valid value only if HasAllowUntrustedEventsAttr returns
157 : // true.
158 0 : bool AllowUntrustedEvents()
159 : {
160 0 : return (mType & NS_HANDLER_ALLOW_UNTRUSTED) != 0;
161 : }
162 :
163 : nsresult Read(nsIScriptContext* aContext, nsIObjectInputStream* aStream);
164 : nsresult Write(nsIScriptContext* aContext, nsIObjectOutputStream* aStream);
165 :
166 : public:
167 : static PRUint32 gRefCnt;
168 :
169 : protected:
170 0 : void Init() {
171 0 : ++gRefCnt;
172 0 : if (gRefCnt == 1)
173 : // Get the primary accelerator key.
174 0 : InitAccessKeys();
175 0 : }
176 :
177 : already_AddRefed<nsIController> GetController(nsIDOMEventTarget* aTarget);
178 :
179 : inline PRInt32 GetMatchingKeyCode(const nsAString& aKeyName);
180 : void ConstructPrototype(nsIContent* aKeyElement,
181 : const PRUnichar* aEvent=nsnull, const PRUnichar* aPhase=nsnull,
182 : const PRUnichar* aAction=nsnull, const PRUnichar* aCommand=nsnull,
183 : const PRUnichar* aKeyCode=nsnull, const PRUnichar* aCharCode=nsnull,
184 : const PRUnichar* aModifiers=nsnull, const PRUnichar* aButton=nsnull,
185 : const PRUnichar* aClickCount=nsnull, const PRUnichar* aGroup=nsnull,
186 : const PRUnichar* aPreventDefault=nsnull,
187 : const PRUnichar* aAllowUntrusted=nsnull);
188 :
189 : void ReportKeyConflict(const PRUnichar* aKey, const PRUnichar* aModifiers, nsIContent* aElement, const char *aMessageName);
190 : void GetEventType(nsAString& type);
191 : bool ModifiersMatchMask(nsIDOMUIEvent* aEvent,
192 : bool aIgnoreShiftKey = false);
193 : nsresult DispatchXBLCommand(nsIDOMEventTarget* aTarget, nsIDOMEvent* aEvent);
194 : nsresult DispatchXULKeyCommand(nsIDOMEvent* aEvent);
195 : nsresult EnsureEventHandler(nsIScriptGlobalObject* aGlobal,
196 : nsIScriptContext *aBoundContext, nsIAtom *aName,
197 : nsScriptObjectHolder<JSObject>& aHandler);
198 : static PRInt32 KeyToMask(PRInt32 key);
199 :
200 : static PRInt32 kAccelKey;
201 : static PRInt32 kMenuAccessKey;
202 : static void InitAccessKeys();
203 :
204 : static const PRInt32 cShift;
205 : static const PRInt32 cAlt;
206 : static const PRInt32 cControl;
207 : static const PRInt32 cMeta;
208 :
209 : static const PRInt32 cShiftMask;
210 : static const PRInt32 cAltMask;
211 : static const PRInt32 cControlMask;
212 : static const PRInt32 cMetaMask;
213 :
214 : static const PRInt32 cAllModifiers;
215 :
216 : protected:
217 : union {
218 : nsIWeakReference* mHandlerElement; // For XUL <key> element handlers. [STRONG]
219 : PRUnichar* mHandlerText; // For XBL handlers (we don't build an
220 : // element for the <handler>, and instead
221 : // we cache the JS text or command name
222 : // that we should use.
223 : };
224 :
225 : PRUint32 mLineNumber; // The line number we started at in the XBL file
226 :
227 : // The following four values make up 32 bits.
228 : PRUint8 mPhase; // The phase (capturing, bubbling)
229 : PRUint8 mKeyMask; // Which modifier keys this event handler expects to have down
230 : // in order to be matched.
231 : PRUint8 mType; // The type of the handler. The handler is either a XUL key
232 : // handler, an XBL "command" event, or a normal XBL event with
233 : // accompanying JavaScript. The high bit is used to indicate
234 : // whether this handler should prevent the default action.
235 : PRUint8 mMisc; // Miscellaneous extra information. For key events,
236 : // stores whether or not we're a key code or char code.
237 : // For mouse events, stores the clickCount.
238 :
239 : // The primary filter information for mouse/key events.
240 : PRInt32 mDetail; // For key events, contains a charcode or keycode. For
241 : // mouse events, stores the button info.
242 :
243 : // Prototype handlers are chained. We own the next handler in the chain.
244 : nsXBLPrototypeHandler* mNextHandler;
245 : nsCOMPtr<nsIAtom> mEventName; // The type of the event, e.g., "keypress"
246 : nsRefPtr<nsXBLEventHandler> mHandler;
247 : nsXBLPrototypeBinding* mPrototypeBinding; // the binding owns us
248 : };
249 :
250 : #endif
|