Home | History | Annotate | Download | only in mac
      1 /*
      2  * Copyright (C) 2007 Apple Inc.  All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #import "config.h"
     27 #import "DragData.h"
     28 
     29 #if ENABLE(DRAG_SUPPORT)
     30 #import "ClipboardMac.h"
     31 #import "ClipboardAccessPolicy.h"
     32 #import "Document.h"
     33 #import "DocumentFragment.h"
     34 #import "DOMDocumentFragment.h"
     35 #import "DOMDocumentFragmentInternal.h"
     36 #import "MIMETypeRegistry.h"
     37 #import "Pasteboard.h"
     38 #import "PasteboardHelper.h"
     39 
     40 namespace WebCore {
     41 
     42 DragData::DragData(DragDataRef data, const IntPoint& clientPosition, const IntPoint& globalPosition,
     43     DragOperation sourceOperationMask, PasteboardHelper* pasteboardHelper)
     44     : m_clientPosition(clientPosition)
     45     , m_globalPosition(globalPosition)
     46     , m_platformDragData(data)
     47     , m_draggingSourceOperationMask(sourceOperationMask)
     48     , m_pasteboardHelper(pasteboardHelper)
     49 {
     50     ASSERT(pasteboardHelper);
     51 }
     52 
     53 bool DragData::canSmartReplace() const
     54 {
     55     //Need to call this so that the various Pasteboard type strings are intialised
     56     Pasteboard::generalPasteboard();
     57     return [[[m_platformDragData draggingPasteboard] types] containsObject:WebSmartPastePboardType];
     58 }
     59 
     60 bool DragData::containsColor() const
     61 {
     62     return [[[m_platformDragData draggingPasteboard] types] containsObject:NSColorPboardType];
     63 }
     64 
     65 bool DragData::containsFiles() const
     66 {
     67     return [[[m_platformDragData draggingPasteboard] types] containsObject:NSFilenamesPboardType];
     68 }
     69 
     70 void DragData::asFilenames(Vector<String>& result) const
     71 {
     72     NSArray *filenames = [[m_platformDragData draggingPasteboard] propertyListForType:NSFilenamesPboardType];
     73     NSEnumerator *fileEnumerator = [filenames objectEnumerator];
     74 
     75     while (NSString *filename = [fileEnumerator nextObject])
     76         result.append(filename);
     77 }
     78 
     79 bool DragData::containsPlainText() const
     80 {
     81     NSPasteboard *pasteboard = [m_platformDragData draggingPasteboard];
     82     NSArray *types = [pasteboard types];
     83 
     84     return [types containsObject:NSStringPboardType]
     85         || [types containsObject:NSRTFDPboardType]
     86         || [types containsObject:NSRTFPboardType]
     87         || [types containsObject:NSFilenamesPboardType]
     88         || [NSURL URLFromPasteboard:pasteboard];
     89 }
     90 
     91 String DragData::asPlainText() const
     92 {
     93     return m_pasteboardHelper->plainTextFromPasteboard([m_platformDragData draggingPasteboard]);
     94 }
     95 
     96 Color DragData::asColor() const
     97 {
     98     NSColor *color = [NSColor colorFromPasteboard:[m_platformDragData draggingPasteboard]];
     99     return makeRGBA((int)([color redComponent] * 255.0 + 0.5), (int)([color greenComponent] * 255.0 + 0.5),
    100                     (int)([color blueComponent] * 255.0 + 0.5), (int)([color alphaComponent] * 255.0 + 0.5));
    101 }
    102 
    103 PassRefPtr<Clipboard> DragData::createClipboard(ClipboardAccessPolicy policy) const
    104 {
    105     return ClipboardMac::create(true, [m_platformDragData draggingPasteboard], policy, 0);
    106 }
    107 
    108 bool DragData::containsCompatibleContent() const
    109 {
    110     NSPasteboard *pasteboard = [m_platformDragData draggingPasteboard];
    111     NSMutableSet *types = [NSMutableSet setWithArray:[pasteboard types]];
    112     [types intersectSet:[NSSet setWithArray:m_pasteboardHelper->insertablePasteboardTypes()]];
    113     return [types count] != 0;
    114 }
    115 
    116 bool DragData::containsURL() const
    117 {
    118     return !asURL().isEmpty();
    119 }
    120 
    121 String DragData::asURL(String* title) const
    122 {
    123     return m_pasteboardHelper->urlFromPasteboard([m_platformDragData draggingPasteboard], title);
    124 }
    125 
    126 PassRefPtr<DocumentFragment> DragData::asFragment(Document*) const
    127 {
    128     return core(m_pasteboardHelper->fragmentFromPasteboard([m_platformDragData draggingPasteboard]));
    129 }
    130 
    131 } // namespace WebCore
    132 
    133 #endif // ENABLE(DRAG_SUPPORT)
    134