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 "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 // How far the editor insets itself, for purposes of determining if
28 // decorations need to be trimmed.
29 const CGFloat kEditorHorizontalInset = 3.0;
31 // Conveniences to centralize width+offset calculations.
32 CGFloat WidthForResults(NSAttributedString* resultsString) {
33 return kResultsXOffset + ceil([resultsString size].width) +
34 2 * kResultsTokenInset;
39 @implementation FindBarTextFieldCell
41 - (CGFloat)topTextFrameOffset {
45 - (CGFloat)bottomTextFrameOffset {
49 - (CGFloat)cornerRadius {
53 - (rect_path_utils::RoundedCornerFlags)roundedCornerFlags {
54 return rect_path_utils::RoundedCornerLeft;
57 // Convenience for the attributes used in the right-justified info
58 // cells. Sets the background color to red if |foundMatches| is YES.
59 - (NSDictionary*)resultsAttributes:(BOOL)foundMatches {
60 base::scoped_nsobject<NSMutableParagraphStyle> style(
61 [[NSMutableParagraphStyle alloc] init]);
62 [style setAlignment:NSRightTextAlignment];
64 return [NSDictionary dictionaryWithObjectsAndKeys:
65 [self font], NSFontAttributeName,
66 [NSColor lightGrayColor], NSForegroundColorAttributeName,
67 [NSColor whiteColor], NSBackgroundColorAttributeName,
68 style.get(), NSParagraphStyleAttributeName,
72 - (void)setActiveMatch:(NSInteger)current of:(NSInteger)total {
74 base::SysUTF16ToNSString(l10n_util::GetStringFUTF16(
75 IDS_FIND_IN_PAGE_COUNT,
76 base::IntToString16(current),
77 base::IntToString16(total)));
78 resultsString_.reset([[NSAttributedString alloc]
79 initWithString:results
80 attributes:[self resultsAttributes:(total > 0)]]);
83 - (void)clearResults {
84 resultsString_.reset(nil);
87 - (NSString*)resultsString {
88 return [resultsString_ string];
91 - (NSRect)textFrameForFrame:(NSRect)cellFrame {
92 NSRect textFrame([super textFrameForFrame:cellFrame]);
94 textFrame.size.width -= WidthForResults(resultsString_);
98 // Do not show the I-beam cursor over the results label.
99 - (NSRect)textCursorFrameForFrame:(NSRect)cellFrame {
100 return [self textFrameForFrame:cellFrame];
103 - (void)drawResultsWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
104 DCHECK(resultsString_);
106 NSRect textFrame = [self textFrameForFrame:cellFrame];
107 NSRect infoFrame(NSMakeRect(NSMaxX(textFrame),
108 cellFrame.origin.y + kResultsYOffset,
109 ceil([resultsString_ size].width),
110 cellFrame.size.height - kResultsYOffset));
111 [resultsString_.get() drawInRect:infoFrame];
114 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
116 [self drawResultsWithFrame:cellFrame inView:controlView];
117 [super drawInteriorWithFrame:cellFrame inView:controlView];