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 "webkit/glue/webmenurunner_mac.h" | 5 #include "webkit/glue/webmenurunner_mac.h" |
6 | 6 |
7 #include "base/sys_string_conversions.h" | 7 #include "base/sys_string_conversions.h" |
8 | 8 |
| 9 @interface WebMenuRunner (PrivateAPI) |
| 10 |
| 11 // Worker function used during initialization. |
| 12 - (void)addItem:(const WebMenuItem&)item; |
| 13 |
| 14 // A callback for the menu controller object to call when an item is selected |
| 15 // from the menu. This is not called if the menu is dismissed without a |
| 16 // selection. |
| 17 - (void)menuItemSelected:(id)sender; |
| 18 |
| 19 @end // WebMenuRunner (PrivateAPI) |
| 20 |
9 @implementation WebMenuRunner | 21 @implementation WebMenuRunner |
10 | 22 |
11 - (id)initWithItems:(const std::vector<WebMenuItem>&)items { | 23 - (id)initWithItems:(const std::vector<WebMenuItem>&)items { |
12 if ((self = [super init])) { | 24 if ((self = [super init])) { |
13 menu_ = [[NSMenu alloc] initWithTitle:@""]; | 25 menu_.reset([[NSMenu alloc] initWithTitle:@""]); |
14 [menu_ setAutoenablesItems:NO]; | 26 [menu_ setAutoenablesItems:NO]; |
15 menuItemWasChosen_ = NO; | |
16 index_ = -1; | 27 index_ = -1; |
17 for (int i = 0; i < static_cast<int>(items.size()); ++i) | 28 for (int i = 0; i < static_cast<int>(items.size()); ++i) |
18 [self addItem:items[i]]; | 29 [self addItem:items[i]]; |
19 } | 30 } |
20 return self; | 31 return self; |
21 } | 32 } |
22 | 33 |
23 - (void)dealloc { | |
24 [menu_ release]; | |
25 [super dealloc]; | |
26 } | |
27 | |
28 - (void)addItem:(const WebMenuItem&)item { | 34 - (void)addItem:(const WebMenuItem&)item { |
29 if (item.type == WebMenuItem::SEPARATOR) { | 35 if (item.type == WebMenuItem::SEPARATOR) { |
30 [menu_ addItem:[NSMenuItem separatorItem]]; | 36 [menu_ addItem:[NSMenuItem separatorItem]]; |
31 return; | 37 return; |
32 } | 38 } |
33 | 39 |
34 NSString* title = base::SysUTF16ToNSString(item.label); | 40 NSString* title = base::SysUTF16ToNSString(item.label); |
35 NSMenuItem* menu_item = [menu_ addItemWithTitle:title | 41 NSMenuItem* menuItem = [menu_ addItemWithTitle:title |
36 action:@selector(menuItemSelected:) | 42 action:@selector(menuItemSelected:) |
37 keyEquivalent:@""]; | 43 keyEquivalent:@""]; |
38 [menu_item setEnabled:(item.enabled && item.type != WebMenuItem::GROUP)]; | 44 [menuItem setEnabled:(item.enabled && item.type != WebMenuItem::GROUP)]; |
39 [menu_item setTarget:self]; | 45 [menuItem setTarget:self]; |
40 } | 46 } |
41 | 47 |
42 // Reflects the result of the user's interaction with the popup menu. If NO, the | 48 // Reflects the result of the user's interaction with the popup menu. If NO, the |
43 // menu was dismissed without the user choosing an item, which can happen if the | 49 // menu was dismissed without the user choosing an item, which can happen if the |
44 // user clicked outside the menu region or hit the escape key. If YES, the user | 50 // user clicked outside the menu region or hit the escape key. If YES, the user |
45 // selected an item from the menu. | 51 // selected an item from the menu. |
46 - (BOOL)menuItemWasChosen { | 52 - (BOOL)menuItemWasChosen { |
47 return menuItemWasChosen_; | 53 return menuItemWasChosen_; |
48 } | 54 } |
49 | 55 |
(...skipping 19 matching lines...) Expand all Loading... |
69 if ([self menuItemWasChosen]) | 75 if ([self menuItemWasChosen]) |
70 index_ = [button indexOfSelectedItem]; | 76 index_ = [button indexOfSelectedItem]; |
71 } | 77 } |
72 | 78 |
73 - (int)indexOfSelectedItem { | 79 - (int)indexOfSelectedItem { |
74 return index_; | 80 return index_; |
75 } | 81 } |
76 | 82 |
77 @end // WebMenuRunner | 83 @end // WebMenuRunner |
78 | 84 |
| 85 namespace webkit_glue { |
| 86 |
79 // Helper function for manufacturing input events to send to WebKit. | 87 // Helper function for manufacturing input events to send to WebKit. |
80 NSEvent* CreateEventForMenuAction(BOOL item_chosen, int window_num, | 88 NSEvent* EventWithMenuAction(BOOL item_chosen, int window_num, |
81 int item_height, int selected_index, | 89 int item_height, int selected_index, |
82 NSRect menu_bounds, NSRect view_bounds) { | 90 NSRect menu_bounds, NSRect view_bounds) { |
83 NSEvent* event = nil; | 91 NSEvent* event = nil; |
84 double event_time = (double)(AbsoluteToDuration(UpTime())) / 1000.0; | 92 double event_time = (double)(AbsoluteToDuration(UpTime())) / 1000.0; |
85 | 93 |
86 if (item_chosen) { | 94 if (item_chosen) { |
87 // Construct a mouse up event to simulate the selection of an appropriate | 95 // Construct a mouse up event to simulate the selection of an appropriate |
88 // menu item. | 96 // menu item. |
89 NSPoint click_pos; | 97 NSPoint click_pos; |
90 click_pos.x = menu_bounds.size.width / 2; | 98 click_pos.x = menu_bounds.size.width / 2; |
91 | 99 |
92 // This is going to be hard to calculate since the button is painted by | 100 // This is going to be hard to calculate since the button is painted by |
(...skipping 28 matching lines...) Expand all Loading... |
121 windowNumber:window_num | 129 windowNumber:window_num |
122 context:nil | 130 context:nil |
123 characters:@"" | 131 characters:@"" |
124 charactersIgnoringModifiers:@"" | 132 charactersIgnoringModifiers:@"" |
125 isARepeat:NO | 133 isARepeat:NO |
126 keyCode:0x1B]; | 134 keyCode:0x1B]; |
127 } | 135 } |
128 | 136 |
129 return event; | 137 return event; |
130 } | 138 } |
| 139 |
| 140 } // namespace webkit_glue |
OLD | NEW |