diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-07 20:57:16 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-07 20:57:16 +0000 |
commit | f0953debdd98af23595f4cb2545e1d56f1e8c36d (patch) | |
tree | 71f47f67a85708ebac8e2a199db8890bf1a9b45d /views/widget | |
parent | 92c6fc387d81ca52bd5d6cd7a87c0b0c3b792a3f (diff) | |
download | chromium_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 'views/widget')
-rw-r--r-- | views/widget/widget_win.cc | 219 | ||||
-rw-r--r-- | views/widget/widget_win.h | 66 |
2 files changed, 47 insertions, 238 deletions
diff --git a/views/widget/widget_win.cc b/views/widget/widget_win.cc index 1f49d1d..6a7b787 100644 --- a/views/widget/widget_win.cc +++ b/views/widget/widget_win.cc @@ -22,11 +22,6 @@ namespace views { -static const DWORD kWindowDefaultChildStyle = - WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS; -static const DWORD kWindowDefaultStyle = WS_OVERLAPPEDWINDOW; -static const DWORD kWindowDefaultExStyle = 0; - // Property used to link the HWND to its RootView. static const wchar_t* const kRootViewWindowProperty = L"__ROOT_VIEW__"; @@ -44,110 +39,19 @@ NativeControlWin* GetNativeControlWinForHWND(HWND hwnd) { } /////////////////////////////////////////////////////////////////////////////// -// Window class tracking. - -// static -const wchar_t* const WidgetWin::kBaseClassName = - L"Chrome_WidgetWin_"; - -// Window 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(WidgetWin::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); -}; - -/////////////////////////////////////////////////////////////////////////////// // WidgetWin, public WidgetWin::WidgetWin() : close_widget_factory_(this), active_mouse_tracking_flags_(0), has_capture_(false), - window_style_(0), - window_ex_style_(kWindowDefaultExStyle), use_layered_buffer_(true), layered_alpha_(255), delete_on_destroy_(true), can_update_layered_window_(true), last_mouse_event_was_move_(false), is_mouse_down_(false), - is_window_(false), - class_style_(CS_DBLCLKS), - hwnd_(NULL) { + is_window_(false) { } WidgetWin::~WidgetWin() { @@ -158,44 +62,31 @@ WidgetWin::~WidgetWin() { // Widget implementation: void WidgetWin::Init(gfx::NativeView parent, const gfx::Rect& bounds) { - if (window_style_ == 0) - window_style_ = parent ? kWindowDefaultChildStyle : kWindowDefaultStyle; + // Force creation of the RootView; otherwise, we may get a WM_SIZE after the + // window is created and before the root view is set up. + GetRootView(); + + // Create the window. + WindowImpl::Init(parent, bounds); // See if the style has been overridden. - opaque_ = !(window_ex_style_ & WS_EX_TRANSPARENT); + opaque_ = !(window_ex_style() & WS_EX_TRANSPARENT); use_layered_buffer_ = (use_layered_buffer_ && - !!(window_ex_style_ & WS_EX_LAYERED)); - - // Force creation of the RootView if it hasn't been created yet. - GetRootView(); + !!(window_ex_style() & WS_EX_LAYERED)); default_theme_provider_.reset(new DefaultThemeProvider()); - // 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_); - SetWindowSupportsRerouteMouseWheel(hwnd_); - - // The window procedure should have set the data for us. - DCHECK(win_util::GetWindowUserData(hwnd_) == this); + SetWindowSupportsRerouteMouseWheel(GetNativeView()); root_view_->OnWidgetCreated(); - if ((window_style_ & WS_CHILD) == 0) { + if ((window_style() & WS_CHILD) == 0) { // Top-level widgets get a FocusManager. focus_manager_.reset(new FocusManager(this)); } // Sets the RootView as a property, so the automation can introspect windows. - SetRootViewForHWND(hwnd_, root_view_.get()); + SetRootViewForHWND(GetNativeView(), root_view_.get()); MessageLoopForUI::current()->AddObserver(this); @@ -234,7 +125,7 @@ void WidgetWin::GetBounds(gfx::Rect* out, bool including_frame) const { GetClientRect(&crect); POINT p = {0, 0}; - ::ClientToScreen(hwnd_, &p); + ::ClientToScreen(GetNativeView(), &p); out->SetRect(crect.left + p.x, crect.top + p.y, crect.Width(), crect.Height()); } @@ -293,7 +184,7 @@ void WidgetWin::Hide() { } gfx::NativeView WidgetWin::GetNativeView() const { - return hwnd_; + return WindowImpl::GetNativeView(); } static BOOL CALLBACK EnumChildProcForRedraw(HWND hwnd, LPARAM lparam) { @@ -320,7 +211,7 @@ void WidgetWin::PaintNow(const gfx::Rect& update_rect) { // We're transparent. Need to force painting to occur from our parent. CRect parent_update_rect = update_rect.ToRECT(); POINT location_in_parent = { 0, 0 }; - ClientToScreen(hwnd_, &location_in_parent); + ClientToScreen(GetNativeView(), &location_in_parent); ::ScreenToClient(GetParent(), &location_in_parent); parent_update_rect.OffsetRect(location_in_parent); ::RedrawWindow(GetParent(), parent_update_rect, NULL, @@ -338,11 +229,11 @@ void WidgetWin::PaintNow(const gfx::Rect& update_rect) { gfx::Rect invalid_screen_rect = update_rect; invalid_screen_rect.Offset(screen_rect.x(), screen_rect.y()); - ::RedrawWindow(hwnd_, &update_rect.ToRECT(), NULL, + ::RedrawWindow(GetNativeView(), &update_rect.ToRECT(), NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_NOCHILDREN); LPARAM lparam = reinterpret_cast<LPARAM>(&invalid_screen_rect); - EnumChildWindows(hwnd_, EnumChildProcForRedraw, lparam); + EnumChildWindows(GetNativeView(), EnumChildProcForRedraw, lparam); } // As we were created with a style of WS_CLIPCHILDREN redraw requests may // result in an empty paint rect in WM_PAINT (this'll happen if a @@ -369,7 +260,7 @@ RootView* WidgetWin::GetRootView() { Widget* WidgetWin::GetRootWidget() const { return reinterpret_cast<WidgetWin*>( - win_util::GetWindowUserData(GetAncestor(hwnd_, GA_ROOT))); + win_util::GetWindowUserData(GetAncestor(GetNativeView(), GA_ROOT))); } bool WidgetWin::IsVisible() const { @@ -409,11 +300,11 @@ ThemeProvider* WidgetWin::GetThemeProvider() const { } Window* WidgetWin::GetWindow() { - return GetWindowImpl(hwnd_); + return GetWindowImpl(GetNativeView()); } const Window* WidgetWin::GetWindow() const { - return GetWindowImpl(hwnd_); + return GetWindowImpl(GetNativeView()); } FocusManager* WidgetWin::GetFocusManager() { @@ -434,7 +325,7 @@ void WidgetWin::SetUseLayeredBuffer(bool use_layered_buffer) { return; use_layered_buffer_ = use_layered_buffer; - if (!hwnd_) + if (!GetNativeView()) return; if (use_layered_buffer_) { @@ -543,7 +434,7 @@ void WidgetWin::OnClose() { void WidgetWin::OnDestroy() { root_view_->OnWidgetDestroyed(); - RemoveProp(hwnd_, kRootViewWindowProperty); + RemoveProp(GetNativeView(), kRootViewWindowProperty); } LRESULT WidgetWin::OnEraseBkgnd(HDC dc) { @@ -647,7 +538,7 @@ LRESULT WidgetWin::OnMouseWheel(UINT message, WPARAM w_param, LPARAM l_param) { // Reroute the mouse-wheel to the window under the mouse pointer if // applicable. if (message == WM_MOUSEWHEEL && - views::RerouteMouseWheel(hwnd_, w_param, l_param)) { + views::RerouteMouseWheel(GetNativeView(), w_param, l_param)) { return 0; } @@ -948,41 +839,11 @@ void WidgetWin::UpdateWindowFromContents(HDC dib_dc) { BLENDFUNCTION blend = {AC_SRC_OVER, 0, layered_alpha_, AC_SRC_ALPHA}; ::UpdateLayeredWindow( - hwnd_, NULL, &window_position, &size, dib_dc, &zero_origin, + GetNativeView(), NULL, &window_position, &size, dib_dc, &zero_origin, RGB(0xFF, 0xFF, 0xFF), &blend, ULW_ALPHA); } } -std::wstring WidgetWin::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 = &WidgetWin::WndProc; - class_ex.cbClsExtra = 0; - class_ex.cbWndExtra = 0; - class_ex.hInstance = NULL; - class_ex.hIcon = NULL; - if (ViewsDelegate::views_delegate) - class_ex.hIcon = ViewsDelegate::views_delegate->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; -} - // Get the source HWND of the specified message. Depending on the message, the // source HWND is encoded in either the WPARAM or the LPARAM value. HWND GetControlHWNDForMessage(UINT message, WPARAM w_param, LPARAM l_param) { @@ -1002,6 +863,12 @@ HWND GetControlHWNDForMessage(UINT message, WPARAM w_param, LPARAM l_param) { return NULL; } +HICON WidgetWin::GetDefaultWindowIcon() const { + if (ViewsDelegate::views_delegate) + return ViewsDelegate::views_delegate->GetDefaultWindowIcon(); + return NULL; +} + // Some messages may be sent to us by a child HWND managed by // NativeControlWin. If this is the case, this function will forward those // messages on to the object associated with the source HWND and return true, @@ -1026,23 +893,8 @@ bool ProcessNativeControlMessage(UINT message, return false; } -// static -LRESULT CALLBACK WidgetWin::WndProc(HWND window, UINT message, - WPARAM w_param, LPARAM l_param) { - if (message == WM_NCCREATE) { - CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(l_param); - WidgetWin* widget = reinterpret_cast<WidgetWin*>(cs->lpCreateParams); - DCHECK(widget); - win_util::SetWindowUserData(window, widget); - widget->hwnd_ = window; - return TRUE; - } - - WidgetWin* widget = reinterpret_cast<WidgetWin*>( - win_util::GetWindowUserData(window)); - if (!widget) - return 0; - +LRESULT WidgetWin::OnWndProc(UINT message, WPARAM w_param, LPARAM l_param) { + HWND window = GetNativeView(); LRESULT result = 0; // First allow messages sent by child controls to be processed directly by @@ -1052,12 +904,12 @@ LRESULT CALLBACK WidgetWin::WndProc(HWND window, UINT message, return result; // Otherwise we handle everything else. - if (!widget->ProcessWindowMessage(window, message, w_param, l_param, result)) + if (!ProcessWindowMessage(window, message, w_param, l_param, result)) result = DefWindowProc(window, message, w_param, l_param); if (message == WM_NCDESTROY) - widget->OnFinalMessage(window); + OnFinalMessage(window); if (message == WM_ACTIVATE) - PostProcessActivateMessage(widget, LOWORD(w_param)); + PostProcessActivateMessage(this, LOWORD(w_param)); return result; } @@ -1092,4 +944,3 @@ Widget* Widget::CreateTransparentPopupWidget(bool delete_on_destroy) { } } // namespace views - diff --git a/views/widget/widget_win.h b/views/widget/widget_win.h index b15b682..49be8ea 100644 --- a/views/widget/widget_win.h +++ b/views/widget/widget_win.h @@ -12,6 +12,7 @@ #include "base/message_loop.h" #include "base/system_monitor.h" +#include "base/window_impl.h" #include "views/focus/focus_manager.h" #include "views/layout_manager.h" #include "views/widget/widget.h" @@ -63,7 +64,8 @@ const int WM_NCUAHDRAWFRAME = 0xAF; // then responsible for cleaning up after it. // /////////////////////////////////////////////////////////////////////////////// -class WidgetWin : public Widget, +class WidgetWin : public base::WindowImpl, + public Widget, public MessageLoopForUI::Observer, public FocusTraversable, public AcceleratorTarget { @@ -71,22 +73,11 @@ class WidgetWin : public Widget, WidgetWin(); virtual ~WidgetWin(); - // 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_; }; + // Returns the RootView associated with the specified HWND (if any). + static RootView* FindRootView(HWND hwnd); - // 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((class_style & CS_GLOBALCLASS) == 0); - class_style_ = class_style; - } - UINT initial_class_style() { return class_style_; } + // Returns the Widget associated with the specified HWND (if any). + static WidgetWin* GetWidget(HWND hwnd); void set_delete_on_destroy(bool delete_on_destroy) { delete_on_destroy_ = delete_on_destroy; @@ -100,16 +91,7 @@ class WidgetWin : public Widget, can_update_layered_window_ = can_update_layered_window; } - // Returns the RootView associated with the specified HWND (if any). - static RootView* FindRootView(HWND hwnd); - - // Returns the Widget associated with the specified HWND (if any). - static WidgetWin* GetWidget(HWND hwnd); - - // All classes registered by WidgetWin start with this name. - static const wchar_t* const kBaseClassName; - - BEGIN_MSG_MAP_EX(0) + BEGIN_MSG_MAP_EX(WidgetWin) // Range handlers must go first! MESSAGE_RANGE_HANDLER_EX(WM_MOUSEFIRST, WM_MOUSELAST, OnMouseRange) MESSAGE_RANGE_HANDLER_EX(WM_NCMOUSEMOVE, WM_NCMOUSEMOVE, OnMouseRange) @@ -316,11 +298,9 @@ class WidgetWin : public Widget, } protected: - // Call close instead of this to Destroy the window. - BOOL DestroyWindow() { - DCHECK(::IsWindow(GetNativeView())); - return ::DestroyWindow(GetNativeView()); - } + // Overridden from WindowImpl: + virtual HICON GetDefaultWindowIcon() const; + virtual LRESULT OnWndProc(UINT message, WPARAM w_param, LPARAM l_param); // Message Handlers // These are all virtual so that specialized Widgets can modify or augment @@ -545,21 +525,11 @@ class WidgetWin : public Widget, // so that subclasses can do any cleanup they need to. void OnDestroyImpl(); - // The windows procedure used by all WidgetWins. - static LRESULT CALLBACK WndProc(HWND window, - UINT message, - WPARAM w_param, - LPARAM l_param); - // Called after the WM_ACTIVATE message has been processed by the default // windows procedure. static void PostProcessActivateMessage(WidgetWin* widget, int activation_state); - // Gets the window class name to use when creating the corresponding HWND. - // If necessary, this registers the window class. - std::wstring GetWindowClassName(); - // The following factory is used for calls to close the WidgetWin // instance. ScopedRunnableMethodFactory<WidgetWin> close_widget_factory_; @@ -571,15 +541,6 @@ class WidgetWin : public Widget, bool opaque_; - // 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_; - // Should we keep an offscreen buffer? This is initially true and if the // window has WS_EX_LAYERED then it remains true. You can set this to false // at any time to ditch the buffer, and similarly set back to true to force @@ -624,11 +585,8 @@ class WidgetWin : public Widget, CComPtr<IAccessible> accessibility_root_; scoped_ptr<DefaultThemeProvider> default_theme_provider_; - - // Our hwnd. - HWND hwnd_; }; } // namespace views -#endif // #ifndef VIEWS_WIDGET_WIDGET_WIN_H_ +#endif // VIEWS_WIDGET_WIDGET_WIN_H_ |