Back to index

lightning-sunbird  0.9+nobinonly
nsEventStateManager.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.org 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  *   Mats Palmgren <mats.palmgren@bredband.net>
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 nsEventStateManager_h__
00040 #define nsEventStateManager_h__
00041 
00042 #include "nsIEventStateManager.h"
00043 #include "nsGUIEvent.h"
00044 #include "nsIContent.h"
00045 #include "nsIObserver.h"
00046 #include "nsWeakReference.h"
00047 #include "nsHashtable.h"
00048 #include "nsITimer.h"
00049 #include "nsCOMPtr.h"
00050 #include "nsIDocument.h"
00051 #include "nsCOMArray.h"
00052 
00053 class nsIScrollableView;
00054 class nsIPresShell;
00055 class nsIFrameSelection;
00056 class nsIDocShell;
00057 class nsIDocShellTreeNode;
00058 class nsIDocShellTreeItem;
00059 class nsIFocusController;
00060 class imgIContainer;
00061 
00062 // mac uses click-hold context menus, a holdover from 4.x
00063 #if defined(XP_MAC) || defined(XP_MACOSX)
00064 #define CLICK_HOLD_CONTEXT_MENUS 1
00065 #endif
00066 
00067 
00068 /*
00069  * Event listener manager
00070  */
00071 
00072 class nsEventStateManager : public nsSupportsWeakReference,
00073                             public nsIEventStateManager,
00074                             public nsIObserver
00075 {
00076 public:
00077   nsEventStateManager();
00078   virtual ~nsEventStateManager();
00079 
00080   NS_DECL_ISUPPORTS
00081   NS_DECL_NSIOBSERVER
00082 
00083   NS_IMETHOD Init();
00084   nsresult Shutdown();
00085 
00086   /* The PreHandleEvent method is called before event dispatch to either
00087    * the DOM or frames.  Any processing which must not be prevented or
00088    * cancelled should occur here.  Any processing which is intended to
00089    * be conditional based on either DOM or frame processing should occur in
00090    * PostHandleEvent.  Any centralized event processing which must occur before
00091    * DOM or frame event handling should occur here as well.
00092    */
00093   NS_IMETHOD PreHandleEvent(nsPresContext* aPresContext,
00094                          nsEvent *aEvent,
00095                          nsIFrame* aTargetFrame,
00096                          nsEventStatus* aStatus,
00097                          nsIView* aView);
00098 
00099   /* The PostHandleEvent method should contain all system processing which
00100    * should occur conditionally based on DOM or frame processing.  It should
00101    * also contain any centralized event processing which must occur after
00102    * DOM and frame processing.
00103    */
00104   NS_IMETHOD PostHandleEvent(nsPresContext* aPresContext,
00105                              nsEvent *aEvent,
00106                              nsIFrame* aTargetFrame,
00107                              nsEventStatus* aStatus,
00108                              nsIView* aView);
00109 
00110   NS_IMETHOD SetPresContext(nsPresContext* aPresContext);
00111   NS_IMETHOD ClearFrameRefs(nsIFrame* aFrame);
00112 
00113   NS_IMETHOD GetEventTarget(nsIFrame **aFrame);
00114   NS_IMETHOD GetEventTargetContent(nsEvent* aEvent, nsIContent** aContent);
00115   NS_IMETHOD GetEventRelatedContent(nsIContent** aContent);
00116 
00117   NS_IMETHOD GetContentState(nsIContent *aContent, PRInt32& aState);
00118   NS_IMETHOD SetContentState(nsIContent *aContent, PRInt32 aState);
00119   NS_IMETHOD GetFocusedContent(nsIContent **aContent);
00120   NS_IMETHOD SetFocusedContent(nsIContent* aContent);
00121   NS_IMETHOD GetLastFocusedContent(nsIContent **aContent);
00122   NS_IMETHOD GetFocusedFrame(nsIFrame **aFrame);
00123   NS_IMETHOD ContentRemoved(nsIContent* aContent);
00124   NS_IMETHOD EventStatusOK(nsGUIEvent* aEvent, PRBool *aOK);
00125 
00126   // This is an experiement and may be temporary
00127   NS_IMETHOD ConsumeFocusEvents(PRBool aDoConsume) { mConsumeFocusEvents = aDoConsume; return NS_OK; }
00128 
00129   // Access Key Registration
00130   NS_IMETHOD RegisterAccessKey(nsIContent* aContent, PRUint32 aKey);
00131   NS_IMETHOD UnregisterAccessKey(nsIContent* aContent, PRUint32 aKey);
00132 
00133   NS_IMETHOD SetCursor(PRInt32 aCursor, imgIContainer* aContainer,
00134                        PRBool aHaveHotspot, float aHotspotX, float aHotspotY,
00135                        nsIWidget* aWidget, PRBool aLockCursor);
00136 
00137   //Method for centralized distribution of new DOM events
00138   NS_IMETHOD DispatchNewEvent(nsISupports* aTarget, nsIDOMEvent* aEvent, PRBool *aDefaultActionEnabled);
00139 
00140   NS_IMETHOD ShiftFocus(PRBool aForward, nsIContent* aStart=nsnull);
00141 
00142   virtual PRBool GetBrowseWithCaret();
00143   void ResetBrowseWithCaret();
00144 
00145   NS_IMETHOD MoveFocusToCaret(PRBool aCanFocusDoc, PRBool *aIsSelectionWithFocus);
00146   NS_IMETHOD MoveCaretToFocus();
00147   NS_IMETHOD ChangeFocusWith(nsIContent* aFocus, EFocusedWithType aFocusedWith);
00148 
00149   static void StartHandlingUserInput()
00150   {
00151     ++sUserInputEventDepth;
00152   }
00153 
00154   static void StopHandlingUserInput()
00155   {
00156     --sUserInputEventDepth;
00157   }
00158 
00159   static PRBool IsHandlingUserInput()
00160   {
00161     return sUserInputEventDepth > 0;
00162   }
00163 
00164 protected:
00171   void EnsureFocusSynchronization();
00172 
00173   void UpdateCursor(nsPresContext* aPresContext, nsEvent* aEvent, nsIFrame* aTargetFrame, nsEventStatus* aStatus);
00179   nsIFrame* DispatchMouseEvent(nsGUIEvent* aEvent, PRUint32 aMessage,
00180                                nsIContent* aTargetContent,
00181                                nsIContent* aRelatedContent);
00186   void GenerateMouseEnterExit(nsGUIEvent* aEvent);
00191   void NotifyMouseOver(nsGUIEvent* aEvent, nsIContent* aContent);
00201   void NotifyMouseOut(nsGUIEvent* aEvent, nsIContent* aMovingInto);
00202   void GenerateDragDropEnterExit(nsPresContext* aPresContext, nsGUIEvent* aEvent);
00203   nsresult SetClickCount(nsPresContext* aPresContext, nsMouseEvent *aEvent, nsEventStatus* aStatus);
00204   nsresult CheckForAndDispatchClick(nsPresContext* aPresContext, nsMouseEvent *aEvent, nsEventStatus* aStatus);
00205   nsresult GetNextTabbableContent(nsIContent* aRootContent,
00206                                   nsIContent* aStartContent,
00207                                   nsIFrame* aStartFrame,
00208                                   PRBool forward, PRBool ignoreTabIndex,
00209                                   nsIContent** aResultNode,
00210                                   nsIFrame** aResultFrame);
00211   nsIContent *GetNextTabbableMapArea(PRBool aForward, nsIContent *imageContent);
00212 
00213   PRInt32 GetNextTabIndex(nsIContent* aParent, PRBool foward);
00214   nsresult SendFocusBlur(nsPresContext* aPresContext, nsIContent *aContent, PRBool aEnsureWindowHasFocus);
00215   void EnsureDocument(nsIPresShell* aPresShell);
00216   void EnsureDocument(nsPresContext* aPresContext);
00217   void FlushPendingEvents(nsPresContext* aPresContext);
00218   nsIFocusController* GetFocusControllerForDocument(nsIDocument* aDocument);
00219 
00220   typedef enum {
00221     eAccessKeyProcessingNormal = 0,
00222     eAccessKeyProcessingUp,
00223     eAccessKeyProcessingDown
00224   } ProcessingAccessKeyState;
00225   void HandleAccessKey(nsPresContext* aPresContext,
00226                        nsKeyEvent* aEvent,
00227                        nsEventStatus* aStatus,
00228                        PRInt32 aChildOffset,
00229                        ProcessingAccessKeyState aAccessKeyState,
00230                        PRInt32 aModifierMask);
00231 
00232   //---------------------------------------------
00233   // DocShell Focus Traversal Methods
00234   //---------------------------------------------
00235 
00236   nsresult ShiftFocusInternal(PRBool aForward, nsIContent* aStart = nsnull);
00237   void TabIntoDocument(nsIDocShell* aDocShell, PRBool aForward);
00238   void ShiftFocusByDoc(PRBool forward);
00239   PRBool IsFrameSetDoc(nsIDocShell* aDocShell);
00240   PRBool IsIFrameDoc(nsIDocShell* aDocShell);
00241   PRBool IsShellVisible(nsIDocShell* aShell);
00242   void GetLastChildDocShell(nsIDocShellTreeItem* aItem,
00243                             nsIDocShellTreeItem** aResult);
00244   void GetNextDocShell(nsIDocShellTreeNode* aNode,
00245                        nsIDocShellTreeItem** aResult);
00246   void GetPrevDocShell(nsIDocShellTreeNode* aNode,
00247                        nsIDocShellTreeItem** aResult);
00248 
00249   // These functions are for mousewheel scrolling
00250   nsresult GetParentScrollingView(nsInputEvent* aEvent,
00251                                   nsPresContext* aPresContext,
00252                                   nsIFrame* &targetOuterFrame,
00253                                   nsPresContext* &presCtxOuter);
00254 
00255   typedef enum {
00256     eScrollByPixel,
00257     eScrollByLine,
00258     eScrollByPage
00259   } ScrollQuantity;
00260   nsresult DoScrollText(nsPresContext* aPresContext,
00261                         nsIFrame* aTargetFrame,
00262                         nsInputEvent* aEvent,
00263                         PRInt32 aNumLines,
00264                         PRBool aScrollHorizontal,
00265                         ScrollQuantity aScrollQuantity);
00266   void ForceViewUpdate(nsIView* aView);
00267   void DoScrollHistory(PRInt32 direction);
00268   void DoScrollTextsize(nsIFrame *aTargetFrame, PRInt32 adjustment);
00269   nsresult ChangeTextSize(PRInt32 change);
00270   // end mousewheel functions
00271 
00272   // routines for the d&d gesture tracking state machine
00273   void BeginTrackingDragGesture ( nsPresContext* aPresContext, nsMouseEvent* inDownEvent,
00274                                   nsIFrame* inDownFrame ) ;
00275   void StopTrackingDragGesture ( ) ;
00276   void GenerateDragGesture ( nsPresContext* aPresContext, nsMouseEvent *aEvent ) ;
00277   PRBool IsTrackingDragGesture ( ) const { return mGestureDownContent != nsnull; }
00284   void FillInEventFromGestureDown(nsMouseEvent* aEvent);
00285 
00286   PRBool mSuppressFocusChange; // Used only for Ender text fields to suppress a focus firing on mouse down
00287 
00288   nsresult SetCaretEnabled(nsIPresShell *aPresShell, PRBool aVisibility);
00289   nsresult SetContentCaretVisible(nsIPresShell* aPresShell, nsIContent *aContent, PRBool aVisible);
00290   void FocusElementButNotDocument(nsIContent *aElement);
00291 
00292   // Return the location of the caret
00293   nsresult GetDocSelectionLocation(nsIContent **start, nsIContent **end, 
00294                                    nsIFrame **startFrame, PRUint32 *startOffset);
00295 
00296   void GetSelection ( nsIFrame* inFrame, nsPresContext* inPresContext, nsIFrameSelection** outSelection ) ;
00297 
00298   // To be called before and after you fire an event, to update booleans and
00299   // such
00300   void BeforeDispatchEvent() { ++mDOMEventLevel; }
00301   void AfterDispatchEvent();
00302 
00303   PRInt32     mLockCursor;
00304 
00305   //Any frames here must be checked for validity in ClearFrameRefs
00306   nsIFrame* mCurrentTarget;
00307   nsCOMPtr<nsIContent> mCurrentTargetContent;
00308   nsCOMPtr<nsIContent> mCurrentRelatedContent;
00309   nsIFrame* mLastMouseOverFrame;
00310   nsCOMPtr<nsIContent> mLastMouseOverElement;
00311   nsIFrame* mLastDragOverFrame;
00312 
00313   // member variables for the d&d gesture state machine
00314   nsPoint mGestureDownPoint; // screen coordinates
00315   // The content to use as target if we start a d&d (what we drag).
00316   nsCOMPtr<nsIContent> mGestureDownContent;
00317   // The content of the frame where the mouse-down event occurred. It's the same
00318   // as the target in most cases but not always - for example when dragging
00319   // an <area> of an image map this is the image. (bug 289667)
00320   nsCOMPtr<nsIContent> mGestureDownFrameOwner;
00321   // State of keys when the original gesture-down happened
00322   PRPackedBool mGestureDownShift;
00323   PRPackedBool mGestureDownControl;
00324   PRPackedBool mGestureDownAlt;
00325   PRPackedBool mGestureDownMeta;
00326 
00327   nsCOMPtr<nsIContent> mLastLeftMouseDownContent;
00328   nsCOMPtr<nsIContent> mLastMiddleMouseDownContent;
00329   nsCOMPtr<nsIContent> mLastRightMouseDownContent;
00330 
00331   nsCOMPtr<nsIContent> mActiveContent;
00332   nsCOMPtr<nsIContent> mHoverContent;
00333   nsCOMPtr<nsIContent> mDragOverContent;
00334   nsCOMPtr<nsIContent> mURLTargetContent;
00335   nsCOMPtr<nsIContent> mCurrentFocus;
00336   nsCOMPtr<nsIContent> mLastFocus;
00337   nsIFrame* mCurrentFocusFrame;
00338   PRInt32 mCurrentTabIndex;
00339   EFocusedWithType mLastFocusedWith;
00340 
00341   // DocShell Traversal Data Memebers
00342   nsCOMPtr<nsIContent> mLastContentFocus;
00343 
00344   //Anti-recursive stack controls
00345 
00346   nsCOMPtr<nsIContent> mFirstBlurEvent;
00347   nsCOMPtr<nsIContent> mFirstFocusEvent;
00348 
00349   // The last element on which we fired a mouseover event, or null if
00350   // the last mouseover event we fired has finished processing.
00351   nsCOMPtr<nsIContent> mFirstMouseOverEventElement;
00352 
00353   // The last element on which we fired a mouseout event, or null if
00354   // the last mouseout event we fired has finished processing.
00355   nsCOMPtr<nsIContent> mFirstMouseOutEventElement;
00356 
00357   nsPresContext* mPresContext;      // Not refcnted
00358   nsCOMPtr<nsIDocument> mDocument;   // Doesn't necessarily need to be owner
00359 
00360   PRUint32 mLClickCount;
00361   PRUint32 mMClickCount;
00362   PRUint32 mRClickCount;
00363 
00364   PRPackedBool mConsumeFocusEvents;
00365 
00366   PRPackedBool mNormalLMouseEventInProcess;
00367 
00368   PRPackedBool m_haveShutdown;
00369 
00370   // To inform people that dispatched events that frames have been cleared and
00371   // they need to drop frame refs
00372   PRPackedBool mClearedFrameRefsDuringEvent;
00373 
00374   // So we don't have to keep checking accessibility.browsewithcaret pref
00375   PRPackedBool mBrowseWithCaret;
00376 
00377   // Recursion guard for tabbing
00378   PRPackedBool mTabbedThroughDocument;
00379 
00380   // The number of events we are currently nested in (currently just applies to
00381   // those handlers that care about clearing frame refs)
00382   PRInt32 mDOMEventLevel;
00383 
00384   //Hashtable for accesskey support
00385   nsSupportsHashtable *mAccessKeys;
00386 
00387   nsCOMArray<nsIDocShell> mTabbingFromDocShells;
00388 
00389 #ifdef CLICK_HOLD_CONTEXT_MENUS
00390   enum { kClickHoldDelay = 500 } ;        // 500ms == 1/2 second
00391 
00392   void CreateClickHoldTimer ( nsPresContext* aPresContext, nsIFrame* inDownFrame,
00393                               nsGUIEvent* inMouseDownEvent ) ;
00394   void KillClickHoldTimer ( ) ;
00395   void FireContextClick ( ) ;
00396   static void sClickHoldCallback ( nsITimer* aTimer, void* aESM ) ;
00397   
00398   nsCOMPtr<nsITimer> mClickHoldTimer;
00399 #endif
00400 
00401   static PRInt32 sUserInputEventDepth;
00402 };
00403 
00404 
00405 class nsAutoHandlingUserInputStatePusher
00406 {
00407 public:
00408   nsAutoHandlingUserInputStatePusher(PRBool aIsHandlingUserInput)
00409     : mIsHandlingUserInput(aIsHandlingUserInput)
00410   {
00411     if (aIsHandlingUserInput) {
00412       nsEventStateManager::StartHandlingUserInput();
00413     }
00414   }
00415 
00416   ~nsAutoHandlingUserInputStatePusher()
00417   {
00418     if (mIsHandlingUserInput) {
00419       nsEventStateManager::StopHandlingUserInput();
00420     }
00421   }
00422 
00423 protected:
00424   PRBool mIsHandlingUserInput;
00425 
00426 private:
00427   // Hide so that this class can only be stack-allocated
00428   static void* operator new(size_t /*size*/) CPP_THROW_NEW { return nsnull; }
00429   static void operator delete(void* /*memory*/) {}
00430 };
00431 
00432 #endif // nsEventStateManager_h__