summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-07 20:57:16 +0000
committerjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-07 20:57:16 +0000
commitf0953debdd98af23595f4cb2545e1d56f1e8c36d (patch)
tree71f47f67a85708ebac8e2a199db8890bf1a9b45d /base
parent92c6fc387d81ca52bd5d6cd7a87c0b0c3b792a3f (diff)
downloadchromium_src-f0953debdd98af23595f4cb2545e1d56f1e8c36d.zip
chromium_src-f0953debdd98af23595f4cb2545e1d56f1e8c36d.tar.gz
chromium_src-f0953debdd98af23595f4cb2545e1d56f1e8c36d.tar.bz2
Factor out window creation into base::WindowImpl. This class will be used in place of CWindowImpl to reduce our dependency on ATL.
BUG=none TEST=none Review URL: http://codereview.chromium.org/165022 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@22787 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/base.gyp7
-rw-r--r--base/window_impl.cc213
-rw-r--r--base/window_impl.h104
3 files changed, 324 insertions, 0 deletions
diff --git a/base/base.gyp b/base/base.gyp
index 6c76edb..9bb37cb 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -446,8 +446,15 @@
}
],
[ 'OS == "win"', {
+ 'include_dirs': [
+ '../chrome/third_party/wtl/include',
+ ],
'sources/': [ ['exclude', '_(linux|mac|posix|chromeos)\\.cc$'],
['exclude', '\\.mm?$' ] ],
+ 'sources': [
+ 'window_impl.cc',
+ 'window_impl.h',
+ ],
'sources!': [
'data_pack.cc',
'event_recorder_stubs.cc',
diff --git a/base/window_impl.cc b/base/window_impl.cc
new file mode 100644
index 0000000..d428ebe
--- /dev/null
+++ b/base/window_impl.cc
@@ -0,0 +1,213 @@
+// Copyright (c) 2009 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 <list>
+
+#include "base/singleton.h"
+#include "base/string_util.h"
+#include "base/window_impl.h"
+#include "base/win_util.h"
+
+namespace base {
+
+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.
+
+// static
+const wchar_t* const WindowImpl::kBaseClassName = L"Chrome_WindowImpl_";
+
+// 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:
+ ~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(std::wstring(WindowImpl::kBaseClassName) +
+ IntToWString(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 ben 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;
+ }
+
+ hwnd_ = CreateWindowEx(window_ex_style_, GetWindowClassName().c_str(), L"",
+ window_style_, bounds.x(), bounds.y(), bounds.width(),
+ bounds.height(), parent, NULL, NULL, this);
+ DCHECK(hwnd_);
+
+ // The window procedure should have set the data for us.
+ DCHECK(win_util::GetWindowUserData(hwnd_) == this);
+}
+
+gfx::NativeView WindowImpl::GetNativeView() const {
+ return hwnd_;
+}
+
+HICON WindowImpl::GetDefaultWindowIcon() const {
+ return NULL;
+}
+
+BOOL WindowImpl::DestroyWindow() {
+ DCHECK(::IsWindow(GetNativeView()));
+ return ::DestroyWindow(GetNativeView());
+}
+
+LRESULT WindowImpl::OnWndProc(UINT message, WPARAM w_param, LPARAM l_param) {
+ HWND window = GetNativeView();
+ LRESULT result = 0;
+
+ // Handle the message if it's in our message map; otherwise, let the system
+ // handle it.
+ if (!ProcessWindowMessage(window, message, w_param, l_param, result))
+ result = DefWindowProc(window, 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 (Singleton<ClassRegistrar>()->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);
+
+ Singleton<ClassRegistrar>()->RegisterClass(class_info, name, atom);
+
+ return name;
+}
+
+} // namespace base
diff --git a/base/window_impl.h b/base/window_impl.h
new file mode 100644
index 0000000..eb1de35d
--- /dev/null
+++ b/base/window_impl.h
@@ -0,0 +1,104 @@
+// Copyright (c) 2009 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 BASE_WINDOW_IMPL_H_
+#define BASE_WINDOW_IMPL_H_
+
+#include <atlbase.h>
+#include <atlapp.h>
+#include <atlmisc.h>
+#include <atlcrack.h>
+
+#include <string>
+
+#include "base/gfx/native_widget_types.h"
+#include "base/gfx/rect.h"
+#include "base/logging.h"
+
+namespace base {
+
+///////////////////////////////////////////////////////////////////////////////
+//
+// 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:
+ WindowImpl();
+ virtual ~WindowImpl();
+
+ BEGIN_MSG_MAP_EX(WidgetWin)
+ // No messages to handle
+ END_MSG_MAP()
+
+ // Initialize the Window with a parent and an initial desired size.
+ virtual void Init(HWND parent, const gfx::Rect& bounds);
+
+ // Returns the gfx::NativeView associated with this Window.
+ virtual gfx::NativeView GetNativeView() const;
+
+ // Retrieves the default window icon to use for windows if none is specified.
+ virtual HICON GetDefaultWindowIcon() const;
+
+ // 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), 0);
+ class_style_ = class_style;
+ }
+ UINT initial_class_style() { return class_style_; }
+
+ protected:
+ // Call close instead of this to Destroy the window.
+ BOOL DestroyWindow();
+
+ // Handles the WndProc callback for this object.
+ virtual LRESULT OnWndProc(UINT message, WPARAM w_param, LPARAM l_param);
+
+ private:
+ friend class ClassRegistrar;
+
+ // The windows 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 base
+
+#endif // BASE_WINDOW_IMPL_H_