diff options
author | brettw@google.com <brettw@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-30 15:38:47 +0000 |
---|---|---|
committer | brettw@google.com <brettw@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-30 15:38:47 +0000 |
commit | fff369b8be4e7df5af4f9c2cedd29234248fd35b (patch) | |
tree | 4e6494c94c2cb379273ab1beab20c0c142485601 /app/win | |
parent | c376c45bd9f166acbdc776c640161d7fbd5956a4 (diff) | |
download | chromium_src-fff369b8be4e7df5af4f9c2cedd29234248fd35b.zip chromium_src-fff369b8be4e7df5af4f9c2cedd29234248fd35b.tar.gz chromium_src-fff369b8be4e7df5af4f9c2cedd29234248fd35b.tar.bz2 |
Remove win_util::FormatMessage and FormatLastWin32Error. These were only used in a couple of diagnostic places and one can always use the "Error Lookup" utility.
Move window HWND-specific functions from base/win_util.h to a new file app/win/hwnd_util.h. I plan to put some more stuff from app/win_util into this file as well.
Move gfx/window_impl.h into app/win
TEST=it compiles
BUG=none
Review URL: http://codereview.chromium.org/6019007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@70312 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app/win')
-rw-r--r-- | app/win/hwnd_util.cc | 59 | ||||
-rw-r--r-- | app/win/hwnd_util.h | 31 | ||||
-rw-r--r-- | app/win/scoped_prop.cc | 2 | ||||
-rw-r--r-- | app/win/window_impl.cc | 232 | ||||
-rw-r--r-- | app/win/window_impl.h | 116 |
5 files changed, 439 insertions, 1 deletions
diff --git a/app/win/hwnd_util.cc b/app/win/hwnd_util.cc new file mode 100644 index 0000000..b0be404 --- /dev/null +++ b/app/win/hwnd_util.cc @@ -0,0 +1,59 @@ +// 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 "app/win/hwnd_util.h" + +#include "base/string_util.h" + +namespace app { +namespace win { + +string16 GetClassName(HWND window) { + // GetClassNameW will return a truncated result (properly null terminated) if + // the given buffer is not large enough. So, it is not possible to determine + // that we got the entire class name if the result is exactly equal to the + // size of the buffer minus one. + DWORD buffer_size = MAX_PATH; + while (true) { + std::wstring output; + DWORD size_ret = + GetClassNameW(window, WriteInto(&output, buffer_size), buffer_size); + if (size_ret == 0) + break; + if (size_ret < (buffer_size - 1)) { + output.resize(size_ret); + return output; + } + buffer_size *= 2; + } + return std::wstring(); // error +} + +#pragma warning(push) +#pragma warning(disable:4312 4244) + +WNDPROC SetWindowProc(HWND hwnd, WNDPROC proc) { + // The reason we don't return the SetwindowLongPtr() value is that it returns + // the orignal window procedure and not the current one. I don't know if it is + // a bug or an intended feature. + WNDPROC oldwindow_proc = + reinterpret_cast<WNDPROC>(GetWindowLongPtr(hwnd, GWLP_WNDPROC)); + SetWindowLongPtr(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(proc)); + return oldwindow_proc; +} + +void* SetWindowUserData(HWND hwnd, void* user_data) { + return + reinterpret_cast<void*>(SetWindowLongPtr(hwnd, GWLP_USERDATA, + reinterpret_cast<LONG_PTR>(user_data))); +} + +void* GetWindowUserData(HWND hwnd) { + return reinterpret_cast<void*>(GetWindowLongPtr(hwnd, GWLP_USERDATA)); +} + +#pragma warning(pop) + +} // namespace win +} // namespace app diff --git a/app/win/hwnd_util.h b/app/win/hwnd_util.h new file mode 100644 index 0000000..f8ae37e --- /dev/null +++ b/app/win/hwnd_util.h @@ -0,0 +1,31 @@ +// 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. + +#ifndef APP_WIN_HWND_UTIL_H_ +#define APP_WIN_HWND_UTIL_H_ +#pragma once + +#include <windows.h> + +#include "base/string16.h" + +namespace app { +namespace win { + +// A version of the GetClassNameW API that returns the class name in an +// string16. An empty result indicates a failure to get the class name. +string16 GetClassName(HWND hwnd); + +// Useful for subclassing a HWND. Returns the previous window procedure. +WNDPROC SetWindowProc(HWND hwnd, WNDPROC wndproc); + +// Pointer-friendly wrappers around Get/SetWindowLong(..., GWLP_USERDATA, ...) +// Returns the previously set value. +void* SetWindowUserData(HWND hwnd, void* user_data); +void* GetWindowUserData(HWND hwnd); + +} // namespace win +} // namespace app + +#endif // APP_WIN_HWND_UTIL_H_ diff --git a/app/win/scoped_prop.cc b/app/win/scoped_prop.cc index e2d623e..203e1e4 100644 --- a/app/win/scoped_prop.cc +++ b/app/win/scoped_prop.cc @@ -18,7 +18,7 @@ ScopedProp::ScopedProp(HWND hwnd, const std::wstring& key, HANDLE data) // is going to ask for the property and get NULL. So, rather than crash later // on when someone expects a non-NULL value we crash here in hopes of // diagnosing the failure. - CHECK(result) << win_util::FormatLastWin32Error(); + CHECK(result) << ::GetLastError(); } ScopedProp::~ScopedProp() { diff --git a/app/win/window_impl.cc b/app/win/window_impl.cc new file mode 100644 index 0000000..a0f0b3f --- /dev/null +++ b/app/win/window_impl.cc @@ -0,0 +1,232 @@ +// 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 "app/win/window_impl.h" + +#include <list> + +#include "app/win/hwnd_util.h" +#include "base/singleton.h" +#include "base/string_number_conversions.h" + +namespace app { +namespace win { + +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(app::win::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); + app::win::SetWindowUserData(hwnd, window); + window->hwnd_ = hwnd; + return TRUE; + } + + WindowImpl* window = reinterpret_cast<WindowImpl*>( + app::win::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 win +} // namespace app diff --git a/app/win/window_impl.h b/app/win/window_impl.h new file mode 100644 index 0000000..606f4c2 --- /dev/null +++ b/app/win/window_impl.h @@ -0,0 +1,116 @@ +// 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. + +#ifndef APP_WIN_WINDOW_IMPL_H_ +#define APP_WIN_WINDOW_IMPL_H_ +#pragma once + +#include <atlbase.h> +#include <atlapp.h> +#include <atlmisc.h> +#include <atlcrack.h> + +#include <string> + +#include "base/logging.h" +#include "gfx/native_widget_types.h" +#include "gfx/rect.h" + +namespace app { +namespace win { + +// An interface implemented by classes that use message maps. +// ProcessWindowMessage is implemented by the BEGIN_MESSAGE_MAP_EX macro. +class MessageMapInterface { + public: + // Processes one message from the window's message queue. + virtual BOOL ProcessWindowMessage(HWND window, + UINT message, + WPARAM w_param, + LPARAM l_param, + LRESULT& result, + DWORD msg_mad_id = 0) = 0; +}; + +/////////////////////////////////////////////////////////////////////////////// +// +// WindowImpl +// A convenience class that encapsulates the details of creating and +// destroying a HWND. This class also hosts the windows procedure used by all +// Windows. +// +/////////////////////////////////////////////////////////////////////////////// +class WindowImpl : public MessageMapInterface { + public: + WindowImpl(); + virtual ~WindowImpl(); + + // Initializes the Window with a parent and an initial desired size. + void Init(HWND parent, const gfx::Rect& bounds); + + // Retrieves the default window icon to use for windows if none is specified. + virtual HICON GetDefaultWindowIcon() const; + + // Returns the HWND associated with this Window. + HWND hwnd() const { return hwnd_; } + + // Sets the window styles. This is ONLY used when the window is created. + // In other words, if you invoke this after invoking Init, nothing happens. + void set_window_style(DWORD style) { window_style_ = style; } + DWORD window_style() const { return window_style_; } + + // Sets the extended window styles. See comment about |set_window_style|. + void set_window_ex_style(DWORD style) { window_ex_style_ = style; } + DWORD window_ex_style() const { return window_ex_style_; } + + // Sets the class style to use. The default is CS_DBLCLKS. + void set_initial_class_style(UINT class_style) { + // We dynamically generate the class name, so don't register it globally! + DCHECK_EQ((class_style & CS_GLOBALCLASS), 0u); + class_style_ = class_style; + } + UINT initial_class_style() const { return class_style_; } + + // Returns true if the specified |hwnd| is a WindowImpl. + static bool IsWindowImpl(HWND hwnd); + + protected: + // Handles the WndProc callback for this object. + virtual LRESULT OnWndProc(UINT message, WPARAM w_param, LPARAM l_param); + + private: + friend class ClassRegistrar; + + // The window procedure used by all Windows. + static LRESULT CALLBACK WndProc(HWND window, + UINT message, + WPARAM w_param, + LPARAM l_param); + + // Gets the window class name to use when creating the corresponding HWND. + // If necessary, this registers the window class. + std::wstring GetWindowClassName(); + + // All classes registered by WidgetWin start with this name. + static const wchar_t* const kBaseClassName; + + // Window Styles used when creating the window. + DWORD window_style_; + + // Window Extended Styles used when creating the window. + DWORD window_ex_style_; + + // Style of the class to use. + UINT class_style_; + + // Our hwnd. + HWND hwnd_; + + DISALLOW_COPY_AND_ASSIGN(WindowImpl); +}; + +} // namespace win +} // namespace app + +#endif // APP_WIN_WINDOW_IMPL_H_ |