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/FocusDirection.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/scroll/ScrollTypes.h"
41 #include "wtf/Forward.h"
42 #include "wtf/HashMap.h"
43 #include "wtf/RefPtr.h"
47 class AutoscrollController;
55 class FullscreenElementStack;
57 class HTMLFrameSetElement;
61 class MouseEventWithHitTestResults;
64 class PlatformGestureEvent;
65 class PlatformKeyboardEvent;
66 class PlatformTouchEvent;
67 class PlatformWheelEvent;
69 class RenderLayerScrollableArea;
72 class SVGElementInstance;
77 class VisibleSelection;
83 enum AppendTrailingWhitespace { ShouldAppendTrailingWhitespace, DontAppendTrailingWhitespace };
84 enum CheckDragHysteresis { ShouldCheckDragHysteresis, DontCheckDragHysteresis };
87 WTF_MAKE_NONCOPYABLE(EventHandler);
89 explicit EventHandler(Frame*);
93 void nodeWillBeRemoved(Node&);
95 void updateSelectionForMouseDrag();
97 Node* mousePressNode() const;
100 void startPanScrolling(RenderObject*);
103 void stopAutoscroll();
105 void dispatchFakeMouseMoveEventSoon();
106 void dispatchFakeMouseMoveEventSoonInQuad(const FloatQuad&);
108 HitTestResult hitTestResultAtPoint(const LayoutPoint&,
109 HitTestRequest::HitTestRequestType hitType = HitTestRequest::ReadOnly | HitTestRequest::Active,
110 const LayoutSize& padding = LayoutSize());
112 bool mousePressed() const { return m_mousePressed; }
113 void setMousePressed(bool pressed) { m_mousePressed = pressed; }
115 void setCapturingMouseEventsNode(PassRefPtr<Node>); // A caller is responsible for resetting capturing node to 0.
117 bool updateDragAndDrop(const PlatformMouseEvent&, Clipboard*);
118 void cancelDragAndDrop(const PlatformMouseEvent&, Clipboard*);
119 bool performDragAndDrop(const PlatformMouseEvent&, Clipboard*);
120 void updateDragStateAfterEditDragIfNeeded(Element* rootEditableElement);
122 void scheduleHoverStateUpdate();
123 void scheduleCursorUpdate();
125 void setResizingFrameSet(HTMLFrameSetElement*);
127 void resizeScrollableAreaDestroyed();
129 IntPoint lastKnownMousePosition() const;
130 Cursor currentMouseCursor() const { return m_currentMouseCursor; }
132 // Attempts to scroll the DOM tree. If that fails, scrolls the view.
133 // If the view can't be scrolled either, recursively bubble to the parent frame.
134 bool bubblingScroll(ScrollDirection, ScrollGranularity, Node* startingNode = 0);
136 bool handleMouseMoveEvent(const PlatformMouseEvent&);
137 void handleMouseLeaveEvent(const PlatformMouseEvent&);
139 bool handleMousePressEvent(const PlatformMouseEvent&);
140 bool handleMouseReleaseEvent(const PlatformMouseEvent&);
141 bool handleWheelEvent(const PlatformWheelEvent&);
142 void defaultWheelEventHandler(Node*, WheelEvent*);
144 bool handleGestureEvent(const PlatformGestureEvent&);
145 bool handleGestureScrollEnd(const PlatformGestureEvent&);
146 bool isScrollbarHandlingGestures() const;
148 bool bestClickableNodeForTouchPoint(const IntPoint& touchCenter, const IntSize& touchRadius, IntPoint& targetPoint, Node*& targetNode);
149 bool bestContextMenuNodeForTouchPoint(const IntPoint& touchCenter, const IntSize& touchRadius, IntPoint& targetPoint, Node*& targetNode);
150 bool bestZoomableAreaForTouchPoint(const IntPoint& touchCenter, const IntSize& touchRadius, IntRect& targetArea, Node*& targetNode);
152 bool adjustGesturePosition(const PlatformGestureEvent&, IntPoint& adjustedPoint);
154 bool sendContextMenuEvent(const PlatformMouseEvent&);
155 bool sendContextMenuEventForKey();
156 bool sendContextMenuEventForGesture(const PlatformGestureEvent&);
158 void setMouseDownMayStartAutoscroll() { m_mouseDownMayStartAutoscroll = true; }
160 static unsigned accessKeyModifiers();
161 bool handleAccessKey(const PlatformKeyboardEvent&);
162 bool keyEvent(const PlatformKeyboardEvent&);
163 void defaultKeyboardEventHandler(KeyboardEvent*);
165 bool handleTextInputEvent(const String& text, Event* underlyingEvent = 0, TextEventInputType = TextEventInputKeyboard);
166 void defaultTextInputEventHandler(TextEvent*);
168 void dragSourceEndedAt(const PlatformMouseEvent&, DragOperation);
170 void focusDocumentView();
172 void capsLockStateMayHaveChanged(); // Only called by FrameSelection
174 bool handleTouchEvent(const PlatformTouchEvent&);
176 bool useHandCursor(Node*, bool isOverLink, bool shiftKey);
178 void notifyElementActivated();
181 static DragState& dragState();
183 PassRefPtr<Clipboard> createDraggingClipboard() const;
185 bool updateSelectionForMouseDownDispatchingSelectStart(Node*, const VisibleSelection&, TextGranularity);
186 void selectClosestWordFromHitTestResult(const HitTestResult&, AppendTrailingWhitespace);
187 void selectClosestMisspellingFromHitTestResult(const HitTestResult&, AppendTrailingWhitespace);
188 void selectClosestWordFromMouseEvent(const MouseEventWithHitTestResults&);
189 void selectClosestMisspellingFromMouseEvent(const MouseEventWithHitTestResults&);
190 void selectClosestWordOrLinkFromMouseEvent(const MouseEventWithHitTestResults&);
192 bool handleMouseMoveOrLeaveEvent(const PlatformMouseEvent&, HitTestResult* hoveredNode = 0, bool onlyUpdateScrollbars = false);
193 bool handleMousePressEvent(const MouseEventWithHitTestResults&);
194 bool handleMousePressEventSingleClick(const MouseEventWithHitTestResults&);
195 bool handleMousePressEventDoubleClick(const MouseEventWithHitTestResults&);
196 bool handleMousePressEventTripleClick(const MouseEventWithHitTestResults&);
197 bool handleMouseDraggedEvent(const MouseEventWithHitTestResults&);
198 bool handleMouseReleaseEvent(const MouseEventWithHitTestResults&);
200 bool handlePasteGlobalSelection(const PlatformMouseEvent&);
202 bool handleGestureTap(const PlatformGestureEvent&, const IntPoint& adjustedPoint);
203 bool handleGestureLongPress(const PlatformGestureEvent&, const IntPoint& adjustedPoint);
204 bool handleGestureLongTap(const PlatformGestureEvent&, const IntPoint& adjustedPoint);
205 bool handleGestureTwoFingerTap(const PlatformGestureEvent&, const IntPoint& adjustedPoint);
206 bool handleGestureScrollUpdate(const PlatformGestureEvent&);
207 bool handleGestureScrollBegin(const PlatformGestureEvent&);
208 void clearGestureScrollNodes();
210 bool shouldApplyTouchAdjustment(const PlatformGestureEvent&) const;
212 OptionalCursor selectCursor(const HitTestResult&, bool shiftKey);
213 OptionalCursor selectAutoCursor(const HitTestResult&, Node*, const Cursor& iBeam, bool shiftKey);
215 void hoverTimerFired(Timer<EventHandler>*);
216 void cursorUpdateTimerFired(Timer<EventHandler>*);
217 void activeIntervalTimerFired(Timer<EventHandler>*);
219 bool shouldTurnVerticalTicksIntoHorizontal(const HitTestResult&, const PlatformWheelEvent&) const;
220 bool mouseDownMayStartSelect() const { return m_mouseDownMayStartSelect; }
222 void fakeMouseMoveEventTimerFired(Timer<EventHandler>*);
223 void cancelFakeMouseMoveEvent();
224 bool isCursorVisible() const;
227 bool isInsideScrollbar(const IntPoint&) const;
229 ScrollableArea* associatedScrollableArea(const RenderLayer*) const;
231 // Scrolls the elements of the DOM tree. Returns true if a node was scrolled.
232 // False if we reached the root and couldn't scroll anything.
233 // direction - The direction to scroll in. If this is a logicl direction, it will be
234 // converted to the physical direction based on a node's writing mode.
235 // granularity - The units that the scroll delta parameter is in.
236 // startNode - The node to start bubbling the scroll from. If a node can't scroll,
237 // the scroll bubbles up to the containing block.
238 // stopNode - On input, if provided and non-null, the node at which we should stop bubbling on input.
239 // On output, if provided and a node was scrolled stopNode will point to that node.
240 // delta - The delta to scroll by, in the units of the granularity parameter. (e.g. pixels, lines, pages, etc.)
241 // absolutePoint - For wheel scrolls - the location, in absolute coordinates, where the event occured.
242 bool scroll(ScrollDirection, ScrollGranularity, Node* startNode = 0, Node** stopNode = 0, float delta = 1.0f, IntPoint absolutePoint = IntPoint());
244 bool dispatchSyntheticTouchEventIfEnabled(const PlatformMouseEvent&);
246 TouchAction intersectTouchAction(const TouchAction, const TouchAction);
247 TouchAction computeEffectiveTouchAction(const Node&);
249 bool handleMouseEventAsEmulatedGesture(const PlatformMouseEvent&);
250 bool handleWheelEventAsEmulatedGesture(const PlatformWheelEvent&);
251 HitTestResult hitTestResultInFrame(Frame*, 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, bool cancelable, 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&, Frame* subframe);
276 bool passMouseMoveEventToSubframe(MouseEventWithHitTestResults&, Frame* subframe, HitTestResult* hoveredNode = 0);
277 bool passMouseReleaseEventToSubframe(MouseEventWithHitTestResults&, Frame* 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(FocusDirection, 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 Frame* getSubFrameForGestureEvent(const IntPoint& touchAdjustedPoint, const PlatformGestureEvent&);
310 AutoscrollController* autoscrollController() const;
311 bool panScrollInProgress() const;
312 void setLastKnownMousePosition(const PlatformMouseEvent&);
314 Frame* 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;
337 RefPtr<SVGElementInstance> m_instanceUnderMouse;
338 RefPtr<SVGElementInstance> m_lastInstanceUnderMouse;
340 RenderLayerScrollableArea* m_resizeScrollableArea;
342 RefPtr<Node> m_capturingMouseEventsNode;
343 bool m_eventHandlerWillResetCapturingMouseEventsNode;
345 RefPtr<Node> m_nodeUnderMouse;
346 RefPtr<Node> m_lastNodeUnderMouse;
347 RefPtr<Frame> m_lastMouseMoveEventSubframe;
348 RefPtr<Scrollbar> m_lastScrollbarUnderMouse;
349 Cursor m_currentMouseCursor;
352 RefPtr<Node> m_clickNode;
354 RefPtr<Node> m_dragTarget;
355 bool m_shouldOnlyFireDragOverEvent;
357 RefPtr<HTMLFrameSetElement> m_frameSetBeingResized;
359 LayoutSize m_offsetFromResizeCorner; // In the coords of m_resizeScrollableArea.
361 bool m_mousePositionIsUnknown;
362 IntPoint m_lastKnownMousePosition;
363 IntPoint m_lastKnownMouseGlobalPosition;
364 IntPoint m_mouseDownPos; // In our view's coords.
365 double m_mouseDownTimestamp;
366 PlatformMouseEvent m_mouseDown;
367 RefPtr<UserGestureToken> m_lastMouseDownUserGestureToken;
369 RefPtr<Node> m_latchedWheelEventNode;
370 bool m_widgetIsLatched;
372 RefPtr<Node> m_previousWheelScrolledNode;
374 typedef HashMap<int, RefPtr<EventTarget> > TouchTargetMap;
375 TouchTargetMap m_originatingTouchPointTargets;
376 RefPtr<Document> m_originatingTouchPointDocument;
377 unsigned m_originatingTouchPointTargetKey;
380 RefPtr<Node> m_scrollGestureHandlingNode;
381 bool m_lastHitTestResultOverWidget;
382 RefPtr<Node> m_previousGestureScrolledNode;
383 RefPtr<Scrollbar> m_scrollbarHandlingScrollGesture;
385 double m_maxMouseMovedDuration;
386 PlatformEvent::Type m_baseEventType;
389 bool m_longTapShouldInvokeContextMenu;
390 OwnPtr<IntPoint> m_lastSyntheticPinchAnchorCss;
391 OwnPtr<IntPoint> m_lastSyntheticPinchAnchorDip;
392 OwnPtr<IntPoint> m_lastSyntheticPanLocation;
393 float m_syntheticPageScaleFactor;
395 Timer<EventHandler> m_activeIntervalTimer;
396 double m_lastShowPressTimestamp;
397 RefPtr<Element> m_lastDeferredTapElement;
400 } // namespace WebCore
402 #endif // EventHandler_h