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