1/*
2 * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
3 * Copyright (C) 2013 Xueqing Huang <huangxueqing@baidu.com>
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 "Clipboard.h"
29
30#include "CachedImage.h"
31#include "ClipboardUtilitiesWin.h"
32#include "Document.h"
33#include "Element.h"
34#include "Frame.h"
35#include "HTMLNames.h"
36#include "HTMLParserIdioms.h"
37#include "NotImplemented.h"
38#include "Pasteboard.h"
39#include "markup.h"
40
41namespace WebCore {
42
43DragImageRef Clipboard::createDragImage(IntPoint& dragLocation) const
44{
45    HBITMAP result = 0;
46    if (m_dragImage) {
47#if USE(CAIRO) || USE(CG)
48        result = createDragImageFromImage(m_dragImage->image());
49        dragLocation = m_dragLoc;
50#else
51        notImplemented();
52#endif
53    } else if (m_dragImageElement) {
54        Node* node = m_dragImageElement.get();
55        result = node->document()->frame()->nodeImage(node);
56        dragLocation = m_dragLoc;
57    }
58    return result;
59}
60
61#if ENABLE(DRAG_SUPPORT)
62void Clipboard::declareAndWriteDragImage(Element* element, const KURL& url, const String& title, Frame* frame)
63{
64    // Order is important here for Explorer's sake
65    if (!m_pasteboard->writableDataObject())
66        return;
67    m_pasteboard->writeURLToWritableDataObject(url, title);
68    m_pasteboard->writeImageToDataObject(element, url);
69    AtomicString imageURL = element->getAttribute(HTMLNames::srcAttr);
70    if (imageURL.isEmpty())
71        return;
72
73    KURL fullURL = frame->document()->completeURL(stripLeadingAndTrailingHTMLSpaces(imageURL));
74    if (fullURL.isEmpty())
75        return;
76    STGMEDIUM medium = {0};
77    medium.tymed = TYMED_HGLOBAL;
78
79    // Put img tag on the clipboard referencing the image
80    Vector<char> data;
81    markupToCFHTML(createMarkup(element, IncludeNode, 0, ResolveAllURLs), "", data);
82    medium.hGlobal = createGlobalData(data);
83    if (medium.hGlobal && FAILED(m_pasteboard->writableDataObject()->SetData(htmlFormat(), &medium, TRUE)))
84        ::GlobalFree(medium.hGlobal);
85}
86#endif
87
88} // namespace WebCore
89