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