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