This source file includes following definitions.
- GetInstance
- RetrieveClassAtom
- atom
- destroyed_
- Init
- GetDefaultWindowIcon
- OnWndProc
- ClearUserData
- WndProc
- GetWindowClassAtom
#include "ui/gfx/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/gfx/win/hwnd_util.h"
namespace gfx {
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;
const wchar_t* const WindowImpl::kBaseClassName = L"Chrome_WidgetWin_";
struct ClassInfo {
UINT style;
HICON icon;
ClassInfo(int style, HICON icon)
: style(style),
icon(icon) {}
bool Equals(const ClassInfo& other) const {
return (other.style == style && other.icon == icon);
}
};
class ClassRegistrar {
public:
~ClassRegistrar();
static ClassRegistrar* GetInstance();
ATOM RetrieveClassAtom(const ClassInfo& class_info);
private:
struct RegisteredClass {
RegisteredClass(const ClassInfo& info, ATOM atom);
ClassInfo info;
ATOM atom;
};
ClassRegistrar();
friend struct DefaultSingletonTraits<ClassRegistrar>;
typedef std::list<RegisteredClass> RegisteredClasses;
RegisteredClasses registered_classes_;
int registered_count_;
base::Lock lock_;
DISALLOW_COPY_AND_ASSIGN(ClassRegistrar);
};
ClassRegistrar::~ClassRegistrar() {}
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;
}
base::string16 name = base::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,
reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)),
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::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 Rect& bounds) {
if (window_style_ == 0)
window_style_ = parent ? kWindowDefaultChildStyle : kWindowDefaultStyle;
if (parent == HWND_DESKTOP) {
CHECK((window_style_ & WS_CHILD) == 0);
parent = GetWindowToParentTo(false);
} else if (parent == ::GetDesktopWindow()) {
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);
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_);
CHECK_EQ(this, GetWindowUserData(hwnd));
}
HICON WindowImpl::GetDefaultWindowIcon() const {
return NULL;
}
LRESULT WindowImpl::OnWndProc(UINT message, WPARAM w_param, LPARAM l_param) {
LRESULT result = 0;
HWND hwnd = hwnd_;
if (message == WM_NCDESTROY)
hwnd_ = NULL;
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_))
gfx::SetWindowUserData(hwnd_, NULL);
}
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);
gfx::SetWindowUserData(hwnd, window);
window->hwnd_ = hwnd;
window->got_create_ = true;
if (hwnd)
window->got_valid_hwnd_ = true;
return TRUE;
}
WindowImpl* window = reinterpret_cast<WindowImpl*>(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);
}
}