• 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_GFX_WIN_WINDOW_IMPL_H_
6 #define UI_GFX_WIN_WINDOW_IMPL_H_
7 
8 #include <string>
9 
10 #include "base/logging.h"
11 #include "ui/gfx/gfx_export.h"
12 #include "ui/gfx/native_widget_types.h"
13 #include "ui/gfx/rect.h"
14 #include "ui/gfx/win/msg_util.h"
15 
16 namespace gfx {
17 
18 // An interface implemented by classes that use message maps.
19 // ProcessWindowMessage is implemented by the BEGIN_MESSAGE_MAP_EX macro.
20 class MessageMapInterface {
21  public:
22   // Processes one message from the window's message queue.
23   virtual BOOL ProcessWindowMessage(HWND window,
24                                     UINT message,
25                                     WPARAM w_param,
26                                     LPARAM l_param,
27                                     LRESULT& result,
28                                     DWORD msg_map_id = 0) = 0;
29 };
30 
31 ///////////////////////////////////////////////////////////////////////////////
32 //
33 // WindowImpl
34 //  A convenience class that encapsulates the details of creating and
35 //  destroying a HWND.  This class also hosts the windows procedure used by all
36 //  Windows.
37 //
38 ///////////////////////////////////////////////////////////////////////////////
39 class GFX_EXPORT WindowImpl : public MessageMapInterface {
40  public:
41   WindowImpl();
42   virtual ~WindowImpl();
43 
44   // Causes all generated windows classes to be unregistered at exit.
45   // This can cause result in errors for tests that don't destroy all instances
46   // of windows, but is necessary if the tests unload the classes WndProc.
47   static void UnregisterClassesAtExit();
48 
49   // Initializes the Window with a parent and an initial desired size.
50   void Init(HWND parent, const gfx::Rect& bounds);
51 
52   // Returns the default window icon to use for windows of this type.
53   virtual HICON GetDefaultWindowIcon() const;
54 
55   // Returns the HWND associated with this Window.
hwnd()56   HWND hwnd() const { return hwnd_; }
57 
58   // Sets the window styles. This is ONLY used when the window is created.
59   // In other words, if you invoke this after invoking Init, nothing happens.
set_window_style(DWORD style)60   void set_window_style(DWORD style) { window_style_ = style; }
window_style()61   DWORD window_style() const { return window_style_; }
62 
63   // Sets the extended window styles. See comment about |set_window_style|.
set_window_ex_style(DWORD style)64   void set_window_ex_style(DWORD style) { window_ex_style_ = style; }
window_ex_style()65   DWORD window_ex_style() const { return window_ex_style_; }
66 
67   // Sets the class style to use. The default is CS_DBLCLKS.
set_initial_class_style(UINT class_style)68   void set_initial_class_style(UINT class_style) {
69     // We dynamically generate the class name, so don't register it globally!
70     DCHECK_EQ((class_style & CS_GLOBALCLASS), 0u);
71     class_style_ = class_style;
72   }
initial_class_style()73   UINT initial_class_style() const { return class_style_; }
74 
75  protected:
76   // Handles the WndProc callback for this object.
77   virtual LRESULT OnWndProc(UINT message, WPARAM w_param, LPARAM l_param);
78 
79   // Subclasses must call this method from their destructors to ensure that
80   // this object is properly disassociated from the HWND during destruction,
81   // otherwise it's possible this object may still exist while a subclass is
82   // destroyed.
83   void ClearUserData();
84 
85  private:
86   friend class ClassRegistrar;
87 
88   // The window procedure used by all Windows.
89   static LRESULT CALLBACK WndProc(HWND window,
90                                   UINT message,
91                                   WPARAM w_param,
92                                   LPARAM l_param);
93 
94   // Gets the window class atom to use when creating the corresponding HWND.
95   // If necessary, this registers the window class.
96   ATOM GetWindowClassAtom();
97 
98   // All classes registered by WindowImpl start with this name.
99   static const wchar_t* const kBaseClassName;
100 
101   // Window Styles used when creating the window.
102   DWORD window_style_;
103 
104   // Window Extended Styles used when creating the window.
105   DWORD window_ex_style_;
106 
107   // Style of the class to use.
108   UINT class_style_;
109 
110   // Our hwnd.
111   HWND hwnd_;
112 
113   // For debugging.
114   // TODO(sky): nuke this when get crash data.
115   bool got_create_;
116   bool got_valid_hwnd_;
117   bool* destroyed_;
118 
119   DISALLOW_COPY_AND_ASSIGN(WindowImpl);
120 };
121 
122 }  // namespace gfx
123 
124 #endif  // UI_GFX_WIN_WINDOW_IMPL_H_
125