diff options
author | Ben Murdoch <benm@google.com> | 2010-11-25 19:40:10 +0000 |
---|---|---|
committer | Ben Murdoch <benm@google.com> | 2010-12-03 13:52:53 +0000 |
commit | 4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7 (patch) | |
tree | 938665d93a11fe7a6d0124e3c1e020d1f9d3f947 /chrome/browser/ui/views/infobars/after_translate_infobar.cc | |
parent | 7c627d87728a355737862918d144f98f69406954 (diff) | |
download | external_chromium-4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7.zip external_chromium-4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7.tar.gz external_chromium-4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7.tar.bz2 |
Merge Chromium at r66597: Initial merge by git.
Change-Id: I9639f8a997f90ec219573aa22a49f5dbde78cc7b
Diffstat (limited to 'chrome/browser/ui/views/infobars/after_translate_infobar.cc')
-rw-r--r-- | chrome/browser/ui/views/infobars/after_translate_infobar.cc | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/chrome/browser/ui/views/infobars/after_translate_infobar.cc b/chrome/browser/ui/views/infobars/after_translate_infobar.cc new file mode 100644 index 0000000..63b6c8a --- /dev/null +++ b/chrome/browser/ui/views/infobars/after_translate_infobar.cc @@ -0,0 +1,169 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/views/infobars/after_translate_infobar.h" + +#include "app/l10n_util.h" +#include "base/utf_string_conversions.h" +#include "chrome/browser/translate/options_menu_model.h" +#include "chrome/browser/translate/translate_infobar_delegate.h" +#include "chrome/browser/views/infobars/infobar_button_border.h" +#include "chrome/browser/views/infobars/infobar_text_button.h" +#include "grit/app_resources.h" +#include "grit/generated_resources.h" +#include "views/controls/button/menu_button.h" +#include "views/controls/button/text_button.h" +#include "views/controls/image_view.h" +#include "views/controls/label.h" +#include "views/controls/menu/menu_2.h" + +AfterTranslateInfoBar::AfterTranslateInfoBar( + TranslateInfoBarDelegate* delegate) + : TranslateInfoBarBase(delegate), + original_language_menu_model_(delegate, LanguagesMenuModel::ORIGINAL), + target_language_menu_model_(delegate, LanguagesMenuModel::TARGET), + options_menu_model_(delegate), + swapped_language_buttons_(false) { + std::vector<string16> strings; + TranslateInfoBarDelegate::GetAfterTranslateStrings( + &strings, &swapped_language_buttons_); + DCHECK(strings.size() == 3U); + + label_1_ = CreateLabel(strings[0]); + AddChildView(label_1_); + + label_2_ = CreateLabel(strings[1]); + AddChildView(label_2_); + + label_3_ = CreateLabel(strings[2]); + AddChildView(label_3_); + + original_language_menu_button_ = CreateMenuButton(string16(), true, this); + AddChildView(original_language_menu_button_); + + target_language_menu_button_ = CreateMenuButton(string16(), true, this); + AddChildView(target_language_menu_button_); + + options_menu_button_ = + CreateMenuButton(l10n_util::GetStringUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS), + false, this); + AddChildView(options_menu_button_); + + revert_button_ = InfoBarTextButton::Create(this, + l10n_util::GetStringUTF16(IDS_TRANSLATE_INFOBAR_REVERT)); + AddChildView(revert_button_); + + UpdateLanguageButtonText(LanguagesMenuModel::ORIGINAL); + UpdateLanguageButtonText(LanguagesMenuModel::TARGET); +} + +AfterTranslateInfoBar::~AfterTranslateInfoBar() { +} + +// Overridden from views::View: +void AfterTranslateInfoBar::Layout() { + // Layout the icon and close button. + TranslateInfoBarBase::Layout(); + + // Layout the options menu button on right of bar. + int available_width = InfoBar::GetAvailableWidth(); + gfx::Size pref_size = options_menu_button_->GetPreferredSize(); + options_menu_button_->SetBounds(available_width - pref_size.width(), + OffsetY(this, pref_size), pref_size.width(), pref_size.height()); + + views::MenuButton* left_button = swapped_language_buttons_ ? + target_language_menu_button_ : original_language_menu_button_; + views::MenuButton* right_button = swapped_language_buttons_ ? + original_language_menu_button_ : target_language_menu_button_; + + pref_size = label_1_->GetPreferredSize(); + label_1_->SetBounds(icon_->bounds().right() + InfoBar::kIconLabelSpacing, + InfoBar::OffsetY(this, pref_size), pref_size.width(), pref_size.height()); + + pref_size = left_button->GetPreferredSize(); + left_button->SetBounds(label_1_->bounds().right() + + InfoBar::kButtonInLabelSpacing, OffsetY(this, pref_size), + pref_size.width(), pref_size.height()); + + pref_size = label_2_->GetPreferredSize(); + label_2_->SetBounds(left_button->bounds().right() + + InfoBar::kButtonInLabelSpacing, InfoBar::OffsetY(this, pref_size), + pref_size.width(), pref_size.height()); + + pref_size = right_button->GetPreferredSize(); + right_button->SetBounds(label_2_->bounds().right() + + InfoBar::kButtonInLabelSpacing, OffsetY(this, pref_size), + pref_size.width(), pref_size.height()); + + pref_size = label_3_->GetPreferredSize(); + label_3_->SetBounds(right_button->bounds().right() + + InfoBar::kButtonInLabelSpacing, InfoBar::OffsetY(this, pref_size), + pref_size.width(), pref_size.height()); + + pref_size = revert_button_->GetPreferredSize(); + revert_button_->SetBounds(label_3_->bounds().right() + + InfoBar::kButtonInLabelSpacing, InfoBar::OffsetY(this, pref_size), + pref_size.width(), pref_size.height()); +} + +void AfterTranslateInfoBar::OriginalLanguageChanged() { + UpdateLanguageButtonText(LanguagesMenuModel::ORIGINAL); +} + +void AfterTranslateInfoBar::TargetLanguageChanged() { + UpdateLanguageButtonText(LanguagesMenuModel::TARGET); +} + +void AfterTranslateInfoBar::ButtonPressed(views::Button* sender, + const views::Event& event) { + if (sender == revert_button_) { + GetDelegate()->RevertTranslation(); + return; + } + TranslateInfoBarBase::ButtonPressed(sender, event); +} + +void AfterTranslateInfoBar::RunMenu(views::View* source, + const gfx::Point& pt) { + if (source == original_language_menu_button_) { + if (!original_language_menu_.get()) { + original_language_menu_.reset( + new views::Menu2(&original_language_menu_model_)); + } + original_language_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); + } else if (source == target_language_menu_button_) { + if (!target_language_menu_.get()) { + target_language_menu_.reset( + new views::Menu2(&target_language_menu_model_)); + } + target_language_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); + } else if (source == options_menu_button_) { + if (!options_menu_.get()) + options_menu_.reset(new views::Menu2(&options_menu_model_)); + options_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); + } else { + NOTREACHED(); + } +} + +void AfterTranslateInfoBar::UpdateLanguageButtonText( + LanguagesMenuModel::LanguageType language_type) { + int language_index; + views::MenuButton* language_button; + if (language_type == LanguagesMenuModel::ORIGINAL) { + language_index = GetDelegate()->original_language_index(); + language_button = original_language_menu_button_; + } else { + language_index = GetDelegate()->target_language_index(); + language_button = target_language_menu_button_; + } + string16 language = + GetDelegate()->GetLanguageDisplayableNameAt(language_index); + language_button->SetText(UTF16ToWideHack(language)); + // The following line is necessary for the preferred size to be recomputed. + language_button->ClearMaxTextSize(); + // The button may have to grow to show the new text. + Layout(); + SchedulePaint(); +} |