1/*
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.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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.
26 */
27
28#include "config.h"
29#include "PlatformWebView.h"
30
31#include <WebKit/WKImageCairo.h>
32#include <WebKit/WKPageConfigurationRef.h>
33#include <WebKit/WKView.h>
34#include <WebKit/WKViewPrivate.h>
35#include <gtk/gtk.h>
36#include <wtf/Assertions.h>
37
38namespace WTR {
39
40PlatformWebView::PlatformWebView(WKPageConfigurationRef configuration, const TestOptions& options)
41 : m_view(WKViewCreate(configuration))
42 , m_window(gtk_window_new(GTK_WINDOW_POPUP))
43 , m_windowIsKey(true)
44 , m_options(options)
45{
46 gtk_container_add(GTK_CONTAINER(m_window), GTK_WIDGET(m_view));
47
48 GtkAllocation size = { 0, 0, 800, 600 };
49 gtk_widget_size_allocate(GTK_WIDGET(m_view), &size);
50 gtk_window_resize(GTK_WINDOW(m_window), 800, 600);
51 gtk_widget_show_all(m_window);
52
53 while (gtk_events_pending())
54 gtk_main_iteration();
55}
56
57PlatformWebView::~PlatformWebView()
58{
59 gtk_widget_destroy(m_window);
60 if (m_otherWindow)
61 gtk_widget_destroy(m_otherWindow);
62}
63
64void PlatformWebView::setWindowIsKey(bool isKey)
65{
66 if (m_windowIsKey == isKey)
67 return;
68
69 m_windowIsKey = isKey;
70
71 if (isKey) {
72 if (m_otherWindow) {
73 gtk_widget_destroy(m_otherWindow);
74 m_otherWindow = nullptr;
75 }
76 gtk_window_present(GTK_WINDOW(m_window));
77 } else {
78 ASSERT(!m_otherWindow);
79 m_otherWindow = gtk_window_new(GTK_WINDOW_POPUP);
80 gtk_widget_show_all(m_otherWindow);
81 gtk_window_present(GTK_WINDOW(m_otherWindow));
82 }
83
84 while (gtk_events_pending())
85 gtk_main_iteration();
86}
87
88void PlatformWebView::resizeTo(unsigned width, unsigned height, WebViewSizingMode sizingMode)
89{
90 WKRect frame = windowFrame();
91 frame.size.width = width;
92 frame.size.height = height;
93 setWindowFrame(frame, sizingMode);
94}
95
96WKPageRef PlatformWebView::page()
97{
98 return WKViewGetPage(m_view);
99}
100
101void PlatformWebView::focus()
102{
103 WKViewSetFocus(m_view, true);
104 setWindowIsKey(true);
105}
106
107WKRect PlatformWebView::windowFrame()
108{
109 GtkAllocation geometry;
110 gdk_window_get_geometry(gtk_widget_get_window(GTK_WIDGET(m_window)),
111 &geometry.x, &geometry.y, &geometry.width, &geometry.height);
112
113 WKRect frame;
114 frame.origin.x = geometry.x;
115 frame.origin.y = geometry.y;
116 frame.size.width = geometry.width;
117 frame.size.height = geometry.height;
118 return frame;
119}
120
121void PlatformWebView::setWindowFrame(WKRect frame, WebViewSizingMode)
122{
123 gdk_window_move_resize(gtk_widget_get_window(GTK_WIDGET(m_window)),
124 frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
125 GtkAllocation size = { 0, 0, static_cast<int>(frame.size.width), static_cast<int>(frame.size.height) };
126 gtk_widget_size_allocate(GTK_WIDGET(m_view), &size);
127
128 while (gtk_events_pending())
129 gtk_main_iteration();
130}
131
132void PlatformWebView::addChromeInputField()
133{
134}
135
136void PlatformWebView::removeChromeInputField()
137{
138}
139
140void PlatformWebView::addToWindow()
141{
142}
143
144void PlatformWebView::removeFromWindow()
145{
146}
147
148void PlatformWebView::makeWebViewFirstResponder()
149{
150}
151
152void PlatformWebView::changeWindowScaleIfNeeded(float)
153{
154}
155
156cairo_surface_t* PlatformWebView::windowSnapshotImage()
157{
158 int width = gtk_widget_get_allocated_width(GTK_WIDGET(m_view));
159 int height = gtk_widget_get_allocated_height(GTK_WIDGET(m_view));
160
161 while (gtk_events_pending())
162 gtk_main_iteration();
163
164 cairo_surface_t* imageSurface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
165
166 cairo_t* context = cairo_create(imageSurface);
167 gtk_widget_draw(GTK_WIDGET(m_view), context);
168 cairo_destroy(context);
169
170 return imageSurface;
171}
172
173void PlatformWebView::didInitializeClients()
174{
175}
176
177void PlatformWebView::dismissAllPopupMenus()
178{
179 // gtk_menu_popdown doesn't modify the GList of attached menus, so it should
180 // be safe to walk this list while calling it.
181 GList* attachedMenusList = gtk_menu_get_for_attach_widget(GTK_WIDGET(m_view));
182 g_list_foreach(attachedMenusList, [] (void* data, void*) {
183 ASSERT(data);
184 gtk_menu_popdown(GTK_MENU(data));
185 }, nullptr);
186}
187
188void PlatformWebView::setNavigationGesturesEnabled(bool)
189{
190}
191
192bool PlatformWebView::drawsBackground() const
193{
194 return false;
195}
196
197void PlatformWebView::setDrawsBackground(bool)
198{
199}
200
201void PlatformWebView::setEditable(bool)
202{
203}
204
205} // namespace WTR
206
207