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/selected_keyword_view.h"
7 #include "base/logging.h"
8 #include "base/strings/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 "grit/theme_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/views/painter.h"
21 const int kBackgroundImages[] = IMAGE_GRID(IDR_OMNIBOX_SELECTED_KEYWORD_BUBBLE);
25 SelectedKeywordView::SelectedKeywordView(const gfx::FontList& font_list,
27 SkColor parent_background_color,
29 : IconLabelBubbleView(kBackgroundImages, NULL, IDR_KEYWORD_SEARCH_MAGNIFIER,
30 font_list, text_color, parent_background_color,
33 full_label_.SetFontList(font_list);
34 full_label_.SetVisible(false);
35 partial_label_.SetFontList(font_list);
36 partial_label_.SetVisible(false);
39 SelectedKeywordView::~SelectedKeywordView() {
42 gfx::Size SelectedKeywordView::GetPreferredSize() {
43 // Height will be ignored by the LocationBarView.
44 return GetSizeForLabelWidth(full_label_.GetPreferredSize().width());
47 gfx::Size SelectedKeywordView::GetMinimumSize() {
48 // Height will be ignored by the LocationBarView.
49 return GetSizeForLabelWidth(partial_label_.GetMinimumSize().width());
52 void SelectedKeywordView::Layout() {
53 SetLabel(((width() == GetPreferredSize().width()) ?
54 full_label_ : partial_label_).text());
55 IconLabelBubbleView::Layout();
58 void SelectedKeywordView::SetKeyword(const string16& keyword) {
63 TemplateURLService* model =
64 TemplateURLServiceFactory::GetForProfile(profile_);
68 bool is_extension_keyword;
69 const string16 short_name = model->GetKeywordShortName(keyword,
70 &is_extension_keyword);
71 const string16 full_name = is_extension_keyword ?
73 l10n_util::GetStringFUTF16(IDS_OMNIBOX_KEYWORD_TEXT, short_name);
74 full_label_.SetText(full_name);
76 const string16 min_string(location_bar_util::CalculateMinString(short_name));
77 const string16 partial_name = is_extension_keyword ?
79 l10n_util::GetStringFUTF16(IDS_OMNIBOX_KEYWORD_TEXT, min_string);
80 partial_label_.SetText(min_string.empty() ?
81 full_label_.text() : partial_name);