Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "views/controls/menu/native_menu_win.h" | 5 #include "views/controls/menu/native_menu_win.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop.h" | |
| 8 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
| 9 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 10 #include "base/win/wrapped_window_proc.h" | 11 #include "base/win/wrapped_window_proc.h" |
| 11 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
| 12 #include "ui/base/keycodes/keyboard_codes.h" | 13 #include "ui/base/keycodes/keyboard_codes.h" |
| 13 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
| 14 #include "ui/base/l10n/l10n_util_win.h" | 15 #include "ui/base/l10n/l10n_util_win.h" |
| 15 #include "ui/base/win/hwnd_util.h" | 16 #include "ui/base/win/hwnd_util.h" |
| 16 #include "ui/gfx/canvas_skia.h" | 17 #include "ui/gfx/canvas_skia.h" |
| 17 #include "ui/gfx/font.h" | 18 #include "ui/gfx/font.h" |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 304 // NativeMenuWin, public: | 305 // NativeMenuWin, public: |
| 305 | 306 |
| 306 NativeMenuWin::NativeMenuWin(ui::MenuModel* model, HWND system_menu_for) | 307 NativeMenuWin::NativeMenuWin(ui::MenuModel* model, HWND system_menu_for) |
| 307 : model_(model), | 308 : model_(model), |
| 308 menu_(NULL), | 309 menu_(NULL), |
| 309 owner_draw_(l10n_util::NeedOverrideDefaultUIFont(NULL, NULL) && | 310 owner_draw_(l10n_util::NeedOverrideDefaultUIFont(NULL, NULL) && |
| 310 !system_menu_for), | 311 !system_menu_for), |
| 311 system_menu_for_(system_menu_for), | 312 system_menu_for_(system_menu_for), |
| 312 first_item_index_(0), | 313 first_item_index_(0), |
| 313 menu_action_(MENU_ACTION_NONE) { | 314 menu_action_(MENU_ACTION_NONE) { |
| 315 model_->SetMenuModelDelegate(this); | |
|
sky
2011/04/05 22:40:48
Move this to RUnMenuAt.
dill
2011/04/08 15:48:46
Done.
| |
| 314 } | 316 } |
| 315 | 317 |
| 316 NativeMenuWin::~NativeMenuWin() { | 318 NativeMenuWin::~NativeMenuWin() { |
| 317 STLDeleteContainerPointers(items_.begin(), items_.end()); | 319 STLDeleteContainerPointers(items_.begin(), items_.end()); |
| 318 DestroyMenu(menu_); | 320 DestroyMenu(menu_); |
| 319 } | 321 } |
| 320 | 322 |
| 321 //////////////////////////////////////////////////////////////////////////////// | 323 //////////////////////////////////////////////////////////////////////////////// |
| 322 // NativeMenuWin, MenuWrapper implementation: | 324 // NativeMenuWin, MenuWrapper implementation: |
| 323 | 325 |
| 324 void NativeMenuWin::RunMenuAt(const gfx::Point& point, int alignment) { | 326 void NativeMenuWin::RunMenuAt(const gfx::Point& point, int alignment) { |
| 325 CreateHostWindow(); | 327 CreateHostWindow(); |
| 326 UpdateStates(); | 328 UpdateStates(); |
| 327 UINT flags = TPM_LEFTBUTTON | TPM_RECURSE; | 329 UINT flags = TPM_LEFTBUTTON | TPM_RECURSE; |
| 328 flags |= GetAlignmentFlags(alignment); | 330 flags |= GetAlignmentFlags(alignment); |
| 329 menu_action_ = MENU_ACTION_NONE; | 331 menu_action_ = MENU_ACTION_NONE; |
| 330 | 332 |
| 331 // Set a hook function so we can listen for keyboard events while the | 333 // Set a hook function so we can listen for keyboard events while the |
| 332 // menu is open, and store a pointer to this object in a static | 334 // menu is open, and store a pointer to this object in a static |
| 333 // variable so the hook has access to it (ugly, but it's the | 335 // variable so the hook has access to it (ugly, but it's the |
| 334 // only way). | 336 // only way). |
| 335 open_native_menu_win_ = this; | 337 open_native_menu_win_ = this; |
| 336 HHOOK hhook = SetWindowsHookEx(WH_MSGFILTER, MenuMessageHook, | 338 HHOOK hhook = SetWindowsHookEx(WH_MSGFILTER, MenuMessageHook, |
| 337 GetModuleHandle(NULL), ::GetCurrentThreadId()); | 339 GetModuleHandle(NULL), ::GetCurrentThreadId()); |
| 338 | 340 |
| 339 // Mark that any registered listeners have not been called for this particular | 341 // Mark that any registered listeners have not been called for this particular |
| 340 // opening of the menu. | 342 // opening of the menu. |
| 341 listeners_called_ = false; | 343 listeners_called_ = false; |
| 342 | 344 |
| 345 // Retrieving favicons from history requires nestable tasks. | |
| 346 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | |
| 347 | |
| 343 // Command dispatch is done through WM_MENUCOMMAND, handled by the host | 348 // Command dispatch is done through WM_MENUCOMMAND, handled by the host |
| 344 // window. | 349 // window. |
| 345 HWND hwnd = host_window_->hwnd(); | 350 HWND hwnd = host_window_->hwnd(); |
| 346 TrackPopupMenuEx(menu_, flags, point.x(), point.y(), host_window_->hwnd(), | 351 TrackPopupMenuEx(menu_, flags, point.x(), point.y(), host_window_->hwnd(), |
| 347 NULL); | 352 NULL); |
| 348 | 353 |
| 354 model_->SetMenuModelDelegate(NULL); | |
| 349 UnhookWindowsHookEx(hhook); | 355 UnhookWindowsHookEx(hhook); |
| 350 open_native_menu_win_ = NULL; | 356 open_native_menu_win_ = NULL; |
| 351 } | 357 } |
| 352 | 358 |
| 353 void NativeMenuWin::CancelMenu() { | 359 void NativeMenuWin::CancelMenu() { |
| 354 EndMenu(); | 360 EndMenu(); |
| 355 } | 361 } |
| 356 | 362 |
| 357 void NativeMenuWin::Rebuild() { | 363 void NativeMenuWin::Rebuild() { |
| 358 ResetNativeMenu(); | 364 ResetNativeMenu(); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 409 listeners_.erase(iter); | 415 listeners_.erase(iter); |
| 410 return; | 416 return; |
| 411 } | 417 } |
| 412 } | 418 } |
| 413 } | 419 } |
| 414 | 420 |
| 415 void NativeMenuWin::SetMinimumWidth(int width) { | 421 void NativeMenuWin::SetMinimumWidth(int width) { |
| 416 NOTIMPLEMENTED(); | 422 NOTIMPLEMENTED(); |
| 417 } | 423 } |
| 418 | 424 |
| 425 void NativeMenuWin::OnIconChanged(int model_index) { | |
| 426 // Translate the model_index to the menu_index. | |
| 427 int first_item_index_ = model_->GetFirstItemIndex(GetNativeMenu()); | |
| 428 int menu_index = model_index + first_item_index_; | |
| 429 // Unfortunately this repaints the entire menu. | |
| 430 SetMenuItemState(menu_index, model_->IsEnabledAt(model_index), | |
| 431 model_->IsItemCheckedAt(model_index), false); | |
| 432 } | |
| 433 | |
| 419 //////////////////////////////////////////////////////////////////////////////// | 434 //////////////////////////////////////////////////////////////////////////////// |
| 420 // NativeMenuWin, private: | 435 // NativeMenuWin, private: |
| 421 | 436 |
| 422 // static | 437 // static |
| 423 NativeMenuWin* NativeMenuWin::open_native_menu_win_ = NULL; | 438 NativeMenuWin* NativeMenuWin::open_native_menu_win_ = NULL; |
| 424 | 439 |
| 425 // static | 440 // static |
| 426 bool NativeMenuWin::GetHighlightedMenuItemInfo( | 441 bool NativeMenuWin::GetHighlightedMenuItemInfo( |
| 427 HMENU menu, bool* has_parent, bool* has_submenu) { | 442 HMENU menu, bool* has_parent, bool* has_submenu) { |
| 428 for (int i = 0; i < ::GetMenuItemCount(menu); i++) { | 443 for (int i = 0; i < ::GetMenuItemCount(menu); i++) { |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 635 | 650 |
| 636 //////////////////////////////////////////////////////////////////////////////// | 651 //////////////////////////////////////////////////////////////////////////////// |
| 637 // MenuWrapper, public: | 652 // MenuWrapper, public: |
| 638 | 653 |
| 639 // static | 654 // static |
| 640 MenuWrapper* MenuWrapper::CreateWrapper(Menu2* menu) { | 655 MenuWrapper* MenuWrapper::CreateWrapper(Menu2* menu) { |
| 641 return new NativeMenuWin(menu->model(), NULL); | 656 return new NativeMenuWin(menu->model(), NULL); |
| 642 } | 657 } |
| 643 | 658 |
| 644 } // namespace views | 659 } // namespace views |
| OLD | NEW |