summaryrefslogtreecommitdiffstats
path: root/Source/WebKit/gtk/webkit/webkitviewportattributes.cpp
blob: 9a98e44255265a7a8b36a9f3c6fbf0aa4757974b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
 * Copyright (C) 2010 Joone Hur <joone@kldp.org>
 * Copyright (C) 2010 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#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>

/**
 * SECTION:webkitviewportattributes
 * @short_description: Represents the viewport properties of a web page
 * @see_also: #WebKitWebView::viewport-attributes-recompute-requested
 * @see_also: #WebKitWebView::viewport-attributes-changed
 *
 * #WebKitViewportAttributes offers the viewport properties to user agents to
 * control the viewport layout. It contains the viewport size, initial scale with limits,
 * and information about whether a user is able to scale the contents in the viewport.
 * This makes a web page fit the device screen.
 *
 * The #WebKitWebView::viewport-attributes-changed signal will be emitted with #WebKitViewportAttributes
 * when the viewport attributes are updated in the case of loading web pages contain
 * the viewport properties and calling webkit_viewport_attributes_recompute.
 *
 * If the device size, available size, desktop width, or device DPI needs to be changed due to 
 * a consequence of an explicit browser request (caused by screen rotation, resizing, or similar reasons),
 * You should call #webkit_viewport_attributes_recompute to recompute the viewport properties and 
 * override those values in the handler of #WebKitWebView::viewport-attributes-recompute-requested signal.
 *
 * For more information on the viewport properties, refer to the Safari reference library at
 * http://developer.apple.com/safari/library/documentation/appleapplications/reference/safarihtmlref/articles/metatags.html
 *
 * <informalexample><programlisting>
 * /<!-- -->* Connect to the viewport-attributes-changes signal *<!-- -->/
 * WebKitViewportAttributes* attributes = webkit_web_view_get_viewport_attributes (web_view);
 * g_signal_connect (web_view, "viewport-attributes-recompute-requested", G_CALLBACK (viewport_recompute_cb), window);
 * g_signal_connect (web_view, "viewport-attributes-changed", G_CALLBACK (viewport_changed_cb), window);
 * g_signal_connect (attributes, "notify::valid", G_CALLBACK (viewport_valid_changed_cb), web_view);
 *
 * /<!-- -->* Handle the viewport-attributes-recompute-requested signal to override the device width *<!-- -->/
 * static void
 * viewport_recompute_cb (WebKitWebView* web_view, WebKitViewportAttributes* attributes, GtkWidget* window)
 * {
 *     int override_available_width = 480;
 *     g_object_set (G_OBJECT(attributes), "available-width", override_available_width, NULL);
 * }
 *
 * /<!-- -->* Handle the viewport-attributes-changed signal to recompute the initial scale factor *<!-- -->/
 * static void 
 * viewport_changed_cb (WebKitWebView* web_view, WebKitViewportAttributes* attributes, gpointer data)
 * {
 *     gfloat initialScale;
 *     g_object_get (G_OBJECT (atributes), "initial-scale-factor", &initialScale, NULL);
 *     webkit_web_view_set_zoom_level (web_view, initialScale);
 * }
 * 
 * /<!-- -->* Handle the notify::valid signal to initialize the zoom level *<!-- -->/
 * static void
 * viewport_valid_changed_cb (WebKitViewportAttributes* attributes, GParamSpec* pspec, WebKitWebView* web_view)
 * {
 *     gboolean is_valid;
 *     g_object_get (attributes, "valid", &is_valid, NULL);
 *     if (!is_valid) 
 *         webkit_web_view_set_zoom_level (web_view, 1.0);
 * }
 * </programlisting></informalexample>
 */

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;

    /**
     * WebKitViewportAttributs:device-width:
     *
     * The width of the screen. This value is always automatically
     * pre-computed during a viewport attributes recomputation, and
     * can be overridden by the handler of
     * WebKitWebView::viewport-attributes-recompute-requested. You
     * should not do that unless you have a very good reason.
     *
     * Since: 1.3.8
     */
    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));

    /**
     * WebKitViewportAttributs:device-height:
     *
     * The height of the screen. This value is always automatically
     * pre-computed during a viewport attributes recomputation, and
     * can be overriden by the handler of
     * WebKitWebView::viewport-attributes-recompute-requested. You
     * should not do that unless you have a very good reason.
     *
     * Since: 1.3.8
     */
    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));

    /**
     * WebKitViewportAttributs:available-width:
     *
     * The width of the current visible area. This will usually be the
     * same as the space allocated to the widget, but in some cases
     * you may have decided to make the widget bigger than the visible
     * area. This value is by default initialized to the size
     * allocated by the widget, but you can override it in the handler
     * of WebKitWebView::viewport-attributes-recompute-requested to
     * let the engine know what the visible area is.
     *
     * Since: 1.3.8
     */
    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));

    /**
     * WebKitViewportAttributs:available-height:
     *
     * The height of the current visible area. This will usually be the
     * same as the space allocated to the widget, but in some cases
     * you may have decided to make the widget bigger than the visible
     * area. This value is by default initialized to the size
     * allocated by the widget, but you can override it in the handler
     * of WebKitWebView::viewport-attributes-recompute-requested to
     * let the engine know what the visible area is.
     *
     * Since: 1.3.8
     */
    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));

    /**
     * WebKitViewportAttributs:desktop-width:
     *
     * The width of viewport that works well for most web pages designed for 
     * desktop. This value is initialized to 980 pixels by default and used 
     * during a viewport attributes recomputation. Also, it can be overriden by 
     * the handler of WebKitWebView::viewport-attributes-recompute-requested. 
     * You should not do that unless you have a very good reason.
     *
     * Since: 1.3.8
     */
    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));

    /**
     * WebKitViewportAttributs:device-dpi:
     *
     * The number of dots per inch of the screen. This value is 
     * initialized to 160 dpi by default and used during a viewport 
     * attributes recomputation, because it is the dpi of the original 
     * iPhone and Android devices. Also, it can be overriden by the 
     * handler of WebKitWebView::viewport-attributes-recompute-requested. 
     * You should not do that unless you have a very good reason.
     *
     * Since: 1.3.8
     */
    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));

    /**
     * WebKitViewportAttributs:width:
     *
     * The width of the viewport. Before getting this property, 
     * you need to make sure that #WebKitViewportAttributes is valid.
     * 
     * Since: 1.3.8
     */
    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));

    /**
     * WebKitViewportAttributs:height:
     *
     * The height of the viewport. Before getting this property,
     * you need to make sure that #WebKitViewportAttributes is valid.
     *
     * Since: 1.3.8
     */
    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));

    /**
     * WebKitViewportAttributs:initial-scale-factor:
     *
     * The initial scale of the viewport. Before getting this property,
     * you need to make sure that #WebKitViewportAttributes is valid.
     *
     * Since: 1.3.8
     */
    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));

    /**
     * WebKitViewportAttributs:minimum-scale-factor:
     *
     * The minimum scale of the viewport. Before getting this property, 
     * you need to make sure that #WebKitViewportAttributes is valid.
     *
     * Since: 1.3.8
     */
    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));

    /**
     * WebKitViewportAttributs:maximum-scale-factor:
     *
     * The maximum scale of the viewport. Before getting this property, 
     * you need to make sure that #WebKitViewportAttributes is valid.
     *
     * Since: 1.3.8
     */
    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));

    /**
     * WebKitViewportAttributs:device-pixel-ratio:
     *
     * The device pixel ratio of the viewport. Before getting this property,
     * you need to make sure that #WebKitViewportAttributes is valid.
     *
     * Since: 1.3.8
     */
    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));

    /**
     * WebKitViewportAttributs:user-scalable:
     *
     * Determines whether or not the user can zoom in and out.
     * Before getting this property, you need to make sure that 
     * #WebKitViewportAttributes is valid.
     *
     * Since: 1.3.8
     */
    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));

    /**
     * WebKitViewportAttributs:valid:
     *
     * Determines whether or not the attributes are valid. 
     * #WebKitViewportAttributes are only valid on pages 
     * which have a viewport meta tag, and have already 
     * had the attributes calculated.
     *
     * Since: 1.3.8
     */
    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; // This value works well for most web pages designed for desktop browsers.
    viewport->priv->deviceDPI = 160; // It is the dpi of the original iPhone and Android devices.
    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();

    // First of all, we give the application an opportunity to override some of the values.
    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));

    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 = arguments.userScalable;

    if (!priv->isValid) {
        priv->isValid = TRUE;
        g_object_notify(G_OBJECT(viewportAttributes), "valid");
    }

    // Now let the application know it is safe to use the new values.
    g_signal_emit_by_name(webView, "viewport-attributes-changed", viewportAttributes);
}

/**
 * webkit_viewport_attributes_recompute:
 * @viewportAttributes: a #WebKitViewportAttributes
 *
 * Recompute the optimal viewport attributes and emit the viewport-attribute-changed signal. 
 * The viewport-attributes-recompute-requested signal also will be handled to override 
 * the device size, available size, desktop width, or device DPI.
 *
 * Since: 1.3.8
 */
void webkit_viewport_attributes_recompute(WebKitViewportAttributes* viewportAttributes)
{
    if (!viewportAttributes->priv->isValid)
        return;
    webkitViewportAttributesRecompute(viewportAttributes);
}