| 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 "skia/ext/skia_utils_mac.h" | 10 #include "skia/ext/skia_utils_mac.h" |
| 11 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
| 12 #include "ui/base/accelerators/accelerator_cocoa.h" | 12 #include "ui/base/accelerators/accelerator_cocoa.h" |
| 13 #include "ui/base/l10n/l10n_util_mac.h" | 13 #include "ui/base/l10n/l10n_util_mac.h" |
| 14 #include "ui/base/models/simple_menu_model.h" | 14 #include "ui/base/models/simple_menu_model.h" |
| 15 #include "ui/gfx/image/image_skia.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_; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 fromModel:(ui::MenuModel*)model | 100 fromModel:(ui::MenuModel*)model |
| 100 modelIndex:(int)modelIndex { | 101 modelIndex:(int)modelIndex { |
| 101 NSString* label = | 102 NSString* label = |
| 102 l10n_util::FixUpWindowsStyleLabel(model->GetLabelAt(modelIndex)); | 103 l10n_util::FixUpWindowsStyleLabel(model->GetLabelAt(modelIndex)); |
| 103 scoped_nsobject<NSMenuItem> item( | 104 scoped_nsobject<NSMenuItem> item( |
| 104 [[NSMenuItem alloc] initWithTitle:label | 105 [[NSMenuItem alloc] initWithTitle:label |
| 105 action:@selector(itemSelected:) | 106 action:@selector(itemSelected:) |
| 106 keyEquivalent:@""]); | 107 keyEquivalent:@""]); |
| 107 | 108 |
| 108 // If the menu item has an icon, set it. | 109 // If the menu item has an icon, set it. |
| 109 SkBitmap skiaIcon; | 110 gfx::ImageSkia skiaIcon; |
| 110 if (model->GetIconAt(modelIndex, &skiaIcon) && !skiaIcon.isNull()) { | 111 if (model->GetIconAt(modelIndex, &skiaIcon) && !skiaIcon.isNull()) { |
| 111 NSImage* icon = gfx::SkBitmapToNSImage(skiaIcon); | 112 NSImage* icon = gfx::SkBitmapToNSImage(*skiaIcon.bitmap()); |
| 112 if (icon) { | 113 if (icon) { |
| 113 [item setImage:icon]; | 114 [item setImage:icon]; |
| 114 } | 115 } |
| 115 } | 116 } |
| 116 | 117 |
| 117 ui::MenuModel::ItemType type = model->GetTypeAt(modelIndex); | 118 ui::MenuModel::ItemType type = model->GetTypeAt(modelIndex); |
| 118 if (type == ui::MenuModel::TYPE_SUBMENU) { | 119 if (type == ui::MenuModel::TYPE_SUBMENU) { |
| 119 // Recursively build a submenu from the sub-model at this index. | 120 // Recursively build a submenu from the sub-model at this index. |
| 120 [item setTarget:nil]; | 121 [item setTarget:nil]; |
| 121 [item setAction:nil]; | 122 [item setAction:nil]; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 if (model) { | 159 if (model) { |
| 159 BOOL checked = model->IsItemCheckedAt(modelIndex); | 160 BOOL checked = model->IsItemCheckedAt(modelIndex); |
| 160 DCHECK([(id)item isKindOfClass:[NSMenuItem class]]); | 161 DCHECK([(id)item isKindOfClass:[NSMenuItem class]]); |
| 161 [(id)item setState:(checked ? NSOnState : NSOffState)]; | 162 [(id)item setState:(checked ? NSOnState : NSOffState)]; |
| 162 [(id)item setHidden:(!model->IsVisibleAt(modelIndex))]; | 163 [(id)item setHidden:(!model->IsVisibleAt(modelIndex))]; |
| 163 if (model->IsItemDynamicAt(modelIndex)) { | 164 if (model->IsItemDynamicAt(modelIndex)) { |
| 164 // Update the label and the icon. | 165 // Update the label and the icon. |
| 165 NSString* label = | 166 NSString* label = |
| 166 l10n_util::FixUpWindowsStyleLabel(model->GetLabelAt(modelIndex)); | 167 l10n_util::FixUpWindowsStyleLabel(model->GetLabelAt(modelIndex)); |
| 167 [(id)item setTitle:label]; | 168 [(id)item setTitle:label]; |
| 168 SkBitmap skiaIcon; | 169 gfx::ImageSkia skiaIcon; |
| 169 NSImage* icon = nil; | 170 NSImage* icon = nil; |
| 170 if (model->GetIconAt(modelIndex, &skiaIcon) && !skiaIcon.isNull()) | 171 if (model->GetIconAt(modelIndex, &skiaIcon) && !skiaIcon.isNull()) |
| 171 icon = gfx::SkBitmapToNSImage(skiaIcon); | 172 icon = gfx::SkBitmapToNSImage(*skiaIcon.bitmap()); |
| 172 [(id)item setImage:icon]; | 173 [(id)item setImage:icon]; |
| 173 } | 174 } |
| 174 return model->IsEnabledAt(modelIndex); | 175 return model->IsEnabledAt(modelIndex); |
| 175 } | 176 } |
| 176 return NO; | 177 return NO; |
| 177 } | 178 } |
| 178 | 179 |
| 179 // Called when the user chooses a particular menu item. |sender| is the menu | 180 // Called when the user chooses a particular menu item. |sender| is the menu |
| 180 // item chosen. | 181 // item chosen. |
| 181 - (void)itemSelected:(id)sender { | 182 - (void)itemSelected:(id)sender { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 } | 214 } |
| 214 | 215 |
| 215 - (void)menuDidClose:(NSMenu*)menu { | 216 - (void)menuDidClose:(NSMenu*)menu { |
| 216 if (isMenuOpen_) { | 217 if (isMenuOpen_) { |
| 217 model_->MenuClosed(); | 218 model_->MenuClosed(); |
| 218 isMenuOpen_ = NO; | 219 isMenuOpen_ = NO; |
| 219 } | 220 } |
| 220 } | 221 } |
| 221 | 222 |
| 222 @end | 223 @end |
| OLD | NEW |