OLD | NEW |
1 // Copyright (c) 2010 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/wrench_menu_controller.h" | 5 #import "chrome/browser/cocoa/wrench_menu_controller.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "app/menus/menu_model.h" | 8 #include "app/menus/menu_model.h" |
9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
10 #include "chrome/app/chrome_dll_resource.h" | 10 #include "chrome/app/chrome_dll_resource.h" |
11 #include "chrome/browser/browser.h" | 11 #include "chrome/browser/browser.h" |
12 #include "chrome/browser/browser_window.h" | 12 #include "chrome/browser/browser_window.h" |
| 13 #import "chrome/browser/cocoa/menu_tracked_root_view.h" |
13 #import "chrome/browser/cocoa/toolbar_controller.h" | 14 #import "chrome/browser/cocoa/toolbar_controller.h" |
14 #include "chrome/browser/wrench_menu_model.h" | 15 #include "chrome/browser/wrench_menu_model.h" |
15 #include "grit/chromium_strings.h" | 16 #include "grit/chromium_strings.h" |
16 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
17 | 18 |
18 @interface WrenchMenuController (Private) | 19 @interface WrenchMenuController (Private) |
19 - (WrenchMenuModel*)wrenchMenuModel; | 20 - (WrenchMenuModel*)wrenchMenuModel; |
20 - (void)adjustPositioning; | 21 - (void)adjustPositioning; |
21 @end | 22 @end |
22 | 23 |
(...skipping 21 matching lines...) Expand all Loading... |
44 // Handle the special-cased menu items. | 45 // Handle the special-cased menu items. |
45 int command_id = model->GetCommandIdAt(modelIndex); | 46 int command_id = model->GetCommandIdAt(modelIndex); |
46 scoped_nsobject<NSMenuItem> customItem( | 47 scoped_nsobject<NSMenuItem> customItem( |
47 [[NSMenuItem alloc] initWithTitle:@"" | 48 [[NSMenuItem alloc] initWithTitle:@"" |
48 action:nil | 49 action:nil |
49 keyEquivalent:@""]); | 50 keyEquivalent:@""]); |
50 switch (command_id) { | 51 switch (command_id) { |
51 case IDC_EDIT_MENU: | 52 case IDC_EDIT_MENU: |
52 DCHECK(editItem_); | 53 DCHECK(editItem_); |
53 [customItem setView:editItem_]; | 54 [customItem setView:editItem_]; |
| 55 [editItem_ setMenuItem:customItem]; |
54 break; | 56 break; |
55 case IDC_ZOOM_MENU: | 57 case IDC_ZOOM_MENU: |
56 DCHECK(zoomItem_); | 58 DCHECK(zoomItem_); |
57 [customItem setView:zoomItem_]; | 59 [customItem setView:zoomItem_]; |
| 60 [zoomItem_ setMenuItem:customItem]; |
58 break; | 61 break; |
59 default: | 62 default: |
60 NOTREACHED(); | 63 NOTREACHED(); |
61 break; | 64 break; |
62 } | 65 } |
63 [self adjustPositioning]; | 66 [self adjustPositioning]; |
64 [menu insertItem:customItem.get() atIndex:index]; | 67 [menu insertItem:customItem.get() atIndex:index]; |
65 } | 68 } |
66 | 69 |
67 - (NSMenu*)menu { | 70 - (NSMenu*)menu { |
68 NSMenu* menu = [super menu]; | 71 NSMenu* menu = [super menu]; |
69 if (![menu delegate]) { | 72 if (![menu delegate]) { |
70 [menu setDelegate:self]; | 73 [menu setDelegate:self]; |
71 } | 74 } |
72 return menu; | 75 return menu; |
73 } | 76 } |
74 | 77 |
75 - (void)menuWillOpen:(NSMenu*)menu { | 78 - (void)menuWillOpen:(NSMenu*)menu { |
76 NSString* title = base::SysUTF16ToNSString( | 79 NSString* title = base::SysUTF16ToNSString( |
77 [self wrenchMenuModel]->GetLabelForCommandId(IDC_ZOOM_PERCENT_DISPLAY)); | 80 [self wrenchMenuModel]->GetLabelForCommandId(IDC_ZOOM_PERCENT_DISPLAY)); |
78 [[zoomItem_ viewWithTag:IDC_ZOOM_PERCENT_DISPLAY] setTitle:title]; | 81 [[zoomItem_ viewWithTag:IDC_ZOOM_PERCENT_DISPLAY] setTitle:title]; |
| 82 |
79 bool plusEnabled = [self wrenchMenuModel]->IsCommandIdEnabled(IDC_ZOOM_PLUS); | 83 bool plusEnabled = [self wrenchMenuModel]->IsCommandIdEnabled(IDC_ZOOM_PLUS); |
80 bool minusEnabled = [self wrenchMenuModel]->IsCommandIdEnabled( | 84 bool minusEnabled = [self wrenchMenuModel]->IsCommandIdEnabled( |
81 IDC_ZOOM_MINUS); | 85 IDC_ZOOM_MINUS); |
82 | |
83 [zoomPlus_ setEnabled:plusEnabled]; | 86 [zoomPlus_ setEnabled:plusEnabled]; |
84 [zoomMinus_ setEnabled:minusEnabled]; | 87 [zoomMinus_ setEnabled:minusEnabled]; |
85 | 88 |
86 NSImage* icon = [self wrenchMenuModel]->browser()->window()->IsFullscreen() ? | 89 NSImage* icon = [self wrenchMenuModel]->browser()->window()->IsFullscreen() ? |
87 [NSImage imageNamed:NSImageNameExitFullScreenTemplate] : | 90 [NSImage imageNamed:NSImageNameExitFullScreenTemplate] : |
88 [NSImage imageNamed:NSImageNameEnterFullScreenTemplate]; | 91 [NSImage imageNamed:NSImageNameEnterFullScreenTemplate]; |
89 [zoomFullScreen_ setImage:icon]; | 92 [zoomFullScreen_ setImage:icon]; |
90 } | 93 } |
91 | 94 |
92 // Used to dispatch commands from the Wrench menu. The custom items within the | 95 // Used to dispatch commands from the Wrench menu. The custom items within the |
93 // menu cannot be hooked up directly to First Responder because the window in | 96 // menu cannot be hooked up directly to First Responder because the window in |
94 // which the controls reside is not the BrowserWindowController, but a | 97 // which the controls reside is not the BrowserWindowController, but a |
95 // NSCarbonMenuWindow; this screws up the typical |-commandDispatch:| system. | 98 // NSCarbonMenuWindow; this screws up the typical |-commandDispatch:| system. |
96 - (IBAction)dispatchWrenchMenuCommand:(id)sender { | 99 - (IBAction)dispatchWrenchMenuCommand:(id)sender { |
97 NSInteger tag = [sender tag]; | 100 NSInteger tag = [sender tag]; |
98 | |
99 // NSSegmentedControls (used for the Edit item) need a little help to get the | |
100 // command_id of the pressed item. | |
101 if ([sender isKindOfClass:[NSSegmentedControl class]]) | |
102 tag = [[sender cell] tagForSegment:[sender selectedSegment]]; | |
103 | |
104 // The custom views within the Wrench menu are abnormal and keep the menu open | 101 // The custom views within the Wrench menu are abnormal and keep the menu open |
105 // after a target-action. Close the menu manually. | 102 // after a target-action. Close the menu manually. |
106 // TODO(rsesek): It'd be great if the zoom buttons didn't have to close the | 103 // TODO(rsesek): It'd be great if the zoom buttons didn't have to close the |
107 // menu. See http://crbug.com/48679 for more info. | 104 // menu. See http://crbug.com/48679 for more info. |
108 [menu_ cancelTracking]; | 105 [menu_ cancelTracking]; |
109 [self wrenchMenuModel]->ExecuteCommand(tag); | 106 [self wrenchMenuModel]->ExecuteCommand(tag); |
110 } | 107 } |
111 | 108 |
112 - (WrenchMenuModel*)wrenchMenuModel { | 109 - (WrenchMenuModel*)wrenchMenuModel { |
113 return static_cast<WrenchMenuModel*>(model_); | 110 return static_cast<WrenchMenuModel*>(model_); |
(...skipping 25 matching lines...) Expand all Loading... |
139 // to be adjusted after insertion. | 136 // to be adjusted after insertion. |
140 for (NSInteger i = menuIndex + 1; i < [[self menu] numberOfItems]; ++i) { | 137 for (NSInteger i = menuIndex + 1; i < [[self menu] numberOfItems]; ++i) { |
141 NSMenuItem* item = [[self menu] itemAtIndex:i]; | 138 NSMenuItem* item = [[self menu] itemAtIndex:i]; |
142 [item setTag:[item tag] + 1]; | 139 [item setTag:[item tag] + 1]; |
143 } | 140 } |
144 } | 141 } |
145 | 142 |
146 // Fit the localized strings into the Cut/Copy/Paste control, then resize the | 143 // Fit the localized strings into the Cut/Copy/Paste control, then resize the |
147 // whole menu item accordingly. | 144 // whole menu item accordingly. |
148 - (void)adjustPositioning { | 145 - (void)adjustPositioning { |
| 146 const CGFloat kButtonPadding = 12; |
| 147 CGFloat delta = 0; |
| 148 |
| 149 // Go through the three buttons from right-to-left, adjusting the size to fit |
| 150 // the localized strings while keeping them all aligned on their horizontal |
| 151 // edges. |
| 152 const size_t kAdjustViewCount = 3; |
| 153 NSButton* views[kAdjustViewCount] = { editPaste_, editCopy_, editCut_ }; |
| 154 for (size_t i = 0; i < kAdjustViewCount; ++i) { |
| 155 NSButton* button = views[i]; |
| 156 CGFloat originalWidth = NSWidth([button frame]); |
| 157 |
| 158 // Do not let |-sizeToFit| change the height of the button. |
| 159 NSSize size = [button frame].size; |
| 160 [button sizeToFit]; |
| 161 size.width = [button frame].size.width + kButtonPadding; |
| 162 [button setFrameSize:size]; |
| 163 |
| 164 CGFloat newWidth = size.width; |
| 165 delta += newWidth - originalWidth; |
| 166 |
| 167 NSRect frame = [button frame]; |
| 168 frame.origin.x -= delta; |
| 169 [button setFrame:frame]; |
| 170 } |
| 171 |
| 172 // Resize the menu item by the total amound the buttons changed so that the |
| 173 // spacing between the buttons and the title remains the same. |
149 NSRect itemFrame = [editItem_ frame]; | 174 NSRect itemFrame = [editItem_ frame]; |
150 NSRect controlFrame = [editControl_ frame]; | 175 itemFrame.size.width += delta; |
151 | |
152 CGFloat originalControlWidth = NSWidth(controlFrame); | |
153 // Maintain the carefully pixel-pushed gap between the edge of the menu and | |
154 // the rightmost control. | |
155 CGFloat edge = NSWidth(itemFrame) - | |
156 (controlFrame.origin.x + originalControlWidth); | |
157 | |
158 // Resize the edit segmented control to fit the localized strings. | |
159 [editControl_ sizeToFit]; | |
160 controlFrame = [editControl_ frame]; | |
161 CGFloat resizeAmount = NSWidth(controlFrame) - originalControlWidth; | |
162 | |
163 // Adjust the size of the entire menu item to account for changes in the size | |
164 // of the segmented control. | |
165 itemFrame.size.width += resizeAmount; | |
166 [editItem_ setFrame:itemFrame]; | 176 [editItem_ setFrame:itemFrame]; |
167 | 177 |
168 // Keep the spacing between the right edges of the menu and the control. | 178 // Also resize the superview of the buttons, which is an NSView used to slide |
169 controlFrame.origin.x = NSWidth(itemFrame) - edge - NSWidth(controlFrame); | 179 // when the item title is too big and GTM resizes it. |
170 [editControl_ setFrame:controlFrame]; | 180 NSRect parentFrame = [[editCut_ superview] frame]; |
| 181 parentFrame.size.width += delta; |
| 182 parentFrame.origin.x -= delta; |
| 183 [[editCut_ superview] setFrame:parentFrame]; |
171 } | 184 } |
172 | 185 |
173 @end // @implementation WrenchMenuController | 186 @end // @implementation WrenchMenuController |
OLD | NEW |