Please add a version to the Gtk port
[WebKit-https.git] / WebKit / gtk / webkit / webkitwebsettings.cpp
1 /*
2  * Copyright (C) 2008 Christian Dywan <christian@imendio.com>
3  * Copyright (C) 2008 Nuanti Ltd.
4  * Copyright (C) 2008 Collabora Ltd.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include "config.h"
23
24 #include "webkitwebsettings.h"
25 #include "webkitprivate.h"
26
27 extern "C" {
28
29 G_DEFINE_TYPE(WebKitWebSettings, webkit_web_settings, G_TYPE_OBJECT)
30
31 struct _WebKitWebSettingsPrivate {
32     gchar* default_encoding;
33     gchar* cursive_font_family;
34     gchar* default_font_family;
35     gchar* fantasy_font_family;
36     gchar* monospace_font_family;
37     gchar* sans_serif_font_family;
38     gchar* serif_font_family;
39     guint default_font_size;
40     guint default_monospace_font_size;
41     guint minimum_font_size;
42     guint minimum_logical_font_size;
43     gboolean auto_load_images;
44     gboolean auto_shrink_images;
45     gboolean print_backgrounds;
46     gboolean enable_scripts;
47     gboolean enable_plugins;
48     gboolean resizable_text_areas;
49     gchar* user_stylesheet_uri;
50     gfloat zoom_step;
51 };
52
53 #define WEBKIT_WEB_SETTINGS_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_SETTINGS, WebKitWebSettingsPrivate))
54
55 enum {
56     PROP_0,
57
58     PROP_DEFAULT_ENCODING,
59     PROP_CURSIVE_FONT_FAMILY,
60     PROP_DEFAULT_FONT_FAMILY,
61     PROP_FANTASY_FONT_FAMILY,
62     PROP_MONOSPACE_FONT_FAMILY,
63     PROP_SANS_SERIF_FONT_FAMILY,
64     PROP_SERIF_FONT_FAMILY,
65     PROP_DEFAULT_FONT_SIZE,
66     PROP_DEFAULT_MONOSPACE_FONT_SIZE,
67     PROP_MINIMUM_FONT_SIZE,
68     PROP_MINIMUM_LOGICAL_FONT_SIZE,
69     PROP_AUTO_LOAD_IMAGES,
70     PROP_AUTO_SHRINK_IMAGES,
71     PROP_PRINT_BACKGROUNDS,
72     PROP_ENABLE_SCRIPTS,
73     PROP_ENABLE_PLUGINS,
74     PROP_RESIZABLE_TEXT_AREAS,
75     PROP_USER_STYLESHEET_URI,
76     PROP_ZOOM_STEP
77 };
78
79 static void webkit_web_settings_finalize(GObject* object);
80
81 static void webkit_web_settings_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec);
82
83 static void webkit_web_settings_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec);
84
85 static void webkit_web_settings_class_init(WebKitWebSettingsClass* klass)
86 {
87     GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
88     gobject_class->finalize = webkit_web_settings_finalize;
89     gobject_class->set_property = webkit_web_settings_set_property;
90     gobject_class->get_property = webkit_web_settings_get_property;
91
92     GParamFlags flags = (GParamFlags)(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT);
93
94     g_object_class_install_property(gobject_class,
95                                     PROP_DEFAULT_ENCODING,
96                                     g_param_spec_string(
97                                     "default-encoding",
98                                     "Default Encoding",
99                                     "The default encoding used to display text.",
100                                     "iso-8859-1",
101                                     flags));
102
103     g_object_class_install_property(gobject_class,
104                                     PROP_CURSIVE_FONT_FAMILY,
105                                     g_param_spec_string(
106                                     "cursive-font-family",
107                                     "Cursive Font Family",
108                                     "The default Cursive font family used to display text.",
109                                     "serif",
110                                     flags));
111
112     g_object_class_install_property(gobject_class,
113                                     PROP_DEFAULT_FONT_FAMILY,
114                                     g_param_spec_string(
115                                     "default-font-family",
116                                     "Default Font Family",
117                                     "The default font family used to display text.",
118                                     "sans-serif",
119                                     flags));
120
121     g_object_class_install_property(gobject_class,
122                                     PROP_FANTASY_FONT_FAMILY,
123                                     g_param_spec_string(
124                                     "fantasy-font-family",
125                                     "Fantasy Font Family",
126                                     "The default Fantasy font family used to display text.",
127                                     "serif",
128                                     flags));
129
130     g_object_class_install_property(gobject_class,
131                                     PROP_MONOSPACE_FONT_FAMILY,
132                                     g_param_spec_string(
133                                     "monospace-font-family",
134                                     "Monospace Font Family",
135                                     "The default font family used to display monospace text.",
136                                     "monospace",
137                                     flags));
138
139     g_object_class_install_property(gobject_class,
140                                     PROP_SANS_SERIF_FONT_FAMILY,
141                                     g_param_spec_string(
142                                     "sans-serif-font-family",
143                                     "Sans Serif Font Family",
144                                     "The default Sans Serif font family used to display text.",
145                                     "sans-serif",
146                                     flags));
147
148     g_object_class_install_property(gobject_class,
149                                     PROP_SERIF_FONT_FAMILY,
150                                     g_param_spec_string(
151                                     "serif-font-family",
152                                     "Serif Font Family",
153                                     "The default Serif font family used to display text.",
154                                     "serif",
155                                     flags));
156
157     g_object_class_install_property(gobject_class,
158                                     PROP_DEFAULT_FONT_SIZE,
159                                     g_param_spec_int(
160                                     "default-font-size",
161                                     "Default Font Size",
162                                     "The default font size used to display text.",
163                                     5, G_MAXINT, 10,
164                                     flags));
165
166     g_object_class_install_property(gobject_class,
167                                     PROP_DEFAULT_MONOSPACE_FONT_SIZE,
168                                     g_param_spec_int(
169                                     "default-monospace-font-size",
170                                     "Default Monospace Font Size",
171                                     "The default font size used to display monospace text.",
172                                     5, G_MAXINT, 10,
173                                     flags));
174
175     g_object_class_install_property(gobject_class,
176                                     PROP_MINIMUM_FONT_SIZE,
177                                     g_param_spec_int(
178                                     "minimum-font-size",
179                                     "Minimum Font Size",
180                                     "The minimum font size used to display text.",
181                                     1, G_MAXINT, 5,
182                                     flags));
183
184     g_object_class_install_property(gobject_class,
185                                     PROP_MINIMUM_LOGICAL_FONT_SIZE,
186                                     g_param_spec_int(
187                                     "minimum-logical-font-size",
188                                     "Minimum Logical Font Size",
189                                     "The minimum logical font size used to display text.",
190                                     1, G_MAXINT, 5,
191                                     flags));
192
193     g_object_class_install_property(gobject_class,
194                                     PROP_AUTO_LOAD_IMAGES,
195                                     g_param_spec_boolean(
196                                     "auto-load-images",
197                                     "Auto Load Images",
198                                     "Load images automatically.",
199                                     TRUE,
200                                     flags));
201
202     g_object_class_install_property(gobject_class,
203                                     PROP_AUTO_SHRINK_IMAGES,
204                                     g_param_spec_boolean(
205                                     "auto-shrink-images",
206                                     "Auto Shrink Images",
207                                     "Automatically shrink standalone images to fit.",
208                                     TRUE,
209                                     flags));
210
211     g_object_class_install_property(gobject_class,
212                                     PROP_PRINT_BACKGROUNDS,
213                                     g_param_spec_boolean(
214                                     "print-backgrounds",
215                                     "Print Backgrounds",
216                                     "Whether background images should be printed.",
217                                     TRUE,
218                                     flags));
219
220     g_object_class_install_property(gobject_class,
221                                     PROP_ENABLE_SCRIPTS,
222                                     g_param_spec_boolean(
223                                     "enable-scripts",
224                                     "Enable Scripts",
225                                     "Enable embedded scripting languages.",
226                                     TRUE,
227                                     flags));
228
229     g_object_class_install_property(gobject_class,
230                                     PROP_ENABLE_PLUGINS,
231                                     g_param_spec_boolean(
232                                     "enable-plugins",
233                                     "Enable Plugins",
234                                     "Enable embedded plugin objects.",
235                                     TRUE,
236                                     flags));
237
238     g_object_class_install_property(gobject_class,
239                                     PROP_RESIZABLE_TEXT_AREAS,
240                                     g_param_spec_boolean(
241                                     "resizable-text-areas",
242                                     "Resizable Text Areas",
243                                     "Whether text areas are resizable.",
244                                     TRUE,
245                                     flags));
246
247     g_object_class_install_property(gobject_class,
248                                     PROP_USER_STYLESHEET_URI,
249                                     g_param_spec_string("user-stylesheet-uri",
250                                     "User Stylesheet URI",
251                                     "The URI of a stylesheet that is applied to every page.",
252                                     0,
253                                     flags));
254
255     /**
256     * WebKitWebSettings:zoom-step:
257     *
258     * The value by which the zoom level is changed when zooming in or out.
259     *
260     * Since: 1.0.1
261     */
262     g_object_class_install_property(gobject_class,
263                                     PROP_ZOOM_STEP,
264                                     g_param_spec_float(
265                                     "zoom-step",
266                                     "Zoom Stepping Value",
267                                     "The value by which the zoom level is changed when zooming in or out.",
268                                     0.0f, G_MAXFLOAT, 0.1f,
269                                     flags));
270
271     g_type_class_add_private(klass, sizeof(WebKitWebSettingsPrivate));
272 }
273
274 static void webkit_web_settings_init(WebKitWebSettings* web_settings)
275 {
276     web_settings->priv = WEBKIT_WEB_SETTINGS_GET_PRIVATE(web_settings);
277 }
278
279 static void webkit_web_settings_finalize(GObject* object)
280 {
281     WebKitWebSettings* web_settings = WEBKIT_WEB_SETTINGS(object);
282     WebKitWebSettingsPrivate* priv = web_settings->priv;
283
284     g_free(priv->default_encoding);
285     g_free(priv->cursive_font_family);
286     g_free(priv->default_font_family);
287     g_free(priv->fantasy_font_family);
288     g_free(priv->monospace_font_family);
289     g_free(priv->sans_serif_font_family);
290     g_free(priv->serif_font_family);
291     g_free(priv->user_stylesheet_uri);
292
293     G_OBJECT_CLASS(webkit_web_settings_parent_class)->finalize(object);
294 }
295
296 static void webkit_web_settings_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec)
297 {
298     WebKitWebSettings* web_settings = WEBKIT_WEB_SETTINGS(object);
299     WebKitWebSettingsPrivate* priv = web_settings->priv;
300
301     switch(prop_id) {
302     case PROP_DEFAULT_ENCODING:
303         g_free(priv->default_encoding);
304         priv->default_encoding = g_strdup(g_value_get_string(value));
305         break;
306     case PROP_CURSIVE_FONT_FAMILY:
307         g_free(priv->cursive_font_family);
308         priv->cursive_font_family = g_strdup(g_value_get_string(value));
309         break;
310     case PROP_DEFAULT_FONT_FAMILY:
311         g_free(priv->default_font_family);
312         priv->default_font_family = g_strdup(g_value_get_string(value));
313         break;
314     case PROP_FANTASY_FONT_FAMILY:
315         g_free(priv->fantasy_font_family);
316         priv->fantasy_font_family = g_strdup(g_value_get_string(value));
317         break;
318     case PROP_MONOSPACE_FONT_FAMILY:
319         g_free(priv->monospace_font_family);
320         priv->monospace_font_family = g_strdup(g_value_get_string(value));
321         break;
322     case PROP_SANS_SERIF_FONT_FAMILY:
323         g_free(priv->sans_serif_font_family);
324         priv->sans_serif_font_family = g_strdup(g_value_get_string(value));
325         break;
326     case PROP_SERIF_FONT_FAMILY:
327         g_free(priv->serif_font_family);
328         priv->serif_font_family = g_strdup(g_value_get_string(value));
329         break;
330     case PROP_DEFAULT_FONT_SIZE:
331         priv->default_font_size = g_value_get_int(value);
332         break;
333     case PROP_DEFAULT_MONOSPACE_FONT_SIZE:
334         priv->default_monospace_font_size = g_value_get_int(value);
335         break;
336     case PROP_MINIMUM_FONT_SIZE:
337         priv->minimum_font_size = g_value_get_int(value);
338         break;
339     case PROP_MINIMUM_LOGICAL_FONT_SIZE:
340         priv->minimum_logical_font_size = g_value_get_int(value);
341         break;
342     case PROP_AUTO_LOAD_IMAGES:
343         priv->auto_load_images = g_value_get_boolean(value);
344         break;
345     case PROP_AUTO_SHRINK_IMAGES:
346         priv->auto_shrink_images = g_value_get_boolean(value);
347         break;
348     case PROP_PRINT_BACKGROUNDS:
349         priv->print_backgrounds = g_value_get_boolean(value);
350         break;
351     case PROP_ENABLE_SCRIPTS:
352         priv->enable_scripts = g_value_get_boolean(value);
353         break;
354     case PROP_ENABLE_PLUGINS:
355         priv->enable_plugins = g_value_get_boolean(value);
356         break;
357     case PROP_RESIZABLE_TEXT_AREAS:
358         priv->resizable_text_areas = g_value_get_boolean(value);
359         break;
360     case PROP_USER_STYLESHEET_URI:
361         g_free(priv->user_stylesheet_uri);
362         priv->user_stylesheet_uri = g_strdup(g_value_get_string(value));
363         break;
364     case PROP_ZOOM_STEP:
365         priv->zoom_step = g_value_get_float(value);
366         break;
367     default:
368         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
369         break;
370     }
371 }
372
373 static void webkit_web_settings_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec)
374 {
375     WebKitWebSettings* web_settings = WEBKIT_WEB_SETTINGS(object);
376     WebKitWebSettingsPrivate* priv = web_settings->priv;
377
378     switch (prop_id) {
379     case PROP_DEFAULT_ENCODING:
380         g_value_set_string(value, priv->default_encoding);
381         break;
382     case PROP_CURSIVE_FONT_FAMILY:
383         g_value_set_string(value, priv->cursive_font_family);
384         break;
385     case PROP_DEFAULT_FONT_FAMILY:
386         g_value_set_string(value, priv->default_font_family);
387         break;
388     case PROP_FANTASY_FONT_FAMILY:
389         g_value_set_string(value, priv->fantasy_font_family);
390         break;
391     case PROP_MONOSPACE_FONT_FAMILY:
392         g_value_set_string(value, priv->monospace_font_family);
393         break;
394     case PROP_SANS_SERIF_FONT_FAMILY:
395         g_value_set_string(value, priv->sans_serif_font_family);
396         break;
397     case PROP_SERIF_FONT_FAMILY:
398         g_value_set_string(value, priv->serif_font_family);
399         break;
400     case PROP_DEFAULT_FONT_SIZE:
401         g_value_set_int(value, priv->default_font_size);
402         break;
403     case PROP_DEFAULT_MONOSPACE_FONT_SIZE:
404         g_value_set_int(value, priv->default_monospace_font_size);
405         break;
406     case PROP_MINIMUM_FONT_SIZE:
407         g_value_set_int(value, priv->minimum_font_size);
408         break;
409     case PROP_MINIMUM_LOGICAL_FONT_SIZE:
410         g_value_set_int(value, priv->minimum_logical_font_size);
411         break;
412     case PROP_AUTO_LOAD_IMAGES:
413         g_value_set_boolean(value, priv->auto_load_images);
414         break;
415     case PROP_AUTO_SHRINK_IMAGES:
416         g_value_set_boolean(value, priv->auto_shrink_images);
417         break;
418     case PROP_PRINT_BACKGROUNDS:
419         g_value_set_boolean(value, priv->print_backgrounds);
420         break;
421     case PROP_ENABLE_SCRIPTS:
422         g_value_set_boolean(value, priv->enable_scripts);
423         break;
424     case PROP_ENABLE_PLUGINS:
425         g_value_set_boolean(value, priv->enable_plugins);
426         break;
427     case PROP_RESIZABLE_TEXT_AREAS:
428         g_value_set_boolean(value, priv->resizable_text_areas);
429         break;
430     case PROP_USER_STYLESHEET_URI:
431         g_value_set_string(value, priv->user_stylesheet_uri);
432         break;
433     case PROP_ZOOM_STEP:
434         g_value_set_float(value, priv->zoom_step);
435         break;
436     default:
437         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
438         break;
439     }
440 }
441
442 /**
443  * webkit_web_settings_new:
444  *
445  * Creates a new #WebKitWebSettings instance with default values. It must
446  * be manually attached to a WebView.
447  *
448  * Returns: a new #WebKitWebSettings instance
449  **/
450 WebKitWebSettings* webkit_web_settings_new()
451 {
452     return WEBKIT_WEB_SETTINGS(g_object_new(WEBKIT_TYPE_WEB_SETTINGS, NULL));
453 }
454
455 /**
456  * webkit_web_settings_copy:
457  *
458  * Copies an existing #WebKitWebSettings instance.
459  *
460  * Returns: a new #WebKitWebSettings instance
461  **/
462 WebKitWebSettings* webkit_web_settings_copy(WebKitWebSettings* web_settings)
463 {
464     WebKitWebSettingsPrivate* priv = web_settings->priv;
465
466     WebKitWebSettings* copy = WEBKIT_WEB_SETTINGS(g_object_new(WEBKIT_TYPE_WEB_SETTINGS,
467                  "default-encoding", priv->default_encoding,
468                  "cursive-font-family", priv->cursive_font_family,
469                  "default-font-family", priv->default_font_family,
470                  "fantasy-font-family", priv->fantasy_font_family,
471                  "monospace-font-family", priv->monospace_font_family,
472                  "sans-serif-font-family", priv->sans_serif_font_family,
473                  "serif-font-family", priv->serif_font_family,
474                  "default-font-size", priv->default_font_size,
475                  "default-monospace-font-size", priv->default_monospace_font_size,
476                  "minimum-font-size", priv->minimum_font_size,
477                  "minimum-logical-font-size", priv->minimum_logical_font_size,
478                  "auto-load-images", priv->auto_load_images,
479                  "auto-shrink-images", priv->auto_shrink_images,
480                  "print-backgrounds", priv->print_backgrounds,
481                  "enable-scripts", priv->enable_scripts,
482                  "enable-plugins", priv->enable_plugins,
483                  "resizable-text-areas", priv->resizable_text_areas,
484                  "user-stylesheet-uri", priv->user_stylesheet_uri,
485                  "zoom-step", priv->zoom_step,
486                  NULL));
487
488     return copy;
489 }
490
491 }