selected_keyword_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/selected_keyword_view.h"
6
7#include "base/logging.h"
8#include "base/utf_string_conversions.h"
9#include "chrome/browser/profiles/profile.h"
10#include "chrome/browser/search_engines/template_url_service.h"
11#include "chrome/browser/search_engines/template_url_service_factory.h"
12#include "chrome/browser/ui/omnibox/location_bar_util.h"
13#include "chrome/browser/ui/views/location_bar/keyword_hint_view.h"
14#include "grit/generated_resources.h"
15#include "ui/base/l10n/l10n_util.h"
16
17
18SelectedKeywordView::SelectedKeywordView(const int background_images[],
19                                         int contained_image,
20                                         const gfx::Font& font,
21                                         int font_y_offset,
22                                         SkColor color,
23                                         Profile* profile)
24    : IconLabelBubbleView(background_images, contained_image, font,
25                          font_y_offset, color, false),
26      profile_(profile) {
27  full_label_.SetFont(font);
28  full_label_.SetVisible(false);
29  partial_label_.SetFont(font);
30  partial_label_.SetVisible(false);
31}
32
33SelectedKeywordView::~SelectedKeywordView() {
34}
35
36gfx::Size SelectedKeywordView::GetPreferredSize() {
37  // Height will be ignored by the LocationBarView.
38  return GetSizeForLabelWidth(full_label_.GetPreferredSize().width());
39}
40
41gfx::Size SelectedKeywordView::GetMinimumSize() {
42  // Height will be ignored by the LocationBarView.
43  return GetSizeForLabelWidth(partial_label_.GetMinimumSize().width());
44}
45
46void SelectedKeywordView::Layout() {
47  SetLabel(((width() == GetPreferredSize().width()) ?
48      full_label_ : partial_label_).text());
49  IconLabelBubbleView::Layout();
50}
51
52void SelectedKeywordView::SetKeyword(const string16& keyword) {
53  keyword_ = keyword;
54  if (keyword.empty())
55    return;
56  DCHECK(profile_);
57  TemplateURLService* model =
58      TemplateURLServiceFactory::GetForProfile(profile_);
59  if (!model)
60    return;
61
62  bool is_extension_keyword;
63  const string16 short_name = model->GetKeywordShortName(keyword,
64                                                         &is_extension_keyword);
65  const string16 full_name = is_extension_keyword ?
66      short_name :
67      l10n_util::GetStringFUTF16(IDS_OMNIBOX_KEYWORD_TEXT, short_name);
68  full_label_.SetText(full_name);
69
70  const string16 min_string(location_bar_util::CalculateMinString(short_name));
71  const string16 partial_name = is_extension_keyword ?
72      min_string :
73      l10n_util::GetStringFUTF16(IDS_OMNIBOX_KEYWORD_TEXT, min_string);
74  partial_label_.SetText(min_string.empty() ?
75      full_label_.text() : partial_name);
76}
77