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.
5 #import "chrome/browser/ui/cocoa/find_bar/find_bar_text_field_cell.h"
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"
16 // How far to offset the keyword token into the field.
17 const NSInteger kResultsXOffset = 3;
19 // How much width (beyond text) to add to the keyword token on each
21 const NSInteger kResultsTokenInset = 3;
23 // How far to shift bounding box of hint down from top of field.
24 // Assumes -setFlipped:YES.
25 const NSInteger kResultsYOffset = 4;
27 // Conveniences to centralize width+offset calculations.
28 CGFloat WidthForResults(NSAttributedString* resultsString) {
29 return kResultsXOffset + ceil([resultsString size].width) +
30 2 * kResultsTokenInset;
35 @implementation FindBarTextFieldCell
37 - (CGFloat)topTextFrameOffset {
41 - (CGFloat)bottomTextFrameOffset {
45 - (CGFloat)cornerRadius {
49 - (rect_path_utils::RoundedCornerFlags)roundedCornerFlags {
50 return rect_path_utils::RoundedCornerLeft;
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];
60 return [NSDictionary dictionaryWithObjectsAndKeys:
61 [self font], NSFontAttributeName,
62 [NSColor lightGrayColor], NSForegroundColorAttributeName,
63 [NSColor whiteColor], NSBackgroundColorAttributeName,
64 style.get(), NSParagraphStyleAttributeName,
68 - (void)setActiveMatch:(NSInteger)current of:(NSInteger)total {
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)]]);
79 - (void)clearResults {
80 resultsString_.reset(nil);
83 - (NSString*)resultsString {
84 return [resultsString_ string];
87 - (NSRect)textFrameForFrame:(NSRect)cellFrame {
88 NSRect textFrame([super textFrameForFrame:cellFrame]);
90 textFrame.size.width -= WidthForResults(resultsString_);
94 // Do not show the I-beam cursor over the results label.
95 - (NSRect)textCursorFrameForFrame:(NSRect)cellFrame {
96 return [self textFrameForFrame:cellFrame];
99 - (void)drawResultsWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
100 DCHECK(resultsString_);
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];
110 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
112 [self drawResultsWithFrame:cellFrame inView:controlView];
113 [super drawInteriorWithFrame:cellFrame inView:controlView];