1/*
2 * 2010 Igalia S.L
3 *
4 *  This library is free software; you can redistribute it and/or
5 *  modify it under the terms of the GNU Lesser General Public
6 *  License as published by the Free Software Foundation; either
7 *  version 2 of the License, or (at your option) any later version.
8 *
9 *  This library is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 *  Lesser General Public License for more details.
13 *
14 *  You should have received a copy of the GNU Lesser General Public
15 *  License along with this library; if not, write to the Free Software
16 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19#include "config.h"
20#include "DragImage.h"
21
22#include "Image.h"
23#include "RefPtrCairo.h"
24#include <cairo.h>
25#include <gdk/gdk.h>
26
27namespace WebCore {
28
29IntSize dragImageSize(DragImageRef image)
30{
31    if (image)
32        return IntSize(cairo_image_surface_get_width(image), cairo_image_surface_get_height(image));
33
34    return IntSize(0, 0);
35}
36
37void deleteDragImage(DragImageRef image)
38{
39    if (image)
40        cairo_surface_destroy(image);
41}
42
43DragImageRef scaleDragImage(DragImageRef image, FloatSize scale)
44{
45    if (!image)
46        return 0;
47
48    int newWidth = scale.width() * cairo_image_surface_get_width(image);
49    int newHeight = scale.height() * cairo_image_surface_get_height(image);
50    cairo_surface_t* scaledSurface = cairo_surface_create_similar(image, CAIRO_CONTENT_COLOR_ALPHA, newWidth, newHeight);
51
52    RefPtr<cairo_t> context = adoptRef(cairo_create(scaledSurface));
53    cairo_scale(context.get(), scale.width(), scale.height());
54    cairo_pattern_set_extend(cairo_get_source(context.get()), CAIRO_EXTEND_PAD);
55    cairo_pattern_set_filter(cairo_get_source(context.get()), CAIRO_FILTER_BEST);
56    cairo_set_operator(context.get(), CAIRO_OPERATOR_SOURCE);
57    cairo_set_source_surface(context.get(), image, 0, 0);
58    cairo_paint(context.get());
59
60    deleteDragImage(image);
61    return scaledSurface;
62}
63
64DragImageRef dissolveDragImageToFraction(DragImageRef image, float fraction)
65{
66    if (!image)
67        return 0;
68
69    if (!gdk_screen_is_composited(gdk_screen_get_default()))
70        return image;
71
72    RefPtr<cairo_t> context = adoptRef(cairo_create(image));
73    cairo_set_operator(context.get(), CAIRO_OPERATOR_DEST_IN);
74    cairo_set_source_rgba(context.get(), 0, 0, 0, fraction);
75    cairo_paint(context.get());
76    return image;
77}
78
79DragImageRef createDragImageFromImage(Image* image, ImageOrientationDescription)
80{
81    return image->nativeImageForCurrentFrame().leakRef();
82}
83
84DragImageRef createDragImageIconForCachedImageFilename(const String&)
85{
86    return 0;
87}
88
89}
90