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