OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/base/win/singleton_hwnd.h" | 5 #include "ui/base/win/singleton_hwnd.h" |
6 | 6 |
7 #include "base/memory/singleton.h" | 7 #include "base/memory/singleton.h" |
8 #include "base/process_util.h" | |
9 #include "base/win/wrapped_window_proc.h" | |
10 #include "ui/base/win/hwnd_util.h" | |
11 | |
12 namespace { | |
13 | |
14 // Windows class name to use for the listener window. | |
15 const wchar_t kWindowClassName[] = L"Chrome_SingletonHwnd"; | |
16 | |
17 // Windows callback for listening for WM_* messages. | |
18 LRESULT CALLBACK ListenerWindowProc(HWND hwnd, | |
19 UINT message, | |
20 WPARAM wparam, | |
21 LPARAM lparam) { | |
22 ui::SingletonHwnd::GetInstance()->OnWndProc(hwnd, message, wparam, lparam); | |
23 return ::DefWindowProc(hwnd, message, wparam, lparam); | |
24 } | |
25 | |
26 // Creates a listener window to receive WM_* messages. | |
27 HWND CreateListenerWindow() { | |
28 WNDCLASSEX wc = {0}; | |
29 wc.cbSize = sizeof(wc); | |
30 wc.lpfnWndProc = base::win::WrappedWindowProc<ListenerWindowProc>; | |
31 wc.hInstance = base::GetModuleFromAddress(&ListenerWindowProc); | |
32 wc.lpszClassName = kWindowClassName; | |
33 ATOM window_class = ::RegisterClassEx(&wc); | |
34 DCHECK(window_class); | |
35 | |
36 return ::CreateWindow(MAKEINTATOM(window_class), 0, 0, 0, 0, 0, 0, 0, 0, | |
37 wc.hInstance, 0); | |
38 } | |
39 | |
40 } // namespace | |
41 | 8 |
42 namespace ui { | 9 namespace ui { |
43 | 10 |
44 // static | 11 // static |
45 SingletonHwnd* SingletonHwnd::GetInstance() { | 12 SingletonHwnd* SingletonHwnd::GetInstance() { |
46 return Singleton<SingletonHwnd>::get(); | 13 return Singleton<SingletonHwnd>::get(); |
47 } | 14 } |
48 | 15 |
49 void SingletonHwnd::AddObserver(Observer* observer) { | 16 void SingletonHwnd::AddObserver(Observer* observer) { |
50 if (!listener_window_) { | 17 if (!hwnd()) |
51 listener_window_ = CreateListenerWindow(); | 18 WindowImpl::Init(NULL, gfx::Rect()); |
52 ui::CheckWindowCreated(listener_window_); | |
53 } | |
54 observer_list_.AddObserver(observer); | 19 observer_list_.AddObserver(observer); |
55 } | 20 } |
56 | 21 |
57 void SingletonHwnd::RemoveObserver(Observer* observer) { | 22 void SingletonHwnd::RemoveObserver(Observer* observer) { |
58 observer_list_.RemoveObserver(observer); | 23 observer_list_.RemoveObserver(observer); |
59 } | 24 } |
60 | 25 |
61 void SingletonHwnd::OnWndProc(HWND hwnd, | 26 BOOL SingletonHwnd::ProcessWindowMessage(HWND window, |
62 UINT message, | 27 UINT message, |
63 WPARAM wparam, | 28 WPARAM wparam, |
64 LPARAM lparam) { | 29 LPARAM lparam, |
30 LRESULT& result, | |
31 DWORD msg_map_id) { | |
65 FOR_EACH_OBSERVER(Observer, | 32 FOR_EACH_OBSERVER(Observer, |
66 observer_list_, | 33 observer_list_, |
67 OnWndProc(hwnd, message, wparam, lparam)); | 34 OnWndProc(window, message, wparam, lparam)); |
35 result = ::DefWindowProc(window, message, wparam, lparam); | |
msw
2012/04/02 22:51:03
Can you return false here without explicitly calli
| |
36 return true; | |
68 } | 37 } |
69 | 38 |
70 SingletonHwnd::SingletonHwnd() | 39 SingletonHwnd::SingletonHwnd() { |
71 : listener_window_(NULL) { | |
72 } | 40 } |
73 | 41 |
74 SingletonHwnd::~SingletonHwnd() { | 42 SingletonHwnd::~SingletonHwnd() { |
75 if (listener_window_) { | |
76 ::DestroyWindow(listener_window_); | |
77 ::UnregisterClass(kWindowClassName, GetModuleHandle(NULL)); | |
78 } | |
79 } | 43 } |
80 | 44 |
81 } // namespace ui | 45 } // namespace ui |
OLD | NEW |