1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/win/window_impl.h"
#include <list>
#include "base/debug/alias.h"
#include "base/memory/singleton.h"
#include "base/strings/string_number_conversions.h"
#include "base/synchronization/lock.h"
#include "base/win/wrapped_window_proc.h"
#include "ui/base/win/hwnd_util.h"
namespace ui {
static const DWORD kWindowDefaultChildStyle =
WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
static const DWORD kWindowDefaultStyle = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN;
static const DWORD kWindowDefaultExStyle = 0;
///////////////////////////////////////////////////////////////////////////////
// WindowImpl class tracking.
// Several external scripts rely explicitly on this base class name for
// acquiring the window handle and will break if this is modified!
// static
const wchar_t* const WindowImpl::kBaseClassName = L"Chrome_WidgetWin_";
// WindowImpl class information used for registering unique windows.
struct ClassInfo {
UINT style;
HICON icon;
ClassInfo(int style, HICON icon)
: style(style),
icon(icon) {}
// Compares two ClassInfos. Returns true if all members match.
bool Equals(const ClassInfo& other) const {
return (other.style == style && other.icon == icon);
}
};
// WARNING: this class may be used on multiple threads.
class ClassRegistrar {
public:
~ClassRegistrar();
static ClassRegistrar* GetInstance();
// Returns the atom identifying the class matching |class_info|,
// creating and registering a new class if the class is not yet known.
ATOM RetrieveClassAtom(const ClassInfo& class_info);
private:
// Represents a registered window class.
struct RegisteredClass {
RegisteredClass(const ClassInfo& info, ATOM atom);
// Info used to create the class.
ClassInfo info;
// The atom identifying the window class.
ATOM atom;
};
ClassRegistrar();
friend struct DefaultSingletonTraits<ClassRegistrar>;
typedef std::list<RegisteredClass> RegisteredClasses;
RegisteredClasses registered_classes_;
// Counter of how many classes have been registered so far.
int registered_count_;
base::Lock lock_;
DISALLOW_COPY_AND_ASSIGN(ClassRegistrar);
};
ClassRegistrar::~ClassRegistrar() {}
// static
ClassRegistrar* ClassRegistrar::GetInstance() {
return Singleton<ClassRegistrar,
LeakySingletonTraits<ClassRegistrar> >::get();
}
ATOM ClassRegistrar::RetrieveClassAtom(const ClassInfo& class_info) {
base::AutoLock auto_lock(lock_);
for (RegisteredClasses::const_iterator i = registered_classes_.begin();
i != registered_classes_.end(); ++i) {
if (class_info.Equals(i->info))
return i->atom;
}
// No class found, need to register one.
string16 name = string16(WindowImpl::kBaseClassName) +
base::IntToString16(registered_count_++);
WNDCLASSEX window_class;
base::win::InitializeWindowClass(
name.c_str(),
&base::win::WrappedWindowProc<WindowImpl::WndProc>,
class_info.style,
0,
0,
NULL,
NULL,
NULL,
class_info.icon,
class_info.icon,
&window_class);
HMODULE instance = window_class.hInstance;
ATOM atom = RegisterClassEx(&window_class);
CHECK(atom) << GetLastError();
registered_classes_.push_back(RegisteredClass(class_info, atom));
return atom;
}
ClassRegistrar::RegisteredClass::RegisteredClass(const ClassInfo& info,
ATOM atom)
: info(info),
atom(atom) {}
ClassRegistrar::ClassRegistrar() : registered_count_(0) {}
///////////////////////////////////////////////////////////////////////////////
// WindowImpl, public
WindowImpl::WindowImpl()
: window_style_(0),
window_ex_style_(kWindowDefaultExStyle),
class_style_(CS_DBLCLKS),
hwnd_(NULL),
got_create_(false),
got_valid_hwnd_(false),
destroyed_(NULL) {
}
WindowImpl::~WindowImpl() {
if (destroyed_)
*destroyed_ = true;
ClearUserData();
}
void WindowImpl::Init(HWND parent, const gfx::Rect& bounds) {
if (window_style_ == 0)
window_style_ = parent ? kWindowDefaultChildStyle : kWindowDefaultStyle;
if (parent == HWND_DESKTOP) {
// Only non-child windows can have HWND_DESKTOP (0) as their parent.
CHECK((window_style_ & WS_CHILD) == 0);
parent = GetWindowToParentTo(false);
} else if (parent == ::GetDesktopWindow()) {
// Any type of window can have the "Desktop Window" as their parent.
parent = GetWindowToParentTo(true);
} else if (parent != HWND_MESSAGE) {
CHECK(::IsWindow(parent));
}
int x, y, width, height;
if (bounds.IsEmpty()) {
x = y = width = height = CW_USEDEFAULT;
} else {
x = bounds.x();
y = bounds.y();
width = bounds.width();
height = bounds.height();
}
ATOM atom = GetWindowClassAtom();
bool destroyed = false;
destroyed_ = &destroyed;
HWND hwnd = CreateWindowEx(window_ex_style_,
reinterpret_cast<wchar_t*>(atom), NULL,
window_style_, x, y, width, height,
parent, NULL, NULL, this);
// First nccalcszie (during CreateWindow) for captioned windows is
// deliberately ignored so force a second one here to get the right
// non-client set up.
if (hwnd && (window_style_ & WS_CAPTION)) {
SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE |
SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
}
if (!hwnd_ && GetLastError() == 0) {
base::debug::Alias(&destroyed);
base::debug::Alias(&hwnd);
bool got_create = got_create_;
base::debug::Alias(&got_create);
bool got_valid_hwnd = got_valid_hwnd_;
base::debug::Alias(&got_valid_hwnd);
WNDCLASSEX class_info;
memset(&class_info, 0, sizeof(WNDCLASSEX));
class_info.cbSize = sizeof(WNDCLASSEX);
BOOL got_class = GetClassInfoEx(GetModuleHandle(NULL),
reinterpret_cast<wchar_t*>(atom),
&class_info);
base::debug::Alias(&got_class);
bool procs_match = got_class && class_info.lpfnWndProc ==
base::win::WrappedWindowProc<&WindowImpl::WndProc>;
base::debug::Alias(&procs_match);
CHECK(false);
}
if (!destroyed)
destroyed_ = NULL;
CheckWindowCreated(hwnd_);
// The window procedure should have set the data for us.
CHECK_EQ(this, ui::GetWindowUserData(hwnd));
}
HICON WindowImpl::GetDefaultWindowIcon() const {
return NULL;
}
LRESULT WindowImpl::OnWndProc(UINT message, WPARAM w_param, LPARAM l_param) {
LRESULT result = 0;
// Handle the message if it's in our message map; otherwise, let the system
// handle it.
if (!ProcessWindowMessage(hwnd_, message, w_param, l_param, result))
result = DefWindowProc(hwnd_, message, w_param, l_param);
return result;
}
void WindowImpl::ClearUserData() {
if (::IsWindow(hwnd_))
ui::SetWindowUserData(hwnd_, NULL);
}
// static
LRESULT CALLBACK WindowImpl::WndProc(HWND hwnd,
UINT message,
WPARAM w_param,
LPARAM l_param) {
if (message == WM_NCCREATE) {
CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(l_param);
WindowImpl* window = reinterpret_cast<WindowImpl*>(cs->lpCreateParams);
DCHECK(window);
ui::SetWindowUserData(hwnd, window);
window->hwnd_ = hwnd;
window->got_create_ = true;
if (hwnd)
window->got_valid_hwnd_ = true;
return TRUE;
}
WindowImpl* window = reinterpret_cast<WindowImpl*>(
ui::GetWindowUserData(hwnd));
if (!window)
return 0;
return window->OnWndProc(message, w_param, l_param);
}
ATOM WindowImpl::GetWindowClassAtom() {
HICON icon = GetDefaultWindowIcon();
ClassInfo class_info(initial_class_style(), icon);
return ClassRegistrar::GetInstance()->RetrieveClassAtom(class_info);
}
} // namespace ui
|