1e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke/*
2e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * Copyright (C) 2009 Zan Dobersek <zandobersek@gmail.com>
3e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * Copyright (C) 2010 Igalia S.L.
4e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke *
5e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * Redistribution and use in source and binary forms, with or without
6e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * modification, are permitted provided that the following conditions
7e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * are met:
8e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke *
9e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * 1.  Redistributions of source code must retain the above copyright
10e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke *     notice, this list of conditions and the following disclaimer.
11e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * 2.  Redistributions in binary form must reproduce the above copyright
12e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke *     notice, this list of conditions and the following disclaimer in the
13e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke *     documentation and/or other materials provided with the distribution.
14e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke *     its contributors may be used to endorse or promote products derived
16e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke *     from this software without specific prior written permission.
17e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke *
18e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke */
29e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
30e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke#include "config.h"
31e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
32e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke#include "DumpRenderTree.h"
3365f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch#include "GtkVersioning.h"
34e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke#include "PixelDumpSupportCairo.h"
352daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch#include "WebCoreSupport/DumpRenderTreeSupportGtk.h"
36e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke#include <webkit/webkit.h>
37e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
382daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben MurdochPassRefPtr<BitmapContext> createBitmapContextFromWebView(bool, bool, bool, bool drawSelectionRect)
39e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke{
40e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
412fc2651226baac27029e38c9d6ef883fa32084dbSteve Block    GtkWidget* viewContainer = gtk_widget_get_parent(GTK_WIDGET(view));
42e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    gint width, height;
43a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#ifdef GTK_API_VERSION_2
442fc2651226baac27029e38c9d6ef883fa32084dbSteve Block    GdkPixmap* pixmap = gtk_widget_get_snapshot(viewContainer, 0);
4565f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch    gdk_pixmap_get_size(pixmap, &width, &height);
46a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#else
472fc2651226baac27029e38c9d6ef883fa32084dbSteve Block    width = gtk_widget_get_allocated_width(viewContainer);
482fc2651226baac27029e38c9d6ef883fa32084dbSteve Block    height = gtk_widget_get_allocated_height(viewContainer);
49a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#endif
50e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
51e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    cairo_surface_t* imageSurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
52e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    cairo_t* context = cairo_create(imageSurface);
532daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch
54a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#ifdef GTK_API_VERSION_2
55e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    gdk_cairo_set_source_pixmap(context, pixmap, 0, 0);
56e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    cairo_paint(context);
57e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    g_object_unref(pixmap);
58a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#else
592fc2651226baac27029e38c9d6ef883fa32084dbSteve Block    gtk_widget_draw(viewContainer, context);
60a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#endif
61e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
622daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    if (drawSelectionRect) {
632daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch        GdkRectangle rectangle;
642daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch        DumpRenderTreeSupportGtk::rectangleForSelection(mainFrame, &rectangle);
652daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch
662daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch        cairo_set_line_width(context, 1.0);
672daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch        cairo_rectangle(context, rectangle.x, rectangle.y, rectangle.width, rectangle.height);
682daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch        cairo_set_source_rgba(context, 1.0, 0.0, 0.0, 1.0);
692daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch        cairo_stroke(context);
702daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    }
712daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch
72e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    return BitmapContext::createByAdoptingBitmapAndContext(0, context);
73e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke}
74