diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-19 22:16:53 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-19 22:16:53 +0000 |
commit | e766106da8a18fc2d2dcf45968262763312f7071 (patch) | |
tree | 1d426c74a7e1f62e6637c349ed7c4453ded441f7 /app/win | |
parent | 8450c4f5d875881bfcc1b728390d4a01e6538efa (diff) | |
download | chromium_src-e766106da8a18fc2d2dcf45968262763312f7071.zip chromium_src-e766106da8a18fc2d2dcf45968262763312f7071.tar.gz chromium_src-e766106da8a18fc2d2dcf45968262763312f7071.tar.bz2 |
Move UI-relevant Windows files to ui/base.
BUG=none
TEST=none
TBR=brettw
Review URL: http://codereview.chromium.org/6254011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71854 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app/win')
-rw-r--r-- | app/win/hwnd_util.cc | 183 | ||||
-rw-r--r-- | app/win/hwnd_util.h | 51 | ||||
-rw-r--r-- | app/win/window_impl.cc | 232 | ||||
-rw-r--r-- | app/win/window_impl.h | 116 |
4 files changed, 0 insertions, 582 deletions
diff --git a/app/win/hwnd_util.cc b/app/win/hwnd_util.cc deleted file mode 100644 index 30b7395..0000000 --- a/app/win/hwnd_util.cc +++ /dev/null @@ -1,183 +0,0 @@ -// 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 <dwmapi.h> - -#include "base/string_util.h" -#include "base/win/windows_version.h" -#include "gfx/rect.h" -#include "gfx/size.h" - -#pragma comment(lib, "dwmapi.lib") - -namespace app { -namespace win { - -namespace { - -// Adjust the window to fit, returning true if the window was resized or moved. -bool AdjustWindowToFit(HWND hwnd, const RECT& bounds) { - // Get the monitor. - HMONITOR hmon = MonitorFromRect(&bounds, MONITOR_DEFAULTTONEAREST); - if (!hmon) { - NOTREACHED() << "Unable to find default monitor"; - // No monitor available. - return false; - } - - MONITORINFO mi; - mi.cbSize = sizeof(mi); - GetMonitorInfo(hmon, &mi); - gfx::Rect window_rect(bounds); - gfx::Rect monitor_rect(mi.rcWork); - gfx::Rect new_window_rect = window_rect.AdjustToFit(monitor_rect); - if (!new_window_rect.Equals(window_rect)) { - // Window doesn't fit on monitor, move and possibly resize. - SetWindowPos(hwnd, 0, new_window_rect.x(), new_window_rect.y(), - new_window_rect.width(), new_window_rect.height(), - SWP_NOACTIVATE | SWP_NOZORDER); - return true; - } else { - return false; - } -} - -} // namespace - -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) - -bool DoesWindowBelongToActiveWindow(HWND window) { - DCHECK(window); - HWND top_window = ::GetAncestor(window, GA_ROOT); - if (!top_window) - return false; - - HWND active_top_window = ::GetAncestor(::GetForegroundWindow(), GA_ROOT); - return (top_window == active_top_window); -} - -void CenterAndSizeWindow(HWND parent, - HWND window, - const gfx::Size& pref, - bool pref_is_client) { - DCHECK(window && pref.width() > 0 && pref.height() > 0); - - // Calculate the ideal bounds. - RECT window_bounds; - RECT center_bounds = {0}; - if (parent) { - // If there is a parent, center over the parents bounds. - ::GetWindowRect(parent, ¢er_bounds); - } else { - // No parent. Center over the monitor the window is on. - HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST); - if (monitor) { - MONITORINFO mi = {0}; - mi.cbSize = sizeof(mi); - GetMonitorInfo(monitor, &mi); - center_bounds = mi.rcWork; - } else { - NOTREACHED() << "Unable to get default monitor"; - } - } - window_bounds.left = center_bounds.left + - (center_bounds.right - center_bounds.left - pref.width()) / 2; - window_bounds.right = window_bounds.left + pref.width(); - window_bounds.top = center_bounds.top + - (center_bounds.bottom - center_bounds.top - pref.height()) / 2; - window_bounds.bottom = window_bounds.top + pref.height(); - - // If we're centering a child window, we are positioning in client - // coordinates, and as such we need to offset the target rectangle by the - // position of the parent window. - if (::GetWindowLong(window, GWL_STYLE) & WS_CHILD) { - DCHECK(parent && ::GetParent(window) == parent); - POINT topleft = { window_bounds.left, window_bounds.top }; - ::MapWindowPoints(HWND_DESKTOP, parent, &topleft, 1); - window_bounds.left = topleft.x; - window_bounds.top = topleft.y; - window_bounds.right = window_bounds.left + pref.width(); - window_bounds.bottom = window_bounds.top + pref.height(); - } - - // Get the WINDOWINFO for window. We need the style to calculate the size - // for the window. - WINDOWINFO win_info = {0}; - win_info.cbSize = sizeof(WINDOWINFO); - GetWindowInfo(window, &win_info); - - // Calculate the window size needed for the content size. - - if (!pref_is_client || - AdjustWindowRectEx(&window_bounds, win_info.dwStyle, FALSE, - win_info.dwExStyle)) { - if (!AdjustWindowToFit(window, window_bounds)) { - // The window fits, reset the bounds. - SetWindowPos(window, 0, window_bounds.left, window_bounds.top, - window_bounds.right - window_bounds.left, - window_bounds.bottom - window_bounds.top, - SWP_NOACTIVATE | SWP_NOZORDER); - } // else case, AdjustWindowToFit set the bounds for us. - } else { - NOTREACHED() << "Unable to adjust window to fit"; - } -} - -bool ShouldUseVistaFrame() { - if (base::win::GetVersion() < base::win::VERSION_VISTA) - return false; - // If composition is not enabled, we behave like on XP. - BOOL f; - DwmIsCompositionEnabled(&f); - return !!f; -} - -} // namespace win -} // namespace app diff --git a/app/win/hwnd_util.h b/app/win/hwnd_util.h deleted file mode 100644 index bace47e..0000000 --- a/app/win/hwnd_util.h +++ /dev/null @@ -1,51 +0,0 @@ -// 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 gfx { -class Size; -} - -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); - -// Returns true if the specified window is the current active top window or one -// of its children. -bool DoesWindowBelongToActiveWindow(HWND window); - -// Sizes the window to have a client or window size (depending on the value of -// |pref_is_client|) of pref, then centers the window over parent, ensuring the -// window fits on screen. -void CenterAndSizeWindow(HWND parent, - HWND window, - const gfx::Size& pref, - bool pref_is_client); - -// Returns true if we are on Windows Vista or greater and composition is -// enabled. -bool ShouldUseVistaFrame(); - -} // namespace win -} // namespace app - -#endif // APP_WIN_HWND_UTIL_H_ diff --git a/app/win/window_impl.cc b/app/win/window_impl.cc deleted file mode 100644 index a0f0b3f..0000000 --- a/app/win/window_impl.cc +++ /dev/null @@ -1,232 +0,0 @@ -// 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 deleted file mode 100644 index 606f4c2..0000000 --- a/app/win/window_impl.h +++ /dev/null @@ -1,116 +0,0 @@ -// 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_ |