| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 #include "gfx/window_impl.h" | |
| 6 | |
| 7 #include <list> | |
| 8 | |
| 9 #include "base/singleton.h" | |
| 10 #include "base/string_number_conversions.h" | |
| 11 #include "base/win_util.h" | |
| 12 | |
| 13 namespace gfx { | |
| 14 | |
| 15 static const DWORD kWindowDefaultChildStyle = | |
| 16 WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS; | |
| 17 static const DWORD kWindowDefaultStyle = WS_OVERLAPPEDWINDOW; | |
| 18 static const DWORD kWindowDefaultExStyle = 0; | |
| 19 | |
| 20 /////////////////////////////////////////////////////////////////////////////// | |
| 21 // WindowImpl class tracking. | |
| 22 | |
| 23 // Several external scripts rely explicitly on this base class name for | |
| 24 // acquiring the window handle and will break if this is modified! | |
| 25 // static | |
| 26 const wchar_t* const WindowImpl::kBaseClassName = L"Chrome_WidgetWin_"; | |
| 27 | |
| 28 // WindowImpl class information used for registering unique windows. | |
| 29 struct ClassInfo { | |
| 30 UINT style; | |
| 31 HBRUSH background; | |
| 32 | |
| 33 explicit ClassInfo(int style) | |
| 34 : style(style), | |
| 35 background(NULL) {} | |
| 36 | |
| 37 // Compares two ClassInfos. Returns true if all members match. | |
| 38 bool Equals(const ClassInfo& other) const { | |
| 39 return (other.style == style && other.background == background); | |
| 40 } | |
| 41 }; | |
| 42 | |
| 43 class ClassRegistrar { | |
| 44 public: | |
| 45 static ClassRegistrar* GetInstance() { | |
| 46 return Singleton<ClassRegistrar>::get(); | |
| 47 } | |
| 48 | |
| 49 ~ClassRegistrar() { | |
| 50 for (RegisteredClasses::iterator i = registered_classes_.begin(); | |
| 51 i != registered_classes_.end(); ++i) { | |
| 52 UnregisterClass(i->name.c_str(), NULL); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 // Puts the name for the class matching |class_info| in |class_name|, creating | |
| 57 // a new name if the class is not yet known. | |
| 58 // Returns true if this class was already known, false otherwise. | |
| 59 bool RetrieveClassName(const ClassInfo& class_info, std::wstring* name) { | |
| 60 for (RegisteredClasses::const_iterator i = registered_classes_.begin(); | |
| 61 i != registered_classes_.end(); ++i) { | |
| 62 if (class_info.Equals(i->info)) { | |
| 63 name->assign(i->name); | |
| 64 return true; | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 name->assign(string16(WindowImpl::kBaseClassName) + | |
| 69 base::IntToString16(registered_count_++)); | |
| 70 return false; | |
| 71 } | |
| 72 | |
| 73 void RegisterClass(const ClassInfo& class_info, | |
| 74 const std::wstring& name, | |
| 75 ATOM atom) { | |
| 76 registered_classes_.push_back(RegisteredClass(class_info, name, atom)); | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 // Represents a registered window class. | |
| 81 struct RegisteredClass { | |
| 82 RegisteredClass(const ClassInfo& info, | |
| 83 const std::wstring& name, | |
| 84 ATOM atom) | |
| 85 : info(info), | |
| 86 name(name), | |
| 87 atom(atom) { | |
| 88 } | |
| 89 | |
| 90 // Info used to create the class. | |
| 91 ClassInfo info; | |
| 92 | |
| 93 // The name given to the window. | |
| 94 std::wstring name; | |
| 95 | |
| 96 // The ATOM returned from creating the window. | |
| 97 ATOM atom; | |
| 98 }; | |
| 99 | |
| 100 ClassRegistrar() : registered_count_(0) { } | |
| 101 friend struct DefaultSingletonTraits<ClassRegistrar>; | |
| 102 | |
| 103 typedef std::list<RegisteredClass> RegisteredClasses; | |
| 104 RegisteredClasses registered_classes_; | |
| 105 | |
| 106 // Counter of how many classes have been registered so far. | |
| 107 int registered_count_; | |
| 108 | |
| 109 DISALLOW_COPY_AND_ASSIGN(ClassRegistrar); | |
| 110 }; | |
| 111 | |
| 112 /////////////////////////////////////////////////////////////////////////////// | |
| 113 // WindowImpl, public | |
| 114 | |
| 115 WindowImpl::WindowImpl() | |
| 116 : window_style_(0), | |
| 117 window_ex_style_(kWindowDefaultExStyle), | |
| 118 class_style_(CS_DBLCLKS), | |
| 119 hwnd_(NULL) { | |
| 120 } | |
| 121 | |
| 122 WindowImpl::~WindowImpl() { | |
| 123 } | |
| 124 | |
| 125 void WindowImpl::Init(HWND parent, const gfx::Rect& bounds) { | |
| 126 if (window_style_ == 0) | |
| 127 window_style_ = parent ? kWindowDefaultChildStyle : kWindowDefaultStyle; | |
| 128 | |
| 129 // Ensures the parent we have been passed is valid, otherwise CreateWindowEx | |
| 130 // will fail. | |
| 131 if (parent && !::IsWindow(parent)) { | |
| 132 NOTREACHED() << "invalid parent window specified."; | |
| 133 parent = NULL; | |
| 134 } | |
| 135 | |
| 136 int x, y, width, height; | |
| 137 if (bounds.IsEmpty()) { | |
| 138 x = y = width = height = CW_USEDEFAULT; | |
| 139 } else { | |
| 140 x = bounds.x(); | |
| 141 y = bounds.y(); | |
| 142 width = bounds.width(); | |
| 143 height = bounds.height(); | |
| 144 } | |
| 145 | |
| 146 hwnd_ = CreateWindowEx(window_ex_style_, GetWindowClassName().c_str(), NULL, | |
| 147 window_style_, x, y, width, height, | |
| 148 parent, NULL, NULL, this); | |
| 149 DCHECK(hwnd_); | |
| 150 | |
| 151 // The window procedure should have set the data for us. | |
| 152 DCHECK(win_util::GetWindowUserData(hwnd_) == this); | |
| 153 } | |
| 154 | |
| 155 HICON WindowImpl::GetDefaultWindowIcon() const { | |
| 156 return NULL; | |
| 157 } | |
| 158 | |
| 159 // static | |
| 160 bool WindowImpl::IsWindowImpl(HWND hwnd) { | |
| 161 wchar_t tmp[128]; | |
| 162 if (!::GetClassName(hwnd, tmp, 128)) | |
| 163 return false; | |
| 164 | |
| 165 std::wstring class_name(tmp); | |
| 166 return class_name.find(kBaseClassName) == 0; | |
| 167 } | |
| 168 | |
| 169 LRESULT WindowImpl::OnWndProc(UINT message, WPARAM w_param, LPARAM l_param) { | |
| 170 LRESULT result = 0; | |
| 171 | |
| 172 // Handle the message if it's in our message map; otherwise, let the system | |
| 173 // handle it. | |
| 174 if (!ProcessWindowMessage(hwnd_, message, w_param, l_param, result)) | |
| 175 result = DefWindowProc(hwnd_, message, w_param, l_param); | |
| 176 | |
| 177 return result; | |
| 178 } | |
| 179 | |
| 180 // static | |
| 181 LRESULT CALLBACK WindowImpl::WndProc(HWND hwnd, | |
| 182 UINT message, | |
| 183 WPARAM w_param, | |
| 184 LPARAM l_param) { | |
| 185 if (message == WM_NCCREATE) { | |
| 186 CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(l_param); | |
| 187 WindowImpl* window = reinterpret_cast<WindowImpl*>(cs->lpCreateParams); | |
| 188 DCHECK(window); | |
| 189 win_util::SetWindowUserData(hwnd, window); | |
| 190 window->hwnd_ = hwnd; | |
| 191 return TRUE; | |
| 192 } | |
| 193 | |
| 194 WindowImpl* window = reinterpret_cast<WindowImpl*>( | |
| 195 win_util::GetWindowUserData(hwnd)); | |
| 196 if (!window) | |
| 197 return 0; | |
| 198 | |
| 199 return window->OnWndProc(message, w_param, l_param); | |
| 200 } | |
| 201 | |
| 202 std::wstring WindowImpl::GetWindowClassName() { | |
| 203 ClassInfo class_info(initial_class_style()); | |
| 204 std::wstring name; | |
| 205 if (ClassRegistrar::GetInstance()->RetrieveClassName(class_info, &name)) | |
| 206 return name; | |
| 207 | |
| 208 // No class found, need to register one. | |
| 209 WNDCLASSEX class_ex; | |
| 210 class_ex.cbSize = sizeof(WNDCLASSEX); | |
| 211 class_ex.style = class_info.style; | |
| 212 class_ex.lpfnWndProc = &WindowImpl::WndProc; | |
| 213 class_ex.cbClsExtra = 0; | |
| 214 class_ex.cbWndExtra = 0; | |
| 215 class_ex.hInstance = NULL; | |
| 216 class_ex.hIcon = GetDefaultWindowIcon(); | |
| 217 class_ex.hCursor = LoadCursor(NULL, IDC_ARROW); | |
| 218 class_ex.hbrBackground = reinterpret_cast<HBRUSH>(class_info.background + 1); | |
| 219 class_ex.lpszMenuName = NULL; | |
| 220 class_ex.lpszClassName = name.c_str(); | |
| 221 class_ex.hIconSm = class_ex.hIcon; | |
| 222 ATOM atom = RegisterClassEx(&class_ex); | |
| 223 DCHECK(atom); | |
| 224 | |
| 225 ClassRegistrar::GetInstance()->RegisterClass(class_info, name, atom); | |
| 226 | |
| 227 return name; | |
| 228 } | |
| 229 | |
| 230 } // namespace gfx | |
| OLD | NEW |