2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "EventHandler.h"
30 #include "CachedImage.h"
31 #include "ChromeClient.h"
34 #include "DragController.h"
36 #include "EventNames.h"
37 #include "FloatPoint.h"
38 #include "FocusController.h"
40 #include "FrameLoader.h"
41 #include "FrameTree.h"
42 #include "FrameView.h"
43 #include "HitTestRequest.h"
44 #include "HitTestResult.h"
45 #include "HTMLFrameSetElement.h"
46 #include "HTMLFrameElementBase.h"
47 #include "HTMLInputElement.h"
48 #include "HTMLNames.h"
50 #include "KeyboardEvent.h"
51 #include "MouseEvent.h"
52 #include "MouseEventWithHitTestResults.h"
54 #include "PlatformKeyboardEvent.h"
55 #include "PlatformScrollBar.h"
56 #include "PlatformWheelEvent.h"
57 #include "RenderWidget.h"
58 #include "SelectionController.h"
60 #include "TextEvent.h"
63 #include "SVGCursorElement.h"
64 #include "SVGDocument.h"
65 #include "SVGLength.h"
71 using namespace EventNames;
72 using namespace HTMLNames;
74 // The link drag hysteresis is much larger than the others because there
75 // needs to be enough space to cancel the link press without starting a link drag,
76 // and because dragging links is rare.
77 const int LinkDragHysteresis = 40;
78 const int ImageDragHysteresis = 5;
79 const int TextDragHysteresis = 3;
80 const int GeneralDragHysteresis = 3;
81 const double TextDragDelay = 0.15;
83 // Match key code of composition keydown event on windows.
84 // IE sends VK_PROCESSKEY which has value 229;
85 const int CompositionEventKeyCode = 229;
88 using namespace SVGNames;
91 const double autoscrollInterval = 0.1;
93 static Frame* subframeForTargetNode(Node* node);
95 EventHandler::EventHandler(Frame* frame)
97 , m_mousePressed(false)
98 , m_mouseDownMayStartSelect(false)
99 , m_mouseDownMayStartDrag(false)
100 , m_mouseDownWasSingleClickInSelection(false)
101 , m_beganSelectingText(false)
102 , m_hoverTimer(this, &EventHandler::hoverTimerFired)
103 , m_autoscrollTimer(this, &EventHandler::autoscrollTimerFired)
104 , m_autoscrollRenderer(0)
105 , m_mouseDownMayStartAutoscroll(false)
106 , m_mouseDownWasInSubframe(false)
111 , m_capturingMouseEventsNode(0)
113 , m_mouseDownTimestamp(0)
115 , m_mouseDownView(nil)
116 , m_sendingEventToSubview(false)
117 , m_activationEventNumber(0)
122 EventHandler::~EventHandler()
126 EventHandler::EventHandlerDragState& EventHandler::dragState()
128 static EventHandlerDragState state;
132 void EventHandler::clear()
136 m_nodeUnderMouse = 0;
137 m_lastNodeUnderMouse = 0;
138 m_lastMouseMoveEventSubframe = 0;
139 m_lastScrollbarUnderMouse = 0;
142 m_frameSetBeingResized = 0;
144 m_currentMousePosition = IntPoint();
145 m_mousePressNode = 0;
146 m_mousePressed = false;
147 m_capturingMouseEventsNode = 0;
150 void EventHandler::selectClosestWordFromMouseEvent(const MouseEventWithHitTestResults& result)
152 Node* innerNode = result.targetNode();
153 Selection newSelection;
155 if (innerNode && innerNode->renderer() && m_mouseDownMayStartSelect) {
156 VisiblePosition pos(innerNode->renderer()->positionForPoint(result.localPoint()));
157 if (pos.isNotNull()) {
158 newSelection = Selection(pos);
159 newSelection.expandUsingGranularity(WordGranularity);
162 if (newSelection.isRange()) {
163 m_frame->setSelectionGranularity(WordGranularity);
164 m_beganSelectingText = true;
167 if (m_frame->shouldChangeSelection(newSelection))
168 m_frame->selectionController()->setSelection(newSelection);
172 void EventHandler::selectClosestWordOrLinkFromMouseEvent(const MouseEventWithHitTestResults& result)
174 if (!result.hitTestResult().isLiveLink())
175 return selectClosestWordFromMouseEvent(result);
177 Node* innerNode = result.targetNode();
179 if (innerNode && innerNode->renderer() && m_mouseDownMayStartSelect) {
180 Selection newSelection;
181 Element* URLElement = result.hitTestResult().URLElement();
182 VisiblePosition pos(innerNode->renderer()->positionForPoint(result.localPoint()));
183 if (pos.isNotNull() && pos.deepEquivalent().node()->isDescendantOf(URLElement))
184 newSelection = Selection::selectionFromContentsOfNode(URLElement);
186 if (newSelection.isRange()) {
187 m_frame->setSelectionGranularity(WordGranularity);
188 m_beganSelectingText = true;
191 if (m_frame->shouldChangeSelection(newSelection))
192 m_frame->selectionController()->setSelection(newSelection);
196 bool EventHandler::handleMousePressEventDoubleClick(const MouseEventWithHitTestResults& event)
198 if (event.event().button() != LeftButton)
201 if (m_frame->selectionController()->isRange())
202 // A double-click when range is already selected
203 // should not change the selection. So, do not call
204 // selectClosestWordFromMouseEvent, but do set
205 // m_beganSelectingText to prevent handleMouseReleaseEvent
206 // from setting caret selection.
207 m_beganSelectingText = true;
209 selectClosestWordFromMouseEvent(event);
214 bool EventHandler::handleMousePressEventTripleClick(const MouseEventWithHitTestResults& event)
216 if (event.event().button() != LeftButton)
219 Node* innerNode = event.targetNode();
220 if (!(innerNode && innerNode->renderer() && m_mouseDownMayStartSelect))
223 Selection newSelection;
224 VisiblePosition pos(innerNode->renderer()->positionForPoint(event.localPoint()));
225 if (pos.isNotNull()) {
226 newSelection = Selection(pos);
227 newSelection.expandUsingGranularity(ParagraphGranularity);
229 if (newSelection.isRange()) {
230 m_frame->setSelectionGranularity(ParagraphGranularity);
231 m_beganSelectingText = true;
234 if (m_frame->shouldChangeSelection(newSelection))
235 m_frame->selectionController()->setSelection(newSelection);
240 bool EventHandler::handleMousePressEventSingleClick(const MouseEventWithHitTestResults& event)
242 if (event.event().button() != LeftButton)
245 Node* innerNode = event.targetNode();
246 if (!(innerNode && innerNode->renderer() && m_mouseDownMayStartSelect))
249 // Extend the selection if the Shift key is down, unless the click is in a link.
250 bool extendSelection = event.event().shiftKey() && !event.isOverLink();
252 // Don't restart the selection when the mouse is pressed on an
253 // existing selection so we can allow for text dragging.
254 IntPoint vPoint = m_frame->view()->windowToContents(event.event().pos());
255 if (!extendSelection && m_frame->selectionController()->contains(vPoint)) {
256 m_mouseDownWasSingleClickInSelection = true;
260 VisiblePosition visiblePos(innerNode->renderer()->positionForPoint(event.localPoint()));
261 if (visiblePos.isNull())
262 visiblePos = VisiblePosition(innerNode, 0, DOWNSTREAM);
263 Position pos = visiblePos.deepEquivalent();
265 Selection newSelection = m_frame->selectionController()->selection();
266 if (extendSelection && newSelection.isCaretOrRange()) {
267 m_frame->selectionController()->setLastChangeWasHorizontalExtension(false);
269 // See <rdar://problem/3668157> REGRESSION (Mail): shift-click deselects when selection
270 // was created right-to-left
271 Position start = newSelection.start();
272 Position end = newSelection.end();
273 short before = Range::compareBoundaryPoints(pos.node(), pos.offset(), start.node(), start.offset());
275 newSelection = Selection(pos, end);
277 newSelection = Selection(start, pos);
279 if (m_frame->selectionGranularity() != CharacterGranularity)
280 newSelection.expandUsingGranularity(m_frame->selectionGranularity());
281 m_beganSelectingText = true;
283 newSelection = Selection(visiblePos);
284 m_frame->setSelectionGranularity(CharacterGranularity);
287 if (m_frame->shouldChangeSelection(newSelection))
288 m_frame->selectionController()->setSelection(newSelection);
293 bool EventHandler::handleMousePressEvent(const MouseEventWithHitTestResults& event)
295 bool singleClick = event.event().clickCount() <= 1;
297 // If we got the event back, that must mean it wasn't prevented,
298 // so it's allowed to start a drag or selection.
299 m_mouseDownMayStartSelect = canMouseDownStartSelect(event.targetNode());
301 // Careful that the drag starting logic stays in sync with eventMayStartDrag()
302 m_mouseDownMayStartDrag = singleClick;
304 m_mouseDownWasSingleClickInSelection = false;
306 if (passWidgetMouseDownEventToWidget(event))
310 if (m_frame->document()->isSVGDocument() &&
311 static_cast<SVGDocument*>(m_frame->document())->zoomAndPanEnabled()) {
312 if (event.event().shiftKey() && singleClick) {
314 static_cast<SVGDocument*>(m_frame->document())->startPan(event.event().pos());
320 // We don't do this at the start of mouse down handling,
321 // because we don't want to do it until we know we didn't hit a widget.
325 Node* innerNode = event.targetNode();
327 m_mousePressNode = innerNode;
328 m_dragStartPos = event.event().pos();
330 bool swallowEvent = false;
331 if (event.event().button() == LeftButton || event.event().button() == MiddleButton) {
332 m_frame->selectionController()->setCaretBlinkingSuspended(true);
333 m_mousePressed = true;
334 m_beganSelectingText = false;
336 if (event.event().clickCount() == 2)
337 swallowEvent = handleMousePressEventDoubleClick(event);
338 else if (event.event().clickCount() >= 3)
339 swallowEvent = handleMousePressEventTripleClick(event);
341 swallowEvent = handleMousePressEventSingleClick(event);
344 m_mouseDownMayStartAutoscroll = m_mouseDownMayStartSelect ||
345 (m_mousePressNode && m_mousePressNode->renderer() && m_mousePressNode->renderer()->shouldAutoscroll());
350 bool EventHandler::handleMouseDraggedEvent(const MouseEventWithHitTestResults& event)
352 if (handleDrag(event))
358 Node* targetNode = event.targetNode();
359 if (event.event().button() != LeftButton || !targetNode || !targetNode->renderer())
362 #if PLATFORM(MAC) // FIXME: Why does this assertion fire on other platforms?
363 ASSERT(m_mouseDownMayStartSelect || m_mouseDownMayStartAutoscroll);
366 m_mouseDownMayStartDrag = false;
368 if (m_mouseDownMayStartAutoscroll) {
369 // If the selection is contained in a layer that can scroll, that layer should handle the autoscroll
370 // Otherwise, let the bridge handle it so the view can scroll itself.
371 RenderObject* renderer = targetNode->renderer();
372 while (renderer && !renderer->shouldAutoscroll())
373 renderer = renderer->parent();
375 handleAutoscroll(renderer);
378 updateSelectionForMouseDrag(targetNode, event.localPoint());
382 bool EventHandler::eventMayStartDrag(const PlatformMouseEvent& event) const
384 // This is a pre-flight check of whether the event might lead to a drag being started. Be careful
385 // that its logic needs to stay in sync with handleMouseMoveEvent() and the way we setMouseDownMayStartDrag
386 // in handleMousePressEvent
388 if (!m_frame->renderer() || !m_frame->renderer()->hasLayer()
389 || event.button() != LeftButton || event.clickCount() != 1)
394 allowDHTMLDrag(DHTMLFlag, UAFlag);
395 if (!DHTMLFlag && !UAFlag)
398 HitTestRequest request(true, false);
399 HitTestResult result(m_frame->view()->windowToContents(event.pos()));
400 m_frame->renderer()->layer()->hitTest(request, result);
402 return result.innerNode() && result.innerNode()->renderer()->draggableNode(DHTMLFlag, UAFlag, result.point().x(), result.point().y(), srcIsDHTML);
405 void EventHandler::updateSelectionForMouseDrag()
407 FrameView* view = m_frame->view();
410 RenderObject* renderer = m_frame->renderer();
413 RenderLayer* layer = renderer->layer();
417 HitTestResult result(view->windowToContents(m_currentMousePosition));
418 layer->hitTest(HitTestRequest(true, true, true), result);
419 updateSelectionForMouseDrag(result.innerNode(), result.localPoint());
422 void EventHandler::updateSelectionForMouseDrag(Node* targetNode, const IntPoint& localPoint)
424 if (!m_mouseDownMayStartSelect)
430 RenderObject* targetRenderer = targetNode->renderer();
434 if (!canMouseDragExtendSelect(targetNode))
437 VisiblePosition targetPosition(targetRenderer->positionForPoint(localPoint));
439 // Don't modify the selection if we're not on a node.
440 if (targetPosition.isNull())
443 // Restart the selection if this is the first mouse move. This work is usually
444 // done in handleMousePressEvent, but not if the mouse press was on an existing selection.
445 Selection newSelection = m_frame->selectionController()->selection();
448 // Special case to limit selection to the containing block for SVG text.
449 // FIXME: Isn't there a better non-SVG-specific way to do this?
450 if (Node* selectionBaseNode = newSelection.base().node())
451 if (RenderObject* selectionBaseRenderer = selectionBaseNode->renderer())
452 if (selectionBaseRenderer->isSVGText())
453 if (targetNode->renderer()->containingBlock() != selectionBaseRenderer->containingBlock())
457 if (!m_beganSelectingText) {
458 m_beganSelectingText = true;
459 newSelection = Selection(targetPosition);
462 newSelection.setExtent(targetPosition);
463 if (m_frame->selectionGranularity() != CharacterGranularity)
464 newSelection.expandUsingGranularity(m_frame->selectionGranularity());
466 if (m_frame->shouldChangeSelection(newSelection)) {
467 m_frame->selectionController()->setLastChangeWasHorizontalExtension(false);
468 m_frame->selectionController()->setSelection(newSelection);
472 bool EventHandler::handleMouseUp(const MouseEventWithHitTestResults& event)
474 if (eventLoopHandleMouseUp(event))
477 // If this was the first click in the window, we don't even want to clear the selection.
478 // This case occurs when the user clicks on a draggable element, since we have to process
479 // the mouse down and drag events to see if we might start a drag. For other first clicks
480 // in a window, we just don't acceptFirstMouse, and the whole down-drag-up sequence gets
481 // ignored upstream of this layer.
482 return eventActivatedView(event.event());
485 bool EventHandler::handleMouseReleaseEvent(const MouseEventWithHitTestResults& event)
487 stopAutoscrollTimer();
489 if (handleMouseUp(event))
492 // Used to prevent mouseMoveEvent from initiating a drag before
493 // the mouse is pressed again.
494 m_frame->selectionController()->setCaretBlinkingSuspended(false);
495 m_mousePressed = false;
496 m_mouseDownMayStartDrag = false;
497 m_mouseDownMayStartSelect = false;
498 m_mouseDownMayStartAutoscroll = false;
499 m_mouseDownWasInSubframe = false;
501 bool handled = false;
503 // Clear the selection if the mouse didn't move after the last mouse press.
504 // We do this so when clicking on the selection, the selection goes away.
505 // However, if we are editing, place the caret.
506 if (m_mouseDownWasSingleClickInSelection && !m_beganSelectingText
507 && m_dragStartPos == event.event().pos()
508 && m_frame->selectionController()->isRange()) {
509 Selection newSelection;
510 Node *node = event.targetNode();
511 if (node && node->isContentEditable() && node->renderer()) {
512 VisiblePosition pos = node->renderer()->positionForPoint(event.localPoint());
513 newSelection = Selection(pos);
515 if (m_frame->shouldChangeSelection(newSelection))
516 m_frame->selectionController()->setSelection(newSelection);
521 m_frame->notifyRendererOfSelectionChange(true);
523 m_frame->selectionController()->selectFrameElementInParentIfFullySelected();
528 void EventHandler::handleAutoscroll(RenderObject* renderer)
530 if (m_autoscrollTimer.isActive())
532 setAutoscrollRenderer(renderer);
533 startAutoscrollTimer();
536 void EventHandler::autoscrollTimerFired(Timer<EventHandler>*)
538 if (!m_mousePressed) {
539 stopAutoscrollTimer();
542 if (RenderObject* r = autoscrollRenderer())
546 RenderObject* EventHandler::autoscrollRenderer() const
548 return m_autoscrollRenderer;
551 void EventHandler::setAutoscrollRenderer(RenderObject* renderer)
553 m_autoscrollRenderer = renderer;
556 void EventHandler::allowDHTMLDrag(bool& flagDHTML, bool& flagUA) const
558 if (!m_frame || !m_frame->document()) {
563 unsigned mask = m_frame->page()->dragController()->delegateDragSourceAction(m_frame->view()->contentsToWindow(m_mouseDownPos));
564 flagDHTML = (mask & DragSourceActionDHTML) != DragSourceActionNone;
565 flagUA = ((mask & DragSourceActionImage) || (mask & DragSourceActionLink) || (mask & DragSourceActionSelection));
568 HitTestResult EventHandler::hitTestResultAtPoint(const IntPoint& point, bool allowShadowContent)
570 HitTestResult result(point);
571 if (!m_frame->renderer())
573 m_frame->renderer()->layer()->hitTest(HitTestRequest(true, true), result);
575 IntPoint widgetPoint(point);
577 Node* n = result.innerNode();
578 if (!n || !n->renderer() || !n->renderer()->isWidget())
580 Widget* widget = static_cast<RenderWidget*>(n->renderer())->widget();
581 if (!widget || !widget->isFrameView())
583 Frame* frame = static_cast<HTMLFrameElementBase*>(n)->contentFrame();
584 if (!frame || !frame->renderer())
587 n->renderer()->absolutePosition(absX, absY, true);
588 FrameView* view = static_cast<FrameView*>(widget);
589 widgetPoint.move(view->contentsX() - absX, view->contentsY() - absY);
590 HitTestResult widgetHitTestResult(widgetPoint);
591 frame->renderer()->layer()->hitTest(HitTestRequest(true, true), widgetHitTestResult);
592 result = widgetHitTestResult;
595 if (!allowShadowContent)
596 result.setToNonShadowAncestor();
602 void EventHandler::startAutoscrollTimer()
604 m_autoscrollTimer.startRepeating(autoscrollInterval);
607 void EventHandler::stopAutoscrollTimer(bool rendererIsBeingDestroyed)
609 if (m_mouseDownWasInSubframe) {
610 if (Frame* subframe = subframeForTargetNode(m_mousePressNode.get()))
611 subframe->eventHandler()->stopAutoscrollTimer(rendererIsBeingDestroyed);
615 if (!rendererIsBeingDestroyed && autoscrollRenderer())
616 autoscrollRenderer()->stopAutoscroll();
617 setAutoscrollRenderer(0);
618 m_autoscrollTimer.stop();
621 Node* EventHandler::mousePressNode() const
623 return m_mousePressNode.get();
626 void EventHandler::setMousePressNode(PassRefPtr<Node> node)
628 m_mousePressNode = node;
631 bool EventHandler::scrollOverflow(ScrollDirection direction, ScrollGranularity granularity)
633 if (!m_frame->document())
636 Node* node = m_frame->document()->focusedNode();
638 node = m_mousePressNode.get();
641 RenderObject *r = node->renderer();
642 if (r && !r->isListBox())
643 return r->scroll(direction, granularity);
649 IntPoint EventHandler::currentMousePosition() const
651 return m_currentMousePosition;
654 Frame* subframeForTargetNode(Node* node)
659 RenderObject* renderer = node->renderer();
660 if (!renderer || !renderer->isWidget())
663 Widget* widget = static_cast<RenderWidget*>(renderer)->widget();
664 if (!widget || !widget->isFrameView())
667 return static_cast<FrameView*>(widget)->frame();
670 static bool isSubmitImage(Node* node)
672 return node && node->hasTagName(inputTag)
673 && static_cast<HTMLInputElement*>(node)->inputType() == HTMLInputElement::IMAGE;
676 // Returns true if the node's editable block is not current focused for editing
677 static bool nodeIsNotBeingEdited(Node* node, Frame* frame)
679 return frame->selectionController()->rootEditableElement() != node->rootEditableElement();
682 Cursor EventHandler::selectCursor(const MouseEventWithHitTestResults& event, PlatformScrollbar* scrollbar)
684 // During selection, use an I-beam no matter what we're over.
685 // If you're capturing mouse events for a particular node, don't treat this as a selection.
686 if (m_mousePressed && m_mouseDownMayStartSelect && m_frame->selectionController()->isCaretOrRange() && !m_capturingMouseEventsNode)
687 return iBeamCursor();
689 Node* node = event.targetNode();
690 RenderObject* renderer = node ? node->renderer() : 0;
691 RenderStyle* style = renderer ? renderer->style() : 0;
693 if (style && style->cursors()) {
694 const CursorList* cursors = style->cursors();
695 for (unsigned i = 0; i < cursors->size(); ++i) {
696 CachedImage* cimage = (*cursors)[i].cursorImage;
697 IntPoint hotSpot = (*cursors)[i].hotSpot;
700 Element* e = node->document()->getElementById((*cursors)[i].cursorFragmentId);
701 if (e && e->hasTagName(cursorTag)) {
702 hotSpot.setX(int(static_cast<SVGCursorElement*>(e)->x().value()));
703 hotSpot.setY(int(static_cast<SVGCursorElement*>(e)->y().value()));
704 cimage = static_cast<SVGCursorElement*>(e)->cachedImage();
710 if (cimage->image()->isNull())
712 if (!cimage->errorOccurred())
713 return Cursor(cimage->image(), hotSpot);
717 switch (style ? style->cursor() : CURSOR_AUTO) {
719 bool editable = (node && node->isContentEditable());
720 bool editableLinkEnabled = false;
722 // If the link is editable, then we need to check the settings to see whether or not the link should be followed
724 ASSERT(m_frame->settings());
725 switch(m_frame->settings()->editableLinkBehavior()) {
727 case EditableLinkDefaultBehavior:
728 case EditableLinkAlwaysLive:
729 editableLinkEnabled = true;
732 case EditableLinkNeverLive:
733 editableLinkEnabled = false;
736 case EditableLinkLiveWhenNotFocused:
737 editableLinkEnabled = nodeIsNotBeingEdited(node, m_frame) || event.event().shiftKey();
740 case EditableLinkOnlyLiveWithShiftKey:
741 editableLinkEnabled = event.event().shiftKey();
746 if ((event.isOverLink() || isSubmitImage(node)) && (!editable || editableLinkEnabled))
748 RenderLayer* layer = renderer ? renderer->enclosingLayer() : 0;
749 bool inResizer = false;
750 if (m_frame->view() && layer && layer->isPointInResizeControl(m_frame->view()->windowToContents(event.event().pos())))
752 if ((editable || (renderer && renderer->isText() && node->canStartSelection())) && !inResizer && !scrollbar)
753 return iBeamCursor();
754 return pointerCursor();
757 return crossCursor();
762 case CURSOR_ALL_SCROLL:
764 case CURSOR_E_RESIZE:
765 return eastResizeCursor();
766 case CURSOR_W_RESIZE:
767 return westResizeCursor();
768 case CURSOR_N_RESIZE:
769 return northResizeCursor();
770 case CURSOR_S_RESIZE:
771 return southResizeCursor();
772 case CURSOR_NE_RESIZE:
773 return northEastResizeCursor();
774 case CURSOR_SW_RESIZE:
775 return southWestResizeCursor();
776 case CURSOR_NW_RESIZE:
777 return northWestResizeCursor();
778 case CURSOR_SE_RESIZE:
779 return southEastResizeCursor();
780 case CURSOR_NS_RESIZE:
781 return northSouthResizeCursor();
782 case CURSOR_EW_RESIZE:
783 return eastWestResizeCursor();
784 case CURSOR_NESW_RESIZE:
785 return northEastSouthWestResizeCursor();
786 case CURSOR_NWSE_RESIZE:
787 return northWestSouthEastResizeCursor();
788 case CURSOR_COL_RESIZE:
789 return columnResizeCursor();
790 case CURSOR_ROW_RESIZE:
791 return rowResizeCursor();
793 return iBeamCursor();
798 case CURSOR_VERTICAL_TEXT:
799 return verticalTextCursor();
802 case CURSOR_CONTEXT_MENU:
803 return contextMenuCursor();
804 case CURSOR_PROGRESS:
805 return progressCursor();
807 return noDropCursor();
809 return aliasCursor();
814 case CURSOR_NOT_ALLOWED:
815 return notAllowedCursor();
817 return pointerCursor();
818 case CURSOR_WEBKIT_ZOOM_IN:
819 return zoomInCursor();
820 case CURSOR_WEBKIT_ZOOM_OUT:
821 return zoomOutCursor();
823 return pointerCursor();
826 bool EventHandler::handleMousePressEvent(const PlatformMouseEvent& mouseEvent)
828 if (!m_frame->document())
831 RefPtr<FrameView> protector(m_frame->view());
833 m_mousePressed = true;
834 m_currentMousePosition = mouseEvent.pos();
835 m_mouseDownTimestamp = mouseEvent.timestamp();
836 m_mouseDownMayStartDrag = false;
837 m_mouseDownMayStartSelect = false;
838 m_mouseDownMayStartAutoscroll = false;
839 m_mouseDownPos = m_frame->view()->windowToContents(mouseEvent.pos());
840 m_mouseDownWasInSubframe = false;
842 MouseEventWithHitTestResults mev = prepareMouseEvent(HitTestRequest(false, true), mouseEvent);
844 if (!mev.targetNode()) {
849 m_mousePressNode = mev.targetNode();
851 Frame* subframe = subframeForTargetNode(mev.targetNode());
852 if (subframe && passMousePressEventToSubframe(mev, subframe)) {
853 // Start capturing future events for this frame. We only do this if we didn't clear
854 // the m_mousePressed flag, which may happen if an AppKit widget entered a modal event loop.
856 m_capturingMouseEventsNode = mev.targetNode();
861 m_clickCount = mouseEvent.clickCount();
862 m_clickNode = mev.targetNode();
864 RenderLayer* layer = m_clickNode->renderer() ? m_clickNode->renderer()->enclosingLayer() : 0;
865 IntPoint p = m_frame->view()->windowToContents(mouseEvent.pos());
866 if (layer && layer->isPointInResizeControl(p)) {
867 layer->setInResizeMode(true);
868 m_resizeLayer = layer;
869 m_offsetFromResizeCorner = layer->offsetFromResizeCorner(p);
874 bool swallowEvent = dispatchMouseEvent(mousedownEvent, mev.targetNode(), true, m_clickCount, mouseEvent, true);
876 // If the hit testing originally determined the event was in a scrollbar, refetch the MouseEventWithHitTestResults
877 // in case the scrollbar widget was destroyed when the mouse event was handled.
878 if (mev.scrollbar()) {
879 const bool wasLastScrollBar = mev.scrollbar() == m_lastScrollbarUnderMouse.get();
880 mev = prepareMouseEvent(HitTestRequest(true, true), mouseEvent);
882 if (wasLastScrollBar && mev.scrollbar() != m_lastScrollbarUnderMouse.get())
883 m_lastScrollbarUnderMouse = 0;
887 // scrollbars should get events anyway, even disabled controls might be scrollable
889 passMousePressEventToScrollbar(mev, mev.scrollbar());
891 // Refetch the event target node if it currently is the shadow node inside an <input> element.
892 // If a mouse event handler changes the input element type to one that has a widget associated,
893 // we'd like to EventHandler::handleMousePressEvent to pass the event to the widget and thus the
894 // event target node can't still be the shadow node.
895 if (mev.targetNode()->isShadowNode() && mev.targetNode()->shadowParentNode()->hasTagName(inputTag))
896 mev = prepareMouseEvent(HitTestRequest(true, true), mouseEvent);
898 PlatformScrollbar* scrollbar = m_frame->view()->scrollbarUnderMouse(mouseEvent);
900 scrollbar = mev.scrollbar();
901 if (scrollbar && passMousePressEventToScrollbar(mev, scrollbar))
904 swallowEvent = handleMousePressEvent(mev);
910 // This method only exists for platforms that don't know how to deliver
911 bool EventHandler::handleMouseDoubleClickEvent(const PlatformMouseEvent& mouseEvent)
913 if (!m_frame->document())
916 RefPtr<FrameView> protector(m_frame->view());
918 // We get this instead of a second mouse-up
919 m_mousePressed = false;
920 m_currentMousePosition = mouseEvent.pos();
922 MouseEventWithHitTestResults mev = prepareMouseEvent(HitTestRequest(false, true), mouseEvent);
923 Frame* subframe = subframeForTargetNode(mev.targetNode());
924 if (subframe && passMousePressEventToSubframe(mev, subframe)) {
925 m_capturingMouseEventsNode = 0;
929 m_clickCount = mouseEvent.clickCount();
930 bool swallowMouseUpEvent = dispatchMouseEvent(mouseupEvent, mev.targetNode(), true, m_clickCount, mouseEvent, false);
932 bool swallowClickEvent = false;
933 // Don't ever dispatch click events for right clicks
934 if (mouseEvent.button() != RightButton && mev.targetNode() == m_clickNode)
935 swallowClickEvent = dispatchMouseEvent(clickEvent, mev.targetNode(), true, m_clickCount, mouseEvent, true);
937 bool swallowMouseReleaseEvent = false;
938 if (!swallowMouseUpEvent)
939 swallowMouseReleaseEvent = handleMouseReleaseEvent(mev);
943 return swallowMouseUpEvent || swallowClickEvent || swallowMouseReleaseEvent;
946 bool EventHandler::mouseMoved(const PlatformMouseEvent& event)
948 HitTestResult hoveredNode = HitTestResult(IntPoint());
949 bool result = handleMouseMoveEvent(event, &hoveredNode);
951 Page* page = m_frame->page();
955 hoveredNode.setToNonShadowAncestor();
956 page->chrome()->mouseDidMoveOverElement(hoveredNode, event.modifierFlags());
957 page->chrome()->setToolTip(hoveredNode);
961 bool EventHandler::handleMouseMoveEvent(const PlatformMouseEvent& mouseEvent, HitTestResult* hoveredNode)
963 // in Radar 3703768 we saw frequent crashes apparently due to the
964 // part being null here, which seems impossible, so check for nil
965 // but also assert so that we can try to figure this out in debug
966 // builds, if it happens.
968 if (!m_frame || !m_frame->document())
971 RefPtr<FrameView> protector(m_frame->view());
972 m_currentMousePosition = mouseEvent.pos();
974 if (m_hoverTimer.isActive())
979 static_cast<SVGDocument*>(m_frame->document())->updatePan(m_currentMousePosition);
984 if (m_frameSetBeingResized)
985 return dispatchMouseEvent(mousemoveEvent, m_frameSetBeingResized.get(), false, 0, mouseEvent, false);
987 // Send events right to a scrollbar if the mouse is pressed.
988 if (m_lastScrollbarUnderMouse && m_mousePressed)
989 return m_lastScrollbarUnderMouse->handleMouseMoveEvent(mouseEvent);
991 // Treat mouse move events while the mouse is pressed as "read-only" in prepareMouseEvent
992 // if we are allowed to select.
993 // This means that :hover and :active freeze in the state they were in when the mouse
994 // was pressed, rather than updating for nodes the mouse moves over as you hold the mouse down.
995 HitTestRequest request(m_mousePressed && m_mouseDownMayStartSelect, m_mousePressed, true);
996 MouseEventWithHitTestResults mev = prepareMouseEvent(request, mouseEvent);
998 *hoveredNode = mev.hitTestResult();
1000 PlatformScrollbar* scrollbar = 0;
1002 if (m_resizeLayer && m_resizeLayer->inResizeMode())
1003 m_resizeLayer->resize(mouseEvent, m_offsetFromResizeCorner);
1005 if (m_frame->view())
1006 scrollbar = m_frame->view()->scrollbarUnderMouse(mouseEvent);
1009 scrollbar = mev.scrollbar();
1011 if (m_lastScrollbarUnderMouse != scrollbar) {
1012 // Send mouse exited to the old scrollbar.
1013 if (m_lastScrollbarUnderMouse)
1014 m_lastScrollbarUnderMouse->handleMouseOutEvent(mouseEvent);
1015 m_lastScrollbarUnderMouse = m_mousePressed ? 0 : scrollbar;
1019 bool swallowEvent = false;
1020 Node* targetNode = m_capturingMouseEventsNode ? m_capturingMouseEventsNode.get() : mev.targetNode();
1021 RefPtr<Frame> newSubframe = subframeForTargetNode(targetNode);
1023 // We want mouseouts to happen first, from the inside out. First send a move event to the last subframe so that it will fire mouseouts.
1024 if (m_lastMouseMoveEventSubframe && m_lastMouseMoveEventSubframe->tree()->isDescendantOf(m_frame) && m_lastMouseMoveEventSubframe != newSubframe)
1025 passMouseMoveEventToSubframe(mev, m_lastMouseMoveEventSubframe.get());
1028 // Update over/out state before passing the event to the subframe.
1029 updateMouseEventTargetNode(mev.targetNode(), mouseEvent, true);
1030 swallowEvent |= passMouseMoveEventToSubframe(mev, newSubframe.get(), hoveredNode);
1032 if (scrollbar && !m_mousePressed)
1033 scrollbar->handleMouseMoveEvent(mouseEvent); // Handle hover effects on platforms that support visual feedback on scrollbar hovering.
1034 if ((!m_resizeLayer || !m_resizeLayer->inResizeMode()) && m_frame->view())
1035 m_frame->view()->setCursor(selectCursor(mev, scrollbar));
1038 m_lastMouseMoveEventSubframe = newSubframe;
1043 swallowEvent = dispatchMouseEvent(mousemoveEvent, mev.targetNode(), false, 0, mouseEvent, true);
1045 swallowEvent = handleMouseDraggedEvent(mev);
1047 return swallowEvent;
1050 void EventHandler::invalidateClick()
1056 bool EventHandler::handleMouseReleaseEvent(const PlatformMouseEvent& mouseEvent)
1058 if (!m_frame->document())
1061 RefPtr<FrameView> protector(m_frame->view());
1063 m_mousePressed = false;
1064 m_currentMousePosition = mouseEvent.pos();
1069 static_cast<SVGDocument*>(m_frame->document())->updatePan(m_currentMousePosition);
1074 if (m_frameSetBeingResized)
1075 return dispatchMouseEvent(mouseupEvent, m_frameSetBeingResized.get(), true, m_clickCount, mouseEvent, false);
1077 if (m_lastScrollbarUnderMouse) {
1079 return m_lastScrollbarUnderMouse->handleMouseReleaseEvent(mouseEvent);
1082 MouseEventWithHitTestResults mev = prepareMouseEvent(HitTestRequest(false, false, false, true), mouseEvent);
1083 Node* targetNode = m_capturingMouseEventsNode.get() ? m_capturingMouseEventsNode.get() : mev.targetNode();
1084 Frame* subframe = subframeForTargetNode(targetNode);
1085 if (subframe && passMouseReleaseEventToSubframe(mev, subframe)) {
1086 m_capturingMouseEventsNode = 0;
1090 bool swallowMouseUpEvent = dispatchMouseEvent(mouseupEvent, mev.targetNode(), true, m_clickCount, mouseEvent, false);
1092 // Don't ever dispatch click events for right clicks
1093 bool swallowClickEvent = false;
1094 if (m_clickCount > 0 && mouseEvent.button() != RightButton && mev.targetNode() == m_clickNode)
1095 swallowClickEvent = dispatchMouseEvent(clickEvent, mev.targetNode(), true, m_clickCount, mouseEvent, true);
1097 if (m_resizeLayer) {
1098 m_resizeLayer->setInResizeMode(false);
1102 bool swallowMouseReleaseEvent = false;
1103 if (!swallowMouseUpEvent)
1104 swallowMouseReleaseEvent = handleMouseReleaseEvent(mev);
1108 return swallowMouseUpEvent || swallowClickEvent || swallowMouseReleaseEvent;
1111 bool EventHandler::dispatchDragEvent(const AtomicString& eventType, Node* dragTarget, const PlatformMouseEvent& event, Clipboard* clipboard)
1113 IntPoint contentsPos = m_frame->view()->windowToContents(event.pos());
1115 RefPtr<MouseEvent> me = new MouseEvent(eventType,
1116 true, true, m_frame->document()->defaultView(),
1117 0, event.globalX(), event.globalY(), contentsPos.x(), contentsPos.y(),
1118 event.ctrlKey(), event.altKey(), event.shiftKey(), event.metaKey(),
1121 ExceptionCode ec = 0;
1122 EventTargetNodeCast(dragTarget)->dispatchEvent(me.get(), ec, true);
1123 return me->defaultPrevented();
1126 bool EventHandler::updateDragAndDrop(const PlatformMouseEvent& event, Clipboard* clipboard)
1128 bool accept = false;
1130 if (!m_frame->document())
1133 if (!m_frame->view())
1136 MouseEventWithHitTestResults mev = prepareMouseEvent(HitTestRequest(true, false), event);
1138 // Drag events should never go to text nodes (following IE, and proper mouseover/out dispatch)
1139 Node* newTarget = mev.targetNode();
1140 if (newTarget && newTarget->isTextNode())
1141 newTarget = newTarget->parentNode();
1143 newTarget = newTarget->shadowAncestorNode();
1145 if (m_dragTarget != newTarget) {
1146 // FIXME: this ordering was explicitly chosen to match WinIE. However,
1147 // it is sometimes incorrect when dragging within subframes, as seen with
1148 // LayoutTests/fast/events/drag-in-frames.html.
1150 if (newTarget->hasTagName(frameTag) || newTarget->hasTagName(iframeTag))
1151 accept = static_cast<HTMLFrameElementBase*>(newTarget)->contentFrame()->eventHandler()->updateDragAndDrop(event, clipboard);
1153 accept = dispatchDragEvent(dragenterEvent, newTarget, event, clipboard);
1156 Frame* frame = (m_dragTarget->hasTagName(frameTag) || m_dragTarget->hasTagName(iframeTag))
1157 ? static_cast<HTMLFrameElementBase*>(m_dragTarget.get())->contentFrame() : 0;
1159 accept = frame->eventHandler()->updateDragAndDrop(event, clipboard);
1161 dispatchDragEvent(dragleaveEvent, m_dragTarget.get(), event, clipboard);
1165 if (newTarget->hasTagName(frameTag) || newTarget->hasTagName(iframeTag))
1166 accept = static_cast<HTMLFrameElementBase*>(newTarget)->contentFrame()->eventHandler()->updateDragAndDrop(event, clipboard);
1168 accept = dispatchDragEvent(dragoverEvent, newTarget, event, clipboard);
1170 m_dragTarget = newTarget;
1175 void EventHandler::cancelDragAndDrop(const PlatformMouseEvent& event, Clipboard* clipboard)
1178 Frame* frame = (m_dragTarget->hasTagName(frameTag) || m_dragTarget->hasTagName(iframeTag))
1179 ? static_cast<HTMLFrameElementBase*>(m_dragTarget.get())->contentFrame() : 0;
1181 frame->eventHandler()->cancelDragAndDrop(event, clipboard);
1183 dispatchDragEvent(dragleaveEvent, m_dragTarget.get(), event, clipboard);
1188 bool EventHandler::performDragAndDrop(const PlatformMouseEvent& event, Clipboard* clipboard)
1190 bool accept = false;
1192 Frame* frame = (m_dragTarget->hasTagName(frameTag) || m_dragTarget->hasTagName(iframeTag))
1193 ? static_cast<HTMLFrameElementBase*>(m_dragTarget.get())->contentFrame() : 0;
1195 accept = frame->eventHandler()->performDragAndDrop(event, clipboard);
1197 accept = dispatchDragEvent(dropEvent, m_dragTarget.get(), event, clipboard);
1203 void EventHandler::clearDragState()
1206 m_capturingMouseEventsNode = 0;
1208 m_sendingEventToSubview = false;
1212 Node* EventHandler::nodeUnderMouse() const
1214 return m_nodeUnderMouse.get();
1217 void EventHandler::setCapturingMouseEventsNode(PassRefPtr<Node> n)
1219 m_capturingMouseEventsNode = n;
1222 MouseEventWithHitTestResults EventHandler::prepareMouseEvent(const HitTestRequest& request, const PlatformMouseEvent& mev)
1225 ASSERT(m_frame->document());
1227 IntPoint documentPoint = m_frame->view()->windowToContents(mev.pos());
1228 return m_frame->document()->prepareMouseEvent(request, documentPoint, mev);
1231 void EventHandler::updateMouseEventTargetNode(Node* targetNode, const PlatformMouseEvent& mouseEvent, bool fireMouseOverOut)
1233 Node* result = targetNode;
1235 // If we're capturing, we always go right to that node.
1236 if (m_capturingMouseEventsNode)
1237 result = m_capturingMouseEventsNode.get();
1239 // If the target node is a text node, dispatch on the parent node - rdar://4196646
1240 if (result && result->isTextNode())
1241 result = result->parentNode();
1243 result = result->shadowAncestorNode();
1245 m_nodeUnderMouse = result;
1247 // Fire mouseout/mouseover if the mouse has shifted to a different node.
1248 if (fireMouseOverOut) {
1249 if (m_lastNodeUnderMouse && m_lastNodeUnderMouse->document() != m_frame->document()) {
1250 m_lastNodeUnderMouse = 0;
1251 m_lastScrollbarUnderMouse = 0;
1254 if (m_lastNodeUnderMouse != m_nodeUnderMouse) {
1255 // send mouseout event to the old node
1256 if (m_lastNodeUnderMouse)
1257 EventTargetNodeCast(m_lastNodeUnderMouse.get())->dispatchMouseEvent(mouseEvent, mouseoutEvent, 0, m_nodeUnderMouse.get());
1258 // send mouseover event to the new node
1259 if (m_nodeUnderMouse)
1260 EventTargetNodeCast(m_nodeUnderMouse.get())->dispatchMouseEvent(mouseEvent, mouseoverEvent, 0, m_lastNodeUnderMouse.get());
1262 m_lastNodeUnderMouse = m_nodeUnderMouse;
1266 bool EventHandler::dispatchMouseEvent(const AtomicString& eventType, Node* targetNode, bool cancelable, int clickCount, const PlatformMouseEvent& mouseEvent, bool setUnder)
1268 updateMouseEventTargetNode(targetNode, mouseEvent, setUnder);
1270 bool swallowEvent = false;
1272 if (m_nodeUnderMouse)
1273 swallowEvent = EventTargetNodeCast(m_nodeUnderMouse.get())->dispatchMouseEvent(mouseEvent, eventType, clickCount);
1275 if (!swallowEvent && eventType == mousedownEvent) {
1276 // Blur current focus node when a link/button is clicked; this
1277 // is expected by some sites that rely on onChange handlers running
1278 // from form fields before the button click is processed.
1279 Node* node = m_nodeUnderMouse.get();
1280 RenderObject* renderer = node ? node->renderer() : 0;
1282 // Walk up the render tree to search for a node to focus.
1283 // Walking up the DOM tree wouldn't work for shadow trees, like those behind the engine-based text fields.
1285 node = renderer->element();
1286 if (node && node->isFocusable()) {
1287 // To fix <rdar://problem/4895428> Can't drag selected ToDo, we don't focus a
1288 // node on mouse down if it's selected and inside a focused node. It will be
1289 // focused if the user does a mouseup over it, however, because the mouseup
1290 // will set a selection inside it, which will call setFocuseNodeIfNeeded.
1291 ExceptionCode ec = 0;
1292 Node* n = node->isShadowNode() ? node->shadowParentNode() : node;
1293 if (m_frame->selectionController()->isRange() &&
1294 m_frame->selectionController()->toRange()->compareNode(n, ec) == Range::NODE_INSIDE &&
1295 n->isDescendantOf(m_frame->document()->focusedNode()))
1301 renderer = renderer->parent();
1303 // If focus shift is blocked, we eat the event. Note we should never clear swallowEvent
1304 // if the page already set it (e.g., by canceling default behavior).
1305 if (node && node->isMouseFocusable()) {
1306 if (!m_frame->page()->focusController()->setFocusedNode(node, m_frame))
1307 swallowEvent = true;
1308 } else if (!node || !node->focused()) {
1309 if (!m_frame->page()->focusController()->setFocusedNode(0, m_frame))
1310 swallowEvent = true;
1314 return swallowEvent;
1317 bool EventHandler::handleWheelEvent(PlatformWheelEvent& e)
1319 Document* doc = m_frame->document();
1323 RenderObject* docRenderer = doc->renderer();
1327 IntPoint vPoint = m_frame->view()->windowToContents(e.pos());
1329 HitTestRequest request(true, false);
1330 HitTestResult result(vPoint);
1331 doc->renderer()->layer()->hitTest(request, result);
1332 Node* node = result.innerNode();
1335 // Figure out which view to send the event to.
1336 RenderObject* target = node->renderer();
1338 if (target && target->isWidget()) {
1339 Widget* widget = static_cast<RenderWidget*>(target)->widget();
1341 if (widget && passWheelEventToWidget(e, widget)) {
1347 node = node->shadowAncestorNode();
1348 EventTargetNodeCast(node)->dispatchWheelEvent(e);
1352 if (node->renderer()) {
1353 // Just break up into two scrolls if we need to. Diagonal movement on
1354 // a MacBook pro is an example of a 2-dimensional mouse wheel event (where both deltaX and deltaY can be set).
1355 float deltaX = e.isContinuous() ? e.continuousDeltaX() : e.deltaX();
1356 float deltaY = e.isContinuous() ? e.continuousDeltaY() : e.deltaY();
1357 if (deltaX && node->renderer()->scroll(deltaX < 0 ? ScrollRight : ScrollLeft, e.isContinuous() ? ScrollByPixel : ScrollByLine,
1358 deltaX < 0 ? -deltaX : deltaX))
1360 if (deltaY && node->renderer()->scroll(deltaY < 0 ? ScrollDown : ScrollUp, e.isContinuous() ? ScrollByPixel : ScrollByLine,
1361 deltaY < 0 ? -deltaY : deltaY))
1366 if (!e.isAccepted())
1367 m_frame->view()->wheelEvent(e);
1369 return e.isAccepted();
1372 bool EventHandler::sendContextMenuEvent(const PlatformMouseEvent& event)
1374 Document* doc = m_frame->document();
1375 FrameView* v = m_frame->view();
1380 IntPoint viewportPos = v->windowToContents(event.pos());
1381 MouseEventWithHitTestResults mev = doc->prepareMouseEvent(HitTestRequest(false, true), viewportPos, event);
1383 if (!m_frame->selectionController()->contains(viewportPos) &&
1384 // FIXME: In the editable case, word selection sometimes selects content that isn't underneath the mouse.
1385 // If the selection is non-editable, we do word selection to make it easier to use the contextual menu items
1386 // available for text selections. But only if we're above text.
1387 (m_frame->selectionController()->isContentEditable() || mev.targetNode() && mev.targetNode()->isTextNode())) {
1388 m_mouseDownMayStartSelect = true; // context menu events are always allowed to perform a selection
1389 selectClosestWordOrLinkFromMouseEvent(mev);
1392 swallowEvent = dispatchMouseEvent(contextmenuEvent, mev.targetNode(), true, 0, event, true);
1394 return swallowEvent;
1397 void EventHandler::scheduleHoverStateUpdate()
1399 if (!m_hoverTimer.isActive())
1400 m_hoverTimer.startOneShot(0);
1403 // Whether or not a mouse down can begin the creation of a selection. Fires the selectStart event.
1404 bool EventHandler::canMouseDownStartSelect(Node* node)
1406 if (!node || !node->renderer())
1409 // Some controls and images can't start a select on a mouse down.
1410 if (!node->canStartSelection())
1413 for (RenderObject* curr = node->renderer(); curr; curr = curr->parent())
1414 if (Node* node = curr->element())
1415 return EventTargetNodeCast(node)->dispatchHTMLEvent(selectstartEvent, true, true);
1420 bool EventHandler::canMouseDragExtendSelect(Node* node)
1422 if (!node || !node->renderer())
1425 for (RenderObject* curr = node->renderer(); curr; curr = curr->parent())
1426 if (Node* node = curr->element())
1427 return EventTargetNodeCast(node)->dispatchHTMLEvent(selectstartEvent, true, true);
1432 void EventHandler::setResizingFrameSet(HTMLFrameSetElement* frameSet)
1434 m_frameSetBeingResized = frameSet;
1437 void EventHandler::hoverTimerFired(Timer<EventHandler>*)
1439 m_hoverTimer.stop();
1442 ASSERT(m_frame->document());
1444 if (RenderObject* renderer = m_frame->renderer()) {
1445 HitTestResult result(m_frame->view()->windowToContents(m_currentMousePosition));
1446 renderer->layer()->hitTest(HitTestRequest(false, false, true), result);
1447 m_frame->document()->updateRendering();
1451 static EventTargetNode* eventTargetNodeForDocument(Document* doc)
1455 Node* node = doc->focusedNode();
1457 if (doc->isHTMLDocument())
1460 node = doc->documentElement();
1464 return EventTargetNodeCast(node);
1467 bool EventHandler::handleAccessKey(const PlatformKeyboardEvent& evt)
1475 String key = evt.unmodifiedText();
1476 Element* elem = m_frame->document()->getElementByAccessKey(key.lower());
1478 elem->accessKeyAction(false);
1487 bool EventHandler::needsKeyboardEventDisambiguationQuirks() const
1493 bool EventHandler::keyEvent(const PlatformKeyboardEvent& initialKeyEvent)
1495 // Check for cases where we are too early for events -- possible unmatched key up
1496 // from pressing return in the location bar.
1497 RefPtr<EventTargetNode> node = eventTargetNodeForDocument(m_frame->document());
1501 // FIXME: what is this doing here, in keyboard event handler?
1502 m_frame->loader()->resetMultipleFormSubmissionProtection();
1504 // In IE, access keys are special, they are handled after default keydown processing, but cannot be canceled - this is hard to match.
1505 // On Mac OS X, we process them before dispatching keydown, as the default keydown handler implements Emacs key bindings, which may conflict
1506 // with access keys. Then we dispatch keydown, but suppress its default handling.
1507 // On Windows, WebKit explicitly calls handleAccessKey() instead of dispatching a keypress event for WM_SYSCHAR messages.
1508 // Other platforms currently match either Mac or Windows behavior, depending on whether they send combined KeyDown events.
1509 bool matchedAnAccessKey = false;
1510 if (initialKeyEvent.type() == PlatformKeyboardEvent::KeyDown)
1511 matchedAnAccessKey = handleAccessKey(initialKeyEvent);
1513 // FIXME: it would be fair to let an input method handle KeyUp events before DOM dispatch.
1514 if (initialKeyEvent.type() == PlatformKeyboardEvent::KeyUp || initialKeyEvent.type() == PlatformKeyboardEvent::Char)
1515 return !node->dispatchKeyEvent(initialKeyEvent);
1517 bool backwardCompatibilityMode = needsKeyboardEventDisambiguationQuirks();
1520 PlatformKeyboardEvent keyDownEvent = initialKeyEvent;
1521 if (keyDownEvent.type() != PlatformKeyboardEvent::RawKeyDown)
1522 keyDownEvent.disambiguateKeyDownEvent(PlatformKeyboardEvent::RawKeyDown, backwardCompatibilityMode);
1523 RefPtr<KeyboardEvent> keydown = new KeyboardEvent(keyDownEvent, m_frame->document()->defaultView());
1524 if (matchedAnAccessKey)
1525 keydown->setDefaultPrevented(true);
1526 keydown->setTarget(node);
1528 if (initialKeyEvent.type() == PlatformKeyboardEvent::RawKeyDown) {
1529 node->dispatchEvent(keydown, ec, true);
1530 return keydown->defaultHandled() || keydown->defaultPrevented();
1533 // Run input method in advance of DOM event handling. This may result in the IM
1534 // modifying the page prior the keydown event, but this behaviour is necessary
1535 // in order to match IE:
1536 // 1. preventing default handling of keydown and keypress events has no effect on IM input;
1537 // 2. if an input method handles the event, its keyCode is set to 229 in keydown event.
1538 m_frame->editor()->handleInputMethodKeydown(keydown.get());
1540 bool handledByInputMethod = keydown->defaultHandled();
1542 if (handledByInputMethod) {
1543 keyDownEvent.setWindowsVirtualKeyCode(CompositionEventKeyCode);
1544 keydown = new KeyboardEvent(keyDownEvent, m_frame->document()->defaultView());
1545 keydown->setTarget(node);
1546 keydown->setDefaultHandled();
1549 node->dispatchEvent(keydown, ec, true);
1550 bool keydownResult = keydown->defaultHandled() || keydown->defaultPrevented();
1551 if (handledByInputMethod || (keydownResult && !backwardCompatibilityMode))
1552 return keydownResult;
1554 // Focus may have changed during keydown handling, so refetch node.
1555 // But if we are dispatching a fake backward compatibility keypress, then we pretend that the keypress happened on the original node.
1556 if (!keydownResult) {
1557 node = eventTargetNodeForDocument(m_frame->document());
1562 PlatformKeyboardEvent keyPressEvent = initialKeyEvent;
1563 keyPressEvent.disambiguateKeyDownEvent(PlatformKeyboardEvent::Char, backwardCompatibilityMode);
1564 if (keyPressEvent.text().isEmpty())
1565 return keydownResult;
1566 RefPtr<KeyboardEvent> keypress = new KeyboardEvent(keyPressEvent, m_frame->document()->defaultView());
1567 keypress->setTarget(node);
1569 keypress->setDefaultPrevented(true);
1571 keypress->keypressCommands() = keydown->keypressCommands();
1573 node->dispatchEvent(keypress, ec, true);
1575 return keydownResult || keypress->defaultPrevented() || keypress->defaultHandled();
1578 void EventHandler::defaultKeyboardEventHandler(KeyboardEvent* event)
1580 if (event->type() == keydownEvent) {
1581 m_frame->editor()->handleKeyboardEvent(event);
1582 if (event->defaultHandled())
1584 if (event->keyIdentifier() == "U+0009")
1585 defaultTabEventHandler(event, false);
1587 if (event->type() == keypressEvent) {
1588 m_frame->editor()->handleKeyboardEvent(event);
1589 if (event->defaultHandled())
1594 bool EventHandler::dragHysteresisExceeded(const FloatPoint& floatDragViewportLocation) const
1596 IntPoint dragViewportLocation((int)floatDragViewportLocation.x(), (int)floatDragViewportLocation.y());
1597 return dragHysteresisExceeded(dragViewportLocation);
1600 bool EventHandler::dragHysteresisExceeded(const IntPoint& dragViewportLocation) const
1602 IntPoint dragLocation = m_frame->view()->windowToContents(dragViewportLocation);
1603 IntSize delta = dragLocation - m_mouseDownPos;
1605 int threshold = GeneralDragHysteresis;
1606 if (dragState().m_dragSrcIsImage)
1607 threshold = ImageDragHysteresis;
1608 else if (dragState().m_dragSrcIsLink)
1609 threshold = LinkDragHysteresis;
1610 else if (dragState().m_dragSrcInSelection)
1611 threshold = TextDragHysteresis;
1613 return abs(delta.width()) >= threshold || abs(delta.height()) >= threshold;
1616 void EventHandler::freeClipboard()
1618 if (dragState().m_dragClipboard)
1619 dragState().m_dragClipboard->setAccessPolicy(ClipboardNumb);
1622 bool EventHandler::shouldDragAutoNode(Node* node, const IntPoint& point) const
1625 if (node->hasChildNodes() || !m_frame->view())
1627 return m_frame->page() && m_frame->page()->dragController()->mayStartDragAtEventLocation(m_frame, point);
1630 void EventHandler::dragSourceMovedTo(const PlatformMouseEvent& event)
1632 if (dragState().m_dragSrc && dragState().m_dragSrcMayBeDHTML)
1633 // for now we don't care if event handler cancels default behavior, since there is none
1634 dispatchDragSrcEvent(dragEvent, event);
1637 void EventHandler::dragSourceEndedAt(const PlatformMouseEvent& event, DragOperation operation)
1639 if (dragState().m_dragSrc && dragState().m_dragSrcMayBeDHTML) {
1640 dragState().m_dragClipboard->setDestinationOperation(operation);
1641 // for now we don't care if event handler cancels default behavior, since there is none
1642 dispatchDragSrcEvent(dragendEvent, event);
1645 dragState().m_dragSrc = 0;
1648 // returns if we should continue "default processing", i.e., whether eventhandler canceled
1649 bool EventHandler::dispatchDragSrcEvent(const AtomicString& eventType, const PlatformMouseEvent& event)
1651 return !dispatchDragEvent(eventType, dragState().m_dragSrc.get(), event, dragState().m_dragClipboard.get());
1654 bool EventHandler::handleDrag(const MouseEventWithHitTestResults& event)
1656 if (event.event().button() != LeftButton || event.event().eventType() != MouseEventMoved) {
1657 // If we allowed the other side of the bridge to handle a drag
1658 // last time, then m_mousePressed might still be set. So we
1659 // clear it now to make sure the next move after a drag
1660 // doesn't look like a drag.
1661 m_mousePressed = false;
1665 if (eventLoopHandleMouseDragged(event))
1668 // Careful that the drag starting logic stays in sync with eventMayStartDrag()
1670 if (m_mouseDownMayStartDrag && !dragState().m_dragSrc) {
1671 allowDHTMLDrag(dragState().m_dragSrcMayBeDHTML, dragState().m_dragSrcMayBeUA);
1672 if (!dragState().m_dragSrcMayBeDHTML && !dragState().m_dragSrcMayBeUA)
1673 m_mouseDownMayStartDrag = false; // no element is draggable
1676 if (m_mouseDownMayStartDrag && !dragState().m_dragSrc) {
1677 // try to find an element that wants to be dragged
1678 HitTestRequest request(true, false);
1679 HitTestResult result(m_mouseDownPos);
1680 m_frame->renderer()->layer()->hitTest(request, result);
1681 Node* node = result.innerNode();
1682 if (node && node->renderer())
1683 dragState().m_dragSrc = node->renderer()->draggableNode(dragState().m_dragSrcMayBeDHTML, dragState().m_dragSrcMayBeUA,
1684 m_mouseDownPos.x(), m_mouseDownPos.y(), dragState().m_dragSrcIsDHTML);
1686 dragState().m_dragSrc = 0;
1688 if (!dragState().m_dragSrc)
1689 m_mouseDownMayStartDrag = false; // no element is draggable
1691 // remember some facts about this source, while we have a HitTestResult handy
1692 node = result.URLElement();
1693 dragState().m_dragSrcIsLink = node && node->isLink();
1695 node = result.innerNonSharedNode();
1696 dragState().m_dragSrcIsImage = node && node->renderer() && node->renderer()->isImage();
1698 dragState().m_dragSrcInSelection = m_frame->selectionController()->contains(m_mouseDownPos);
1702 // For drags starting in the selection, the user must wait between the mousedown and mousedrag,
1703 // or else we bail on the dragging stuff and allow selection to occur
1704 if (m_mouseDownMayStartDrag && !dragState().m_dragSrcIsImage && dragState().m_dragSrcInSelection && event.event().timestamp() - m_mouseDownTimestamp < TextDragDelay) {
1705 m_mouseDownMayStartDrag = false;
1706 dragState().m_dragSrc = 0;
1707 // ...but if this was the first click in the window, we don't even want to start selection
1708 if (eventActivatedView(event.event()))
1709 m_mouseDownMayStartSelect = false;
1712 if (!m_mouseDownMayStartDrag)
1713 return !mouseDownMayStartSelect() && !m_mouseDownMayStartAutoscroll;
1715 // We are starting a text/image/url drag, so the cursor should be an arrow
1716 m_frame->view()->setCursor(pointerCursor());
1718 if (!dragHysteresisExceeded(event.event().pos()))
1721 // Once we're past the hysteresis point, we don't want to treat this gesture as a click
1724 DragOperation srcOp = DragOperationNone;
1726 freeClipboard(); // would only happen if we missed a dragEnd. Do it anyway, just
1727 // to make sure it gets numbified
1728 dragState().m_dragClipboard = createDraggingClipboard();
1730 if (dragState().m_dragSrcMayBeDHTML) {
1731 // Check to see if the is a DOM based drag, if it is get the DOM specified drag
1733 if (dragState().m_dragSrcIsDHTML) {
1735 dragState().m_dragSrc->renderer()->absolutePosition(srcX, srcY);
1736 IntSize delta = m_mouseDownPos - IntPoint(srcX, srcY);
1737 dragState().m_dragClipboard->setDragImageElement(dragState().m_dragSrc.get(), IntPoint() + delta);
1740 m_mouseDownMayStartDrag = dispatchDragSrcEvent(dragstartEvent, m_mouseDown)
1741 && !m_frame->selectionController()->isInPasswordField();
1743 // Invalidate clipboard here against anymore pasteboard writing for security. The drag
1744 // image can still be changed as we drag, but not the pasteboard data.
1745 dragState().m_dragClipboard->setAccessPolicy(ClipboardImageWritable);
1747 if (m_mouseDownMayStartDrag) {
1748 // gather values from DHTML element, if it set any
1749 dragState().m_dragClipboard->sourceOperation(srcOp);
1751 // Yuck, dragSourceMovedTo() can be called as a result of kicking off the drag with
1752 // dragImage! Because of that dumb reentrancy, we may think we've not started the
1753 // drag when that happens. So we have to assume it's started before we kick it off.
1754 dragState().m_dragClipboard->setDragHasStarted();
1758 if (m_mouseDownMayStartDrag) {
1759 DragController* dragController = m_frame->page() ? m_frame->page()->dragController() : 0;
1760 bool startedDrag = dragController && dragController->startDrag(m_frame, dragState().m_dragClipboard.get(), srcOp, event.event(), m_mouseDownPos, dragState().m_dragSrcIsDHTML);
1761 if (!startedDrag && dragState().m_dragSrcMayBeDHTML) {
1762 // Drag was canned at the last minute - we owe m_dragSrc a DRAGEND event
1763 dispatchDragSrcEvent(dragendEvent, event.event());
1764 m_mouseDownMayStartDrag = false;
1768 if (!m_mouseDownMayStartDrag) {
1769 // something failed to start the drag, cleanup
1771 dragState().m_dragSrc = 0;
1774 // No more default handling (like selection), whether we're past the hysteresis bounds or not
1778 bool EventHandler::handleTextInputEvent(const String& text, Event* underlyingEvent,
1779 bool isLineBreak, bool isBackTab)
1784 // Platforms should differentiate real commands like selectAll from text input in disguise (like insertNewline),
1785 // and avoid dispatching text input events from keydown default handlers.
1786 if (underlyingEvent && underlyingEvent->isKeyboardEvent())
1787 ASSERT(static_cast<KeyboardEvent*>(underlyingEvent)->type() == keypressEvent);
1789 EventTarget* target;
1790 if (underlyingEvent)
1791 target = underlyingEvent->target();
1793 target = eventTargetNodeForDocument(m_frame->document());
1796 RefPtr<TextEvent> event = new TextEvent(m_frame->domWindow(), text);
1797 event->setUnderlyingEvent(underlyingEvent);
1798 event->setIsLineBreak(isLineBreak);
1799 event->setIsBackTab(isBackTab);
1801 return target->dispatchEvent(event.release(), ec, true);
1805 #if !PLATFORM(MAC) && !PLATFORM(QT)
1806 bool EventHandler::invertSenseOfTabsToLinks(KeyboardEvent*) const
1812 bool EventHandler::tabsToLinks(KeyboardEvent* event) const
1814 Page* page = m_frame->page();
1818 if (page->chrome()->client()->tabsToLinks())
1819 return !invertSenseOfTabsToLinks(event);
1821 return invertSenseOfTabsToLinks(event);
1824 void EventHandler::defaultTextInputEventHandler(TextEvent* event)
1826 String data = event->data();
1828 if (event->isLineBreak()) {
1829 if (m_frame->editor()->insertLineBreak())
1830 event->setDefaultHandled();
1832 if (m_frame->editor()->insertParagraphSeparator())
1833 event->setDefaultHandled();
1836 if (m_frame->editor()->insertTextWithoutSendingTextEvent(data, false, event))
1837 event->setDefaultHandled();
1841 void EventHandler::defaultTabEventHandler(Event* event, bool isBackTab)
1843 Page* page = m_frame->page();
1844 // Tabs can be used in design mode editing. You can still move out with back tab.
1845 if (!page || !page->tabKeyCyclesThroughElements() || (m_frame->document()->inDesignMode() && !isBackTab))
1847 FocusController* focus = page->focusController();
1848 KeyboardEvent* keyboardEvent = findKeyboardEvent(event);
1851 handled = focus->advanceFocus(FocusDirectionBackward, keyboardEvent);
1853 handled = focus->advanceFocus(keyboardEvent); // get direction from keyboard event
1855 event->setDefaultHandled();
1858 void EventHandler::capsLockStateMayHaveChanged()
1860 if (Document* d = m_frame->document())
1861 if (Node* node = d->focusedNode())
1862 if (RenderObject* r = node->renderer())
1863 r->capsLockStateMayHaveChanged();