keyword_hint_view.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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/location_bar/keyword_hint_view.h"
6
7#include <vector>
8
9#include "base/logging.h"
10#include "base/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"
23
24
25KeywordHintView::KeywordHintView(Profile* profile,
26                                 const gfx::Font& font,
27                                 int font_y_offset,
28                                 const LocationBarView* location_bar_view)
29    : profile_(profile),
30      tab_image_(new views::ImageView()) {
31  leading_label_ = CreateLabel(font, font_y_offset, location_bar_view);
32  tab_image_->SetImage(
33      ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
34          IDR_OMNIBOX_KEYWORD_HINT_TAB));
35  AddChildView(tab_image_);
36  trailing_label_ = CreateLabel(font, font_y_offset, location_bar_view);
37}
38
39KeywordHintView::~KeywordHintView() {
40}
41
42void KeywordHintView::SetKeyword(const string16& keyword) {
43  keyword_ = keyword;
44  if (keyword_.empty())
45    return;
46  DCHECK(profile_);
47  TemplateURLService* url_service =
48      TemplateURLServiceFactory::GetForProfile(profile_);
49  if (!url_service)
50    return;
51
52  std::vector<size_t> content_param_offsets;
53  bool is_extension_keyword;
54  string16 short_name(
55      url_service->GetKeywordShortName(keyword, &is_extension_keyword));
56  int message_id = is_extension_keyword ?
57      IDS_OMNIBOX_EXTENSION_KEYWORD_HINT : IDS_OMNIBOX_KEYWORD_HINT;
58  const string16 keyword_hint = l10n_util::GetStringFUTF16(
59      message_id, string16(), short_name, &content_param_offsets);
60  DCHECK_EQ(2U, content_param_offsets.size());
61  leading_label_->SetText(
62      keyword_hint.substr(0, content_param_offsets.front()));
63  trailing_label_->SetText(keyword_hint.substr(content_param_offsets.front()));
64}
65
66gfx::Size KeywordHintView::GetPreferredSize() {
67  // Height will be ignored by the LocationBarView.
68  return gfx::Size(leading_label_->GetPreferredSize().width() +
69                       tab_image_->GetPreferredSize().width() +
70                       trailing_label_->GetPreferredSize().width(),
71                   0);
72}
73
74gfx::Size KeywordHintView::GetMinimumSize() {
75  // Height will be ignored by the LocationBarView.
76  return tab_image_->GetPreferredSize();
77}
78
79void KeywordHintView::Layout() {
80  int tab_width = tab_image_->GetPreferredSize().width();
81  bool show_labels = (width() != tab_width);
82  gfx::Size leading_size(leading_label_->GetPreferredSize());
83  leading_label_->SetBounds(0, 0, show_labels ? leading_size.width() : 0,
84                            leading_size.height());
85  tab_image_->SetBounds(leading_label_->bounds().right(), 0, tab_width,
86                        height());
87  gfx::Size trailing_size(trailing_label_->GetPreferredSize());
88  trailing_label_->SetBounds(tab_image_->bounds().right(), 0,
89                             show_labels ? trailing_size.width() : 0,
90                             trailing_size.height());
91}
92
93views::Label* KeywordHintView::CreateLabel(
94    const gfx::Font& font,
95    int font_y_offset,
96    const LocationBarView* location_bar_view) {
97  views::Label* label = new views::Label();
98  label->set_border(views::Border::CreateEmptyBorder(font_y_offset, 0, 0, 0));
99  label->SetFont(font);
100  label->SetEnabledColor(location_bar_view->GetColor(
101      ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT));
102  label->SetBackgroundColor(location_bar_view->GetColor(
103      ToolbarModel::NONE, LocationBarView::BACKGROUND));
104  AddChildView(label);
105  return label;
106}
107