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 |
11 const CGFloat kPopupXOffset = -10.0f; | 11 const CGFloat kPopupXOffset = -10.0f; |
12 BOOL gNewNSMenuAPI; | 12 BOOL gNewNSMenuAPI; |
13 | 13 |
14 } // namespace | 14 } // namespace |
15 | 15 |
16 #if !defined(MAC_OS_X_VERSION_10_6) || \ | 16 #if !defined(MAC_OS_X_VERSION_10_6) || \ |
17 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 | 17 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
18 @interface NSMenu (SnowLeopardSDKDeclarations) | 18 @interface NSMenu (SnowLeopardSDKDeclarations) |
19 - (BOOL)popUpMenuPositioningItem:(NSMenuItem *)item | 19 - (BOOL)popUpMenuPositioningItem:(NSMenuItem *)item |
20 atLocation:(NSPoint)location | 20 atLocation:(NSPoint)location |
21 inView:(NSView *)view; | 21 inView:(NSView *)view; |
22 - (void)setFont:(NSFont *)font; | 22 - (void)setFont:(NSFont *)font; |
23 @end | 23 @end |
24 #endif | 24 #endif |
25 | 25 |
26 @interface WebMenuRunner (PrivateAPI) | 26 @interface WebMenuRunner (PrivateAPI) |
27 | 27 |
28 // Worker function used during initialization. | 28 // Worker function used during initialization. |
29 - (void)addItem:(const WebMenuItem&)item; | 29 - (void)addItem:(const WebMenuItem&)item |
| 30 withAttributes:(NSDictionary*)attrs; |
30 | 31 |
31 // A callback for the menu controller object to call when an item is selected | 32 // A callback for the menu controller object to call when an item is selected |
32 // from the menu. This is not called if the menu is dismissed without a | 33 // from the menu. This is not called if the menu is dismissed without a |
33 // selection. | 34 // selection. |
34 - (void)menuItemSelected:(id)sender; | 35 - (void)menuItemSelected:(id)sender; |
35 | 36 |
36 @end // WebMenuRunner (PrivateAPI) | 37 @end // WebMenuRunner (PrivateAPI) |
37 | 38 |
38 @implementation WebMenuRunner | 39 @implementation WebMenuRunner |
39 | 40 |
40 - (id)initWithItems:(const std::vector<WebMenuItem>&)items | 41 - (id)initWithItems:(const std::vector<WebMenuItem>&)items |
41 fontSize:(CGFloat)fontSize { | 42 fontSize:(CGFloat)fontSize |
| 43 rightAligned:(BOOL)rightAligned { |
42 static BOOL newNSMenuAPIInitialized = NO; | 44 static BOOL newNSMenuAPIInitialized = NO; |
43 if (!newNSMenuAPIInitialized) { | 45 if (!newNSMenuAPIInitialized) { |
44 newNSMenuAPIInitialized = YES; | 46 newNSMenuAPIInitialized = YES; |
45 gNewNSMenuAPI = [NSMenu instancesRespondToSelector: | 47 gNewNSMenuAPI = [NSMenu instancesRespondToSelector: |
46 @selector(popUpMenuPositioningItem:atLocation:inView:)] && | 48 @selector(popUpMenuPositioningItem:atLocation:inView:)] && |
47 [NSMenu instancesRespondToSelector:@selector(setFont:)]; | 49 [NSMenu instancesRespondToSelector:@selector(setFont:)]; |
48 } | 50 } |
49 | 51 |
50 if ((self = [super init])) { | 52 if ((self = [super init])) { |
51 menu_.reset([[NSMenu alloc] initWithTitle:@""]); | 53 menu_.reset([[NSMenu alloc] initWithTitle:@""]); |
52 if (gNewNSMenuAPI) | 54 if (gNewNSMenuAPI) |
53 [menu_ setFont:[NSFont menuFontOfSize:fontSize]]; | 55 [menu_ setFont:[NSFont menuFontOfSize:fontSize]]; |
54 [menu_ setAutoenablesItems:NO]; | 56 [menu_ setAutoenablesItems:NO]; |
55 index_ = -1; | 57 index_ = -1; |
56 fontSize_ = fontSize; | 58 fontSize_ = fontSize; |
| 59 scoped_nsobject<NSDictionary> attrs; |
| 60 if (rightAligned) { |
| 61 // NB: Right-aligning menu items in this manner is known to not work in |
| 62 // Mac OS X 10.5. |
| 63 scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( |
| 64 [[NSMutableParagraphStyle alloc] init]); |
| 65 [paragraphStyle setAlignment:NSRightTextAlignment]; |
| 66 attrs.reset([[NSDictionary alloc] initWithObjectsAndKeys: |
| 67 paragraphStyle, NSParagraphStyleAttributeName, nil]); |
| 68 } |
57 for (size_t i = 0; i < items.size(); ++i) | 69 for (size_t i = 0; i < items.size(); ++i) |
58 [self addItem:items[i]]; | 70 [self addItem:items[i] withAttributes:attrs]; |
59 } | 71 } |
60 return self; | 72 return self; |
61 } | 73 } |
62 | 74 |
63 - (void)addItem:(const WebMenuItem&)item { | 75 - (void)addItem:(const WebMenuItem&)item |
| 76 withAttributes:(NSDictionary*)attrs { |
64 if (item.type == WebMenuItem::SEPARATOR) { | 77 if (item.type == WebMenuItem::SEPARATOR) { |
65 [menu_ addItem:[NSMenuItem separatorItem]]; | 78 [menu_ addItem:[NSMenuItem separatorItem]]; |
66 return; | 79 return; |
67 } | 80 } |
68 | 81 |
69 NSString* title = base::SysUTF16ToNSString(item.label); | 82 NSString* title = base::SysUTF16ToNSString(item.label); |
70 NSMenuItem* menuItem = [menu_ addItemWithTitle:title | 83 NSMenuItem* menuItem = [menu_ addItemWithTitle:title |
71 action:@selector(menuItemSelected:) | 84 action:@selector(menuItemSelected:) |
72 keyEquivalent:@""]; | 85 keyEquivalent:@""]; |
73 [menuItem setEnabled:(item.enabled && item.type != WebMenuItem::GROUP)]; | 86 [menuItem setEnabled:(item.enabled && item.type != WebMenuItem::GROUP)]; |
74 [menuItem setTarget:self]; | 87 [menuItem setTarget:self]; |
| 88 if (attrs) { |
| 89 scoped_nsobject<NSAttributedString> attrTitle( |
| 90 [[NSAttributedString alloc] initWithString:title |
| 91 attributes:attrs]); |
| 92 [menuItem setAttributedTitle:attrTitle]; |
| 93 } |
75 if (gNewNSMenuAPI) | 94 if (gNewNSMenuAPI) |
76 [menuItem setTag:[menu_ numberOfItems] - 1]; | 95 [menuItem setTag:[menu_ numberOfItems] - 1]; |
77 } | 96 } |
78 | 97 |
79 // Reflects the result of the user's interaction with the popup menu. If NO, the | 98 // Reflects the result of the user's interaction with the popup menu. If NO, the |
80 // menu was dismissed without the user choosing an item, which can happen if the | 99 // menu was dismissed without the user choosing an item, which can happen if the |
81 // user clicked outside the menu region or hit the escape key. If YES, the user | 100 // user clicked outside the menu region or hit the escape key. If YES, the user |
82 // selected an item from the menu. | 101 // selected an item from the menu. |
83 - (BOOL)menuItemWasChosen { | 102 - (BOOL)menuItemWasChosen { |
84 return menuItemWasChosen_; | 103 return menuItemWasChosen_; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 characters:@"" | 195 characters:@"" |
177 charactersIgnoringModifiers:escape_str | 196 charactersIgnoringModifiers:escape_str |
178 isARepeat:NO | 197 isARepeat:NO |
179 keyCode:0x1B]; | 198 keyCode:0x1B]; |
180 } | 199 } |
181 | 200 |
182 return event; | 201 return event; |
183 } | 202 } |
184 | 203 |
185 } // namespace webkit_glue | 204 } // namespace webkit_glue |
OLD | NEW |