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 "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" | 5 #include "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" |
6 | 6 |
7 #include <Carbon/Carbon.h> // kVK_Return | 7 #include <Carbon/Carbon.h> // kVK_Return |
8 | 8 |
9 #include "app/mac/nsimage_cache.h" | 9 #include "app/mac/nsimage_cache.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 void AutocompleteEditViewMac::ClosePopup() { | 437 void AutocompleteEditViewMac::ClosePopup() { |
438 if (popup_view_->GetModel()->IsOpen()) | 438 if (popup_view_->GetModel()->IsOpen()) |
439 controller_->OnAutocompleteWillClosePopup(); | 439 controller_->OnAutocompleteWillClosePopup(); |
440 | 440 |
441 popup_view_->GetModel()->StopAutocomplete(); | 441 popup_view_->GetModel()->StopAutocomplete(); |
442 } | 442 } |
443 | 443 |
444 void AutocompleteEditViewMac::SetFocus() { | 444 void AutocompleteEditViewMac::SetFocus() { |
445 } | 445 } |
446 | 446 |
447 bool AutocompleteEditViewMac::CommitSuggestText() { | |
448 if (suggest_text_length_ == 0) | |
449 return false; | |
450 | |
451 string16 input_text(GetText()); | |
452 suggest_text_length_ = 0; | |
453 string16 text(GetText()); | |
454 // Call SetText() to force a redraw and move the cursor to the end. | |
455 SetText(text); | |
456 model()->FinalizeInstantQuery(input_text, text.substr(input_text.size())); | |
457 return true; | |
458 } | |
459 | |
460 void AutocompleteEditViewMac::SetText(const string16& display_text) { | 447 void AutocompleteEditViewMac::SetText(const string16& display_text) { |
461 // If we are setting the text directly, there cannot be any suggest text. | 448 // If we are setting the text directly, there cannot be any suggest text. |
462 suggest_text_length_ = 0; | 449 suggest_text_length_ = 0; |
463 SetTextInternal(display_text); | 450 SetTextInternal(display_text); |
464 } | 451 } |
465 | 452 |
466 void AutocompleteEditViewMac::SetTextInternal( | 453 void AutocompleteEditViewMac::SetTextInternal( |
467 const string16& display_text) { | 454 const string16& display_text) { |
468 NSString* ss = base::SysUTF16ToNSString(display_text); | 455 NSString* ss = base::SysUTF16ToNSString(display_text); |
469 NSMutableAttributedString* as = | 456 NSMutableAttributedString* as = |
(...skipping 30 matching lines...) Expand all Loading... |
500 NSString* text = [field_ stringValue]; | 487 NSString* text = [field_ stringValue]; |
501 if (suggest_text_length_ > 0) { | 488 if (suggest_text_length_ > 0) { |
502 NSUInteger length = [text length]; | 489 NSUInteger length = [text length]; |
503 | 490 |
504 DCHECK_LE(suggest_text_length_, length); | 491 DCHECK_LE(suggest_text_length_, length); |
505 text = [text substringToIndex:(length - suggest_text_length_)]; | 492 text = [text substringToIndex:(length - suggest_text_length_)]; |
506 } | 493 } |
507 return text; | 494 return text; |
508 } | 495 } |
509 | 496 |
| 497 NSString* AutocompleteEditViewMac::GetSuggestTextSubstring() const { |
| 498 if (suggest_text_length_ == 0) |
| 499 return nil; |
| 500 |
| 501 NSString* text = [field_ stringValue]; |
| 502 NSUInteger length = [text length]; |
| 503 DCHECK_LE(suggest_text_length_, length); |
| 504 return [text substringFromIndex:(length - suggest_text_length_)]; |
| 505 } |
| 506 |
510 void AutocompleteEditViewMac::EmphasizeURLComponents() { | 507 void AutocompleteEditViewMac::EmphasizeURLComponents() { |
511 NSTextView* editor = (NSTextView*)[field_ currentEditor]; | 508 NSTextView* editor = (NSTextView*)[field_ currentEditor]; |
512 // If the autocomplete text field is in editing mode, then we can just change | 509 // If the autocomplete text field is in editing mode, then we can just change |
513 // its attributes through its editor. Otherwise, we simply reset its content. | 510 // its attributes through its editor. Otherwise, we simply reset its content. |
514 if (editor) { | 511 if (editor) { |
515 NSTextStorage* storage = [editor textStorage]; | 512 NSTextStorage* storage = [editor textStorage]; |
516 [storage beginEditing]; | 513 [storage beginEditing]; |
517 | 514 |
518 // Clear the existing attributes from the text storage, then | 515 // Clear the existing attributes from the text storage, then |
519 // overlay the appropriate Omnibox attributes. | 516 // overlay the appropriate Omnibox attributes. |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
716 if (needs_update) { | 713 if (needs_update) { |
717 NSRange current_range = GetSelectedRange(); | 714 NSRange current_range = GetSelectedRange(); |
718 SetTextInternal(base::SysNSStringToUTF16(text)); | 715 SetTextInternal(base::SysNSStringToUTF16(text)); |
719 if (NSMaxRange(current_range) <= [text length] - suggest_text_length_) | 716 if (NSMaxRange(current_range) <= [text length] - suggest_text_length_) |
720 SetSelectedRange(current_range); | 717 SetSelectedRange(current_range); |
721 else | 718 else |
722 SetSelectedRange(NSMakeRange([text length] - suggest_text_length_, 0)); | 719 SetSelectedRange(NSMakeRange([text length] - suggest_text_length_, 0)); |
723 } | 720 } |
724 } | 721 } |
725 | 722 |
| 723 string16 AutocompleteEditViewMac::GetInstantSuggestion() const { |
| 724 return suggest_text_length_ ? |
| 725 base::SysNSStringToUTF16(GetSuggestTextSubstring()) : string16(); |
| 726 } |
| 727 |
726 int AutocompleteEditViewMac::TextWidth() const { | 728 int AutocompleteEditViewMac::TextWidth() const { |
727 // Not used on mac. | 729 // Not used on mac. |
728 NOTREACHED(); | 730 NOTREACHED(); |
729 return 0; | 731 return 0; |
730 } | 732 } |
731 | 733 |
732 bool AutocompleteEditViewMac::IsImeComposing() const { | 734 bool AutocompleteEditViewMac::IsImeComposing() const { |
733 return [(NSTextView*)[field_ currentEditor] hasMarkedText]; | 735 return [(NSTextView*)[field_ currentEditor] hasMarkedText]; |
734 } | 736 } |
735 | 737 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 } | 785 } |
784 | 786 |
785 if (cmd == @selector(moveRight:)) { | 787 if (cmd == @selector(moveRight:)) { |
786 // Only commit suggested text if the cursor is all the way to the right and | 788 // Only commit suggested text if the cursor is all the way to the right and |
787 // there is no selection. | 789 // there is no selection. |
788 NSRange range = GetSelectedRange(); | 790 NSRange range = GetSelectedRange(); |
789 if (range.length == 0 && | 791 if (range.length == 0 && |
790 suggest_text_length_ > 0 && | 792 suggest_text_length_ > 0 && |
791 (range.location + suggest_text_length_ == | 793 (range.location + suggest_text_length_ == |
792 [[field_ stringValue] length])) { | 794 [[field_ stringValue] length])) { |
793 controller_->OnCommitSuggestedText(GetText()); | 795 controller_->OnCommitSuggestedText(true); |
794 return true; | 796 return true; |
795 } | 797 } |
796 } | 798 } |
797 | 799 |
798 if (cmd == @selector(scrollPageDown:)) { | 800 if (cmd == @selector(scrollPageDown:)) { |
799 model_->OnUpOrDownKeyPressed(model_->result().size()); | 801 model_->OnUpOrDownKeyPressed(model_->result().size()); |
800 return true; | 802 return true; |
801 } | 803 } |
802 | 804 |
803 if (cmd == @selector(scrollPageUp:)) { | 805 if (cmd == @selector(scrollPageUp:)) { |
804 model_->OnUpOrDownKeyPressed(-model_->result().size()); | 806 model_->OnUpOrDownKeyPressed(-model_->result().size()); |
805 return true; | 807 return true; |
806 } | 808 } |
807 | 809 |
808 if (cmd == @selector(cancelOperation:)) { | 810 if (cmd == @selector(cancelOperation:)) { |
809 return model_->OnEscapeKeyPressed(); | 811 return model_->OnEscapeKeyPressed(); |
810 } | 812 } |
811 | 813 |
812 if (cmd == @selector(insertTab:) || | 814 if (cmd == @selector(insertTab:) || |
813 cmd == @selector(insertTabIgnoringFieldEditor:)) { | 815 cmd == @selector(insertTabIgnoringFieldEditor:)) { |
814 if (model_->is_keyword_hint()) | 816 if (model_->is_keyword_hint()) |
815 return model_->AcceptKeyword(); | 817 return model_->AcceptKeyword(); |
816 | 818 |
817 if (suggest_text_length_ > 0) { | 819 if (suggest_text_length_ > 0) { |
818 controller_->OnCommitSuggestedText(GetText()); | 820 controller_->OnCommitSuggestedText(true); |
819 return true; | 821 return true; |
820 } | 822 } |
821 | 823 |
| 824 NSRange selected_range = GetSelectedRange(); |
| 825 NSUInteger length = [[[field_ currentEditor] string] length]; |
| 826 if (selected_range.length != 0 || selected_range.location < length) { |
| 827 [[field_ currentEditor] setSelectedRange:NSMakeRange(length, length)]; |
| 828 // OnDidChange() will not be triggered when setting selected range in this |
| 829 // method, so we need to call it explicitly. |
| 830 OnDidChange(); |
| 831 return true; |
| 832 } |
| 833 |
822 if (controller_->AcceptCurrentInstantPreview()) | 834 if (controller_->AcceptCurrentInstantPreview()) |
823 return true; | 835 return true; |
824 } | 836 } |
825 | 837 |
826 // |-noop:| is sent when the user presses Cmd+Return. Override the no-op | 838 // |-noop:| is sent when the user presses Cmd+Return. Override the no-op |
827 // behavior with the proper WindowOpenDisposition. | 839 // behavior with the proper WindowOpenDisposition. |
828 NSEvent* event = [NSApp currentEvent]; | 840 NSEvent* event = [NSApp currentEvent]; |
829 if (cmd == @selector(insertNewline:) || | 841 if (cmd == @selector(insertNewline:) || |
830 (cmd == @selector(noop:) && [event keyCode] == kVK_Return)) { | 842 (cmd == @selector(noop:) && [event keyCode] == kVK_Return)) { |
831 WindowOpenDisposition disposition = | 843 WindowOpenDisposition disposition = |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1087 } | 1099 } |
1088 | 1100 |
1089 return string16(); | 1101 return string16(); |
1090 } | 1102 } |
1091 | 1103 |
1092 // static | 1104 // static |
1093 NSFont* AutocompleteEditViewMac::GetFieldFont() { | 1105 NSFont* AutocompleteEditViewMac::GetFieldFont() { |
1094 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 1106 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
1095 return rb.GetFont(ResourceBundle::BaseFont).GetNativeFont(); | 1107 return rb.GetFont(ResourceBundle::BaseFont).GetNativeFont(); |
1096 } | 1108 } |
OLD | NEW |