0de3be6c7595b8bc18294b4ddd5ca65bfb6feecb
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / page / EventHandler.h
1 /*
2  * Copyright (C) 2006, 2007, 2009, 2010, 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #ifndef EventHandler_h
27 #define EventHandler_h
28
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"
44
45 namespace WebCore {
46
47 class AutoscrollController;
48 class Clipboard;
49 class Document;
50 class Element;
51 class Event;
52 class EventTarget;
53 class FloatPoint;
54 class FloatQuad;
55 class FullscreenElementStack;
56 class Frame;
57 class HTMLFrameSetElement;
58 class HitTestRequest;
59 class HitTestResult;
60 class KeyboardEvent;
61 class MouseEventWithHitTestResults;
62 class Node;
63 class OptionalCursor;
64 class PlatformGestureEvent;
65 class PlatformKeyboardEvent;
66 class PlatformTouchEvent;
67 class PlatformWheelEvent;
68 class RenderLayer;
69 class RenderLayerScrollableArea;
70 class RenderObject;
71 class RenderWidget;
72 class SVGElementInstance;
73 class ScrollableArea;
74 class Scrollbar;
75 class TextEvent;
76 class TouchEvent;
77 class VisibleSelection;
78 class WheelEvent;
79 class Widget;
80
81 struct DragState;
82
83 enum AppendTrailingWhitespace { ShouldAppendTrailingWhitespace, DontAppendTrailingWhitespace };
84 enum CheckDragHysteresis { ShouldCheckDragHysteresis, DontCheckDragHysteresis };
85
86 class EventHandler {
87     WTF_MAKE_NONCOPYABLE(EventHandler);
88 public:
89     explicit EventHandler(Frame*);
90     ~EventHandler();
91
92     void clear();
93     void nodeWillBeRemoved(Node&);
94
95     void updateSelectionForMouseDrag();
96
97     Node* mousePressNode() const;
98
99 #if OS(WIN)
100     void startPanScrolling(RenderObject*);
101 #endif
102
103     void stopAutoscroll();
104
105     void dispatchFakeMouseMoveEventSoon();
106     void dispatchFakeMouseMoveEventSoonInQuad(const FloatQuad&);
107
108     HitTestResult hitTestResultAtPoint(const LayoutPoint&,
109         HitTestRequest::HitTestRequestType hitType = HitTestRequest::ReadOnly | HitTestRequest::Active,
110         const LayoutSize& padding = LayoutSize());
111
112     bool mousePressed() const { return m_mousePressed; }
113     void setMousePressed(bool pressed) { m_mousePressed = pressed; }
114
115     void setCapturingMouseEventsNode(PassRefPtr<Node>); // A caller is responsible for resetting capturing node to 0.
116
117     bool updateDragAndDrop(const PlatformMouseEvent&, Clipboard*);
118     void cancelDragAndDrop(const PlatformMouseEvent&, Clipboard*);
119     bool performDragAndDrop(const PlatformMouseEvent&, Clipboard*);
120     void updateDragStateAfterEditDragIfNeeded(Element* rootEditableElement);
121
122     void scheduleHoverStateUpdate();
123     void scheduleCursorUpdate();
124
125     void setResizingFrameSet(HTMLFrameSetElement*);
126
127     void resizeScrollableAreaDestroyed();
128
129     IntPoint lastKnownMousePosition() const;
130     Cursor currentMouseCursor() const { return m_currentMouseCursor; }
131
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);
135
136     bool handleMouseMoveEvent(const PlatformMouseEvent&);
137     void handleMouseLeaveEvent(const PlatformMouseEvent&);
138
139     bool handleMousePressEvent(const PlatformMouseEvent&);
140     bool handleMouseReleaseEvent(const PlatformMouseEvent&);
141     bool handleWheelEvent(const PlatformWheelEvent&);
142     void defaultWheelEventHandler(Node*, WheelEvent*);
143
144     bool handleGestureEvent(const PlatformGestureEvent&);
145     bool handleGestureScrollEnd(const PlatformGestureEvent&);
146     bool isScrollbarHandlingGestures() const;
147
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);
151
152     bool adjustGesturePosition(const PlatformGestureEvent&, IntPoint& adjustedPoint);
153
154     bool sendContextMenuEvent(const PlatformMouseEvent&);
155     bool sendContextMenuEventForKey();
156     bool sendContextMenuEventForGesture(const PlatformGestureEvent&);
157
158     void setMouseDownMayStartAutoscroll() { m_mouseDownMayStartAutoscroll = true; }
159
160     static unsigned accessKeyModifiers();
161     bool handleAccessKey(const PlatformKeyboardEvent&);
162     bool keyEvent(const PlatformKeyboardEvent&);
163     void defaultKeyboardEventHandler(KeyboardEvent*);
164
165     bool handleTextInputEvent(const String& text, Event* underlyingEvent = 0, TextEventInputType = TextEventInputKeyboard);
166     void defaultTextInputEventHandler(TextEvent*);
167
168     void dragSourceEndedAt(const PlatformMouseEvent&, DragOperation);
169
170     void focusDocumentView();
171
172     void capsLockStateMayHaveChanged(); // Only called by FrameSelection
173
174     bool handleTouchEvent(const PlatformTouchEvent&);
175
176     bool useHandCursor(Node*, bool isOverLink, bool shiftKey);
177
178     void notifyElementActivated();
179
180 private:
181     static DragState& dragState();
182
183     PassRefPtr<Clipboard> createDraggingClipboard() const;
184
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&);
191
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&);
199
200     bool handlePasteGlobalSelection(const PlatformMouseEvent&);
201
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();
209
210     bool shouldApplyTouchAdjustment(const PlatformGestureEvent&) const;
211
212     OptionalCursor selectCursor(const HitTestResult&, bool shiftKey);
213     OptionalCursor selectAutoCursor(const HitTestResult&, Node*, const Cursor& iBeam, bool shiftKey);
214
215     void hoverTimerFired(Timer<EventHandler>*);
216     void cursorUpdateTimerFired(Timer<EventHandler>*);
217     void activeIntervalTimerFired(Timer<EventHandler>*);
218
219     bool shouldTurnVerticalTicksIntoHorizontal(const HitTestResult&, const PlatformWheelEvent&) const;
220     bool mouseDownMayStartSelect() const { return m_mouseDownMayStartSelect; }
221
222     void fakeMouseMoveEventTimerFired(Timer<EventHandler>*);
223     void cancelFakeMouseMoveEvent();
224     bool isCursorVisible() const;
225     void updateCursor();
226
227     bool isInsideScrollbar(const IntPoint&) const;
228
229     ScrollableArea* associatedScrollableArea(const RenderLayer*) const;
230
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());
243
244     bool dispatchSyntheticTouchEventIfEnabled(const PlatformMouseEvent&);
245
246     TouchAction intersectTouchAction(const TouchAction, const TouchAction);
247     TouchAction computeEffectiveTouchAction(const Node&);
248
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);
252
253     void invalidateClick();
254
255     Node* nodeUnderMouse() const;
256
257     void updateMouseEventTargetNode(Node*, const PlatformMouseEvent&, bool fireMouseOverOut);
258
259     MouseEventWithHitTestResults prepareMouseEvent(const HitTestRequest&, const PlatformMouseEvent&);
260
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*);
263
264     void freeClipboard();
265
266     bool handleDrag(const MouseEventWithHitTestResults&, CheckDragHysteresis);
267     bool tryStartDrag(const MouseEventWithHitTestResults&);
268     void clearDragState();
269
270     bool dispatchDragSrcEvent(const AtomicString& eventType, const PlatformMouseEvent&);
271
272     bool dragHysteresisExceeded(const FloatPoint&) const;
273     bool dragHysteresisExceeded(const IntPoint&) const;
274
275     bool passMousePressEventToSubframe(MouseEventWithHitTestResults&, Frame* subframe);
276     bool passMouseMoveEventToSubframe(MouseEventWithHitTestResults&, Frame* subframe, HitTestResult* hoveredNode = 0);
277     bool passMouseReleaseEventToSubframe(MouseEventWithHitTestResults&, Frame* subframe);
278
279     bool passMousePressEventToScrollbar(MouseEventWithHitTestResults&, Scrollbar*);
280
281     bool passWidgetMouseDownEventToWidget(const MouseEventWithHitTestResults&);
282
283     bool passWheelEventToWidget(const PlatformWheelEvent&, Widget*);
284
285     void defaultSpaceEventHandler(KeyboardEvent*);
286     void defaultBackspaceEventHandler(KeyboardEvent*);
287     void defaultTabEventHandler(KeyboardEvent*);
288     void defaultEscapeEventHandler(KeyboardEvent*);
289     void defaultArrowEventHandler(FocusDirection, KeyboardEvent*);
290
291     void updateSelectionForMouseDrag(const HitTestResult&);
292
293     void updateLastScrollbarUnderMouse(Scrollbar*, bool);
294
295     void setFrameWasScrolledByUser();
296
297     bool capturesDragging() const { return m_capturesDragging; }
298
299     bool isKeyEventAllowedInFullScreen(FullscreenElementStack*, const PlatformKeyboardEvent&) const;
300
301     bool handleGestureShowPress();
302
303     bool handleScrollGestureOnResizer(Node*, const PlatformGestureEvent&);
304
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&);
309
310     AutoscrollController* autoscrollController() const;
311     bool panScrollInProgress() const;
312     void setLastKnownMousePosition(const PlatformMouseEvent&);
313
314     Frame* const m_frame;
315
316     bool m_mousePressed;
317     bool m_capturesDragging;
318     RefPtr<Node> m_mousePressNode;
319
320     bool m_mouseDownMayStartSelect;
321     bool m_mouseDownMayStartDrag;
322     bool m_mouseDownWasSingleClickInSelection;
323     enum SelectionInitiationState { HaveNotStartedSelection, PlacedCaret, ExtendedSelection };
324     SelectionInitiationState m_selectionInitiationState;
325
326     LayoutPoint m_dragStartPos;
327
328     Timer<EventHandler> m_hoverTimer;
329     Timer<EventHandler> m_cursorUpdateTimer;
330
331     bool m_mouseDownMayStartAutoscroll;
332     bool m_mouseDownWasInSubframe;
333
334     Timer<EventHandler> m_fakeMouseMoveEventTimer;
335
336     bool m_svgPan;
337     RefPtr<SVGElementInstance> m_instanceUnderMouse;
338     RefPtr<SVGElementInstance> m_lastInstanceUnderMouse;
339
340     RenderLayerScrollableArea* m_resizeScrollableArea;
341
342     RefPtr<Node> m_capturingMouseEventsNode;
343     bool m_eventHandlerWillResetCapturingMouseEventsNode;
344
345     RefPtr<Node> m_nodeUnderMouse;
346     RefPtr<Node> m_lastNodeUnderMouse;
347     RefPtr<Frame> m_lastMouseMoveEventSubframe;
348     RefPtr<Scrollbar> m_lastScrollbarUnderMouse;
349     Cursor m_currentMouseCursor;
350
351     int m_clickCount;
352     RefPtr<Node> m_clickNode;
353
354     RefPtr<Node> m_dragTarget;
355     bool m_shouldOnlyFireDragOverEvent;
356
357     RefPtr<HTMLFrameSetElement> m_frameSetBeingResized;
358
359     LayoutSize m_offsetFromResizeCorner; // In the coords of m_resizeScrollableArea.
360
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;
368
369     RefPtr<Node> m_latchedWheelEventNode;
370     bool m_widgetIsLatched;
371
372     RefPtr<Node> m_previousWheelScrolledNode;
373
374     typedef HashMap<int, RefPtr<EventTarget> > TouchTargetMap;
375     TouchTargetMap m_originatingTouchPointTargets;
376     RefPtr<Document> m_originatingTouchPointDocument;
377     unsigned m_originatingTouchPointTargetKey;
378     bool m_touchPressed;
379
380     RefPtr<Node> m_scrollGestureHandlingNode;
381     bool m_lastHitTestResultOverWidget;
382     RefPtr<Node> m_previousGestureScrolledNode;
383     RefPtr<Scrollbar> m_scrollbarHandlingScrollGesture;
384
385     double m_maxMouseMovedDuration;
386     PlatformEvent::Type m_baseEventType;
387     bool m_didStartDrag;
388
389     bool m_longTapShouldInvokeContextMenu;
390     OwnPtr<IntPoint> m_lastSyntheticPinchAnchorCss;
391     OwnPtr<IntPoint> m_lastSyntheticPinchAnchorDip;
392     OwnPtr<IntPoint> m_lastSyntheticPanLocation;
393     float m_syntheticPageScaleFactor;
394
395     Timer<EventHandler> m_activeIntervalTimer;
396     double m_lastShowPressTimestamp;
397     RefPtr<Element> m_lastDeferredTapElement;
398 };
399
400 } // namespace WebCore
401
402 #endif // EventHandler_h