OLD | NEW |
1 // Copyright (c) 2009 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 #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" | 10 #include "base/sys_string_conversions.h" |
11 #include "chrome/app/chrome_dll_resource.h" // IDC_* | 11 #include "chrome/app/chrome_dll_resource.h" // IDC_* |
12 #include "chrome/browser/browser_list.h" | 12 #include "chrome/browser/browser_list.h" |
13 #import "chrome/browser/cocoa/autocomplete_text_field.h" | 13 #import "chrome/browser/cocoa/autocomplete_text_field.h" |
14 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h" | 14 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h" |
15 #import "chrome/browser/cocoa/browser_window_controller.h" | 15 #import "chrome/browser/cocoa/browser_window_controller.h" |
16 #import "chrome/browser/cocoa/extensions/extension_action_context_menu.h" | 16 #import "chrome/browser/cocoa/extensions/extension_action_context_menu.h" |
17 #import "chrome/browser/cocoa/toolbar_controller.h" | 17 #import "chrome/browser/cocoa/toolbar_controller.h" |
18 #include "chrome/browser/extensions/extensions_service.h" | 18 #include "chrome/browser/extensions/extensions_service.h" |
19 #include "chrome/common/extensions/extension_action.h" | 19 #include "chrome/common/extensions/extension_action.h" |
20 #include "chrome/browser/profile.h" | 20 #include "chrome/browser/profile.h" |
21 | 21 |
22 class Extension; | 22 class Extension; |
23 | 23 |
24 @interface AutocompleteTextFieldEditor(Private) | 24 @interface AutocompleteTextFieldEditor(Private) |
25 // Returns the default context menu to be displayed on a right mouse click. | 25 // Returns the default context menu to be displayed on a right mouse click. |
26 - (NSMenu*)defaultMenuForEvent:(NSEvent*)event; | 26 - (NSMenu*)defaultMenuForEvent:(NSEvent*)event; |
27 @end | 27 @end |
28 | 28 |
29 @implementation AutocompleteTextFieldEditor | 29 @implementation AutocompleteTextFieldEditor |
30 | 30 |
| 31 @synthesize profile = profile_; |
| 32 |
31 - (id)initWithFrame:(NSRect)frameRect { | 33 - (id)initWithFrame:(NSRect)frameRect { |
32 if ((self = [super initWithFrame:frameRect])) | 34 if ((self = [super initWithFrame:frameRect])) |
33 dropHandler_.reset([[URLDropTargetHandler alloc] initWithView:self]); | 35 dropHandler_.reset([[URLDropTargetHandler alloc] initWithView:self]); |
34 return self; | 36 return self; |
35 } | 37 } |
36 | 38 |
37 - (void)copy:(id)sender { | 39 - (void)copy:(id)sender { |
38 AutocompleteTextFieldObserver* observer = [self observer]; | 40 AutocompleteTextFieldObserver* observer = [self observer]; |
39 DCHECK(observer); | 41 DCHECK(observer); |
40 if (observer) { | 42 if (observer) |
41 observer->OnCopy(); | 43 observer->OnCopy(); |
42 } | |
43 } | 44 } |
44 | 45 |
45 - (void)cut:(id)sender { | 46 - (void)cut:(id)sender { |
46 [self copy:sender]; | 47 [self copy:sender]; |
47 [self delete:nil]; | 48 [self delete:nil]; |
48 } | 49 } |
49 | 50 |
50 // This class assumes that the delegate is an AutocompleteTextField. | 51 // This class assumes that the delegate is an AutocompleteTextField. |
51 // Enforce that assumption. | 52 // Enforce that assumption. |
52 - (void)setDelegate:(id)anObject { | 53 - (void)setDelegate:(id)anObject { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 91 |
91 - (NSMenu*)menuForEvent:(NSEvent*)event { | 92 - (NSMenu*)menuForEvent:(NSEvent*)event { |
92 NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil]; | 93 NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil]; |
93 | 94 |
94 // Was the right click within a Page Action? Show a different menu if so. | 95 // Was the right click within a Page Action? Show a different menu if so. |
95 AutocompleteTextField* field = (AutocompleteTextField*)[self delegate]; | 96 AutocompleteTextField* field = (AutocompleteTextField*)[self delegate]; |
96 NSRect bounds([field bounds]); | 97 NSRect bounds([field bounds]); |
97 AutocompleteTextFieldCell* cell = [field autocompleteTextFieldCell]; | 98 AutocompleteTextFieldCell* cell = [field autocompleteTextFieldCell]; |
98 const size_t pageActionCount = [cell pageActionCount]; | 99 const size_t pageActionCount = [cell pageActionCount]; |
99 BOOL flipped = [self isFlipped]; | 100 BOOL flipped = [self isFlipped]; |
100 Browser* browser = BrowserList::GetLastActive(); | 101 if (!profile_) |
101 // GetLastActive() returns NULL during testing. | |
102 if (!browser) | |
103 return [self defaultMenuForEvent:event]; | 102 return [self defaultMenuForEvent:event]; |
104 ExtensionsService* service = browser->profile()->GetExtensionsService(); | 103 |
| 104 ExtensionsService* service = profile_->GetExtensionsService(); |
| 105 if (!service) |
| 106 return [self defaultMenuForEvent:event]; |
| 107 |
105 for (size_t i = 0; i < pageActionCount; ++i) { | 108 for (size_t i = 0; i < pageActionCount; ++i) { |
106 NSRect pageActionFrame = [cell pageActionFrameForIndex:i inFrame:bounds]; | 109 NSRect pageActionFrame = [cell pageActionFrameForIndex:i inFrame:bounds]; |
107 if (NSMouseInRect(location, pageActionFrame, flipped)) { | 110 if (NSMouseInRect(location, pageActionFrame, flipped)) { |
108 Extension* extension = service->GetExtensionById( | 111 Extension* extension = service->GetExtensionById( |
109 [cell pageActionForIndex:i]->extension_id(), false); | 112 [cell pageActionForIndex:i]->extension_id(), false); |
110 DCHECK(extension); | 113 DCHECK(extension); |
111 if (!extension) | 114 if (!extension) |
112 break; | 115 break; |
113 return [[[ExtensionActionContextMenu alloc] initWithExtension:extension] | 116 return [[[ExtensionActionContextMenu alloc] |
114 autorelease]; | 117 initWithExtension:extension profile:profile_] autorelease]; |
115 } | 118 } |
116 } | 119 } |
117 | 120 |
118 // Otherwise, simply return the default menu for this instance. | 121 // Otherwise, simply return the default menu for this instance. |
119 return [self defaultMenuForEvent:event]; | 122 return [self defaultMenuForEvent:event]; |
120 } | 123 } |
121 | 124 |
122 - (NSMenu*)defaultMenuForEvent:(NSEvent*)event { | 125 - (NSMenu*)defaultMenuForEvent:(NSEvent*)event { |
123 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"TITLE"] autorelease]; | 126 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"TITLE"] autorelease]; |
124 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_CUT) | 127 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_CUT) |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 - (void)draggingExited:(id<NSDraggingInfo>)sender { | 192 - (void)draggingExited:(id<NSDraggingInfo>)sender { |
190 return [dropHandler_ draggingExited:sender]; | 193 return [dropHandler_ draggingExited:sender]; |
191 } | 194 } |
192 | 195 |
193 // (URLDropTarget protocol) | 196 // (URLDropTarget protocol) |
194 - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender { | 197 - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender { |
195 return [dropHandler_ performDragOperation:sender]; | 198 return [dropHandler_ performDragOperation:sender]; |
196 } | 199 } |
197 | 200 |
198 @end | 201 @end |
OLD | NEW |