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
15 * The Original Code is mozilla.org 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.
23 * Mats Palmgren <mats.palmgren@bredband.net>
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 #ifndef nsEventStateManager_h__
40 #define nsEventStateManager_h__
42 #include "nsIEventStateManager.h"
43 #include "nsGUIEvent.h"
44 #include "nsIContent.h"
45 #include "nsIObserver.h"
46 #include "nsWeakReference.h"
47 #include "nsHashtable.h"
50 #include "nsIDocument.h"
51 #include "nsCOMArray.h"
53 #include "nsCycleCollectionParticipant.h"
54 #include "nsIMarkupDocumentViewer.h"
56 class nsIScrollableView
;
59 class nsIDocShellTreeNode
;
60 class nsIDocShellTreeItem
;
61 class nsIFocusController
;
63 class nsDOMDataTransfer
;
65 // mac uses click-hold context menus, a holdover from 4.x
66 // touch screens (like hildon) could use this also,
67 // perhaps we should move to NS_TOUCHSCREEN
68 #if defined(XP_MACOSX) || defined(NS_HILDON)
69 #define CLICK_HOLD_CONTEXT_MENUS 1
74 * Event listener manager
77 class nsEventStateManager
: public nsSupportsWeakReference
,
78 public nsIEventStateManager
,
82 nsEventStateManager();
83 virtual ~nsEventStateManager();
85 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
91 /* The PreHandleEvent method is called before event dispatch to either
92 * the DOM or frames. Any processing which must not be prevented or
93 * cancelled should occur here. Any processing which is intended to
94 * be conditional based on either DOM or frame processing should occur in
95 * PostHandleEvent. Any centralized event processing which must occur before
96 * DOM or frame event handling should occur here as well.
98 NS_IMETHOD
PreHandleEvent(nsPresContext
* aPresContext
,
100 nsIFrame
* aTargetFrame
,
101 nsEventStatus
* aStatus
,
104 /* The PostHandleEvent method should contain all system processing which
105 * should occur conditionally based on DOM or frame processing. It should
106 * also contain any centralized event processing which must occur after
107 * DOM and frame processing.
109 NS_IMETHOD
PostHandleEvent(nsPresContext
* aPresContext
,
111 nsIFrame
* aTargetFrame
,
112 nsEventStatus
* aStatus
,
115 NS_IMETHOD
NotifyDestroyPresContext(nsPresContext
* aPresContext
);
116 NS_IMETHOD
SetPresContext(nsPresContext
* aPresContext
);
117 NS_IMETHOD
ClearFrameRefs(nsIFrame
* aFrame
);
119 NS_IMETHOD
GetEventTarget(nsIFrame
**aFrame
);
120 NS_IMETHOD
GetEventTargetContent(nsEvent
* aEvent
, nsIContent
** aContent
);
122 NS_IMETHOD
GetContentState(nsIContent
*aContent
, PRInt32
& aState
);
123 virtual PRBool
SetContentState(nsIContent
*aContent
, PRInt32 aState
);
124 NS_IMETHOD
GetFocusedContent(nsIContent
**aContent
);
125 NS_IMETHOD
SetFocusedContent(nsIContent
* aContent
);
126 NS_IMETHOD
GetLastFocusedContent(nsIContent
**aContent
);
127 NS_IMETHOD
GetFocusedFrame(nsIFrame
**aFrame
);
128 NS_IMETHOD
ContentRemoved(nsIContent
* aContent
);
129 NS_IMETHOD
EventStatusOK(nsGUIEvent
* aEvent
, PRBool
*aOK
);
131 // Access Key Registration
132 NS_IMETHOD
RegisterAccessKey(nsIContent
* aContent
, PRUint32 aKey
);
133 NS_IMETHOD
UnregisterAccessKey(nsIContent
* aContent
, PRUint32 aKey
);
134 NS_IMETHOD
GetRegisteredAccessKey(nsIContent
* aContent
, PRUint32
* aKey
);
136 NS_IMETHOD
SetCursor(PRInt32 aCursor
, imgIContainer
* aContainer
,
137 PRBool aHaveHotspot
, float aHotspotX
, float aHotspotY
,
138 nsIWidget
* aWidget
, PRBool aLockCursor
);
140 NS_IMETHOD
ShiftFocus(PRBool aForward
, nsIContent
* aStart
=nsnull
);
142 virtual PRBool
GetBrowseWithCaret();
143 void ResetBrowseWithCaret();
145 NS_IMETHOD
MoveFocusToCaret(PRBool aCanFocusDoc
, PRBool
*aIsSelectionWithFocus
);
146 NS_IMETHOD
MoveCaretToFocus();
147 NS_IMETHOD
ChangeFocusWith(nsIContent
* aFocus
, EFocusedWithType aFocusedWith
);
149 static void StartHandlingUserInput()
151 ++sUserInputEventDepth
;
154 static void StopHandlingUserInput()
156 --sUserInputEventDepth
;
159 static PRBool
IsHandlingUserInput()
161 return sUserInputEventDepth
> 0;
164 NS_IMETHOD_(PRBool
) IsHandlingUserInputExternal() { return IsHandlingUserInput(); }
166 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsEventStateManager
,
167 nsIEventStateManager
)
171 * In certain situations the focus controller's concept of focus gets out of
172 * whack with mCurrentFocus. This is used in known cases to reset the focus
173 * controller's focus. At some point we should probably move to a single
174 * focus storage mechanism because tracking it in several places is error-prone.
176 void EnsureFocusSynchronization();
178 void UpdateCursor(nsPresContext
* aPresContext
, nsEvent
* aEvent
, nsIFrame
* aTargetFrame
, nsEventStatus
* aStatus
);
180 * Turn a GUI mouse event into a mouse event targeted at the specified
181 * content. This returns the primary frame for the content (or null
182 * if it goes away during the event).
184 nsIFrame
* DispatchMouseEvent(nsGUIEvent
* aEvent
, PRUint32 aMessage
,
185 nsIContent
* aTargetContent
,
186 nsIContent
* aRelatedContent
);
188 * Synthesize DOM and frame mouseover and mouseout events from this
189 * MOUSE_MOVE or MOUSE_EXIT event.
191 void GenerateMouseEnterExit(nsGUIEvent
* aEvent
);
193 * Tell this ESM and ESMs in parent documents that the mouse is
194 * over some content in this document.
196 void NotifyMouseOver(nsGUIEvent
* aEvent
, nsIContent
* aContent
);
198 * Tell this ESM and ESMs in affected child documents that the mouse
199 * has exited this document's currently hovered content.
200 * @param aEvent the event that triggered the mouseout
201 * @param aMovingInto the content node we've moved into. This is used to set
202 * the relatedTarget for mouseout events. Also, if it's non-null
203 * NotifyMouseOut will NOT change the current hover content to null;
204 * in that case the caller is responsible for updating hover state.
206 void NotifyMouseOut(nsGUIEvent
* aEvent
, nsIContent
* aMovingInto
);
207 void GenerateDragDropEnterExit(nsPresContext
* aPresContext
, nsGUIEvent
* aEvent
);
209 * Fire the dragenter and dragexit/dragleave events when the mouse moves to a
212 * @param aRelatedTarget relatedTarget to set for the event
213 * @param aTargetContent target to set for the event
214 * @param aTargetFrame target frame for the event
216 void FireDragEnterOrExit(nsPresContext
* aPresContext
,
219 nsIContent
* aRelatedTarget
,
220 nsIContent
* aTargetContent
,
221 nsWeakFrame
& aTargetFrame
);
222 nsresult
SetClickCount(nsPresContext
* aPresContext
, nsMouseEvent
*aEvent
, nsEventStatus
* aStatus
);
223 nsresult
CheckForAndDispatchClick(nsPresContext
* aPresContext
, nsMouseEvent
*aEvent
, nsEventStatus
* aStatus
);
224 nsresult
GetNextTabbableContent(nsIContent
* aRootContent
,
225 nsIContent
* aStartContent
,
226 nsIFrame
* aStartFrame
,
227 PRBool forward
, PRBool ignoreTabIndex
,
228 nsIContent
** aResultNode
,
229 nsIFrame
** aResultFrame
);
230 nsIContent
*GetNextTabbableMapArea(PRBool aForward
, nsIContent
*imageContent
);
232 PRInt32
GetNextTabIndex(nsIContent
* aParent
, PRBool foward
);
233 nsresult
SendFocusBlur(nsPresContext
* aPresContext
, nsIContent
*aContent
, PRBool aEnsureWindowHasFocus
);
234 void EnsureDocument(nsIPresShell
* aPresShell
);
235 void EnsureDocument(nsPresContext
* aPresContext
);
236 void FlushPendingEvents(nsPresContext
* aPresContext
);
237 nsIFocusController
* GetFocusControllerForDocument(nsIDocument
* aDocument
);
240 * The phases of HandleAccessKey processing. See below.
243 eAccessKeyProcessingNormal
= 0,
244 eAccessKeyProcessingUp
,
245 eAccessKeyProcessingDown
246 } ProcessingAccessKeyState
;
249 * Access key handling. If there is registered content for the accesskey
250 * given by the key event and modifier mask then call
251 * content.PerformAccesskey(), otherwise call HandleAccessKey() recursively,
252 * on descendant docshells first, then on the ancestor (with |aBubbledFrom|
253 * set to the docshell associated with |this|), until something matches.
255 * @param aPresContext the presentation context
256 * @param aEvent the key event
257 * @param aStatus the event status
258 * @param aBubbledFrom is used by an ancestor to avoid calling HandleAccessKey()
259 * on the child the call originally came from, i.e. this is the child
260 * that recursively called us in it's Up phase. The initial caller
261 * passes |nsnull| here. This is to avoid an infinite loop.
262 * @param aAccessKeyState Normal, Down or Up processing phase (see enums
263 * above). The initial event receiver uses 'normal', then 'down' when
264 * processing children and Up when recursively calling its ancestor.
265 * @param aModifierMask modifier mask for the key event
267 void HandleAccessKey(nsPresContext
* aPresContext
,
269 nsEventStatus
* aStatus
,
270 nsIDocShellTreeItem
* aBubbledFrom
,
271 ProcessingAccessKeyState aAccessKeyState
,
272 PRInt32 aModifierMask
);
274 PRBool
ExecuteAccessKey(nsTArray
<PRUint32
>& aAccessCharCodes
,
275 PRBool aIsTrustedEvent
);
277 //---------------------------------------------
278 // DocShell Focus Traversal Methods
279 //---------------------------------------------
281 nsresult
ShiftFocusInternal(PRBool aForward
, nsIContent
* aStart
= nsnull
);
282 void TabIntoDocument(nsIDocShell
* aDocShell
, PRBool aForward
);
283 void ShiftFocusByDoc(PRBool forward
);
284 PRBool
IsFrameSetDoc(nsIDocShell
* aDocShell
);
285 PRBool
IsIFrameDoc(nsIDocShell
* aDocShell
);
286 PRBool
IsShellVisible(nsIDocShell
* aShell
);
287 void GetLastChildDocShell(nsIDocShellTreeItem
* aItem
,
288 nsIDocShellTreeItem
** aResult
);
289 void GetNextDocShell(nsIDocShellTreeNode
* aNode
,
290 nsIDocShellTreeItem
** aResult
);
291 void GetPrevDocShell(nsIDocShellTreeNode
* aNode
,
292 nsIDocShellTreeItem
** aResult
);
294 // These functions are for mousewheel scrolling
295 nsresult
GetParentScrollingView(nsInputEvent
* aEvent
,
296 nsPresContext
* aPresContext
,
297 nsIFrame
* &targetOuterFrame
,
298 nsPresContext
* &presCtxOuter
);
300 void SendPixelScrollEvent(nsIFrame
* aTargetFrame
,
301 nsMouseScrollEvent
* aEvent
,
302 nsPresContext
* aPresContext
,
303 nsEventStatus
* aStatus
);
309 nsresult
DoScrollText(nsPresContext
* aPresContext
,
310 nsIFrame
* aTargetFrame
,
311 nsInputEvent
* aEvent
,
313 PRBool aScrollHorizontal
,
314 ScrollQuantity aScrollQuantity
);
315 void ForceViewUpdate(nsIView
* aView
);
316 void DoScrollHistory(PRInt32 direction
);
317 void DoScrollZoom(nsIFrame
*aTargetFrame
, PRInt32 adjustment
);
318 nsresult
GetMarkupDocumentViewer(nsIMarkupDocumentViewer
** aMv
);
319 nsresult
ChangeTextSize(PRInt32 change
);
320 nsresult
ChangeFullZoom(PRInt32 change
);
321 // end mousewheel functions
323 // routines for the d&d gesture tracking state machine
324 void BeginTrackingDragGesture ( nsPresContext
* aPresContext
, nsMouseEvent
* inDownEvent
,
325 nsIFrame
* inDownFrame
) ;
326 void StopTrackingDragGesture ( ) ;
327 void GenerateDragGesture ( nsPresContext
* aPresContext
, nsMouseEvent
*aEvent
) ;
330 * Determine which node the drag should be targeted at.
331 * This is either the node clicked when there is a selection, or, for HTML,
332 * the element with a draggable property set to true.
334 * aSelectionTarget - target to check for selection
335 * aDataTransfer - data transfer object that will contain the data to drag
336 * aIsSelection - [out] set to true if a selection is being dragged
337 * aIsInEditor - [out] set to true if the content is in an editor field
338 * aTargetNode - [out] the draggable node, or null if there isn't one
340 void DetermineDragTarget(nsPresContext
* aPresContext
,
341 nsIContent
* aSelectionTarget
,
342 nsDOMDataTransfer
* aDataTransfer
,
343 PRBool
* aIsSelection
,
345 nsIContent
** aTargetNode
);
348 * Perform the default handling for the dragstart/draggesture event and set up a
349 * drag for aDataTransfer if it contains any data.
351 * aDragEvent - the dragstart/draggesture event
352 * aDataTransfer - the data transfer that holds the data to be dragged
353 * aDragTarget - the target of the drag
354 * aIsSelection - true if a selection is being dragged
356 void DoDefaultDragStart(nsPresContext
* aPresContext
,
357 nsDragEvent
* aDragEvent
,
358 nsDOMDataTransfer
* aDataTransfer
,
359 nsIContent
* aDragTarget
,
360 PRBool aIsSelection
);
362 PRBool
IsTrackingDragGesture ( ) const { return mGestureDownContent
!= nsnull
; }
364 * Set the fields of aEvent to reflect the mouse position and modifier keys
365 * that were set when the user first pressed the mouse button (stored by
366 * BeginTrackingDragGesture). aEvent->widget must be
367 * mCurrentTarget->GetWindow().
369 void FillInEventFromGestureDown(nsMouseEvent
* aEvent
);
371 PRBool mSuppressFocusChange
; // Used only for Ender text fields to suppress a focus firing on mouse down
373 nsresult
SetCaretEnabled(nsIPresShell
*aPresShell
, PRBool aVisibility
);
374 nsresult
SetContentCaretVisible(nsIPresShell
* aPresShell
, nsIContent
*aContent
, PRBool aVisible
);
375 void FocusElementButNotDocument(nsIContent
*aElement
);
377 // Return the location of the caret
378 nsresult
GetDocSelectionLocation(nsIContent
**start
, nsIContent
**end
,
379 nsIFrame
**startFrame
, PRUint32
*startOffset
);
383 nsWeakFrame mCurrentTarget
;
384 nsCOMPtr
<nsIContent
> mCurrentTargetContent
;
385 nsWeakFrame mLastMouseOverFrame
;
386 nsCOMPtr
<nsIContent
> mLastMouseOverElement
;
387 nsWeakFrame mLastDragOverFrame
;
389 // member variables for the d&d gesture state machine
390 nsPoint mGestureDownPoint
; // screen coordinates
391 // The content to use as target if we start a d&d (what we drag).
392 nsCOMPtr
<nsIContent
> mGestureDownContent
;
393 // The content of the frame where the mouse-down event occurred. It's the same
394 // as the target in most cases but not always - for example when dragging
395 // an <area> of an image map this is the image. (bug 289667)
396 nsCOMPtr
<nsIContent
> mGestureDownFrameOwner
;
397 // State of keys when the original gesture-down happened
398 PRPackedBool mGestureDownShift
;
399 PRPackedBool mGestureDownControl
;
400 PRPackedBool mGestureDownAlt
;
401 PRPackedBool mGestureDownMeta
;
403 nsCOMPtr
<nsIContent
> mLastLeftMouseDownContent
;
404 nsCOMPtr
<nsIContent
> mLastMiddleMouseDownContent
;
405 nsCOMPtr
<nsIContent
> mLastRightMouseDownContent
;
407 nsCOMPtr
<nsIContent
> mActiveContent
;
408 nsCOMPtr
<nsIContent
> mHoverContent
;
409 nsCOMPtr
<nsIContent
> mDragOverContent
;
410 nsCOMPtr
<nsIContent
> mURLTargetContent
;
411 nsCOMPtr
<nsIContent
> mCurrentFocus
;
412 nsCOMPtr
<nsIContent
> mLastFocus
;
413 nsWeakFrame mCurrentFocusFrame
;
414 PRInt32 mCurrentTabIndex
;
415 EFocusedWithType mLastFocusedWith
;
417 // DocShell Traversal Data Memebers
418 nsCOMPtr
<nsIContent
> mLastContentFocus
;
420 //Anti-recursive stack controls
422 nsCOMPtr
<nsIContent
> mFirstBlurEvent
;
423 nsCOMPtr
<nsIDocument
> mFirstDocumentBlurEvent
;
424 nsCOMPtr
<nsIContent
> mFirstFocusEvent
;
426 // The last element on which we fired a mouseover event, or null if
427 // the last mouseover event we fired has finished processing.
428 nsCOMPtr
<nsIContent
> mFirstMouseOverEventElement
;
430 // The last element on which we fired a mouseout event, or null if
431 // the last mouseout event we fired has finished processing.
432 nsCOMPtr
<nsIContent
> mFirstMouseOutEventElement
;
434 nsPresContext
* mPresContext
; // Not refcnted
435 nsCOMPtr
<nsIDocument
> mDocument
; // Doesn't necessarily need to be owner
437 PRUint32 mLClickCount
;
438 PRUint32 mMClickCount
;
439 PRUint32 mRClickCount
;
441 PRPackedBool mNormalLMouseEventInProcess
;
443 PRPackedBool m_haveShutdown
;
445 // So we don't have to keep checking accessibility.browsewithcaret pref
446 PRPackedBool mBrowseWithCaret
;
448 // Recursion guard for tabbing
449 PRPackedBool mTabbedThroughDocument
;
451 // Array for accesskey support
452 nsCOMArray
<nsIContent
> mAccessKeys
;
454 nsCOMArray
<nsIDocShell
> mTabbingFromDocShells
;
456 // Unlocks pixel scrolling
457 PRPackedBool mLastLineScrollConsumedX
;
458 PRPackedBool mLastLineScrollConsumedY
;
460 #ifdef CLICK_HOLD_CONTEXT_MENUS
461 enum { kClickHoldDelay
= 500 } ; // 500ms == 1/2 second
463 void CreateClickHoldTimer ( nsPresContext
* aPresContext
, nsIFrame
* inDownFrame
,
464 nsGUIEvent
* inMouseDownEvent
) ;
465 void KillClickHoldTimer ( ) ;
466 void FireContextClick ( ) ;
467 static void sClickHoldCallback ( nsITimer
* aTimer
, void* aESM
) ;
469 nsCOMPtr
<nsITimer
> mClickHoldTimer
;
472 static PRInt32 sUserInputEventDepth
;
476 class nsAutoHandlingUserInputStatePusher
479 nsAutoHandlingUserInputStatePusher(PRBool aIsHandlingUserInput
)
480 : mIsHandlingUserInput(aIsHandlingUserInput
)
482 if (aIsHandlingUserInput
) {
483 nsEventStateManager::StartHandlingUserInput();
487 ~nsAutoHandlingUserInputStatePusher()
489 if (mIsHandlingUserInput
) {
490 nsEventStateManager::StopHandlingUserInput();
495 PRBool mIsHandlingUserInput
;
498 // Hide so that this class can only be stack-allocated
499 static void* operator new(size_t /*size*/) CPP_THROW_NEW
{ return nsnull
; }
500 static void operator delete(void* /*memory*/) {}
503 #endif // nsEventStateManager_h__