OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/autofill/autofill_cc_infobar.h" |
| 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" |
| 9 #include "chrome/browser/views/event_utils.h" |
| 10 #include "chrome/browser/views/infobars/infobar_button_border.h" |
| 11 #include "chrome/browser/views/infobars/infobars.h" |
| 12 #include "chrome/common/pref_names.h" |
| 13 #include "grit/chromium_strings.h" |
| 14 #include "grit/generated_resources.h" |
| 15 #include "grit/theme_resources.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 #include "views/controls/button/text_button.h" |
| 18 #include "views/controls/link.h" |
| 19 |
| 20 class SaveCCInfoConfirmInfoBar : public AlertInfoBar, |
| 21 public views::LinkController { |
| 22 public: |
| 23 explicit SaveCCInfoConfirmInfoBar(ConfirmInfoBarDelegate* delegate); |
| 24 virtual ~SaveCCInfoConfirmInfoBar(); |
| 25 |
| 26 // Overridden from views::View: |
| 27 virtual void Layout(); |
| 28 |
| 29 // Overridden from views::LinkController: |
| 30 virtual void LinkActivated(views::Link* source, int event_flags); |
| 31 |
| 32 protected: |
| 33 // Overridden from views::View: |
| 34 virtual void ViewHierarchyChanged(bool is_add, |
| 35 views::View* parent, |
| 36 views::View* child); |
| 37 |
| 38 // Overridden from views::ButtonListener: |
| 39 virtual void ButtonPressed(views::Button* sender, const views::Event& event); |
| 40 |
| 41 // Overridden from InfoBar: |
| 42 virtual int GetAvailableWidth() const; |
| 43 |
| 44 private: |
| 45 void Init(); |
| 46 views::TextButton* CreateTextButton(const std::wstring& text); |
| 47 |
| 48 ConfirmInfoBarDelegate* GetDelegate(); |
| 49 |
| 50 // The buttons are owned by InfoBar view from the moment they added to its |
| 51 // hierarchy (Init() called), but we still need pointers to them to process |
| 52 // messages from them. |
| 53 views::TextButton* save_button_; |
| 54 views::TextButton* dont_save_button_; |
| 55 views::Link* link_; |
| 56 bool initialized_; |
| 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(SaveCCInfoConfirmInfoBar); |
| 59 }; |
| 60 |
| 61 SaveCCInfoConfirmInfoBar::SaveCCInfoConfirmInfoBar( |
| 62 ConfirmInfoBarDelegate* delegate) |
| 63 : AlertInfoBar(delegate), |
| 64 initialized_(false) { |
| 65 save_button_ = CreateTextButton(delegate->GetButtonLabel( |
| 66 ConfirmInfoBarDelegate::BUTTON_OK)); |
| 67 dont_save_button_ = CreateTextButton(delegate->GetButtonLabel( |
| 68 ConfirmInfoBarDelegate::BUTTON_CANCEL)); |
| 69 // Set up the link. |
| 70 link_ = new views::Link; |
| 71 link_->SetText(delegate->GetLinkText()); |
| 72 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 73 link_->SetFont(rb.GetFont(ResourceBundle::MediumFont)); |
| 74 link_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 75 link_->SetController(this); |
| 76 link_->MakeReadableOverBackgroundColor(background()->get_color()); |
| 77 } |
| 78 |
| 79 SaveCCInfoConfirmInfoBar::~SaveCCInfoConfirmInfoBar() { |
| 80 if (!initialized_) { |
| 81 delete save_button_; |
| 82 delete dont_save_button_; |
| 83 delete link_; |
| 84 } |
| 85 } |
| 86 |
| 87 void SaveCCInfoConfirmInfoBar::Layout() { |
| 88 // Layout the close button. |
| 89 InfoBar::Layout(); |
| 90 |
| 91 int available_width = AlertInfoBar::GetAvailableWidth(); |
| 92 |
| 93 // Now append the link to the label's right edge. |
| 94 link_->SetVisible(!link_->GetText().empty()); |
| 95 gfx::Size link_ps = link_->GetPreferredSize(); |
| 96 int link_x = available_width - kButtonButtonSpacing - link_ps.width(); |
| 97 link_->SetBounds(link_x, OffsetY(this, link_ps), link_ps.width(), |
| 98 link_ps.height()); |
| 99 available_width = link_x; |
| 100 |
| 101 // Layout the cancel and OK buttons. |
| 102 gfx::Size save_ps = save_button_->GetPreferredSize(); |
| 103 gfx::Size dont_save_ps = dont_save_button_->GetPreferredSize(); |
| 104 |
| 105 // Layout the icon and label. |
| 106 AlertInfoBar::Layout(); |
| 107 |
| 108 int save_x = label()->bounds().right() + kEndOfLabelSpacing; |
| 109 save_x = std::max(0, std::min(save_x, available_width - (save_ps.width() + |
| 110 dont_save_ps.width() + kButtonButtonSpacing))); |
| 111 |
| 112 save_button_->SetVisible(true); |
| 113 dont_save_button_->SetVisible(true); |
| 114 |
| 115 save_button_->SetBounds(save_x, OffsetY(this, save_ps), save_ps.width(), |
| 116 save_ps.height()); |
| 117 int dont_save_x = save_x + save_ps.width() + kButtonButtonSpacing; |
| 118 dont_save_button_->SetBounds(dont_save_x, OffsetY(this, dont_save_ps), |
| 119 dont_save_ps.width(), dont_save_ps.height()); |
| 120 } |
| 121 |
| 122 void SaveCCInfoConfirmInfoBar::LinkActivated(views::Link* source, |
| 123 int event_flags) { |
| 124 DCHECK(source == link_); |
| 125 DCHECK(link_->IsVisible()); |
| 126 DCHECK(!link_->GetText().empty()); |
| 127 GetDelegate()->LinkClicked( |
| 128 event_utils::DispositionFromEventFlags(event_flags)); |
| 129 } |
| 130 |
| 131 void SaveCCInfoConfirmInfoBar::ViewHierarchyChanged(bool is_add, |
| 132 views::View* parent, |
| 133 views::View* child) { |
| 134 InfoBar::ViewHierarchyChanged(is_add, parent, child); |
| 135 if (is_add && child == this && !initialized_) { |
| 136 Init(); |
| 137 initialized_ = true; |
| 138 } |
| 139 } |
| 140 |
| 141 void SaveCCInfoConfirmInfoBar::ButtonPressed(views::Button* sender, |
| 142 const views::Event& event) { |
| 143 InfoBar::ButtonPressed(sender, event); |
| 144 if (sender == save_button_) { |
| 145 if (GetDelegate()->Accept()) |
| 146 RemoveInfoBar(); |
| 147 } else if (sender == dont_save_button_) { |
| 148 if (GetDelegate()->Cancel()) |
| 149 RemoveInfoBar(); |
| 150 } |
| 151 } |
| 152 |
| 153 int SaveCCInfoConfirmInfoBar::GetAvailableWidth() const { |
| 154 int buttons_area_size = save_button_->GetPreferredSize().width() + |
| 155 dont_save_button_->GetPreferredSize().width() + kButtonButtonSpacing + |
| 156 kEndOfLabelSpacing; |
| 157 return std::max(0, link_->x() - buttons_area_size); |
| 158 } |
| 159 |
| 160 void SaveCCInfoConfirmInfoBar::Init() { |
| 161 AddChildView(save_button_); |
| 162 AddChildView(dont_save_button_); |
| 163 AddChildView(link_); |
| 164 } |
| 165 |
| 166 views::TextButton* SaveCCInfoConfirmInfoBar::CreateTextButton( |
| 167 const std::wstring& text) { |
| 168 views::TextButton* text_button = new views::TextButton(this, std::wstring()); |
| 169 text_button->set_border(new InfoBarButtonBorder); |
| 170 |
| 171 // Set font colors for different states. |
| 172 text_button->SetEnabledColor(SK_ColorBLACK); |
| 173 text_button->SetHighlightColor(SK_ColorBLACK); |
| 174 text_button->SetHoverColor(SK_ColorBLACK); |
| 175 text_button->SetNormalHasBorder(true); |
| 176 text_button->SetShowHighlighted(true); |
| 177 |
| 178 // Set font then text, then size button to fit text. |
| 179 text_button->SetFont(ResourceBundle::GetSharedInstance().GetFont( |
| 180 ResourceBundle::MediumFont)); |
| 181 text_button->SetText(text); |
| 182 text_button->ClearMaxTextSize(); |
| 183 text_button->SizeToPreferredSize(); |
| 184 return text_button; |
| 185 } |
| 186 |
| 187 ConfirmInfoBarDelegate* SaveCCInfoConfirmInfoBar::GetDelegate() { |
| 188 return delegate()->AsConfirmInfoBarDelegate(); |
| 189 } |
| 190 |
| 191 InfoBar* CreateAutofillCcInfoBar(ConfirmInfoBarDelegate* delegate) { |
| 192 DCHECK(delegate); |
| 193 return new SaveCCInfoConfirmInfoBar(delegate); |
| 194 } |
| 195 |
OLD | NEW |