DragController.cpp [plain text]
#include "config.h"
#include "DragController.h"
#if ENABLE(DRAG_SUPPORT)
#include "CSSStyleDeclaration.h"
#include "Clipboard.h"
#include "ClipboardAccessPolicy.h"
#include "CachedResourceLoader.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 "FrameSelection.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 "RenderLayer.h"
#include "RenderView.h"
#include "ReplaceSelectionCommand.h"
#include "ResourceRequest.h"
#include "SecurityOrigin.h"
#include "Settings.h"
#include "Text.h"
#include "TextEvent.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, Frame* frame, 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(frame, context, allowPlainText, chosePlainText))
return fragment;
if (dragData->containsURL(frame, DragData::DoNotConvertFilenames)) {
String title;
String url = dragData->asURL(frame, DragData::DoNotConvertFilenames, &title);
if (!url.isEmpty()) {
RefPtr<HTMLAnchorElement> anchor = HTMLAnchorElement::create(document);
anchor->setHref(url);
if (title.isEmpty()) {
if (dragData->containsPlainText())
title = dragData->asPlainText(frame);
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(frame)).get();
}
return 0;
}
bool DragController::dragIsMove(FrameSelection* selection, DragData* dragData)
{
return m_documentUnderMouse == m_dragInitiator && selection->isContentEditable() && !isCopyKeyDown(dragData);
}
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 = Clipboard::create(policy, dragData, mainFrame);
clipboard->setSourceOperation(dragData->draggingSourceOperationMask());
mainFrame->eventHandler()->cancelDragAndDrop(createMouseEvent(dragData), clipboard.get());
clipboard->setAccessPolicy(ClipboardNumb); }
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()) {
RefPtr<Clipboard> clipboard = Clipboard::create(ClipboardReadable, dragData, mainFrame.get());
clipboard->setSourceOperation(dragData->draggingSourceOperationMask());
mainFrame->eventHandler()->performDragAndDrop(createMouseEvent(dragData), clipboard.get());
clipboard->setAccessPolicy(ClipboardNumb); }
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(m_page->mainFrame())), false);
return true;
}
void DragController::mouseMovedIntoDocument(Document* newDocument)
{
if (m_documentUnderMouse == newDocument)
return;
if (m_documentUnderMouse)
cancelDrag();
m_documentUnderMouse = newDocument;
}
DragOperation DragController::dragEnteredOrUpdated(DragData* dragData)
{
ASSERT(dragData);
ASSERT(m_page->mainFrame()); mouseMovedIntoDocument(m_page->mainFrame()->documentAtPoint(dragData->clientPosition()));
m_dragDestinationAction = m_client->actionMaskForDrag(dragData);
if (m_dragDestinationAction == DragDestinationActionNone) {
cancelDrag(); 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);
if (node->hasTagName(HTMLNames::inputTag) && node->isShadowRoot() && !static_cast<HTMLInputElement*>(node)->isFileUpload())
node = node->shadowHost();
if (!node || !node->hasTagName(HTMLNames::inputTag))
return 0;
HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(node);
if (!inputElement->isFileUpload())
return 0;
return inputElement;
}
static Element* elementUnderMouse(Document* documentUnderMouse, const IntPoint& p)
{
Frame* frame = documentUnderMouse->frame();
float zoomFactor = frame ? frame->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();
return static_cast<Element*>(n);
}
bool DragController::tryDocumentDrag(DragData* dragData, DragDestinationAction actionMask, DragOperation& operation)
{
ASSERT(dragData);
if (!m_documentUnderMouse)
return false;
if (m_dragInitiator && !m_documentUnderMouse->securityOrigin()->canReceiveDragData(m_dragInitiator->securityOrigin()))
return false;
m_isHandlingDrag = false;
if (actionMask & DragDestinationActionDHTML) {
m_isHandlingDrag = tryDHTMLDrag(dragData, operation);
if (!m_documentUnderMouse)
return false;
}
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 (!element)
return false;
if (!asFileInput(element))
m_page->dragCaretController()->setCaretPosition(m_documentUnderMouse->frame()->visiblePositionForPoint(point));
Frame* innerFrame = element->document()->frame();
operation = dragIsMove(innerFrame->selection(), dragData) ? DragOperationMove : DragOperationCopy;
return true;
}
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->rendererIsEditable()))
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::dispatchTextInputEventFor(Frame* innerFrame, DragData* dragData)
{
ASSERT(m_page->dragCaretController()->hasCaret());
String text = m_page->dragCaretController()->isContentRichlyEditable() ? "" : dragData->asPlainText(innerFrame);
Node* target = innerFrame->editor()->findEventTargetFrom(m_page->dragCaretController()->caretPosition());
ExceptionCode ec = 0;
return target->dispatchEvent(TextEvent::createForDrop(innerFrame->domWindow(), text), ec);
}
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);
if (!element)
return false;
Frame* innerFrame = element->ownerDocument()->frame();
ASSERT(innerFrame);
if (m_page->dragCaretController()->hasCaret() && !dispatchTextInputEventFor(innerFrame, dragData))
return true;
if (dragData->containsColor()) {
Color color = dragData->asColor();
if (!color.isValid())
return false;
RefPtr<Range> innerRange = innerFrame->selection()->toNormalizedRange();
RefPtr<CSSStyleDeclaration> style = m_documentUnderMouse->createCSSStyleDeclaration();
ExceptionCode ec;
style->setProperty("color", color.serialized(), 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;
RenderFileUploadControl* renderer = toRenderFileUploadControl(fileInput->renderer());
if (!renderer)
return false;
renderer->receiveDroppedFiles(filenames);
return true;
}
VisibleSelection dragCaret = m_page->dragCaretController()->caretPosition();
m_page->dragCaretController()->clear();
RefPtr<Range> range = dragCaret.toNormalizedRange();
if (!range)
return false;
CachedResourceLoader* cachedResourceLoader = range->ownerDocument()->cachedResourceLoader();
ResourceCacheValidationSuppressor validationSuppressor(cachedResourceLoader);
if (dragIsMove(innerFrame->selection(), dragData) || dragCaret.isContentRichlyEditable()) {
bool chosePlainText = false;
RefPtr<DocumentFragment> fragment = documentFragmentFromDragData(dragData, innerFrame, range, true, chosePlainText);
if (!fragment || !innerFrame->editor()->shouldInsertFragment(fragment, range, EditorInsertActionDropped)) {
return false;
}
m_client->willPerformDragDestinationAction(DragDestinationActionEdit, dragData);
if (dragIsMove(innerFrame->selection(), dragData)) {
bool smartDelete = innerFrame->editor()->smartInsertDeleteEnabled();
bool smartInsert = smartDelete && innerFrame->selection()->granularity() == WordGranularity && dragData->canSmartReplace();
applyCommand(MoveSelectionCommand::create(fragment, dragCaret.base(), smartInsert, smartDelete));
} else {
if (setSelectionToDragCaret(innerFrame, dragCaret, range, point)) {
ReplaceSelectionCommand::CommandOptions options = ReplaceSelectionCommand::SelectReplacement | ReplaceSelectionCommand::PreventNesting;
if (dragData->canSmartReplace())
options |= ReplaceSelectionCommand::SmartReplace;
if (chosePlainText)
options |= ReplaceSelectionCommand::MatchStyle;
applyCommand(ReplaceSelectionCommand::create(m_documentUnderMouse.get(), fragment, options));
}
}
} else {
String text = dragData->asPlainText(innerFrame);
if (text.isEmpty() || !innerFrame->editor()->shouldInsertText(text, range.get(), EditorInsertActionDropped)) {
return false;
}
m_client->willPerformDragDestinationAction(DragDestinationActionEdit, dragData);
if (setSelectionToDragCaret(innerFrame, dragCaret, range, point))
applyCommand(ReplaceSelectionCommand::create(m_documentUnderMouse.get(), createFragmentFromText(range.get(), text), ReplaceSelectionCommand::SelectReplacement | ReplaceSelectionCommand::MatchStyle | ReplaceSelectionCommand::PreventNesting));
}
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()->rendererIsEditable())
return false;
if (m_didInitiateDrag && m_documentUnderMouse == m_dragInitiator && result.isSelected())
return false;
return true;
}
static DragOperation defaultOperationForDrag(DragOperation srcOpMask)
{
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;
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 = Clipboard::create(policy, dragData, mainFrame.get());
DragOperation srcOpMask = dragData->draggingSourceOperationMask();
clipboard->setSourceOperation(srcOpMask);
PlatformMouseEvent event = createMouseEvent(dragData);
if (!mainFrame->eventHandler()->updateDragAndDrop(event, clipboard.get())) {
clipboard->setAccessPolicy(ClipboardNumb); return false;
}
operation = clipboard->destinationOperation();
if (clipboard->dropEffectIsUninitialized())
operation = defaultOperationForDrag(srcOpMask);
else if (!(srcOpMask & operation)) {
operation = DragOperationNone;
}
clipboard->setAccessPolicy(ClipboardNumb); return true;
}
Node* DragController::draggableNode(const Frame* src, Node* startNode, const IntPoint& dragOrigin, DragState& state) const
{
state.m_dragType = (src->selection()->contains(dragOrigin)) ? DragSourceActionSelection : DragSourceActionNone;
for (const RenderObject* renderer = startNode->renderer(); renderer; renderer = renderer->parent()) {
Node* node = renderer->node();
if (!node)
continue;
if (!(state.m_dragType & DragSourceActionSelection) && node->isTextNode() && node->canStartSelection())
return 0;
if (node->isElementNode()) {
EUserDrag dragMode = renderer->style()->userDrag();
if ((m_dragSourceAction & DragSourceActionDHTML) && dragMode == DRAG_ELEMENT) {
state.m_dragType = static_cast<DragSourceAction>(state.m_dragType | DragSourceActionDHTML);
return node;
}
if (dragMode == DRAG_AUTO) {
if ((m_dragSourceAction & DragSourceActionImage)
&& node->hasTagName(HTMLNames::imgTag)
&& src->settings()->loadsImagesAutomatically()) {
state.m_dragType = static_cast<DragSourceAction>(state.m_dragType | DragSourceActionImage);
return node;
}
if ((m_dragSourceAction & DragSourceActionLink)
&& node->hasTagName(HTMLNames::aTag)
&& static_cast<HTMLAnchorElement*>(node)->isLiveLink()) {
state.m_dragType = static_cast<DragSourceAction>(state.m_dragType | DragSourceActionLink);
return node;
}
}
}
}
return (state.m_dragType & DragSourceActionSelection) ? startNode : 0;
}
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)
{
#if PLATFORM(MAC)
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->selection()->bounds());
int xpos = draggingRect.maxX();
xpos = draggingRect.x() < xpos ? draggingRect.x() : xpos;
int ypos = draggingRect.maxY();
#if PLATFORM(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, const DragState& state, DragOperation srcOp, const PlatformMouseEvent& dragEvent, const IntPoint& dragOrigin)
{
ASSERT(src);
if (!src->view() || !src->contentRenderer())
return false;
HitTestResult hitTestResult = src->eventHandler()->hitTestResultAtPoint(dragOrigin, true);
if (!state.m_dragSrc->contains(hitTestResult.innerNode()))
return false;
KURL linkURL = hitTestResult.absoluteLinkURL();
KURL imageURL = hitTestResult.absoluteImageURL();
IntPoint mouseDraggedPoint = src->view()->windowToContents(dragEvent.pos());
m_draggingImageURL = KURL();
m_sourceDragOperation = srcOp;
DragImageRef dragImage = 0;
IntPoint dragLoc(0, 0);
IntPoint dragImageOffset(0, 0);
Clipboard* clipboard = state.m_dragClipboard.get();
if (state.m_dragType == DragSourceActionDHTML)
dragImage = clipboard->createDragImage(dragImageOffset);
if (state.m_dragType == DragSourceActionSelection || !imageURL.isEmpty() || !linkURL.isEmpty())
m_sourceDragOperation = static_cast<DragOperation>(m_sourceDragOperation | DragOperationGeneric | DragOperationCopy);
if (dragImage) {
dragLoc = dragLocForDHTMLDrag(mouseDraggedPoint, dragOrigin, dragImageOffset, !linkURL.isEmpty());
m_dragOffset = dragImageOffset;
}
bool startedDrag = true;
Node* node = state.m_dragSrc.get();
Image* image = getImage(static_cast<Element*>(node));
if (state.m_dragType == DragSourceActionSelection) {
if (!clipboard->hasData()) {
if (isNodeInTextFormControl(src->selection()->start().deprecatedNode()))
clipboard->writePlainText(src->editor()->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(dragOrigin.x() - dragLoc.x(), dragOrigin.y() - dragLoc.y());
}
doSystemDrag(dragImage, dragLoc, dragOrigin, clipboard, src, false);
} else if (!imageURL.isEmpty() && node && node->isElementNode() && image
&& (m_dragSourceAction & DragSourceActionImage)) {
ASSERT(!image->filenameExtension().isEmpty());
Element* element = static_cast<Element*>(node);
if (!clipboard->hasData()) {
m_draggingImageURL = imageURL;
prepareClipboardForImageDrag(src, clipboard, element, linkURL, imageURL, hitTestResult.altDisplayString());
}
m_client->willPerformDragSourceAction(DragSourceActionImage, dragOrigin, clipboard);
if (!dragImage) {
IntRect imageRect = hitTestResult.imageRect();
imageRect.setLocation(m_page->mainFrame()->view()->windowToContents(src->view()->contentsToWindow(imageRect.location())));
doImageDrag(element, dragOrigin, hitTestResult.imageRect(), clipboard, src, m_dragOffset);
} else
doSystemDrag(dragImage, dragLoc, dragOrigin, clipboard, src, false);
} else if (!linkURL.isEmpty() && (m_dragSourceAction & DragSourceActionLink)) {
if (!clipboard->hasData())
clipboard->writeURL(linkURL, hitTestResult.textContent().simplifyWhiteSpace(), src);
if (src->selection()->isCaret() && src->selection()->isContentEditable()) {
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 = createDragImageForLink(linkURL, hitTestResult.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 (state.m_dragType == DragSourceActionDHTML) {
ASSERT(m_dragSourceAction & DragSourceActionDHTML);
m_client->willPerformDragSourceAction(DragSourceActionDHTML, dragOrigin, clipboard);
doSystemDrag(dragImage, dragLoc, dragOrigin, clipboard, src, false);
} else {
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);
float scale = newSize.width() / (float)originalSize.width();
float dx = origin.x() - mouseDownPoint.x();
dx *= scale;
origin.setX((int)(dx + 0.5));
#if PLATFORM(MAC)
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();
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();
}
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);
m_page->dragCaretController()->setCaretPosition(frame->visiblePositionForPoint(framePoint));
}
}
#endif // ENABLE(DRAG_SUPPORT)