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 #import "chrome/browser/cocoa/autocomplete_text_field_editor.h" | 5 #import "chrome/browser/cocoa/autocomplete_text_field_editor.h" |
6 | 6 |
7 #include "app/l10n_util_mac.h" | 7 #include "app/l10n_util_mac.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "grit/generated_resources.h" | 9 #include "grit/generated_resources.h" |
| 10 #include "base/sys_string_conversions.h" |
| 11 #import "chrome/browser/cocoa/autocomplete_text_field.h" |
10 | 12 |
11 @implementation AutocompleteTextFieldEditor | 13 @implementation AutocompleteTextFieldEditor |
12 | 14 |
13 - (void)copy:(id)sender { | 15 - (void)copy:(id)sender { |
14 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 16 NSPasteboard* pb = [NSPasteboard generalPasteboard]; |
15 [self performCopy:pb]; | 17 [self performCopy:pb]; |
16 } | 18 } |
17 | 19 |
18 - (void)cut:(id)sender { | 20 - (void)cut:(id)sender { |
19 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 21 NSPasteboard* pb = [NSPasteboard generalPasteboard]; |
20 [self performCut:pb]; | 22 [self performCut:pb]; |
21 } | 23 } |
22 | 24 |
23 - (void)performCopy:(NSPasteboard*)pb { | 25 - (void)performCopy:(NSPasteboard*)pb { |
24 [pb declareTypes:[NSArray array] owner:nil]; | 26 [pb declareTypes:[NSArray array] owner:nil]; |
25 [self writeSelectionToPasteboard:pb types: | 27 [self writeSelectionToPasteboard:pb types: |
26 [NSArray arrayWithObject:NSStringPboardType]]; | 28 [NSArray arrayWithObject:NSStringPboardType]]; |
27 } | 29 } |
28 | 30 |
29 - (void)performCut:(NSPasteboard*)pb { | 31 - (void)performCut:(NSPasteboard*)pb { |
30 [self performCopy:pb]; | 32 [self performCopy:pb]; |
31 [self delete:nil]; | 33 [self delete:nil]; |
32 } | 34 } |
33 | 35 |
34 - (BOOL)shouldPaste { | 36 // This class assumes that the delegate is an AutocompleteTextField. |
35 id delegate = [self delegate]; | 37 // Enforce that assumption. |
36 if (![delegate respondsToSelector:@selector(textShouldPaste:)] || | 38 - (void)setDelegate:(id)anObject { |
37 [delegate textShouldPaste:self]) { | 39 DCHECK(anObject == nil || |
38 return YES; | 40 [anObject isKindOfClass:[AutocompleteTextField class]]); |
39 } | 41 [super setDelegate:anObject]; |
40 return NO; | 42 } |
| 43 |
| 44 // Convenience method for retrieving the observer from the delegate. |
| 45 - (AutocompleteTextFieldObserver*)observer { |
| 46 DCHECK([[self delegate] isKindOfClass:[AutocompleteTextField class]]); |
| 47 return [static_cast<AutocompleteTextField*>([self delegate]) observer]; |
41 } | 48 } |
42 | 49 |
43 - (void)paste:(id)sender { | 50 - (void)paste:(id)sender { |
44 if ([self shouldPaste]) { | 51 AutocompleteTextFieldObserver* observer = [self observer]; |
45 [super paste:sender]; | 52 DCHECK(observer); |
| 53 if (observer) { |
| 54 observer->OnPaste(); |
46 } | 55 } |
47 } | 56 } |
48 | 57 |
49 - (void)pasteAndGo:sender { | 58 - (void)pasteAndGo:sender { |
50 id delegate = [self delegate]; | 59 id delegate = [self delegate]; |
51 if ([delegate respondsToSelector:@selector(textDidPasteAndGo:)]) | 60 if ([delegate respondsToSelector:@selector(textDidPasteAndGo:)]) |
52 [delegate textDidPasteAndGo:self]; | 61 [delegate textDidPasteAndGo:self]; |
53 } | 62 } |
54 | 63 |
55 // We have rich text, but it shouldn't be modified by the user, so | 64 // We have rich text, but it shouldn't be modified by the user, so |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 if (label) { | 96 if (label) { |
88 [menu insertItemWithTitle:label action:@selector(pasteAndGo:) | 97 [menu insertItemWithTitle:label action:@selector(pasteAndGo:) |
89 keyEquivalent:@"" atIndex:3]; | 98 keyEquivalent:@"" atIndex:3]; |
90 } | 99 } |
91 } | 100 } |
92 | 101 |
93 return menu; | 102 return menu; |
94 } | 103 } |
95 | 104 |
96 @end | 105 @end |
OLD | NEW |