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