Merge "[CherryPick] Refactoring: Move the content of HTMLInputElement::subtreeHasChan...
[framework/web/webkit-efl.git] / Source / WebCore / 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 "DragActions.h"
30 #include "DragState.h"
31 #include "FocusDirection.h"
32 #include "HitTestRequest.h"
33 #include "PlatformMouseEvent.h"
34 #include "PlatformWheelEvent.h"
35 #include "ScrollTypes.h"
36 #include "TextEventInputType.h"
37 #include "TextGranularity.h"
38 #include "Timer.h"
39 #include <wtf/Forward.h>
40 #include <wtf/OwnPtr.h>
41 #include <wtf/RefPtr.h>
42
43 #if PLATFORM(MAC) && !defined(__OBJC__)
44 class NSView;
45 #endif
46
47 #if ENABLE(TOUCH_EVENTS)
48 #include <wtf/HashMap.h>
49 #endif
50
51 namespace WebCore {
52
53 class Clipboard;
54 class Event;
55 class EventTarget;
56 class FloatPoint;
57 class FloatQuad;
58 class Frame;
59 class HTMLFrameSetElement;
60 class HitTestRequest;
61 class HitTestResult;
62 class KeyboardEvent;
63 class MouseEventWithHitTestResults;
64 class Node;
65 class OptionalCursor;
66 class PlatformKeyboardEvent;
67 class PlatformTouchEvent;
68 class PlatformWheelEvent;
69 class RenderLayer;
70 class RenderObject;
71 class RenderWidget;
72 class SVGElementInstance;
73 class Scrollbar;
74 class TextEvent;
75 class TouchEvent;
76 class VisibleSelection;
77 class WheelEvent;
78 class Widget;
79
80 #if ENABLE(GESTURE_EVENTS)
81 class PlatformGestureEvent;
82 #endif
83
84 #if ENABLE(DRAG_SUPPORT)
85 extern const int LinkDragHysteresis;
86 extern const int ImageDragHysteresis;
87 extern const int TextDragHysteresis;
88 extern const int GeneralDragHysteresis;
89 #endif // ENABLE(DRAG_SUPPORT)
90
91 enum HitTestScrollbars { ShouldHitTestScrollbars, DontHitTestScrollbars };
92
93 class EventHandler {
94     WTF_MAKE_NONCOPYABLE(EventHandler);
95 public:
96     explicit EventHandler(Frame*);
97     ~EventHandler();
98
99     void clear();
100     void nodeWillBeRemoved(Node*);
101
102 #if ENABLE(DRAG_SUPPORT)
103     void updateSelectionForMouseDrag();
104 #endif
105
106     Node* mousePressNode() const;
107     void setMousePressNode(PassRefPtr<Node>);
108
109     void startPanScrolling(RenderObject*);
110
111     void stopAutoscrollTimer(bool rendererIsBeingDestroyed = false);
112     RenderObject* autoscrollRenderer() const;
113     void updateAutoscrollRenderer();
114
115     void dispatchFakeMouseMoveEventSoon();
116     void dispatchFakeMouseMoveEventSoonInQuad(const FloatQuad&);
117
118     HitTestResult hitTestResultAtPoint(const LayoutPoint&, bool allowShadowContent, bool ignoreClipping = false,
119                                        HitTestScrollbars scrollbars = DontHitTestScrollbars,
120                                        HitTestRequest::HitTestRequestType hitType = HitTestRequest::ReadOnly | HitTestRequest::Active,
121                                        const LayoutSize& padding = LayoutSize());
122
123     bool mousePressed() const { return m_mousePressed; }
124     void setMousePressed(bool pressed) { m_mousePressed = pressed; }
125
126     void setCapturingMouseEventsNode(PassRefPtr<Node>); // A caller is responsible for resetting capturing node to 0.
127
128 #if ENABLE(DRAG_SUPPORT)
129     bool updateDragAndDrop(const PlatformMouseEvent&, Clipboard*);
130     void cancelDragAndDrop(const PlatformMouseEvent&, Clipboard*);
131     bool performDragAndDrop(const PlatformMouseEvent&, Clipboard*);
132     void updateDragStateAfterEditDragIfNeeded(Element* rootEditableElement);
133 #endif
134
135     void scheduleHoverStateUpdate();
136
137     void setResizingFrameSet(HTMLFrameSetElement*);
138
139     void resizeLayerDestroyed();
140
141     IntPoint currentMousePosition() const;
142
143     static Frame* subframeForTargetNode(Node*);
144     static Frame* subframeForHitTestResult(const MouseEventWithHitTestResults&);
145
146     bool scrollOverflow(ScrollDirection, ScrollGranularity, Node* startingNode = 0);
147 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
148     bool scrollOverflow(const FloatPoint&);
149     bool setMousePressNodeAtPoint(const IntPoint&, bool checkOverflowLayer, RenderObject*&);
150 #endif
151     bool scrollRecursively(ScrollDirection, ScrollGranularity, Node* startingNode = 0);
152     bool logicalScrollRecursively(ScrollLogicalDirection, ScrollGranularity, Node* startingNode = 0);
153
154     bool tabsToLinks(KeyboardEvent*) const;
155     bool tabsToAllFormControls(KeyboardEvent*) const;
156
157     bool mouseMoved(const PlatformMouseEvent&);
158     bool passMouseMovedEventToScrollbars(const PlatformMouseEvent&);
159
160     void lostMouseCapture();
161
162     bool handleMousePressEvent(const PlatformMouseEvent&);
163     bool handleMouseMoveEvent(const PlatformMouseEvent&, HitTestResult* hoveredNode = 0, bool onlyUpdateScrollbars = false);
164     bool handleMouseReleaseEvent(const PlatformMouseEvent&);
165     bool handleWheelEvent(const PlatformWheelEvent&);
166     void defaultWheelEventHandler(Node*, WheelEvent*);
167
168 #if ENABLE(GESTURE_EVENTS)
169     bool handleGestureEvent(const PlatformGestureEvent&);
170     bool handleGestureTap(const PlatformGestureEvent&);
171     bool handleGestureScrollUpdate(const PlatformGestureEvent&);
172 #endif
173
174 #if ENABLE(TOUCH_ADJUSTMENT)
175     bool bestClickableNodeForTouchPoint(const IntPoint& touchCenter, const IntSize& touchRadius, IntPoint& targetPoint, Node*& targetNode);
176     bool bestContextMenuNodeForTouchPoint(const IntPoint& touchCenter, const IntSize& touchRadius, IntPoint& targetPoint, Node*& targetNode);
177     bool bestZoomableAreaForTouchPoint(const IntPoint& touchCenter, const IntSize& touchRadius, IntRect& targetArea, Node*& targetNode);
178
179     bool adjustGesturePosition(const PlatformGestureEvent&, IntPoint& adjustedPoint);
180 #endif
181
182 #if ENABLE(CONTEXT_MENUS)
183     bool sendContextMenuEvent(const PlatformMouseEvent&);
184     bool sendContextMenuEventForKey();
185 #if ENABLE(GESTURE_EVENTS)
186     bool sendContextMenuEventForGesture(const PlatformGestureEvent&);
187 #endif
188 #endif
189
190     void setMouseDownMayStartAutoscroll() { m_mouseDownMayStartAutoscroll = true; }
191
192     bool needsKeyboardEventDisambiguationQuirks() const;
193
194     static unsigned accessKeyModifiers();
195     bool handleAccessKey(const PlatformKeyboardEvent&);
196     bool keyEvent(const PlatformKeyboardEvent&);
197     void defaultKeyboardEventHandler(KeyboardEvent*);
198
199     bool handleTextInputEvent(const String& text, Event* underlyingEvent = 0, TextEventInputType = TextEventInputKeyboard);
200     void defaultTextInputEventHandler(TextEvent*);
201
202 #if ENABLE(DRAG_SUPPORT)
203     bool eventMayStartDrag(const PlatformMouseEvent&) const;
204     
205     void dragSourceEndedAt(const PlatformMouseEvent&, DragOperation);
206 #endif
207
208     void focusDocumentView();
209
210     void capsLockStateMayHaveChanged(); // Only called by FrameSelection
211     
212     void sendResizeEvent(); // Only called in FrameView
213     void sendScrollEvent(); // Ditto
214
215 #if PLATFORM(MAC) && defined(__OBJC__)
216     void mouseDown(NSEvent *);
217     void mouseDragged(NSEvent *);
218     void mouseUp(NSEvent *);
219     void mouseMoved(NSEvent *);
220     bool keyEvent(NSEvent *);
221     bool wheelEvent(NSEvent *);
222
223     void passMouseMovedEventToScrollbars(NSEvent *);
224
225     void sendFakeEventsAfterWidgetTracking(NSEvent *initiatingEvent);
226
227     void setActivationEventNumber(int num) { m_activationEventNumber = num; }
228
229     static NSEvent *currentNSEvent();
230 #endif
231
232 #if ENABLE(TOUCH_EVENTS)
233     bool handleTouchEvent(const PlatformTouchEvent&);
234 #endif
235
236 private:
237 #if ENABLE(DRAG_SUPPORT)
238     static DragState& dragState();
239     static const double TextDragDelay;
240     
241     PassRefPtr<Clipboard> createDraggingClipboard() const;
242 #endif // ENABLE(DRAG_SUPPORT)
243
244     bool eventActivatedView(const PlatformMouseEvent&) const;
245     bool updateSelectionForMouseDownDispatchingSelectStart(Node*, const VisibleSelection&, TextGranularity);
246     void selectClosestWordFromMouseEvent(const MouseEventWithHitTestResults&);
247     void selectClosestWordOrLinkFromMouseEvent(const MouseEventWithHitTestResults&);
248
249     bool handleMouseDoubleClickEvent(const PlatformMouseEvent&);
250
251     static Node* targetNode(const MouseEventWithHitTestResults&);
252     static Node* targetNode(const HitTestResult&);
253
254     bool handleMousePressEvent(const MouseEventWithHitTestResults&);
255     bool handleMousePressEventSingleClick(const MouseEventWithHitTestResults&);
256     bool handleMousePressEventDoubleClick(const MouseEventWithHitTestResults&);
257     bool handleMousePressEventTripleClick(const MouseEventWithHitTestResults&);
258 #if ENABLE(DRAG_SUPPORT)
259     bool handleMouseDraggedEvent(const MouseEventWithHitTestResults&);
260 #endif
261     bool handleMouseReleaseEvent(const MouseEventWithHitTestResults&);
262
263     OptionalCursor selectCursor(const MouseEventWithHitTestResults&, Scrollbar*);
264 #if ENABLE(PAN_SCROLLING)
265     void updatePanScrollState();
266 #endif
267
268     void hoverTimerFired(Timer<EventHandler>*);
269
270     void handleAutoscroll(RenderObject*);
271     void startAutoscrollTimer();
272     void setAutoscrollRenderer(RenderObject*);
273     void autoscrollTimerFired(Timer<EventHandler>*);
274     bool logicalScrollOverflow(ScrollLogicalDirection, ScrollGranularity, Node* startingNode = 0);
275     
276     bool shouldTurnVerticalTicksIntoHorizontal(const HitTestResult&, const PlatformWheelEvent&) const;
277     bool mouseDownMayStartSelect() const { return m_mouseDownMayStartSelect; }
278
279     static bool isKeyboardOptionTab(KeyboardEvent*);
280     static bool eventInvertsTabsToLinksClientCallResult(KeyboardEvent*);
281
282     void fakeMouseMoveEventTimerFired(Timer<EventHandler>*);
283     void cancelFakeMouseMoveEvent();
284
285 #if ENABLE(TOUCH_EVENTS)
286     bool dispatchSyntheticTouchEventIfEnabled(const PlatformMouseEvent&);
287 #endif
288
289     void invalidateClick();
290
291     Node* nodeUnderMouse() const;
292     
293     void updateMouseEventTargetNode(Node*, const PlatformMouseEvent&, bool fireMouseOverOut);
294     void fireMouseOverOut(bool fireMouseOver = true, bool fireMouseOut = true, bool updateLastNodeUnderMouse = true);
295     
296     MouseEventWithHitTestResults prepareMouseEvent(const HitTestRequest&, const PlatformMouseEvent&);
297
298     bool dispatchMouseEvent(const AtomicString& eventType, Node* target, bool cancelable, int clickCount, const PlatformMouseEvent&, bool setUnder);
299 #if ENABLE(DRAG_SUPPORT)
300     bool dispatchDragEvent(const AtomicString& eventType, Node* target, const PlatformMouseEvent&, Clipboard*);
301
302     void freeClipboard();
303
304     bool handleDrag(const MouseEventWithHitTestResults&);
305 #endif
306     bool handleMouseUp(const MouseEventWithHitTestResults&);
307 #if ENABLE(DRAG_SUPPORT)
308     void clearDragState();
309
310     bool dispatchDragSrcEvent(const AtomicString& eventType, const PlatformMouseEvent&);
311
312     bool dragHysteresisExceeded(const FloatPoint&) const;
313     bool dragHysteresisExceeded(const IntPoint&) const;
314 #endif // ENABLE(DRAG_SUPPORT)
315
316     bool passMousePressEventToSubframe(MouseEventWithHitTestResults&, Frame* subframe);
317     bool passMouseMoveEventToSubframe(MouseEventWithHitTestResults&, Frame* subframe, HitTestResult* hoveredNode = 0);
318     bool passMouseReleaseEventToSubframe(MouseEventWithHitTestResults&, Frame* subframe);
319
320     bool passSubframeEventToSubframe(MouseEventWithHitTestResults&, Frame* subframe, HitTestResult* hoveredNode = 0);
321
322     bool passMousePressEventToScrollbar(MouseEventWithHitTestResults&, Scrollbar*);
323
324     bool passWidgetMouseDownEventToWidget(const MouseEventWithHitTestResults&);
325     bool passWidgetMouseDownEventToWidget(RenderWidget*);
326
327     bool passMouseDownEventToWidget(Widget*);
328     bool passWheelEventToWidget(const PlatformWheelEvent&, Widget*);
329
330     void defaultSpaceEventHandler(KeyboardEvent*);
331     void defaultBackspaceEventHandler(KeyboardEvent*);
332     void defaultTabEventHandler(KeyboardEvent*);
333     void defaultArrowEventHandler(FocusDirection, KeyboardEvent*);
334
335 #if ENABLE(DRAG_SUPPORT)
336     DragSourceAction updateDragSourceActionsAllowed() const;
337 #endif
338
339     // The following are called at the beginning of handleMouseUp and handleDrag.  
340     // If they return true it indicates that they have consumed the event.
341     bool eventLoopHandleMouseUp(const MouseEventWithHitTestResults&);
342 #if ENABLE(DRAG_SUPPORT)
343     bool eventLoopHandleMouseDragged(const MouseEventWithHitTestResults&);
344 #endif
345
346 #if ENABLE(DRAG_SUPPORT)
347     void updateSelectionForMouseDrag(const HitTestResult&);
348 #endif
349
350     void updateLastScrollbarUnderMouse(Scrollbar*, bool);
351     
352     void setFrameWasScrolledByUser();
353
354     bool capturesDragging() const { return m_capturesDragging; }
355
356 #if PLATFORM(MAC) && defined(__OBJC__)
357     NSView *mouseDownViewIfStillGood();
358
359     PlatformMouseEvent currentPlatformMouseEvent() const;
360 #endif
361
362 #if ENABLE(FULLSCREEN_API)
363     bool isKeyEventAllowedInFullScreen(const PlatformKeyboardEvent&) const;
364 #endif
365
366 #if ENABLE(GESTURE_EVENTS)
367     bool handleGestureScrollCore(const PlatformGestureEvent&, PlatformWheelEventGranularity, bool latchedWheel);
368     bool handleGestureTapDown();
369 #endif
370
371     Frame* m_frame;
372
373     bool m_mousePressed;
374     bool m_capturesDragging;
375     RefPtr<Node> m_mousePressNode;
376
377     bool m_mouseDownMayStartSelect;
378 #if ENABLE(DRAG_SUPPORT)
379     bool m_mouseDownMayStartDrag;
380     bool m_dragMayStartSelectionInstead;
381 #endif
382     bool m_mouseDownWasSingleClickInSelection;
383     enum SelectionInitiationState { HaveNotStartedSelection, PlacedCaret, ExtendedSelection };
384     SelectionInitiationState m_selectionInitiationState;
385
386 #if ENABLE(DRAG_SUPPORT)
387     LayoutPoint m_dragStartPos;
388 #endif
389
390     IntPoint m_panScrollStartPos;
391     bool m_panScrollInProgress;
392
393     bool m_panScrollButtonPressed;
394     bool m_springLoadedPanScrollInProgress;
395
396     Timer<EventHandler> m_hoverTimer;
397     
398     Timer<EventHandler> m_autoscrollTimer;
399     RenderObject* m_autoscrollRenderer;
400     bool m_autoscrollInProgress;
401     bool m_mouseDownMayStartAutoscroll;
402     bool m_mouseDownWasInSubframe;
403
404     Timer<EventHandler> m_fakeMouseMoveEventTimer;
405
406 #if ENABLE(SVG)
407     bool m_svgPan;
408     RefPtr<SVGElementInstance> m_instanceUnderMouse;
409     RefPtr<SVGElementInstance> m_lastInstanceUnderMouse;
410 #endif
411
412     RenderLayer* m_resizeLayer;
413
414     RefPtr<Node> m_capturingMouseEventsNode;
415     bool m_eventHandlerWillResetCapturingMouseEventsNode;
416     
417     RefPtr<Node> m_nodeUnderMouse;
418     RefPtr<Node> m_lastNodeUnderMouse;
419     RefPtr<Frame> m_lastMouseMoveEventSubframe;
420     RefPtr<Scrollbar> m_lastScrollbarUnderMouse;
421
422     int m_clickCount;
423     RefPtr<Node> m_clickNode;
424
425 #if ENABLE(DRAG_SUPPORT)
426     RefPtr<Node> m_dragTarget;
427     bool m_shouldOnlyFireDragOverEvent;
428 #endif
429     
430     RefPtr<HTMLFrameSetElement> m_frameSetBeingResized;
431
432     LayoutSize m_offsetFromResizeCorner; // In the coords of m_resizeLayer.
433     
434     IntPoint m_currentMousePosition;
435     IntPoint m_currentMouseGlobalPosition;
436     IntPoint m_mouseDownPos; // In our view's coords.
437     double m_mouseDownTimestamp;
438     PlatformMouseEvent m_mouseDown;
439
440     bool m_useLatchedWheelEventNode;
441     RefPtr<Node> m_latchedWheelEventNode;
442     bool m_widgetIsLatched;
443
444     RefPtr<Node> m_previousWheelScrolledNode;
445
446 #if PLATFORM(MAC)
447     NSView *m_mouseDownView;
448     bool m_sendingEventToSubview;
449     int m_activationEventNumber;
450 #endif
451 #if ENABLE(TOUCH_EVENTS)
452     typedef HashMap<int, RefPtr<EventTarget> > TouchTargetMap;
453     TouchTargetMap m_originatingTouchPointTargets;
454     bool m_touchPressed;
455 #endif
456     double m_maxMouseMovedDuration;
457     PlatformEvent::Type m_baseEventType;
458 };
459
460 } // namespace WebCore
461
462 #endif // EventHandler_h