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 "Document.h"
     31 #import "DocumentFragment.h"
     32 #import "DOMDocumentFragment.h"
     33 #import "DOMDocumentFragmentInternal.h"
     34 #import "MIMETypeRegistry.h"
     35 #import "Pasteboard.h"
     36 #import "Range.h"
     37 
     38 namespace WebCore {
     39 
     40 DragData::DragData(DragDataRef data, const IntPoint& clientPosition, const IntPoint& globalPosition,
     41     DragOperation sourceOperationMask, DragApplicationFlags flags)
     42     : m_clientPosition(clientPosition)
     43     , m_globalPosition(globalPosition)
     44     , m_platformDragData(data)
     45     , m_draggingSourceOperationMask(sourceOperationMask)
     46     , m_applicationFlags(flags)
     47     , m_pasteboard([m_platformDragData draggingPasteboard])
     48 {
     49 }
     50 
     51 DragData::DragData(const String& dragStorageName, const IntPoint& clientPosition, const IntPoint& globalPosition,
     52     DragOperation sourceOperationMask, DragApplicationFlags flags)
     53     : m_clientPosition(clientPosition)
     54     , m_globalPosition(globalPosition)
     55     , m_platformDragData(0)
     56     , m_draggingSourceOperationMask(sourceOperationMask)
     57     , m_applicationFlags(flags)
     58     , m_pasteboard([NSPasteboard pasteboardWithName:dragStorageName])
     59 {
     60 }
     61 
     62 bool DragData::canSmartReplace() const
     63 {
     64     //Need to call this so that the various Pasteboard type strings are intialised
     65     Pasteboard::generalPasteboard();
     66     return [[m_pasteboard.get() types] containsObject:WebSmartPastePboardType];
     67 }
     68 
     69 bool DragData::containsColor() const
     70 {
     71     return [[m_pasteboard.get() types] containsObject:NSColorPboardType];
     72 }
     73 
     74 bool DragData::containsFiles() const
     75 {
     76     return [[m_pasteboard.get() types] containsObject:NSFilenamesPboardType];
     77 }
     78 
     79 void DragData::asFilenames(Vector<String>& result) const
     80 {
     81     NSArray *filenames = [m_pasteboard.get() propertyListForType:NSFilenamesPboardType];
     82     NSEnumerator *fileEnumerator = [filenames objectEnumerator];
     83 
     84     while (NSString *filename = [fileEnumerator nextObject])
     85         result.append(filename);
     86 }
     87 
     88 bool DragData::containsPlainText() const
     89 {
     90     NSArray *types = [m_pasteboard.get() types];
     91 
     92     return [types containsObject:NSStringPboardType]
     93         || [types containsObject:NSRTFDPboardType]
     94         || [types containsObject:NSRTFPboardType]
     95         || [types containsObject:NSFilenamesPboardType]
     96         || [NSURL URLFromPasteboard:m_pasteboard.get()];
     97 }
     98 
     99 String DragData::asPlainText(Frame *frame) const
    100 {
    101     Pasteboard pasteboard(m_pasteboard.get());
    102     return pasteboard.plainText(frame);
    103 }
    104 
    105 Color DragData::asColor() const
    106 {
    107     NSColor *color = [NSColor colorFromPasteboard:m_pasteboard.get()];
    108     return makeRGBA((int)([color redComponent] * 255.0 + 0.5), (int)([color greenComponent] * 255.0 + 0.5),
    109                     (int)([color blueComponent] * 255.0 + 0.5), (int)([color alphaComponent] * 255.0 + 0.5));
    110 }
    111 
    112 static NSArray *insertablePasteboardTypes()
    113 {
    114     static NSArray *types = nil;
    115     if (!types) {
    116         types = [[NSArray alloc] initWithObjects:WebArchivePboardType, NSHTMLPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPDFPboardType,
    117 #if defined(BUILDING_ON_TIGER) || defined(BUILDING_ON_LEOPARD)
    118                  NSPICTPboardType,
    119 #endif
    120                  NSURLPboardType, NSRTFDPboardType, NSRTFPboardType, NSStringPboardType, NSColorPboardType, kUTTypePNG, nil];
    121         CFRetain(types);
    122     }
    123     return types;
    124 }
    125 
    126 bool DragData::containsCompatibleContent() const
    127 {
    128     NSMutableSet *types = [NSMutableSet setWithArray:[m_pasteboard.get() types]];
    129     [types intersectSet:[NSSet setWithArray:insertablePasteboardTypes()]];
    130     return [types count] != 0;
    131 }
    132 
    133 bool DragData::containsURL(Frame* frame, FilenameConversionPolicy filenamePolicy) const
    134 {
    135     return !asURL(frame, filenamePolicy).isEmpty();
    136 }
    137 
    138 String DragData::asURL(Frame* frame, FilenameConversionPolicy filenamePolicy, String* title) const
    139 {
    140     // FIXME: Use filenamePolicy.
    141     (void)filenamePolicy;
    142 
    143     if (title) {
    144         if (NSString *URLTitleString = [m_pasteboard.get() stringForType:WebURLNamePboardType])
    145             *title = URLTitleString;
    146     }
    147     Pasteboard pasteboard(m_pasteboard.get());
    148     return pasteboard.asURL(frame);
    149 }
    150 
    151 PassRefPtr<DocumentFragment> DragData::asFragment(Frame* frame, PassRefPtr<Range> range, bool allowPlainText, bool& chosePlainText) const
    152 {
    153     Pasteboard pasteboard(m_pasteboard.get());
    154 
    155     return pasteboard.documentFragment(frame, range, allowPlainText, chosePlainText);
    156 }
    157 
    158 } // namespace WebCore
    159 
    160 #endif // ENABLE(DRAG_SUPPORT)
    161