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#import "ClipboardMac.h" 30#import "ClipboardAccessPolicy.h" 31#import "Document.h" 32#import "DocumentFragment.h" 33#import "DOMDocumentFragment.h" 34#import "DOMDocumentFragmentInternal.h" 35#import "MIMETypeRegistry.h" 36#import "Pasteboard.h" 37#import "PasteboardHelper.h" 38 39namespace WebCore { 40 41DragData::DragData(DragDataRef data, const IntPoint& clientPosition, const IntPoint& globalPosition, 42 DragOperation sourceOperationMask, PasteboardHelper* pasteboardHelper) 43 : m_clientPosition(clientPosition) 44 , m_globalPosition(globalPosition) 45 , m_platformDragData(data) 46 , m_draggingSourceOperationMask(sourceOperationMask) 47 , m_pasteboardHelper(pasteboardHelper) 48{ 49 ASSERT(pasteboardHelper); 50} 51 52bool DragData::canSmartReplace() const 53{ 54 //Need to call this so that the various Pasteboard type strings are intialised 55 Pasteboard::generalPasteboard(); 56 return [[[m_platformDragData draggingPasteboard] types] containsObject:WebSmartPastePboardType]; 57} 58 59bool DragData::containsColor() const 60{ 61 return [[[m_platformDragData draggingPasteboard] types] containsObject:NSColorPboardType]; 62} 63 64bool DragData::containsFiles() const 65{ 66 return [[[m_platformDragData draggingPasteboard] types] containsObject:NSFilenamesPboardType]; 67} 68 69void DragData::asFilenames(Vector<String>& result) const 70{ 71 NSArray *filenames = [[m_platformDragData draggingPasteboard] propertyListForType:NSFilenamesPboardType]; 72 NSEnumerator *fileEnumerator = [filenames objectEnumerator]; 73 74 while (NSString *filename = [fileEnumerator nextObject]) 75 result.append(filename); 76} 77 78bool DragData::containsPlainText() const 79{ 80 NSPasteboard *pasteboard = [m_platformDragData draggingPasteboard]; 81 NSArray *types = [pasteboard types]; 82 83 return [types containsObject:NSStringPboardType] 84 || [types containsObject:NSRTFDPboardType] 85 || [types containsObject:NSRTFPboardType] 86 || [types containsObject:NSFilenamesPboardType] 87 || [NSURL URLFromPasteboard:pasteboard]; 88} 89 90String DragData::asPlainText() const 91{ 92 return m_pasteboardHelper->plainTextFromPasteboard([m_platformDragData draggingPasteboard]); 93} 94 95Color DragData::asColor() const 96{ 97 NSColor *color = [NSColor colorFromPasteboard:[m_platformDragData draggingPasteboard]]; 98 return makeRGBA((int)([color redComponent] * 255.0 + 0.5), (int)([color greenComponent] * 255.0 + 0.5), 99 (int)([color blueComponent] * 255.0 + 0.5), (int)([color alphaComponent] * 255.0 + 0.5)); 100} 101 102PassRefPtr<Clipboard> DragData::createClipboard(ClipboardAccessPolicy policy) const 103{ 104 return ClipboardMac::create(true, [m_platformDragData draggingPasteboard], policy, 0); 105} 106 107bool DragData::containsCompatibleContent() const 108{ 109 NSPasteboard *pasteboard = [m_platformDragData draggingPasteboard]; 110 NSMutableSet *types = [NSMutableSet setWithArray:[pasteboard types]]; 111 [types intersectSet:[NSSet setWithArray:m_pasteboardHelper->insertablePasteboardTypes()]]; 112 return [types count] != 0; 113} 114 115bool DragData::containsURL() const 116{ 117 return !asURL().isEmpty(); 118} 119 120String DragData::asURL(String* title) const 121{ 122 return m_pasteboardHelper->urlFromPasteboard([m_platformDragData draggingPasteboard], title); 123} 124 125PassRefPtr<DocumentFragment> DragData::asFragment(Document*) const 126{ 127 return core(m_pasteboardHelper->fragmentFromPasteboard([m_platformDragData draggingPasteboard])); 128} 129 130} 131 132