OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2008 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/views/infobars/infobars.h" |
| 6 |
| 7 #include "chrome/app/theme/theme_resources.h" |
| 8 #include "chrome/browser/views/infobars/infobar_container.h" |
| 9 #include "chrome/common/l10n_util.h" |
| 10 #include "chrome/common/resource_bundle.h" |
| 11 #include "chrome/common/slide_animation.h" |
| 12 #include "chrome/views/background.h" |
| 13 #include "chrome/views/button.h" |
| 14 #include "chrome/views/image_view.h" |
| 15 #include "chrome/views/label.h" |
| 16 |
| 17 #include "generated_resources.h" |
| 18 |
| 19 const double kInfoBarHeight = 37.0; |
| 20 |
| 21 static const int kVerticalPadding = 3; |
| 22 static const int kHorizontalPadding = 3; |
| 23 static const int kIconLabelSpacing = 5; |
| 24 static const int kButtonSpacing = 5; |
| 25 |
| 26 static const SkColor kBackgroundColorTop = SkColorSetRGB(255, 242, 183); |
| 27 static const SkColor kBackgroundColorBottom = SkColorSetRGB(250, 230, 145); |
| 28 |
| 29 static const int kSeparatorLineHeight = 1; |
| 30 static const SkColor kSeparatorColor = SkColorSetRGB(165, 165, 165); |
| 31 |
| 32 namespace { |
| 33 int OffsetY(views::View* parent, const gfx::Size prefsize) { |
| 34 return std::max((parent->height() - prefsize.height()) / 2, 0); |
| 35 } |
| 36 } |
| 37 |
| 38 // InfoBarBackground ----------------------------------------------------------- |
| 39 |
| 40 class InfoBarBackground : public views::Background { |
| 41 public: |
| 42 InfoBarBackground() { |
| 43 gradient_background_.reset( |
| 44 views::Background::CreateVerticalGradientBackground( |
| 45 kBackgroundColorTop, kBackgroundColorBottom)); |
| 46 } |
| 47 |
| 48 // Overridden from views::View: |
| 49 virtual void Paint(ChromeCanvas* canvas, views::View* view) const { |
| 50 // First paint the gradient background. |
| 51 gradient_background_->Paint(canvas, view); |
| 52 |
| 53 // Now paint the separator line. |
| 54 canvas->FillRectInt(kSeparatorColor, 0, |
| 55 view->height() - kSeparatorLineHeight, view->width(), |
| 56 kSeparatorLineHeight); |
| 57 } |
| 58 |
| 59 private: |
| 60 scoped_ptr<views::Background> gradient_background_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(InfoBarBackground); |
| 63 }; |
| 64 |
| 65 // InfoBar, public: ------------------------------------------------------------ |
| 66 |
| 67 InfoBar::InfoBar(InfoBarDelegate* delegate) |
| 68 : delegate_(delegate), |
| 69 close_button_(new views::Button) { |
| 70 set_background(new InfoBarBackground); |
| 71 |
| 72 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 73 close_button_->SetImage(views::Button::BS_NORMAL, |
| 74 rb.GetBitmapNamed(IDR_CLOSE_BAR)); |
| 75 close_button_->SetImage(views::Button::BS_HOT, |
| 76 rb.GetBitmapNamed(IDR_CLOSE_BAR_H)); |
| 77 close_button_->SetImage(views::Button::BS_PUSHED, |
| 78 rb.GetBitmapNamed(IDR_CLOSE_BAR_P)); |
| 79 close_button_->SetListener(this, 0); |
| 80 close_button_->SetAccessibleName(l10n_util::GetString(IDS_ACCNAME_CLOSE)); |
| 81 AddChildView(close_button_); |
| 82 |
| 83 animation_.reset(new SlideAnimation(this)); |
| 84 animation_->SetTweenType(SlideAnimation::NONE); |
| 85 } |
| 86 |
| 87 InfoBar::~InfoBar() { |
| 88 } |
| 89 |
| 90 void InfoBar::AnimateOpen() { |
| 91 animation_->Show(); |
| 92 } |
| 93 |
| 94 void InfoBar::Open() { |
| 95 animation_->Reset(1.0); |
| 96 animation_->Show(); |
| 97 } |
| 98 |
| 99 void InfoBar::AnimateClose() { |
| 100 animation_->Hide(); |
| 101 } |
| 102 |
| 103 void InfoBar::Close() { |
| 104 GetParent()->RemoveChildView(this); |
| 105 if (delegate()) |
| 106 delegate()->InfoBarClosed(); |
| 107 delete this; |
| 108 } |
| 109 |
| 110 // InfoBar, views::View overrides: --------------------------------------------- |
| 111 |
| 112 gfx::Size InfoBar::GetPreferredSize() { |
| 113 int height = static_cast<int>(kInfoBarHeight * animation_->GetCurrentValue()); |
| 114 return gfx::Size(0, height); |
| 115 } |
| 116 |
| 117 void InfoBar::Layout() { |
| 118 gfx::Size button_ps = close_button_->GetPreferredSize(); |
| 119 close_button_->SetBounds(width() - kHorizontalPadding - button_ps.width(), |
| 120 OffsetY(this, button_ps), button_ps.width(), |
| 121 button_ps.height()); |
| 122 |
| 123 } |
| 124 |
| 125 // InfoBar, protected: --------------------------------------------------------- |
| 126 |
| 127 int InfoBar::GetAvailableWidth() const { |
| 128 return close_button_->x() - kIconLabelSpacing; |
| 129 } |
| 130 |
| 131 // InfoBar, views::BaseButton::ButtonListener implementation: ------------------ |
| 132 |
| 133 void InfoBar::ButtonPressed(views::BaseButton* sender) { |
| 134 if (sender == close_button_) |
| 135 container_->RemoveDelegate(delegate()); |
| 136 } |
| 137 |
| 138 // InfoBar, AnimationDelegate implementation: ---------------------------------- |
| 139 |
| 140 void InfoBar::AnimationProgressed(const Animation* animation) { |
| 141 container_->InfoBarAnimated(true); |
| 142 } |
| 143 |
| 144 void InfoBar::AnimationEnded(const Animation* animation) { |
| 145 container_->InfoBarAnimated(false); |
| 146 |
| 147 if (!animation_->IsShowing()) |
| 148 Close(); |
| 149 } |
| 150 |
| 151 // AlertInfoBar, public: ------------------------------------------------------- |
| 152 |
| 153 AlertInfoBar::AlertInfoBar(AlertInfoBarDelegate* delegate) |
| 154 : InfoBar(delegate) { |
| 155 label_ = new views::Label( |
| 156 delegate->GetMessageText(), |
| 157 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::MediumFont)); |
| 158 label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 159 AddChildView(label_); |
| 160 |
| 161 icon_ = new views::ImageView; |
| 162 if (delegate->GetIcon()) |
| 163 icon_->SetImage(delegate->GetIcon()); |
| 164 AddChildView(icon_); |
| 165 } |
| 166 |
| 167 AlertInfoBar::~AlertInfoBar() { |
| 168 |
| 169 } |
| 170 |
| 171 // AlertInfoBar, views::View overrides: ---------------------------------------- |
| 172 |
| 173 void AlertInfoBar::Layout() { |
| 174 // Layout the close button. |
| 175 InfoBar::Layout(); |
| 176 |
| 177 // Layout the icon and text. |
| 178 gfx::Size icon_ps = icon_->GetPreferredSize(); |
| 179 icon_->SetBounds(kHorizontalPadding, OffsetY(this, icon_ps), icon_ps.width(), |
| 180 icon_ps.height()); |
| 181 |
| 182 gfx::Size text_ps = label_->GetPreferredSize(); |
| 183 int text_width = |
| 184 GetAvailableWidth() - icon_->bounds().right() - kIconLabelSpacing; |
| 185 label_->SetBounds(icon_->bounds().right() + kIconLabelSpacing, |
| 186 OffsetY(this, text_ps), text_width, text_ps.height()); |
| 187 } |
| 188 |
| 189 // AlertInfoBar, private: ------------------------------------------------------ |
| 190 |
| 191 AlertInfoBarDelegate* AlertInfoBar::GetDelegate() { |
| 192 return delegate()->AsAlertInfoBarDelegate(); |
| 193 } |
| 194 |
| 195 // ConfirmInfoBar, public: ----------------------------------------------------- |
| 196 |
| 197 ConfirmInfoBar::ConfirmInfoBar(ConfirmInfoBarDelegate* delegate) |
| 198 : ok_button_(NULL), |
| 199 cancel_button_(NULL), |
| 200 initialized_(false), |
| 201 AlertInfoBar(delegate) { |
| 202 } |
| 203 |
| 204 ConfirmInfoBar::~ConfirmInfoBar() { |
| 205 } |
| 206 |
| 207 // ConfirmInfoBar, views::View overrides: -------------------------------------- |
| 208 |
| 209 void ConfirmInfoBar::Layout() { |
| 210 InfoBar::Layout(); |
| 211 int available_width = InfoBar::GetAvailableWidth(); |
| 212 int ok_button_width = 0; |
| 213 int cancel_button_width = 0; |
| 214 gfx::Size ok_ps = ok_button_->GetPreferredSize(); |
| 215 gfx::Size cancel_ps = cancel_button_->GetPreferredSize(); |
| 216 |
| 217 if (GetDelegate()->GetButtons() & ConfirmInfoBarDelegate::BUTTON_OK) |
| 218 ok_button_width = ok_ps.width(); |
| 219 if (GetDelegate()->GetButtons() & ConfirmInfoBarDelegate::BUTTON_CANCEL) |
| 220 cancel_button_width = cancel_ps.width(); |
| 221 |
| 222 cancel_button_->SetBounds(available_width - cancel_button_width, |
| 223 OffsetY(this, cancel_ps), cancel_ps.width(), |
| 224 cancel_ps.height()); |
| 225 int spacing = cancel_button_width > 0 ? kButtonSpacing : 0; |
| 226 ok_button_->SetBounds(cancel_button_->x() - spacing - ok_button_width, |
| 227 OffsetY(this, ok_ps), ok_ps.width(), ok_ps.height()); |
| 228 |
| 229 AlertInfoBar::Layout(); |
| 230 } |
| 231 |
| 232 void ConfirmInfoBar::ViewHierarchyChanged(bool is_add, |
| 233 views::View* parent, |
| 234 views::View* child) { |
| 235 if (is_add && child == this && !initialized_) { |
| 236 Init(); |
| 237 initialized_ = true; |
| 238 } |
| 239 } |
| 240 |
| 241 // ConfirmInfoBar, views::NativeButton::Listener implementation: --------------- |
| 242 |
| 243 void ConfirmInfoBar::ButtonPressed(views::NativeButton* sender) { |
| 244 if (sender == ok_button_) { |
| 245 GetDelegate()->Accept(); |
| 246 } else if (sender == cancel_button_) { |
| 247 GetDelegate()->Cancel(); |
| 248 } else { |
| 249 NOTREACHED(); |
| 250 } |
| 251 } |
| 252 |
| 253 // ConfirmInfoBar, InfoBar overrides: ------------------------------------------ |
| 254 |
| 255 int ConfirmInfoBar::GetAvailableWidth() const { |
| 256 if (ok_button_) |
| 257 return ok_button_->x() - kButtonSpacing; |
| 258 if (cancel_button_) |
| 259 return cancel_button_->x() - kButtonSpacing; |
| 260 return InfoBar::GetAvailableWidth(); |
| 261 } |
| 262 |
| 263 // ConfirmInfoBar, private: ---------------------------------------------------- |
| 264 |
| 265 ConfirmInfoBarDelegate* ConfirmInfoBar::GetDelegate() { |
| 266 return delegate()->AsConfirmInfoBarDelegate(); |
| 267 } |
| 268 |
| 269 void ConfirmInfoBar::Init() { |
| 270 ok_button_ = new views::NativeButton( |
| 271 GetDelegate()->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK)); |
| 272 ok_button_->SetListener(this); |
| 273 AddChildView(ok_button_); |
| 274 |
| 275 cancel_button_ = new views::NativeButton( |
| 276 GetDelegate()->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL)); |
| 277 cancel_button_->SetListener(this); |
| 278 AddChildView(cancel_button_); |
| 279 } |
| 280 |
| 281 // AlertInfoBarDelegate, InfoBarDelegate overrides: ---------------------------- |
| 282 |
| 283 InfoBar* AlertInfoBarDelegate::CreateInfoBar() { |
| 284 return new AlertInfoBar(this); |
| 285 } |
| 286 |
| 287 // ConfirmInfoBarDelegate, InfoBarDelegate overrides: -------------------------- |
| 288 |
| 289 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar() { |
| 290 return new ConfirmInfoBar(this); |
| 291 } |
OLD | NEW |