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.
5 #ifndef UI_BASE_WIN_HWND_SUBCLASS_H_
6 #define UI_BASE_WIN_HWND_SUBCLASS_H_
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "ui/base/ui_base_export.h"
14 #include "ui/base/view_prop.h"
18 // Classes implementing this interface get the opportunity to handle and consume
19 // messages before they are sent to their target HWND.
20 class UI_BASE_EXPORT HWNDMessageFilter
{
22 virtual ~HWNDMessageFilter();
24 // A derived class overrides this method to perform filtering of the messages.
25 // Return true to prevent other HWNDMessageFilter's of the target HWND and the
26 // system message handler |original_wnd_proc_| from receiving the message.
27 // Return false to propagate the message further to other HWNDMessageFilters
28 // and eventually to |original_wnd_proc|.
29 // The order in which HWNDMessageFilters are added in HWNDSubclass::AddFilter
30 // determines which filter gets to see the message first (a filter added first
31 // will see the message first).
32 virtual bool FilterMessage(HWND hwnd
,
36 LRESULT
* l_result
) = 0;
39 // An object that instance-subclasses a window. If the window has already been
40 // instance-subclassed, that subclassing is lost.
41 class UI_BASE_EXPORT HWNDSubclass
{
45 // Adds |filter| to the HWNDSubclass of |target|. Caller retains ownership of
46 // |filter|. See the comment about the order in which filters are added in
47 // HWNDMessageFilter::FilterMessage.
48 static void AddFilterToTarget(HWND target
, HWNDMessageFilter
* filter
);
50 // Removes |filter| from any HWNDSubclass that has it.
51 static void RemoveFilterFromAllTargets(HWNDMessageFilter
* filter
);
53 // Returns a non-null HWNDSubclass corresponding to the HWND |target|. Creates
54 // one if none exists. Retains ownership of the returned pointer.
55 static HWNDSubclass
* GetHwndSubclassForTarget(HWND target
);
57 // Adds |filter| if not already added to this HWNDSubclass. Caller retains
58 // ownership of |filter|. See the comment about the order in which filters are
59 // added in HWNDMessageFilter::FilterMessage.
60 void AddFilter(HWNDMessageFilter
* filter
);
62 // Removes |filter| from this HWNDSubclass instance if present.
63 void RemoveFilter(HWNDMessageFilter
* filter
);
65 LRESULT
OnWndProc(HWND hwnd
, UINT message
, WPARAM w_param
, LPARAM l_param
);
68 class HWNDSubclassFactory
;
69 friend class HWNDSubclassFactory
;
71 explicit HWNDSubclass(HWND target
);
74 std::vector
<HWNDMessageFilter
*> filters_
;
75 WNDPROC original_wnd_proc_
;
78 DISALLOW_COPY_AND_ASSIGN(HWNDSubclass
);
83 #endif // UI_BASE_WIN_HWND_SUBCLASS_H_