~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/platform/mac/DragImageMac.mm

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#import "config.h"
 
26
#import "DragImage.h"
 
27
 
 
28
#import "CachedImage.h"
 
29
#import "Image.h"
 
30
#import "KURL.h"
 
31
#import "PlatformString.h"
 
32
#import "ResourceResponse.h"
 
33
#import <FoundationExtras.h>
 
34
 
 
35
namespace WebCore {
 
36
 
 
37
 
 
38
 
 
39
IntSize dragImageSize(DragImageRef image)
 
40
{
 
41
    return (IntSize)[image.get() size];
 
42
}
 
43
 
 
44
void deleteDragImage(DragImageRef image)
 
45
{
 
46
    //DragImageRef is a RetainPtr, so we don't need to explicitly delete it
 
47
}
 
48
 
 
49
DragImageRef scaleDragImage(DragImageRef image, FloatSize scale)
 
50
{
 
51
    NSSize originalSize = [image.get() size];
 
52
    NSSize newSize = NSMakeSize((originalSize.width * scale.width()), (originalSize.height * scale.height()));
 
53
    newSize.width = roundf(newSize.width);
 
54
    newSize.height = roundf(newSize.height);
 
55
    [image.get() setScalesWhenResized:YES];
 
56
    [image.get() setSize:newSize];
 
57
    return image;
 
58
}
 
59
    
 
60
DragImageRef dissolveDragImageToFraction(DragImageRef image, float delta)
 
61
{
 
62
    RetainPtr<NSImage> dissolvedImage(AdoptNS, [[NSImage alloc] initWithSize:[image.get() size]]);
 
63
    
 
64
    NSPoint point = [image.get() isFlipped] ? NSMakePoint(0, [image.get() size].height) : NSZeroPoint;
 
65
    
 
66
    // In this case the dragging image is always correct.
 
67
    [dissolvedImage.get() setFlipped:[image.get() isFlipped]];
 
68
    
 
69
    [dissolvedImage.get() lockFocus];
 
70
    [image.get() dissolveToPoint:point fraction: delta];
 
71
    [dissolvedImage.get() unlockFocus];
 
72
    
 
73
    [image.get() lockFocus];
 
74
    [dissolvedImage.get() compositeToPoint:point operation:NSCompositeCopy];
 
75
    [image.get() unlockFocus];
 
76
    
 
77
    return image;
 
78
}
 
79
        
 
80
DragImageRef createDragImageFromImage(Image* image)
 
81
{
 
82
    DragImageRef dragImage(AdoptNS, [image->getNSImage() copy]);
 
83
    [dragImage.get() setSize:(NSSize)(image->size())];
 
84
    return dragImage;
 
85
}
 
86
    
 
87
DragImageRef createDragImageIconForCachedImage(CachedImage* image)
 
88
{
 
89
    const String& filename = image->response().suggestedFilename();
 
90
    NSString *extension = nil;
 
91
    int dotIndex = filename.reverseFind('.');
 
92
    
 
93
    if (dotIndex > 0 && dotIndex < (int)(filename.length() - 1)) // require that a . exists after the first character and before the last
 
94
        extension = filename.substring(dotIndex + 1);
 
95
    else
 
96
        //It might be worth doing a further look up to pull the extension from the mimetype
 
97
        extension = @"";
 
98
    
 
99
    return DragImageRef([[NSWorkspace sharedWorkspace] iconForFileType:extension]);
 
100
     
 
101
}
 
102
    
 
103
}