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/window_impl.h" | 5 #include "ui/base/win/window_impl.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
9 #include "base/debug/alias.h" | 9 #include "base/debug/alias.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 icon(icon) {} | 37 icon(icon) {} |
38 | 38 |
39 // Compares two ClassInfos. Returns true if all members match. | 39 // Compares two ClassInfos. Returns true if all members match. |
40 bool Equals(const ClassInfo& other) const { | 40 bool Equals(const ClassInfo& other) const { |
41 return (other.style == style && other.icon == icon); | 41 return (other.style == style && other.icon == icon); |
42 } | 42 } |
43 }; | 43 }; |
44 | 44 |
45 class ClassRegistrar { | 45 class ClassRegistrar { |
46 public: | 46 public: |
47 static ClassRegistrar* GetInstance() { | 47 ~ClassRegistrar(); |
48 return Singleton<ClassRegistrar>::get(); | |
49 } | |
50 | 48 |
51 ~ClassRegistrar() { | 49 static ClassRegistrar* GetInstance(); |
52 for (RegisteredClasses::iterator i = registered_classes_.begin(); | |
53 i != registered_classes_.end(); ++i) { | |
54 if (!UnregisterClass(MAKEINTATOM(i->atom), i->instance)) { | |
55 LOG(ERROR) << "Failed to unregister class " << i->name.c_str() | |
56 << ". Error = " << GetLastError(); | |
57 } | |
58 } | |
59 } | |
60 | 50 |
61 // Puts the name for the class matching |class_info| in |class_name|, creating | 51 // Returns the atom identifying the class matching |class_info|, |
62 // a new name if the class is not yet known. | 52 // creating and registering a new class if the class is not yet known. |
63 // Returns true if this class was already known, false otherwise. | 53 ATOM RetrieveClassAtom(const ClassInfo& class_info); |
64 bool RetrieveClassName(const ClassInfo& class_info, std::wstring* name) { | |
65 for (RegisteredClasses::const_iterator i = registered_classes_.begin(); | |
66 i != registered_classes_.end(); ++i) { | |
67 if (class_info.Equals(i->info)) { | |
68 name->assign(i->name); | |
69 return true; | |
70 } | |
71 } | |
72 | |
73 name->assign(string16(WindowImpl::kBaseClassName) + | |
74 base::IntToString16(registered_count_++)); | |
75 return false; | |
76 } | |
77 | |
78 void RegisterClass(const ClassInfo& class_info, | |
79 const std::wstring& name, | |
80 ATOM atom, | |
81 HMODULE instance) { | |
82 registered_classes_.push_back( | |
83 RegisteredClass(class_info, name, atom, instance)); | |
84 } | |
85 | 54 |
86 private: | 55 private: |
87 // Represents a registered window class. | 56 // Represents a registered window class. |
88 struct RegisteredClass { | 57 struct RegisteredClass { |
89 RegisteredClass(const ClassInfo& info, | 58 RegisteredClass(const ClassInfo& info, ATOM atom); |
90 const std::wstring& name, | |
91 ATOM atom, | |
92 HMODULE instance) | |
93 : info(info), | |
94 name(name), | |
95 atom(atom), | |
96 instance(instance) { | |
97 } | |
98 | 59 |
99 // Info used to create the class. | 60 // Info used to create the class. |
100 ClassInfo info; | 61 ClassInfo info; |
101 | 62 |
102 // The name given to the window class. | 63 // The atom identifying the window class. |
103 std::wstring name; | |
104 | |
105 // The ATOM returned from registering the window class. | |
106 ATOM atom; | 64 ATOM atom; |
107 | |
108 // The handle of the module containing the window procedure. | |
109 HMODULE instance; | |
110 }; | 65 }; |
111 | 66 |
112 ClassRegistrar() : registered_count_(0) { } | 67 ClassRegistrar(); |
113 friend struct DefaultSingletonTraits<ClassRegistrar>; | 68 friend struct DefaultSingletonTraits<ClassRegistrar>; |
114 | 69 |
115 typedef std::list<RegisteredClass> RegisteredClasses; | 70 typedef std::list<RegisteredClass> RegisteredClasses; |
116 RegisteredClasses registered_classes_; | 71 RegisteredClasses registered_classes_; |
117 | 72 |
118 // Counter of how many classes have been registered so far. | 73 // Counter of how many classes have been registered so far. |
119 int registered_count_; | 74 int registered_count_; |
120 | 75 |
121 DISALLOW_COPY_AND_ASSIGN(ClassRegistrar); | 76 DISALLOW_COPY_AND_ASSIGN(ClassRegistrar); |
122 }; | 77 }; |
123 | 78 |
| 79 ClassRegistrar::~ClassRegistrar() {} |
| 80 |
| 81 // static |
| 82 ClassRegistrar* ClassRegistrar::GetInstance() { |
| 83 return Singleton<ClassRegistrar, |
| 84 LeakySingletonTraits<ClassRegistrar> >::get(); |
| 85 } |
| 86 |
| 87 ATOM ClassRegistrar::RetrieveClassAtom(const ClassInfo& class_info) { |
| 88 for (RegisteredClasses::const_iterator i = registered_classes_.begin(); |
| 89 i != registered_classes_.end(); ++i) { |
| 90 if (class_info.Equals(i->info)) |
| 91 return i->atom; |
| 92 } |
| 93 |
| 94 // No class found, need to register one. |
| 95 string16 name = string16(WindowImpl::kBaseClassName) + |
| 96 base::IntToString16(registered_count_++); |
| 97 |
| 98 HBRUSH background = NULL; |
| 99 WNDCLASSEX window_class; |
| 100 base::win::InitializeWindowClass( |
| 101 name.c_str(), |
| 102 &base::win::WrappedWindowProc<WindowImpl::WndProc>, |
| 103 class_info.style, |
| 104 0, |
| 105 0, |
| 106 NULL, |
| 107 reinterpret_cast<HBRUSH>(background + 1), |
| 108 NULL, |
| 109 class_info.icon, |
| 110 class_info.icon, |
| 111 &window_class); |
| 112 HMODULE instance = window_class.hInstance; |
| 113 ATOM atom = RegisterClassEx(&window_class); |
| 114 CHECK(atom) << GetLastError(); |
| 115 |
| 116 registered_classes_.push_back(RegisteredClass(class_info, atom)); |
| 117 |
| 118 return atom; |
| 119 } |
| 120 |
| 121 ClassRegistrar::RegisteredClass::RegisteredClass(const ClassInfo& info, |
| 122 ATOM atom) |
| 123 : info(info), |
| 124 atom(atom) {} |
| 125 |
| 126 ClassRegistrar::ClassRegistrar() : registered_count_(0) { } |
| 127 |
| 128 |
124 /////////////////////////////////////////////////////////////////////////////// | 129 /////////////////////////////////////////////////////////////////////////////// |
125 // WindowImpl, public | 130 // WindowImpl, public |
126 | 131 |
127 WindowImpl::WindowImpl() | 132 WindowImpl::WindowImpl() |
128 : window_style_(0), | 133 : window_style_(0), |
129 window_ex_style_(kWindowDefaultExStyle), | 134 window_ex_style_(kWindowDefaultExStyle), |
130 class_style_(CS_DBLCLKS), | 135 class_style_(CS_DBLCLKS), |
131 hwnd_(NULL), | 136 hwnd_(NULL), |
132 got_create_(false), | 137 got_create_(false), |
133 got_valid_hwnd_(false), | 138 got_valid_hwnd_(false), |
(...skipping 24 matching lines...) Expand all Loading... |
158 int x, y, width, height; | 163 int x, y, width, height; |
159 if (bounds.IsEmpty()) { | 164 if (bounds.IsEmpty()) { |
160 x = y = width = height = CW_USEDEFAULT; | 165 x = y = width = height = CW_USEDEFAULT; |
161 } else { | 166 } else { |
162 x = bounds.x(); | 167 x = bounds.x(); |
163 y = bounds.y(); | 168 y = bounds.y(); |
164 width = bounds.width(); | 169 width = bounds.width(); |
165 height = bounds.height(); | 170 height = bounds.height(); |
166 } | 171 } |
167 | 172 |
168 std::wstring name(GetWindowClassName()); | 173 ATOM atom = GetWindowClassAtom(); |
169 bool destroyed = false; | 174 bool destroyed = false; |
170 destroyed_ = &destroyed; | 175 destroyed_ = &destroyed; |
171 HWND hwnd = CreateWindowEx(window_ex_style_, name.c_str(), NULL, | 176 HWND hwnd = CreateWindowEx(window_ex_style_, |
| 177 reinterpret_cast<wchar_t*>(atom), NULL, |
172 window_style_, x, y, width, height, | 178 window_style_, x, y, width, height, |
173 parent, NULL, NULL, this); | 179 parent, NULL, NULL, this); |
174 if (!hwnd_ && GetLastError() == 0) { | 180 if (!hwnd_ && GetLastError() == 0) { |
175 base::debug::Alias(&destroyed); | 181 base::debug::Alias(&destroyed); |
176 base::debug::Alias(&hwnd); | 182 base::debug::Alias(&hwnd); |
177 bool got_create = got_create_; | 183 bool got_create = got_create_; |
178 base::debug::Alias(&got_create); | 184 base::debug::Alias(&got_create); |
179 bool got_valid_hwnd = got_valid_hwnd_; | 185 bool got_valid_hwnd = got_valid_hwnd_; |
180 base::debug::Alias(&got_valid_hwnd); | 186 base::debug::Alias(&got_valid_hwnd); |
181 WNDCLASSEX class_info; | 187 WNDCLASSEX class_info; |
182 memset(&class_info, 0, sizeof(WNDCLASSEX)); | 188 memset(&class_info, 0, sizeof(WNDCLASSEX)); |
183 class_info.cbSize = sizeof(WNDCLASSEX); | 189 class_info.cbSize = sizeof(WNDCLASSEX); |
184 BOOL got_class = GetClassInfoEx( | 190 BOOL got_class = GetClassInfoEx(GetModuleHandle(NULL), |
185 GetModuleHandle(NULL), name.c_str(), &class_info); | 191 reinterpret_cast<wchar_t*>(atom), |
| 192 &class_info); |
186 base::debug::Alias(&got_class); | 193 base::debug::Alias(&got_class); |
187 bool procs_match = got_class && class_info.lpfnWndProc == | 194 bool procs_match = got_class && class_info.lpfnWndProc == |
188 base::win::WrappedWindowProc<&WindowImpl::WndProc>; | 195 base::win::WrappedWindowProc<&WindowImpl::WndProc>; |
189 base::debug::Alias(&procs_match); | 196 base::debug::Alias(&procs_match); |
190 CHECK(false); | 197 CHECK(false); |
191 } | 198 } |
192 if (!destroyed) | 199 if (!destroyed) |
193 destroyed_ = NULL; | 200 destroyed_ = NULL; |
194 | 201 |
195 CheckWindowCreated(hwnd_); | 202 CheckWindowCreated(hwnd_); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 } | 243 } |
237 | 244 |
238 WindowImpl* window = reinterpret_cast<WindowImpl*>( | 245 WindowImpl* window = reinterpret_cast<WindowImpl*>( |
239 ui::GetWindowUserData(hwnd)); | 246 ui::GetWindowUserData(hwnd)); |
240 if (!window) | 247 if (!window) |
241 return 0; | 248 return 0; |
242 | 249 |
243 return window->OnWndProc(message, w_param, l_param); | 250 return window->OnWndProc(message, w_param, l_param); |
244 } | 251 } |
245 | 252 |
246 std::wstring WindowImpl::GetWindowClassName() { | 253 ATOM WindowImpl::GetWindowClassAtom() { |
247 HICON icon = GetDefaultWindowIcon(); | 254 HICON icon = GetDefaultWindowIcon(); |
248 ClassInfo class_info(initial_class_style(), icon); | 255 ClassInfo class_info(initial_class_style(), icon); |
249 std::wstring name; | 256 return ClassRegistrar::GetInstance()->RetrieveClassAtom(class_info); |
250 if (ClassRegistrar::GetInstance()->RetrieveClassName(class_info, &name)) | |
251 return name; | |
252 | |
253 // No class found, need to register one. | |
254 HBRUSH background = NULL; | |
255 WNDCLASSEX window_class; | |
256 base::win::InitializeWindowClass( | |
257 name.c_str(), | |
258 &base::win::WrappedWindowProc<WindowImpl::WndProc>, | |
259 class_info.style, | |
260 0, | |
261 0, | |
262 NULL, | |
263 reinterpret_cast<HBRUSH>(background + 1), | |
264 NULL, | |
265 icon, | |
266 icon, | |
267 &window_class); | |
268 HMODULE instance = window_class.hInstance; | |
269 ATOM atom = RegisterClassEx(&window_class); | |
270 CHECK(atom) << GetLastError(); | |
271 | |
272 ClassRegistrar::GetInstance()->RegisterClass( | |
273 class_info, name, atom, instance); | |
274 | |
275 return name; | |
276 } | 257 } |
277 | 258 |
278 } // namespace ui | 259 } // namespace ui |
OLD | NEW |