1/*
2 Copyright (C) 2012 Samsung Electronics.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library 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 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "config.h"
21#include "ewk_viewport_attributes.h"
22
23#include "Chrome.h"
24#include "ChromeClientEfl.h"
25#include "EflScreenUtilities.h"
26#include "FloatRect.h"
27#include "Frame.h"
28#include "Page.h"
29#include "ewk_logging.h"
30#include "ewk_private.h"
31#include "ewk_viewport_attributes_private.h"
32#include <Evas.h>
33
34/**
35 * \struct _Ewk_Viewport_Attributes
36 * @brief Contains the viewport attributes data.
37 */
38struct _Ewk_Viewport_Attributes {
39 float width;
40 float height;
41 float initialScale;
42 float minimumScale;
43 float maximumScale;
44 float devicePixelRatio;
45 float userScalable;
46};
47
48/**
49 * @brief Viewport private data that is used internally by EFL WebKit
50 * and should never be modified from outside.
51 *
52 * @internal
53 */
54struct _Ewk_Viewport_Private_Data {
55 Evas_Object* ewkView;
56 WebCore::ViewportArguments arguments;
57};
58
59static Ewk_Viewport_Attributes _ewk_view_viewport_attributes_compute(const Ewk_Viewport_Private_Data* priv)
60{
61 int desktopWidth = 980; // This value works well for most web pages designed for desktop browsers.
62 int deviceDPI = WebCore::getDPI();
63
64 WebCore::Page* page = EWKPrivate::corePage(priv->ewkView);
65
66 WebCore::IntRect availableRect = WebCore::enclosingIntRect(page->chrome()->client()->pageRect());
67 WebCore::IntRect deviceRect = WebCore::enclosingIntRect(page->chrome()->client()->windowRect());
68
69 WebCore::ViewportArguments arguments = page->mainFrame()->document()->viewportArguments();
70 WebCore::ViewportAttributes attributes = WebCore::computeViewportAttributes(arguments,
71 desktopWidth,
72 deviceRect.width(),
73 deviceRect.height(),
74 deviceDPI,
75 availableRect.size());
76
77 WebCore::restrictMinimumScaleFactorToViewportSize(attributes, availableRect.size());
78 WebCore::restrictScaleFactorToInitialScaleIfNotUserScalable(attributes);
79
80 Ewk_Viewport_Attributes ewkAttributes;
81 ewkAttributes.width = attributes.layoutSize.width();
82 ewkAttributes.height = attributes.layoutSize.height();
83 ewkAttributes.initialScale = attributes.initialScale;
84 ewkAttributes.minimumScale = attributes.minimumScale;
85 ewkAttributes.maximumScale = attributes.maximumScale;
86 ewkAttributes.devicePixelRatio = attributes.devicePixelRatio;
87 ewkAttributes.userScalable = attributes.userScalable;
88
89 return ewkAttributes;
90}
91
92/**
93 * @internal
94 * Reports the viewport has changed.
95 *
96 * @param arguments viewport argument.
97 *
98 * Emits signal: "viewport,changed" with Ewk_Viewport_Attributes parameters.
99 */
100void ewk_viewport_attributes_changed(Ewk_Viewport_Private_Data* priv)
101{
102 Ewk_Viewport_Attributes attributes = _ewk_view_viewport_attributes_compute(priv);
103 DBG("computed viewport attribute: width:%f, height:%f, initialScale:%f, minimumScale:%f,"
104 "maximumScale:%f, devicePixelRatio:%f, userScalable:%d", attributes.width, attributes.height,
105 attributes.initialScale, attributes.minimumScale, attributes.maximumScale, attributes.devicePixelRatio,
106 static_cast<bool>(attributes.userScalable));
107
108 evas_object_smart_callback_call(priv->ewkView, "viewport,changed", &attributes);
109}
110
111/**
112 * @internal
113 * Creates private data for viewport attributes.
114 *
115 * @param ewkView view object.
116 *
117 * @return private data for viewport attributes.
118 */
119Ewk_Viewport_Private_Data* ewk_viewport_attributes_new(Evas_Object* ewkView)
120{
121 ASSERT(ewkView);
122
123 Ewk_Viewport_Private_Data* priv = new Ewk_Viewport_Private_Data;
124
125 if (!priv) {
126 CRITICAL("could not allocate Ewk_Viewport_Private_Data");
127 return 0;
128 }
129
130 priv->ewkView = ewkView;
131 priv->arguments.width = WebCore::ViewportArguments::ValueAuto;
132 priv->arguments.height = WebCore::ViewportArguments::ValueAuto;
133 priv->arguments.initialScale = WebCore::ViewportArguments::ValueAuto;
134 priv->arguments.minimumScale = WebCore::ViewportArguments::ValueAuto;
135 priv->arguments.maximumScale = WebCore::ViewportArguments::ValueAuto;
136 priv->arguments.targetDensityDpi = WebCore::ViewportArguments::ValueAuto;
137 priv->arguments.userScalable = true;
138
139 return priv;
140}