Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Side by Side Diff: chrome/browser/autocomplete/autocomplete_popup_view_mac.mm

Issue 3083022: Rework gfx::Font by moving platform-specific code into inner classes.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <cmath> 5 #include <cmath>
6 6
7 #include "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" 7 #include "chrome/browser/autocomplete/autocomplete_popup_view_mac.h"
8 8
9 #include "app/resource_bundle.h" 9 #include "app/resource_bundle.h"
10 #include "app/text_elider.h" 10 #include "app/text_elider.h"
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 NSMutableAttributedString* AutocompletePopupViewMac::DecorateMatchedString( 96 NSMutableAttributedString* AutocompletePopupViewMac::DecorateMatchedString(
97 const std::wstring &matchString, 97 const std::wstring &matchString,
98 const AutocompleteMatch::ACMatchClassifications &classifications, 98 const AutocompleteMatch::ACMatchClassifications &classifications,
99 NSColor* textColor, gfx::Font& font) { 99 NSColor* textColor, gfx::Font& font) {
100 // Cache for on-demand computation of the bold version of |font|. 100 // Cache for on-demand computation of the bold version of |font|.
101 NSFont* boldFont = nil; 101 NSFont* boldFont = nil;
102 102
103 // Start out with a string using the default style info. 103 // Start out with a string using the default style info.
104 NSString* s = base::SysWideToNSString(matchString); 104 NSString* s = base::SysWideToNSString(matchString);
105 NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys: 105 NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:
106 font.nativeFont(), NSFontAttributeName, 106 font.GetNativeFont(), NSFontAttributeName,
107 textColor, NSForegroundColorAttributeName, 107 textColor, NSForegroundColorAttributeName,
108 nil]; 108 nil];
109 NSMutableAttributedString* as = 109 NSMutableAttributedString* as =
110 [[[NSMutableAttributedString alloc] initWithString:s 110 [[[NSMutableAttributedString alloc] initWithString:s
111 attributes:attributes] 111 attributes:attributes]
112 autorelease]; 112 autorelease];
113 113
114 // Mark up the runs which differ from the default. 114 // Mark up the runs which differ from the default.
115 for (ACMatchClassifications::const_iterator i = classifications.begin(); 115 for (ACMatchClassifications::const_iterator i = classifications.begin();
116 i != classifications.end(); ++i) { 116 i != classifications.end(); ++i) {
117 const BOOL isLast = (i+1) == classifications.end(); 117 const BOOL isLast = (i+1) == classifications.end();
118 const size_t nextOffset = (isLast ? matchString.length() : (i+1)->offset); 118 const size_t nextOffset = (isLast ? matchString.length() : (i+1)->offset);
119 const NSInteger location = static_cast<NSInteger>(i->offset); 119 const NSInteger location = static_cast<NSInteger>(i->offset);
120 const NSInteger length = static_cast<NSInteger>(nextOffset - i->offset); 120 const NSInteger length = static_cast<NSInteger>(nextOffset - i->offset);
121 const NSRange range = NSMakeRange(location, length); 121 const NSRange range = NSMakeRange(location, length);
122 122
123 if (0 != (i->style & ACMatchClassification::URL)) { 123 if (0 != (i->style & ACMatchClassification::URL)) {
124 [as addAttribute:NSForegroundColorAttributeName 124 [as addAttribute:NSForegroundColorAttributeName
125 value:URLTextColor() range:range]; 125 value:URLTextColor() range:range];
126 } 126 }
127 127
128 if (0 != (i->style & ACMatchClassification::MATCH)) { 128 if (0 != (i->style & ACMatchClassification::MATCH)) {
129 if (!boldFont) { 129 if (!boldFont) {
130 NSFontManager* fontManager = [NSFontManager sharedFontManager]; 130 NSFontManager* fontManager = [NSFontManager sharedFontManager];
131 boldFont = [fontManager convertFont:font.nativeFont() 131 boldFont = [fontManager convertFont:font.GetNativeFont()
132 toHaveTrait:NSBoldFontMask]; 132 toHaveTrait:NSBoldFontMask];
133 } 133 }
134 [as addAttribute:NSFontAttributeName value:boldFont range:range]; 134 [as addAttribute:NSFontAttributeName value:boldFont range:range];
135 } 135 }
136 } 136 }
137 137
138 return as; 138 return as;
139 } 139 }
140 140
141 NSMutableAttributedString* AutocompletePopupViewMac::ElideString( 141 NSMutableAttributedString* AutocompletePopupViewMac::ElideString(
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 // the cell to make sure the description will be at least 190 // the cell to make sure the description will be at least
191 // partially visible. 191 // partially visible.
192 // TODO(shess): Consider revising our NSCell subclass to have two 192 // TODO(shess): Consider revising our NSCell subclass to have two
193 // bits and just draw them right, rather than truncating here. 193 // bits and just draw them right, rather than truncating here.
194 const float textWidth = cellWidth - kTextXOffset; 194 const float textWidth = cellWidth - kTextXOffset;
195 as = ElideString(as, match.contents, font, 195 as = ElideString(as, match.contents, font,
196 textWidth * kMaxContentsFraction); 196 textWidth * kMaxContentsFraction);
197 197
198 NSDictionary* attributes = 198 NSDictionary* attributes =
199 [NSDictionary dictionaryWithObjectsAndKeys: 199 [NSDictionary dictionaryWithObjectsAndKeys:
200 font.nativeFont(), NSFontAttributeName, 200 font.GetNativeFont(), NSFontAttributeName,
201 ContentTextColor(), NSForegroundColorAttributeName, 201 ContentTextColor(), NSForegroundColorAttributeName,
202 nil]; 202 nil];
203 NSString* rawEnDash = [NSString stringWithFormat:@" %C ", 0x2013]; 203 NSString* rawEnDash = [NSString stringWithFormat:@" %C ", 0x2013];
204 NSAttributedString* enDash = 204 NSAttributedString* enDash =
205 [[[NSAttributedString alloc] initWithString:rawEnDash 205 [[[NSAttributedString alloc] initWithString:rawEnDash
206 attributes:attributes] autorelease]; 206 attributes:attributes] autorelease];
207 207
208 NSAttributedString* description = 208 NSAttributedString* description =
209 DecorateMatchedString(match.description, match.description_class, 209 DecorateMatchedString(match.description, match.description_class,
210 DescriptionTextColor(), font); 210 DescriptionTextColor(), font);
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 popup_.reset(nil); 398 popup_.reset(nil);
399 399
400 return; 400 return;
401 } 401 }
402 402
403 CreatePopupIfNeeded(); 403 CreatePopupIfNeeded();
404 404
405 // The popup's font is a slightly smaller version of the field's. 405 // The popup's font is a slightly smaller version of the field's.
406 NSFont* fieldFont = AutocompleteEditViewMac::GetFieldFont(); 406 NSFont* fieldFont = AutocompleteEditViewMac::GetFieldFont();
407 const CGFloat resultFontSize = [fieldFont pointSize] + kEditFontAdjust; 407 const CGFloat resultFontSize = [fieldFont pointSize] + kEditFontAdjust;
408 gfx::Font resultFont = gfx::Font::CreateFont( 408 gfx::Font resultFont(base::SysNSStringToWide([fieldFont fontName]),
409 base::SysNSStringToWide([fieldFont fontName]), (int)resultFontSize); 409 static_cast<int>(resultFontSize));
410 410
411 AutocompleteMatrix* matrix = [popup_ contentView]; 411 AutocompleteMatrix* matrix = [popup_ contentView];
412 412
413 // Calculate the width of the matrix based on backing out the 413 // Calculate the width of the matrix based on backing out the
414 // popup's border from the width of the field. Would prefer to use 414 // popup's border from the width of the field. Would prefer to use
415 // [matrix convertSize:fromView:] for converting from screen size, 415 // [matrix convertSize:fromView:] for converting from screen size,
416 // but that doesn't work until the popup is on-screen (bug?). 416 // but that doesn't work until the popup is on-screen (bug?).
417 const NSRect fieldRectBase = [field_ convertRect:[field_ bounds] toView:nil]; 417 const NSRect fieldRectBase = [field_ convertRect:[field_ bounds] toView:nil];
418 const CGFloat popupWidth = NSWidth(fieldRectBase) - 2 * kWindowBorderWidth; 418 const CGFloat popupWidth = NSWidth(fieldRectBase) - 2 * kWindowBorderWidth;
419 DCHECK_GT(popupWidth, 0.0); 419 DCHECK_GT(popupWidth, 0.0);
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
734 yRadius:kPopupRoundingRadius]; 734 yRadius:kPopupRoundingRadius];
735 735
736 // Draw the matrix clipped to our border. 736 // Draw the matrix clipped to our border.
737 [NSGraphicsContext saveGraphicsState]; 737 [NSGraphicsContext saveGraphicsState];
738 [path addClip]; 738 [path addClip];
739 [super drawRect:rect]; 739 [super drawRect:rect];
740 [NSGraphicsContext restoreGraphicsState]; 740 [NSGraphicsContext restoreGraphicsState];
741 } 741 }
742 742
743 @end 743 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698