1/*
2 * Copyright (C) 2011 Igalia S.L.
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 "WebKitWindowProperties.h"
22
23#include "APIDictionary.h"
24#include "APINumber.h"
25#include "APIURLRequest.h"
26#include "WebKitWindowPropertiesPrivate.h"
27#include <WebCore/IntRect.h>
28#include <WebCore/WindowFeatures.h>
29#include <glib/gi18n-lib.h>
30#include <wtf/glib/WTFGType.h>
31
32using namespace WebCore;
33
34/**
35 * SECTION: WebKitWindowProperties
36 * @short_description: Window properties of a #WebKitWebView
37 * @title: WebKitWindowProperties
38 * @see_also: #WebKitWebView::ready-to-show
39 *
40 * The content of a #WebKitWebView can request to change certain
41 * properties of the window containing the view. This can include the x, y position
42 * of the window, the width and height but also if a toolbar,
43 * scrollbar, statusbar, locationbar should be visible to the user,
44 * and the request to show the #WebKitWebView fullscreen.
45 *
46 * The #WebKitWebView::ready-to-show signal handler is the proper place
47 * to apply the initial window properties. Then you can monitor the
48 * #WebKitWindowProperties by connecting to ::notify signal.
49 *
50 * <informalexample><programlisting>
51 * static void ready_to_show_cb (WebKitWebView *web_view, gpointer user_data)
52 * {
53 * GtkWidget *window;
54 * WebKitWindowProperties *window_properties;
55 * gboolean visible;
56 *
57 * /<!-- -->* Create the window to contain the WebKitWebView *<!-- -->/
58 * window = browser_window_new ();
59 * gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (web_view));
60 * gtk_widget_show (GTK_WIDGET (web_view));
61 *
62 * /<!-- -->* Get the WebKitWindowProperties of the web view and monitor it *<!-- -->/
63 * window_properties = webkit_web_view_get_window_properties (web_view);
64 * g_signal_connect (window_properties, "notify::geometry",
65 * G_CALLBACK (window_geometry_changed), window);
66 * g_signal_connect (window_properties, "notify::toolbar-visible",
67 * G_CALLBACK (window_toolbar_visibility_changed), window);
68 * g_signal_connect (window_properties, "notify::menubar-visible",
69 * G_CALLBACK (window_menubar_visibility_changed), window);
70 * ....
71 *
72 * /<!-- -->* Apply the window properties before showing the window *<!-- -->/
73 * visible = webkit_window_properties_get_toolbar_visible (window_properties);
74 * browser_window_set_toolbar_visible (BROWSER_WINDOW (window), visible);
75 * visible = webkit_window_properties_get_menubar_visible (window_properties);
76 * browser_window_set_menubar_visible (BROWSER_WINDOW (window), visible);
77 * ....
78 *
79 * if (webkit_window_properties_get_fullscreen (window_properties)) {
80 * gtk_window_fullscreen (GTK_WINDOW (window));
81 * } else {
82 * GdkRectangle geometry;
83 *
84 * gtk_window_set_resizable (GTK_WINDOW (window),
85 * webkit_window_properties_get_resizable (window_properties));
86 * webkit_window_properties_get_geometry (window_properties, &geometry);
87 * gtk_window_move (GTK_WINDOW (window), geometry.x, geometry.y);
88 * gtk_window_resize (GTK_WINDOW (window), geometry.width, geometry.height);
89 * }
90 *
91 * gtk_widget_show (window);
92 * }
93 * </programlisting></informalexample>
94 */
95
96enum {
97 PROP_0,
98
99#if PLATFORM(GTK)
100 PROP_GEOMETRY,
101#endif
102 PROP_TOOLBAR_VISIBLE,
103 PROP_STATUSBAR_VISIBLE,
104 PROP_SCROLLBARS_VISIBLE,
105 PROP_MENUBAR_VISIBLE,
106 PROP_LOCATIONBAR_VISIBLE,
107 PROP_RESIZABLE,
108 PROP_FULLSCREEN
109};
110
111struct _WebKitWindowPropertiesPrivate {
112#if PLATFORM(GTK)
113 GdkRectangle geometry;
114#endif
115
116 bool toolbarVisible : 1;
117 bool statusbarVisible : 1;
118 bool scrollbarsVisible : 1;
119 bool menubarVisible : 1;
120 bool locationbarVisible : 1;
121
122 bool resizable : 1;
123 bool fullscreen : 1;
124};
125
126WEBKIT_DEFINE_TYPE(WebKitWindowProperties, webkit_window_properties, G_TYPE_OBJECT)
127
128static void webkitWindowPropertiesGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec)
129{
130 WebKitWindowProperties* windowProperties = WEBKIT_WINDOW_PROPERTIES(object);
131
132 switch (propId) {
133#if PLATFORM(GTK)
134 case PROP_GEOMETRY:
135 g_value_set_boxed(value, &windowProperties->priv->geometry);
136 break;
137#endif
138 case PROP_TOOLBAR_VISIBLE:
139 g_value_set_boolean(value, webkit_window_properties_get_toolbar_visible(windowProperties));
140 break;
141 case PROP_STATUSBAR_VISIBLE:
142 g_value_set_boolean(value, webkit_window_properties_get_statusbar_visible(windowProperties));
143 break;
144 case PROP_SCROLLBARS_VISIBLE:
145 g_value_set_boolean(value, webkit_window_properties_get_scrollbars_visible(windowProperties));
146 break;
147 case PROP_MENUBAR_VISIBLE:
148 g_value_set_boolean(value, webkit_window_properties_get_menubar_visible(windowProperties));
149 break;
150 case PROP_LOCATIONBAR_VISIBLE:
151 g_value_set_boolean(value, webkit_window_properties_get_locationbar_visible(windowProperties));
152 break;
153 case PROP_RESIZABLE:
154 g_value_set_boolean(value, webkit_window_properties_get_resizable(windowProperties));
155 break;
156 case PROP_FULLSCREEN:
157 g_value_set_boolean(value, webkit_window_properties_get_fullscreen(windowProperties));
158 break;
159 default:
160 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
161 }
162}
163
164static void webkitWindowPropertiesSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec)
165{
166 WebKitWindowProperties* windowProperties = WEBKIT_WINDOW_PROPERTIES(object);
167
168 switch (propId) {
169#if PLATFORM(GTK)
170 case PROP_GEOMETRY:
171 if (GdkRectangle* geometry = static_cast<GdkRectangle*>(g_value_get_boxed(value)))
172 windowProperties->priv->geometry = *geometry;
173 break;
174#endif
175 case PROP_TOOLBAR_VISIBLE:
176 windowProperties->priv->toolbarVisible = g_value_get_boolean(value);
177 break;
178 case PROP_STATUSBAR_VISIBLE:
179 windowProperties->priv->statusbarVisible = g_value_get_boolean(value);
180 break;
181 case PROP_SCROLLBARS_VISIBLE:
182 windowProperties->priv->scrollbarsVisible = g_value_get_boolean(value);
183 break;
184 case PROP_MENUBAR_VISIBLE:
185 windowProperties->priv->menubarVisible = g_value_get_boolean(value);
186 break;
187 case PROP_LOCATIONBAR_VISIBLE:
188 windowProperties->priv->locationbarVisible = g_value_get_boolean(value);
189 break;
190 case PROP_RESIZABLE:
191 windowProperties->priv->resizable = g_value_get_boolean(value);
192 break;
193 case PROP_FULLSCREEN:
194 windowProperties->priv->fullscreen = g_value_get_boolean(value);
195 break;
196 default:
197 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
198 }
199}
200
201static void webkit_window_properties_class_init(WebKitWindowPropertiesClass* requestClass)
202{
203 GObjectClass* objectClass = G_OBJECT_CLASS(requestClass);
204 objectClass->get_property = webkitWindowPropertiesGetProperty;
205 objectClass->set_property = webkitWindowPropertiesSetProperty;
206
207 GParamFlags paramFlags = static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
208
209#if PLATFORM(GTK)
210 /**
211 * WebKitWebWindowProperties:geometry:
212 *
213 * The size and position of the window on the screen.
214 */
215 g_object_class_install_property(objectClass,
216 PROP_GEOMETRY,
217 g_param_spec_boxed("geometry",
218 _("Geometry"),
219 _("The size and position of the window on the screen."),
220 GDK_TYPE_RECTANGLE,
221 paramFlags));
222#endif
223
224 /**
225 * WebKitWebWindowProperties:toolbar-visible:
226 *
227 * Whether the toolbar should be visible for the window.
228 */
229 g_object_class_install_property(objectClass,
230 PROP_TOOLBAR_VISIBLE,
231 g_param_spec_boolean("toolbar-visible",
232 _("Toolbar Visible"),
233 _("Whether the toolbar should be visible for the window."),
234 TRUE,
235 paramFlags));
236
237 /**
238 * WebKitWebWindowProperties:statusbar-visible:
239 *
240 * Whether the statusbar should be visible for the window.
241 */
242 g_object_class_install_property(objectClass,
243 PROP_STATUSBAR_VISIBLE,
244 g_param_spec_boolean("statusbar-visible",
245 _("Statusbar Visible"),
246 _("Whether the statusbar should be visible for the window."),
247 TRUE,
248 paramFlags));
249
250 /**
251 * WebKitWebWindowProperties:scrollbars-visible:
252 *
253 * Whether the scrollbars should be visible for the window.
254 */
255 g_object_class_install_property(objectClass,
256 PROP_SCROLLBARS_VISIBLE,
257 g_param_spec_boolean("scrollbars-visible",
258 _("Scrollbars Visible"),
259 _("Whether the scrollbars should be visible for the window."),
260 TRUE,
261 paramFlags));
262
263 /**
264 * WebKitWebWindowProperties:menubar-visible:
265 *
266 * Whether the menubar should be visible for the window.
267 */
268 g_object_class_install_property(objectClass,
269 PROP_MENUBAR_VISIBLE,
270 g_param_spec_boolean("menubar-visible",
271 _("Menubar Visible"),
272 _("Whether the menubar should be visible for the window."),
273 TRUE,
274 paramFlags));
275
276 /**
277 * WebKitWebWindowProperties:locationbar-visible:
278 *
279 * Whether the locationbar should be visible for the window.
280 */
281 g_object_class_install_property(objectClass,
282 PROP_LOCATIONBAR_VISIBLE,
283 g_param_spec_boolean("locationbar-visible",
284 _("Locationbar Visible"),
285 _("Whether the locationbar should be visible for the window."),
286 TRUE,
287 paramFlags));
288 /**
289 * WebKitWebWindowProperties:resizable:
290 *
291 * Whether the window can be resized.
292 */
293 g_object_class_install_property(objectClass,
294 PROP_RESIZABLE,
295 g_param_spec_boolean("resizable",
296 _("Resizable"),
297 _("Whether the window can be resized."),
298 TRUE,
299 paramFlags));
300
301 /**
302 * WebKitWebWindowProperties:fullscreen:
303 *
304 * Whether window will be displayed fullscreen.
305 */
306 g_object_class_install_property(objectClass,
307 PROP_FULLSCREEN,
308 g_param_spec_boolean("fullscreen",
309 _("Fullscreen"),
310 _("Whether window will be displayed fullscreen."),
311 FALSE,
312 paramFlags));
313}
314
315WebKitWindowProperties* webkitWindowPropertiesCreate()
316{
317 return WEBKIT_WINDOW_PROPERTIES(g_object_new(WEBKIT_TYPE_WINDOW_PROPERTIES, NULL));
318}
319
320#if PLATFORM(GTK)
321void webkitWindowPropertiesSetGeometry(WebKitWindowProperties* windowProperties, GdkRectangle* geometry)
322{
323 if (windowProperties->priv->geometry.x == geometry->x
324 && windowProperties->priv->geometry.y == geometry->y
325 && windowProperties->priv->geometry.width == geometry->width
326 && windowProperties->priv->geometry.height == geometry->height)
327 return;
328 windowProperties->priv->geometry = *geometry;
329 g_object_notify(G_OBJECT(windowProperties), "geometry");
330}
331#endif
332
333void webkitWindowPropertiesSetToolbarVisible(WebKitWindowProperties* windowProperties, bool toolbarsVisible)
334{
335 if (windowProperties->priv->toolbarVisible == toolbarsVisible)
336 return;
337 windowProperties->priv->toolbarVisible = toolbarsVisible;
338 g_object_notify(G_OBJECT(windowProperties), "toolbar-visible");
339}
340
341void webkitWindowPropertiesSetMenubarVisible(WebKitWindowProperties* windowProperties, bool menuBarVisible)
342{
343 if (windowProperties->priv->menubarVisible == menuBarVisible)
344 return;
345 windowProperties->priv->menubarVisible = menuBarVisible;
346 g_object_notify(G_OBJECT(windowProperties), "menubar-visible");
347}
348
349void webkitWindowPropertiesSetStatusbarVisible(WebKitWindowProperties* windowProperties, bool statusBarVisible)
350{
351 if (windowProperties->priv->statusbarVisible == statusBarVisible)
352 return;
353 windowProperties->priv->statusbarVisible = statusBarVisible;
354 g_object_notify(G_OBJECT(windowProperties), "statusbar-visible");
355}
356
357void webkitWindowPropertiesSetLocationbarVisible(WebKitWindowProperties* windowProperties, bool locationBarVisible)
358{
359 if (windowProperties->priv->locationbarVisible == locationBarVisible)
360 return;
361 windowProperties->priv->locationbarVisible = locationBarVisible;
362 g_object_notify(G_OBJECT(windowProperties), "locationbar-visible");
363}
364
365void webkitWindowPropertiesSetScrollbarsVisible(WebKitWindowProperties* windowProperties, bool scrollBarsVisible)
366{
367 if (windowProperties->priv->scrollbarsVisible == scrollBarsVisible)
368 return;
369 windowProperties->priv->scrollbarsVisible = scrollBarsVisible;
370 g_object_notify(G_OBJECT(windowProperties), "scrollbars-visible");
371}
372
373void webkitWindowPropertiesSetResizable(WebKitWindowProperties* windowProperties, bool resizable)
374{
375 if (windowProperties->priv->resizable == resizable)
376 return;
377 windowProperties->priv->resizable = resizable;
378 g_object_notify(G_OBJECT(windowProperties), "resizable");
379}
380
381void webkitWindowPropertiesSetFullscreen(WebKitWindowProperties* windowProperties, bool fullscreen)
382{
383 if (windowProperties->priv->fullscreen == fullscreen)
384 return;
385 windowProperties->priv->fullscreen = fullscreen;
386 g_object_notify(G_OBJECT(windowProperties), "fullscreen");
387}
388
389void webkitWindowPropertiesUpdateFromWebWindowFeatures(WebKitWindowProperties* windowProperties, const WindowFeatures& windowFeatures)
390{
391#if PLATFORM(GTK)
392 GdkRectangle geometry = windowProperties->priv->geometry;
393 if (windowFeatures.x)
394 geometry.x = *windowFeatures.x;
395 if (windowFeatures.y)
396 geometry.y = *windowFeatures.y;
397 if (windowFeatures.width)
398 geometry.width = *windowFeatures.width;
399 if (windowFeatures.height)
400 geometry.height = *windowFeatures.height;
401 webkitWindowPropertiesSetGeometry(windowProperties, &geometry);
402#endif
403
404 webkitWindowPropertiesSetMenubarVisible(windowProperties, windowFeatures.menuBarVisible);
405 webkitWindowPropertiesSetStatusbarVisible(windowProperties, windowFeatures.statusBarVisible);
406 webkitWindowPropertiesSetToolbarVisible(windowProperties, windowFeatures.toolBarVisible);
407 webkitWindowPropertiesSetLocationbarVisible(windowProperties, windowFeatures.locationBarVisible);
408 webkitWindowPropertiesSetScrollbarsVisible(windowProperties, windowFeatures.scrollbarsVisible);
409 webkitWindowPropertiesSetResizable(windowProperties, windowFeatures.resizable);
410 webkitWindowPropertiesSetFullscreen(windowProperties, windowFeatures.fullscreen);
411}
412
413#if PLATFORM(GTK)
414/**
415 * webkit_window_properties_get_geometry:
416 * @window_properties: a #WebKitWindowProperties
417 * @geometry: (out): return location for the window geometry
418 *
419 * Get the geometry the window should have on the screen when shown.
420 */
421void webkit_window_properties_get_geometry(WebKitWindowProperties* windowProperties, GdkRectangle* geometry)
422{
423 g_return_if_fail(WEBKIT_IS_WINDOW_PROPERTIES(windowProperties));
424 g_return_if_fail(geometry);
425
426 *geometry = windowProperties->priv->geometry;
427}
428#endif
429
430/**
431 * webkit_window_properties_get_toolbar_visible:
432 * @window_properties: a #WebKitWindowProperties
433 *
434 * Get whether the window should have the toolbar visible or not.
435 *
436 * Returns: %TRUE if toolbar should be visible or %FALSE otherwise.
437 */
438gboolean webkit_window_properties_get_toolbar_visible(WebKitWindowProperties* windowProperties)
439{
440 g_return_val_if_fail(WEBKIT_IS_WINDOW_PROPERTIES(windowProperties), TRUE);
441
442 return windowProperties->priv->toolbarVisible;
443}
444
445/**
446 * webkit_window_properties_get_statusbar_visible:
447 * @window_properties: a #WebKitWindowProperties
448 *
449 * Get whether the window should have the statusbar visible or not.
450 *
451 * Returns: %TRUE if statusbar should be visible or %FALSE otherwise.
452 */
453gboolean webkit_window_properties_get_statusbar_visible(WebKitWindowProperties* windowProperties)
454{
455 g_return_val_if_fail(WEBKIT_IS_WINDOW_PROPERTIES(windowProperties), TRUE);
456
457 return windowProperties->priv->statusbarVisible;
458}
459
460/**
461 * webkit_window_properties_get_scrollbars_visible:
462 * @window_properties: a #WebKitWindowProperties
463 *
464 * Get whether the window should have the scrollbars visible or not.
465 *
466 * Returns: %TRUE if scrollbars should be visible or %FALSE otherwise.
467 */
468gboolean webkit_window_properties_get_scrollbars_visible(WebKitWindowProperties* windowProperties)
469{
470 g_return_val_if_fail(WEBKIT_IS_WINDOW_PROPERTIES(windowProperties), TRUE);
471
472 return windowProperties->priv->scrollbarsVisible;
473}
474
475/**
476 * webkit_window_properties_get_menubar_visible:
477 * @window_properties: a #WebKitWindowProperties
478 *
479 * Get whether the window should have the menubar visible or not.
480 *
481 * Returns: %TRUE if menubar should be visible or %FALSE otherwise.
482 */
483gboolean webkit_window_properties_get_menubar_visible(WebKitWindowProperties* windowProperties)
484{
485 g_return_val_if_fail(WEBKIT_IS_WINDOW_PROPERTIES(windowProperties), TRUE);
486
487 return windowProperties->priv->menubarVisible;
488}
489
490/**
491 * webkit_window_properties_get_locationbar_visible:
492 * @window_properties: a #WebKitWindowProperties
493 *
494 * Get whether the window should have the locationbar visible or not.
495 *
496 * Returns: %TRUE if locationbar should be visible or %FALSE otherwise.
497 */
498gboolean webkit_window_properties_get_locationbar_visible(WebKitWindowProperties* windowProperties)
499{
500 g_return_val_if_fail(WEBKIT_IS_WINDOW_PROPERTIES(windowProperties), TRUE);
501
502 return windowProperties->priv->locationbarVisible;
503}
504
505/**
506 * webkit_window_properties_get_resizable:
507 * @window_properties: a #WebKitWindowProperties
508 *
509 * Get whether the window should be resizable by the user or not.
510 *
511 * Returns: %TRUE if the window should be resizable or %FALSE otherwise.
512 */
513gboolean webkit_window_properties_get_resizable(WebKitWindowProperties* windowProperties)
514{
515 g_return_val_if_fail(WEBKIT_IS_WINDOW_PROPERTIES(windowProperties), TRUE);
516
517 return windowProperties->priv->resizable;
518}
519
520/**
521 * webkit_window_properties_get_fullscreen:
522 * @window_properties: a #WebKitWindowProperties
523 *
524 * Get whether the window should be shown in fullscreen state or not.
525 *
526 * Returns: %TRUE if the window should be fullscreen or %FALSE otherwise.
527 */
528gboolean webkit_window_properties_get_fullscreen(WebKitWindowProperties* windowProperties)
529{
530 g_return_val_if_fail(WEBKIT_IS_WINDOW_PROPERTIES(windowProperties), FALSE);
531
532 return windowProperties->priv->fullscreen;
533}
534