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