bmalloc: Miscellaneous cleanup
[WebKit-https.git] / Tools / MiniBrowser / gtk / BrowserSettingsDialog.c
1 /*
2  * Copyright (C) 2011 Igalia S.L.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "BrowserSettingsDialog.h"
27 #include "BrowserCellRendererVariant.h"
28
29 enum {
30     PROP_0,
31
32     PROP_SETTINGS
33 };
34
35 enum {
36     SETTINGS_LIST_COLUMN_NAME,
37     SETTINGS_LIST_COLUMN_NICK,
38     SETTINGS_LIST_COLUMN_BLURB,
39     SETTINGS_LIST_COLUMN_VALUE,
40     SETTINGS_LIST_COLUMN_ADJUSTMENT,
41
42     SETTINGS_LIST_N_COLUMNS
43 };
44
45 struct _BrowserSettingsDialog {
46     GtkDialog parent;
47
48     GtkWidget *settingsList;
49     WebKitSettings *settings;
50 };
51
52 struct _BrowserSettingsDialogClass {
53     GtkDialogClass parent;
54 };
55
56 G_DEFINE_TYPE(BrowserSettingsDialog, browser_settings_dialog, GTK_TYPE_DIALOG)
57
58 static void cellRendererChanged(GtkCellRenderer *renderer, const char *path, const GValue *value, BrowserSettingsDialog *dialog)
59 {
60     GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(dialog->settingsList));
61     GtkTreePath *treePath = gtk_tree_path_new_from_string(path);
62     GtkTreeIter iter;
63     gtk_tree_model_get_iter(model, &iter, treePath);
64
65     char *name;
66     gtk_tree_model_get(model, &iter, SETTINGS_LIST_COLUMN_NAME, &name, -1);
67     g_object_set_property(G_OBJECT(dialog->settings), name, value);
68     g_free(name);
69
70     gtk_list_store_set(GTK_LIST_STORE(model), &iter, SETTINGS_LIST_COLUMN_VALUE, value, -1);
71     gtk_tree_path_free(treePath);
72 }
73
74 static void browserSettingsDialogSetProperty(GObject *object, guint propId, const GValue *value, GParamSpec *pspec)
75 {
76     BrowserSettingsDialog *dialog = BROWSER_SETTINGS_DIALOG(object);
77
78     switch (propId) {
79     case PROP_SETTINGS:
80         dialog->settings = g_value_get_object(value);
81         break;
82     default:
83         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, pspec);
84     }
85 }
86
87 static void browser_settings_dialog_init(BrowserSettingsDialog *dialog)
88 {
89     GtkBox *contentArea = GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
90     gtk_box_set_spacing(contentArea, 2);
91
92     gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 300);
93     gtk_window_set_title(GTK_WINDOW(dialog), "WebKit View Settings");
94     gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
95     gtk_container_set_border_width(GTK_CONTAINER(dialog), 5);
96     gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
97     gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
98
99     GtkWidget *scrolledWindow = gtk_scrolled_window_new(NULL, NULL);
100     dialog->settingsList = gtk_tree_view_new();
101     GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
102     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(dialog->settingsList),
103                                                 0, "Name", renderer,
104                                                 "text", SETTINGS_LIST_COLUMN_NICK,
105                                                 NULL);
106     renderer = browser_cell_renderer_variant_new();
107     g_signal_connect(renderer, "changed", G_CALLBACK(cellRendererChanged), dialog);
108     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(dialog->settingsList),
109                                                 1, "Value", renderer,
110                                                 "value", SETTINGS_LIST_COLUMN_VALUE,
111                                                 "adjustment", SETTINGS_LIST_COLUMN_ADJUSTMENT,
112                                                 NULL);
113     gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(dialog->settingsList), SETTINGS_LIST_COLUMN_BLURB);
114
115     gtk_container_add(GTK_CONTAINER(scrolledWindow), dialog->settingsList);
116     gtk_widget_show(dialog->settingsList);
117
118     gtk_box_pack_start(contentArea, scrolledWindow, TRUE, TRUE, 0);
119     gtk_widget_show(scrolledWindow);
120
121     g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), NULL);
122 }
123
124 static void browserSettingsDialogConstructed(GObject *object)
125 {
126     BrowserSettingsDialog *dialog = BROWSER_SETTINGS_DIALOG(object);
127     WebKitSettings *settings = dialog->settings;
128     GtkListStore *model = gtk_list_store_new(SETTINGS_LIST_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
129                                              G_TYPE_VALUE, G_TYPE_OBJECT);
130     guint propertiesCount;
131     GParamSpec **properties = g_object_class_list_properties(G_OBJECT_GET_CLASS(settings), &propertiesCount);
132     guint i;
133     for (i = 0; i < propertiesCount; i++) {
134         GParamSpec *property = properties[i];
135         const char *name = g_param_spec_get_name(property);
136         const char *nick = g_param_spec_get_nick(property);
137
138         GValue value = { 0, { { 0 } } };
139         g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE(property));
140         g_object_get_property(G_OBJECT(settings), name, &value);
141
142         GtkAdjustment *adjustment = NULL;
143         if (G_PARAM_SPEC_VALUE_TYPE(property) == G_TYPE_UINT) {
144             GParamSpecUInt *uIntProperty = G_PARAM_SPEC_UINT(property);
145             adjustment = gtk_adjustment_new(uIntProperty->default_value, uIntProperty->minimum,
146                                             uIntProperty->maximum, 1, 1, 1);
147         }
148
149         char *blurb = g_markup_escape_text(g_param_spec_get_blurb(property), -1);
150         GtkTreeIter iter;
151         gtk_list_store_append(model, &iter);
152         gtk_list_store_set(model, &iter,
153                            SETTINGS_LIST_COLUMN_NAME, name,
154                            SETTINGS_LIST_COLUMN_NICK, nick,
155                            SETTINGS_LIST_COLUMN_BLURB, blurb,
156                            SETTINGS_LIST_COLUMN_VALUE, &value,
157                            SETTINGS_LIST_COLUMN_ADJUSTMENT, adjustment,
158                            -1);
159         g_free(blurb);
160         g_value_unset(&value);
161     }
162     g_free(properties);
163
164     gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->settingsList), GTK_TREE_MODEL(model));
165     g_object_unref(model);
166 }
167
168 static void browser_settings_dialog_class_init(BrowserSettingsDialogClass *klass)
169 {
170     GObjectClass *gobjectClass = G_OBJECT_CLASS(klass);
171
172     gobjectClass->constructed = browserSettingsDialogConstructed;
173     gobjectClass->set_property = browserSettingsDialogSetProperty;
174
175     g_object_class_install_property(gobjectClass,
176                                     PROP_SETTINGS,
177                                     g_param_spec_object("settings",
178                                                         "Settings",
179                                                         "The WebKitSettings",
180                                                         WEBKIT_TYPE_SETTINGS,
181                                                         G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
182 }
183
184 GtkWidget *browser_settings_dialog_new(WebKitSettings *settings)
185 {
186     g_return_val_if_fail(WEBKIT_IS_SETTINGS(settings), NULL);
187
188     return GTK_WIDGET(g_object_new(BROWSER_TYPE_SETTINGS_DIALOG, "settings", settings, NULL));
189 }
190