| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 #include "app/gfx/font.h" | 8 #include "app/gfx/font.h" |
| 9 #include "app/l10n_util_mac.h" | 9 #include "app/l10n_util_mac.h" |
| 10 #include "app/resource_bundle.h" | 10 #include "app/resource_bundle.h" |
| 11 #include "base/clipboard.h" | 11 #include "base/clipboard.h" |
| 12 #import "base/cocoa_protocols_mac.h" | 12 #import "base/cocoa_protocols_mac.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/sys_string_conversions.h" | 14 #include "base/sys_string_conversions.h" |
| 15 #include "chrome/browser/autocomplete/autocomplete_edit.h" | 15 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
| 16 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" | 16 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" |
| 17 #include "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" | 17 #include "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" |
| 18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/cocoa/autocomplete_text_field.h" | |
| 20 #include "chrome/browser/cocoa/event_utils.h" | 19 #include "chrome/browser/cocoa/event_utils.h" |
| 21 #include "chrome/browser/tab_contents/tab_contents.h" | 20 #include "chrome/browser/tab_contents/tab_contents.h" |
| 22 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
| 23 | 22 |
| 24 // Focus-handling between |field_| and |model_| is a bit subtle. | 23 // Focus-handling between |field_| and |model_| is a bit subtle. |
| 25 // Other platforms detect change of focus, which is inconvenient | 24 // Other platforms detect change of focus, which is inconvenient |
| 26 // without subclassing NSTextField (even with a subclass, the use of a | 25 // without subclassing NSTextField (even with a subclass, the use of a |
| 27 // field editor may complicate things). | 26 // field editor may complicate things). |
| 28 // | 27 // |
| 29 // |model_| doesn't actually do anything when it gains focus, it just | 28 // |model_| doesn't actually do anything when it gains focus, it just |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 return NSMakeRange(static_cast<NSInteger>(component.begin), | 120 return NSMakeRange(static_cast<NSInteger>(component.begin), |
| 122 static_cast<NSInteger>(component.len)); | 121 static_cast<NSInteger>(component.len)); |
| 123 } | 122 } |
| 124 | 123 |
| 125 } // namespace | 124 } // namespace |
| 126 | 125 |
| 127 // Thin Obj-C bridge class that is the delegate of the omnibox field. | 126 // Thin Obj-C bridge class that is the delegate of the omnibox field. |
| 128 // It intercepts various control delegate methods and vectors them to | 127 // It intercepts various control delegate methods and vectors them to |
| 129 // the edit view. | 128 // the edit view. |
| 130 | 129 |
| 130 // TODO(shess): Consider moving more of this code off to |
| 131 // AutocompleteTextFieldObserver. |
| 132 |
| 131 @interface AutocompleteFieldDelegate : NSObject<NSTextFieldDelegate> { | 133 @interface AutocompleteFieldDelegate : NSObject<NSTextFieldDelegate> { |
| 132 @private | 134 @private |
| 133 AutocompleteEditViewMac* edit_view_; // weak, owns us. | 135 AutocompleteEditViewMac* edit_view_; // weak, owns us. |
| 134 } | 136 } |
| 135 - initWithEditView:(AutocompleteEditViewMac*)view; | 137 - initWithEditView:(AutocompleteEditViewMac*)view; |
| 136 - (void)windowDidResignKey:(NSNotification*)notification; | 138 - (void)windowDidResignKey:(NSNotification*)notification; |
| 137 @end | 139 @end |
| 138 | 140 |
| 139 // TODO(shess): AutocompletePopupViewMac doesn't really need an | 141 // TODO(shess): AutocompletePopupViewMac doesn't really need an |
| 140 // NSTextField. It wants to know where the position the popup, what | 142 // NSTextField. It wants to know where the position the popup, what |
| (...skipping 13 matching lines...) Expand all Loading... |
| 154 toolbar_model_(toolbar_model), | 156 toolbar_model_(toolbar_model), |
| 155 command_updater_(command_updater), | 157 command_updater_(command_updater), |
| 156 field_(field), | 158 field_(field), |
| 157 edit_helper_([[AutocompleteFieldDelegate alloc] initWithEditView:this]) { | 159 edit_helper_([[AutocompleteFieldDelegate alloc] initWithEditView:this]) { |
| 158 DCHECK(controller); | 160 DCHECK(controller); |
| 159 DCHECK(toolbar_model); | 161 DCHECK(toolbar_model); |
| 160 DCHECK(profile); | 162 DCHECK(profile); |
| 161 DCHECK(command_updater); | 163 DCHECK(command_updater); |
| 162 DCHECK(field); | 164 DCHECK(field); |
| 163 [field_ setDelegate:edit_helper_.get()]; | 165 [field_ setDelegate:edit_helper_.get()]; |
| 166 [field_ setObserver:this]; |
| 164 | 167 |
| 165 // Needed so that editing doesn't lose the styling. | 168 // Needed so that editing doesn't lose the styling. |
| 166 [field_ setAllowsEditingTextAttributes:YES]; | 169 [field_ setAllowsEditingTextAttributes:YES]; |
| 167 | 170 |
| 168 // Track the window's key status for signalling focus changes to | 171 // Track the window's key status for signalling focus changes to |
| 169 // |model_|. | 172 // |model_|. |
| 170 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; | 173 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; |
| 171 [nc addObserver:edit_helper_ | 174 [nc addObserver:edit_helper_ |
| 172 selector:@selector(windowDidResignKey:) | 175 selector:@selector(windowDidResignKey:) |
| 173 name:NSWindowDidResignKeyNotification | 176 name:NSWindowDidResignKeyNotification |
| 174 object:[field_ window]]; | 177 object:[field_ window]]; |
| 175 } | 178 } |
| 176 | 179 |
| 177 AutocompleteEditViewMac::~AutocompleteEditViewMac() { | 180 AutocompleteEditViewMac::~AutocompleteEditViewMac() { |
| 178 // TODO(shess): Having to be aware of destructor ordering in this | 181 // TODO(shess): Having to be aware of destructor ordering in this |
| 179 // way seems brittle. There must be a better way. | 182 // way seems brittle. There must be a better way. |
| 180 | 183 |
| 181 // Destroy popup view before this object in case it tries to call us | 184 // Destroy popup view before this object in case it tries to call us |
| 182 // back in the destructor. Likewise for destroying the model before | 185 // back in the destructor. Likewise for destroying the model before |
| 183 // this object. | 186 // this object. |
| 184 popup_view_.reset(); | 187 popup_view_.reset(); |
| 185 model_.reset(); | 188 model_.reset(); |
| 186 | 189 |
| 187 // Disconnect field_ from edit_helper_ so that we don't get calls | 190 // Disconnect field_ from edit_helper_ so that we don't get calls |
| 188 // after destruction. | 191 // after destruction. |
| 189 [field_ setDelegate:nil]; | 192 [field_ setDelegate:nil]; |
| 193 [field_ setObserver:NULL]; |
| 190 } | 194 } |
| 191 | 195 |
| 192 void AutocompleteEditViewMac::SaveStateToTab(TabContents* tab) { | 196 void AutocompleteEditViewMac::SaveStateToTab(TabContents* tab) { |
| 193 DCHECK(tab); | 197 DCHECK(tab); |
| 194 | 198 |
| 195 const bool hasFocus = [field_ currentEditor] ? true : false; | 199 const bool hasFocus = [field_ currentEditor] ? true : false; |
| 196 | 200 |
| 197 NSRange range; | 201 NSRange range; |
| 198 if (hasFocus) { | 202 if (hasFocus) { |
| 199 range = GetSelectedRange(); | 203 range = GetSelectedRange(); |
| (...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 851 | 855 |
| 852 - (BOOL)control:(NSControl*)control textShouldEndEditing:(NSText*)fieldEditor { | 856 - (BOOL)control:(NSControl*)control textShouldEndEditing:(NSText*)fieldEditor { |
| 853 edit_view_->OnDidEndEditing(); | 857 edit_view_->OnDidEndEditing(); |
| 854 | 858 |
| 855 return YES; | 859 return YES; |
| 856 | 860 |
| 857 // TODO(shess): Figure out where the selection belongs. On GTK, | 861 // TODO(shess): Figure out where the selection belongs. On GTK, |
| 858 // it's set to the start of the text. | 862 // it's set to the start of the text. |
| 859 } | 863 } |
| 860 | 864 |
| 861 // AutocompleteTextField/Editor adds a delegate method which allows us | |
| 862 // to intercept and handle -paste: calls. | |
| 863 - (BOOL)control:(NSControl*)control textShouldPaste:(NSText*)fieldEditor { | |
| 864 edit_view_->OnPaste(); | |
| 865 | |
| 866 // Caller shouldn't also paste. | |
| 867 return NO; | |
| 868 } | |
| 869 | |
| 870 - (NSString*)control:(NSControl*)control | 865 - (NSString*)control:(NSControl*)control |
| 871 textPasteActionString:(NSText*)fieldEditor { | 866 textPasteActionString:(NSText*)fieldEditor { |
| 872 if (!edit_view_->CanPasteAndGo()) | 867 if (!edit_view_->CanPasteAndGo()) |
| 873 return nil; | 868 return nil; |
| 874 | 869 |
| 875 return l10n_util::GetNSStringWithFixup(edit_view_->GetPasteActionStringId()); | 870 return l10n_util::GetNSStringWithFixup(edit_view_->GetPasteActionStringId()); |
| 876 } | 871 } |
| 877 | 872 |
| 878 - (void)control:(NSControl*)control textDidPasteAndGo:(NSText*)fieldEditor { | 873 - (void)control:(NSControl*)control textDidPasteAndGo:(NSText*)fieldEditor { |
| 879 edit_view_->OnPasteAndGo(); | 874 edit_view_->OnPasteAndGo(); |
| 880 } | 875 } |
| 881 | 876 |
| 882 // Signal that we've lost focus when the window resigns key. | 877 // Signal that we've lost focus when the window resigns key. |
| 883 - (void)windowDidResignKey:(NSNotification*)notification { | 878 - (void)windowDidResignKey:(NSNotification*)notification { |
| 884 edit_view_->OnDidResignKey(); | 879 edit_view_->OnDidResignKey(); |
| 885 } | 880 } |
| 886 | 881 |
| 887 // AutocompleteTextField adds a delegate method which allows us to | |
| 888 // track -flagsChanged: calls. | |
| 889 // | |
| 890 // When the user types Control-Enter, the existing content has "www." | |
| 891 // prepended and ".com" appended. This calls down to | |
| 892 // AutocompleteEditModel::OnControlKeyChanged() so that it can change | |
| 893 // the popup to reflect this. See autocomplete_edit.cc | |
| 894 // OnControlKeyChanged() and OnAfterPossibleChange(). | |
| 895 - (void)control:(NSControl*)control flagsChanged:(NSEvent*)theEvent { | |
| 896 bool controlFlag = ([theEvent modifierFlags]&NSControlKeyMask) != 0; | |
| 897 edit_view_->OnControlKeyChanged(controlFlag); | |
| 898 } | |
| 899 | |
| 900 @end | 882 @end |
| OLD | NEW |