1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
|
/*
* Copyright (C) 2007, 2009, 2010 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "DragController.h"
#if ENABLE(DRAG_SUPPORT)
#include "CSSStyleDeclaration.h"
#include "Clipboard.h"
#include "ClipboardAccessPolicy.h"
#include "DocLoader.h"
#include "Document.h"
#include "DocumentFragment.h"
#include "DragActions.h"
#include "DragClient.h"
#include "DragData.h"
#include "Editor.h"
#include "EditorClient.h"
#include "Element.h"
#include "EventHandler.h"
#include "FloatRect.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameView.h"
#include "HTMLAnchorElement.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "Image.h"
#include "MoveSelectionCommand.h"
#include "Node.h"
#include "Page.h"
#include "PlatformKeyboardEvent.h"
#include "RenderFileUploadControl.h"
#include "RenderImage.h"
#include "RenderView.h"
#include "ReplaceSelectionCommand.h"
#include "ResourceRequest.h"
#include "SelectionController.h"
#include "Settings.h"
#include "Text.h"
#include "htmlediting.h"
#include "markup.h"
#include <wtf/CurrentTime.h>
#include <wtf/RefPtr.h>
namespace WebCore {
static PlatformMouseEvent createMouseEvent(DragData* dragData)
{
bool shiftKey, ctrlKey, altKey, metaKey;
shiftKey = ctrlKey = altKey = metaKey = false;
PlatformKeyboardEvent::getCurrentModifierState(shiftKey, ctrlKey, altKey, metaKey);
return PlatformMouseEvent(dragData->clientPosition(), dragData->globalPosition(),
LeftButton, MouseEventMoved, 0, shiftKey, ctrlKey, altKey,
metaKey, currentTime());
}
DragController::DragController(Page* page, DragClient* client)
: m_page(page)
, m_client(client)
, m_documentUnderMouse(0)
, m_dragInitiator(0)
, m_dragDestinationAction(DragDestinationActionNone)
, m_dragSourceAction(DragSourceActionNone)
, m_didInitiateDrag(false)
, m_isHandlingDrag(false)
, m_sourceDragOperation(DragOperationNone)
{
}
DragController::~DragController()
{
m_client->dragControllerDestroyed();
}
static PassRefPtr<DocumentFragment> documentFragmentFromDragData(DragData* dragData, RefPtr<Range> context,
bool allowPlainText, bool& chosePlainText)
{
ASSERT(dragData);
chosePlainText = false;
Document* document = context->ownerDocument();
ASSERT(document);
if (document && dragData->containsCompatibleContent()) {
if (PassRefPtr<DocumentFragment> fragment = dragData->asFragment(document))
return fragment;
if (dragData->containsURL()) {
String title;
String url = dragData->asURL(&title);
if (!url.isEmpty()) {
RefPtr<HTMLAnchorElement> anchor = HTMLAnchorElement::create(document);
anchor->setHref(url);
if (title.isEmpty()) {
// Try the plain text first because the url might be normalized or escaped.
if (dragData->containsPlainText())
title = dragData->asPlainText();
if (title.isEmpty())
title = url;
}
RefPtr<Node> anchorText = document->createTextNode(title);
ExceptionCode ec;
anchor->appendChild(anchorText, ec);
RefPtr<DocumentFragment> fragment = document->createDocumentFragment();
fragment->appendChild(anchor, ec);
return fragment.get();
}
}
}
if (allowPlainText && dragData->containsPlainText()) {
chosePlainText = true;
return createFragmentFromText(context.get(), dragData->asPlainText()).get();
}
return 0;
}
bool DragController::dragIsMove(SelectionController* selection)
{
return m_documentUnderMouse == m_dragInitiator && selection->isContentEditable() && !isCopyKeyDown();
}
// FIXME: This method is poorly named. We're just clearing the selection from the document this drag is exiting.
void DragController::cancelDrag()
{
m_page->dragCaretController()->clear();
}
void DragController::dragEnded()
{
m_dragInitiator = 0;
m_didInitiateDrag = false;
m_page->dragCaretController()->clear();
}
DragOperation DragController::dragEntered(DragData* dragData)
{
return dragEnteredOrUpdated(dragData);
}
void DragController::dragExited(DragData* dragData)
{
ASSERT(dragData);
Frame* mainFrame = m_page->mainFrame();
if (RefPtr<FrameView> v = mainFrame->view()) {
ClipboardAccessPolicy policy = (!m_documentUnderMouse || m_documentUnderMouse->securityOrigin()->isLocal()) ? ClipboardReadable : ClipboardTypesReadable;
RefPtr<Clipboard> clipboard = dragData->createClipboard(policy);
clipboard->setSourceOperation(dragData->draggingSourceOperationMask());
mainFrame->eventHandler()->cancelDragAndDrop(createMouseEvent(dragData), clipboard.get());
clipboard->setAccessPolicy(ClipboardNumb); // invalidate clipboard here for security
}
mouseMovedIntoDocument(0);
}
DragOperation DragController::dragUpdated(DragData* dragData)
{
return dragEnteredOrUpdated(dragData);
}
bool DragController::performDrag(DragData* dragData)
{
ASSERT(dragData);
m_documentUnderMouse = m_page->mainFrame()->documentAtPoint(dragData->clientPosition());
if (m_isHandlingDrag) {
ASSERT(m_dragDestinationAction & DragDestinationActionDHTML);
m_client->willPerformDragDestinationAction(DragDestinationActionDHTML, dragData);
RefPtr<Frame> mainFrame = m_page->mainFrame();
if (mainFrame->view()) {
// Sending an event can result in the destruction of the view and part.
RefPtr<Clipboard> clipboard = dragData->createClipboard(ClipboardReadable);
clipboard->setSourceOperation(dragData->draggingSourceOperationMask());
mainFrame->eventHandler()->performDragAndDrop(createMouseEvent(dragData), clipboard.get());
clipboard->setAccessPolicy(ClipboardNumb); // invalidate clipboard here for security
}
m_documentUnderMouse = 0;
return true;
}
if ((m_dragDestinationAction & DragDestinationActionEdit) && concludeEditDrag(dragData)) {
m_documentUnderMouse = 0;
return true;
}
m_documentUnderMouse = 0;
if (operationForLoad(dragData) == DragOperationNone)
return false;
m_client->willPerformDragDestinationAction(DragDestinationActionLoad, dragData);
m_page->mainFrame()->loader()->load(ResourceRequest(dragData->asURL()), false);
return true;
}
void DragController::mouseMovedIntoDocument(Document* newDocument)
{
if (m_documentUnderMouse == newDocument)
return;
// If we were over another document clear the selection
if (m_documentUnderMouse)
cancelDrag();
m_documentUnderMouse = newDocument;
}
DragOperation DragController::dragEnteredOrUpdated(DragData* dragData)
{
ASSERT(dragData);
ASSERT(m_page->mainFrame()); // It is not possible in Mac WebKit to have a Page without a mainFrame()
mouseMovedIntoDocument(m_page->mainFrame()->documentAtPoint(dragData->clientPosition()));
m_dragDestinationAction = m_client->actionMaskForDrag(dragData);
if (m_dragDestinationAction == DragDestinationActionNone) {
cancelDrag(); // FIXME: Why not call mouseMovedIntoDocument(0)?
return DragOperationNone;
}
DragOperation operation = DragOperationNone;
bool handledByDocument = tryDocumentDrag(dragData, m_dragDestinationAction, operation);
if (!handledByDocument && (m_dragDestinationAction & DragDestinationActionLoad))
return operationForLoad(dragData);
return operation;
}
static HTMLInputElement* asFileInput(Node* node)
{
ASSERT(node);
// The button for a FILE input is a sub element with no set input type
// In order to get around this problem we assume any non-FILE input element
// is this internal button, and try querying the shadow parent node.
if (node->hasTagName(HTMLNames::inputTag) && node->isShadowNode() && static_cast<HTMLInputElement*>(node)->inputType() != HTMLInputElement::FILE)
node = node->shadowParentNode();
if (!node || !node->hasTagName(HTMLNames::inputTag))
return 0;
HTMLInputElement* inputElem = static_cast<HTMLInputElement*>(node);
if (inputElem->inputType() == HTMLInputElement::FILE)
return inputElem;
return 0;
}
static Element* elementUnderMouse(Document* documentUnderMouse, const IntPoint& p)
{
FrameView* view = documentUnderMouse->view();
float zoomFactor = view ? view->pageZoomFactor() : 1;
IntPoint point = roundedIntPoint(FloatPoint(p.x() * zoomFactor, p.y() * zoomFactor));
HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active);
HitTestResult result(point);
documentUnderMouse->renderView()->layer()->hitTest(request, result);
Node* n = result.innerNode();
while (n && !n->isElementNode())
n = n->parentNode();
if (n)
n = n->shadowAncestorNode();
ASSERT(n);
return static_cast<Element*>(n);
}
bool DragController::tryDocumentDrag(DragData* dragData, DragDestinationAction actionMask, DragOperation& operation)
{
ASSERT(dragData);
if (!m_documentUnderMouse)
return false;
m_isHandlingDrag = false;
if (actionMask & DragDestinationActionDHTML) {
m_isHandlingDrag = tryDHTMLDrag(dragData, operation);
// Do not continue if m_documentUnderMouse has been reset by tryDHTMLDrag.
// tryDHTMLDrag fires dragenter event. The event listener that listens
// to this event may create a nested message loop (open a modal dialog),
// which could process dragleave event and reset m_documentUnderMouse in
// dragExited.
if (!m_documentUnderMouse)
return false;
}
// It's unclear why this check is after tryDHTMLDrag.
// We send drag events in tryDHTMLDrag and that may be the reason.
RefPtr<FrameView> frameView = m_documentUnderMouse->view();
if (!frameView)
return false;
if (m_isHandlingDrag) {
m_page->dragCaretController()->clear();
return true;
} else if ((actionMask & DragDestinationActionEdit) && canProcessDrag(dragData)) {
if (dragData->containsColor()) {
operation = DragOperationGeneric;
return true;
}
IntPoint point = frameView->windowToContents(dragData->clientPosition());
Element* element = elementUnderMouse(m_documentUnderMouse.get(), point);
if (!asFileInput(element)) {
VisibleSelection dragCaret = m_documentUnderMouse->frame()->visiblePositionForPoint(point);
m_page->dragCaretController()->setSelection(dragCaret);
}
Frame* innerFrame = element->document()->frame();
operation = dragIsMove(innerFrame->selection()) ? DragOperationMove : DragOperationCopy;
return true;
}
// If we're not over an editable region, make sure we're clearing any prior drag cursor.
m_page->dragCaretController()->clear();
return false;
}
DragSourceAction DragController::delegateDragSourceAction(const IntPoint& windowPoint)
{
m_dragSourceAction = m_client->dragSourceActionMaskForPoint(windowPoint);
return m_dragSourceAction;
}
DragOperation DragController::operationForLoad(DragData* dragData)
{
ASSERT(dragData);
Document* doc = m_page->mainFrame()->documentAtPoint(dragData->clientPosition());
if (doc && (m_didInitiateDrag || doc->isPluginDocument() || (doc->frame() && doc->frame()->editor()->clientIsEditable())))
return DragOperationNone;
return dragOperation(dragData);
}
static bool setSelectionToDragCaret(Frame* frame, VisibleSelection& dragCaret, RefPtr<Range>& range, const IntPoint& point)
{
frame->selection()->setSelection(dragCaret);
if (frame->selection()->isNone()) {
dragCaret = frame->visiblePositionForPoint(point);
frame->selection()->setSelection(dragCaret);
range = dragCaret.toNormalizedRange();
}
return !frame->selection()->isNone() && frame->selection()->isContentEditable();
}
bool DragController::concludeEditDrag(DragData* dragData)
{
ASSERT(dragData);
ASSERT(!m_isHandlingDrag);
if (!m_documentUnderMouse)
return false;
IntPoint point = m_documentUnderMouse->view()->windowToContents(dragData->clientPosition());
Element* element = elementUnderMouse(m_documentUnderMouse.get(), point);
Frame* innerFrame = element->ownerDocument()->frame();
ASSERT(innerFrame);
if (dragData->containsColor()) {
Color color = dragData->asColor();
if (!color.isValid())
return false;
if (!innerFrame)
return false;
RefPtr<Range> innerRange = innerFrame->selection()->toNormalizedRange();
RefPtr<CSSStyleDeclaration> style = m_documentUnderMouse->createCSSStyleDeclaration();
ExceptionCode ec;
style->setProperty("color", color.name(), ec);
if (!innerFrame->editor()->shouldApplyStyle(style.get(), innerRange.get()))
return false;
m_client->willPerformDragDestinationAction(DragDestinationActionEdit, dragData);
innerFrame->editor()->applyStyle(style.get(), EditActionSetColor);
return true;
}
if (!m_page->dragController()->canProcessDrag(dragData)) {
m_page->dragCaretController()->clear();
return false;
}
if (HTMLInputElement* fileInput = asFileInput(element)) {
if (fileInput->disabled())
return false;
if (!dragData->containsFiles())
return false;
Vector<String> filenames;
dragData->asFilenames(filenames);
if (filenames.isEmpty())
return false;
// Ugly. For security none of the APIs available to us can set the input value
// on file inputs. Even forcing a change in HTMLInputElement doesn't work as
// RenderFileUploadControl clears the file when doing updateFromElement().
RenderFileUploadControl* renderer = toRenderFileUploadControl(fileInput->renderer());
if (!renderer)
return false;
renderer->receiveDroppedFiles(filenames);
return true;
}
VisibleSelection dragCaret(m_page->dragCaretController()->selection());
m_page->dragCaretController()->clear();
RefPtr<Range> range = dragCaret.toNormalizedRange();
// For range to be null a WebKit client must have done something bad while
// manually controlling drag behaviour
if (!range)
return false;
DocLoader* loader = range->ownerDocument()->docLoader();
loader->setAllowStaleResources(true);
if (dragIsMove(innerFrame->selection()) || dragCaret.isContentRichlyEditable()) {
bool chosePlainText = false;
RefPtr<DocumentFragment> fragment = documentFragmentFromDragData(dragData, range, true, chosePlainText);
if (!fragment || !innerFrame->editor()->shouldInsertFragment(fragment, range, EditorInsertActionDropped)) {
loader->setAllowStaleResources(false);
return false;
}
m_client->willPerformDragDestinationAction(DragDestinationActionEdit, dragData);
if (dragIsMove(innerFrame->selection())) {
// NSTextView behavior is to always smart delete on moving a selection,
// but only to smart insert if the selection granularity is word granularity.
bool smartDelete = innerFrame->editor()->smartInsertDeleteEnabled();
bool smartInsert = smartDelete && innerFrame->selectionGranularity() == WordGranularity && dragData->canSmartReplace();
applyCommand(MoveSelectionCommand::create(fragment, dragCaret.base(), smartInsert, smartDelete));
} else {
if (setSelectionToDragCaret(innerFrame, dragCaret, range, point))
applyCommand(ReplaceSelectionCommand::create(m_documentUnderMouse.get(), fragment, true, dragData->canSmartReplace(), chosePlainText));
}
} else {
String text = dragData->asPlainText();
if (text.isEmpty() || !innerFrame->editor()->shouldInsertText(text, range.get(), EditorInsertActionDropped)) {
loader->setAllowStaleResources(false);
return false;
}
m_client->willPerformDragDestinationAction(DragDestinationActionEdit, dragData);
if (setSelectionToDragCaret(innerFrame, dragCaret, range, point))
applyCommand(ReplaceSelectionCommand::create(m_documentUnderMouse.get(), createFragmentFromText(range.get(), text), true, false, true));
}
loader->setAllowStaleResources(false);
return true;
}
bool DragController::canProcessDrag(DragData* dragData)
{
ASSERT(dragData);
if (!dragData->containsCompatibleContent())
return false;
IntPoint point = m_page->mainFrame()->view()->windowToContents(dragData->clientPosition());
HitTestResult result = HitTestResult(point);
if (!m_page->mainFrame()->contentRenderer())
return false;
result = m_page->mainFrame()->eventHandler()->hitTestResultAtPoint(point, true);
if (!result.innerNonSharedNode())
return false;
if (dragData->containsFiles() && asFileInput(result.innerNonSharedNode()))
return true;
if (!result.innerNonSharedNode()->isContentEditable())
return false;
if (m_didInitiateDrag && m_documentUnderMouse == m_dragInitiator && result.isSelected())
return false;
return true;
}
static DragOperation defaultOperationForDrag(DragOperation srcOpMask)
{
// This is designed to match IE's operation fallback for the case where
// the page calls preventDefault() in a drag event but doesn't set dropEffect.
if (srcOpMask == DragOperationEvery)
return DragOperationCopy;
if (srcOpMask == DragOperationNone)
return DragOperationNone;
if (srcOpMask & DragOperationMove || srcOpMask & DragOperationGeneric)
return DragOperationMove;
if (srcOpMask & DragOperationCopy)
return DragOperationCopy;
if (srcOpMask & DragOperationLink)
return DragOperationLink;
// FIXME: Does IE really return "generic" even if no operations were allowed by the source?
return DragOperationGeneric;
}
bool DragController::tryDHTMLDrag(DragData* dragData, DragOperation& operation)
{
ASSERT(dragData);
ASSERT(m_documentUnderMouse);
RefPtr<Frame> mainFrame = m_page->mainFrame();
RefPtr<FrameView> viewProtector = mainFrame->view();
if (!viewProtector)
return false;
ClipboardAccessPolicy policy = m_documentUnderMouse->securityOrigin()->isLocal() ? ClipboardReadable : ClipboardTypesReadable;
RefPtr<Clipboard> clipboard = dragData->createClipboard(policy);
DragOperation srcOpMask = dragData->draggingSourceOperationMask();
clipboard->setSourceOperation(srcOpMask);
PlatformMouseEvent event = createMouseEvent(dragData);
if (!mainFrame->eventHandler()->updateDragAndDrop(event, clipboard.get())) {
clipboard->setAccessPolicy(ClipboardNumb); // invalidate clipboard here for security
return false;
}
operation = clipboard->destinationOperation();
if (clipboard->dropEffectIsUninitialized())
operation = defaultOperationForDrag(srcOpMask);
else if (!(srcOpMask & operation)) {
// The element picked an operation which is not supported by the source
operation = DragOperationNone;
}
clipboard->setAccessPolicy(ClipboardNumb); // invalidate clipboard here for security
return true;
}
bool DragController::mayStartDragAtEventLocation(const Frame* frame, const IntPoint& framePos, Node* node)
{
ASSERT(frame);
ASSERT(frame->settings());
if (!frame->view() || !frame->contentRenderer())
return false;
HitTestResult mouseDownTarget = HitTestResult(framePos);
mouseDownTarget = frame->eventHandler()->hitTestResultAtPoint(framePos, true);
if (node)
mouseDownTarget.setInnerNonSharedNode(node);
if (mouseDownTarget.image()
&& !mouseDownTarget.absoluteImageURL().isEmpty()
&& frame->settings()->loadsImagesAutomatically()
&& m_dragSourceAction & DragSourceActionImage)
return true;
if (!mouseDownTarget.absoluteLinkURL().isEmpty()
&& m_dragSourceAction & DragSourceActionLink
&& mouseDownTarget.isLiveLink()
&& mouseDownTarget.URLElement()->renderer() && mouseDownTarget.URLElement()->renderer()->style()->userDrag() != DRAG_NONE)
return true;
if (mouseDownTarget.isSelected()
&& m_dragSourceAction & DragSourceActionSelection)
return true;
return false;
}
static CachedImage* getCachedImage(Element* element)
{
ASSERT(element);
RenderObject* renderer = element->renderer();
if (!renderer || !renderer->isImage())
return 0;
RenderImage* image = toRenderImage(renderer);
return image->cachedImage();
}
static Image* getImage(Element* element)
{
ASSERT(element);
CachedImage* cachedImage = getCachedImage(element);
return (cachedImage && !cachedImage->errorOccurred()) ?
cachedImage->image() : 0;
}
static void prepareClipboardForImageDrag(Frame* src, Clipboard* clipboard, Element* node, const KURL& linkURL, const KURL& imageURL, const String& label)
{
RefPtr<Range> range = src->document()->createRange();
ExceptionCode ec = 0;
range->selectNode(node, ec);
ASSERT(!ec);
src->selection()->setSelection(VisibleSelection(range.get(), DOWNSTREAM));
clipboard->declareAndWriteDragImage(node, !linkURL.isEmpty() ? linkURL : imageURL, label, src);
}
static IntPoint dragLocForDHTMLDrag(const IntPoint& mouseDraggedPoint, const IntPoint& dragOrigin, const IntPoint& dragImageOffset, bool isLinkImage)
{
// dragImageOffset is the cursor position relative to the lower-left corner of the image.
#if PLATFORM(MAC)
// We add in the Y dimension because we are a flipped view, so adding moves the image down.
const int yOffset = dragImageOffset.y();
#else
const int yOffset = -dragImageOffset.y();
#endif
if (isLinkImage)
return IntPoint(mouseDraggedPoint.x() - dragImageOffset.x(), mouseDraggedPoint.y() + yOffset);
return IntPoint(dragOrigin.x() - dragImageOffset.x(), dragOrigin.y() + yOffset);
}
static IntPoint dragLocForSelectionDrag(Frame* src)
{
IntRect draggingRect = enclosingIntRect(src->selectionBounds());
int xpos = draggingRect.right();
xpos = draggingRect.x() < xpos ? draggingRect.x() : xpos;
int ypos = draggingRect.bottom();
#if PLATFORM(MAC)
// Deal with flipped coordinates on Mac
ypos = draggingRect.y() > ypos ? draggingRect.y() : ypos;
#else
ypos = draggingRect.y() < ypos ? draggingRect.y() : ypos;
#endif
return IntPoint(xpos, ypos);
}
bool DragController::startDrag(Frame* src, Clipboard* clipboard, DragOperation srcOp, const PlatformMouseEvent& dragEvent, const IntPoint& dragOrigin, bool isDHTMLDrag)
{
ASSERT(src);
ASSERT(clipboard);
if (!src->view() || !src->contentRenderer())
return false;
HitTestResult dragSource = HitTestResult(dragOrigin);
dragSource = src->eventHandler()->hitTestResultAtPoint(dragOrigin, true);
KURL linkURL = dragSource.absoluteLinkURL();
KURL imageURL = dragSource.absoluteImageURL();
bool isSelected = dragSource.isSelected();
IntPoint mouseDraggedPoint = src->view()->windowToContents(dragEvent.pos());
m_draggingImageURL = KURL();
m_sourceDragOperation = srcOp;
DragImageRef dragImage = 0;
IntPoint dragLoc(0, 0);
IntPoint dragImageOffset(0, 0);
if (isDHTMLDrag)
dragImage = clipboard->createDragImage(dragImageOffset);
else {
// This drag operation is not a DHTML drag and may go outside the WebView.
// We provide a default set of allowed drag operations that follows from:
// http://trac.webkit.org/browser/trunk/WebKit/mac/WebView/WebHTMLView.mm?rev=48526#L3430
m_sourceDragOperation = (DragOperation)(DragOperationGeneric | DragOperationCopy);
}
// We allow DHTML/JS to set the drag image, even if its a link, image or text we're dragging.
// This is in the spirit of the IE API, which allows overriding of pasteboard data and DragOp.
if (dragImage) {
dragLoc = dragLocForDHTMLDrag(mouseDraggedPoint, dragOrigin, dragImageOffset, !linkURL.isEmpty());
m_dragOffset = dragImageOffset;
}
bool startedDrag = true; // optimism - we almost always manage to start the drag
Node* node = dragSource.innerNonSharedNode();
Image* image = getImage(static_cast<Element*>(node));
if (!imageURL.isEmpty() && node && node->isElementNode() && image
&& (m_dragSourceAction & DragSourceActionImage)) {
// We shouldn't be starting a drag for an image that can't provide an extension.
// This is an early detection for problems encountered later upon drop.
ASSERT(!image->filenameExtension().isEmpty());
Element* element = static_cast<Element*>(node);
if (!clipboard->hasData()) {
m_draggingImageURL = imageURL;
prepareClipboardForImageDrag(src, clipboard, element, linkURL, imageURL, dragSource.altDisplayString());
}
m_client->willPerformDragSourceAction(DragSourceActionImage, dragOrigin, clipboard);
if (!dragImage) {
IntRect imageRect = dragSource.imageRect();
imageRect.setLocation(m_page->mainFrame()->view()->windowToContents(src->view()->contentsToWindow(imageRect.location())));
doImageDrag(element, dragOrigin, dragSource.imageRect(), clipboard, src, m_dragOffset);
} else
// DHTML defined drag image
doSystemDrag(dragImage, dragLoc, dragOrigin, clipboard, src, false);
} else if (!linkURL.isEmpty() && (m_dragSourceAction & DragSourceActionLink)) {
if (!clipboard->hasData())
// Simplify whitespace so the title put on the clipboard resembles what the user sees
// on the web page. This includes replacing newlines with spaces.
clipboard->writeURL(linkURL, dragSource.textContent().simplifyWhiteSpace(), src);
if (src->selection()->isCaret() && src->selection()->isContentEditable()) {
// a user can initiate a drag on a link without having any text
// selected. In this case, we should expand the selection to
// the enclosing anchor element
Position pos = src->selection()->base();
Node* node = enclosingAnchorElement(pos);
if (node)
src->selection()->setSelection(VisibleSelection::selectionFromContentsOfNode(node));
}
m_client->willPerformDragSourceAction(DragSourceActionLink, dragOrigin, clipboard);
if (!dragImage) {
dragImage = m_client->createDragImageForLink(linkURL, dragSource.textContent(), src);
IntSize size = dragImageSize(dragImage);
m_dragOffset = IntPoint(-size.width() / 2, -LinkDragBorderInset);
dragLoc = IntPoint(mouseDraggedPoint.x() + m_dragOffset.x(), mouseDraggedPoint.y() + m_dragOffset.y());
}
doSystemDrag(dragImage, dragLoc, mouseDraggedPoint, clipboard, src, true);
} else if (isSelected && (m_dragSourceAction & DragSourceActionSelection)) {
if (!clipboard->hasData()) {
if (isNodeInTextFormControl(src->selection()->start().node()))
clipboard->writePlainText(src->selectedText());
else {
RefPtr<Range> selectionRange = src->selection()->toNormalizedRange();
ASSERT(selectionRange);
clipboard->writeRange(selectionRange.get(), src);
}
}
m_client->willPerformDragSourceAction(DragSourceActionSelection, dragOrigin, clipboard);
if (!dragImage) {
dragImage = createDragImageForSelection(src);
dragLoc = dragLocForSelectionDrag(src);
m_dragOffset = IntPoint((int)(dragOrigin.x() - dragLoc.x()), (int)(dragOrigin.y() - dragLoc.y()));
}
doSystemDrag(dragImage, dragLoc, dragOrigin, clipboard, src, false);
} else if (isDHTMLDrag) {
ASSERT(m_dragSourceAction & DragSourceActionDHTML);
m_client->willPerformDragSourceAction(DragSourceActionDHTML, dragOrigin, clipboard);
doSystemDrag(dragImage, dragLoc, dragOrigin, clipboard, src, false);
} else {
// Only way I know to get here is if to get here is if the original element clicked on in the mousedown is no longer
// under the mousedown point, so linkURL, imageURL and isSelected are all false/empty.
startedDrag = false;
}
if (dragImage)
deleteDragImage(dragImage);
return startedDrag;
}
void DragController::doImageDrag(Element* element, const IntPoint& dragOrigin, const IntRect& rect, Clipboard* clipboard, Frame* frame, IntPoint& dragImageOffset)
{
IntPoint mouseDownPoint = dragOrigin;
DragImageRef dragImage;
IntPoint origin;
Image* image = getImage(element);
if (image && image->size().height() * image->size().width() <= MaxOriginalImageArea
&& (dragImage = createDragImageFromImage(image))) {
IntSize originalSize = rect.size();
origin = rect.location();
dragImage = fitDragImageToMaxSize(dragImage, rect.size(), maxDragImageSize());
dragImage = dissolveDragImageToFraction(dragImage, DragImageAlpha);
IntSize newSize = dragImageSize(dragImage);
// Properly orient the drag image and orient it differently if it's smaller than the original
float scale = newSize.width() / (float)originalSize.width();
float dx = origin.x() - mouseDownPoint.x();
dx *= scale;
origin.setX((int)(dx + 0.5));
#if PLATFORM(MAC)
//Compensate for accursed flipped coordinates in cocoa
origin.setY(origin.y() + originalSize.height());
#endif
float dy = origin.y() - mouseDownPoint.y();
dy *= scale;
origin.setY((int)(dy + 0.5));
} else {
dragImage = createDragImageIconForCachedImage(getCachedImage(element));
if (dragImage)
origin = IntPoint(DragIconRightInset - dragImageSize(dragImage).width(), DragIconBottomInset);
}
dragImageOffset.setX(mouseDownPoint.x() + origin.x());
dragImageOffset.setY(mouseDownPoint.y() + origin.y());
doSystemDrag(dragImage, dragImageOffset, dragOrigin, clipboard, frame, false);
deleteDragImage(dragImage);
}
void DragController::doSystemDrag(DragImageRef image, const IntPoint& dragLoc, const IntPoint& eventPos, Clipboard* clipboard, Frame* frame, bool forLink)
{
m_didInitiateDrag = true;
m_dragInitiator = frame->document();
// Protect this frame and view, as a load may occur mid drag and attempt to unload this frame
RefPtr<Frame> frameProtector = m_page->mainFrame();
RefPtr<FrameView> viewProtector = frameProtector->view();
m_client->startDrag(image, viewProtector->windowToContents(frame->view()->contentsToWindow(dragLoc)),
viewProtector->windowToContents(frame->view()->contentsToWindow(eventPos)), clipboard, frameProtector.get(), forLink);
cleanupAfterSystemDrag();
}
// Manual drag caret manipulation
void DragController::placeDragCaret(const IntPoint& windowPoint)
{
mouseMovedIntoDocument(m_page->mainFrame()->documentAtPoint(windowPoint));
if (!m_documentUnderMouse)
return;
Frame* frame = m_documentUnderMouse->frame();
FrameView* frameView = frame->view();
if (!frameView)
return;
IntPoint framePoint = frameView->windowToContents(windowPoint);
VisibleSelection dragCaret(frame->visiblePositionForPoint(framePoint));
m_page->dragCaretController()->setSelection(dragCaret);
}
} // namespace WebCore
#endif // ENABLE(DRAG_SUPPORT)
|