OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/views/infobars/translate_infobars.h" | 5 #include "chrome/browser/views/infobars/translate_infobars.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
11 #include "app/resource_bundle.h" | 11 #include "app/resource_bundle.h" |
12 #include "app/slide_animation.h" | 12 #include "app/slide_animation.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "chrome/app/chrome_dll_resource.h" | 14 #include "chrome/app/chrome_dll_resource.h" |
15 #include "chrome/common/notification_service.h" | 15 #include "chrome/common/notification_service.h" |
16 #include "chrome/browser/tab_contents/tab_contents.h" | 16 #include "chrome/browser/tab_contents/tab_contents.h" |
17 #include "chrome/browser/translate/languages_menu_model.h" | 17 #include "chrome/browser/translate/languages_menu_model.h" |
18 #include "chrome/browser/translate/options_menu_model.h" | 18 #include "chrome/browser/translate/options_menu_model.h" |
19 #include "chrome/browser/translate/page_translated_details.h" | 19 #include "chrome/browser/translate/page_translated_details.h" |
| 20 #include "chrome/browser/views/infobars/infobar_button_border.h" |
| 21 #include "chrome/browser/views/infobars/infobar_text_button.h" |
20 #include "gfx/canvas.h" | 22 #include "gfx/canvas.h" |
21 #include "grit/app_resources.h" | 23 #include "grit/app_resources.h" |
22 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
23 #include "grit/locale_settings.h" | 25 #include "grit/locale_settings.h" |
24 #include "grit/theme_resources.h" | 26 #include "grit/theme_resources.h" |
25 #include "views/controls/button/image_button.h" | 27 #include "views/controls/button/image_button.h" |
26 #include "views/controls/button/menu_button.h" | 28 #include "views/controls/button/menu_button.h" |
27 #include "views/controls/button/text_button.h" | 29 #include "views/controls/button/text_button.h" |
28 #include "views/controls/image_view.h" | 30 #include "views/controls/image_view.h" |
29 #include "views/controls/label.h" | 31 #include "views/controls/label.h" |
30 | 32 |
| 33 #include "chrome/browser/views/infobars/before_translate_infobar.h" |
| 34 |
31 // IDs for various menus. | 35 // IDs for various menus. |
32 static const int kMenuIDOptions = 1000; | 36 static const int kMenuIDOptions = 1000; |
33 static const int kMenuIDOriginalLanguage = 1001; | 37 static const int kMenuIDOriginalLanguage = 1001; |
34 static const int kMenuIDTargetLanguage = 1002; | 38 static const int kMenuIDTargetLanguage = 1002; |
35 | 39 |
36 //////////////////////////////////////////////////////////////////////////////// | |
37 // | |
38 // TranslateButtonBorder | |
39 // | |
40 // A TextButtonBorder subclass that adds to also paint button frame in normal | |
41 // state, and changes the images used. | |
42 // | |
43 //////////////////////////////////////////////////////////////////////////////// | |
44 | |
45 class TranslateButtonBorder : public views::TextButtonBorder { | |
46 public: | |
47 TranslateButtonBorder(); | |
48 virtual ~TranslateButtonBorder(); | |
49 | |
50 // Overriden from Border | |
51 virtual void Paint(const views::View& view, gfx::Canvas* canvas) const; | |
52 | |
53 private: | |
54 MBBImageSet normal_set_; | |
55 | |
56 private: | |
57 DISALLOW_COPY_AND_ASSIGN(TranslateButtonBorder); | |
58 }; | |
59 | |
60 // TranslateButtonBorder, public: ---------------------------------------------- | |
61 | |
62 TranslateButtonBorder::TranslateButtonBorder() { | |
63 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
64 | |
65 normal_set_.top_left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_LEFT_N); | |
66 normal_set_.top = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_N); | |
67 normal_set_.top_right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_RIGHT_N); | |
68 normal_set_.left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_LEFT_N); | |
69 normal_set_.center = rb.GetBitmapNamed(IDR_INFOBARBUTTON_CENTER_N); | |
70 normal_set_.right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_RIGHT_N); | |
71 normal_set_.bottom_left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_LEFT_N); | |
72 normal_set_.bottom = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_N); | |
73 normal_set_.bottom_right = rb.GetBitmapNamed( | |
74 IDR_INFOBARBUTTON_BOTTOM_RIGHT_N); | |
75 | |
76 hot_set_.top_left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_LEFT_H); | |
77 hot_set_.top = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_H); | |
78 hot_set_.top_right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_RIGHT_H); | |
79 hot_set_.left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_LEFT_H); | |
80 hot_set_.center = rb.GetBitmapNamed(IDR_INFOBARBUTTON_CENTER_H); | |
81 hot_set_.right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_RIGHT_H); | |
82 hot_set_.bottom_left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_LEFT_H); | |
83 hot_set_.bottom = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_H); | |
84 hot_set_.bottom_right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_RIGHT_H); | |
85 | |
86 pushed_set_.top_left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_LEFT_P); | |
87 pushed_set_.top = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_P); | |
88 pushed_set_.top_right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_RIGHT_P); | |
89 pushed_set_.left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_LEFT_P); | |
90 pushed_set_.center = rb.GetBitmapNamed(IDR_INFOBARBUTTON_CENTER_P); | |
91 pushed_set_.right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_RIGHT_P); | |
92 pushed_set_.bottom_left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_LEFT_P); | |
93 pushed_set_.bottom = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_P); | |
94 pushed_set_.bottom_right = rb.GetBitmapNamed( | |
95 IDR_INFOBARBUTTON_BOTTOM_RIGHT_P); | |
96 } | |
97 | |
98 TranslateButtonBorder::~TranslateButtonBorder() { | |
99 } | |
100 | |
101 // TranslateButtonBorder, Border overrides: ------------------------------------ | |
102 | |
103 void TranslateButtonBorder::Paint(const views::View& view, gfx::Canvas* canvas) | |
104 const { | |
105 const views::TextButton* mb = static_cast<const views::TextButton*>(&view); | |
106 int state = mb->state(); | |
107 | |
108 // TextButton takes care of deciding when to call Paint. | |
109 const MBBImageSet* set = &normal_set_; | |
110 if (state == views::TextButton::BS_HOT) | |
111 set = &hot_set_; | |
112 else if (state == views::TextButton::BS_PUSHED) | |
113 set = &pushed_set_; | |
114 | |
115 gfx::Rect bounds = view.bounds(); | |
116 | |
117 // Draw top left image. | |
118 canvas->DrawBitmapInt(*set->top_left, 0, 0); | |
119 | |
120 // Stretch top image. | |
121 canvas->DrawBitmapInt( | |
122 *set->top, | |
123 0, 0, set->top->width(), set->top->height(), | |
124 set->top_left->width(), | |
125 0, | |
126 bounds.width() - set->top_right->width() - set->top_left->width(), | |
127 set->top->height(), false); | |
128 | |
129 // Draw top right image. | |
130 canvas->DrawBitmapInt(*set->top_right, | |
131 bounds.width() - set->top_right->width(), 0); | |
132 | |
133 // Stretch left image. | |
134 canvas->DrawBitmapInt( | |
135 *set->left, | |
136 0, 0, set->left->width(), set->left->height(), | |
137 0, | |
138 set->top_left->height(), | |
139 set->top_left->width(), | |
140 bounds.height() - set->top->height() - set->bottom_left->height(), false); | |
141 | |
142 // Stretch center image. | |
143 canvas->DrawBitmapInt( | |
144 *set->center, | |
145 0, 0, set->center->width(), set->center->height(), | |
146 set->left->width(), | |
147 set->top->height(), | |
148 bounds.width() - set->right->width() - set->left->width(), | |
149 bounds.height() - set->bottom->height() - set->top->height(), false); | |
150 | |
151 // Stretch right image. | |
152 canvas->DrawBitmapInt( | |
153 *set->right, | |
154 0, 0, set->right->width(), set->right->height(), | |
155 bounds.width() - set->right->width(), | |
156 set->top_right->height(), | |
157 set->right->width(), | |
158 bounds.height() - set->bottom_right->height() - | |
159 set->top_right->height(), false); | |
160 | |
161 // Draw bottom left image. | |
162 canvas->DrawBitmapInt(*set->bottom_left, | |
163 0, | |
164 bounds.height() - set->bottom_left->height()); | |
165 | |
166 // Stretch bottom image. | |
167 canvas->DrawBitmapInt( | |
168 *set->bottom, | |
169 0, 0, set->bottom->width(), set->bottom->height(), | |
170 set->bottom_left->width(), | |
171 bounds.height() - set->bottom->height(), | |
172 bounds.width() - set->bottom_right->width() - | |
173 set->bottom_left->width(), | |
174 set->bottom->height(), false); | |
175 | |
176 // Draw bottom right image. | |
177 canvas->DrawBitmapInt(*set->bottom_right, | |
178 bounds.width() - set->bottom_right->width(), | |
179 bounds.height() - set->bottom_right->height()); | |
180 } | |
181 | |
182 //////////////////////////////////////////////////////////////////////////////// | |
183 // | |
184 // TranslateTextButton | |
185 // | |
186 // A TextButton subclass that overrides OnMousePressed to default to | |
187 // CustomButton so as to create pressed state effect. | |
188 // | |
189 //////////////////////////////////////////////////////////////////////////////// | |
190 | |
191 class TranslateTextButton : public views::TextButton { | |
192 public: | |
193 TranslateTextButton(views::ButtonListener* listener, int label_id); | |
194 virtual ~TranslateTextButton(); | |
195 | |
196 protected: | |
197 // Overriden from TextButton: | |
198 virtual bool OnMousePressed(const views::MouseEvent& e); | |
199 | |
200 private: | |
201 DISALLOW_COPY_AND_ASSIGN(TranslateTextButton); | |
202 }; | |
203 | |
204 // TranslateButtonBorder, public: ---------------------------------------------- | |
205 | |
206 TranslateTextButton::TranslateTextButton(views::ButtonListener* listener, | |
207 int label_id) | |
208 // Don't use text to construct TextButton because we need to set font | |
209 // before setting text so that the button will resize to fit entire text. | |
210 : TextButton(listener, std::wstring()) { | |
211 set_border(new TranslateButtonBorder); | |
212 SetNormalHasBorder(true); // Normal button state has border. | |
213 SetAnimationDuration(0); // Disable animation during state change. | |
214 // Set font colors for different states. | |
215 SetEnabledColor(SK_ColorBLACK); | |
216 SetHighlightColor(SK_ColorBLACK); | |
217 SetHoverColor(SK_ColorBLACK); | |
218 // Set font then text, then size button to fit text. | |
219 SetFont(ResourceBundle::GetSharedInstance().GetFont( | |
220 ResourceBundle::MediumFont)); | |
221 SetText(l10n_util::GetString(label_id)); | |
222 ClearMaxTextSize(); | |
223 SizeToPreferredSize(); | |
224 } | |
225 | |
226 TranslateTextButton::~TranslateTextButton() { | |
227 } | |
228 | |
229 // TranslateTextButton, protected: --------------------------------------------- | |
230 | |
231 bool TranslateTextButton::OnMousePressed(const views::MouseEvent& e) { | |
232 return views::CustomButton::OnMousePressed(e); | |
233 } | |
234 | |
235 // TranslateInfoBar, public: --------------------------------------------------- | 40 // TranslateInfoBar, public: --------------------------------------------------- |
236 | 41 |
237 TranslateInfoBar::TranslateInfoBar(TranslateInfoBarDelegate* delegate) | 42 TranslateInfoBar::TranslateInfoBar(TranslateInfoBarDelegate* delegate) |
238 : InfoBar(delegate), | 43 : InfoBar(delegate), |
239 state_(TranslateInfoBarDelegate::kTranslateNone), | 44 state_(TranslateInfoBarDelegate::kTranslateNone), |
240 translation_pending_(false), | 45 translation_pending_(false), |
241 label_1_(NULL), | 46 label_1_(NULL), |
242 label_2_(NULL), | 47 label_2_(NULL), |
243 label_3_(NULL), | 48 label_3_(NULL), |
244 translating_label_(NULL), | 49 translating_label_(NULL), |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 if (!target_language_menu_button_) { | 119 if (!target_language_menu_button_) { |
315 CreateLabels(); | 120 CreateLabels(); |
316 string16 language_name = | 121 string16 language_name = |
317 GetDelegate()->GetDisplayNameForLocale( | 122 GetDelegate()->GetDisplayNameForLocale( |
318 GetDelegate()->target_lang_code()); | 123 GetDelegate()->target_lang_code()); |
319 target_language_menu_button_ = CreateMenuButton(kMenuIDTargetLanguage, | 124 target_language_menu_button_ = CreateMenuButton(kMenuIDTargetLanguage, |
320 UTF16ToWideHack(language_name), true); | 125 UTF16ToWideHack(language_name), true); |
321 AddChildView(target_language_menu_button_); | 126 AddChildView(target_language_menu_button_); |
322 } | 127 } |
323 if (!revert_button_) { | 128 if (!revert_button_) { |
324 revert_button_ = new TranslateTextButton(this, | 129 revert_button_ = InfoBarTextButton::CreateWithMessageID(this, |
325 IDS_TRANSLATE_INFOBAR_REVERT); | 130 IDS_TRANSLATE_INFOBAR_REVERT); |
326 AddChildView(revert_button_); | 131 AddChildView(revert_button_); |
327 } | 132 } |
328 break; | 133 break; |
329 | 134 |
330 case TranslateInfoBarDelegate::kBeforeTranslate: | 135 case TranslateInfoBarDelegate::kBeforeTranslate: |
331 if (!label_1_) | 136 if (!label_1_) |
332 CreateLabels(); | 137 CreateLabels(); |
333 if (!accept_button_) { | 138 if (!accept_button_) { |
334 accept_button_ = new TranslateTextButton(this, | 139 accept_button_ = InfoBarTextButton::CreateWithMessageID(this, |
335 IDS_TRANSLATE_INFOBAR_ACCEPT); | 140 IDS_TRANSLATE_INFOBAR_ACCEPT); |
336 AddChildView(accept_button_); | 141 AddChildView(accept_button_); |
337 } | 142 } |
338 if (!deny_button_) { | 143 if (!deny_button_) { |
339 deny_button_ = new TranslateTextButton(this, | 144 deny_button_ = InfoBarTextButton::CreateWithMessageID(this, |
340 IDS_TRANSLATE_INFOBAR_DENY); | 145 IDS_TRANSLATE_INFOBAR_DENY); |
341 AddChildView(deny_button_); | 146 AddChildView(deny_button_); |
342 } | 147 } |
343 break; | 148 break; |
344 | 149 |
345 case TranslateInfoBarDelegate::kTranslateError: { | 150 case TranslateInfoBarDelegate::kTranslateError: { |
346 string16 error_message_utf16 = GetDelegate()->GetErrorMessage(error_type); | 151 string16 error_message_utf16 = GetDelegate()->GetErrorMessage(error_type); |
347 std::wstring error_message = UTF16ToWideHack(error_message_utf16); | 152 std::wstring error_message = UTF16ToWideHack(error_message_utf16); |
348 if (error_label_) { | 153 if (error_label_) { |
349 error_label_->SetText(error_message); | 154 error_label_->SetText(error_message); |
350 } else { | 155 } else { |
351 error_label_ = CreateLabel(error_message); | 156 error_label_ = CreateLabel(error_message); |
352 AddChildView(error_label_); | 157 AddChildView(error_label_); |
353 } | 158 } |
354 if (!retry_button_) { | 159 if (!retry_button_) { |
355 retry_button_ = new TranslateTextButton(this, | 160 retry_button_ = InfoBarTextButton::CreateWithMessageID(this, |
356 IDS_TRANSLATE_INFOBAR_RETRY); | 161 IDS_TRANSLATE_INFOBAR_RETRY); |
357 AddChildView(retry_button_); | 162 AddChildView(retry_button_); |
358 } | 163 } |
359 break; | 164 break; |
360 } | 165 } |
361 | 166 |
362 default: | 167 default: |
363 NOTREACHED() << "Invalid translate state change"; | 168 NOTREACHED() << "Invalid translate state change"; |
364 break; | 169 break; |
365 } | 170 } |
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
868 return label; | 673 return label; |
869 } | 674 } |
870 | 675 |
871 views::MenuButton* TranslateInfoBar::CreateMenuButton(int menu_id, | 676 views::MenuButton* TranslateInfoBar::CreateMenuButton(int menu_id, |
872 const std::wstring& text, bool normal_has_border) { | 677 const std::wstring& text, bool normal_has_border) { |
873 // Don't use text to instantiate MenuButton because we need to set font before | 678 // Don't use text to instantiate MenuButton because we need to set font before |
874 // setting text so that the button will resize to fit entire text. | 679 // setting text so that the button will resize to fit entire text. |
875 views::MenuButton* menu_button = | 680 views::MenuButton* menu_button = |
876 new views::MenuButton(NULL, std::wstring(), this, true); | 681 new views::MenuButton(NULL, std::wstring(), this, true); |
877 menu_button->SetID(menu_id); | 682 menu_button->SetID(menu_id); |
878 menu_button->set_border(new TranslateButtonBorder); | 683 menu_button->set_border(new InfoBarButtonBorder); |
879 menu_button->set_menu_marker(ResourceBundle::GetSharedInstance(). | 684 menu_button->set_menu_marker(ResourceBundle::GetSharedInstance(). |
880 GetBitmapNamed(IDR_INFOBARBUTTON_MENU_DROPARROW)); | 685 GetBitmapNamed(IDR_INFOBARBUTTON_MENU_DROPARROW)); |
881 if (normal_has_border) { | 686 if (normal_has_border) { |
882 menu_button->SetNormalHasBorder(true); // Normal button state has border. | 687 menu_button->SetNormalHasBorder(true); // Normal button state has border. |
883 // Disable animation during state change. | 688 // Disable animation during state change. |
884 menu_button->SetAnimationDuration(0); | 689 menu_button->SetAnimationDuration(0); |
885 } | 690 } |
886 // Set font colors for different states. | 691 // Set font colors for different states. |
887 menu_button->SetEnabledColor(SK_ColorBLACK); | 692 menu_button->SetEnabledColor(SK_ColorBLACK); |
888 menu_button->SetHighlightColor(SK_ColorBLACK); | 693 menu_button->SetHighlightColor(SK_ColorBLACK); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
972 inline int TranslateInfoBar::GetSpacingAfterFirstLanguageButton() const { | 777 inline int TranslateInfoBar::GetSpacingAfterFirstLanguageButton() const { |
973 return (state_ == TranslateInfoBarDelegate::kAfterTranslate ? | 778 return (state_ == TranslateInfoBarDelegate::kAfterTranslate ? |
974 kButtonInLabelSpacing : 10); | 779 kButtonInLabelSpacing : 10); |
975 } | 780 } |
976 | 781 |
977 // TranslateInfoBarDelegate, InfoBarDelegate overrides: ------------------ | 782 // TranslateInfoBarDelegate, InfoBarDelegate overrides: ------------------ |
978 | 783 |
979 InfoBar* TranslateInfoBarDelegate::CreateInfoBar() { | 784 InfoBar* TranslateInfoBarDelegate::CreateInfoBar() { |
980 return new TranslateInfoBar(this); | 785 return new TranslateInfoBar(this); |
981 } | 786 } |
OLD | NEW |