| Index: chrome/browser/ui/gtk/website_settings/permission_selector.cc
|
| diff --git a/chrome/browser/ui/gtk/website_settings/permission_selector.cc b/chrome/browser/ui/gtk/website_settings/permission_selector.cc
|
| index 3a5f30ae0d2c36f73e2427c812057fe936a6307e..9a2222f7d1862d0303dab337c50192b123aaf274 100644
|
| --- a/chrome/browser/ui/gtk/website_settings/permission_selector.cc
|
| +++ b/chrome/browser/ui/gtk/website_settings/permission_selector.cc
|
| @@ -4,25 +4,35 @@
|
|
|
| #include "chrome/browser/ui/gtk/website_settings/permission_selector.h"
|
|
|
| -#include <gtk/gtk.h>
|
| -
|
| #include "base/compiler_specific.h"
|
| +#include "base/i18n/rtl.h"
|
| +#include "base/utf_string_conversions.h"
|
| #include "chrome/browser/ui/gtk/gtk_theme_service.h"
|
| #include "chrome/browser/ui/gtk/gtk_util.h"
|
| +#include "chrome/browser/ui/gtk/menu_gtk.h"
|
| #include "chrome/browser/ui/website_settings/website_settings_ui.h"
|
| #include "grit/generated_resources.h"
|
| +#include "grit/theme_resources.h"
|
| #include "ui/base/gtk/gtk_hig_constants.h"
|
| #include "ui/base/l10n/l10n_util.h"
|
| +#include "ui/base/models/menu_model.h"
|
| +#include "ui/base/resource/resource_bundle.h"
|
| #include "ui/gfx/image/image.h"
|
|
|
| namespace {
|
|
|
| -GtkWidget* CreateTextLabel(const std::string& text,
|
| - GtkThemeService* theme_service) {
|
| - GtkWidget* label = theme_service->BuildLabel(text, ui::kGdkBlack);
|
| - gtk_label_set_selectable(GTK_LABEL(label), TRUE);
|
| - gtk_label_set_line_wrap_mode(GTK_LABEL(label), PANGO_WRAP_WORD_CHAR);
|
| - return label;
|
| +ContentSetting CommandIdToContentSetting(int command_id) {
|
| + switch (command_id) {
|
| + case PermissionMenuModel::COMMAND_SET_TO_DEFAULT:
|
| + return CONTENT_SETTING_DEFAULT;
|
| + case PermissionMenuModel::COMMAND_SET_TO_ALLOW:
|
| + return CONTENT_SETTING_ALLOW;
|
| + case PermissionMenuModel::COMMAND_SET_TO_BLOCK:
|
| + return CONTENT_SETTING_BLOCK;
|
| + default:
|
| + NOTREACHED();
|
| + return CONTENT_SETTING_DEFAULT;
|
| + }
|
| }
|
|
|
| } // namespace
|
| @@ -30,121 +40,92 @@ GtkWidget* CreateTextLabel(const std::string& text,
|
| PermissionSelector::PermissionSelector(GtkThemeService* theme_service,
|
| ContentSettingsType type,
|
| ContentSetting setting,
|
| - ContentSetting default_setting)
|
| - : theme_service_(theme_service),
|
| + ContentSetting default_setting,
|
| + content_settings::SettingSource source)
|
| + : widget_(NULL),
|
| + menu_button_(NULL),
|
| + icon_(NULL),
|
| type_(type),
|
| - setting_(setting),
|
| default_setting_(default_setting),
|
| - icon_(NULL) {
|
| + setting_(setting) {
|
| DCHECK_NE(default_setting, CONTENT_SETTING_DEFAULT);
|
| -}
|
|
|
| -PermissionSelector::~PermissionSelector() {
|
| -}
|
| -
|
| -GtkWidget* PermissionSelector::CreateUI() {
|
| // Create permission info box.
|
| const int kChildSpacing = 4;
|
| - GtkWidget* hbox = gtk_hbox_new(FALSE, kChildSpacing);
|
| + widget_ = gtk_hbox_new(FALSE, kChildSpacing);
|
|
|
| // Add permission type icon.
|
| - ContentSetting effective_setting = setting_;
|
| + ContentSetting effective_setting = setting;
|
| if (effective_setting == CONTENT_SETTING_DEFAULT)
|
| - effective_setting = default_setting_;
|
| + effective_setting = default_setting;
|
| GdkPixbuf* pixbuf = WebsiteSettingsUI::GetPermissionIcon(
|
| - type_, effective_setting).ToGdkPixbuf();
|
| + type, effective_setting).ToGdkPixbuf();
|
| icon_ = gtk_image_new_from_pixbuf(pixbuf);
|
| - gtk_box_pack_start(GTK_BOX(hbox), icon_, FALSE, FALSE, 0);
|
| + gtk_box_pack_start(GTK_BOX(widget_), icon_, FALSE, FALSE, 0);
|
|
|
| // Add a label for the permission type.
|
| - GtkWidget* label = CreateTextLabel(
|
| - l10n_util::GetStringFUTF8(
|
| - IDS_WEBSITE_SETTINGS_PERMISSION_TYPE,
|
| - WebsiteSettingsUI::PermissionTypeToUIString(type_)),
|
| - theme_service_);
|
| - gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
|
| -
|
| - GtkListStore* store =
|
| - gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
|
| - GtkTreeIter iter;
|
| - // Add option for permission "Global Default" to the combobox model.
|
| - std::string setting_str = l10n_util::GetStringFUTF8(
|
| - IDS_WEBSITE_SETTINGS_DEFAULT_PERMISSION_LABEL,
|
| - WebsiteSettingsUI::PermissionValueToUIString(default_setting_));
|
| - gtk_list_store_append(store, &iter);
|
| - gtk_list_store_set(store, &iter, 0, setting_str.c_str(), 1,
|
| - CONTENT_SETTING_DEFAULT, -1);
|
| - GtkWidget* combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
|
| - // Add option for permission "Allow" to the combobox model.
|
| - setting_str = l10n_util::GetStringFUTF8(
|
| - IDS_WEBSITE_SETTINGS_PERMISSION_LABEL,
|
| - WebsiteSettingsUI::PermissionValueToUIString(CONTENT_SETTING_ALLOW));
|
| - gtk_list_store_append(store, &iter);
|
| - gtk_list_store_set(store, &iter, 0, setting_str.c_str(), 1,
|
| - CONTENT_SETTING_ALLOW, -1);
|
| - // The content settings type fullscreen does not support the concept of
|
| - // blocking.
|
| - if (type_ != CONTENT_SETTINGS_TYPE_FULLSCREEN) {
|
| - // Add option for permission "BLOCK" to the combobox model.
|
| - setting_str = l10n_util::GetStringFUTF8(
|
| - IDS_WEBSITE_SETTINGS_PERMISSION_LABEL,
|
| - WebsiteSettingsUI::PermissionValueToUIString(CONTENT_SETTING_BLOCK));
|
| - gtk_list_store_append(store, &iter);
|
| - gtk_list_store_set(store, &iter, 0, setting_str.c_str(), 1,
|
| - CONTENT_SETTING_BLOCK, -1);
|
| - }
|
| - // Remove reference to the store to prevent leaking.
|
| - g_object_unref(G_OBJECT(store));
|
| -
|
| - GtkCellRenderer* cell = gtk_cell_renderer_text_new();
|
| - gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), cell, TRUE );
|
| - gtk_cell_layout_set_attributes(
|
| - GTK_CELL_LAYOUT(combo_box), cell, "text", 0, NULL);
|
| -
|
| - // Select the combobox entry for the currently configured permission value.
|
| - int active = -1;
|
| - switch (setting_) {
|
| - case CONTENT_SETTING_DEFAULT: active = 0;
|
| - break;
|
| - case CONTENT_SETTING_ALLOW: active = 1;
|
| - break;
|
| - case CONTENT_SETTING_BLOCK: active = 2;
|
| - break;
|
| - default:
|
| - NOTREACHED() << "Bad content setting:" << setting_;
|
| - break;
|
| + GtkWidget* label = theme_service->BuildLabel(l10n_util::GetStringFUTF8(
|
| + IDS_WEBSITE_SETTINGS_PERMISSION_TYPE,
|
| + WebsiteSettingsUI::PermissionTypeToUIString(type)),
|
| + ui::kGdkBlack);
|
| + gtk_label_set_selectable(GTK_LABEL(label), TRUE);
|
| + gtk_label_set_line_wrap_mode(GTK_LABEL(label), PANGO_WRAP_WORD_CHAR);
|
| +
|
| + gtk_box_pack_start(GTK_BOX(widget_), label, FALSE, FALSE, 0);
|
| +
|
| + // Add the menu button.
|
| + menu_button_ = gtk_button_new_with_label(
|
| + UTF16ToUTF8(WebsiteSettingsUI::PermissionActionToUIString(
|
| + setting, default_setting, source)).c_str());
|
| + bool user_setting = source == content_settings::SETTING_SOURCE_USER;
|
| + gtk_widget_set_sensitive(GTK_WIDGET(menu_button_), user_setting);
|
| + if (user_setting) {
|
| + ResourceBundle& rb = ResourceBundle::GetSharedInstance();
|
| + pixbuf = rb.GetNativeImageNamed(IDR_APP_DROPARROW).ToGdkPixbuf();
|
| + GtkWidget* arrow = gtk_image_new_from_pixbuf(pixbuf);
|
| + gtk_button_set_image(GTK_BUTTON(menu_button_), arrow);
|
| + gtk_button_set_image_position(GTK_BUTTON(menu_button_),
|
| + base::i18n::IsRTL() ? GTK_POS_LEFT
|
| + : GTK_POS_RIGHT);
|
| }
|
| - gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), active);
|
| -
|
| - // Add change listener to the combobox.
|
| - g_signal_connect(combo_box, "changed",
|
| - G_CALLBACK(OnPermissionChangedThunk), this);
|
| - // Once the popup (window) for a combobox is shown, the bubble container
|
| - // (window) loses it's focus. Therefore it necessary to reset the focus to
|
| - // the bubble container after the combobox popup is closed.
|
| - g_signal_connect(combo_box, "notify::popup-shown",
|
| - G_CALLBACK(OnComboBoxShownThunk), this);
|
| - gtk_box_pack_start(GTK_BOX(hbox), combo_box, FALSE, FALSE, 0);
|
| -
|
| - return hbox;
|
| + gtk_button_set_relief(GTK_BUTTON(menu_button_), GTK_RELIEF_NONE);
|
| + gtk_box_pack_start(GTK_BOX(widget_), menu_button_, FALSE, FALSE, 0);
|
| +
|
| + menu_model_.reset(new PermissionMenuModel(this, type, default_setting,
|
| + setting));
|
| + MenuGtk::Delegate* delegate = new MenuGtk::Delegate();
|
| + menu_.reset(new MenuGtk(delegate, menu_model_.get()));
|
| + g_signal_connect(menu_button_, "button-press-event",
|
| + G_CALLBACK(OnMenuButtonPressEventThunk), this);
|
| +}
|
| +
|
| +PermissionSelector::~PermissionSelector() {
|
| }
|
|
|
| void PermissionSelector::AddObserver(PermissionSelectorObserver* observer) {
|
| observer_list_.AddObserver(observer);
|
| }
|
|
|
| -void PermissionSelector::OnPermissionChanged(GtkWidget* widget) {
|
| - // Get the selected setting.
|
| - GtkTreeIter it;
|
| - bool has_active = gtk_combo_box_get_active_iter(GTK_COMBO_BOX(widget), &it);
|
| - DCHECK(has_active);
|
| - GtkTreeModel* store =
|
| - GTK_TREE_MODEL(gtk_combo_box_get_model(GTK_COMBO_BOX(widget)));
|
| - int value = -1;
|
| - gtk_tree_model_get(store, &it, 1, &value, -1);
|
| - setting_ = ContentSetting(value);
|
| -
|
| - // Change the permission icon according the the selected setting.
|
| +ContentSetting PermissionSelector::GetSetting() const {
|
| + return setting_;
|
| +}
|
| +
|
| +ContentSettingsType PermissionSelector::GetType() const {
|
| + return type_;
|
| +}
|
| +
|
| +gboolean PermissionSelector::OnMenuButtonPressEvent(GtkWidget* button,
|
| + GdkEventButton* event) {
|
| + if (event->button != 1)
|
| + return FALSE;
|
| + menu_->PopupForWidget(button, event->button, event->time);
|
| + return TRUE;
|
| +}
|
| +
|
| +void PermissionSelector::ExecuteCommand(int command_id) {
|
| + setting_ = CommandIdToContentSetting(command_id);
|
| +
|
| + // Change the permission icon to reflect the selected setting.
|
| ContentSetting effective_setting = setting_;
|
| if (effective_setting == CONTENT_SETTING_DEFAULT)
|
| effective_setting = default_setting_;
|
| @@ -152,26 +133,18 @@ void PermissionSelector::OnPermissionChanged(GtkWidget* widget) {
|
| type_, effective_setting).ToGdkPixbuf();
|
| gtk_image_set_from_pixbuf(GTK_IMAGE(icon_), pixbuf);
|
|
|
| + // Change the text of the menu button to reflect the selected setting.
|
| + gtk_button_set_label(GTK_BUTTON(menu_button_), UTF16ToUTF8(
|
| + WebsiteSettingsUI::PermissionActionToUIString(
|
| + setting_,
|
| + default_setting_,
|
| + content_settings::SETTING_SOURCE_USER)).c_str());
|
| +
|
| FOR_EACH_OBSERVER(PermissionSelectorObserver,
|
| observer_list_,
|
| OnPermissionChanged(this));
|
| }
|
|
|
| -void PermissionSelector::OnComboBoxShown(GtkWidget* widget,
|
| - GParamSpec* property) {
|
| - // GtkComboBox grabs the keyboard and pointer when it displays its popup,
|
| - // which steals the grabs that BubbleGtk had installed. When the popup is
|
| - // hidden, we notify BubbleGtk so it can try to reacquire the grabs
|
| - // (otherwise, GTK won't activate our widgets when the user clicks in them).
|
| - // When then combobox popup is closed we notify the
|
| - // |PermissionSelectorObserver|s so that BubbleGtk can grab the keyboard and
|
| - // pointer.
|
| - gboolean popup_shown = FALSE;
|
| - g_object_get(G_OBJECT(GTK_COMBO_BOX(widget)), "popup-shown", &popup_shown,
|
| - NULL);
|
| - if (!popup_shown) {
|
| - FOR_EACH_OBSERVER(PermissionSelectorObserver,
|
| - observer_list_,
|
| - OnComboboxShown());
|
| - }
|
| +bool PermissionSelector::IsCommandIdChecked(int command_id) {
|
| + return setting_ == CommandIdToContentSetting(command_id);
|
| }
|
|
|