OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/panels/panel_mouse_watcher_win.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/scoped_ptr.h" |
| 9 #include "chrome/browser/ui/panels/panel.h" |
| 10 #include "chrome/browser/ui/panels/panel_manager.h" |
| 11 |
| 12 #include <windows.h> |
| 13 |
| 14 namespace { |
| 15 |
| 16 HMODULE GetModuleHandleFromAddress(void *address) { |
| 17 MEMORY_BASIC_INFORMATION mbi; |
| 18 SIZE_T result = ::VirtualQuery(address, &mbi, sizeof(mbi)); |
| 19 return static_cast<HMODULE>(mbi.AllocationBase); |
| 20 } |
| 21 |
| 22 |
| 23 // Gets the handle to the currently executing module. |
| 24 HMODULE GetCurrentModuleHandle() { |
| 25 return ::GetModuleHandleFromAddress(GetCurrentModuleHandle); |
| 26 } |
| 27 |
| 28 class PanelMouseWatcherWin { |
| 29 public: |
| 30 PanelMouseWatcherWin(); |
| 31 ~PanelMouseWatcherWin(); |
| 32 |
| 33 private: |
| 34 static LRESULT CALLBACK MouseHookProc(int code, WPARAM wparam, LPARAM lparam); |
| 35 |
| 36 void OnMouseAction(int mouse_x, int mouse_y); |
| 37 |
| 38 HHOOK mouse_hook_; |
| 39 bool bring_up_titlebar_; |
| 40 |
| 41 DISALLOW_COPY_AND_ASSIGN(PanelMouseWatcherWin); |
| 42 }; |
| 43 |
| 44 scoped_ptr<PanelMouseWatcherWin> mouse_watcher; |
| 45 |
| 46 PanelMouseWatcherWin::PanelMouseWatcherWin() |
| 47 : mouse_hook_(NULL), |
| 48 bring_up_titlebar_(false) { |
| 49 mouse_hook_ = ::SetWindowsHookEx( |
| 50 WH_MOUSE_LL, MouseHookProc, GetCurrentModuleHandle(), 0); |
| 51 DCHECK(mouse_hook_); |
| 52 } |
| 53 |
| 54 PanelMouseWatcherWin::~PanelMouseWatcherWin() { |
| 55 ::UnhookWindowsHookEx(mouse_hook_); |
| 56 } |
| 57 |
| 58 LRESULT CALLBACK PanelMouseWatcherWin::MouseHookProc(int code, |
| 59 WPARAM wparam, |
| 60 LPARAM lparam) { |
| 61 if (code == HC_ACTION) { |
| 62 MOUSEHOOKSTRUCT* hook_struct = reinterpret_cast<MOUSEHOOKSTRUCT*>(lparam); |
| 63 if (hook_struct) |
| 64 mouse_watcher->OnMouseAction(hook_struct->pt.x, hook_struct->pt.y); |
| 65 } |
| 66 return ::CallNextHookEx(NULL, code, wparam, lparam); |
| 67 } |
| 68 |
| 69 void PanelMouseWatcherWin::OnMouseAction(int mouse_x, int mouse_y) { |
| 70 PanelManager* panel_manager = PanelManager::GetInstance(); |
| 71 |
| 72 bool bring_up_titlebar = |
| 73 panel_manager->ShouldBringUpTitleBarForAllMinimizedPanels( |
| 74 mouse_x, mouse_y); |
| 75 if (bring_up_titlebar == bring_up_titlebar_) |
| 76 return; |
| 77 bring_up_titlebar_ = bring_up_titlebar; |
| 78 |
| 79 panel_manager->BringUpOrDownTitleBarForAllMinimizedPanels(bring_up_titlebar); |
| 80 } |
| 81 |
| 82 } |
| 83 |
| 84 void EnsureMouseWatcherStarted() { |
| 85 if (!mouse_watcher.get()) { |
| 86 mouse_watcher.reset( |
| 87 new PanelMouseWatcherWin()); |
| 88 } |
| 89 } |
| 90 |
| 91 void StopMouseWatcher() { |
| 92 mouse_watcher.reset(NULL); |
| 93 } |
OLD | NEW |