• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef UI_BASE_WIN_HWND_SUBCLASS_H_
6 #define UI_BASE_WIN_HWND_SUBCLASS_H_
7 
8 #include <windows.h>
9 #include <vector>
10 
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"
15 
16 namespace ui {
17 
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 {
21  public:
22   virtual ~HWNDMessageFilter();
23 
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,
33                              UINT message,
34                              WPARAM w_param,
35                              LPARAM l_param,
36                              LRESULT* l_result) = 0;
37 };
38 
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 {
42  public:
43   ~HWNDSubclass();
44 
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);
49 
50   // Removes |filter| from any HWNDSubclass that has it.
51   static void RemoveFilterFromAllTargets(HWNDMessageFilter* filter);
52 
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);
56 
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);
61 
62   // Removes |filter|  from this HWNDSubclass instance if present.
63   void RemoveFilter(HWNDMessageFilter* filter);
64 
65   LRESULT OnWndProc(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param);
66 
67  private:
68   class HWNDSubclassFactory;
69   friend class HWNDSubclassFactory;
70 
71   explicit HWNDSubclass(HWND target);
72 
73   HWND target_;
74   std::vector<HWNDMessageFilter*> filters_;
75   WNDPROC original_wnd_proc_;
76   ui::ViewProp prop_;
77 
78   DISALLOW_COPY_AND_ASSIGN(HWNDSubclass);
79 };
80 
81 }  // namespace ui
82 
83 #endif  // UI_BASE_WIN_HWND_SUBCLASS_H_
84