webkitviewportattributes.cpp [plain text]
#include "config.h"
#include "webkitviewportattributes.h"
#include "Chrome.h"
#include "Frame.h"
#include "Page.h"
#include "webkitglobalsprivate.h"
#include "webkitviewportattributesprivate.h"
#include "webkitwebviewprivate.h"
#include <glib/gi18n-lib.h>
using namespace WebKit;
using namespace WebCore;
enum {
PROP_0,
PROP_DEVICE_WIDTH,
PROP_DEVICE_HEIGHT,
PROP_AVAILABLE_WIDTH,
PROP_AVAILABLE_HEIGHT,
PROP_DESKTOP_WIDTH,
PROP_DEVICE_DPI,
PROP_WIDTH,
PROP_HEIGHT,
PROP_INITIAL_SCALE_FACTOR,
PROP_MINIMUM_SCALE_FACTOR,
PROP_MAXIMUM_SCALE_FACTOR,
PROP_DEVICE_PIXEL_RATIO,
PROP_USER_SCALABLE,
PROP_VALID
};
G_DEFINE_TYPE(WebKitViewportAttributes, webkit_viewport_attributes, G_TYPE_OBJECT);
static void webkit_viewport_attributes_get_property(GObject* object, guint propertyID, GValue* value, GParamSpec* paramSpec);
static void webkit_viewport_attributes_set_property(GObject* object, guint propertyID, const GValue* value, GParamSpec* paramSpec);
static void webkit_viewport_attributes_class_init(WebKitViewportAttributesClass* kclass)
{
GObjectClass* gobjectClass = G_OBJECT_CLASS(kclass);
gobjectClass->get_property = webkit_viewport_attributes_get_property;
gobjectClass->set_property = webkit_viewport_attributes_set_property;
g_object_class_install_property(gobjectClass,
PROP_DEVICE_WIDTH,
g_param_spec_int(
"device-width",
_("Device Width"),
_("The width of the screen."),
0,
G_MAXINT,
0,
WEBKIT_PARAM_READWRITE));
g_object_class_install_property(gobjectClass,
PROP_DEVICE_HEIGHT,
g_param_spec_int(
"device-height",
_("Device Height"),
_("The height of the screen."),
0,
G_MAXINT,
0,
WEBKIT_PARAM_READWRITE));
g_object_class_install_property(gobjectClass,
PROP_AVAILABLE_WIDTH,
g_param_spec_int(
"available-width",
_("Available Width"),
_("The width of the visible area."),
0,
G_MAXINT,
0,
WEBKIT_PARAM_READWRITE));
g_object_class_install_property(gobjectClass,
PROP_AVAILABLE_HEIGHT,
g_param_spec_int(
"available-height",
_("Available Height"),
_("The height of the visible area."),
0,
G_MAXINT,
0,
WEBKIT_PARAM_READWRITE));
g_object_class_install_property(gobjectClass,
PROP_DESKTOP_WIDTH,
g_param_spec_int(
"desktop-width",
_("Desktop Width"),
_("The width of viewport that works well for most web pages designed for desktop."),
0,
G_MAXINT,
980,
WEBKIT_PARAM_READWRITE));
g_object_class_install_property(gobjectClass,
PROP_DEVICE_DPI,
g_param_spec_int(
"device-dpi",
_("Device DPI"),
_("The number of dots per inch of the screen."),
0,
G_MAXINT,
160,
WEBKIT_PARAM_READWRITE));
g_object_class_install_property(gobjectClass,
PROP_WIDTH,
g_param_spec_int(
"width",
_("Width"),
_("The width of the viewport."),
0,
G_MAXINT,
0,
WEBKIT_PARAM_READABLE));
g_object_class_install_property(gobjectClass,
PROP_HEIGHT,
g_param_spec_int(
"height",
_("Height"),
_("The height of the viewport."),
0,
G_MAXINT,
0,
WEBKIT_PARAM_READABLE));
g_object_class_install_property(gobjectClass,
PROP_INITIAL_SCALE_FACTOR,
g_param_spec_float(
"initial-scale-factor",
_("Initial Scale Factor"),
_("The initial scale of the viewport."),
-1,
G_MAXFLOAT,
-1,
WEBKIT_PARAM_READABLE));
g_object_class_install_property(gobjectClass,
PROP_MINIMUM_SCALE_FACTOR,
g_param_spec_float(
"minimum-scale-factor",
_("Minimum Scale Factor"),
_("The minimum scale of the viewport."),
-1,
G_MAXFLOAT,
-1,
WEBKIT_PARAM_READABLE));
g_object_class_install_property(gobjectClass,
PROP_MAXIMUM_SCALE_FACTOR,
g_param_spec_float(
"maximum-scale-factor",
_("Maximum Scale Factor"),
_("The maximum scale of the viewport."),
-1,
G_MAXFLOAT,
-1,
WEBKIT_PARAM_READABLE));
g_object_class_install_property(gobjectClass,
PROP_DEVICE_PIXEL_RATIO,
g_param_spec_float(
"device-pixel-ratio",
_("Device Pixel Ratio"),
_("The device pixel ratio of the viewport."),
-1,
G_MAXFLOAT,
-1,
WEBKIT_PARAM_READABLE));
g_object_class_install_property(gobjectClass,
PROP_USER_SCALABLE,
g_param_spec_boolean(
_("user-scalable"),
_("User Scalable"),
_("Determines whether or not the user can zoom in and out."),
TRUE,
WEBKIT_PARAM_READABLE));
g_object_class_install_property(gobjectClass,
PROP_VALID,
g_param_spec_boolean(
_("valid"),
_("Valid"),
_("Determines whether or not the attributes are valid, and can be used."),
FALSE,
WEBKIT_PARAM_READABLE));
g_type_class_add_private(kclass, sizeof(WebKitViewportAttributesPrivate));
}
static void webkit_viewport_attributes_init(WebKitViewportAttributes* viewport)
{
viewport->priv = G_TYPE_INSTANCE_GET_PRIVATE(viewport, WEBKIT_TYPE_VIEWPORT_ATTRIBUTES, WebKitViewportAttributesPrivate);
viewport->priv->deviceWidth = 0;
viewport->priv->deviceHeight = 0;
viewport->priv->availableWidth = 0;
viewport->priv->availableHeight = 0;
viewport->priv->desktopWidth = 980; viewport->priv->deviceDPI = 160; viewport->priv->width = 0;
viewport->priv->height = 0;
viewport->priv->initialScaleFactor = -1;
viewport->priv->minimumScaleFactor = -1;
viewport->priv->maximumScaleFactor = -1;
viewport->priv->devicePixelRatio = -1;
viewport->priv->userScalable = TRUE;
viewport->priv->isValid = FALSE;
}
static void webkit_viewport_attributes_get_property(GObject* object, guint propertyID, GValue* value, GParamSpec* paramSpec)
{
WebKitViewportAttributes* viewportAttributes = WEBKIT_VIEWPORT_ATTRIBUTES(object);
WebKitViewportAttributesPrivate* priv = viewportAttributes->priv;
switch (propertyID) {
case PROP_DEVICE_WIDTH:
g_value_set_int(value, priv->deviceWidth);
break;
case PROP_DEVICE_HEIGHT:
g_value_set_int(value, priv->deviceHeight);
break;
case PROP_AVAILABLE_WIDTH:
g_value_set_int(value, priv->availableWidth);
break;
case PROP_AVAILABLE_HEIGHT:
g_value_set_int(value, priv->availableHeight);
break;
case PROP_DESKTOP_WIDTH:
g_value_set_int(value, priv->desktopWidth);
break;
case PROP_DEVICE_DPI:
g_value_set_int(value, priv->deviceDPI);
break;
case PROP_WIDTH:
g_value_set_int(value, priv->width);
break;
case PROP_HEIGHT:
g_value_set_int(value, priv->height);
break;
case PROP_INITIAL_SCALE_FACTOR:
g_value_set_float(value, priv->initialScaleFactor);
break;
case PROP_MINIMUM_SCALE_FACTOR:
g_value_set_float(value, priv->minimumScaleFactor);
break;
case PROP_MAXIMUM_SCALE_FACTOR:
g_value_set_float(value, priv->maximumScaleFactor);
break;
case PROP_DEVICE_PIXEL_RATIO:
g_value_set_float(value, priv->devicePixelRatio);
break;
case PROP_USER_SCALABLE:
g_value_set_boolean(value, priv->userScalable);
break;
case PROP_VALID:
g_value_set_boolean(value, priv->isValid);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, paramSpec);
break;
}
}
static void webkit_viewport_attributes_set_property(GObject* object, guint propertyID, const GValue* value, GParamSpec* paramSpec)
{
WebKitViewportAttributes* viewportAttributes = WEBKIT_VIEWPORT_ATTRIBUTES(object);
WebKitViewportAttributesPrivate* priv = viewportAttributes->priv;
switch (propertyID) {
case PROP_DEVICE_WIDTH:
priv->deviceWidth = g_value_get_int(value);
break;
case PROP_DEVICE_HEIGHT:
priv->deviceHeight = g_value_get_int(value);
break;
case PROP_AVAILABLE_WIDTH:
priv->availableWidth = g_value_get_int(value);
break;
case PROP_AVAILABLE_HEIGHT:
priv->availableHeight = g_value_get_int(value);
break;
case PROP_DESKTOP_WIDTH:
priv->desktopWidth = g_value_get_int(value);
break;
case PROP_DEVICE_DPI:
priv->deviceDPI = g_value_get_int(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, paramSpec);
break;
}
}
void webkitViewportAttributesRecompute(WebKitViewportAttributes* viewportAttributes)
{
WebKitViewportAttributesPrivate* priv = viewportAttributes->priv;
WebKitWebView* webView = priv->webView;
IntRect windowRect(webView->priv->corePage->chrome()->windowRect());
priv->deviceWidth = windowRect.width();
priv->deviceHeight = windowRect.height();
IntRect rect(webView->priv->corePage->chrome()->pageRect());
priv->availableWidth = rect.width();
priv->availableHeight = rect.height();
g_signal_emit_by_name(webView, "viewport-attributes-recompute-requested", viewportAttributes);
ViewportArguments arguments = webView->priv->corePage->mainFrame()->document()->viewportArguments();
ViewportAttributes attributes = computeViewportAttributes(arguments, priv->desktopWidth, priv->deviceWidth, priv->deviceHeight, priv->deviceDPI, IntSize(priv->availableWidth, priv->availableHeight));
restrictMinimumScaleFactorToViewportSize(attributes, IntSize(priv->availableWidth, priv->availableHeight));
restrictScaleFactorToInitialScaleIfNotUserScalable(attributes);
priv->width = attributes.layoutSize.width();
priv->height = attributes.layoutSize.height();
priv->initialScaleFactor = attributes.initialScale;
priv->minimumScaleFactor = attributes.minimumScale;
priv->maximumScaleFactor = attributes.maximumScale;
priv->devicePixelRatio = attributes.devicePixelRatio;
priv->userScalable = static_cast<bool>(arguments.userScalable);
if (!priv->isValid) {
priv->isValid = TRUE;
g_object_notify(G_OBJECT(viewportAttributes), "valid");
}
g_signal_emit_by_name(webView, "viewport-attributes-changed", viewportAttributes);
}
void webkit_viewport_attributes_recompute(WebKitViewportAttributes* viewportAttributes)
{
if (!viewportAttributes->priv->isValid)
return;
webkitViewportAttributesRecompute(viewportAttributes);
}