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