keyword_hint_view.cc revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2011 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 "base/logging.h"
8#include "base/utf_string_conversions.h"
9#include "chrome/app/chrome_command_ids.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/search_engines/template_url_model.h"
12#include "grit/generated_resources.h"
13#include "grit/theme_resources.h"
14#include "third_party/skia/include/core/SkBitmap.h"
15#include "ui/base/l10n/l10n_util.h"
16#include "ui/base/resource/resource_bundle.h"
17#include "ui/gfx/canvas.h"
18#include "views/controls/label.h"
19
20// Amount of space to offset the tab image from the top of the view by.
21static const int kTabImageYOffset = 4;
22
23// The tab key image.
24static const SkBitmap* kTabButtonBitmap = NULL;
25
26KeywordHintView::KeywordHintView(Profile* profile) : profile_(profile) {
27  leading_label_ = new views::Label();
28  trailing_label_ = new views::Label();
29  AddChildView(leading_label_);
30  AddChildView(trailing_label_);
31
32  if (!kTabButtonBitmap) {
33    kTabButtonBitmap = ResourceBundle::GetSharedInstance().
34        GetBitmapNamed(IDR_LOCATION_BAR_KEYWORD_HINT_TAB);
35  }
36}
37
38KeywordHintView::~KeywordHintView() {
39}
40
41void KeywordHintView::SetFont(const gfx::Font& font) {
42  leading_label_->SetFont(font);
43  trailing_label_->SetFont(font);
44}
45
46void KeywordHintView::SetColor(const SkColor& color) {
47  leading_label_->SetColor(color);
48  trailing_label_->SetColor(color);
49}
50
51void KeywordHintView::SetKeyword(const string16& keyword) {
52  keyword_ = keyword;
53  if (keyword_.empty())
54    return;
55  DCHECK(profile_);
56  if (!profile_->GetTemplateURLModel())
57    return;
58
59  std::vector<size_t> content_param_offsets;
60  bool is_extension_keyword;
61  string16 short_name = profile_->GetTemplateURLModel()->
62      GetKeywordShortName(keyword, &is_extension_keyword);
63  int message_id = is_extension_keyword ?
64      IDS_OMNIBOX_EXTENSION_KEYWORD_HINT : IDS_OMNIBOX_KEYWORD_HINT;
65  const std::wstring keyword_hint =
66      UTF16ToWide(l10n_util::GetStringFUTF16(
67          message_id,
68          string16(),
69          short_name,
70          &content_param_offsets));
71  if (content_param_offsets.size() == 2) {
72    leading_label_->SetText(
73        keyword_hint.substr(0, content_param_offsets.front()));
74    trailing_label_->SetText(
75        keyword_hint.substr(content_param_offsets.front()));
76  } else {
77    // See comments on an identical NOTREACHED() in search_provider.cc.
78    NOTREACHED();
79  }
80}
81
82void KeywordHintView::OnPaint(gfx::Canvas* canvas) {
83  int image_x = leading_label_->IsVisible() ? leading_label_->width() : 0;
84
85  // Since we paint the button image directly on the canvas (instead of using a
86  // child view), we must mirror the button's position manually if the locale
87  // is right-to-left.
88  gfx::Rect tab_button_bounds(image_x,
89                              kTabImageYOffset,
90                              kTabButtonBitmap->width(),
91                              kTabButtonBitmap->height());
92  tab_button_bounds.set_x(GetMirroredXForRect(tab_button_bounds));
93  canvas->DrawBitmapInt(*kTabButtonBitmap,
94                        tab_button_bounds.x(),
95                        tab_button_bounds.y());
96}
97
98gfx::Size KeywordHintView::GetPreferredSize() {
99  // TODO(sky): currently height doesn't matter, once baseline support is
100  // added this should check baselines.
101  gfx::Size prefsize = leading_label_->GetPreferredSize();
102  int width = prefsize.width();
103  width += kTabButtonBitmap->width();
104  prefsize = trailing_label_->GetPreferredSize();
105  width += prefsize.width();
106  return gfx::Size(width, prefsize.height());
107}
108
109gfx::Size KeywordHintView::GetMinimumSize() {
110  // TODO(sky): currently height doesn't matter, once baseline support is
111  // added this should check baselines.
112  return gfx::Size(kTabButtonBitmap->width(), 0);
113}
114
115void KeywordHintView::Layout() {
116  // TODO(sky): baseline layout.
117  bool show_labels = (width() != kTabButtonBitmap->width());
118
119  leading_label_->SetVisible(show_labels);
120  trailing_label_->SetVisible(show_labels);
121  int x = 0;
122  gfx::Size pref;
123
124  if (show_labels) {
125    pref = leading_label_->GetPreferredSize();
126    leading_label_->SetBounds(x, 0, pref.width(), height());
127
128    x += pref.width() + kTabButtonBitmap->width();
129    pref = trailing_label_->GetPreferredSize();
130    trailing_label_->SetBounds(x, 0, pref.width(), height());
131  }
132}
133