|
1 /* |
|
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. |
|
3 * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org) |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
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. |
|
13 * |
|
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. |
|
25 */ |
|
26 |
|
27 #include "config.h" |
|
28 #include "EventHandler.h" |
|
29 |
|
30 #include "CachedImage.h" |
|
31 #include "ChromeClient.h" |
|
32 #include "Cursor.h" |
|
33 #include "Document.h" |
|
34 #include "DragController.h" |
|
35 #include "Editor.h" |
|
36 #include "EventNames.h" |
|
37 #include "FloatPoint.h" |
|
38 #include "FocusController.h" |
|
39 #include "Frame.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" |
|
49 #include "Image.h" |
|
50 #include "KeyboardEvent.h" |
|
51 #include "MouseEvent.h" |
|
52 #include "MouseEventWithHitTestResults.h" |
|
53 #include "Page.h" |
|
54 #include "PlatformKeyboardEvent.h" |
|
55 #include "PlatformScrollBar.h" |
|
56 #include "PlatformWheelEvent.h" |
|
57 #include "RenderWidget.h" |
|
58 #include "SelectionController.h" |
|
59 #include "Settings.h" |
|
60 #include "TextEvent.h" |
|
61 |
|
62 #if ENABLE(SVG) |
|
63 #include "SVGCursorElement.h" |
|
64 #include "SVGDocument.h" |
|
65 #include "SVGLength.h" |
|
66 #include "SVGNames.h" |
|
67 #endif |
|
68 |
|
69 namespace WebCore { |
|
70 |
|
71 using namespace EventNames; |
|
72 using namespace HTMLNames; |
|
73 |
|
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; |
|
82 |
|
83 // Match key code of composition keydown event on windows. |
|
84 // IE sends VK_PROCESSKEY which has value 229; |
|
85 const int CompositionEventKeyCode = 229; |
|
86 |
|
87 #if ENABLE(SVG) |
|
88 using namespace SVGNames; |
|
89 #endif |
|
90 |
|
91 const double autoscrollInterval = 0.1; |
|
92 |
|
93 static Frame* subframeForTargetNode(Node* node); |
|
94 |
|
95 EventHandler::EventHandler(Frame* frame) |
|
96 : m_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) |
|
107 #if ENABLE(SVG) |
|
108 , m_svgPan(false) |
|
109 #endif |
|
110 , m_resizeLayer(0) |
|
111 , m_capturingMouseEventsNode(0) |
|
112 , m_clickCount(0) |
|
113 , m_mouseDownTimestamp(0) |
|
114 #if PLATFORM(MAC) |
|
115 , m_mouseDownView(nil) |
|
116 , m_sendingEventToSubview(false) |
|
117 , m_activationEventNumber(0) |
|
118 #endif |
|
119 { |
|
120 } |
|
121 |
|
122 EventHandler::~EventHandler() |
|
123 { |
|
124 } |
|
125 |
|
126 EventHandler::EventHandlerDragState& EventHandler::dragState() |
|
127 { |
|
128 static EventHandlerDragState state; |
|
129 return state; |
|
130 } |
|
131 |
|
132 void EventHandler::clear() |
|
133 { |
|
134 m_hoverTimer.stop(); |
|
135 m_resizeLayer = 0; |
|
136 m_nodeUnderMouse = 0; |
|
137 m_lastNodeUnderMouse = 0; |
|
138 m_lastMouseMoveEventSubframe = 0; |
|
139 m_lastScrollbarUnderMouse = 0; |
|
140 m_clickCount = 0; |
|
141 m_clickNode = 0; |
|
142 m_frameSetBeingResized = 0; |
|
143 m_dragTarget = 0; |
|
144 m_currentMousePosition = IntPoint(); |
|
145 m_mousePressNode = 0; |
|
146 m_mousePressed = false; |
|
147 m_capturingMouseEventsNode = 0; |
|
148 } |
|
149 |
|
150 void EventHandler::selectClosestWordFromMouseEvent(const MouseEventWithHitTestResults& result) |
|
151 { |
|
152 Node* innerNode = result.targetNode(); |
|
153 Selection newSelection; |
|
154 |
|
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); |
|
160 } |
|
161 |
|
162 if (newSelection.isRange()) { |
|
163 m_frame->setSelectionGranularity(WordGranularity); |
|
164 m_beganSelectingText = true; |
|
165 } |
|
166 |
|
167 if (m_frame->shouldChangeSelection(newSelection)) |
|
168 m_frame->selectionController()->setSelection(newSelection); |
|
169 } |
|
170 } |
|
171 |
|
172 bool EventHandler::handleMousePressEventDoubleClick(const MouseEventWithHitTestResults& event) |
|
173 { |
|
174 if (event.event().button() != LeftButton) |
|
175 return false; |
|
176 |
|
177 if (m_frame->selectionController()->isRange()) |
|
178 // A double-click when range is already selected |
|
179 // should not change the selection. So, do not call |
|
180 // selectClosestWordFromMouseEvent, but do set |
|
181 // m_beganSelectingText to prevent handleMouseReleaseEvent |
|
182 // from setting caret selection. |
|
183 m_beganSelectingText = true; |
|
184 else |
|
185 selectClosestWordFromMouseEvent(event); |
|
186 |
|
187 return true; |
|
188 } |
|
189 |
|
190 bool EventHandler::handleMousePressEventTripleClick(const MouseEventWithHitTestResults& event) |
|
191 { |
|
192 if (event.event().button() != LeftButton) |
|
193 return false; |
|
194 |
|
195 Node* innerNode = event.targetNode(); |
|
196 if (!(innerNode && innerNode->renderer() && m_mouseDownMayStartSelect)) |
|
197 return false; |
|
198 |
|
199 Selection newSelection; |
|
200 VisiblePosition pos(innerNode->renderer()->positionForPoint(event.localPoint())); |
|
201 if (pos.isNotNull()) { |
|
202 newSelection = Selection(pos); |
|
203 newSelection.expandUsingGranularity(ParagraphGranularity); |
|
204 } |
|
205 if (newSelection.isRange()) { |
|
206 m_frame->setSelectionGranularity(ParagraphGranularity); |
|
207 m_beganSelectingText = true; |
|
208 } |
|
209 |
|
210 if (m_frame->shouldChangeSelection(newSelection)) |
|
211 m_frame->selectionController()->setSelection(newSelection); |
|
212 |
|
213 return true; |
|
214 } |
|
215 |
|
216 bool EventHandler::handleMousePressEventSingleClick(const MouseEventWithHitTestResults& event) |
|
217 { |
|
218 if (event.event().button() != LeftButton) |
|
219 return false; |
|
220 |
|
221 Node* innerNode = event.targetNode(); |
|
222 if (!(innerNode && innerNode->renderer() && m_mouseDownMayStartSelect)) |
|
223 return false; |
|
224 |
|
225 // Extend the selection if the Shift key is down, unless the click is in a link. |
|
226 bool extendSelection = event.event().shiftKey() && !event.isOverLink(); |
|
227 |
|
228 // Don't restart the selection when the mouse is pressed on an |
|
229 // existing selection so we can allow for text dragging. |
|
230 IntPoint vPoint = m_frame->view()->windowToContents(event.event().pos()); |
|
231 if (!extendSelection && m_frame->selectionController()->contains(vPoint)) { |
|
232 m_mouseDownWasSingleClickInSelection = true; |
|
233 return false; |
|
234 } |
|
235 |
|
236 VisiblePosition visiblePos(innerNode->renderer()->positionForPoint(event.localPoint())); |
|
237 if (visiblePos.isNull()) |
|
238 visiblePos = VisiblePosition(innerNode, innerNode->caretMinOffset(), DOWNSTREAM); |
|
239 Position pos = visiblePos.deepEquivalent(); |
|
240 |
|
241 Selection newSelection = m_frame->selectionController()->selection(); |
|
242 if (extendSelection && newSelection.isCaretOrRange()) { |
|
243 m_frame->selectionController()->setLastChangeWasHorizontalExtension(false); |
|
244 |
|
245 // See <rdar://problem/3668157> REGRESSION (Mail): shift-click deselects when selection |
|
246 // was created right-to-left |
|
247 Position start = newSelection.start(); |
|
248 Position end = newSelection.end(); |
|
249 short before = Range::compareBoundaryPoints(pos.node(), pos.offset(), start.node(), start.offset()); |
|
250 if (before <= 0) |
|
251 newSelection = Selection(pos, end); |
|
252 else |
|
253 newSelection = Selection(start, pos); |
|
254 |
|
255 if (m_frame->selectionGranularity() != CharacterGranularity) |
|
256 newSelection.expandUsingGranularity(m_frame->selectionGranularity()); |
|
257 m_beganSelectingText = true; |
|
258 } else { |
|
259 newSelection = Selection(visiblePos); |
|
260 m_frame->setSelectionGranularity(CharacterGranularity); |
|
261 } |
|
262 |
|
263 if (m_frame->shouldChangeSelection(newSelection)) |
|
264 m_frame->selectionController()->setSelection(newSelection); |
|
265 |
|
266 return true; |
|
267 } |
|
268 |
|
269 bool EventHandler::handleMousePressEvent(const MouseEventWithHitTestResults& event) |
|
270 { |
|
271 bool singleClick = event.event().clickCount() <= 1; |
|
272 |
|
273 // If we got the event back, that must mean it wasn't prevented, |
|
274 // so it's allowed to start a drag or selection. |
|
275 m_mouseDownMayStartSelect = canMouseDownStartSelect(event.targetNode()); |
|
276 |
|
277 // Careful that the drag starting logic stays in sync with eventMayStartDrag() |
|
278 m_mouseDownMayStartDrag = singleClick; |
|
279 |
|
280 m_mouseDownWasSingleClickInSelection = false; |
|
281 |
|
282 if (passWidgetMouseDownEventToWidget(event)) |
|
283 return true; |
|
284 |
|
285 #if ENABLE(SVG) |
|
286 if (m_frame->document()->isSVGDocument() && |
|
287 static_cast<SVGDocument*>(m_frame->document())->zoomAndPanEnabled()) { |
|
288 if (event.event().shiftKey() && singleClick) { |
|
289 m_svgPan = true; |
|
290 static_cast<SVGDocument*>(m_frame->document())->startPan(event.event().pos()); |
|
291 return true; |
|
292 } |
|
293 } |
|
294 #endif |
|
295 |
|
296 // We don't do this at the start of mouse down handling, |
|
297 // because we don't want to do it until we know we didn't hit a widget. |
|
298 if (singleClick) |
|
299 focusDocumentView(); |
|
300 |
|
301 Node* innerNode = event.targetNode(); |
|
302 |
|
303 m_mousePressNode = innerNode; |
|
304 m_dragStartPos = event.event().pos(); |
|
305 |
|
306 bool swallowEvent = false; |
|
307 if (event.event().button() == LeftButton || event.event().button() == MiddleButton) { |
|
308 m_frame->selectionController()->setCaretBlinkingSuspended(true); |
|
309 m_mousePressed = true; |
|
310 m_beganSelectingText = false; |
|
311 |
|
312 if (event.event().clickCount() == 2) |
|
313 swallowEvent = handleMousePressEventDoubleClick(event); |
|
314 else if (event.event().clickCount() >= 3) |
|
315 swallowEvent = handleMousePressEventTripleClick(event); |
|
316 else |
|
317 swallowEvent = handleMousePressEventSingleClick(event); |
|
318 } |
|
319 |
|
320 m_mouseDownMayStartAutoscroll = m_mouseDownMayStartSelect || |
|
321 (m_mousePressNode && m_mousePressNode->renderer() && m_mousePressNode->renderer()->shouldAutoscroll()); |
|
322 |
|
323 return swallowEvent; |
|
324 } |
|
325 |
|
326 bool EventHandler::handleMouseDraggedEvent(const MouseEventWithHitTestResults& event) |
|
327 { |
|
328 if (handleDrag(event)) |
|
329 return true; |
|
330 |
|
331 if (!m_mousePressed) |
|
332 return false; |
|
333 |
|
334 Node* targetNode = event.targetNode(); |
|
335 if (event.event().button() != LeftButton || !targetNode || !targetNode->renderer()) |
|
336 return false; |
|
337 |
|
338 #if PLATFORM(MAC) // FIXME: Why does this assertion fire on other platforms? |
|
339 ASSERT(m_mouseDownMayStartSelect || m_mouseDownMayStartAutoscroll); |
|
340 #endif |
|
341 |
|
342 m_mouseDownMayStartDrag = false; |
|
343 |
|
344 if (m_mouseDownMayStartAutoscroll) { |
|
345 // If the selection is contained in a layer that can scroll, that layer should handle the autoscroll |
|
346 // Otherwise, let the bridge handle it so the view can scroll itself. |
|
347 RenderObject* renderer = targetNode->renderer(); |
|
348 while (renderer && !renderer->shouldAutoscroll()) |
|
349 renderer = renderer->parent(); |
|
350 if (renderer) |
|
351 handleAutoscroll(renderer); |
|
352 } |
|
353 |
|
354 updateSelectionForMouseDrag(targetNode, event.localPoint()); |
|
355 return true; |
|
356 } |
|
357 |
|
358 bool EventHandler::eventMayStartDrag(const PlatformMouseEvent& event) const |
|
359 { |
|
360 // This is a pre-flight check of whether the event might lead to a drag being started. Be careful |
|
361 // that its logic needs to stay in sync with handleMouseMoveEvent() and the way we setMouseDownMayStartDrag |
|
362 // in handleMousePressEvent |
|
363 |
|
364 if (!m_frame->renderer() || !m_frame->renderer()->hasLayer() |
|
365 || event.button() != LeftButton || event.clickCount() != 1) |
|
366 return false; |
|
367 |
|
368 bool DHTMLFlag; |
|
369 bool UAFlag; |
|
370 allowDHTMLDrag(DHTMLFlag, UAFlag); |
|
371 if (!DHTMLFlag && !UAFlag) |
|
372 return false; |
|
373 |
|
374 HitTestRequest request(true, false); |
|
375 HitTestResult result(m_frame->view()->windowToContents(event.pos())); |
|
376 m_frame->renderer()->layer()->hitTest(request, result); |
|
377 bool srcIsDHTML; |
|
378 return result.innerNode() && result.innerNode()->renderer()->draggableNode(DHTMLFlag, UAFlag, result.point().x(), result.point().y(), srcIsDHTML); |
|
379 } |
|
380 |
|
381 void EventHandler::updateSelectionForMouseDrag() |
|
382 { |
|
383 FrameView* view = m_frame->view(); |
|
384 if (!view) |
|
385 return; |
|
386 RenderObject* renderer = m_frame->renderer(); |
|
387 if (!renderer) |
|
388 return; |
|
389 RenderLayer* layer = renderer->layer(); |
|
390 if (!layer) |
|
391 return; |
|
392 |
|
393 HitTestResult result(view->windowToContents(m_currentMousePosition)); |
|
394 layer->hitTest(HitTestRequest(true, true, true), result); |
|
395 updateSelectionForMouseDrag(result.innerNode(), result.localPoint()); |
|
396 } |
|
397 |
|
398 void EventHandler::updateSelectionForMouseDrag(Node* targetNode, const IntPoint& localPoint) |
|
399 { |
|
400 if (!m_mouseDownMayStartSelect) |
|
401 return; |
|
402 |
|
403 if (!targetNode) |
|
404 return; |
|
405 |
|
406 RenderObject* targetRenderer = targetNode->renderer(); |
|
407 if (!targetRenderer) |
|
408 return; |
|
409 |
|
410 if (!canMouseDragExtendSelect(targetNode)) |
|
411 return; |
|
412 |
|
413 VisiblePosition targetPosition(targetRenderer->positionForPoint(localPoint)); |
|
414 |
|
415 // Don't modify the selection if we're not on a node. |
|
416 if (targetPosition.isNull()) |
|
417 return; |
|
418 |
|
419 // Restart the selection if this is the first mouse move. This work is usually |
|
420 // done in handleMousePressEvent, but not if the mouse press was on an existing selection. |
|
421 Selection newSelection = m_frame->selectionController()->selection(); |
|
422 |
|
423 #if ENABLE(SVG) |
|
424 // Special case to limit selection to the containing block for SVG text. |
|
425 // FIXME: Isn't there a better non-SVG-specific way to do this? |
|
426 if (Node* selectionBaseNode = newSelection.base().node()) |
|
427 if (RenderObject* selectionBaseRenderer = selectionBaseNode->renderer()) |
|
428 if (selectionBaseRenderer->isSVGText()) |
|
429 if (targetNode->renderer()->containingBlock() != selectionBaseRenderer->containingBlock()) |
|
430 return; |
|
431 #endif |
|
432 |
|
433 if (!m_beganSelectingText) { |
|
434 m_beganSelectingText = true; |
|
435 newSelection = Selection(targetPosition); |
|
436 } |
|
437 |
|
438 newSelection.setExtent(targetPosition); |
|
439 if (m_frame->selectionGranularity() != CharacterGranularity) |
|
440 newSelection.expandUsingGranularity(m_frame->selectionGranularity()); |
|
441 |
|
442 if (m_frame->shouldChangeSelection(newSelection)) { |
|
443 m_frame->selectionController()->setLastChangeWasHorizontalExtension(false); |
|
444 m_frame->selectionController()->setSelection(newSelection); |
|
445 } |
|
446 } |
|
447 |
|
448 bool EventHandler::handleMouseUp(const MouseEventWithHitTestResults& event) |
|
449 { |
|
450 if (eventLoopHandleMouseUp(event)) |
|
451 return true; |
|
452 |
|
453 // If this was the first click in the window, we don't even want to clear the selection. |
|
454 // This case occurs when the user clicks on a draggable element, since we have to process |
|
455 // the mouse down and drag events to see if we might start a drag. For other first clicks |
|
456 // in a window, we just don't acceptFirstMouse, and the whole down-drag-up sequence gets |
|
457 // ignored upstream of this layer. |
|
458 return eventActivatedView(event.event()); |
|
459 } |
|
460 |
|
461 bool EventHandler::handleMouseReleaseEvent(const MouseEventWithHitTestResults& event) |
|
462 { |
|
463 stopAutoscrollTimer(); |
|
464 |
|
465 if (handleMouseUp(event)) |
|
466 return true; |
|
467 |
|
468 // Used to prevent mouseMoveEvent from initiating a drag before |
|
469 // the mouse is pressed again. |
|
470 m_frame->selectionController()->setCaretBlinkingSuspended(false); |
|
471 m_mousePressed = false; |
|
472 m_mouseDownMayStartDrag = false; |
|
473 m_mouseDownMayStartSelect = false; |
|
474 m_mouseDownMayStartAutoscroll = false; |
|
475 m_mouseDownWasInSubframe = false; |
|
476 |
|
477 bool handled = false; |
|
478 |
|
479 // Clear the selection if the mouse didn't move after the last mouse press. |
|
480 // We do this so when clicking on the selection, the selection goes away. |
|
481 // However, if we are editing, place the caret. |
|
482 if (m_mouseDownWasSingleClickInSelection && !m_beganSelectingText |
|
483 && m_dragStartPos == event.event().pos() |
|
484 && m_frame->selectionController()->isRange()) { |
|
485 Selection newSelection; |
|
486 Node *node = event.targetNode(); |
|
487 if (node && node->isContentEditable() && node->renderer()) { |
|
488 VisiblePosition pos = node->renderer()->positionForPoint(event.localPoint()); |
|
489 newSelection = Selection(pos); |
|
490 } |
|
491 if (m_frame->shouldChangeSelection(newSelection)) |
|
492 m_frame->selectionController()->setSelection(newSelection); |
|
493 |
|
494 handled = true; |
|
495 } |
|
496 |
|
497 m_frame->notifyRendererOfSelectionChange(true); |
|
498 |
|
499 m_frame->selectionController()->selectFrameElementInParentIfFullySelected(); |
|
500 |
|
501 return handled; |
|
502 } |
|
503 |
|
504 void EventHandler::handleAutoscroll(RenderObject* renderer) |
|
505 { |
|
506 if (m_autoscrollTimer.isActive()) |
|
507 return; |
|
508 setAutoscrollRenderer(renderer); |
|
509 startAutoscrollTimer(); |
|
510 } |
|
511 |
|
512 void EventHandler::autoscrollTimerFired(Timer<EventHandler>*) |
|
513 { |
|
514 if (!m_mousePressed) { |
|
515 stopAutoscrollTimer(); |
|
516 return; |
|
517 } |
|
518 if (RenderObject* r = autoscrollRenderer()) |
|
519 r->autoscroll(); |
|
520 } |
|
521 |
|
522 RenderObject* EventHandler::autoscrollRenderer() const |
|
523 { |
|
524 return m_autoscrollRenderer; |
|
525 } |
|
526 |
|
527 void EventHandler::setAutoscrollRenderer(RenderObject* renderer) |
|
528 { |
|
529 m_autoscrollRenderer = renderer; |
|
530 } |
|
531 |
|
532 void EventHandler::allowDHTMLDrag(bool& flagDHTML, bool& flagUA) const |
|
533 { |
|
534 if (!m_frame || !m_frame->document()) { |
|
535 flagDHTML = false; |
|
536 flagUA = false; |
|
537 } |
|
538 |
|
539 unsigned mask = m_frame->page()->dragController()->delegateDragSourceAction(m_frame->view()->contentsToWindow(m_mouseDownPos)); |
|
540 flagDHTML = (mask & DragSourceActionDHTML) != DragSourceActionNone; |
|
541 flagUA = ((mask & DragSourceActionImage) || (mask & DragSourceActionLink) || (mask & DragSourceActionSelection)); |
|
542 } |
|
543 |
|
544 HitTestResult EventHandler::hitTestResultAtPoint(const IntPoint& point, bool allowShadowContent) |
|
545 { |
|
546 HitTestResult result(point); |
|
547 if (!m_frame->renderer()) |
|
548 return result; |
|
549 m_frame->renderer()->layer()->hitTest(HitTestRequest(true, true), result); |
|
550 |
|
551 IntPoint widgetPoint(point); |
|
552 while (true) { |
|
553 Node* n = result.innerNode(); |
|
554 if (!n || !n->renderer() || !n->renderer()->isWidget()) |
|
555 break; |
|
556 Widget* widget = static_cast<RenderWidget*>(n->renderer())->widget(); |
|
557 if (!widget || !widget->isFrameView()) |
|
558 break; |
|
559 Frame* frame = static_cast<HTMLFrameElementBase*>(n)->contentFrame(); |
|
560 if (!frame || !frame->renderer()) |
|
561 break; |
|
562 int absX, absY; |
|
563 n->renderer()->absolutePosition(absX, absY, true); |
|
564 FrameView* view = static_cast<FrameView*>(widget); |
|
565 widgetPoint.move(view->contentsX() - absX, view->contentsY() - absY); |
|
566 HitTestResult widgetHitTestResult(widgetPoint); |
|
567 frame->renderer()->layer()->hitTest(HitTestRequest(true, true), widgetHitTestResult); |
|
568 result = widgetHitTestResult; |
|
569 } |
|
570 |
|
571 if (!allowShadowContent) |
|
572 result.setToNonShadowAncestor(); |
|
573 |
|
574 return result; |
|
575 } |
|
576 |
|
577 |
|
578 void EventHandler::startAutoscrollTimer() |
|
579 { |
|
580 m_autoscrollTimer.startRepeating(autoscrollInterval); |
|
581 } |
|
582 |
|
583 void EventHandler::stopAutoscrollTimer(bool rendererIsBeingDestroyed) |
|
584 { |
|
585 if (m_mouseDownWasInSubframe) { |
|
586 if (Frame* subframe = subframeForTargetNode(m_mousePressNode.get())) |
|
587 subframe->eventHandler()->stopAutoscrollTimer(rendererIsBeingDestroyed); |
|
588 return; |
|
589 } |
|
590 |
|
591 if (!rendererIsBeingDestroyed && autoscrollRenderer()) |
|
592 autoscrollRenderer()->stopAutoscroll(); |
|
593 setAutoscrollRenderer(0); |
|
594 m_autoscrollTimer.stop(); |
|
595 } |
|
596 |
|
597 Node* EventHandler::mousePressNode() const |
|
598 { |
|
599 return m_mousePressNode.get(); |
|
600 } |
|
601 |
|
602 void EventHandler::setMousePressNode(PassRefPtr<Node> node) |
|
603 { |
|
604 m_mousePressNode = node; |
|
605 } |
|
606 |
|
607 bool EventHandler::scrollOverflow(ScrollDirection direction, ScrollGranularity granularity) |
|
608 { |
|
609 if (!m_frame->document()) |
|
610 return false; |
|
611 |
|
612 Node* node = m_frame->document()->focusedNode(); |
|
613 if (!node) |
|
614 node = m_mousePressNode.get(); |
|
615 |
|
616 if (node) { |
|
617 RenderObject *r = node->renderer(); |
|
618 if (r && !r->isListBox()) |
|
619 return r->scroll(direction, granularity); |
|
620 } |
|
621 |
|
622 return false; |
|
623 } |
|
624 |
|
625 IntPoint EventHandler::currentMousePosition() const |
|
626 { |
|
627 return m_currentMousePosition; |
|
628 } |
|
629 |
|
630 Frame* subframeForTargetNode(Node* node) |
|
631 { |
|
632 if (!node) |
|
633 return 0; |
|
634 |
|
635 RenderObject* renderer = node->renderer(); |
|
636 if (!renderer || !renderer->isWidget()) |
|
637 return 0; |
|
638 |
|
639 Widget* widget = static_cast<RenderWidget*>(renderer)->widget(); |
|
640 if (!widget || !widget->isFrameView()) |
|
641 return 0; |
|
642 |
|
643 return static_cast<FrameView*>(widget)->frame(); |
|
644 } |
|
645 |
|
646 static bool isSubmitImage(Node* node) |
|
647 { |
|
648 return node && node->hasTagName(inputTag) |
|
649 && static_cast<HTMLInputElement*>(node)->inputType() == HTMLInputElement::IMAGE; |
|
650 } |
|
651 |
|
652 // Returns true if the node's editable block is not current focused for editing |
|
653 static bool nodeIsNotBeingEdited(Node* node, Frame* frame) |
|
654 { |
|
655 return frame->selectionController()->rootEditableElement() != node->rootEditableElement(); |
|
656 } |
|
657 |
|
658 Cursor EventHandler::selectCursor(const MouseEventWithHitTestResults& event, PlatformScrollbar* scrollbar) |
|
659 { |
|
660 // During selection, use an I-beam no matter what we're over. |
|
661 // If you're capturing mouse events for a particular node, don't treat this as a selection. |
|
662 if (m_mousePressed && m_mouseDownMayStartSelect && m_frame->selectionController()->isCaretOrRange() && !m_capturingMouseEventsNode) |
|
663 return iBeamCursor(); |
|
664 |
|
665 Node* node = event.targetNode(); |
|
666 RenderObject* renderer = node ? node->renderer() : 0; |
|
667 RenderStyle* style = renderer ? renderer->style() : 0; |
|
668 |
|
669 if (style && style->cursors()) { |
|
670 const CursorList* cursors = style->cursors(); |
|
671 for (unsigned i = 0; i < cursors->size(); ++i) { |
|
672 CachedImage* cimage = (*cursors)[i].cursorImage; |
|
673 IntPoint hotSpot = (*cursors)[i].hotSpot; |
|
674 #if ENABLE(SVG) |
|
675 if (!cimage) { |
|
676 Element* e = node->document()->getElementById((*cursors)[i].cursorFragmentId); |
|
677 if (e && e->hasTagName(cursorTag)) { |
|
678 hotSpot.setX(int(static_cast<SVGCursorElement*>(e)->x().value())); |
|
679 hotSpot.setY(int(static_cast<SVGCursorElement*>(e)->y().value())); |
|
680 cimage = static_cast<SVGCursorElement*>(e)->cachedImage(); |
|
681 } |
|
682 } |
|
683 #endif |
|
684 if (!cimage) |
|
685 continue; |
|
686 if (cimage->image()->isNull()) |
|
687 break; |
|
688 if (!cimage->errorOccurred()) |
|
689 return Cursor(cimage->image(), hotSpot); |
|
690 } |
|
691 } |
|
692 |
|
693 switch (style ? style->cursor() : CURSOR_AUTO) { |
|
694 case CURSOR_AUTO: { |
|
695 bool editable = (node && node->isContentEditable()); |
|
696 bool editableLinkEnabled = false; |
|
697 |
|
698 // If the link is editable, then we need to check the settings to see whether or not the link should be followed |
|
699 if (editable) { |
|
700 ASSERT(m_frame->settings()); |
|
701 switch(m_frame->settings()->editableLinkBehavior()) { |
|
702 default: |
|
703 case EditableLinkDefaultBehavior: |
|
704 case EditableLinkAlwaysLive: |
|
705 editableLinkEnabled = true; |
|
706 break; |
|
707 |
|
708 case EditableLinkNeverLive: |
|
709 editableLinkEnabled = false; |
|
710 break; |
|
711 |
|
712 case EditableLinkLiveWhenNotFocused: |
|
713 editableLinkEnabled = nodeIsNotBeingEdited(node, m_frame) || event.event().shiftKey(); |
|
714 break; |
|
715 |
|
716 case EditableLinkOnlyLiveWithShiftKey: |
|
717 editableLinkEnabled = event.event().shiftKey(); |
|
718 break; |
|
719 } |
|
720 } |
|
721 |
|
722 if ((event.isOverLink() || isSubmitImage(node)) && (!editable || editableLinkEnabled)) |
|
723 return handCursor(); |
|
724 RenderLayer* layer = renderer ? renderer->enclosingLayer() : 0; |
|
725 bool inResizer = false; |
|
726 if (m_frame->view() && layer && layer->isPointInResizeControl(m_frame->view()->windowToContents(event.event().pos()))) |
|
727 inResizer = true; |
|
728 if ((editable || (renderer && renderer->isText() && node->canStartSelection())) && !inResizer && !scrollbar) |
|
729 return iBeamCursor(); |
|
730 return pointerCursor(); |
|
731 } |
|
732 case CURSOR_CROSS: |
|
733 return crossCursor(); |
|
734 case CURSOR_POINTER: |
|
735 return handCursor(); |
|
736 case CURSOR_MOVE: |
|
737 return moveCursor(); |
|
738 case CURSOR_ALL_SCROLL: |
|
739 return moveCursor(); |
|
740 case CURSOR_E_RESIZE: |
|
741 return eastResizeCursor(); |
|
742 case CURSOR_W_RESIZE: |
|
743 return westResizeCursor(); |
|
744 case CURSOR_N_RESIZE: |
|
745 return northResizeCursor(); |
|
746 case CURSOR_S_RESIZE: |
|
747 return southResizeCursor(); |
|
748 case CURSOR_NE_RESIZE: |
|
749 return northEastResizeCursor(); |
|
750 case CURSOR_SW_RESIZE: |
|
751 return southWestResizeCursor(); |
|
752 case CURSOR_NW_RESIZE: |
|
753 return northWestResizeCursor(); |
|
754 case CURSOR_SE_RESIZE: |
|
755 return southEastResizeCursor(); |
|
756 case CURSOR_NS_RESIZE: |
|
757 return northSouthResizeCursor(); |
|
758 case CURSOR_EW_RESIZE: |
|
759 return eastWestResizeCursor(); |
|
760 case CURSOR_NESW_RESIZE: |
|
761 return northEastSouthWestResizeCursor(); |
|
762 case CURSOR_NWSE_RESIZE: |
|
763 return northWestSouthEastResizeCursor(); |
|
764 case CURSOR_COL_RESIZE: |
|
765 return columnResizeCursor(); |
|
766 case CURSOR_ROW_RESIZE: |
|
767 return rowResizeCursor(); |
|
768 case CURSOR_TEXT: |
|
769 return iBeamCursor(); |
|
770 case CURSOR_WAIT: |
|
771 return waitCursor(); |
|
772 case CURSOR_HELP: |
|
773 return helpCursor(); |
|
774 case CURSOR_VERTICAL_TEXT: |
|
775 return verticalTextCursor(); |
|
776 case CURSOR_CELL: |
|
777 return cellCursor(); |
|
778 case CURSOR_CONTEXT_MENU: |
|
779 return contextMenuCursor(); |
|
780 case CURSOR_PROGRESS: |
|
781 return progressCursor(); |
|
782 case CURSOR_NO_DROP: |
|
783 return noDropCursor(); |
|
784 case CURSOR_ALIAS: |
|
785 return aliasCursor(); |
|
786 case CURSOR_COPY: |
|
787 return copyCursor(); |
|
788 case CURSOR_NONE: |
|
789 return noneCursor(); |
|
790 case CURSOR_NOT_ALLOWED: |
|
791 return notAllowedCursor(); |
|
792 case CURSOR_DEFAULT: |
|
793 return pointerCursor(); |
|
794 case CURSOR_WEBKIT_ZOOM_IN: |
|
795 return zoomInCursor(); |
|
796 case CURSOR_WEBKIT_ZOOM_OUT: |
|
797 return zoomOutCursor(); |
|
798 } |
|
799 return pointerCursor(); |
|
800 } |
|
801 |
|
802 bool EventHandler::handleMousePressEvent(const PlatformMouseEvent& mouseEvent) |
|
803 { |
|
804 if (!m_frame->document()) |
|
805 return false; |
|
806 |
|
807 RefPtr<FrameView> protector(m_frame->view()); |
|
808 |
|
809 m_mousePressed = true; |
|
810 m_currentMousePosition = mouseEvent.pos(); |
|
811 m_mouseDownTimestamp = mouseEvent.timestamp(); |
|
812 m_mouseDownMayStartDrag = false; |
|
813 m_mouseDownMayStartSelect = false; |
|
814 m_mouseDownMayStartAutoscroll = false; |
|
815 m_mouseDownPos = m_frame->view()->windowToContents(mouseEvent.pos()); |
|
816 m_mouseDownWasInSubframe = false; |
|
817 |
|
818 MouseEventWithHitTestResults mev = prepareMouseEvent(HitTestRequest(false, true), mouseEvent); |
|
819 |
|
820 if (!mev.targetNode()) { |
|
821 invalidateClick(); |
|
822 return false; |
|
823 } |
|
824 |
|
825 m_mousePressNode = mev.targetNode(); |
|
826 |
|
827 Frame* subframe = subframeForTargetNode(mev.targetNode()); |
|
828 if (subframe && passMousePressEventToSubframe(mev, subframe)) { |
|
829 // Start capturing future events for this frame. We only do this if we didn't clear |
|
830 // the m_mousePressed flag, which may happen if an AppKit widget entered a modal event loop. |
|
831 if (m_mousePressed) |
|
832 m_capturingMouseEventsNode = mev.targetNode(); |
|
833 invalidateClick(); |
|
834 return true; |
|
835 } |
|
836 |
|
837 m_clickCount = mouseEvent.clickCount(); |
|
838 m_clickNode = mev.targetNode(); |
|
839 |
|
840 RenderLayer* layer = m_clickNode->renderer() ? m_clickNode->renderer()->enclosingLayer() : 0; |
|
841 IntPoint p = m_frame->view()->windowToContents(mouseEvent.pos()); |
|
842 if (layer && layer->isPointInResizeControl(p)) { |
|
843 layer->setInResizeMode(true); |
|
844 m_resizeLayer = layer; |
|
845 m_offsetFromResizeCorner = layer->offsetFromResizeCorner(p); |
|
846 invalidateClick(); |
|
847 return true; |
|
848 } |
|
849 |
|
850 bool swallowEvent = dispatchMouseEvent(mousedownEvent, mev.targetNode(), true, m_clickCount, mouseEvent, true); |
|
851 |
|
852 // If the hit testing originally determined the event was in a scrollbar, refetch the MouseEventWithHitTestResults |
|
853 // in case the scrollbar widget was destroyed when the mouse event was handled. |
|
854 if (mev.scrollbar()) |
|
855 mev = prepareMouseEvent(HitTestRequest(true, true), mouseEvent); |
|
856 |
|
857 if (swallowEvent) { |
|
858 // scrollbars should get events anyway, even disabled controls might be scrollable |
|
859 if (mev.scrollbar()) |
|
860 passMousePressEventToScrollbar(mev, mev.scrollbar()); |
|
861 } else { |
|
862 // Refetch the event target node if it currently is the shadow node inside an <input> element. |
|
863 // If a mouse event handler changes the input element type to one that has a widget associated, |
|
864 // we'd like to EventHandler::handleMousePressEvent to pass the event to the widget and thus the |
|
865 // event target node can't still be the shadow node. |
|
866 if (mev.targetNode()->isShadowNode() && mev.targetNode()->shadowParentNode()->hasTagName(inputTag)) |
|
867 mev = prepareMouseEvent(HitTestRequest(true, true), mouseEvent); |
|
868 |
|
869 PlatformScrollbar* scrollbar = m_frame->view()->scrollbarUnderMouse(mouseEvent); |
|
870 if (!scrollbar) |
|
871 scrollbar = mev.scrollbar(); |
|
872 if (scrollbar && passMousePressEventToScrollbar(mev, scrollbar)) |
|
873 swallowEvent = true; |
|
874 else |
|
875 swallowEvent = handleMousePressEvent(mev); |
|
876 } |
|
877 |
|
878 return swallowEvent; |
|
879 } |
|
880 |
|
881 // This method only exists for platforms that don't know how to deliver |
|
882 bool EventHandler::handleMouseDoubleClickEvent(const PlatformMouseEvent& mouseEvent) |
|
883 { |
|
884 if (!m_frame->document()) |
|
885 return false; |
|
886 |
|
887 RefPtr<FrameView> protector(m_frame->view()); |
|
888 |
|
889 // We get this instead of a second mouse-up |
|
890 m_mousePressed = false; |
|
891 m_currentMousePosition = mouseEvent.pos(); |
|
892 |
|
893 MouseEventWithHitTestResults mev = prepareMouseEvent(HitTestRequest(false, true), mouseEvent); |
|
894 Frame* subframe = subframeForTargetNode(mev.targetNode()); |
|
895 if (subframe && passMousePressEventToSubframe(mev, subframe)) { |
|
896 m_capturingMouseEventsNode = 0; |
|
897 return true; |
|
898 } |
|
899 |
|
900 m_clickCount = mouseEvent.clickCount(); |
|
901 bool swallowMouseUpEvent = dispatchMouseEvent(mouseupEvent, mev.targetNode(), true, m_clickCount, mouseEvent, false); |
|
902 |
|
903 bool swallowClickEvent = false; |
|
904 // Don't ever dispatch click events for right clicks |
|
905 if (mouseEvent.button() != RightButton && mev.targetNode() == m_clickNode) |
|
906 swallowClickEvent = dispatchMouseEvent(clickEvent, mev.targetNode(), true, m_clickCount, mouseEvent, true); |
|
907 |
|
908 bool swallowMouseReleaseEvent = false; |
|
909 if (!swallowMouseUpEvent) |
|
910 swallowMouseReleaseEvent = handleMouseReleaseEvent(mev); |
|
911 |
|
912 invalidateClick(); |
|
913 |
|
914 return swallowMouseUpEvent || swallowClickEvent || swallowMouseReleaseEvent; |
|
915 } |
|
916 |
|
917 bool EventHandler::mouseMoved(const PlatformMouseEvent& event) |
|
918 { |
|
919 HitTestResult hoveredNode = HitTestResult(IntPoint()); |
|
920 bool result = handleMouseMoveEvent(event, &hoveredNode); |
|
921 |
|
922 Page* page = m_frame->page(); |
|
923 if (!page) |
|
924 return result; |
|
925 |
|
926 hoveredNode.setToNonShadowAncestor(); |
|
927 page->chrome()->mouseDidMoveOverElement(hoveredNode, event.modifierFlags()); |
|
928 page->chrome()->setToolTip(hoveredNode); |
|
929 return result; |
|
930 } |
|
931 |
|
932 bool EventHandler::handleMouseMoveEvent(const PlatformMouseEvent& mouseEvent, HitTestResult* hoveredNode) |
|
933 { |
|
934 // in Radar 3703768 we saw frequent crashes apparently due to the |
|
935 // part being null here, which seems impossible, so check for nil |
|
936 // but also assert so that we can try to figure this out in debug |
|
937 // builds, if it happens. |
|
938 ASSERT(m_frame); |
|
939 if (!m_frame || !m_frame->document()) |
|
940 return false; |
|
941 |
|
942 RefPtr<FrameView> protector(m_frame->view()); |
|
943 m_currentMousePosition = mouseEvent.pos(); |
|
944 |
|
945 if (m_hoverTimer.isActive()) |
|
946 m_hoverTimer.stop(); |
|
947 |
|
948 #if ENABLE(SVG) |
|
949 if (m_svgPan) { |
|
950 static_cast<SVGDocument*>(m_frame->document())->updatePan(m_currentMousePosition); |
|
951 return true; |
|
952 } |
|
953 #endif |
|
954 |
|
955 if (m_frameSetBeingResized) |
|
956 return dispatchMouseEvent(mousemoveEvent, m_frameSetBeingResized.get(), false, 0, mouseEvent, false); |
|
957 |
|
958 // Send events right to a scrollbar if the mouse is pressed. |
|
959 if (m_lastScrollbarUnderMouse && m_mousePressed) |
|
960 return m_lastScrollbarUnderMouse->handleMouseMoveEvent(mouseEvent); |
|
961 |
|
962 // Treat mouse move events while the mouse is pressed as "read-only" in prepareMouseEvent |
|
963 // if we are allowed to select. |
|
964 // This means that :hover and :active freeze in the state they were in when the mouse |
|
965 // was pressed, rather than updating for nodes the mouse moves over as you hold the mouse down. |
|
966 HitTestRequest request(m_mousePressed && m_mouseDownMayStartSelect, m_mousePressed, true); |
|
967 MouseEventWithHitTestResults mev = prepareMouseEvent(request, mouseEvent); |
|
968 if (hoveredNode) |
|
969 *hoveredNode = mev.hitTestResult(); |
|
970 |
|
971 PlatformScrollbar* scrollbar = 0; |
|
972 |
|
973 if (m_resizeLayer && m_resizeLayer->inResizeMode()) |
|
974 m_resizeLayer->resize(mouseEvent, m_offsetFromResizeCorner); |
|
975 else { |
|
976 if (m_frame->view()) |
|
977 scrollbar = m_frame->view()->scrollbarUnderMouse(mouseEvent); |
|
978 |
|
979 if (!scrollbar) |
|
980 scrollbar = mev.scrollbar(); |
|
981 |
|
982 if (m_lastScrollbarUnderMouse != scrollbar) { |
|
983 // Send mouse exited to the old scrollbar. |
|
984 if (m_lastScrollbarUnderMouse) |
|
985 m_lastScrollbarUnderMouse->handleMouseOutEvent(mouseEvent); |
|
986 m_lastScrollbarUnderMouse = scrollbar; |
|
987 } |
|
988 } |
|
989 |
|
990 bool swallowEvent = false; |
|
991 Node* targetNode = m_capturingMouseEventsNode ? m_capturingMouseEventsNode.get() : mev.targetNode(); |
|
992 RefPtr<Frame> newSubframe = subframeForTargetNode(targetNode); |
|
993 |
|
994 // 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. |
|
995 if (m_lastMouseMoveEventSubframe && m_lastMouseMoveEventSubframe->tree()->isDescendantOf(m_frame) && m_lastMouseMoveEventSubframe != newSubframe) |
|
996 passMouseMoveEventToSubframe(mev, m_lastMouseMoveEventSubframe.get()); |
|
997 |
|
998 if (newSubframe) { |
|
999 // Update over/out state before passing the event to the subframe. |
|
1000 updateMouseEventTargetNode(mev.targetNode(), mouseEvent, true); |
|
1001 swallowEvent |= passMouseMoveEventToSubframe(mev, newSubframe.get(), hoveredNode); |
|
1002 } else { |
|
1003 if (scrollbar && !m_mousePressed) |
|
1004 scrollbar->handleMouseMoveEvent(mouseEvent); // Handle hover effects on platforms that support visual feedback on scrollbar hovering. |
|
1005 if ((!m_resizeLayer || !m_resizeLayer->inResizeMode()) && m_frame->view()) |
|
1006 m_frame->view()->setCursor(selectCursor(mev, scrollbar)); |
|
1007 } |
|
1008 |
|
1009 m_lastMouseMoveEventSubframe = newSubframe; |
|
1010 |
|
1011 if (swallowEvent) |
|
1012 return true; |
|
1013 |
|
1014 swallowEvent = dispatchMouseEvent(mousemoveEvent, mev.targetNode(), false, 0, mouseEvent, true); |
|
1015 if (!swallowEvent) |
|
1016 swallowEvent = handleMouseDraggedEvent(mev); |
|
1017 |
|
1018 return swallowEvent; |
|
1019 } |
|
1020 |
|
1021 void EventHandler::invalidateClick() |
|
1022 { |
|
1023 m_clickCount = 0; |
|
1024 m_clickNode = 0; |
|
1025 } |
|
1026 |
|
1027 bool EventHandler::handleMouseReleaseEvent(const PlatformMouseEvent& mouseEvent) |
|
1028 { |
|
1029 if (!m_frame->document()) |
|
1030 return false; |
|
1031 |
|
1032 RefPtr<FrameView> protector(m_frame->view()); |
|
1033 |
|
1034 m_mousePressed = false; |
|
1035 m_currentMousePosition = mouseEvent.pos(); |
|
1036 |
|
1037 #if ENABLE(SVG) |
|
1038 if (m_svgPan) { |
|
1039 m_svgPan = false; |
|
1040 return true; |
|
1041 } |
|
1042 #endif |
|
1043 |
|
1044 if (m_frameSetBeingResized) |
|
1045 return dispatchMouseEvent(mouseupEvent, m_frameSetBeingResized.get(), true, m_clickCount, mouseEvent, false); |
|
1046 |
|
1047 if (m_lastScrollbarUnderMouse) { |
|
1048 invalidateClick(); |
|
1049 return m_lastScrollbarUnderMouse->handleMouseReleaseEvent(mouseEvent); |
|
1050 } |
|
1051 |
|
1052 MouseEventWithHitTestResults mev = prepareMouseEvent(HitTestRequest(false, false, false, true), mouseEvent); |
|
1053 Node* targetNode = m_capturingMouseEventsNode.get() ? m_capturingMouseEventsNode.get() : mev.targetNode(); |
|
1054 Frame* subframe = subframeForTargetNode(targetNode); |
|
1055 if (subframe && passMouseReleaseEventToSubframe(mev, subframe)) { |
|
1056 m_capturingMouseEventsNode = 0; |
|
1057 return true; |
|
1058 } |
|
1059 |
|
1060 bool swallowMouseUpEvent = dispatchMouseEvent(mouseupEvent, mev.targetNode(), true, m_clickCount, mouseEvent, false); |
|
1061 |
|
1062 // Don't ever dispatch click events for right clicks |
|
1063 bool swallowClickEvent = false; |
|
1064 if (m_clickCount > 0 && mouseEvent.button() != RightButton && mev.targetNode() == m_clickNode) |
|
1065 #if PLATFORM(SYMBIAN) |
|
1066 { |
|
1067 int clickcount = m_clickCount; |
|
1068 m_clickCount = 0; |
|
1069 swallowClickEvent = dispatchMouseEvent(clickEvent, mev.targetNode(), true, clickcount, mouseEvent, true); |
|
1070 } |
|
1071 #else |
|
1072 swallowClickEvent = dispatchMouseEvent(clickEvent, mev.targetNode(), true, m_clickCount, mouseEvent, true); |
|
1073 #endif |
|
1074 if (m_resizeLayer) { |
|
1075 m_resizeLayer->setInResizeMode(false); |
|
1076 m_resizeLayer = 0; |
|
1077 } |
|
1078 |
|
1079 bool swallowMouseReleaseEvent = false; |
|
1080 if (!swallowMouseUpEvent) |
|
1081 swallowMouseReleaseEvent = handleMouseReleaseEvent(mev); |
|
1082 |
|
1083 invalidateClick(); |
|
1084 |
|
1085 return swallowMouseUpEvent || swallowClickEvent || swallowMouseReleaseEvent; |
|
1086 } |
|
1087 |
|
1088 bool EventHandler::dispatchDragEvent(const AtomicString& eventType, Node* dragTarget, const PlatformMouseEvent& event, Clipboard* clipboard) |
|
1089 { |
|
1090 IntPoint contentsPos = m_frame->view()->windowToContents(event.pos()); |
|
1091 |
|
1092 RefPtr<MouseEvent> me = new MouseEvent(eventType, |
|
1093 true, true, m_frame->document()->defaultView(), |
|
1094 0, event.globalX(), event.globalY(), contentsPos.x(), contentsPos.y(), |
|
1095 event.ctrlKey(), event.altKey(), event.shiftKey(), event.metaKey(), |
|
1096 0, 0, clipboard); |
|
1097 |
|
1098 ExceptionCode ec = 0; |
|
1099 EventTargetNodeCast(dragTarget)->dispatchEvent(me.get(), ec, true); |
|
1100 return me->defaultPrevented(); |
|
1101 } |
|
1102 |
|
1103 bool EventHandler::updateDragAndDrop(const PlatformMouseEvent& event, Clipboard* clipboard) |
|
1104 { |
|
1105 bool accept = false; |
|
1106 |
|
1107 if (!m_frame->document()) |
|
1108 return false; |
|
1109 |
|
1110 if (!m_frame->view()) |
|
1111 return false; |
|
1112 |
|
1113 MouseEventWithHitTestResults mev = prepareMouseEvent(HitTestRequest(true, false), event); |
|
1114 |
|
1115 // Drag events should never go to text nodes (following IE, and proper mouseover/out dispatch) |
|
1116 Node* newTarget = mev.targetNode(); |
|
1117 if (newTarget && newTarget->isTextNode()) |
|
1118 newTarget = newTarget->parentNode(); |
|
1119 if (newTarget) |
|
1120 newTarget = newTarget->shadowAncestorNode(); |
|
1121 |
|
1122 if (m_dragTarget != newTarget) { |
|
1123 // FIXME: this ordering was explicitly chosen to match WinIE. However, |
|
1124 // it is sometimes incorrect when dragging within subframes, as seen with |
|
1125 // LayoutTests/fast/events/drag-in-frames.html. |
|
1126 if (newTarget) |
|
1127 if (newTarget->hasTagName(frameTag) || newTarget->hasTagName(iframeTag)) |
|
1128 accept = static_cast<HTMLFrameElementBase*>(newTarget)->contentFrame()->eventHandler()->updateDragAndDrop(event, clipboard); |
|
1129 else |
|
1130 accept = dispatchDragEvent(dragenterEvent, newTarget, event, clipboard); |
|
1131 |
|
1132 if (m_dragTarget) { |
|
1133 Frame* frame = (m_dragTarget->hasTagName(frameTag) || m_dragTarget->hasTagName(iframeTag)) |
|
1134 ? static_cast<HTMLFrameElementBase*>(m_dragTarget.get())->contentFrame() : 0; |
|
1135 if (frame) |
|
1136 accept = frame->eventHandler()->updateDragAndDrop(event, clipboard); |
|
1137 else |
|
1138 dispatchDragEvent(dragleaveEvent, m_dragTarget.get(), event, clipboard); |
|
1139 } |
|
1140 } else { |
|
1141 if (newTarget) |
|
1142 if (newTarget->hasTagName(frameTag) || newTarget->hasTagName(iframeTag)) |
|
1143 accept = static_cast<HTMLFrameElementBase*>(newTarget)->contentFrame()->eventHandler()->updateDragAndDrop(event, clipboard); |
|
1144 else |
|
1145 accept = dispatchDragEvent(dragoverEvent, newTarget, event, clipboard); |
|
1146 } |
|
1147 m_dragTarget = newTarget; |
|
1148 |
|
1149 return accept; |
|
1150 } |
|
1151 |
|
1152 void EventHandler::cancelDragAndDrop(const PlatformMouseEvent& event, Clipboard* clipboard) |
|
1153 { |
|
1154 if (m_dragTarget) { |
|
1155 Frame* frame = (m_dragTarget->hasTagName(frameTag) || m_dragTarget->hasTagName(iframeTag)) |
|
1156 ? static_cast<HTMLFrameElementBase*>(m_dragTarget.get())->contentFrame() : 0; |
|
1157 if (frame) |
|
1158 frame->eventHandler()->cancelDragAndDrop(event, clipboard); |
|
1159 else |
|
1160 dispatchDragEvent(dragleaveEvent, m_dragTarget.get(), event, clipboard); |
|
1161 } |
|
1162 m_dragTarget = 0; |
|
1163 } |
|
1164 |
|
1165 bool EventHandler::performDragAndDrop(const PlatformMouseEvent& event, Clipboard* clipboard) |
|
1166 { |
|
1167 bool accept = false; |
|
1168 if (m_dragTarget) { |
|
1169 Frame* frame = (m_dragTarget->hasTagName(frameTag) || m_dragTarget->hasTagName(iframeTag)) |
|
1170 ? static_cast<HTMLFrameElementBase*>(m_dragTarget.get())->contentFrame() : 0; |
|
1171 if (frame) |
|
1172 accept = frame->eventHandler()->performDragAndDrop(event, clipboard); |
|
1173 else |
|
1174 accept = dispatchDragEvent(dropEvent, m_dragTarget.get(), event, clipboard); |
|
1175 } |
|
1176 m_dragTarget = 0; |
|
1177 return accept; |
|
1178 } |
|
1179 |
|
1180 Node* EventHandler::nodeUnderMouse() const |
|
1181 { |
|
1182 return m_nodeUnderMouse.get(); |
|
1183 } |
|
1184 |
|
1185 void EventHandler::setCapturingMouseEventsNode(PassRefPtr<Node> n) |
|
1186 { |
|
1187 m_capturingMouseEventsNode = n; |
|
1188 } |
|
1189 |
|
1190 MouseEventWithHitTestResults EventHandler::prepareMouseEvent(const HitTestRequest& request, const PlatformMouseEvent& mev) |
|
1191 { |
|
1192 ASSERT(m_frame); |
|
1193 ASSERT(m_frame->document()); |
|
1194 |
|
1195 IntPoint documentPoint = m_frame->view()->windowToContents(mev.pos()); |
|
1196 return m_frame->document()->prepareMouseEvent(request, documentPoint, mev); |
|
1197 } |
|
1198 |
|
1199 void EventHandler::updateMouseEventTargetNode(Node* targetNode, const PlatformMouseEvent& mouseEvent, bool fireMouseOverOut) |
|
1200 { |
|
1201 Node* result = targetNode; |
|
1202 |
|
1203 // If we're capturing, we always go right to that node. |
|
1204 if (m_capturingMouseEventsNode) |
|
1205 result = m_capturingMouseEventsNode.get(); |
|
1206 |
|
1207 // If the target node is a text node, dispatch on the parent node - rdar://4196646 |
|
1208 if (result && result->isTextNode()) |
|
1209 result = result->parentNode(); |
|
1210 if (result) |
|
1211 result = result->shadowAncestorNode(); |
|
1212 |
|
1213 m_nodeUnderMouse = result; |
|
1214 |
|
1215 // Fire mouseout/mouseover if the mouse has shifted to a different node. |
|
1216 if (fireMouseOverOut) { |
|
1217 if (m_lastNodeUnderMouse && m_lastNodeUnderMouse->document() != m_frame->document()) { |
|
1218 m_lastNodeUnderMouse = 0; |
|
1219 m_lastScrollbarUnderMouse = 0; |
|
1220 } |
|
1221 |
|
1222 if (m_lastNodeUnderMouse != m_nodeUnderMouse) { |
|
1223 // send mouseout event to the old node |
|
1224 if (m_lastNodeUnderMouse) |
|
1225 EventTargetNodeCast(m_lastNodeUnderMouse.get())->dispatchMouseEvent(mouseEvent, mouseoutEvent, 0, m_nodeUnderMouse.get()); |
|
1226 // send mouseover event to the new node |
|
1227 if (m_nodeUnderMouse) |
|
1228 EventTargetNodeCast(m_nodeUnderMouse.get())->dispatchMouseEvent(mouseEvent, mouseoverEvent, 0, m_lastNodeUnderMouse.get()); |
|
1229 } |
|
1230 m_lastNodeUnderMouse = m_nodeUnderMouse; |
|
1231 } |
|
1232 } |
|
1233 |
|
1234 bool EventHandler::dispatchMouseEvent(const AtomicString& eventType, Node* targetNode, bool cancelable, int clickCount, const PlatformMouseEvent& mouseEvent, bool setUnder) |
|
1235 { |
|
1236 updateMouseEventTargetNode(targetNode, mouseEvent, setUnder); |
|
1237 |
|
1238 bool swallowEvent = false; |
|
1239 |
|
1240 if (m_nodeUnderMouse) |
|
1241 swallowEvent = EventTargetNodeCast(m_nodeUnderMouse.get())->dispatchMouseEvent(mouseEvent, eventType, clickCount); |
|
1242 |
|
1243 if (!swallowEvent && eventType == mousedownEvent) { |
|
1244 // Blur current focus node when a link/button is clicked; this |
|
1245 // is expected by some sites that rely on onChange handlers running |
|
1246 // from form fields before the button click is processed. |
|
1247 Node* node = m_nodeUnderMouse.get(); |
|
1248 RenderObject* renderer = node ? node->renderer() : 0; |
|
1249 |
|
1250 // Walk up the render tree to search for a node to focus. |
|
1251 // Walking up the DOM tree wouldn't work for shadow trees, like those behind the engine-based text fields. |
|
1252 while (renderer) { |
|
1253 node = renderer->element(); |
|
1254 if (node && node->isFocusable()) { |
|
1255 // To fix <rdar://problem/4895428> Can't drag selected ToDo, we don't focus a |
|
1256 // node on mouse down if it's selected and inside a focused node. It will be |
|
1257 // focused if the user does a mouseup over it, however, because the mouseup |
|
1258 // will set a selection inside it, which will call setFocuseNodeIfNeeded. |
|
1259 ExceptionCode ec = 0; |
|
1260 Node* n = node->isShadowNode() ? node->shadowParentNode() : node; |
|
1261 if (m_frame->selectionController()->isRange() && |
|
1262 m_frame->selectionController()->toRange()->compareNode(n, ec) == Range::NODE_INSIDE && |
|
1263 n->isDescendantOf(m_frame->document()->focusedNode())) |
|
1264 return false; |
|
1265 |
|
1266 break; |
|
1267 } |
|
1268 |
|
1269 renderer = renderer->parent(); |
|
1270 } |
|
1271 // If focus shift is blocked, we eat the event. Note we should never clear swallowEvent |
|
1272 // if the page already set it (e.g., by canceling default behavior). |
|
1273 if (node && node->isMouseFocusable()) { |
|
1274 if (!m_frame->page()->focusController()->setFocusedNode(node, m_frame)) |
|
1275 swallowEvent = true; |
|
1276 } else if (!node || !node->focused()) { |
|
1277 if (!m_frame->page()->focusController()->setFocusedNode(0, m_frame)) |
|
1278 swallowEvent = true; |
|
1279 } |
|
1280 } |
|
1281 |
|
1282 return swallowEvent; |
|
1283 } |
|
1284 |
|
1285 bool EventHandler::handleWheelEvent(PlatformWheelEvent& e) |
|
1286 { |
|
1287 Document* doc = m_frame->document(); |
|
1288 if (!doc) |
|
1289 return false; |
|
1290 |
|
1291 RenderObject* docRenderer = doc->renderer(); |
|
1292 if (!docRenderer) |
|
1293 return false; |
|
1294 |
|
1295 IntPoint vPoint = m_frame->view()->windowToContents(e.pos()); |
|
1296 |
|
1297 HitTestRequest request(true, false); |
|
1298 HitTestResult result(vPoint); |
|
1299 doc->renderer()->layer()->hitTest(request, result); |
|
1300 Node* node = result.innerNode(); |
|
1301 |
|
1302 if (node) { |
|
1303 // Figure out which view to send the event to. |
|
1304 RenderObject* target = node->renderer(); |
|
1305 |
|
1306 if (target && target->isWidget()) { |
|
1307 Widget* widget = static_cast<RenderWidget*>(target)->widget(); |
|
1308 |
|
1309 if (widget && passWheelEventToWidget(e, widget)) { |
|
1310 e.accept(); |
|
1311 return true; |
|
1312 } |
|
1313 } |
|
1314 |
|
1315 node = node->shadowAncestorNode(); |
|
1316 EventTargetNodeCast(node)->dispatchWheelEvent(e); |
|
1317 if (e.isAccepted()) |
|
1318 return true; |
|
1319 |
|
1320 if (node->renderer()) { |
|
1321 // Just break up into two scrolls if we need to. Diagonal movement on |
|
1322 // a MacBook pro is an example of a 2-dimensional mouse wheel event (where both deltaX and deltaY can be set). |
|
1323 float deltaX = e.isContinuous() ? e.continuousDeltaX() : e.deltaX(); |
|
1324 float deltaY = e.isContinuous() ? e.continuousDeltaY() : e.deltaY(); |
|
1325 if (deltaX && node->renderer()->scroll(deltaX < 0 ? ScrollRight : ScrollLeft, e.isContinuous() ? ScrollByPixel : ScrollByLine, |
|
1326 deltaX < 0 ? -deltaX : deltaX)) |
|
1327 e.accept(); |
|
1328 if (deltaY && node->renderer()->scroll(deltaY < 0 ? ScrollDown : ScrollUp, e.isContinuous() ? ScrollByPixel : ScrollByLine, |
|
1329 deltaY < 0 ? -deltaY : deltaY)) |
|
1330 e.accept(); |
|
1331 } |
|
1332 } |
|
1333 |
|
1334 if (!e.isAccepted()) |
|
1335 m_frame->view()->wheelEvent(e); |
|
1336 |
|
1337 return e.isAccepted(); |
|
1338 } |
|
1339 |
|
1340 bool EventHandler::sendContextMenuEvent(const PlatformMouseEvent& event) |
|
1341 { |
|
1342 Document* doc = m_frame->document(); |
|
1343 FrameView* v = m_frame->view(); |
|
1344 if (!doc || !v) |
|
1345 return false; |
|
1346 |
|
1347 bool swallowEvent; |
|
1348 IntPoint viewportPos = v->windowToContents(event.pos()); |
|
1349 MouseEventWithHitTestResults mev = doc->prepareMouseEvent(HitTestRequest(false, true), viewportPos, event); |
|
1350 |
|
1351 if (!m_frame->selectionController()->contains(viewportPos) && |
|
1352 // FIXME: In the editable case, word selection sometimes selects content that isn't underneath the mouse. |
|
1353 // If the selection is non-editable, we do word selection to make it easier to use the contextual menu items |
|
1354 // available for text selections. But only if we're above text. |
|
1355 (m_frame->selectionController()->isContentEditable() || mev.targetNode() && mev.targetNode()->isTextNode())) { |
|
1356 m_mouseDownMayStartSelect = true; // context menu events are always allowed to perform a selection |
|
1357 selectClosestWordFromMouseEvent(mev); |
|
1358 } |
|
1359 |
|
1360 swallowEvent = dispatchMouseEvent(contextmenuEvent, mev.targetNode(), true, 0, event, true); |
|
1361 |
|
1362 return swallowEvent; |
|
1363 } |
|
1364 |
|
1365 void EventHandler::scheduleHoverStateUpdate() |
|
1366 { |
|
1367 if (!m_hoverTimer.isActive()) |
|
1368 m_hoverTimer.startOneShot(0); |
|
1369 } |
|
1370 |
|
1371 // Whether or not a mouse down can begin the creation of a selection. Fires the selectStart event. |
|
1372 bool EventHandler::canMouseDownStartSelect(Node* node) |
|
1373 { |
|
1374 if (!node || !node->renderer()) |
|
1375 return true; |
|
1376 |
|
1377 // Some controls and images can't start a select on a mouse down. |
|
1378 if (!node->canStartSelection()) |
|
1379 return false; |
|
1380 |
|
1381 for (RenderObject* curr = node->renderer(); curr; curr = curr->parent()) |
|
1382 if (Node* node = curr->element()) |
|
1383 return EventTargetNodeCast(node)->dispatchHTMLEvent(selectstartEvent, true, true); |
|
1384 |
|
1385 return true; |
|
1386 } |
|
1387 |
|
1388 bool EventHandler::canMouseDragExtendSelect(Node* node) |
|
1389 { |
|
1390 if (!node || !node->renderer()) |
|
1391 return true; |
|
1392 |
|
1393 for (RenderObject* curr = node->renderer(); curr; curr = curr->parent()) |
|
1394 if (Node* node = curr->element()) |
|
1395 return EventTargetNodeCast(node)->dispatchHTMLEvent(selectstartEvent, true, true); |
|
1396 |
|
1397 return true; |
|
1398 } |
|
1399 |
|
1400 void EventHandler::setResizingFrameSet(HTMLFrameSetElement* frameSet) |
|
1401 { |
|
1402 m_frameSetBeingResized = frameSet; |
|
1403 } |
|
1404 |
|
1405 void EventHandler::hoverTimerFired(Timer<EventHandler>*) |
|
1406 { |
|
1407 m_hoverTimer.stop(); |
|
1408 |
|
1409 ASSERT(m_frame); |
|
1410 ASSERT(m_frame->document()); |
|
1411 |
|
1412 if (RenderObject* renderer = m_frame->renderer()) { |
|
1413 HitTestResult result(m_frame->view()->windowToContents(m_currentMousePosition)); |
|
1414 renderer->layer()->hitTest(HitTestRequest(false, false, true), result); |
|
1415 m_frame->document()->updateRendering(); |
|
1416 } |
|
1417 } |
|
1418 |
|
1419 static EventTargetNode* eventTargetNodeForDocument(Document* doc) |
|
1420 { |
|
1421 if (!doc) |
|
1422 return 0; |
|
1423 Node* node = doc->focusedNode(); |
|
1424 if (!node) { |
|
1425 if (doc->isHTMLDocument()) |
|
1426 node = doc->body(); |
|
1427 else |
|
1428 node = doc->documentElement(); |
|
1429 if (!node) |
|
1430 return 0; |
|
1431 } |
|
1432 return EventTargetNodeCast(node); |
|
1433 } |
|
1434 |
|
1435 |
|
1436 bool EventHandler::keyEvent(const PlatformKeyboardEvent& initialKeyEvent) |
|
1437 { |
|
1438 // Check for cases where we are too early for events -- possible unmatched key up |
|
1439 // from pressing return in the location bar. |
|
1440 EventTargetNode* node = eventTargetNodeForDocument(m_frame->document()); |
|
1441 if (!node) |
|
1442 return false; |
|
1443 |
|
1444 if (initialKeyEvent.isKeyUp()) |
|
1445 return !node->dispatchKeyEvent(initialKeyEvent); |
|
1446 |
|
1447 m_frame->loader()->resetMultipleFormSubmissionProtection(); |
|
1448 |
|
1449 // Prepare the keyPress in advance of the keyDown so we can fire the input method |
|
1450 // in advance of keyDown |
|
1451 PlatformKeyboardEvent keyPressEvent = initialKeyEvent; |
|
1452 keyPressEvent.setIsAutoRepeat(true); |
|
1453 RefPtr<KeyboardEvent> keypress = new KeyboardEvent(keyPressEvent, m_frame->document()->defaultView()); |
|
1454 keypress->setTarget(node); |
|
1455 |
|
1456 // Run input method in advance of DOM event handling. This may result in the IM |
|
1457 // modifying the page prior the keydown event, however this behaviour is necessary |
|
1458 // in order to match IE |
|
1459 m_frame->editor()->handleInputMethodKeypress(keypress.get()); |
|
1460 |
|
1461 bool handledByInputMethod = keypress->defaultHandled(); |
|
1462 |
|
1463 PlatformKeyboardEvent keyDownEvent = initialKeyEvent; |
|
1464 |
|
1465 if (handledByInputMethod) |
|
1466 keyDownEvent.setWindowsKeyCode(CompositionEventKeyCode); |
|
1467 |
|
1468 // We always send keyDown and keyPress for all events, including autorepeat keys |
|
1469 keyDownEvent.setIsAutoRepeat(false); |
|
1470 |
|
1471 bool result = !node->dispatchKeyEvent(keyDownEvent); |
|
1472 |
|
1473 // Focus may have change during the keyDown handling, so refetch node |
|
1474 node = eventTargetNodeForDocument(m_frame->document()); |
|
1475 if (!node) |
|
1476 return result; |
|
1477 |
|
1478 if (keypress->defaultHandled()) |
|
1479 return true; |
|
1480 |
|
1481 if (handledByInputMethod) |
|
1482 return result; |
|
1483 |
|
1484 // If the default handling has been prevented on the keydown, we prevent it on |
|
1485 // the keypress as well |
|
1486 if (result) |
|
1487 keypress->setDefaultHandled(); |
|
1488 |
|
1489 ExceptionCode ec; |
|
1490 node->dispatchEvent(keypress, ec, true); |
|
1491 |
|
1492 return result || keypress->defaultHandled() || keypress->defaultPrevented(); |
|
1493 } |
|
1494 |
|
1495 void EventHandler::defaultKeyboardEventHandler(KeyboardEvent* event) |
|
1496 { |
|
1497 if (event->type() == keypressEvent) { |
|
1498 m_frame->editor()->handleKeypress(event); |
|
1499 if (event->defaultHandled()) |
|
1500 return; |
|
1501 if (event->keyIdentifier() == "U+0009") |
|
1502 defaultTabEventHandler(event, false); |
|
1503 } |
|
1504 } |
|
1505 |
|
1506 bool EventHandler::dragHysteresisExceeded(const FloatPoint& floatDragViewportLocation) const |
|
1507 { |
|
1508 IntPoint dragViewportLocation((int)floatDragViewportLocation.x(), (int)floatDragViewportLocation.y()); |
|
1509 return dragHysteresisExceeded(dragViewportLocation); |
|
1510 } |
|
1511 |
|
1512 bool EventHandler::dragHysteresisExceeded(const IntPoint& dragViewportLocation) const |
|
1513 { |
|
1514 IntPoint dragLocation = m_frame->view()->windowToContents(dragViewportLocation); |
|
1515 IntSize delta = dragLocation - m_mouseDownPos; |
|
1516 |
|
1517 int threshold = GeneralDragHysteresis; |
|
1518 if (dragState().m_dragSrcIsImage) |
|
1519 threshold = ImageDragHysteresis; |
|
1520 else if (dragState().m_dragSrcIsLink) |
|
1521 threshold = LinkDragHysteresis; |
|
1522 else if (dragState().m_dragSrcInSelection) |
|
1523 threshold = TextDragHysteresis; |
|
1524 |
|
1525 return abs(delta.width()) >= threshold || abs(delta.height()) >= threshold; |
|
1526 } |
|
1527 |
|
1528 void EventHandler::freeClipboard() |
|
1529 { |
|
1530 if (dragState().m_dragClipboard) |
|
1531 dragState().m_dragClipboard->setAccessPolicy(ClipboardNumb); |
|
1532 } |
|
1533 |
|
1534 bool EventHandler::shouldDragAutoNode(Node* node, const IntPoint& point) const |
|
1535 { |
|
1536 ASSERT(node); |
|
1537 if (node->hasChildNodes() || !m_frame->view()) |
|
1538 return false; |
|
1539 return m_frame->page() && m_frame->page()->dragController()->mayStartDragAtEventLocation(m_frame, point); |
|
1540 } |
|
1541 |
|
1542 void EventHandler::dragSourceMovedTo(const PlatformMouseEvent& event) |
|
1543 { |
|
1544 if (dragState().m_dragSrc && dragState().m_dragSrcMayBeDHTML) |
|
1545 // for now we don't care if event handler cancels default behavior, since there is none |
|
1546 dispatchDragSrcEvent(dragEvent, event); |
|
1547 } |
|
1548 |
|
1549 void EventHandler::dragSourceEndedAt(const PlatformMouseEvent& event, DragOperation operation) |
|
1550 { |
|
1551 if (dragState().m_dragSrc && dragState().m_dragSrcMayBeDHTML) { |
|
1552 dragState().m_dragClipboard->setDestinationOperation(operation); |
|
1553 // for now we don't care if event handler cancels default behavior, since there is none |
|
1554 dispatchDragSrcEvent(dragendEvent, event); |
|
1555 } |
|
1556 freeClipboard(); |
|
1557 dragState().m_dragSrc = 0; |
|
1558 } |
|
1559 |
|
1560 // returns if we should continue "default processing", i.e., whether eventhandler canceled |
|
1561 bool EventHandler::dispatchDragSrcEvent(const AtomicString& eventType, const PlatformMouseEvent& event) |
|
1562 { |
|
1563 return !dispatchDragEvent(eventType, dragState().m_dragSrc.get(), event, dragState().m_dragClipboard.get()); |
|
1564 } |
|
1565 |
|
1566 bool EventHandler::handleDrag(const MouseEventWithHitTestResults& event) |
|
1567 { |
|
1568 if (event.event().button() != LeftButton || event.event().eventType() != MouseEventMoved) { |
|
1569 // If we allowed the other side of the bridge to handle a drag |
|
1570 // last time, then m_mousePressed might still be set. So we |
|
1571 // clear it now to make sure the next move after a drag |
|
1572 // doesn't look like a drag. |
|
1573 m_mousePressed = false; |
|
1574 return false; |
|
1575 } |
|
1576 |
|
1577 if (eventLoopHandleMouseDragged(event)) |
|
1578 return true; |
|
1579 |
|
1580 // Careful that the drag starting logic stays in sync with eventMayStartDrag() |
|
1581 |
|
1582 if (m_mouseDownMayStartDrag && !dragState().m_dragSrc) { |
|
1583 allowDHTMLDrag(dragState().m_dragSrcMayBeDHTML, dragState().m_dragSrcMayBeUA); |
|
1584 if (!dragState().m_dragSrcMayBeDHTML && !dragState().m_dragSrcMayBeUA) |
|
1585 m_mouseDownMayStartDrag = false; // no element is draggable |
|
1586 } |
|
1587 |
|
1588 if (m_mouseDownMayStartDrag && !dragState().m_dragSrc) { |
|
1589 // try to find an element that wants to be dragged |
|
1590 HitTestRequest request(true, false); |
|
1591 HitTestResult result(m_mouseDownPos); |
|
1592 m_frame->renderer()->layer()->hitTest(request, result); |
|
1593 Node* node = result.innerNode(); |
|
1594 if (node && node->renderer()) |
|
1595 dragState().m_dragSrc = node->renderer()->draggableNode(dragState().m_dragSrcMayBeDHTML, dragState().m_dragSrcMayBeUA, |
|
1596 m_mouseDownPos.x(), m_mouseDownPos.y(), dragState().m_dragSrcIsDHTML); |
|
1597 else |
|
1598 dragState().m_dragSrc = 0; |
|
1599 |
|
1600 if (!dragState().m_dragSrc) |
|
1601 m_mouseDownMayStartDrag = false; // no element is draggable |
|
1602 else { |
|
1603 // remember some facts about this source, while we have a HitTestResult handy |
|
1604 node = result.URLElement(); |
|
1605 dragState().m_dragSrcIsLink = node && node->isLink(); |
|
1606 |
|
1607 node = result.innerNonSharedNode(); |
|
1608 dragState().m_dragSrcIsImage = node && node->renderer() && node->renderer()->isImage(); |
|
1609 |
|
1610 dragState().m_dragSrcInSelection = m_frame->selectionController()->contains(m_mouseDownPos); |
|
1611 } |
|
1612 } |
|
1613 |
|
1614 // For drags starting in the selection, the user must wait between the mousedown and mousedrag, |
|
1615 // or else we bail on the dragging stuff and allow selection to occur |
|
1616 if (m_mouseDownMayStartDrag && !dragState().m_dragSrcIsImage && dragState().m_dragSrcInSelection && event.event().timestamp() - m_mouseDownTimestamp < TextDragDelay) { |
|
1617 m_mouseDownMayStartDrag = false; |
|
1618 dragState().m_dragSrc = 0; |
|
1619 // ...but if this was the first click in the window, we don't even want to start selection |
|
1620 if (eventActivatedView(event.event())) |
|
1621 m_mouseDownMayStartSelect = false; |
|
1622 } |
|
1623 |
|
1624 if (!m_mouseDownMayStartDrag) |
|
1625 return !mouseDownMayStartSelect() && !m_mouseDownMayStartAutoscroll; |
|
1626 |
|
1627 // We are starting a text/image/url drag, so the cursor should be an arrow |
|
1628 m_frame->view()->setCursor(pointerCursor()); |
|
1629 |
|
1630 if (!dragHysteresisExceeded(event.event().pos())) |
|
1631 return true; |
|
1632 |
|
1633 // Once we're past the hysteresis point, we don't want to treat this gesture as a click |
|
1634 invalidateClick(); |
|
1635 |
|
1636 DragOperation srcOp = DragOperationNone; |
|
1637 |
|
1638 freeClipboard(); // would only happen if we missed a dragEnd. Do it anyway, just |
|
1639 // to make sure it gets numbified |
|
1640 dragState().m_dragClipboard = createDraggingClipboard(); |
|
1641 |
|
1642 if (dragState().m_dragSrcMayBeDHTML) { |
|
1643 // Check to see if the is a DOM based drag, if it is get the DOM specified drag |
|
1644 // image and offset |
|
1645 if (dragState().m_dragSrcIsDHTML) { |
|
1646 int srcX, srcY; |
|
1647 dragState().m_dragSrc->renderer()->absolutePosition(srcX, srcY); |
|
1648 IntSize delta = m_mouseDownPos - IntPoint(srcX, srcY); |
|
1649 dragState().m_dragClipboard->setDragImageElement(dragState().m_dragSrc.get(), IntPoint() + delta); |
|
1650 } |
|
1651 |
|
1652 m_mouseDownMayStartDrag = dispatchDragSrcEvent(dragstartEvent, m_mouseDown) |
|
1653 && !m_frame->selectionController()->isInPasswordField(); |
|
1654 |
|
1655 // Invalidate clipboard here against anymore pasteboard writing for security. The drag |
|
1656 // image can still be changed as we drag, but not the pasteboard data. |
|
1657 dragState().m_dragClipboard->setAccessPolicy(ClipboardImageWritable); |
|
1658 |
|
1659 if (m_mouseDownMayStartDrag) { |
|
1660 // gather values from DHTML element, if it set any |
|
1661 dragState().m_dragClipboard->sourceOperation(srcOp); |
|
1662 |
|
1663 // Yuck, dragSourceMovedTo() can be called as a result of kicking off the drag with |
|
1664 // dragImage! Because of that dumb reentrancy, we may think we've not started the |
|
1665 // drag when that happens. So we have to assume it's started before we kick it off. |
|
1666 dragState().m_dragClipboard->setDragHasStarted(); |
|
1667 } |
|
1668 } |
|
1669 |
|
1670 if (m_mouseDownMayStartDrag) { |
|
1671 DragController* dragController = m_frame->page() ? m_frame->page()->dragController() : 0; |
|
1672 bool startedDrag = dragController && dragController->startDrag(m_frame, dragState().m_dragClipboard.get(), srcOp, event.event(), m_mouseDownPos, dragState().m_dragSrcIsDHTML); |
|
1673 if (!startedDrag && dragState().m_dragSrcMayBeDHTML) { |
|
1674 // Drag was canned at the last minute - we owe m_dragSrc a DRAGEND event |
|
1675 dispatchDragSrcEvent(dragendEvent, event.event()); |
|
1676 m_mouseDownMayStartDrag = false; |
|
1677 } |
|
1678 } |
|
1679 |
|
1680 if (!m_mouseDownMayStartDrag) { |
|
1681 // something failed to start the drag, cleanup |
|
1682 freeClipboard(); |
|
1683 dragState().m_dragSrc = 0; |
|
1684 } |
|
1685 |
|
1686 // No more default handling (like selection), whether we're past the hysteresis bounds or not |
|
1687 return true; |
|
1688 } |
|
1689 |
|
1690 bool EventHandler::handleTextInputEvent(const String& text, Event* underlyingEvent, |
|
1691 bool isLineBreak, bool isBackTab) |
|
1692 { |
|
1693 if (!m_frame) |
|
1694 return false; |
|
1695 EventTarget* target; |
|
1696 if (underlyingEvent) |
|
1697 target = underlyingEvent->target(); |
|
1698 else |
|
1699 target = eventTargetNodeForDocument(m_frame->document()); |
|
1700 if (!target) |
|
1701 return false; |
|
1702 RefPtr<TextEvent> event = new TextEvent(m_frame->domWindow(), text); |
|
1703 event->setUnderlyingEvent(underlyingEvent); |
|
1704 event->setIsLineBreak(isLineBreak); |
|
1705 event->setIsBackTab(isBackTab); |
|
1706 ExceptionCode ec; |
|
1707 return target->dispatchEvent(event.release(), ec, true); |
|
1708 } |
|
1709 |
|
1710 |
|
1711 #if !PLATFORM(MAC) && !PLATFORM(QT) |
|
1712 bool EventHandler::invertSenseOfTabsToLinks(KeyboardEvent*) const |
|
1713 { |
|
1714 return false; |
|
1715 } |
|
1716 #endif |
|
1717 |
|
1718 bool EventHandler::tabsToLinks(KeyboardEvent* event) const |
|
1719 { |
|
1720 Page* page = m_frame->page(); |
|
1721 if (!page) |
|
1722 return false; |
|
1723 |
|
1724 if (page->chrome()->client()->tabsToLinks()) |
|
1725 return !invertSenseOfTabsToLinks(event); |
|
1726 |
|
1727 return invertSenseOfTabsToLinks(event); |
|
1728 } |
|
1729 |
|
1730 void EventHandler::defaultTextInputEventHandler(TextEvent* event) |
|
1731 { |
|
1732 String data = event->data(); |
|
1733 if (data == "\t") { |
|
1734 defaultTabEventHandler(event, event->isBackTab()); |
|
1735 if (event->defaultHandled()) |
|
1736 return; |
|
1737 } |
|
1738 if (data == "\n") { |
|
1739 if (event->isLineBreak()) { |
|
1740 if (m_frame->editor()->insertLineBreak()) |
|
1741 event->setDefaultHandled(); |
|
1742 } else { |
|
1743 if (m_frame->editor()->insertParagraphSeparator()) |
|
1744 event->setDefaultHandled(); |
|
1745 } |
|
1746 } else { |
|
1747 if (m_frame->editor()->insertTextWithoutSendingTextEvent(data, false, event)) |
|
1748 event->setDefaultHandled(); |
|
1749 } |
|
1750 } |
|
1751 |
|
1752 void EventHandler::defaultTabEventHandler(Event* event, bool isBackTab) |
|
1753 { |
|
1754 Page* page = m_frame->page(); |
|
1755 // Tabs can be used in design mode editing. You can still move out with back tab. |
|
1756 if (!page || !page->tabKeyCyclesThroughElements() || (m_frame->document()->inDesignMode() && !isBackTab)) |
|
1757 return; |
|
1758 FocusController* focus = page->focusController(); |
|
1759 KeyboardEvent* keyboardEvent = findKeyboardEvent(event); |
|
1760 bool handled; |
|
1761 if (isBackTab) |
|
1762 handled = focus->advanceFocus(FocusDirectionBackward, keyboardEvent); |
|
1763 else |
|
1764 handled = focus->advanceFocus(keyboardEvent); // get direction from keyboard event |
|
1765 if (handled) |
|
1766 event->setDefaultHandled(); |
|
1767 } |
|
1768 |
|
1769 } |