2 * Copyright (C) 2006, 2007, 2009, 2010, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef EventHandler_h
27 #define EventHandler_h
29 #include "core/editing/TextGranularity.h"
30 #include "core/events/TextEventInputType.h"
31 #include "core/page/DragActions.h"
32 #include "core/page/FocusType.h"
33 #include "core/rendering/HitTestRequest.h"
34 #include "core/rendering/style/RenderStyleConstants.h"
35 #include "platform/Cursor.h"
36 #include "platform/PlatformMouseEvent.h"
37 #include "platform/Timer.h"
38 #include "platform/UserGestureIndicator.h"
39 #include "platform/geometry/LayoutPoint.h"
40 #include "platform/heap/Handle.h"
41 #include "platform/scroll/ScrollTypes.h"
42 #include "wtf/Forward.h"
43 #include "wtf/HashMap.h"
44 #include "wtf/HashTraits.h"
45 #include "wtf/RefPtr.h"
49 class AutoscrollController;
57 class FullscreenElementStack;
59 class HTMLFrameSetElement;
63 class MouseEventWithHitTestResults;
66 class PlatformGestureEvent;
67 class PlatformKeyboardEvent;
68 class PlatformTouchEvent;
69 class PlatformWheelEvent;
71 class RenderLayerScrollableArea;
74 class SVGElementInstance;
79 class VisibleSelection;
85 enum AppendTrailingWhitespace { ShouldAppendTrailingWhitespace, DontAppendTrailingWhitespace };
86 enum CheckDragHysteresis { ShouldCheckDragHysteresis, DontCheckDragHysteresis };
89 WTF_MAKE_NONCOPYABLE(EventHandler);
91 explicit EventHandler(LocalFrame*);
95 void nodeWillBeRemoved(Node&);
97 void updateSelectionForMouseDrag();
99 Node* mousePressNode() const;
102 void startPanScrolling(RenderObject*);
105 void stopAutoscroll();
107 void dispatchFakeMouseMoveEventSoon();
108 void dispatchFakeMouseMoveEventSoonInQuad(const FloatQuad&);
110 HitTestResult hitTestResultAtPoint(const LayoutPoint&,
111 HitTestRequest::HitTestRequestType hitType = HitTestRequest::ReadOnly | HitTestRequest::Active,
112 const LayoutSize& padding = LayoutSize());
114 bool mousePressed() const { return m_mousePressed; }
115 void setMousePressed(bool pressed) { m_mousePressed = pressed; }
117 void setCapturingMouseEventsNode(PassRefPtr<Node>); // A caller is responsible for resetting capturing node to 0.
119 bool updateDragAndDrop(const PlatformMouseEvent&, Clipboard*);
120 void cancelDragAndDrop(const PlatformMouseEvent&, Clipboard*);
121 bool performDragAndDrop(const PlatformMouseEvent&, Clipboard*);
122 void updateDragStateAfterEditDragIfNeeded(Element* rootEditableElement);
124 void scheduleHoverStateUpdate();
125 void scheduleCursorUpdate();
127 void setResizingFrameSet(HTMLFrameSetElement*);
129 void resizeScrollableAreaDestroyed();
131 IntPoint lastKnownMousePosition() const;
132 Cursor currentMouseCursor() const { return m_currentMouseCursor; }
134 // Attempts to scroll the DOM tree. If that fails, scrolls the view.
135 // If the view can't be scrolled either, recursively bubble to the parent frame.
136 bool bubblingScroll(ScrollDirection, ScrollGranularity, Node* startingNode = 0);
138 bool handleMouseMoveEvent(const PlatformMouseEvent&);
139 void handleMouseLeaveEvent(const PlatformMouseEvent&);
141 bool handleMousePressEvent(const PlatformMouseEvent&);
142 bool handleMouseReleaseEvent(const PlatformMouseEvent&);
143 bool handleWheelEvent(const PlatformWheelEvent&);
144 void defaultWheelEventHandler(Node*, WheelEvent*);
146 bool handleGestureEvent(const PlatformGestureEvent&);
147 bool handleGestureScrollEnd(const PlatformGestureEvent&);
148 bool isScrollbarHandlingGestures() const;
150 bool bestClickableNodeForTouchPoint(const IntPoint& touchCenter, const IntSize& touchRadius, IntPoint& targetPoint, Node*& targetNode);
151 bool bestContextMenuNodeForTouchPoint(const IntPoint& touchCenter, const IntSize& touchRadius, IntPoint& targetPoint, Node*& targetNode);
152 bool bestZoomableAreaForTouchPoint(const IntPoint& touchCenter, const IntSize& touchRadius, IntRect& targetArea, Node*& targetNode);
154 void adjustGesturePosition(const PlatformGestureEvent&, IntPoint& adjustedPoint);
156 bool sendContextMenuEvent(const PlatformMouseEvent&);
157 bool sendContextMenuEventForKey();
158 bool sendContextMenuEventForGesture(const PlatformGestureEvent&);
160 void setMouseDownMayStartAutoscroll() { m_mouseDownMayStartAutoscroll = true; }
162 static unsigned accessKeyModifiers();
163 bool handleAccessKey(const PlatformKeyboardEvent&);
164 bool keyEvent(const PlatformKeyboardEvent&);
165 void defaultKeyboardEventHandler(KeyboardEvent*);
167 bool handleTextInputEvent(const String& text, Event* underlyingEvent = 0, TextEventInputType = TextEventInputKeyboard);
168 void defaultTextInputEventHandler(TextEvent*);
170 void dragSourceEndedAt(const PlatformMouseEvent&, DragOperation);
172 void focusDocumentView();
174 void capsLockStateMayHaveChanged(); // Only called by FrameSelection
176 bool handleTouchEvent(const PlatformTouchEvent&);
178 bool useHandCursor(Node*, bool isOverLink);
180 void notifyElementActivated();
182 PassRefPtr<UserGestureToken> takeLastMouseDownGestureToken() { return m_lastMouseDownUserGestureToken.release(); }
185 static DragState& dragState();
187 PassRefPtrWillBeRawPtr<Clipboard> createDraggingClipboard() const;
189 bool updateSelectionForMouseDownDispatchingSelectStart(Node*, const VisibleSelection&, TextGranularity);
190 void selectClosestWordFromHitTestResult(const HitTestResult&, AppendTrailingWhitespace);
191 void selectClosestMisspellingFromHitTestResult(const HitTestResult&, AppendTrailingWhitespace);
192 void selectClosestWordFromMouseEvent(const MouseEventWithHitTestResults&);
193 void selectClosestMisspellingFromMouseEvent(const MouseEventWithHitTestResults&);
194 void selectClosestWordOrLinkFromMouseEvent(const MouseEventWithHitTestResults&);
196 bool handleMouseMoveOrLeaveEvent(const PlatformMouseEvent&, HitTestResult* hoveredNode = 0, bool onlyUpdateScrollbars = false);
197 bool handleMousePressEvent(const MouseEventWithHitTestResults&);
198 bool handleMousePressEventSingleClick(const MouseEventWithHitTestResults&);
199 bool handleMousePressEventDoubleClick(const MouseEventWithHitTestResults&);
200 bool handleMousePressEventTripleClick(const MouseEventWithHitTestResults&);
201 bool handleMouseFocus(const PlatformMouseEvent&);
202 bool handleMouseDraggedEvent(const MouseEventWithHitTestResults&);
203 bool handleMouseReleaseEvent(const MouseEventWithHitTestResults&);
205 bool handlePasteGlobalSelection(const PlatformMouseEvent&);
207 bool handleGestureTap(const PlatformGestureEvent&, const IntPoint& adjustedPoint);
208 bool handleGestureLongPress(const PlatformGestureEvent&, const IntPoint& adjustedPoint);
209 bool handleGestureLongTap(const PlatformGestureEvent&, const IntPoint& adjustedPoint);
210 bool handleGestureTwoFingerTap(const PlatformGestureEvent&, const IntPoint& adjustedPoint);
211 bool handleGestureScrollUpdate(const PlatformGestureEvent&);
212 bool handleGestureScrollBegin(const PlatformGestureEvent&);
213 void clearGestureScrollNodes();
215 bool shouldApplyTouchAdjustment(const PlatformGestureEvent&) const;
217 OptionalCursor selectCursor(const HitTestResult&);
218 OptionalCursor selectAutoCursor(const HitTestResult&, Node*, const Cursor& iBeam);
220 void hoverTimerFired(Timer<EventHandler>*);
221 void cursorUpdateTimerFired(Timer<EventHandler>*);
222 void activeIntervalTimerFired(Timer<EventHandler>*);
224 bool mouseDownMayStartSelect() const { return m_mouseDownMayStartSelect; }
226 void fakeMouseMoveEventTimerFired(Timer<EventHandler>*);
227 void cancelFakeMouseMoveEvent();
228 bool isCursorVisible() const;
231 bool isInsideScrollbar(const IntPoint&) const;
233 ScrollableArea* associatedScrollableArea(const RenderLayer*) const;
235 // Scrolls the elements of the DOM tree. Returns true if a node was scrolled.
236 // False if we reached the root and couldn't scroll anything.
237 // direction - The direction to scroll in. If this is a logicl direction, it will be
238 // converted to the physical direction based on a node's writing mode.
239 // granularity - The units that the scroll delta parameter is in.
240 // startNode - The node to start bubbling the scroll from. If a node can't scroll,
241 // the scroll bubbles up to the containing block.
242 // stopNode - On input, if provided and non-null, the node at which we should stop bubbling on input.
243 // On output, if provided and a node was scrolled stopNode will point to that node.
244 // delta - The delta to scroll by, in the units of the granularity parameter. (e.g. pixels, lines, pages, etc.)
245 // absolutePoint - For wheel scrolls - the location, in absolute coordinates, where the event occured.
246 bool scroll(ScrollDirection, ScrollGranularity, Node* startNode = 0, Node** stopNode = 0, float delta = 1.0f, IntPoint absolutePoint = IntPoint());
248 TouchAction intersectTouchAction(const TouchAction, const TouchAction);
249 TouchAction computeEffectiveTouchAction(const LayoutPoint&);
251 HitTestResult hitTestResultInFrame(LocalFrame*, const LayoutPoint&, HitTestRequest::HitTestRequestType hitType = HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::ConfusingAndOftenMisusedDisallowShadowContent);
253 void invalidateClick();
255 Node* nodeUnderMouse() const;
257 void updateMouseEventTargetNode(Node*, const PlatformMouseEvent&, bool fireMouseOverOut);
259 MouseEventWithHitTestResults prepareMouseEvent(const HitTestRequest&, const PlatformMouseEvent&);
261 bool dispatchMouseEvent(const AtomicString& eventType, Node* target, int clickCount, const PlatformMouseEvent&, bool setUnder);
262 bool dispatchDragEvent(const AtomicString& eventType, Node* target, const PlatformMouseEvent&, Clipboard*);
264 void freeClipboard();
266 bool handleDrag(const MouseEventWithHitTestResults&, CheckDragHysteresis);
267 bool tryStartDrag(const MouseEventWithHitTestResults&);
268 void clearDragState();
270 bool dispatchDragSrcEvent(const AtomicString& eventType, const PlatformMouseEvent&);
272 bool dragHysteresisExceeded(const FloatPoint&) const;
273 bool dragHysteresisExceeded(const IntPoint&) const;
275 bool passMousePressEventToSubframe(MouseEventWithHitTestResults&, LocalFrame* subframe);
276 bool passMouseMoveEventToSubframe(MouseEventWithHitTestResults&, LocalFrame* subframe, HitTestResult* hoveredNode = 0);
277 bool passMouseReleaseEventToSubframe(MouseEventWithHitTestResults&, LocalFrame* subframe);
279 bool passMousePressEventToScrollbar(MouseEventWithHitTestResults&, Scrollbar*);
281 bool passWidgetMouseDownEventToWidget(const MouseEventWithHitTestResults&);
283 bool passWheelEventToWidget(const PlatformWheelEvent&, Widget*);
285 void defaultSpaceEventHandler(KeyboardEvent*);
286 void defaultBackspaceEventHandler(KeyboardEvent*);
287 void defaultTabEventHandler(KeyboardEvent*);
288 void defaultEscapeEventHandler(KeyboardEvent*);
289 void defaultArrowEventHandler(FocusType, KeyboardEvent*);
291 void updateSelectionForMouseDrag(const HitTestResult&);
293 void updateLastScrollbarUnderMouse(Scrollbar*, bool);
295 void setFrameWasScrolledByUser();
297 bool capturesDragging() const { return m_capturesDragging; }
299 bool isKeyEventAllowedInFullScreen(FullscreenElementStack*, const PlatformKeyboardEvent&) const;
301 bool handleGestureShowPress();
303 bool handleScrollGestureOnResizer(Node*, const PlatformGestureEvent&);
305 bool passGestureEventToWidget(const PlatformGestureEvent&, Widget*);
306 bool passGestureEventToWidgetIfPossible(const PlatformGestureEvent&, RenderObject*);
307 bool sendScrollEventToView(const PlatformGestureEvent&, const FloatSize&);
308 LocalFrame* getSubFrameForGestureEvent(const IntPoint& touchAdjustedPoint, const PlatformGestureEvent&);
310 AutoscrollController* autoscrollController() const;
311 bool panScrollInProgress() const;
312 void setLastKnownMousePosition(const PlatformMouseEvent&);
314 LocalFrame* const m_frame;
317 bool m_capturesDragging;
318 RefPtr<Node> m_mousePressNode;
320 bool m_mouseDownMayStartSelect;
321 bool m_mouseDownMayStartDrag;
322 bool m_mouseDownWasSingleClickInSelection;
323 enum SelectionInitiationState { HaveNotStartedSelection, PlacedCaret, ExtendedSelection };
324 SelectionInitiationState m_selectionInitiationState;
326 LayoutPoint m_dragStartPos;
328 Timer<EventHandler> m_hoverTimer;
329 Timer<EventHandler> m_cursorUpdateTimer;
331 bool m_mouseDownMayStartAutoscroll;
332 bool m_mouseDownWasInSubframe;
334 Timer<EventHandler> m_fakeMouseMoveEventTimer;
338 RenderLayerScrollableArea* m_resizeScrollableArea;
340 RefPtr<Node> m_capturingMouseEventsNode;
341 bool m_eventHandlerWillResetCapturingMouseEventsNode;
343 RefPtr<Node> m_nodeUnderMouse;
344 RefPtr<Node> m_lastNodeUnderMouse;
345 RefPtr<LocalFrame> m_lastMouseMoveEventSubframe;
346 RefPtr<Scrollbar> m_lastScrollbarUnderMouse;
347 Cursor m_currentMouseCursor;
350 RefPtr<Node> m_clickNode;
352 RefPtr<Node> m_dragTarget;
353 bool m_shouldOnlyFireDragOverEvent;
355 RefPtr<HTMLFrameSetElement> m_frameSetBeingResized;
357 LayoutSize m_offsetFromResizeCorner; // In the coords of m_resizeScrollableArea.
359 bool m_mousePositionIsUnknown;
360 IntPoint m_lastKnownMousePosition;
361 IntPoint m_lastKnownMouseGlobalPosition;
362 IntPoint m_mouseDownPos; // In our view's coords.
363 double m_mouseDownTimestamp;
364 PlatformMouseEvent m_mouseDown;
365 RefPtr<UserGestureToken> m_lastMouseDownUserGestureToken;
367 RefPtr<Node> m_latchedWheelEventNode;
368 bool m_widgetIsLatched;
370 RefPtr<Node> m_previousWheelScrolledNode;
372 // The target of each active touch point indexed by the touch ID.
373 typedef HashMap<unsigned, RefPtr<EventTarget>, DefaultHash<unsigned>::Hash, WTF::UnsignedWithZeroKeyHashTraits<unsigned> > TouchTargetMap;
374 TouchTargetMap m_targetForTouchID;
376 // If set, the document of the active touch sequence. Unset if no touch sequence active.
377 RefPtr<Document> m_touchSequenceDocument;
381 RefPtr<Node> m_scrollGestureHandlingNode;
382 bool m_lastHitTestResultOverWidget;
383 RefPtr<Node> m_previousGestureScrolledNode;
384 RefPtr<Scrollbar> m_scrollbarHandlingScrollGesture;
386 double m_maxMouseMovedDuration;
387 PlatformEvent::Type m_baseEventType;
390 bool m_longTapShouldInvokeContextMenu;
392 Timer<EventHandler> m_activeIntervalTimer;
393 double m_lastShowPressTimestamp;
394 RefPtr<Element> m_lastDeferredTapElement;
397 } // namespace WebCore
399 #endif // EventHandler_h