BrowserSettingsDialog.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include "BrowserSettingsDialog.h"
  26. #include "BrowserCellRendererVariant.h"
  27. enum {
  28. PROP_0,
  29. PROP_SETTINGS
  30. };
  31. enum {
  32. SETTINGS_LIST_COLUMN_NAME,
  33. SETTINGS_LIST_COLUMN_NICK,
  34. SETTINGS_LIST_COLUMN_BLURB,
  35. SETTINGS_LIST_COLUMN_VALUE,
  36. SETTINGS_LIST_COLUMN_ADJUSTMENT,
  37. SETTINGS_LIST_N_COLUMNS
  38. };
  39. struct _BrowserSettingsDialog {
  40. GtkDialog parent;
  41. GtkWidget *settingsList;
  42. WebKitSettings *settings;
  43. };
  44. struct _BrowserSettingsDialogClass {
  45. GtkDialogClass parent;
  46. };
  47. G_DEFINE_TYPE(BrowserSettingsDialog, browser_settings_dialog, GTK_TYPE_DIALOG)
  48. static void cellRendererChanged(GtkCellRenderer *renderer, const char *path, const GValue *value, BrowserSettingsDialog *dialog)
  49. {
  50. GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(dialog->settingsList));
  51. GtkTreePath *treePath = gtk_tree_path_new_from_string(path);
  52. GtkTreeIter iter;
  53. gtk_tree_model_get_iter(model, &iter, treePath);
  54. char *name;
  55. gtk_tree_model_get(model, &iter, SETTINGS_LIST_COLUMN_NAME, &name, -1);
  56. g_object_set_property(G_OBJECT(dialog->settings), name, value);
  57. g_free(name);
  58. gtk_list_store_set(GTK_LIST_STORE(model), &iter, SETTINGS_LIST_COLUMN_VALUE, value, -1);
  59. gtk_tree_path_free(treePath);
  60. }
  61. static void browserSettingsDialogSetProperty(GObject *object, guint propId, const GValue *value, GParamSpec *pspec)
  62. {
  63. BrowserSettingsDialog *dialog = BROWSER_SETTINGS_DIALOG(object);
  64. switch (propId) {
  65. case PROP_SETTINGS:
  66. dialog->settings = g_value_get_object(value);
  67. break;
  68. default:
  69. G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, pspec);
  70. }
  71. }
  72. static void browser_settings_dialog_init(BrowserSettingsDialog *dialog)
  73. {
  74. GtkBox *contentArea = GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
  75. gtk_box_set_spacing(contentArea, 2);
  76. gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 300);
  77. gtk_window_set_title(GTK_WINDOW(dialog), "WebKit View Settings");
  78. gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
  79. gtk_container_set_border_width(GTK_CONTAINER(dialog), 5);
  80. gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
  81. gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
  82. GtkWidget *scrolledWindow = gtk_scrolled_window_new(NULL, NULL);
  83. dialog->settingsList = gtk_tree_view_new();
  84. GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
  85. gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(dialog->settingsList),
  86. 0, "Name", renderer,
  87. "text", SETTINGS_LIST_COLUMN_NICK,
  88. NULL);
  89. renderer = browser_cell_renderer_variant_new();
  90. g_signal_connect(renderer, "changed", G_CALLBACK(cellRendererChanged), dialog);
  91. gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(dialog->settingsList),
  92. 1, "Value", renderer,
  93. "value", SETTINGS_LIST_COLUMN_VALUE,
  94. "adjustment", SETTINGS_LIST_COLUMN_ADJUSTMENT,
  95. NULL);
  96. gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(dialog->settingsList), SETTINGS_LIST_COLUMN_BLURB);
  97. gtk_container_add(GTK_CONTAINER(scrolledWindow), dialog->settingsList);
  98. gtk_widget_show(dialog->settingsList);
  99. gtk_box_pack_start(contentArea, scrolledWindow, TRUE, TRUE, 0);
  100. gtk_widget_show(scrolledWindow);
  101. g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), NULL);
  102. }
  103. static void browserSettingsDialogConstructed(GObject *object)
  104. {
  105. BrowserSettingsDialog *dialog = BROWSER_SETTINGS_DIALOG(object);
  106. WebKitSettings *settings = dialog->settings;
  107. GtkListStore *model = gtk_list_store_new(SETTINGS_LIST_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
  108. G_TYPE_VALUE, G_TYPE_OBJECT);
  109. guint propertiesCount;
  110. GParamSpec **properties = g_object_class_list_properties(G_OBJECT_GET_CLASS(settings), &propertiesCount);
  111. guint i;
  112. for (i = 0; i < propertiesCount; i++) {
  113. GParamSpec *property = properties[i];
  114. const char *name = g_param_spec_get_name(property);
  115. const char *nick = g_param_spec_get_nick(property);
  116. GValue value = { 0, { { 0 } } };
  117. g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE(property));
  118. g_object_get_property(G_OBJECT(settings), name, &value);
  119. GtkAdjustment *adjustment = NULL;
  120. if (G_PARAM_SPEC_VALUE_TYPE(property) == G_TYPE_UINT) {
  121. GParamSpecUInt *uIntProperty = G_PARAM_SPEC_UINT(property);
  122. adjustment = gtk_adjustment_new(uIntProperty->default_value, uIntProperty->minimum,
  123. uIntProperty->maximum, 1, 1, 1);
  124. }
  125. char *blurb = g_markup_escape_text(g_param_spec_get_blurb(property), -1);
  126. GtkTreeIter iter;
  127. gtk_list_store_append(model, &iter);
  128. gtk_list_store_set(model, &iter,
  129. SETTINGS_LIST_COLUMN_NAME, name,
  130. SETTINGS_LIST_COLUMN_NICK, nick,
  131. SETTINGS_LIST_COLUMN_BLURB, blurb,
  132. SETTINGS_LIST_COLUMN_VALUE, &value,
  133. SETTINGS_LIST_COLUMN_ADJUSTMENT, adjustment,
  134. -1);
  135. g_free(blurb);
  136. g_value_unset(&value);
  137. }
  138. g_free(properties);
  139. gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->settingsList), GTK_TREE_MODEL(model));
  140. g_object_unref(model);
  141. }
  142. static void browser_settings_dialog_class_init(BrowserSettingsDialogClass *klass)
  143. {
  144. GObjectClass *gobjectClass = G_OBJECT_CLASS(klass);
  145. gobjectClass->constructed = browserSettingsDialogConstructed;
  146. gobjectClass->set_property = browserSettingsDialogSetProperty;
  147. g_object_class_install_property(gobjectClass,
  148. PROP_SETTINGS,
  149. g_param_spec_object("settings",
  150. "Settings",
  151. "The WebKitSettings",
  152. WEBKIT_TYPE_SETTINGS,
  153. G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
  154. }
  155. GtkWidget *browser_settings_dialog_new(WebKitSettings *settings)
  156. {
  157. g_return_val_if_fail(WEBKIT_IS_SETTINGS(settings), NULL);
  158. return GTK_WIDGET(g_object_new(BROWSER_TYPE_SETTINGS_DIALOG, "settings", settings, NULL));
  159. }