selected_keyword_decoration.mm revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2010 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/utf_string_conversions.h"
8#import "chrome/browser/autocomplete/autocomplete_edit_view_mac.h"
9#import "chrome/browser/ui/cocoa/image_utils.h"
10#include "chrome/browser/ui/omnibox/location_bar_util.h"
11#include "grit/theme_resources.h"
12#include "grit/generated_resources.h"
13#include "ui/base/l10n/l10n_util_mac.h"
14
15SelectedKeywordDecoration::SelectedKeywordDecoration(NSFont* font)
16    : BubbleDecoration(font) {
17  search_image_.reset([AutocompleteEditViewMac::ImageForResource(
18      IDR_KEYWORD_SEARCH_MAGNIFIER) retain]);
19
20  // Matches the color of the highlighted line in the popup.
21  NSColor* background_color = [NSColor selectedControlColor];
22
23  // Match focus ring's inner color.
24  NSColor* border_color =
25      [[NSColor keyboardFocusIndicatorColor] colorWithAlphaComponent:0.5];
26  SetColors(border_color, background_color, [NSColor blackColor]);
27}
28
29SelectedKeywordDecoration::~SelectedKeywordDecoration() {}
30
31CGFloat SelectedKeywordDecoration::GetWidthForSpace(CGFloat width) {
32  const CGFloat full_width =
33      GetWidthForImageAndLabel(search_image_, full_string_);
34  if (full_width <= width) {
35    BubbleDecoration::SetImage(search_image_);
36    SetLabel(full_string_);
37    return full_width;
38  }
39
40  BubbleDecoration::SetImage(nil);
41  const CGFloat no_image_width = GetWidthForImageAndLabel(nil, full_string_);
42  if (no_image_width <= width || !partial_string_) {
43    SetLabel(full_string_);
44    return no_image_width;
45  }
46
47  SetLabel(partial_string_);
48  return GetWidthForImageAndLabel(nil, partial_string_);
49}
50
51void SelectedKeywordDecoration::SetKeyword(const string16& short_name,
52                                           bool is_extension_keyword) {
53  const string16 min_name(WideToUTF16Hack(
54      location_bar_util::CalculateMinString(UTF16ToWideHack(short_name))));
55  const int message_id = is_extension_keyword ?
56      IDS_OMNIBOX_EXTENSION_KEYWORD_TEXT : IDS_OMNIBOX_KEYWORD_TEXT;
57
58  // The text will be like "Search <name>:".  "<name>" is a parameter
59  // derived from |short_name|.
60  full_string_.reset(
61      [l10n_util::GetNSStringF(message_id, short_name) copy]);
62
63  if (min_name.empty()) {
64    partial_string_.reset();
65  } else {
66    partial_string_.reset(
67        [l10n_util::GetNSStringF(message_id, min_name) copy]);
68  }
69}
70
71void SelectedKeywordDecoration::SetImage(NSImage* image) {
72  if (image != search_image_)
73    search_image_.reset([image retain]);
74  BubbleDecoration::SetImage(image);
75}
76