Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / find_bar / find_bar_text_field_cell.mm
1 // Copyright (c) 2009 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/find_bar/find_bar_text_field_cell.h"
6
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "ui/base/l10n/l10n_util.h"
13
14 namespace {
15
16 // How far to offset the keyword token into the field.
17 const NSInteger kResultsXOffset = 3;
18
19 // How much width (beyond text) to add to the keyword token on each
20 // side.
21 const NSInteger kResultsTokenInset = 3;
22
23 // How far to shift bounding box of hint down from top of field.
24 // Assumes -setFlipped:YES.
25 const NSInteger kResultsYOffset = 4;
26
27 // Conveniences to centralize width+offset calculations.
28 CGFloat WidthForResults(NSAttributedString* resultsString) {
29   return kResultsXOffset + ceil([resultsString size].width) +
30       2 * kResultsTokenInset;
31 }
32
33 }  // namespace
34
35 @implementation FindBarTextFieldCell
36
37 - (CGFloat)topTextFrameOffset {
38   return 1.0;
39 }
40
41 - (CGFloat)bottomTextFrameOffset {
42   return 1.0;
43 }
44
45 - (CGFloat)cornerRadius {
46   return 4.0;
47 }
48
49 - (rect_path_utils::RoundedCornerFlags)roundedCornerFlags {
50   return rect_path_utils::RoundedCornerLeft;
51 }
52
53 // Convenience for the attributes used in the right-justified info
54 // cells.  Sets the background color to red if |foundMatches| is YES.
55 - (NSDictionary*)resultsAttributes:(BOOL)foundMatches {
56   base::scoped_nsobject<NSMutableParagraphStyle> style(
57       [[NSMutableParagraphStyle alloc] init]);
58   [style setAlignment:NSRightTextAlignment];
59
60   return [NSDictionary dictionaryWithObjectsAndKeys:
61               [self font], NSFontAttributeName,
62               [NSColor lightGrayColor], NSForegroundColorAttributeName,
63               [NSColor whiteColor], NSBackgroundColorAttributeName,
64               style.get(), NSParagraphStyleAttributeName,
65               nil];
66 }
67
68 - (void)setActiveMatch:(NSInteger)current of:(NSInteger)total {
69   NSString* results =
70       base::SysUTF16ToNSString(l10n_util::GetStringFUTF16(
71           IDS_FIND_IN_PAGE_COUNT,
72           base::IntToString16(current),
73           base::IntToString16(total)));
74   resultsString_.reset([[NSAttributedString alloc]
75                          initWithString:results
76                          attributes:[self resultsAttributes:(total > 0)]]);
77 }
78
79 - (void)clearResults {
80   resultsString_.reset(nil);
81 }
82
83 - (NSString*)resultsString {
84   return [resultsString_ string];
85 }
86
87 - (NSRect)textFrameForFrame:(NSRect)cellFrame {
88   NSRect textFrame([super textFrameForFrame:cellFrame]);
89   if (resultsString_)
90     textFrame.size.width -= WidthForResults(resultsString_);
91   return textFrame;
92 }
93
94 // Do not show the I-beam cursor over the results label.
95 - (NSRect)textCursorFrameForFrame:(NSRect)cellFrame {
96   return [self textFrameForFrame:cellFrame];
97 }
98
99 - (void)drawResultsWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
100   DCHECK(resultsString_);
101
102   NSRect textFrame = [self textFrameForFrame:cellFrame];
103   NSRect infoFrame(NSMakeRect(NSMaxX(textFrame),
104                               cellFrame.origin.y + kResultsYOffset,
105                               ceil([resultsString_ size].width),
106                               cellFrame.size.height - kResultsYOffset));
107   [resultsString_.get() drawInRect:infoFrame];
108 }
109
110 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
111   if (resultsString_)
112     [self drawResultsWithFrame:cellFrame inView:controlView];
113   [super drawInteriorWithFrame:cellFrame inView:controlView];
114 }
115
116 @end