Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / infobars / translate_infobar_base.cc
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/views/infobars/translate_infobar_base.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/infobars/infobar.h"
9 #include "chrome/browser/translate/translate_infobar_delegate.h"
10 #include "chrome/browser/translate/translate_tab_helper.h"
11 #include "chrome/browser/ui/views/infobars/after_translate_infobar.h"
12 #include "chrome/browser/ui/views/infobars/before_translate_infobar.h"
13 #include "chrome/browser/ui/views/infobars/translate_message_infobar.h"
14 #include "grit/theme_resources.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/animation/slide_animation.h"
17 #include "ui/gfx/canvas.h"
18 #include "ui/views/controls/button/menu_button.h"
19 #include "ui/views/controls/label.h"
20
21
22 // TranslateInfoBarDelegate ---------------------------------------------------
23
24 // static
25 scoped_ptr<InfoBar> TranslateInfoBarDelegate::CreateInfoBar(
26     scoped_ptr<TranslateInfoBarDelegate> delegate) {
27   if (delegate->translate_step() == TranslateTabHelper::BEFORE_TRANSLATE)
28     return scoped_ptr<InfoBar>(new BeforeTranslateInfoBar(delegate.Pass()));
29   if (delegate->translate_step() == TranslateTabHelper::AFTER_TRANSLATE)
30     return scoped_ptr<InfoBar>(new AfterTranslateInfoBar(delegate.Pass()));
31   return scoped_ptr<InfoBar>(new TranslateMessageInfoBar(delegate.Pass()));
32 }
33
34
35 // TranslateInfoBarBase -------------------------------------------------------
36
37 // static
38 const int TranslateInfoBarBase::kButtonInLabelSpacing = 5;
39
40 void TranslateInfoBarBase::UpdateLanguageButtonText(
41     views::MenuButton* button,
42     const base::string16& text) {
43   DCHECK(button);
44   button->SetText(text);
45   button->ClearMaxTextSize();
46   button->SizeToPreferredSize();
47   Layout();
48   SchedulePaint();
49 }
50
51 TranslateInfoBarBase::TranslateInfoBarBase(
52     scoped_ptr<TranslateInfoBarDelegate> delegate)
53     : InfoBarView(delegate.PassAs<InfoBarDelegate>()),
54       error_background_(InfoBarDelegate::WARNING_TYPE) {
55 }
56
57 TranslateInfoBarBase::~TranslateInfoBarBase() {
58 }
59
60 void TranslateInfoBarBase::ViewHierarchyChanged(
61     const ViewHierarchyChangedDetails& details) {
62   if (details.is_add && (details.child == this) &&
63       (background_color_animation_ == NULL)) {
64     background_color_animation_.reset(new gfx::SlideAnimation(this));
65     background_color_animation_->SetTweenType(gfx::Tween::LINEAR);
66     background_color_animation_->SetSlideDuration(500);
67     TranslateInfoBarDelegate::BackgroundAnimationType animation =
68         GetDelegate()->background_animation_type();
69     if (animation == TranslateInfoBarDelegate::NORMAL_TO_ERROR) {
70       background_color_animation_->Show();
71     } else if (animation == TranslateInfoBarDelegate::ERROR_TO_NORMAL) {
72       // Hide() runs the animation in reverse.
73       background_color_animation_->Reset(1.0);
74       background_color_animation_->Hide();
75     }
76   }
77
78   // This must happen after adding all other children so InfoBarView can ensure
79   // the close button is the last child.
80   InfoBarView::ViewHierarchyChanged(details);
81 }
82
83 TranslateInfoBarDelegate* TranslateInfoBarBase::GetDelegate() {
84   return delegate()->AsTranslateInfoBarDelegate();
85 }
86
87 void TranslateInfoBarBase::OnPaintBackground(gfx::Canvas* canvas) {
88   // We need to set the separator color for |error_background_| like
89   // InfoBarView::Layout() does for the normal background.
90   const InfoBarContainer::Delegate* delegate = container_delegate();
91   if (delegate)
92     error_background_.set_separator_color(delegate->GetInfoBarSeparatorColor());
93
94   // If we're not animating, simply paint the background for the current state.
95   if (!background_color_animation_->is_animating()) {
96     GetBackground().Paint(canvas, this);
97     return;
98   }
99
100   FadeBackground(canvas, 1.0 - background_color_animation_->GetCurrentValue(),
101                  *background());
102   FadeBackground(canvas, background_color_animation_->GetCurrentValue(),
103                  error_background_);
104 }
105
106 void TranslateInfoBarBase::AnimationProgressed(
107     const gfx::Animation* animation) {
108   if (animation == background_color_animation_.get())
109     SchedulePaint();  // That'll trigger a PaintBackgroud.
110   else
111     InfoBarView::AnimationProgressed(animation);
112 }
113
114 const views::Background& TranslateInfoBarBase::GetBackground() {
115   return GetDelegate()->is_error() ? error_background_ : *background();
116 }
117
118 void TranslateInfoBarBase::FadeBackground(gfx::Canvas* canvas,
119                                           double animation_value,
120                                           const views::Background& background) {
121   // Draw the background into an offscreen buffer with alpha value per animation
122   // value, then blend it back into the current canvas.
123   canvas->SaveLayerAlpha(static_cast<int>(animation_value * 255));
124   background.Paint(canvas, this);
125   canvas->Restore();
126 }