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