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 #import "ui/base/cocoa/menu_controller.h" | 5 #import "ui/base/cocoa/menu_controller.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
| 9 #include "ui/base/accelerators/accelerator.h" | 9 #include "ui/base/accelerators/accelerator.h" |
| 10 #include "ui/base/accelerators/platform_accelerator_cocoa.h" | 10 #include "ui/base/accelerators/platform_accelerator_cocoa.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 // associated with the entry in the model identified by |modelIndex|. | 100 // associated with the entry in the model identified by |modelIndex|. |
| 101 - (void)addItemToMenu:(NSMenu*)menu | 101 - (void)addItemToMenu:(NSMenu*)menu |
| 102 atIndex:(NSInteger)index | 102 atIndex:(NSInteger)index |
| 103 fromModel:(ui::MenuModel*)model { | 103 fromModel:(ui::MenuModel*)model { |
| 104 string16 label16 = model->GetLabelAt(index); | 104 string16 label16 = model->GetLabelAt(index); |
| 105 int maxWidth = [self maxWidthForMenuModel:model modelIndex:index]; | 105 int maxWidth = [self maxWidthForMenuModel:model modelIndex:index]; |
| 106 if (maxWidth != -1) | 106 if (maxWidth != -1) |
| 107 label16 = [MenuController elideMenuTitle:label16 toWidth:maxWidth]; | 107 label16 = [MenuController elideMenuTitle:label16 toWidth:maxWidth]; |
| 108 | 108 |
| 109 NSString* label = l10n_util::FixUpWindowsStyleLabel(label16); | 109 NSString* label = l10n_util::FixUpWindowsStyleLabel(label16); |
| 110 scoped_nsobject<NSMenuItem> item( | 110 base::scoped_nsobject<NSMenuItem> item( |
| 111 [[NSMenuItem alloc] initWithTitle:label | 111 [[NSMenuItem alloc] initWithTitle:label |
| 112 action:@selector(itemSelected:) | 112 action:@selector(itemSelected:) |
| 113 keyEquivalent:@""]); | 113 keyEquivalent:@""]); |
| 114 | 114 |
| 115 // If the menu item has an icon, set it. | 115 // If the menu item has an icon, set it. |
| 116 gfx::Image icon; | 116 gfx::Image icon; |
| 117 if (model->GetIconAt(index, &icon) && !icon.IsEmpty()) | 117 if (model->GetIconAt(index, &icon) && !icon.IsEmpty()) |
| 118 [item setImage:icon.ToNSImage()]; | 118 [item setImage:icon.ToNSImage()]; |
| 119 | 119 |
| 120 ui::MenuModel::ItemType type = model->GetTypeAt(index); | 120 ui::MenuModel::ItemType type = model->GetTypeAt(index); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 | 177 |
| 178 gfx::Image icon; | 178 gfx::Image icon; |
| 179 model->GetIconAt(modelIndex, &icon); | 179 model->GetIconAt(modelIndex, &icon); |
| 180 [(id)item setImage:icon.IsEmpty() ? nil : icon.ToNSImage()]; | 180 [(id)item setImage:icon.IsEmpty() ? nil : icon.ToNSImage()]; |
| 181 } | 181 } |
| 182 const gfx::Font* font = model->GetLabelFontAt(modelIndex); | 182 const gfx::Font* font = model->GetLabelFontAt(modelIndex); |
| 183 if (font) { | 183 if (font) { |
| 184 NSDictionary *attributes = | 184 NSDictionary *attributes = |
| 185 [NSDictionary dictionaryWithObject:font->GetNativeFont() | 185 [NSDictionary dictionaryWithObject:font->GetNativeFont() |
| 186 forKey:NSFontAttributeName]; | 186 forKey:NSFontAttributeName]; |
| 187 scoped_nsobject<NSAttributedString> title( | 187 base::scoped_nsobject<NSAttributedString> title( |
| 188 [[NSAttributedString alloc] initWithString:[(id)item title] | 188 [[NSAttributedString alloc] initWithString:[(id)item title] |
|
Nico
2013/06/25 01:09:50
clang-format gets this right as of clang r184804.
| |
| 189 attributes:attributes]); | 189 attributes:attributes]); |
| 190 [(id)item setAttributedTitle:title.get()]; | 190 [(id)item setAttributedTitle:title.get()]; |
| 191 } | 191 } |
| 192 return model->IsEnabledAt(modelIndex); | 192 return model->IsEnabledAt(modelIndex); |
| 193 } | 193 } |
| 194 return NO; | 194 return NO; |
| 195 } | 195 } |
| 196 | 196 |
| 197 // Called when the user chooses a particular menu item. |sender| is the menu | 197 // Called when the user chooses a particular menu item. |sender| is the menu |
| 198 // item chosen. | 198 // item chosen. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 210 | 210 |
| 211 - (NSMenu*)menu { | 211 - (NSMenu*)menu { |
| 212 if (!menu_ && model_) { | 212 if (!menu_ && model_) { |
| 213 menu_.reset([[self menuFromModel:model_] retain]); | 213 menu_.reset([[self menuFromModel:model_] retain]); |
| 214 [menu_ setDelegate:self]; | 214 [menu_ setDelegate:self]; |
| 215 // If this is to be used with a NSPopUpButtonCell, add an item at the 0th | 215 // If this is to be used with a NSPopUpButtonCell, add an item at the 0th |
| 216 // position that's empty. Doing it after the menu has been constructed won't | 216 // position that's empty. Doing it after the menu has been constructed won't |
| 217 // complicate creation logic, and since the tags are model indexes, they | 217 // complicate creation logic, and since the tags are model indexes, they |
| 218 // are unaffected by the extra item. | 218 // are unaffected by the extra item. |
| 219 if (useWithPopUpButtonCell_) { | 219 if (useWithPopUpButtonCell_) { |
| 220 scoped_nsobject<NSMenuItem> blankItem( | 220 base::scoped_nsobject<NSMenuItem> blankItem( |
| 221 [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""]); | 221 [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""]); |
| 222 [menu_ insertItem:blankItem atIndex:0]; | 222 [menu_ insertItem:blankItem atIndex:0]; |
| 223 } | 223 } |
| 224 } | 224 } |
| 225 return menu_.get(); | 225 return menu_.get(); |
| 226 } | 226 } |
| 227 | 227 |
| 228 - (BOOL)isMenuOpen { | 228 - (BOOL)isMenuOpen { |
| 229 return isMenuOpen_; | 229 return isMenuOpen_; |
| 230 } | 230 } |
| 231 | 231 |
| 232 - (void)menuWillOpen:(NSMenu*)menu { | 232 - (void)menuWillOpen:(NSMenu*)menu { |
| 233 isMenuOpen_ = YES; | 233 isMenuOpen_ = YES; |
| 234 model_->MenuWillShow(); | 234 model_->MenuWillShow(); |
| 235 } | 235 } |
| 236 | 236 |
| 237 - (void)menuDidClose:(NSMenu*)menu { | 237 - (void)menuDidClose:(NSMenu*)menu { |
| 238 if (isMenuOpen_) { | 238 if (isMenuOpen_) { |
| 239 model_->MenuClosed(); | 239 model_->MenuClosed(); |
| 240 isMenuOpen_ = NO; | 240 isMenuOpen_ = NO; |
| 241 } | 241 } |
| 242 } | 242 } |
| 243 | 243 |
| 244 @end | 244 @end |
| OLD | NEW |