OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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" | |
9 #include "base/stl_util-inl.h" | 8 #include "base/stl_util-inl.h" |
10 #include "base/task.h" | |
11 #include "gfx/canvas_skia.h" | 9 #include "gfx/canvas_skia.h" |
12 #include "gfx/font.h" | 10 #include "gfx/font.h" |
13 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
14 #include "ui/base/keycodes/keyboard_codes.h" | 12 #include "ui/base/keycodes/keyboard_codes.h" |
15 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
16 #include "ui/base/l10n/l10n_util_win.h" | 14 #include "ui/base/l10n/l10n_util_win.h" |
17 #include "ui/base/win/hwnd_util.h" | 15 #include "ui/base/win/hwnd_util.h" |
18 #include "views/accelerator.h" | 16 #include "views/accelerator.h" |
19 #include "views/controls/menu/menu_2.h" | 17 #include "views/controls/menu/menu_2.h" |
20 | 18 |
(...skipping 26 matching lines...) Expand all Loading... |
47 NativeMenuWin* native_menu_win; | 45 NativeMenuWin* native_menu_win; |
48 | 46 |
49 // The index of the item within the menu's model. | 47 // The index of the item within the menu's model. |
50 int model_index; | 48 int model_index; |
51 }; | 49 }; |
52 | 50 |
53 // A window that receives messages from Windows relevant to the native menu | 51 // A window that receives messages from Windows relevant to the native menu |
54 // structure we have constructed in NativeMenuWin. | 52 // structure we have constructed in NativeMenuWin. |
55 class NativeMenuWin::MenuHostWindow { | 53 class NativeMenuWin::MenuHostWindow { |
56 public: | 54 public: |
57 MenuHostWindow(NativeMenuWin* parent) | 55 MenuHostWindow(NativeMenuWin* parent) : parent_(parent) { |
58 : parent_(parent), | |
59 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | |
60 RegisterClass(); | 56 RegisterClass(); |
61 hwnd_ = CreateWindowEx(l10n_util::GetExtendedStyles(), kWindowClassName, | 57 hwnd_ = CreateWindowEx(l10n_util::GetExtendedStyles(), kWindowClassName, |
62 L"", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL); | 58 L"", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL); |
63 ui::SetWindowUserData(hwnd_, this); | 59 ui::SetWindowUserData(hwnd_, this); |
64 } | 60 } |
65 | 61 |
66 ~MenuHostWindow() { | 62 ~MenuHostWindow() { |
67 DestroyWindow(hwnd_); | 63 DestroyWindow(hwnd_); |
68 } | 64 } |
69 | 65 |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 // Draw the separator | 236 // Draw the separator |
241 draw_item_struct->rcItem.top += | 237 draw_item_struct->rcItem.top += |
242 (draw_item_struct->rcItem.bottom - draw_item_struct->rcItem.top) / 3; | 238 (draw_item_struct->rcItem.bottom - draw_item_struct->rcItem.top) / 3; |
243 DrawEdge(dc, &draw_item_struct->rcItem, EDGE_ETCHED, BF_TOP); | 239 DrawEdge(dc, &draw_item_struct->rcItem, EDGE_ETCHED, BF_TOP); |
244 } | 240 } |
245 | 241 |
246 SetBkColor(dc, prev_bg_color); | 242 SetBkColor(dc, prev_bg_color); |
247 SetTextColor(dc, prev_text_color); | 243 SetTextColor(dc, prev_text_color); |
248 } | 244 } |
249 | 245 |
250 void OnMenuClosed() { | |
251 parent_->model_->MenuClosed(); | |
252 } | |
253 | |
254 bool ProcessWindowMessage(HWND window, | 246 bool ProcessWindowMessage(HWND window, |
255 UINT message, | 247 UINT message, |
256 WPARAM w_param, | 248 WPARAM w_param, |
257 LPARAM l_param, | 249 LPARAM l_param, |
258 LRESULT* l_result) { | 250 LRESULT* l_result) { |
259 switch (message) { | 251 switch (message) { |
260 case WM_MENUCOMMAND: | 252 case WM_MENUCOMMAND: |
261 OnMenuCommand(w_param, reinterpret_cast<HMENU>(l_param)); | 253 OnMenuCommand(w_param, reinterpret_cast<HMENU>(l_param)); |
262 *l_result = 0; | 254 *l_result = 0; |
263 return true; | 255 return true; |
264 case WM_MENUSELECT: | 256 case WM_MENUSELECT: |
265 OnMenuSelect(LOWORD(w_param), reinterpret_cast<HMENU>(l_param)); | 257 OnMenuSelect(LOWORD(w_param), reinterpret_cast<HMENU>(l_param)); |
266 *l_result = 0; | 258 *l_result = 0; |
267 return true; | 259 return true; |
268 case WM_MEASUREITEM: | 260 case WM_MEASUREITEM: |
269 OnMeasureItem(w_param, reinterpret_cast<MEASUREITEMSTRUCT*>(l_param)); | 261 OnMeasureItem(w_param, reinterpret_cast<MEASUREITEMSTRUCT*>(l_param)); |
270 *l_result = 0; | 262 *l_result = 0; |
271 return true; | 263 return true; |
272 case WM_DRAWITEM: | 264 case WM_DRAWITEM: |
273 OnDrawItem(w_param, reinterpret_cast<DRAWITEMSTRUCT*>(l_param)); | 265 OnDrawItem(w_param, reinterpret_cast<DRAWITEMSTRUCT*>(l_param)); |
274 *l_result = 0; | 266 *l_result = 0; |
275 return true; | 267 return true; |
276 case WM_EXITMENULOOP: | 268 case WM_EXITMENULOOP: |
277 // WM_MENUCOMMAND comes after this message, but still in the same | 269 parent_->model_->MenuClosed(); |
278 // callstack. So use PostTask to guarantee that we'll tell the model | |
279 // that the menus is closed after any other notifications. | |
280 MessageLoop::current()->PostTask( | |
281 FROM_HERE, | |
282 method_factory_.NewRunnableMethod(&MenuHostWindow::OnMenuClosed)); | |
283 return true; | 270 return true; |
284 // TODO(beng): bring over owner draw from old menu system. | 271 // TODO(beng): bring over owner draw from old menu system. |
285 } | 272 } |
286 return false; | 273 return false; |
287 } | 274 } |
288 | 275 |
289 static LRESULT CALLBACK MenuHostWindowProc(HWND window, | 276 static LRESULT CALLBACK MenuHostWindowProc(HWND window, |
290 UINT message, | 277 UINT message, |
291 WPARAM w_param, | 278 WPARAM w_param, |
292 LPARAM l_param) { | 279 LPARAM l_param) { |
293 MenuHostWindow* host = | 280 MenuHostWindow* host = |
294 reinterpret_cast<MenuHostWindow*>(ui::GetWindowUserData(window)); | 281 reinterpret_cast<MenuHostWindow*>(ui::GetWindowUserData(window)); |
295 // host is null during initial construction. | 282 // host is null during initial construction. |
296 LRESULT l_result = 0; | 283 LRESULT l_result = 0; |
297 if (!host || !host->ProcessWindowMessage(window, message, w_param, l_param, | 284 if (!host || !host->ProcessWindowMessage(window, message, w_param, l_param, |
298 &l_result)) { | 285 &l_result)) { |
299 return DefWindowProc(window, message, w_param, l_param); | 286 return DefWindowProc(window, message, w_param, l_param); |
300 } | 287 } |
301 return l_result; | 288 return l_result; |
302 } | 289 } |
303 | 290 |
304 HWND hwnd_; | 291 HWND hwnd_; |
305 NativeMenuWin* parent_; | 292 NativeMenuWin* parent_; |
306 ScopedRunnableMethodFactory<MenuHostWindow> method_factory_; | |
307 | 293 |
308 DISALLOW_COPY_AND_ASSIGN(MenuHostWindow); | 294 DISALLOW_COPY_AND_ASSIGN(MenuHostWindow); |
309 }; | 295 }; |
310 | 296 |
311 // static | 297 // static |
312 const wchar_t* NativeMenuWin::MenuHostWindow::kWindowClassName = | 298 const wchar_t* NativeMenuWin::MenuHostWindow::kWindowClassName = |
313 L"ViewsMenuHostWindow"; | 299 L"ViewsMenuHostWindow"; |
314 | 300 |
315 //////////////////////////////////////////////////////////////////////////////// | 301 //////////////////////////////////////////////////////////////////////////////// |
316 // NativeMenuWin, public: | 302 // NativeMenuWin, public: |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
644 | 630 |
645 //////////////////////////////////////////////////////////////////////////////// | 631 //////////////////////////////////////////////////////////////////////////////// |
646 // MenuWrapper, public: | 632 // MenuWrapper, public: |
647 | 633 |
648 // static | 634 // static |
649 MenuWrapper* MenuWrapper::CreateWrapper(Menu2* menu) { | 635 MenuWrapper* MenuWrapper::CreateWrapper(Menu2* menu) { |
650 return new NativeMenuWin(menu->model(), NULL); | 636 return new NativeMenuWin(menu->model(), NULL); |
651 } | 637 } |
652 | 638 |
653 } // namespace views | 639 } // namespace views |
OLD | NEW |