| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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/hwnd_util.h" | |
| 6 | |
| 7 #include <dwmapi.h> | |
| 8 | |
| 9 #include "base/string_util.h" | |
| 10 #include "base/win/windows_version.h" | |
| 11 #include "gfx/rect.h" | |
| 12 #include "gfx/size.h" | |
| 13 | |
| 14 #pragma comment(lib, "dwmapi.lib") | |
| 15 | |
| 16 namespace app { | |
| 17 namespace win { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // Adjust the window to fit, returning true if the window was resized or moved. | |
| 22 bool AdjustWindowToFit(HWND hwnd, const RECT& bounds) { | |
| 23 // Get the monitor. | |
| 24 HMONITOR hmon = MonitorFromRect(&bounds, MONITOR_DEFAULTTONEAREST); | |
| 25 if (!hmon) { | |
| 26 NOTREACHED() << "Unable to find default monitor"; | |
| 27 // No monitor available. | |
| 28 return false; | |
| 29 } | |
| 30 | |
| 31 MONITORINFO mi; | |
| 32 mi.cbSize = sizeof(mi); | |
| 33 GetMonitorInfo(hmon, &mi); | |
| 34 gfx::Rect window_rect(bounds); | |
| 35 gfx::Rect monitor_rect(mi.rcWork); | |
| 36 gfx::Rect new_window_rect = window_rect.AdjustToFit(monitor_rect); | |
| 37 if (!new_window_rect.Equals(window_rect)) { | |
| 38 // Window doesn't fit on monitor, move and possibly resize. | |
| 39 SetWindowPos(hwnd, 0, new_window_rect.x(), new_window_rect.y(), | |
| 40 new_window_rect.width(), new_window_rect.height(), | |
| 41 SWP_NOACTIVATE | SWP_NOZORDER); | |
| 42 return true; | |
| 43 } else { | |
| 44 return false; | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 string16 GetClassName(HWND window) { | |
| 51 // GetClassNameW will return a truncated result (properly null terminated) if | |
| 52 // the given buffer is not large enough. So, it is not possible to determine | |
| 53 // that we got the entire class name if the result is exactly equal to the | |
| 54 // size of the buffer minus one. | |
| 55 DWORD buffer_size = MAX_PATH; | |
| 56 while (true) { | |
| 57 std::wstring output; | |
| 58 DWORD size_ret = | |
| 59 GetClassNameW(window, WriteInto(&output, buffer_size), buffer_size); | |
| 60 if (size_ret == 0) | |
| 61 break; | |
| 62 if (size_ret < (buffer_size - 1)) { | |
| 63 output.resize(size_ret); | |
| 64 return output; | |
| 65 } | |
| 66 buffer_size *= 2; | |
| 67 } | |
| 68 return std::wstring(); // error | |
| 69 } | |
| 70 | |
| 71 #pragma warning(push) | |
| 72 #pragma warning(disable:4312 4244) | |
| 73 | |
| 74 WNDPROC SetWindowProc(HWND hwnd, WNDPROC proc) { | |
| 75 // The reason we don't return the SetwindowLongPtr() value is that it returns | |
| 76 // the orignal window procedure and not the current one. I don't know if it is | |
| 77 // a bug or an intended feature. | |
| 78 WNDPROC oldwindow_proc = | |
| 79 reinterpret_cast<WNDPROC>(GetWindowLongPtr(hwnd, GWLP_WNDPROC)); | |
| 80 SetWindowLongPtr(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(proc)); | |
| 81 return oldwindow_proc; | |
| 82 } | |
| 83 | |
| 84 void* SetWindowUserData(HWND hwnd, void* user_data) { | |
| 85 return | |
| 86 reinterpret_cast<void*>(SetWindowLongPtr(hwnd, GWLP_USERDATA, | |
| 87 reinterpret_cast<LONG_PTR>(user_data))); | |
| 88 } | |
| 89 | |
| 90 void* GetWindowUserData(HWND hwnd) { | |
| 91 return reinterpret_cast<void*>(GetWindowLongPtr(hwnd, GWLP_USERDATA)); | |
| 92 } | |
| 93 | |
| 94 #pragma warning(pop) | |
| 95 | |
| 96 bool DoesWindowBelongToActiveWindow(HWND window) { | |
| 97 DCHECK(window); | |
| 98 HWND top_window = ::GetAncestor(window, GA_ROOT); | |
| 99 if (!top_window) | |
| 100 return false; | |
| 101 | |
| 102 HWND active_top_window = ::GetAncestor(::GetForegroundWindow(), GA_ROOT); | |
| 103 return (top_window == active_top_window); | |
| 104 } | |
| 105 | |
| 106 void CenterAndSizeWindow(HWND parent, | |
| 107 HWND window, | |
| 108 const gfx::Size& pref, | |
| 109 bool pref_is_client) { | |
| 110 DCHECK(window && pref.width() > 0 && pref.height() > 0); | |
| 111 | |
| 112 // Calculate the ideal bounds. | |
| 113 RECT window_bounds; | |
| 114 RECT center_bounds = {0}; | |
| 115 if (parent) { | |
| 116 // If there is a parent, center over the parents bounds. | |
| 117 ::GetWindowRect(parent, ¢er_bounds); | |
| 118 } else { | |
| 119 // No parent. Center over the monitor the window is on. | |
| 120 HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST); | |
| 121 if (monitor) { | |
| 122 MONITORINFO mi = {0}; | |
| 123 mi.cbSize = sizeof(mi); | |
| 124 GetMonitorInfo(monitor, &mi); | |
| 125 center_bounds = mi.rcWork; | |
| 126 } else { | |
| 127 NOTREACHED() << "Unable to get default monitor"; | |
| 128 } | |
| 129 } | |
| 130 window_bounds.left = center_bounds.left + | |
| 131 (center_bounds.right - center_bounds.left - pref.width()) / 2; | |
| 132 window_bounds.right = window_bounds.left + pref.width(); | |
| 133 window_bounds.top = center_bounds.top + | |
| 134 (center_bounds.bottom - center_bounds.top - pref.height()) / 2; | |
| 135 window_bounds.bottom = window_bounds.top + pref.height(); | |
| 136 | |
| 137 // If we're centering a child window, we are positioning in client | |
| 138 // coordinates, and as such we need to offset the target rectangle by the | |
| 139 // position of the parent window. | |
| 140 if (::GetWindowLong(window, GWL_STYLE) & WS_CHILD) { | |
| 141 DCHECK(parent && ::GetParent(window) == parent); | |
| 142 POINT topleft = { window_bounds.left, window_bounds.top }; | |
| 143 ::MapWindowPoints(HWND_DESKTOP, parent, &topleft, 1); | |
| 144 window_bounds.left = topleft.x; | |
| 145 window_bounds.top = topleft.y; | |
| 146 window_bounds.right = window_bounds.left + pref.width(); | |
| 147 window_bounds.bottom = window_bounds.top + pref.height(); | |
| 148 } | |
| 149 | |
| 150 // Get the WINDOWINFO for window. We need the style to calculate the size | |
| 151 // for the window. | |
| 152 WINDOWINFO win_info = {0}; | |
| 153 win_info.cbSize = sizeof(WINDOWINFO); | |
| 154 GetWindowInfo(window, &win_info); | |
| 155 | |
| 156 // Calculate the window size needed for the content size. | |
| 157 | |
| 158 if (!pref_is_client || | |
| 159 AdjustWindowRectEx(&window_bounds, win_info.dwStyle, FALSE, | |
| 160 win_info.dwExStyle)) { | |
| 161 if (!AdjustWindowToFit(window, window_bounds)) { | |
| 162 // The window fits, reset the bounds. | |
| 163 SetWindowPos(window, 0, window_bounds.left, window_bounds.top, | |
| 164 window_bounds.right - window_bounds.left, | |
| 165 window_bounds.bottom - window_bounds.top, | |
| 166 SWP_NOACTIVATE | SWP_NOZORDER); | |
| 167 } // else case, AdjustWindowToFit set the bounds for us. | |
| 168 } else { | |
| 169 NOTREACHED() << "Unable to adjust window to fit"; | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 bool ShouldUseVistaFrame() { | |
| 174 if (base::win::GetVersion() < base::win::VERSION_VISTA) | |
| 175 return false; | |
| 176 // If composition is not enabled, we behave like on XP. | |
| 177 BOOL f; | |
| 178 DwmIsCompositionEnabled(&f); | |
| 179 return !!f; | |
| 180 } | |
| 181 | |
| 182 } // namespace win | |
| 183 } // namespace app | |
| OLD | NEW |