| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 | 44 |
| 45 class ClassRegistrar { | 45 class ClassRegistrar { |
| 46 public: | 46 public: |
| 47 static ClassRegistrar* GetInstance() { | 47 static ClassRegistrar* GetInstance() { |
| 48 return Singleton<ClassRegistrar>::get(); | 48 return Singleton<ClassRegistrar>::get(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 ~ClassRegistrar() { | 51 ~ClassRegistrar() { |
| 52 for (RegisteredClasses::iterator i = registered_classes_.begin(); | 52 for (RegisteredClasses::iterator i = registered_classes_.begin(); |
| 53 i != registered_classes_.end(); ++i) { | 53 i != registered_classes_.end(); ++i) { |
| 54 if (!UnregisterClass(i->name.c_str(), NULL)) { | 54 if (!UnregisterClass(MAKEINTATOM(i->atom), i->instance)) { |
| 55 LOG(ERROR) << "Failed to unregister class " << i->name.c_str() | 55 LOG(ERROR) << "Failed to unregister class " << i->name.c_str() |
| 56 << ". Error = " << GetLastError(); | 56 << ". Error = " << GetLastError(); |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 // Puts the name for the class matching |class_info| in |class_name|, creating | 61 // Puts the name for the class matching |class_info| in |class_name|, creating |
| 62 // a new name if the class is not yet known. | 62 // a new name if the class is not yet known. |
| 63 // Returns true if this class was already known, false otherwise. | 63 // Returns true if this class was already known, false otherwise. |
| 64 bool RetrieveClassName(const ClassInfo& class_info, std::wstring* name) { | 64 bool RetrieveClassName(const ClassInfo& class_info, std::wstring* name) { |
| 65 for (RegisteredClasses::const_iterator i = registered_classes_.begin(); | 65 for (RegisteredClasses::const_iterator i = registered_classes_.begin(); |
| 66 i != registered_classes_.end(); ++i) { | 66 i != registered_classes_.end(); ++i) { |
| 67 if (class_info.Equals(i->info)) { | 67 if (class_info.Equals(i->info)) { |
| 68 name->assign(i->name); | 68 name->assign(i->name); |
| 69 return true; | 69 return true; |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 | 72 |
| 73 name->assign(string16(WindowImpl::kBaseClassName) + | 73 name->assign(string16(WindowImpl::kBaseClassName) + |
| 74 base::IntToString16(registered_count_++)); | 74 base::IntToString16(registered_count_++)); |
| 75 return false; | 75 return false; |
| 76 } | 76 } |
| 77 | 77 |
| 78 void RegisterClass(const ClassInfo& class_info, | 78 void RegisterClass(const ClassInfo& class_info, |
| 79 const std::wstring& name, | 79 const std::wstring& name, |
| 80 ATOM atom) { | 80 ATOM atom, |
| 81 registered_classes_.push_back(RegisteredClass(class_info, name, atom)); | 81 HMODULE instance) { |
| 82 registered_classes_.push_back( |
| 83 RegisteredClass(class_info, name, atom, instance)); |
| 82 } | 84 } |
| 83 | 85 |
| 84 private: | 86 private: |
| 85 // Represents a registered window class. | 87 // Represents a registered window class. |
| 86 struct RegisteredClass { | 88 struct RegisteredClass { |
| 87 RegisteredClass(const ClassInfo& info, | 89 RegisteredClass(const ClassInfo& info, |
| 88 const std::wstring& name, | 90 const std::wstring& name, |
| 89 ATOM atom) | 91 ATOM atom, |
| 92 HMODULE instance) |
| 90 : info(info), | 93 : info(info), |
| 91 name(name), | 94 name(name), |
| 92 atom(atom) { | 95 atom(atom), |
| 96 instance(instance) { |
| 93 } | 97 } |
| 94 | 98 |
| 95 // Info used to create the class. | 99 // Info used to create the class. |
| 96 ClassInfo info; | 100 ClassInfo info; |
| 97 | 101 |
| 98 // The name given to the window. | 102 // The name given to the window class. |
| 99 std::wstring name; | 103 std::wstring name; |
| 100 | 104 |
| 101 // The ATOM returned from creating the window. | 105 // The ATOM returned from registering the window class. |
| 102 ATOM atom; | 106 ATOM atom; |
| 107 |
| 108 // The handle of the module containing the window procedure. |
| 109 HMODULE instance; |
| 103 }; | 110 }; |
| 104 | 111 |
| 105 ClassRegistrar() : registered_count_(0) { } | 112 ClassRegistrar() : registered_count_(0) { } |
| 106 friend struct DefaultSingletonTraits<ClassRegistrar>; | 113 friend struct DefaultSingletonTraits<ClassRegistrar>; |
| 107 | 114 |
| 108 typedef std::list<RegisteredClass> RegisteredClasses; | 115 typedef std::list<RegisteredClass> RegisteredClasses; |
| 109 RegisteredClasses registered_classes_; | 116 RegisteredClasses registered_classes_; |
| 110 | 117 |
| 111 // Counter of how many classes have been registered so far. | 118 // Counter of how many classes have been registered so far. |
| 112 int registered_count_; | 119 int registered_count_; |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 | 241 |
| 235 std::wstring WindowImpl::GetWindowClassName() { | 242 std::wstring WindowImpl::GetWindowClassName() { |
| 236 HICON icon = GetDefaultWindowIcon(); | 243 HICON icon = GetDefaultWindowIcon(); |
| 237 ClassInfo class_info(initial_class_style(), icon); | 244 ClassInfo class_info(initial_class_style(), icon); |
| 238 std::wstring name; | 245 std::wstring name; |
| 239 if (ClassRegistrar::GetInstance()->RetrieveClassName(class_info, &name)) | 246 if (ClassRegistrar::GetInstance()->RetrieveClassName(class_info, &name)) |
| 240 return name; | 247 return name; |
| 241 | 248 |
| 242 // No class found, need to register one. | 249 // No class found, need to register one. |
| 243 HBRUSH background = NULL; | 250 HBRUSH background = NULL; |
| 244 WNDCLASSEX class_ex = { | 251 WNDCLASSEXW window_class; |
| 245 sizeof(WNDCLASSEX), | 252 base::win::InitializeWindowClass( |
| 246 class_info.style, | 253 name.c_str(), |
| 247 base::win::WrappedWindowProc<&WindowImpl::WndProc>, | 254 &base::win::WrappedWindowProc<WindowImpl::WndProc>, |
| 248 0, | 255 class_info.style, |
| 249 0, | 256 0, |
| 250 GetModuleHandle(NULL), | 257 0, |
| 251 icon, | 258 NULL, |
| 252 NULL, | 259 reinterpret_cast<HBRUSH>(background + 1), |
| 253 reinterpret_cast<HBRUSH>(background + 1), | 260 NULL, |
| 254 NULL, | 261 icon, |
| 255 name.c_str(), | 262 icon, |
| 256 icon | 263 &window_class); |
| 257 }; | 264 HMODULE instance = window_class.hInstance; |
| 258 ATOM atom = RegisterClassEx(&class_ex); | 265 ATOM atom = RegisterClassEx(&window_class); |
| 259 CHECK(atom) << GetLastError(); | 266 CHECK(atom) << GetLastError(); |
| 260 | 267 |
| 261 ClassRegistrar::GetInstance()->RegisterClass(class_info, name, atom); | 268 ClassRegistrar::GetInstance()->RegisterClass( |
| 269 class_info, name, atom, instance); |
| 262 | 270 |
| 263 return name; | 271 return name; |
| 264 } | 272 } |
| 265 | 273 |
| 266 } // namespace ui | 274 } // namespace ui |
| OLD | NEW |