| OLD | NEW |
| 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 "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
| 10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 } | 95 } |
| 96 static NSColor* URLTextColor() { | 96 static NSColor* URLTextColor() { |
| 97 return [NSColor colorWithCalibratedRed:0.0 green:0.55 blue:0.0 alpha:1.0]; | 97 return [NSColor colorWithCalibratedRed:0.0 green:0.55 blue:0.0 alpha:1.0]; |
| 98 } | 98 } |
| 99 } // namespace | 99 } // namespace |
| 100 | 100 |
| 101 // Helper for MatchText() to allow sharing code between the contents | 101 // Helper for MatchText() to allow sharing code between the contents |
| 102 // and description cases. Returns NSMutableAttributedString as a | 102 // and description cases. Returns NSMutableAttributedString as a |
| 103 // convenience for MatchText(). | 103 // convenience for MatchText(). |
| 104 NSMutableAttributedString* AutocompletePopupViewMac::DecorateMatchedString( | 104 NSMutableAttributedString* AutocompletePopupViewMac::DecorateMatchedString( |
| 105 const string16 &matchString, | 105 const std::wstring &matchString, |
| 106 const AutocompleteMatch::ACMatchClassifications &classifications, | 106 const AutocompleteMatch::ACMatchClassifications &classifications, |
| 107 NSColor* textColor, NSColor* dimTextColor, gfx::Font& font) { | 107 NSColor* textColor, NSColor* dimTextColor, gfx::Font& font) { |
| 108 // Cache for on-demand computation of the bold version of |font|. | 108 // Cache for on-demand computation of the bold version of |font|. |
| 109 NSFont* boldFont = nil; | 109 NSFont* boldFont = nil; |
| 110 | 110 |
| 111 // Start out with a string using the default style info. | 111 // Start out with a string using the default style info. |
| 112 NSString* s = base::SysUTF16ToNSString(matchString); | 112 NSString* s = base::SysWideToNSString(matchString); |
| 113 NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys: | 113 NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys: |
| 114 font.GetNativeFont(), NSFontAttributeName, | 114 font.GetNativeFont(), NSFontAttributeName, |
| 115 textColor, NSForegroundColorAttributeName, | 115 textColor, NSForegroundColorAttributeName, |
| 116 nil]; | 116 nil]; |
| 117 NSMutableAttributedString* as = | 117 NSMutableAttributedString* as = |
| 118 [[[NSMutableAttributedString alloc] initWithString:s | 118 [[[NSMutableAttributedString alloc] initWithString:s |
| 119 attributes:attributes] | 119 attributes:attributes] |
| 120 autorelease]; | 120 autorelease]; |
| 121 | 121 |
| 122 // Mark up the runs which differ from the default. | 122 // Mark up the runs which differ from the default. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 147 value:dimTextColor | 147 value:dimTextColor |
| 148 range:range]; | 148 range:range]; |
| 149 } | 149 } |
| 150 } | 150 } |
| 151 | 151 |
| 152 return as; | 152 return as; |
| 153 } | 153 } |
| 154 | 154 |
| 155 NSMutableAttributedString* AutocompletePopupViewMac::ElideString( | 155 NSMutableAttributedString* AutocompletePopupViewMac::ElideString( |
| 156 NSMutableAttributedString* aString, | 156 NSMutableAttributedString* aString, |
| 157 const string16 originalString, | 157 const std::wstring originalString, |
| 158 const gfx::Font& font, | 158 const gfx::Font& font, |
| 159 const float width) { | 159 const float width) { |
| 160 // If it already fits, nothing to be done. | 160 // If it already fits, nothing to be done. |
| 161 if ([aString size].width <= width) { | 161 if ([aString size].width <= width) { |
| 162 return aString; | 162 return aString; |
| 163 } | 163 } |
| 164 | 164 |
| 165 // If ElideText() decides to do nothing, nothing to be done. | 165 // If ElideText() decides to do nothing, nothing to be done. |
| 166 const string16 elided = ui::ElideText(originalString, font, width, false); | 166 const std::wstring elided(UTF16ToWideHack(ui::ElideText( |
| 167 WideToUTF16Hack(originalString), font, width, false))); |
| 167 if (0 == elided.compare(originalString)) { | 168 if (0 == elided.compare(originalString)) { |
| 168 return aString; | 169 return aString; |
| 169 } | 170 } |
| 170 | 171 |
| 171 // If everything was elided away, clear the string. | 172 // If everything was elided away, clear the string. |
| 172 if (elided.size() == 0) { | 173 if (elided.size() == 0) { |
| 173 [aString deleteCharactersInRange:NSMakeRange(0, [aString length])]; | 174 [aString deleteCharactersInRange:NSMakeRange(0, [aString length])]; |
| 174 return aString; | 175 return aString; |
| 175 } | 176 } |
| 176 | 177 |
| 177 // The ellipses should be the last character, and everything before | 178 // The ellipses should be the last character, and everything before |
| 178 // that should match the original string. | 179 // that should match the original string. |
| 179 const size_t i(elided.size() - 1); | 180 const size_t i(elided.size() - 1); |
| 180 DCHECK(0 != elided.compare(0, i, originalString)); | 181 DCHECK(0 != elided.compare(0, i, originalString)); |
| 181 | 182 |
| 182 // Replace the end of |aString| with the ellipses from |elided|. | 183 // Replace the end of |aString| with the ellipses from |elided|. |
| 183 NSString* s = base::SysUTF16ToNSString(elided.substr(i)); | 184 NSString* s = base::SysWideToNSString(elided.substr(i)); |
| 184 [aString replaceCharactersInRange:NSMakeRange(i, [aString length] - i) | 185 [aString replaceCharactersInRange:NSMakeRange(i, [aString length] - i) |
| 185 withString:s]; | 186 withString:s]; |
| 186 | 187 |
| 187 return aString; | 188 return aString; |
| 188 } | 189 } |
| 189 | 190 |
| 190 // Return the text to show for the match, based on the match's | 191 // Return the text to show for the match, based on the match's |
| 191 // contents and description. Result will be in |font|, with the | 192 // contents and description. Result will be in |font|, with the |
| 192 // boldfaced version used for matches. | 193 // boldfaced version used for matches. |
| 193 NSAttributedString* AutocompletePopupViewMac::MatchText( | 194 NSAttributedString* AutocompletePopupViewMac::MatchText( |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 disposition = | 541 disposition = |
| 541 event_utils::WindowOpenDispositionFromNSEvent([NSApp currentEvent]); | 542 event_utils::WindowOpenDispositionFromNSEvent([NSApp currentEvent]); |
| 542 } | 543 } |
| 543 | 544 |
| 544 // OpenURL() may close the popup, which will clear the result set | 545 // OpenURL() may close the popup, which will clear the result set |
| 545 // and, by extension, |match| and its contents. So copy the | 546 // and, by extension, |match| and its contents. So copy the |
| 546 // relevant strings out to make sure they stay alive until the call | 547 // relevant strings out to make sure they stay alive until the call |
| 547 // completes. | 548 // completes. |
| 548 const AutocompleteMatch& match = model_->result().match_at(row); | 549 const AutocompleteMatch& match = model_->result().match_at(row); |
| 549 const GURL url(match.destination_url); | 550 const GURL url(match.destination_url); |
| 550 string16 keyword; | 551 std::wstring keyword; |
| 551 const bool is_keyword_hint = model_->GetKeywordForMatch(match, &keyword); | 552 const bool is_keyword_hint = model_->GetKeywordForMatch(match, &keyword); |
| 552 edit_view_->OpenURL(url, disposition, match.transition, GURL(), row, | 553 edit_view_->OpenURL(url, disposition, match.transition, GURL(), row, |
| 553 is_keyword_hint ? string16() : keyword); | 554 is_keyword_hint ? std::wstring() : keyword); |
| 554 } | 555 } |
| 555 | 556 |
| 556 void AutocompletePopupViewMac::UserPressedOptIn(bool opt_in) { | 557 void AutocompletePopupViewMac::UserPressedOptIn(bool opt_in) { |
| 557 PromoCounter* counter = model_->profile()->GetInstantPromoCounter(); | 558 PromoCounter* counter = model_->profile()->GetInstantPromoCounter(); |
| 558 DCHECK(counter); | 559 DCHECK(counter); |
| 559 counter->Hide(); | 560 counter->Hide(); |
| 560 if (opt_in) { | 561 if (opt_in) { |
| 561 browser::ShowInstantConfirmDialogIfNecessary([field_ window], | 562 browser::ShowInstantConfirmDialogIfNecessary([field_ window], |
| 562 model_->profile()); | 563 model_->profile()); |
| 563 } | 564 } |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 826 bottomRightCornerRadius:kPopupRoundingRadius]; | 827 bottomRightCornerRadius:kPopupRoundingRadius]; |
| 827 | 828 |
| 828 // Draw the matrix clipped to our border. | 829 // Draw the matrix clipped to our border. |
| 829 [NSGraphicsContext saveGraphicsState]; | 830 [NSGraphicsContext saveGraphicsState]; |
| 830 [path addClip]; | 831 [path addClip]; |
| 831 [super drawRect:rect]; | 832 [super drawRect:rect]; |
| 832 [NSGraphicsContext restoreGraphicsState]; | 833 [NSGraphicsContext restoreGraphicsState]; |
| 833 } | 834 } |
| 834 | 835 |
| 835 @end | 836 @end |
| OLD | NEW |