OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ui/cocoa/menu_controller.h" | 5 #import "chrome/browser/ui/cocoa/menu_controller.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
9 #import "chrome/browser/ui/cocoa/event_utils.h" | 9 #import "chrome/browser/ui/cocoa/event_utils.h" |
10 #include "ui/base/accelerators/accelerator.h" | 10 #include "ui/base/accelerators/accelerator.h" |
11 #include "ui/base/accelerators/platform_accelerator_cocoa.h" | 11 #include "ui/base/accelerators/platform_accelerator_cocoa.h" |
12 #include "ui/base/l10n/l10n_util_mac.h" | 12 #include "ui/base/l10n/l10n_util_mac.h" |
13 #include "ui/base/models/simple_menu_model.h" | 13 #include "ui/base/models/simple_menu_model.h" |
| 14 #include "ui/base/text/text_elider.h" |
14 #include "ui/gfx/image/image.h" | 15 #include "ui/gfx/image/image.h" |
15 | 16 |
16 @interface MenuController (Private) | 17 @interface MenuController (Private) |
17 - (void)addSeparatorToMenu:(NSMenu*)menu | 18 - (void)addSeparatorToMenu:(NSMenu*)menu |
18 atIndex:(int)index; | 19 atIndex:(int)index; |
19 @end | 20 @end |
20 | 21 |
21 @implementation MenuController | 22 @implementation MenuController |
22 | 23 |
23 @synthesize model = model_; | 24 @synthesize model = model_; |
24 @synthesize useWithPopUpButtonCell = useWithPopUpButtonCell_; | 25 @synthesize useWithPopUpButtonCell = useWithPopUpButtonCell_; |
25 | 26 |
| 27 + (string16)elideMenuTitle:(const string16&)title |
| 28 toWidth:(int)width { |
| 29 NSFont* nsfont = [NSFont menuBarFontOfSize:0]; // 0 means "default" |
| 30 gfx::Font font(base::SysNSStringToUTF8([nsfont fontName]), |
| 31 static_cast<int>([nsfont pointSize])); |
| 32 return ui::ElideText(title, font, width, ui::ELIDE_AT_END); |
| 33 } |
| 34 |
26 - (id)init { | 35 - (id)init { |
27 self = [super init]; | 36 self = [super init]; |
28 return self; | 37 return self; |
29 } | 38 } |
30 | 39 |
31 - (id)initWithModel:(ui::MenuModel*)model | 40 - (id)initWithModel:(ui::MenuModel*)model |
32 useWithPopUpButtonCell:(BOOL)useWithCell { | 41 useWithPopUpButtonCell:(BOOL)useWithCell { |
33 if ((self = [super init])) { | 42 if ((self = [super init])) { |
34 model_ = model; | 43 model_ = model; |
35 useWithPopUpButtonCell_ = useWithCell; | 44 useWithPopUpButtonCell_ = useWithCell; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 [self addItemToMenu:menu | 85 [self addItemToMenu:menu |
77 atIndex:index | 86 atIndex:index |
78 fromModel:model | 87 fromModel:model |
79 modelIndex:modelIndex]; | 88 modelIndex:modelIndex]; |
80 } | 89 } |
81 } | 90 } |
82 | 91 |
83 return menu; | 92 return menu; |
84 } | 93 } |
85 | 94 |
| 95 - (int)maxWidthForMenuModel:(ui::MenuModel*)model |
| 96 modelIndex:(int)modelIndex { |
| 97 return -1; |
| 98 } |
| 99 |
86 // Adds a separator item at the given index. As the separator doesn't need | 100 // Adds a separator item at the given index. As the separator doesn't need |
87 // anything from the model, this method doesn't need the model index as the | 101 // anything from the model, this method doesn't need the model index as the |
88 // other method below does. | 102 // other method below does. |
89 - (void)addSeparatorToMenu:(NSMenu*)menu | 103 - (void)addSeparatorToMenu:(NSMenu*)menu |
90 atIndex:(int)index { | 104 atIndex:(int)index { |
91 NSMenuItem* separator = [NSMenuItem separatorItem]; | 105 NSMenuItem* separator = [NSMenuItem separatorItem]; |
92 [menu insertItem:separator atIndex:index]; | 106 [menu insertItem:separator atIndex:index]; |
93 } | 107 } |
94 | 108 |
95 // Adds an item or a hierarchical menu to the item at the |index|, | 109 // Adds an item or a hierarchical menu to the item at the |index|, |
96 // associated with the entry in the model indentifed by |modelIndex|. | 110 // associated with the entry in the model indentifed by |modelIndex|. |
97 - (void)addItemToMenu:(NSMenu*)menu | 111 - (void)addItemToMenu:(NSMenu*)menu |
98 atIndex:(NSInteger)index | 112 atIndex:(NSInteger)index |
99 fromModel:(ui::MenuModel*)model | 113 fromModel:(ui::MenuModel*)model |
100 modelIndex:(int)modelIndex { | 114 modelIndex:(int)modelIndex { |
101 NSString* label = | 115 string16 label16 = model->GetLabelAt(modelIndex); |
102 l10n_util::FixUpWindowsStyleLabel(model->GetLabelAt(modelIndex)); | 116 int maxWidth = [self maxWidthForMenuModel:model modelIndex:modelIndex]; |
| 117 if (maxWidth != -1) |
| 118 label16 = [MenuController elideMenuTitle:label16 toWidth:maxWidth]; |
| 119 |
| 120 NSString* label = l10n_util::FixUpWindowsStyleLabel(label16); |
103 scoped_nsobject<NSMenuItem> item( | 121 scoped_nsobject<NSMenuItem> item( |
104 [[NSMenuItem alloc] initWithTitle:label | 122 [[NSMenuItem alloc] initWithTitle:label |
105 action:@selector(itemSelected:) | 123 action:@selector(itemSelected:) |
106 keyEquivalent:@""]); | 124 keyEquivalent:@""]); |
107 | 125 |
108 // If the menu item has an icon, set it. | 126 // If the menu item has an icon, set it. |
109 gfx::Image icon; | 127 gfx::Image icon; |
110 if (model->GetIconAt(modelIndex, &icon) && !icon.IsEmpty()) | 128 if (model->GetIconAt(modelIndex, &icon) && !icon.IsEmpty()) |
111 [item setImage:icon.ToNSImage()]; | 129 [item setImage:icon.ToNSImage()]; |
112 | 130 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 } | 232 } |
215 | 233 |
216 - (void)menuDidClose:(NSMenu*)menu { | 234 - (void)menuDidClose:(NSMenu*)menu { |
217 if (isMenuOpen_) { | 235 if (isMenuOpen_) { |
218 model_->MenuClosed(); | 236 model_->MenuClosed(); |
219 isMenuOpen_ = NO; | 237 isMenuOpen_ = NO; |
220 } | 238 } |
221 } | 239 } |
222 | 240 |
223 @end | 241 @end |
OLD | NEW |