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/clipboard/clipboard.h" | 9 #include "app/clipboard/clipboard.h" |
10 #include "app/clipboard/scoped_clipboard_writer.h" | 10 #include "app/clipboard/scoped_clipboard_writer.h" |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 } else { | 153 } else { |
154 NOTREACHED() | 154 NOTREACHED() |
155 << "Missing image for " << base::SysNSStringToUTF8(image_name); | 155 << "Missing image for " << base::SysNSStringToUTF8(image_name); |
156 } | 156 } |
157 } | 157 } |
158 | 158 |
159 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 159 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
160 return rb.GetNSImageNamed(resource_id); | 160 return rb.GetNSImageNamed(resource_id); |
161 } | 161 } |
162 | 162 |
163 // TODO(shess): AutocompletePopupViewMac doesn't really need an | |
164 // NSTextField. It wants to know where the position the popup, what | |
165 // font to use, and it also needs to be able to attach the popup to | |
166 // the window |field_| is in. | |
167 AutocompleteEditViewMac::AutocompleteEditViewMac( | 163 AutocompleteEditViewMac::AutocompleteEditViewMac( |
168 AutocompleteEditController* controller, | 164 AutocompleteEditController* controller, |
169 ToolbarModel* toolbar_model, | 165 ToolbarModel* toolbar_model, |
170 Profile* profile, | 166 Profile* profile, |
171 CommandUpdater* command_updater, | 167 CommandUpdater* command_updater, |
172 AutocompleteTextField* field) | 168 AutocompleteTextField* field) |
173 : model_(new AutocompleteEditModel(this, controller, profile)), | 169 : model_(new AutocompleteEditModel(this, controller, profile)), |
174 popup_view_(new AutocompletePopupViewMac(this, model_.get(), profile, | 170 popup_view_(new AutocompletePopupViewMac(this, model_.get(), profile, |
175 field)), | 171 field)), |
176 controller_(controller), | 172 controller_(controller), |
177 toolbar_model_(toolbar_model), | 173 toolbar_model_(toolbar_model), |
178 command_updater_(command_updater), | 174 command_updater_(command_updater), |
179 field_(field), | 175 field_(field), |
180 line_height_(0) { | 176 line_height_(0) { |
181 DCHECK(controller); | 177 DCHECK(controller); |
182 DCHECK(toolbar_model); | 178 DCHECK(toolbar_model); |
183 DCHECK(profile); | 179 DCHECK(profile); |
184 DCHECK(command_updater); | 180 DCHECK(command_updater); |
185 DCHECK(field); | 181 DCHECK(field); |
186 [field_ setObserver:this]; | 182 [field_ setObserver:this]; |
187 | 183 |
188 // Needed so that editing doesn't lose the styling. | 184 // Needed so that editing doesn't lose the styling. |
189 [field_ setAllowsEditingTextAttributes:YES]; | 185 [field_ setAllowsEditingTextAttributes:YES]; |
190 | 186 |
191 // Get the appropriate line height for the font that we use. | 187 // Get the appropriate line height for the font that we use. |
192 NSFont* font = ResourceBundle::GetSharedInstance().GetFont( | |
193 ResourceBundle::BaseFont).nativeFont(); | |
194 scoped_nsobject<NSLayoutManager> | 188 scoped_nsobject<NSLayoutManager> |
195 layoutManager([[NSLayoutManager alloc] init]); | 189 layoutManager([[NSLayoutManager alloc] init]); |
196 [layoutManager setUsesScreenFonts:YES]; | 190 [layoutManager setUsesScreenFonts:YES]; |
197 line_height_ = [layoutManager defaultLineHeightForFont:font]; | 191 line_height_ = [layoutManager defaultLineHeightForFont:GetFieldFont()]; |
198 DCHECK(line_height_ > 0); | 192 DCHECK(line_height_ > 0); |
199 } | 193 } |
200 | 194 |
201 AutocompleteEditViewMac::~AutocompleteEditViewMac() { | 195 AutocompleteEditViewMac::~AutocompleteEditViewMac() { |
202 // TODO(shess): Having to be aware of destructor ordering in this | |
203 // way seems brittle. There must be a better way. | |
204 | |
205 // Destroy popup view before this object in case it tries to call us | 196 // Destroy popup view before this object in case it tries to call us |
206 // back in the destructor. Likewise for destroying the model before | 197 // back in the destructor. Likewise for destroying the model before |
207 // this object. | 198 // this object. |
208 popup_view_.reset(); | 199 popup_view_.reset(); |
209 model_.reset(); | 200 model_.reset(); |
210 | 201 |
211 // Disconnect from |field_|, it outlives this object. | 202 // Disconnect from |field_|, it outlives this object. |
212 [field_ setObserver:NULL]; | 203 [field_ setObserver:NULL]; |
213 } | 204 } |
214 | 205 |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 ApplyTextAttributes(GetText(), storage); | 471 ApplyTextAttributes(GetText(), storage); |
481 | 472 |
482 [storage endEditing]; | 473 [storage endEditing]; |
483 } else { | 474 } else { |
484 SetText(GetText()); | 475 SetText(GetText()); |
485 } | 476 } |
486 } | 477 } |
487 | 478 |
488 void AutocompleteEditViewMac::ApplyTextAttributes( | 479 void AutocompleteEditViewMac::ApplyTextAttributes( |
489 const std::wstring& display_text, NSMutableAttributedString* as) { | 480 const std::wstring& display_text, NSMutableAttributedString* as) { |
490 NSFont* font = ResourceBundle::GetSharedInstance().GetFont( | 481 [as addAttribute:NSFontAttributeName value:GetFieldFont() |
491 ResourceBundle::BaseFont).nativeFont(); | |
492 [as addAttribute:NSFontAttributeName value:font | |
493 range:NSMakeRange(0, [as length])]; | 482 range:NSMakeRange(0, [as length])]; |
494 | 483 |
495 // Make a paragraph style locking in the standard line height as the maximum, | 484 // Make a paragraph style locking in the standard line height as the maximum, |
496 // otherwise the baseline may shift "downwards". | 485 // otherwise the baseline may shift "downwards". |
497 scoped_nsobject<NSMutableParagraphStyle> | 486 scoped_nsobject<NSMutableParagraphStyle> |
498 paragraph_style([[NSMutableParagraphStyle alloc] init]); | 487 paragraph_style([[NSMutableParagraphStyle alloc] init]); |
499 [paragraph_style setMaximumLineHeight:line_height_]; | 488 [paragraph_style setMaximumLineHeight:line_height_]; |
500 [as addAttribute:NSParagraphStyleAttributeName value:paragraph_style | 489 [as addAttribute:NSParagraphStyleAttributeName value:paragraph_style |
501 range:NSMakeRange(0, [as length])]; | 490 range:NSMakeRange(0, [as length])]; |
502 | 491 |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 | 668 |
680 if (cmd == @selector(scrollPageUp:)) { | 669 if (cmd == @selector(scrollPageUp:)) { |
681 model_->OnUpOrDownKeyPressed(-model_->result().size()); | 670 model_->OnUpOrDownKeyPressed(-model_->result().size()); |
682 return true; | 671 return true; |
683 } | 672 } |
684 | 673 |
685 if (cmd == @selector(cancelOperation:)) { | 674 if (cmd == @selector(cancelOperation:)) { |
686 return model_->OnEscapeKeyPressed(); | 675 return model_->OnEscapeKeyPressed(); |
687 } | 676 } |
688 | 677 |
689 if (cmd == @selector(insertTab:)) { | 678 if (cmd == @selector(insertTab:) || |
| 679 cmd == @selector(insertTabIgnoringFieldEditor:)) { |
690 if (model_->is_keyword_hint() && !model_->keyword().empty()) { | 680 if (model_->is_keyword_hint() && !model_->keyword().empty()) { |
691 model_->AcceptKeyword(); | 681 model_->AcceptKeyword(); |
692 return true; | 682 return true; |
693 } | 683 } |
694 } | 684 } |
695 | 685 |
696 // TODO(shess): Option-tab, would normally insert a literal tab | |
697 // character. Consider combining with -insertTab: | |
698 if (cmd == @selector(insertTabIgnoringFieldEditor:)) { | |
699 return true; | |
700 } | |
701 | |
702 // |-noop:| is sent when the user presses Cmd+Return. Override the no-op | 686 // |-noop:| is sent when the user presses Cmd+Return. Override the no-op |
703 // behavior with the proper WindowOpenDisposition. | 687 // behavior with the proper WindowOpenDisposition. |
704 NSEvent* event = [NSApp currentEvent]; | 688 NSEvent* event = [NSApp currentEvent]; |
705 if (cmd == @selector(insertNewline:) || | 689 if (cmd == @selector(insertNewline:) || |
706 (cmd == @selector(noop:) && [event keyCode] == kVK_Return)) { | 690 (cmd == @selector(noop:) && [event keyCode] == kVK_Return)) { |
707 WindowOpenDisposition disposition = | 691 WindowOpenDisposition disposition = |
708 event_utils::WindowOpenDispositionFromNSEvent(event); | 692 event_utils::WindowOpenDispositionFromNSEvent(event); |
709 model_->AcceptInput(disposition, false); | 693 model_->AcceptInput(disposition, false); |
710 // Opening a URL in a background tab should also revert the omnibox contents | 694 // Opening a URL in a background tab should also revert the omnibox contents |
711 // to their original state. We cannot do a blanket revert in OpenURL() | 695 // to their original state. We cannot do a blanket revert in OpenURL() |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
932 clipboard->ReadBookmark(NULL, &url_str); | 916 clipboard->ReadBookmark(NULL, &url_str); |
933 // pass resulting url string through GURL to normalize | 917 // pass resulting url string through GURL to normalize |
934 GURL url(url_str); | 918 GURL url(url_str); |
935 if (url.is_valid()) { | 919 if (url.is_valid()) { |
936 return UTF8ToWide(url.spec()); | 920 return UTF8ToWide(url.spec()); |
937 } | 921 } |
938 } | 922 } |
939 | 923 |
940 return std::wstring(); | 924 return std::wstring(); |
941 } | 925 } |
| 926 |
| 927 // static |
| 928 NSFont* AutocompleteEditViewMac::GetFieldFont() { |
| 929 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 930 return rb.GetFont(ResourceBundle::BaseFont).nativeFont(); |
| 931 } |
OLD | NEW |