OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/base/win/hwnd_subclass.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "ui/base/win/hwnd_util.h" |
| 9 |
| 10 namespace { |
| 11 const char kHWNDSubclassKey[] = "__UI_BASE_WIN_HWND_SUBCLASS_PROC__"; |
| 12 |
| 13 LRESULT CALLBACK WndProc(HWND hwnd, |
| 14 UINT message, |
| 15 WPARAM w_param, |
| 16 LPARAM l_param) { |
| 17 ui::HWNDSubclass* wrapped_wnd_proc = |
| 18 reinterpret_cast<ui::HWNDSubclass*>( |
| 19 ui::ViewProp::GetValue(hwnd, kHWNDSubclassKey)); |
| 20 return wrapped_wnd_proc ? wrapped_wnd_proc->OnWndProc(hwnd, |
| 21 message, |
| 22 w_param, |
| 23 l_param) |
| 24 : DefWindowProc(hwnd, message, w_param, l_param); |
| 25 } |
| 26 |
| 27 WNDPROC GetCurrentWndProc(HWND target) { |
| 28 return reinterpret_cast<WNDPROC>(GetWindowLong(target, GWL_WNDPROC)); |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 33 namespace ui { |
| 34 |
| 35 HWNDSubclass::HWNDSubclass(HWND target) |
| 36 : target_(target), |
| 37 original_wnd_proc_(GetCurrentWndProc(target)), |
| 38 ALLOW_THIS_IN_INITIALIZER_LIST(prop_(target, kHWNDSubclassKey, this)) { |
| 39 ui::SetWindowProc(target_, &WndProc); |
| 40 } |
| 41 |
| 42 HWNDSubclass::~HWNDSubclass() { |
| 43 } |
| 44 |
| 45 void HWNDSubclass::SetFilter(HWNDMessageFilter* filter) { |
| 46 filter_.reset(filter); |
| 47 } |
| 48 |
| 49 LRESULT HWNDSubclass::OnWndProc(HWND hwnd, |
| 50 UINT message, |
| 51 WPARAM w_param, |
| 52 LPARAM l_param) { |
| 53 if (filter_.get()) { |
| 54 LRESULT l_result = 0; |
| 55 if (filter_->FilterMessage(hwnd, message, w_param, l_param, &l_result)) |
| 56 return l_result; |
| 57 } |
| 58 |
| 59 // In most cases, |original_wnd_proc_| will take care of calling |
| 60 // DefWindowProc. |
| 61 return CallWindowProc(original_wnd_proc_, hwnd, message, w_param, l_param); |
| 62 } |
| 63 |
| 64 } // namespace ui |
OLD | NEW |