Home | History | Annotate | Download | only in gtk
      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 "CachedImage.h"
     23 #include "Image.h"
     24 #include "RefPtrCairo.h"
     25 #include <cairo.h>
     26 
     27 namespace WebCore {
     28 
     29 IntSize 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 
     37 void deleteDragImage(DragImageRef image)
     38 {
     39     if (image)
     40         cairo_surface_destroy(image);
     41 }
     42 
     43 DragImageRef 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 
     64 DragImageRef dissolveDragImageToFraction(DragImageRef image, float fraction)
     65 {
     66     if (!image)
     67         return 0;
     68 
     69     RefPtr<cairo_t> context = adoptRef(cairo_create(image));
     70     cairo_set_operator(context.get(), CAIRO_OPERATOR_DEST_IN);
     71     cairo_set_source_rgba(context.get(), 0, 0, 0, fraction);
     72     cairo_paint(context.get());
     73     return image;
     74 }
     75 
     76 DragImageRef createDragImageFromImage(Image* image)
     77 {
     78     return cairo_surface_reference(image->nativeImageForCurrentFrame());
     79 }
     80 
     81 DragImageRef createDragImageIconForCachedImage(CachedImage*)
     82 {
     83     return 0;
     84 }
     85 
     86 }
     87