Back to index

lightning-sunbird  0.9+nobinonly
nsXBLPrototypeHandler.h
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  *   Original Author: David W. Hyatt (hyatt@netscape.com)
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 #ifndef nsXBLPrototypeHandler_h__
00040 #define nsXBLPrototypeHandler_h__
00041 
00042 #include "nsIAtom.h"
00043 #include "nsString.h"
00044 #include "nsCOMPtr.h"
00045 #include "nsIController.h"
00046 #include "nsAutoPtr.h"
00047 #include "nsXBLEventHandler.h"
00048 #include "nsIWeakReference.h"
00049 
00050 class nsIDOMEvent;
00051 class nsIContent;
00052 class nsIBoxObject;
00053 class nsIDOMUIEvent;
00054 class nsIDOMKeyEvent;
00055 class nsIDOMMouseEvent;
00056 class nsIDOMEventReceiver;
00057 class nsIDOM3EventTarget;
00058 class nsXBLPrototypeBinding;
00059 
00060 #define NS_HANDLER_TYPE_XBL_JS          (1 << 0)
00061 #define NS_HANDLER_TYPE_XBL_COMMAND     (1 << 1)
00062 #define NS_HANDLER_TYPE_XUL             (1 << 2)
00063 #define NS_HANDLER_ALLOW_UNTRUSTED      (1 << 5)
00064 #define NS_HANDLER_TYPE_SYSTEM          (1 << 6)
00065 #define NS_HANDLER_TYPE_PREVENTDEFAULT  (1 << 7)
00066 
00067 // XXX Use nsIDOMEvent:: codes?
00068 #define NS_PHASE_CAPTURING          1
00069 #define NS_PHASE_TARGET             2
00070 #define NS_PHASE_BUBBLING           3
00071 
00072 class nsXBLPrototypeHandler
00073 {
00074 public:
00075   // This constructor is used by XBL handlers (both the JS and command shorthand variety)
00076   nsXBLPrototypeHandler(const PRUnichar* aEvent, const PRUnichar* aPhase,
00077                         const PRUnichar* aAction, const PRUnichar* aCommand,
00078                         const PRUnichar* aKeyCode, const PRUnichar* aCharCode,
00079                         const PRUnichar* aModifiers, const PRUnichar* aButton,
00080                         const PRUnichar* aClickCount, const PRUnichar* aGroup,
00081                         const PRUnichar* aPreventDefault,
00082                         const PRUnichar* aAllowUntrusted,
00083                         nsXBLPrototypeBinding* aBinding);
00084 
00085   // This constructor is used only by XUL key handlers (e.g., <key>)
00086   nsXBLPrototypeHandler(nsIContent* aKeyElement);
00087 
00088   ~nsXBLPrototypeHandler();
00089 
00090   PRBool KeyEventMatched(nsIDOMKeyEvent* aKeyEvent);
00091   inline PRBool KeyEventMatched(nsIAtom* aEventType,
00092                                 nsIDOMKeyEvent* aEvent)
00093   {
00094     if (aEventType != mEventName)
00095       return PR_FALSE;
00096 
00097     return KeyEventMatched(aEvent);
00098   }
00099 
00100   PRBool MouseEventMatched(nsIDOMMouseEvent* aMouseEvent);
00101   inline PRBool MouseEventMatched(nsIAtom* aEventType,
00102                                   nsIDOMMouseEvent* aEvent)
00103   {
00104     if (aEventType != mEventName)
00105       return PR_FALSE;
00106 
00107     return MouseEventMatched(aEvent);
00108   }
00109 
00110   already_AddRefed<nsIContent> GetHandlerElement();
00111 
00112   void AppendHandlerText(const nsAString& aText);
00113 
00114   void SetLineNumber(PRUint32 aLineNumber) {
00115     mLineNumber = aLineNumber;
00116   }
00117 
00118   PRUint8 GetPhase() { return mPhase; }
00119   PRUint8 GetType() { return mType; }
00120 
00121   nsXBLPrototypeHandler* GetNextHandler() { return mNextHandler; }
00122   void SetNextHandler(nsXBLPrototypeHandler* aHandler) { mNextHandler = aHandler; }
00123 
00124   nsresult ExecuteHandler(nsIDOMEventReceiver* aReceiver, nsIDOMEvent* aEvent);
00125 
00126   already_AddRefed<nsIAtom> GetEventName();
00127   void SetEventName(nsIAtom* aName) { mEventName = aName; }
00128 
00129   nsXBLEventHandler* GetEventHandler()
00130   {
00131     if (!mHandler) {
00132       NS_NewXBLEventHandler(this, mEventName, getter_AddRefs(mHandler));
00133       // XXX Need to signal out of memory?
00134     }
00135 
00136     return mHandler;
00137   }
00138 
00139   nsXBLEventHandler* GetCachedEventHandler()
00140   {
00141     return mHandler;
00142   }
00143 
00144   PRBool AllowUntrustedEvents()
00145   {
00146     return (mType & NS_HANDLER_ALLOW_UNTRUSTED) != 0;
00147   }
00148 
00149 public:
00150   static PRUint32 gRefCnt;
00151   
00152 protected:
00153   already_AddRefed<nsIController> GetController(nsIDOMEventReceiver* aReceiver);
00154   
00155   inline PRInt32 GetMatchingKeyCode(const nsAString& aKeyName);
00156   void ConstructPrototype(nsIContent* aKeyElement, 
00157                           const PRUnichar* aEvent=nsnull, const PRUnichar* aPhase=nsnull,
00158                           const PRUnichar* aAction=nsnull, const PRUnichar* aCommand=nsnull,
00159                           const PRUnichar* aKeyCode=nsnull, const PRUnichar* aCharCode=nsnull,
00160                           const PRUnichar* aModifiers=nsnull, const PRUnichar* aButton=nsnull,
00161                           const PRUnichar* aClickCount=nsnull, const PRUnichar* aGroup=nsnull,
00162                           const PRUnichar* aPreventDefault=nsnull,
00163                           const PRUnichar* aAllowUntrusted=nsnull);
00164 
00165   void GetEventType(nsAString& type);
00166   PRBool ModifiersMatchMask(nsIDOMUIEvent* aEvent);
00167 
00168   static PRInt32 KeyToMask(PRInt32 key);
00169   
00170   static PRInt32 kAccelKey;
00171   static PRInt32 kMenuAccessKey;
00172   static void InitAccessKeys();
00173 
00174   static const PRInt32 cShift;
00175   static const PRInt32 cAlt;
00176   static const PRInt32 cControl;
00177   static const PRInt32 cMeta;
00178 
00179   static const PRInt32 cShiftMask;
00180   static const PRInt32 cAltMask;
00181   static const PRInt32 cControlMask;
00182   static const PRInt32 cMetaMask;
00183 
00184   static const PRInt32 cAllModifiers;
00185 
00186 protected:
00187   union {
00188     nsIBoxObject* mBoxObjectForHandlerElement;  // For XUL <key> element handlers. [STRONG]
00189     PRUnichar*    mHandlerText;                 // For XBL handlers (we don't build an
00190                                                 // element for the <handler>, and instead
00191                                                 // we cache the JS text or command name
00192                                                 // that we should use.
00193   };
00194 
00195   PRUint32 mLineNumber;  // The line number we started at in the XBL file
00196   
00197   // The following four values make up 32 bits.
00198   PRUint8 mPhase;            // The phase (capturing, bubbling)
00199   PRUint8 mKeyMask;          // Which modifier keys this event handler expects to have down
00200                              // in order to be matched.
00201   PRUint8 mType;             // The type of the handler.  The handler is either a XUL key
00202                              // handler, an XBL "command" event, or a normal XBL event with
00203                              // accompanying JavaScript.  The high bit is used to indicate
00204                              // whether this handler should prevent the default action.
00205   PRUint8 mMisc;             // Miscellaneous extra information.  For key events,
00206                              // stores whether or not we're a key code or char code.
00207                              // For mouse events, stores the clickCount.
00208 
00209   // The primary filter information for mouse/key events.
00210   PRInt32 mDetail;           // For key events, contains a charcode or keycode. For
00211                              // mouse events, stores the button info.
00212   
00213   
00214 
00215   // Prototype handlers are chained. We own the next handler in the chain.
00216   nsXBLPrototypeHandler* mNextHandler;
00217   nsCOMPtr<nsIAtom> mEventName; // The type of the event, e.g., "keypress"
00218   nsRefPtr<nsXBLEventHandler> mHandler;
00219   nsXBLPrototypeBinding* mPrototypeBinding; // the binding owns us
00220 };
00221 
00222 #endif