Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <atlbase.h> | 5 #ifndef WEBKIT_TOOLS_TEST_SHELL_FOREGROUND_HELPER_H_ |
| 6 #include <atlwin.h> | 6 #define WEBKIT_TOOLS_TEST_SHELL_FOREGROUND_HELPER_H_ |
| 7 | |
| 8 #include <atlcrack.h> | |
|
Peter Kasting
2009/08/08 00:33:49
Nit: Don't think you need this
| |
| 7 | 9 |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/window_impl.h" | |
| 9 | 12 |
| 10 // Helper class for moving a window to the foreground. | 13 // Helper class for moving a window to the foreground. |
| 11 // Windows XP and later will not allow a window which is in the background to | 14 // Windows XP and later will not allow a window which is in the background to |
| 12 // move to the foreground, unless requested by the current window in the | 15 // move to the foreground, unless requested by the current window in the |
| 13 // foreground. For automated testing, we really want some of our windows | 16 // foreground. For automated testing, we really want some of our windows |
| 14 // to be capable of moving to the foreground. | 17 // to be capable of moving to the foreground. |
| 15 // | 18 // |
| 16 // This is probably leveraging a windows bug. | 19 // This is probably leveraging a windows bug. |
| 17 class ForegroundHelper : public CWindowImpl<ForegroundHelper> { | 20 class ForegroundHelper : public base::WindowImpl { |
| 18 public: | 21 public: |
| 19 BEGIN_MSG_MAP(ForegroundHelper) | 22 BEGIN_MSG_MAP_EX(ForegroundHelper) |
| 20 MESSAGE_HANDLER(WM_HOTKEY, OnHotKey) | 23 MESSAGE_HANDLER(WM_HOTKEY, OnHotKey) |
| 21 END_MSG_MAP() | 24 END_MSG_MAP() |
| 22 | 25 |
| 23 // Brings a window into the foreground. | 26 // Brings a window into the foreground. |
| 24 // Can be called from any window, even if the caller is not the | 27 // Can be called from any window, even if the caller is not the |
| 25 // foreground window. | 28 // foreground window. |
| 26 static HRESULT SetForeground(HWND window) { | 29 static HRESULT SetForeground(HWND window) { |
| 27 DCHECK(::IsWindow(window)); | 30 DCHECK(::IsWindow(window)); |
| 28 ForegroundHelper foreground_helper; | 31 ForegroundHelper foreground_helper; |
| 29 CHECK(foreground_helper.ForegroundHotKey(window) == S_OK); | 32 CHECK(foreground_helper.ForegroundHotKey(window) == S_OK); |
| 30 return S_OK; | 33 return S_OK; |
| 31 } | 34 } |
| 32 | 35 |
| 33 private: | 36 private: |
| 34 HRESULT ForegroundHotKey(HWND window) { | 37 HRESULT ForegroundHotKey(HWND window) { |
| 35 // This implementation registers a hot key (F22) and then | 38 // This implementation registers a hot key (F22) and then |
| 36 // triggers the hot key. When receiving the hot key, we'll | 39 // triggers the hot key. When receiving the hot key, we'll |
| 37 // be in the foreground and allowed to move the target window | 40 // be in the foreground and allowed to move the target window |
| 38 // into the foreground too. | 41 // into the foreground too. |
| 39 | 42 |
| 40 if (NULL == Create(NULL, NULL, NULL, WS_POPUP)) | 43 set_window_ex_style(WS_POPUP); |
| 41 return AtlHresultFromLastError(); | 44 Init(NULL, gfx::Rect()); |
| 42 | 45 |
| 43 static const int hotkey_id = 0x0000baba; | 46 static const int hotkey_id = 0x0000baba; |
| 44 | 47 |
| 45 // Store the target window into our USERDATA for use in our | 48 // Store the target window into our USERDATA for use in our |
| 46 // HotKey handler. | 49 // HotKey handler. |
| 47 SetWindowLongPtr(GWLP_USERDATA, reinterpret_cast<ULONG_PTR>(window)); | 50 SetWindowLongPtr(GetNativeView(), GWLP_USERDATA, |
| 48 RegisterHotKey(m_hWnd, hotkey_id, 0, VK_F22); | 51 reinterpret_cast<ULONG_PTR>(window)); |
| 52 RegisterHotKey(GetNativeView(), hotkey_id, 0, VK_F22); | |
| 49 | 53 |
| 50 // If the calling thread is not yet a UI thread, call PeekMessage | 54 // If the calling thread is not yet a UI thread, call PeekMessage |
| 51 // to ensure creation of its message queue. | 55 // to ensure creation of its message queue. |
| 52 MSG msg = {0}; | 56 MSG msg = {0}; |
| 53 PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE); | 57 PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE); |
| 54 | 58 |
| 55 // Send the Hotkey. | 59 // Send the Hotkey. |
| 56 INPUT hotkey = {0}; | 60 INPUT hotkey = {0}; |
| 57 hotkey.type = INPUT_KEYBOARD; | 61 hotkey.type = INPUT_KEYBOARD; |
| 58 hotkey.ki.wVk = VK_F22; | 62 hotkey.ki.wVk = VK_F22; |
| 59 if (1 != SendInput(1, &hotkey, sizeof(hotkey))) | 63 if (1 != SendInput(1, &hotkey, sizeof(hotkey))) |
| 60 return E_FAIL; | 64 return E_FAIL; |
| 61 | 65 |
| 62 // Loop until we get the key. | 66 // Loop until we get the key. |
| 63 // TODO: It may be possible to get stuck here if the | 67 // TODO: It may be possible to get stuck here if the |
| 64 // message gets lost? | 68 // message gets lost? |
| 65 while (GetMessage(&msg, NULL, 0, 0)) { | 69 while (GetMessage(&msg, NULL, 0, 0)) { |
| 66 TranslateMessage(&msg); | 70 TranslateMessage(&msg); |
| 67 DispatchMessage(&msg); | 71 DispatchMessage(&msg); |
| 68 | 72 |
| 69 if (WM_HOTKEY == msg.message) | 73 if (WM_HOTKEY == msg.message) |
| 70 break; | 74 break; |
| 71 } | 75 } |
| 72 | 76 |
| 73 UnregisterHotKey(m_hWnd, hotkey_id); | 77 UnregisterHotKey(GetNativeView(), hotkey_id); |
| 74 DestroyWindow(); | 78 DestroyWindow(); |
| 75 | 79 |
| 76 return S_OK; | 80 return S_OK; |
| 77 } | 81 } |
| 78 | 82 |
| 79 // Handle the registered Hotkey being pressed. | 83 // Handle the registered Hotkey being pressed. |
| 80 LRESULT OnHotKey(UINT message, | 84 LRESULT OnHotKey(UINT message, |
| 81 WPARAM wparam, | 85 WPARAM wparam, |
| 82 LPARAM lparam, | 86 LPARAM lparam, |
| 83 BOOL& handled) { | 87 BOOL& handled) { |
| 84 HWND window = reinterpret_cast<HWND>(GetWindowLongPtr(GWLP_USERDATA)); | 88 HWND window = reinterpret_cast<HWND>(GetWindowLongPtr(GetNativeView(), |
| 89 GWLP_USERDATA)); | |
| 85 SetForegroundWindow(window); | 90 SetForegroundWindow(window); |
| 86 return 1; | 91 return 1; |
| 87 } | 92 } |
| 88 }; | 93 }; |
| 94 | |
| 95 #endif // WEBKIT_TOOLS_TEST_SHELL_FOREGROUND_HELPER_H_ | |
| OLD | NEW |