2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 University of Szeged. All rights reserved.
4 * Copyright (C) 2010 Igalia S.L.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "PlatformWebView.h"
31 #include <WebKit/WKImageCairo.h>
32 #include <WebKit/WKViewPrivate.h>
37 PlatformWebView::PlatformWebView(WKContextRef context, WKPageGroupRef pageGroup, WKPageRef /* relatedPage */, WKDictionaryRef options)
38 : m_view(WKViewCreate(context, pageGroup))
39 , m_window(gtk_window_new(GTK_WINDOW_POPUP))
43 gtk_container_add(GTK_CONTAINER(m_window), GTK_WIDGET(m_view));
45 GtkAllocation size = { 0, 0, 800, 600 };
46 gtk_widget_size_allocate(GTK_WIDGET(m_view), &size);
47 gtk_window_resize(GTK_WINDOW(m_window), 800, 600);
48 gtk_widget_show_all(m_window);
50 while (gtk_events_pending())
54 PlatformWebView::~PlatformWebView()
56 gtk_widget_destroy(m_window);
59 void PlatformWebView::resizeTo(unsigned width, unsigned height)
61 WKRect frame = windowFrame();
62 frame.size.width = width;
63 frame.size.height = height;
64 setWindowFrame(frame);
67 WKPageRef PlatformWebView::page()
69 return WKViewGetPage(m_view);
72 void PlatformWebView::focus()
74 WKViewSetFocus(m_view, true);
78 WKRect PlatformWebView::windowFrame()
80 GtkAllocation geometry;
81 gdk_window_get_geometry(gtk_widget_get_window(GTK_WIDGET(m_window)),
82 &geometry.x, &geometry.y, &geometry.width, &geometry.height);
85 frame.origin.x = geometry.x;
86 frame.origin.y = geometry.y;
87 frame.size.width = geometry.width;
88 frame.size.height = geometry.height;
92 void PlatformWebView::setWindowFrame(WKRect frame)
94 gdk_window_move_resize(gtk_widget_get_window(GTK_WIDGET(m_window)),
95 frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
96 GtkAllocation size = { 0, 0, static_cast<int>(frame.size.width), static_cast<int>(frame.size.height) };
97 gtk_widget_size_allocate(GTK_WIDGET(m_view), &size);
99 while (gtk_events_pending())
100 gtk_main_iteration();
103 void PlatformWebView::addChromeInputField()
107 void PlatformWebView::removeChromeInputField()
111 void PlatformWebView::makeWebViewFirstResponder()
115 void PlatformWebView::changeWindowScaleIfNeeded(float)
119 WKRetainPtr<WKImageRef> PlatformWebView::windowSnapshotImage()
121 int width = gtk_widget_get_allocated_width(GTK_WIDGET(m_view));
122 int height = gtk_widget_get_allocated_height(GTK_WIDGET(m_view));
124 while (gtk_events_pending())
125 gtk_main_iteration();
127 cairo_surface_t* imageSurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
129 cairo_t* context = cairo_create(imageSurface);
130 gtk_widget_draw(GTK_WIDGET(m_view), context);
131 cairo_destroy(context);
133 WKRetainPtr<WKImageRef> wkImage = adoptWK(WKImageCreateFromCairoSurface(imageSurface, 0 /* options */));
135 cairo_surface_destroy(imageSurface);
139 void PlatformWebView::didInitializeClients()