Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/renderer_host/webmenurunner_mac.h" | 5 #include "content/browser/renderer_host/webmenurunner_mac.h" |
| 6 | 6 |
| 7 #include "base/strings/sys_string_conversions.h" | 7 #include "base/strings/sys_string_conversions.h" |
| 8 | 8 |
| 9 @interface WebMenuRunner (PrivateAPI) | 9 @interface WebMenuRunner (PrivateAPI) |
| 10 | 10 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 keyEquivalent:@""]; | 47 keyEquivalent:@""]; |
| 48 if (!item.toolTip.empty()) { | 48 if (!item.toolTip.empty()) { |
| 49 NSString* toolTip = base::SysUTF16ToNSString(item.toolTip); | 49 NSString* toolTip = base::SysUTF16ToNSString(item.toolTip); |
| 50 [menuItem setToolTip:toolTip]; | 50 [menuItem setToolTip:toolTip]; |
| 51 } | 51 } |
| 52 [menuItem setEnabled:(item.enabled && item.type != WebMenuItem::GROUP)]; | 52 [menuItem setEnabled:(item.enabled && item.type != WebMenuItem::GROUP)]; |
| 53 [menuItem setTarget:self]; | 53 [menuItem setTarget:self]; |
| 54 | 54 |
| 55 // Set various alignment/language attributes. Note that many (if not most) of | 55 // Set various alignment/language attributes. Note that many (if not most) of |
| 56 // these attributes are functional only on 10.6 and above. | 56 // these attributes are functional only on 10.6 and above. |
| 57 scoped_nsobject<NSMutableDictionary> attrs( | 57 base::scoped_nsobject<NSMutableDictionary> attrs( |
| 58 [[NSMutableDictionary alloc] initWithCapacity:3]); | 58 [[NSMutableDictionary alloc] initWithCapacity:3]); |
| 59 scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( | 59 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( |
| 60 [[NSMutableParagraphStyle alloc] init]); | 60 [[NSMutableParagraphStyle alloc] init]); |
| 61 [paragraphStyle setAlignment:rightAligned_ ? NSRightTextAlignment | 61 [paragraphStyle setAlignment:rightAligned_ ? NSRightTextAlignment |
| 62 : NSLeftTextAlignment]; | 62 : NSLeftTextAlignment]; |
| 63 NSWritingDirection writingDirection = | 63 NSWritingDirection writingDirection = |
| 64 item.rtl ? NSWritingDirectionRightToLeft | 64 item.rtl ? NSWritingDirectionRightToLeft |
| 65 : NSWritingDirectionLeftToRight; | 65 : NSWritingDirectionLeftToRight; |
| 66 [paragraphStyle setBaseWritingDirection:writingDirection]; | 66 [paragraphStyle setBaseWritingDirection:writingDirection]; |
| 67 [attrs setObject:paragraphStyle forKey:NSParagraphStyleAttributeName]; | 67 [attrs setObject:paragraphStyle forKey:NSParagraphStyleAttributeName]; |
| 68 | 68 |
| 69 if (item.has_directional_override) { | 69 if (item.has_directional_override) { |
| 70 scoped_nsobject<NSNumber> directionValue( | 70 base::scoped_nsobject<NSNumber> directionValue( |
| 71 [[NSNumber alloc] initWithInteger: | 71 [[NSNumber alloc] initWithInteger: |
| 72 writingDirection + NSTextWritingDirectionOverride]); | 72 writingDirection + NSTextWritingDirectionOverride]); |
|
Nico
2013/06/25 01:09:50
Also PR15349.
| |
| 73 scoped_nsobject<NSArray> directionArray( | 73 base::scoped_nsobject<NSArray> directionArray( |
| 74 [[NSArray alloc] initWithObjects:directionValue.get(), nil]); | 74 [[NSArray alloc] initWithObjects:directionValue.get(), nil]); |
| 75 [attrs setObject:directionArray forKey:NSWritingDirectionAttributeName]; | 75 [attrs setObject:directionArray forKey:NSWritingDirectionAttributeName]; |
| 76 } | 76 } |
| 77 | 77 |
| 78 [attrs setObject:[NSFont menuFontOfSize:fontSize_] | 78 [attrs setObject:[NSFont menuFontOfSize:fontSize_] |
| 79 forKey:NSFontAttributeName]; | 79 forKey:NSFontAttributeName]; |
| 80 | 80 |
| 81 scoped_nsobject<NSAttributedString> attrTitle( | 81 base::scoped_nsobject<NSAttributedString> attrTitle( |
| 82 [[NSAttributedString alloc] initWithString:title | 82 [[NSAttributedString alloc] initWithString:title attributes:attrs]); |
| 83 attributes:attrs]); | |
| 84 [menuItem setAttributedTitle:attrTitle]; | 83 [menuItem setAttributedTitle:attrTitle]; |
| 85 | 84 |
| 86 [menuItem setTag:[menu_ numberOfItems] - 1]; | 85 [menuItem setTag:[menu_ numberOfItems] - 1]; |
| 87 } | 86 } |
| 88 | 87 |
| 89 // Reflects the result of the user's interaction with the popup menu. If NO, the | 88 // Reflects the result of the user's interaction with the popup menu. If NO, the |
| 90 // menu was dismissed without the user choosing an item, which can happen if the | 89 // menu was dismissed without the user choosing an item, which can happen if the |
| 91 // user clicked outside the menu region or hit the escape key. If YES, the user | 90 // user clicked outside the menu region or hit the escape key. If YES, the user |
| 92 // selected an item from the menu. | 91 // selected an item from the menu. |
| 93 - (BOOL)menuItemWasChosen { | 92 - (BOOL)menuItemWasChosen { |
| 94 return menuItemWasChosen_; | 93 return menuItemWasChosen_; |
| 95 } | 94 } |
| 96 | 95 |
| 97 - (void)menuItemSelected:(id)sender { | 96 - (void)menuItemSelected:(id)sender { |
| 98 menuItemWasChosen_ = YES; | 97 menuItemWasChosen_ = YES; |
| 99 } | 98 } |
| 100 | 99 |
| 101 - (void)runMenuInView:(NSView*)view | 100 - (void)runMenuInView:(NSView*)view |
| 102 withBounds:(NSRect)bounds | 101 withBounds:(NSRect)bounds |
| 103 initialIndex:(int)index { | 102 initialIndex:(int)index { |
| 104 // Set up the button cell, converting to NSView coordinates. The menu is | 103 // Set up the button cell, converting to NSView coordinates. The menu is |
| 105 // positioned such that the currently selected menu item appears over the | 104 // positioned such that the currently selected menu item appears over the |
| 106 // popup button, which is the expected Mac popup menu behavior. | 105 // popup button, which is the expected Mac popup menu behavior. |
| 107 scoped_nsobject<NSPopUpButtonCell> | 106 base::scoped_nsobject<NSPopUpButtonCell> cell( |
| 108 cell([[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO]); | 107 [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO]); |
| 109 [cell setMenu:menu_]; | 108 [cell setMenu:menu_]; |
| 110 // We use selectItemWithTag below so if the index is out-of-bounds nothing | 109 // We use selectItemWithTag below so if the index is out-of-bounds nothing |
| 111 // bad happens. | 110 // bad happens. |
| 112 [cell selectItemWithTag:index]; | 111 [cell selectItemWithTag:index]; |
| 113 | 112 |
| 114 if (rightAligned_ && | 113 if (rightAligned_ && |
| 115 [cell respondsToSelector:@selector(setUserInterfaceLayoutDirection:)]) { | 114 [cell respondsToSelector:@selector(setUserInterfaceLayoutDirection:)]) { |
| 116 [cell setUserInterfaceLayoutDirection: | 115 [cell setUserInterfaceLayoutDirection: |
| 117 NSUserInterfaceLayoutDirectionRightToLeft]; | 116 NSUserInterfaceLayoutDirectionRightToLeft]; |
| 118 } | 117 } |
| 119 | 118 |
| 120 // When popping up a menu near the Dock, Cocoa restricts the menu | 119 // When popping up a menu near the Dock, Cocoa restricts the menu |
| 121 // size to not overlap the Dock, with a scroll arrow. Below a | 120 // size to not overlap the Dock, with a scroll arrow. Below a |
| 122 // certain point this doesn't work. At that point the menu is | 121 // certain point this doesn't work. At that point the menu is |
| 123 // popped up above the element, so that the current item can be | 122 // popped up above the element, so that the current item can be |
| 124 // selected without mouse-tracking selecting a different item | 123 // selected without mouse-tracking selecting a different item |
| 125 // immediately. | 124 // immediately. |
| 126 // | 125 // |
| 127 // Unfortunately, instead of popping up above the passed |bounds|, | 126 // Unfortunately, instead of popping up above the passed |bounds|, |
| 128 // it pops up above the bounds of the view passed to inView:. Use a | 127 // it pops up above the bounds of the view passed to inView:. Use a |
| 129 // dummy view to fake this out. | 128 // dummy view to fake this out. |
| 130 scoped_nsobject<NSView> dummyView([[NSView alloc] initWithFrame:bounds]); | 129 base::scoped_nsobject<NSView> dummyView( |
| 130 [[NSView alloc] initWithFrame:bounds]); | |
| 131 [view addSubview:dummyView]; | 131 [view addSubview:dummyView]; |
| 132 | 132 |
| 133 // Display the menu, and set a flag if a menu item was chosen. | 133 // Display the menu, and set a flag if a menu item was chosen. |
| 134 [cell attachPopUpWithFrame:[dummyView bounds] inView:dummyView]; | 134 [cell attachPopUpWithFrame:[dummyView bounds] inView:dummyView]; |
| 135 [cell performClickWithFrame:[dummyView bounds] inView:dummyView]; | 135 [cell performClickWithFrame:[dummyView bounds] inView:dummyView]; |
| 136 | 136 |
| 137 [dummyView removeFromSuperview]; | 137 [dummyView removeFromSuperview]; |
| 138 | 138 |
| 139 if ([self menuItemWasChosen]) | 139 if ([self menuItemWasChosen]) |
| 140 index_ = [cell indexOfSelectedItem]; | 140 index_ = [cell indexOfSelectedItem]; |
| 141 } | 141 } |
| 142 | 142 |
| 143 - (int)indexOfSelectedItem { | 143 - (int)indexOfSelectedItem { |
| 144 return index_; | 144 return index_; |
| 145 } | 145 } |
| 146 | 146 |
| 147 @end // WebMenuRunner | 147 @end // WebMenuRunner |
| OLD | NEW |