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