OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/gtk/website_settings/permission_selector.h" |
| 6 |
| 7 #include <gtk/gtk.h> |
| 8 |
| 9 #include "base/compiler_specific.h" |
| 10 #include "chrome/browser/ui/gtk/gtk_theme_service.h" |
| 11 #include "chrome/browser/ui/gtk/gtk_util.h" |
| 12 #include "chrome/browser/ui/website_settings/website_settings_ui.h" |
| 13 #include "grit/generated_resources.h" |
| 14 #include "ui/base/gtk/gtk_hig_constants.h" |
| 15 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "ui/gfx/image/image.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 GtkWidget* CreateTextLabel(const std::string& text, |
| 21 int width, |
| 22 GtkThemeService* theme_service) { |
| 23 GtkWidget* label = theme_service->BuildLabel(text, ui::kGdkBlack); |
| 24 if (width > 0) |
| 25 gtk_util::SetLabelWidth(label, width); |
| 26 gtk_label_set_selectable(GTK_LABEL(label), TRUE); |
| 27 gtk_label_set_line_wrap_mode(GTK_LABEL(label), PANGO_WRAP_WORD_CHAR); |
| 28 return label; |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 33 PermissionSelector::PermissionSelector(GtkThemeService* theme_service, |
| 34 ContentSettingsType type, |
| 35 ContentSetting setting, |
| 36 ContentSetting default_setting) |
| 37 : theme_service_(theme_service), |
| 38 type_(type), |
| 39 setting_(setting), |
| 40 default_setting_(default_setting), |
| 41 icon_(NULL) { |
| 42 DCHECK_NE(default_setting, CONTENT_SETTING_DEFAULT); |
| 43 } |
| 44 |
| 45 PermissionSelector::~PermissionSelector() { |
| 46 } |
| 47 |
| 48 GtkWidget* PermissionSelector::CreateUI() { |
| 49 // Create permission info box. |
| 50 const int kChildSpacing = 4; |
| 51 GtkWidget* hbox = gtk_hbox_new(FALSE, kChildSpacing); |
| 52 |
| 53 // Add permission type icon. |
| 54 GdkPixbuf* pixbuf = WebsiteSettingsUI::GetPermissionIcon( |
| 55 type_, setting_).ToGdkPixbuf(); |
| 56 icon_ = gtk_image_new_from_pixbuf(pixbuf); |
| 57 gtk_box_pack_start(GTK_BOX(hbox), icon_, FALSE, FALSE, 0); |
| 58 |
| 59 // Add a label for the permission type. |
| 60 GtkWidget* label = CreateTextLabel( |
| 61 l10n_util::GetStringFUTF8( |
| 62 IDS_WEBSITE_SETTINGS_PERMISSION_TYPE, |
| 63 WebsiteSettingsUI::PermissionTypeToUIString(type_)), |
| 64 0, |
| 65 theme_service_); |
| 66 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); |
| 67 |
| 68 GtkListStore* store = |
| 69 gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT); |
| 70 GtkTreeIter iter; |
| 71 // Add option for permission "Global Default" to the combobox model. |
| 72 std::string setting_str = l10n_util::GetStringFUTF8( |
| 73 IDS_WEBSITE_SETTINGS_DEFAULT_PERMISSION_LABEL, |
| 74 WebsiteSettingsUI::PermissionValueToUIString(default_setting_)); |
| 75 gtk_list_store_append(store, &iter); |
| 76 gtk_list_store_set(store, &iter, 0, setting_str.c_str(), 1, |
| 77 CONTENT_SETTING_DEFAULT, -1); |
| 78 GtkWidget* combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store)); |
| 79 // Add option for permission "Allow" to the combobox model. |
| 80 setting_str = l10n_util::GetStringFUTF8( |
| 81 IDS_WEBSITE_SETTINGS_PERMISSION_LABEL, |
| 82 WebsiteSettingsUI::PermissionValueToUIString(CONTENT_SETTING_ALLOW)); |
| 83 gtk_list_store_append(store, &iter); |
| 84 gtk_list_store_set(store, &iter, 0, setting_str.c_str(), 1, |
| 85 CONTENT_SETTING_ALLOW, -1); |
| 86 // The content settings type fullscreen does not support the concept of |
| 87 // blocking. |
| 88 if (type_ != CONTENT_SETTINGS_TYPE_FULLSCREEN) { |
| 89 // Add option for permission "BLOCK" to the combobox model. |
| 90 setting_str = l10n_util::GetStringFUTF8( |
| 91 IDS_WEBSITE_SETTINGS_PERMISSION_LABEL, |
| 92 WebsiteSettingsUI::PermissionValueToUIString(CONTENT_SETTING_BLOCK)); |
| 93 gtk_list_store_append(store, &iter); |
| 94 gtk_list_store_set(store, &iter, 0, setting_str.c_str(), 1, |
| 95 CONTENT_SETTING_BLOCK, -1); |
| 96 } |
| 97 // Remove reference to the store to prevent leaking. |
| 98 g_object_unref(G_OBJECT(store)); |
| 99 |
| 100 GtkCellRenderer* cell = gtk_cell_renderer_text_new(); |
| 101 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), cell, TRUE ); |
| 102 gtk_cell_layout_set_attributes( |
| 103 GTK_CELL_LAYOUT(combo_box), cell, "text", 0, NULL); |
| 104 |
| 105 // Select the combobox entry for the currently configured permission value. |
| 106 int active = -1; |
| 107 switch (setting_) { |
| 108 case CONTENT_SETTING_DEFAULT: active = 0; |
| 109 break; |
| 110 case CONTENT_SETTING_ALLOW: active = 1; |
| 111 break; |
| 112 case CONTENT_SETTING_BLOCK: active = 2; |
| 113 break; |
| 114 default: |
| 115 NOTREACHED() << "Bad content setting:" << setting_; |
| 116 break; |
| 117 } |
| 118 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), active); |
| 119 |
| 120 // Add change listener to the combobox. |
| 121 g_signal_connect(combo_box, "changed", |
| 122 G_CALLBACK(OnPermissionChangedThunk), this); |
| 123 // Once the popup (window) for a combobox is shown, the bubble container |
| 124 // (window) loses it's focus. Therefore it necessary to reset the focus to |
| 125 // the bubble container after the combobox popup is closed. |
| 126 g_signal_connect(combo_box, "notify::popup-shown", |
| 127 G_CALLBACK(OnComboBoxShownThunk), this); |
| 128 gtk_box_pack_start(GTK_BOX(hbox), combo_box, FALSE, FALSE, 0); |
| 129 |
| 130 return hbox; |
| 131 } |
| 132 |
| 133 void PermissionSelector::AddObserver(PermissionSelectorObserver* observer) { |
| 134 observer_list_.AddObserver(observer); |
| 135 } |
| 136 |
| 137 void PermissionSelector::OnPermissionChanged(GtkWidget* widget) { |
| 138 // Get the selected setting. |
| 139 GtkTreeIter it; |
| 140 bool has_active = gtk_combo_box_get_active_iter(GTK_COMBO_BOX(widget), &it); |
| 141 DCHECK(has_active); |
| 142 GtkTreeModel* store = |
| 143 GTK_TREE_MODEL(gtk_combo_box_get_model(GTK_COMBO_BOX(widget))); |
| 144 int value = -1; |
| 145 gtk_tree_model_get(store, &it, 1, &value, -1); |
| 146 setting_ = ContentSetting(value); |
| 147 |
| 148 // Change the permission icon according the the selected setting. |
| 149 GdkPixbuf* pixbuf = WebsiteSettingsUI::GetPermissionIcon( |
| 150 type_, setting_).ToGdkPixbuf(); |
| 151 gtk_image_set_from_pixbuf(GTK_IMAGE(icon_), pixbuf); |
| 152 |
| 153 FOR_EACH_OBSERVER(PermissionSelectorObserver, |
| 154 observer_list_, |
| 155 OnPermissionChanged(this)); |
| 156 } |
| 157 |
| 158 void PermissionSelector::OnComboBoxShown(GtkWidget* widget, |
| 159 GParamSpec* property) { |
| 160 // GtkComboBox grabs the keyboard and pointer when it displays its popup, |
| 161 // which steals the grabs that BubbleGtk had installed. When the popup is |
| 162 // hidden, we notify BubbleGtk so it can try to reacquire the grabs |
| 163 // (otherwise, GTK won't activate our widgets when the user clicks in them). |
| 164 // When then combobox popup is closed we notify the |
| 165 // |PermissionSelectorObserver|s so that BubbleGtk can grab the keyboard and |
| 166 // pointer. |
| 167 gboolean popup_shown = FALSE; |
| 168 g_object_get(G_OBJECT(GTK_COMBO_BOX(widget)), "popup-shown", &popup_shown, |
| 169 NULL); |
| 170 if (!popup_shown) { |
| 171 FOR_EACH_OBSERVER(PermissionSelectorObserver, |
| 172 observer_list_, |
| 173 OnComboboxShown()); |
| 174 } |
| 175 } |
OLD | NEW |