| 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 #include "ui/views/controls/menu/menu_win.h" | 5 #include "ui/views/controls/menu/menu_win.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 const int kItemRightMargin = 10; | 33 const int kItemRightMargin = 10; |
| 34 // The width for displaying the sub-menu arrow. | 34 // The width for displaying the sub-menu arrow. |
| 35 const int kArrowWidth = 10; | 35 const int kArrowWidth = 10; |
| 36 | 36 |
| 37 // Current active MenuHostWindow. If NULL, no menu is active. | 37 // Current active MenuHostWindow. If NULL, no menu is active. |
| 38 static MenuHostWindow* active_host_window = NULL; | 38 static MenuHostWindow* active_host_window = NULL; |
| 39 | 39 |
| 40 // The data of menu items needed to display. | 40 // The data of menu items needed to display. |
| 41 struct MenuWin::ItemData { | 41 struct MenuWin::ItemData { |
| 42 string16 label; | 42 string16 label; |
| 43 SkBitmap icon; | 43 gfx::ImageSkia icon; |
| 44 bool submenu; | 44 bool submenu; |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 namespace { | 47 namespace { |
| 48 | 48 |
| 49 static int ChromeGetMenuItemID(HMENU hMenu, int pos) { | 49 static int ChromeGetMenuItemID(HMENU hMenu, int pos) { |
| 50 // The built-in Windows GetMenuItemID doesn't work for submenus, | 50 // The built-in Windows GetMenuItemID doesn't work for submenus, |
| 51 // so here's our own implementation. | 51 // so here's our own implementation. |
| 52 MENUITEMINFO mii = {0}; | 52 MENUITEMINFO mii = {0}; |
| 53 mii.cbSize = sizeof(mii); | 53 mii.cbSize = sizeof(mii); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 | 256 |
| 257 MenuWin::~MenuWin() { | 257 MenuWin::~MenuWin() { |
| 258 STLDeleteContainerPointers(submenus_.begin(), submenus_.end()); | 258 STLDeleteContainerPointers(submenus_.begin(), submenus_.end()); |
| 259 STLDeleteContainerPointers(item_data_.begin(), item_data_.end()); | 259 STLDeleteContainerPointers(item_data_.begin(), item_data_.end()); |
| 260 DestroyMenu(menu_); | 260 DestroyMenu(menu_); |
| 261 } | 261 } |
| 262 | 262 |
| 263 void MenuWin::AddMenuItemWithIcon(int index, | 263 void MenuWin::AddMenuItemWithIcon(int index, |
| 264 int item_id, | 264 int item_id, |
| 265 const string16& label, | 265 const string16& label, |
| 266 const SkBitmap& icon) { | 266 const gfx::ImageSkia& icon) { |
| 267 owner_draw_ = true; | 267 owner_draw_ = true; |
| 268 Menu::AddMenuItemWithIcon(index, item_id, label, icon); | 268 Menu::AddMenuItemWithIcon(index, item_id, label, icon); |
| 269 } | 269 } |
| 270 | 270 |
| 271 Menu* MenuWin::AddSubMenuWithIcon(int index, | 271 Menu* MenuWin::AddSubMenuWithIcon(int index, |
| 272 int item_id, | 272 int item_id, |
| 273 const string16& label, | 273 const string16& label, |
| 274 const SkBitmap& icon) { | 274 const gfx::ImageSkia& icon) { |
| 275 MenuWin* submenu = new MenuWin(this); | 275 MenuWin* submenu = new MenuWin(this); |
| 276 submenus_.push_back(submenu); | 276 submenus_.push_back(submenu); |
| 277 AddMenuItemInternal(index, item_id, label, icon, submenu->menu_, NORMAL); | 277 AddMenuItemInternal(index, item_id, label, icon, submenu->menu_, NORMAL); |
| 278 return submenu; | 278 return submenu; |
| 279 } | 279 } |
| 280 | 280 |
| 281 void MenuWin::AddSeparator(int index) { | 281 void MenuWin::AddSeparator(int index) { |
| 282 MENUITEMINFO mii; | 282 MENUITEMINFO mii; |
| 283 mii.cbSize = sizeof(mii); | 283 mii.cbSize = sizeof(mii); |
| 284 mii.fMask = MIIM_FTYPE; | 284 mii.fMask = MIIM_FTYPE; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 298 | 298 |
| 299 void MenuWin::SetMenuLabel(int item_id, const string16& label) { | 299 void MenuWin::SetMenuLabel(int item_id, const string16& label) { |
| 300 MENUITEMINFO mii = {0}; | 300 MENUITEMINFO mii = {0}; |
| 301 mii.cbSize = sizeof(mii); | 301 mii.cbSize = sizeof(mii); |
| 302 mii.fMask = MIIM_STRING; | 302 mii.fMask = MIIM_STRING; |
| 303 mii.dwTypeData = const_cast<wchar_t*>(label.c_str()); | 303 mii.dwTypeData = const_cast<wchar_t*>(label.c_str()); |
| 304 mii.cch = static_cast<UINT>(label.size()); | 304 mii.cch = static_cast<UINT>(label.size()); |
| 305 SetMenuItemInfo(menu_, item_id, false, &mii); | 305 SetMenuItemInfo(menu_, item_id, false, &mii); |
| 306 } | 306 } |
| 307 | 307 |
| 308 bool MenuWin::SetIcon(const SkBitmap& icon, int item_id) { | 308 bool MenuWin::SetIcon(const gfx::ImageSkia& icon, int item_id) { |
| 309 if (!owner_draw_) | 309 if (!owner_draw_) |
| 310 owner_draw_ = true; | 310 owner_draw_ = true; |
| 311 | 311 |
| 312 const int num_items = GetMenuItemCount(menu_); | 312 const int num_items = GetMenuItemCount(menu_); |
| 313 int sep_count = 0; | 313 int sep_count = 0; |
| 314 for (int i = 0; i < num_items; ++i) { | 314 for (int i = 0; i < num_items; ++i) { |
| 315 if (!(GetMenuState(menu_, i, MF_BYPOSITION) & MF_SEPARATOR)) { | 315 if (!(GetMenuState(menu_, i, MF_BYPOSITION) & MF_SEPARATOR)) { |
| 316 if (ChromeGetMenuItemID(menu_, i) == item_id) { | 316 if (ChromeGetMenuItemID(menu_, i) == item_id) { |
| 317 item_data_[i - sep_count]->icon = icon; | 317 item_data_[i - sep_count]->icon = icon; |
| 318 // When the menu is running, we use SetMenuItemInfo to let Windows | 318 // When the menu is running, we use SetMenuItemInfo to let Windows |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 EndMenu(); | 379 EndMenu(); |
| 380 } | 380 } |
| 381 | 381 |
| 382 int MenuWin::ItemCount() { | 382 int MenuWin::ItemCount() { |
| 383 return GetMenuItemCount(menu_); | 383 return GetMenuItemCount(menu_); |
| 384 } | 384 } |
| 385 | 385 |
| 386 void MenuWin::AddMenuItemInternal(int index, | 386 void MenuWin::AddMenuItemInternal(int index, |
| 387 int item_id, | 387 int item_id, |
| 388 const string16& label, | 388 const string16& label, |
| 389 const SkBitmap& icon, | 389 const gfx::ImageSkia& icon, |
| 390 MenuItemType type) { | 390 MenuItemType type) { |
| 391 AddMenuItemInternal(index, item_id, label, icon, NULL, type); | 391 AddMenuItemInternal(index, item_id, label, icon, NULL, type); |
| 392 } | 392 } |
| 393 | 393 |
| 394 void MenuWin::AddMenuItemInternal(int index, | 394 void MenuWin::AddMenuItemInternal(int index, |
| 395 int item_id, | 395 int item_id, |
| 396 const string16& label, | 396 const string16& label, |
| 397 const SkBitmap& icon, | 397 const gfx::ImageSkia& icon, |
| 398 HMENU submenu, | 398 HMENU submenu, |
| 399 MenuItemType type) { | 399 MenuItemType type) { |
| 400 DCHECK(type != SEPARATOR) << "Call AddSeparator instead!"; | 400 DCHECK(type != SEPARATOR) << "Call AddSeparator instead!"; |
| 401 | 401 |
| 402 if (!owner_draw_ && !icon.empty()) | 402 if (!owner_draw_ && !icon.empty()) |
| 403 owner_draw_ = true; | 403 owner_draw_ = true; |
| 404 | 404 |
| 405 if (label.empty() && !delegate()) { | 405 if (label.empty() && !delegate()) { |
| 406 // No label and no delegate; don't add an empty menu. | 406 // No label and no delegate; don't add an empty menu. |
| 407 // It appears under some circumstance we're getting an empty label | 407 // It appears under some circumstance we're getting an empty label |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 break; | 563 break; |
| 564 | 564 |
| 565 default: | 565 default: |
| 566 NOTREACHED(); | 566 NOTREACHED(); |
| 567 return 0; | 567 return 0; |
| 568 } | 568 } |
| 569 return align_flags; | 569 return align_flags; |
| 570 } | 570 } |
| 571 | 571 |
| 572 } // namespace views | 572 } // namespace views |
| OLD | NEW |