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
|
// Copyright (c) 2010 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 "gfx/window_impl.h"
#include <list>
#include "base/singleton.h"
#include "base/string_number_conversions.h"
#include "base/win_util.h"
namespace gfx {
static const DWORD kWindowDefaultChildStyle =
WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
static const DWORD kWindowDefaultStyle = WS_OVERLAPPEDWINDOW;
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;
HBRUSH background;
explicit ClassInfo(int style)
: style(style),
background(NULL) {}
// Compares two ClassInfos. Returns true if all members match.
bool Equals(const ClassInfo& other) const {
return (other.style == style && other.background == background);
}
};
class ClassRegistrar {
public:
static ClassRegistrar* GetInstance() {
return Singleton<ClassRegistrar>::get();
}
~ClassRegistrar() {
for (RegisteredClasses::iterator i = registered_classes_.begin();
i != registered_classes_.end(); ++i) {
UnregisterClass(i->name.c_str(), NULL);
}
}
// Puts the name for the class matching |class_info| in |class_name|, creating
// a new name if the class is not yet known.
// Returns true if this class was already known, false otherwise.
bool RetrieveClassName(const ClassInfo& class_info, std::wstring* name) {
for (RegisteredClasses::const_iterator i = registered_classes_.begin();
i != registered_classes_.end(); ++i) {
if (class_info.Equals(i->info)) {
name->assign(i->name);
return true;
}
}
name->assign(string16(WindowImpl::kBaseClassName) +
base::IntToString16(registered_count_++));
return false;
}
void RegisterClass(const ClassInfo& class_info,
const std::wstring& name,
ATOM atom) {
registered_classes_.push_back(RegisteredClass(class_info, name, atom));
}
private:
// Represents a registered window class.
struct RegisteredClass {
RegisteredClass(const ClassInfo& info,
const std::wstring& name,
ATOM atom)
: info(info),
name(name),
atom(atom) {
}
// Info used to create the class.
ClassInfo info;
// The name given to the window.
std::wstring name;
// The ATOM returned from creating the window.
ATOM atom;
};
ClassRegistrar() : registered_count_(0) { }
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_;
DISALLOW_COPY_AND_ASSIGN(ClassRegistrar);
};
///////////////////////////////////////////////////////////////////////////////
// WindowImpl, public
WindowImpl::WindowImpl()
: window_style_(0),
window_ex_style_(kWindowDefaultExStyle),
class_style_(CS_DBLCLKS),
hwnd_(NULL) {
}
WindowImpl::~WindowImpl() {
}
void WindowImpl::Init(HWND parent, const gfx::Rect& bounds) {
if (window_style_ == 0)
window_style_ = parent ? kWindowDefaultChildStyle : kWindowDefaultStyle;
// Ensures the parent we have been passed is valid, otherwise CreateWindowEx
// will fail.
if (parent && !::IsWindow(parent)) {
NOTREACHED() << "invalid parent window specified.";
parent = NULL;
}
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();
}
hwnd_ = CreateWindowEx(window_ex_style_, GetWindowClassName().c_str(), NULL,
window_style_, x, y, width, height,
parent, NULL, NULL, this);
DCHECK(hwnd_);
// The window procedure should have set the data for us.
DCHECK(win_util::GetWindowUserData(hwnd_) == this);
}
HICON WindowImpl::GetDefaultWindowIcon() const {
return NULL;
}
// static
bool WindowImpl::IsWindowImpl(HWND hwnd) {
wchar_t tmp[128];
if (!::GetClassName(hwnd, tmp, 128))
return false;
std::wstring class_name(tmp);
return class_name.find(kBaseClassName) == 0;
}
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;
}
// 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);
win_util::SetWindowUserData(hwnd, window);
window->hwnd_ = hwnd;
return TRUE;
}
WindowImpl* window = reinterpret_cast<WindowImpl*>(
win_util::GetWindowUserData(hwnd));
if (!window)
return 0;
return window->OnWndProc(message, w_param, l_param);
}
std::wstring WindowImpl::GetWindowClassName() {
ClassInfo class_info(initial_class_style());
std::wstring name;
if (ClassRegistrar::GetInstance()->RetrieveClassName(class_info, &name))
return name;
// No class found, need to register one.
WNDCLASSEX class_ex;
class_ex.cbSize = sizeof(WNDCLASSEX);
class_ex.style = class_info.style;
class_ex.lpfnWndProc = &WindowImpl::WndProc;
class_ex.cbClsExtra = 0;
class_ex.cbWndExtra = 0;
class_ex.hInstance = NULL;
class_ex.hIcon = GetDefaultWindowIcon();
class_ex.hCursor = LoadCursor(NULL, IDC_ARROW);
class_ex.hbrBackground = reinterpret_cast<HBRUSH>(class_info.background + 1);
class_ex.lpszMenuName = NULL;
class_ex.lpszClassName = name.c_str();
class_ex.hIconSm = class_ex.hIcon;
ATOM atom = RegisterClassEx(&class_ex);
DCHECK(atom);
ClassRegistrar::GetInstance()->RegisterClass(class_info, name, atom);
return name;
}
} // namespace gfx
|