| 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 namespace { | 9 namespace { |
| 10 | 10 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 action:@selector(menuItemSelected:) | 84 action:@selector(menuItemSelected:) |
| 85 keyEquivalent:@""]; | 85 keyEquivalent:@""]; |
| 86 [menuItem setEnabled:(item.enabled && item.type != WebMenuItem::GROUP)]; | 86 [menuItem setEnabled:(item.enabled && item.type != WebMenuItem::GROUP)]; |
| 87 [menuItem setTarget:self]; | 87 [menuItem setTarget:self]; |
| 88 if (attrs) { | 88 if (attrs) { |
| 89 scoped_nsobject<NSAttributedString> attrTitle( | 89 scoped_nsobject<NSAttributedString> attrTitle( |
| 90 [[NSAttributedString alloc] initWithString:title | 90 [[NSAttributedString alloc] initWithString:title |
| 91 attributes:attrs]); | 91 attributes:attrs]); |
| 92 [menuItem setAttributedTitle:attrTitle]; | 92 [menuItem setAttributedTitle:attrTitle]; |
| 93 } | 93 } |
| 94 if (gNewNSMenuAPI) | 94 [menuItem setTag:[menu_ numberOfItems] - 1]; |
| 95 [menuItem setTag:[menu_ numberOfItems] - 1]; | |
| 96 } | 95 } |
| 97 | 96 |
| 98 // Reflects the result of the user's interaction with the popup menu. If NO, the | 97 // Reflects the result of the user's interaction with the popup menu. If NO, the |
| 99 // menu was dismissed without the user choosing an item, which can happen if the | 98 // menu was dismissed without the user choosing an item, which can happen if the |
| 100 // user clicked outside the menu region or hit the escape key. If YES, the user | 99 // user clicked outside the menu region or hit the escape key. If YES, the user |
| 101 // selected an item from the menu. | 100 // selected an item from the menu. |
| 102 - (BOOL)menuItemWasChosen { | 101 - (BOOL)menuItemWasChosen { |
| 103 return menuItemWasChosen_; | 102 return menuItemWasChosen_; |
| 104 } | 103 } |
| 105 | 104 |
| 106 - (void)menuItemSelected:(id)sender { | 105 - (void)menuItemSelected:(id)sender { |
| 107 menuItemWasChosen_ = YES; | 106 menuItemWasChosen_ = YES; |
| 108 if (gNewNSMenuAPI) | 107 if (gNewNSMenuAPI) |
| 109 index_ = [sender tag]; | 108 index_ = [sender tag]; |
| 110 } | 109 } |
| 111 | 110 |
| 112 - (void)runMenuInView:(NSView*)view | 111 - (void)runMenuInView:(NSView*)view |
| 113 withBounds:(NSRect)bounds | 112 withBounds:(NSRect)bounds |
| 114 initialIndex:(int)index { | 113 initialIndex:(int)index { |
| 115 if (gNewNSMenuAPI) { | 114 if (gNewNSMenuAPI) { |
| 116 NSMenuItem* selectedItem = [menu_ itemAtIndex:index]; | 115 // index might be out of bounds, in which case we set no selection. |
| 117 [selectedItem setState:NSOnState]; | 116 NSMenuItem* selectedItem = [menu_ itemWithTag:index]; |
| 117 if (selectedItem) { |
| 118 [selectedItem setState:NSOnState]; |
| 119 } else { |
| 120 selectedItem = [menu_ itemWithTag:0]; |
| 121 } |
| 118 NSPoint anchor = NSMakePoint(NSMinX(bounds) + kPopupXOffset, | 122 NSPoint anchor = NSMakePoint(NSMinX(bounds) + kPopupXOffset, |
| 119 NSMaxY(bounds)); | 123 NSMaxY(bounds)); |
| 120 [menu_ popUpMenuPositioningItem:selectedItem | 124 [menu_ popUpMenuPositioningItem:selectedItem |
| 121 atLocation:anchor | 125 atLocation:anchor |
| 122 inView:view]; | 126 inView:view]; |
| 123 } else { | 127 } else { |
| 124 // Set up the button cell, converting to NSView coordinates. The menu is | 128 // Set up the button cell, converting to NSView coordinates. The menu is |
| 125 // positioned such that the currently selected menu item appears over the | 129 // positioned such that the currently selected menu item appears over the |
| 126 // popup button, which is the expected Mac popup menu behavior. | 130 // popup button, which is the expected Mac popup menu behavior. |
| 127 NSPopUpButtonCell* button = [[NSPopUpButtonCell alloc] initTextCell:@"" | 131 NSPopUpButtonCell* button = [[NSPopUpButtonCell alloc] initTextCell:@"" |
| 128 pullsDown:NO]; | 132 pullsDown:NO]; |
| 129 [button autorelease]; | 133 [button autorelease]; |
| 130 [button setMenu:menu_]; | 134 [button setMenu:menu_]; |
| 131 [button selectItemAtIndex:index]; | 135 // We use selectItemWithTag below so if the index is out-of-bounds nothing |
| 136 // bad happens. |
| 137 [button selectItemWithTag:index]; |
| 132 [button setFont:[NSFont menuFontOfSize:fontSize_]]; | 138 [button setFont:[NSFont menuFontOfSize:fontSize_]]; |
| 133 | 139 |
| 134 // Create a dummy view to associate the popup with, since the OS will use | 140 // Create a dummy view to associate the popup with, since the OS will use |
| 135 // that view for positioning the menu. | 141 // that view for positioning the menu. |
| 136 NSView* dummyView = [[[NSView alloc] initWithFrame:bounds] autorelease]; | 142 NSView* dummyView = [[[NSView alloc] initWithFrame:bounds] autorelease]; |
| 137 [view addSubview:dummyView]; | 143 [view addSubview:dummyView]; |
| 138 NSRect dummyBounds = [dummyView convertRect:bounds fromView:view]; | 144 NSRect dummyBounds = [dummyView convertRect:bounds fromView:view]; |
| 139 | 145 |
| 140 // Display the menu, and set a flag if a menu item was chosen. | 146 // Display the menu, and set a flag if a menu item was chosen. |
| 141 [button performClickWithFrame:dummyBounds inView:dummyView]; | 147 [button performClickWithFrame:dummyBounds inView:dummyView]; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 characters:@"" | 209 characters:@"" |
| 204 charactersIgnoringModifiers:escape_str | 210 charactersIgnoringModifiers:escape_str |
| 205 isARepeat:NO | 211 isARepeat:NO |
| 206 keyCode:0x1B]; | 212 keyCode:0x1B]; |
| 207 } | 213 } |
| 208 | 214 |
| 209 return event; | 215 return event; |
| 210 } | 216 } |
| 211 | 217 |
| 212 } // namespace webkit_glue | 218 } // namespace webkit_glue |
| OLD | NEW |