Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / location_bar / selected_keyword_decoration.mm
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 #import "chrome/browser/ui/cocoa/location_bar/selected_keyword_decoration.h"
6
7 #include "base/strings/sys_string_conversions.h"
8 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
9 #include "chrome/browser/ui/location_bar/location_bar_util.h"
10 #include "grit/generated_resources.h"
11 #include "grit/theme_resources.h"
12 #include "ui/base/l10n/l10n_util_mac.h"
13
14 SelectedKeywordDecoration::SelectedKeywordDecoration() {
15   search_image_.reset([OmniboxViewMac::ImageForResource(
16       IDR_KEYWORD_SEARCH_MAGNIFIER) retain]);
17   SetTextColor([NSColor blackColor]);
18 }
19
20 SelectedKeywordDecoration::~SelectedKeywordDecoration() {}
21
22 CGFloat SelectedKeywordDecoration::GetWidthForSpace(CGFloat width) {
23   const CGFloat full_width =
24       GetWidthForImageAndLabel(search_image_, full_string_);
25   if (full_width <= width) {
26     BubbleDecoration::SetImage(search_image_);
27     SetLabel(full_string_);
28     return full_width;
29   }
30
31   BubbleDecoration::SetImage(nil);
32   const CGFloat no_image_width = GetWidthForImageAndLabel(nil, full_string_);
33   if (no_image_width <= width || !partial_string_) {
34     SetLabel(full_string_);
35     return no_image_width;
36   }
37
38   SetLabel(partial_string_);
39   return GetWidthForImageAndLabel(nil, partial_string_);
40 }
41
42 ui::NinePartImageIds SelectedKeywordDecoration::GetBubbleImageIds() {
43   return IMAGE_GRID(IDR_OMNIBOX_SELECTED_KEYWORD_BUBBLE);
44 }
45
46 void SelectedKeywordDecoration::SetKeyword(const base::string16& short_name,
47                                            bool is_extension_keyword) {
48   const base::string16 min_name(
49       location_bar_util::CalculateMinString(short_name));
50   NSString* full_string = is_extension_keyword ?
51       base::SysUTF16ToNSString(short_name) :
52       l10n_util::GetNSStringF(IDS_OMNIBOX_KEYWORD_TEXT, short_name);
53
54   // The text will be like "Search <name>:".  "<name>" is a parameter
55   // derived from |short_name|.
56   full_string_.reset([full_string copy]);
57
58   if (min_name.empty()) {
59     partial_string_.reset();
60   } else {
61     NSString* partial_string = is_extension_keyword ?
62         base::SysUTF16ToNSString(min_name) :
63         l10n_util::GetNSStringF(IDS_OMNIBOX_KEYWORD_TEXT, min_name);
64     partial_string_.reset([partial_string copy]);
65   }
66 }
67
68 void SelectedKeywordDecoration::SetImage(NSImage* image) {
69   if (image != search_image_)
70     search_image_.reset([image retain]);
71   BubbleDecoration::SetImage(image);
72 }