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.
5 #include "chrome/browser/ui/views/location_bar/keyword_hint_view.h"
9 #include "base/logging.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/search_engines/template_url_service.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h"
15 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
16 #include "grit/generated_resources.h"
17 #include "grit/theme_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/gfx/canvas.h"
21 #include "ui/views/controls/image_view.h"
22 #include "ui/views/controls/label.h"
25 KeywordHintView::KeywordHintView(Profile
* profile
,
26 const gfx::FontList
& font_list
,
28 SkColor background_color
)
30 tab_image_(new views::ImageView()) {
32 CreateLabel(font_list
, text_color
, background_color
);
34 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
35 IDR_OMNIBOX_KEYWORD_HINT_TAB
));
36 AddChildView(tab_image_
);
38 CreateLabel(font_list
, text_color
, background_color
);
41 KeywordHintView::~KeywordHintView() {
44 void KeywordHintView::SetKeyword(const base::string16
& keyword
) {
49 TemplateURLService
* url_service
=
50 TemplateURLServiceFactory::GetForProfile(profile_
);
54 std::vector
<size_t> content_param_offsets
;
55 bool is_extension_keyword
;
56 base::string16
short_name(
57 url_service
->GetKeywordShortName(keyword
, &is_extension_keyword
));
58 int message_id
= is_extension_keyword
?
59 IDS_OMNIBOX_EXTENSION_KEYWORD_HINT
: IDS_OMNIBOX_KEYWORD_HINT
;
60 const base::string16 keyword_hint
= l10n_util::GetStringFUTF16(
61 message_id
, base::string16(), short_name
, &content_param_offsets
);
62 DCHECK_EQ(2U, content_param_offsets
.size());
63 leading_label_
->SetText(
64 keyword_hint
.substr(0, content_param_offsets
.front()));
65 trailing_label_
->SetText(keyword_hint
.substr(content_param_offsets
.front()));
68 gfx::Size
KeywordHintView::GetPreferredSize() const {
69 // Height will be ignored by the LocationBarView.
70 return gfx::Size(leading_label_
->GetPreferredSize().width() +
71 tab_image_
->GetPreferredSize().width() +
72 trailing_label_
->GetPreferredSize().width(),
76 gfx::Size
KeywordHintView::GetMinimumSize() const {
77 // Height will be ignored by the LocationBarView.
78 return tab_image_
->GetPreferredSize();
81 void KeywordHintView::Layout() {
82 int tab_width
= tab_image_
->GetPreferredSize().width();
83 bool show_labels
= (width() != tab_width
);
84 gfx::Size
leading_size(leading_label_
->GetPreferredSize());
85 leading_label_
->SetBounds(0, 0, show_labels
? leading_size
.width() : 0,
87 tab_image_
->SetBounds(leading_label_
->bounds().right(), 0, tab_width
,
89 gfx::Size
trailing_size(trailing_label_
->GetPreferredSize());
90 trailing_label_
->SetBounds(tab_image_
->bounds().right(), 0,
91 show_labels
? trailing_size
.width() : 0,
95 views::Label
* KeywordHintView::CreateLabel(const gfx::FontList
& font_list
,
97 SkColor background_color
) {
98 views::Label
* label
= new views::Label(base::string16(), font_list
);
99 label
->SetEnabledColor(text_color
);
100 label
->SetBackgroundColor(background_color
);