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

« back to all changes in this revision

Viewing changes to WebCore/platform/win/DragImageWin.cpp

  • 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
 
 
26
#include "config.h"
 
27
#include "DragImage.h"
 
28
 
 
29
#include "CachedImage.h"
 
30
#include "GraphicsContext.h"
 
31
#include "Image.h"
 
32
#include "RetainPtr.h"
 
33
 
 
34
#include <CoreGraphics/CoreGraphics.h>
 
35
 
 
36
#include <windows.h>
 
37
 
 
38
namespace WebCore {
 
39
 
 
40
IntSize dragImageSize(DragImageRef image)
 
41
{
 
42
    if (!image)
 
43
        return IntSize();
 
44
    BITMAP b;
 
45
    GetObject(image, sizeof(BITMAP), &b);
 
46
    return IntSize(b.bmWidth, b.bmHeight);
 
47
}
 
48
 
 
49
void deleteDragImage(DragImageRef image)
 
50
{
 
51
    if (image)
 
52
        ::DeleteObject(image);
 
53
}
 
54
 
 
55
HBITMAP allocImage(HDC dc, IntSize size, CGContextRef *targetRef)
 
56
{
 
57
    HBITMAP hbmp;
 
58
    BITMAPINFO bmpInfo = {0};
 
59
    bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
 
60
    bmpInfo.bmiHeader.biWidth = size.width();
 
61
    bmpInfo.bmiHeader.biHeight = size.height();
 
62
    bmpInfo.bmiHeader.biPlanes = 1;
 
63
    bmpInfo.bmiHeader.biBitCount = 32;
 
64
    bmpInfo.bmiHeader.biCompression = BI_RGB;
 
65
    LPVOID bits;
 
66
    hbmp = CreateDIBSection(dc, &bmpInfo, DIB_RGB_COLORS, &bits, 0, 0);
 
67
 
 
68
    if (!targetRef)
 
69
        return hbmp;
 
70
 
 
71
    CGColorSpaceRef deviceRGB = CGColorSpaceCreateDeviceRGB();
 
72
    CGContextRef bitmapContext = CGBitmapContextCreate(bits, bmpInfo.bmiHeader.biWidth, bmpInfo.bmiHeader.biHeight, 8,
 
73
                                                       bmpInfo.bmiHeader.biWidth * 4, deviceRGB, 
 
74
                                                       kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst);
 
75
    CGColorSpaceRelease(deviceRGB);
 
76
    if (!bitmapContext) {
 
77
        DeleteObject(hbmp);
 
78
        return 0;
 
79
    }
 
80
 
 
81
    *targetRef = bitmapContext;
 
82
    return hbmp;
 
83
}
 
84
 
 
85
static CGContextRef createCgContextFromBitmap(HBITMAP bitmap)
 
86
{
 
87
    BITMAP info;
 
88
    GetObject(bitmap, sizeof(info), &info);
 
89
    ASSERT(info.bmBitsPixel == 32);
 
90
 
 
91
    CGColorSpaceRef deviceRGB = CGColorSpaceCreateDeviceRGB();
 
92
    CGContextRef bitmapContext = CGBitmapContextCreate(info.bmBits, info.bmWidth, info.bmHeight, 8,
 
93
                                                       info.bmWidthBytes, deviceRGB, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst);
 
94
    CGColorSpaceRelease(deviceRGB);
 
95
    return bitmapContext;
 
96
}
 
97
 
 
98
DragImageRef scaleDragImage(DragImageRef image, FloatSize scale)
 
99
{
 
100
    // FIXME: due to the way drag images are done on windows we need 
 
101
    // to preprocess the alpha channel <rdar://problem/5015946>
 
102
 
 
103
    if (!image)
 
104
        return 0;
 
105
    CGContextRef targetContext;
 
106
    CGContextRef srcContext;
 
107
    CGImageRef srcImage;
 
108
    IntSize srcSize = dragImageSize(image);
 
109
    IntSize dstSize((int)(srcSize.width() * scale.width()), (int)(srcSize.height() * scale.height()));
 
110
    HBITMAP hbmp = 0;
 
111
    HDC dc = GetDC(0);
 
112
    HDC dstDC = CreateCompatibleDC(dc);
 
113
    if (!dstDC)
 
114
        goto exit;
 
115
 
 
116
    hbmp = allocImage(dstDC, dstSize, &targetContext);
 
117
    if (!hbmp)
 
118
        goto exit;
 
119
 
 
120
    srcContext = createCgContextFromBitmap(image);
 
121
    srcImage = CGBitmapContextCreateImage(srcContext);
 
122
    CGRect rect;
 
123
    rect.origin.x = 0;
 
124
    rect.origin.y = 0;
 
125
    rect.size = dstSize;
 
126
    CGContextDrawImage(targetContext, rect, srcImage);
 
127
    CGImageRelease(srcImage);
 
128
    CGContextRelease(srcContext);
 
129
    CGContextRelease(targetContext);
 
130
    ::DeleteObject(image);
 
131
    image = 0;
 
132
 
 
133
exit:
 
134
    if (!hbmp)
 
135
        hbmp = image;
 
136
    if (dstDC)
 
137
        DeleteDC(dstDC);
 
138
    ReleaseDC(0, dc);
 
139
    return hbmp;
 
140
}
 
141
    
 
142
DragImageRef dissolveDragImageToFraction(DragImageRef image, float)
 
143
{
 
144
    //We don't do this on windows as the dragimage is blended by the OS
 
145
    return image;
 
146
}
 
147
        
 
148
DragImageRef createDragImageFromImage(Image* img)
 
149
{    
 
150
    HBITMAP hbmp = 0;
 
151
    HDC dc = GetDC(0);
 
152
    HDC workingDC = CreateCompatibleDC(dc);
 
153
    CGContextRef drawContext = 0;
 
154
    if (!workingDC)
 
155
        goto exit;
 
156
 
 
157
    hbmp = allocImage(workingDC, img->size(), &drawContext);
 
158
 
 
159
    if (!hbmp)
 
160
        goto exit;
 
161
 
 
162
    if (!drawContext) {
 
163
        ::DeleteObject(hbmp);
 
164
        hbmp = 0;
 
165
    }
 
166
 
 
167
    CGImageRef srcImage = img->getCGImageRef();
 
168
    CGRect rect;
 
169
    rect.size = img->size();
 
170
    rect.origin.x = 0;
 
171
    rect.origin.y = -rect.size.height;
 
172
    static const CGFloat white [] = {1.0, 1.0, 1.0, 1.0};
 
173
    CGContextScaleCTM(drawContext, 1, -1);
 
174
    CGContextSetFillColor(drawContext, white);
 
175
    CGContextFillRect(drawContext, rect);
 
176
    CGContextSetBlendMode(drawContext, kCGBlendModeNormal);
 
177
    CGContextDrawImage(drawContext, rect, srcImage);
 
178
    CGContextRelease(drawContext);
 
179
 
 
180
exit:
 
181
    if (workingDC)
 
182
        DeleteDC(workingDC);
 
183
    ReleaseDC(0, dc);
 
184
    return hbmp;
 
185
}
 
186
    
 
187
DragImageRef createDragImageIconForCachedImage(CachedImage*)
 
188
{
 
189
    //FIXME: Provide icon for image type <rdar://problem/5015949>
 
190
    return 0;     
 
191
}
 
192
    
 
193
}