| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/base/win/hwnd_util.h" | 5 #include "ui/base/win/hwnd_util.h" |
| 6 | 6 |
| 7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "ui/gfx/rect.h" | 9 #include "ui/gfx/rect.h" |
| 10 #include "ui/gfx/size.h" | 10 #include "ui/gfx/size.h" |
| 11 | 11 |
| 12 namespace ui { | 12 namespace ui { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // Adjust the window to fit, returning true if the window was resized or moved. | 16 // Adjust the window to fit, returning true if the window was resized or moved. |
| 17 bool AdjustWindowToFit(HWND hwnd, const RECT& bounds) { | 17 void AdjustWindowToFit(HWND hwnd, const RECT& bounds, bool fit_to_monitor) { |
| 18 // Get the monitor. | 18 if (fit_to_monitor) { |
| 19 HMONITOR hmon = MonitorFromRect(&bounds, MONITOR_DEFAULTTONEAREST); | 19 // Get the monitor. |
| 20 if (!hmon) { | 20 HMONITOR hmon = MonitorFromRect(&bounds, MONITOR_DEFAULTTONEAREST); |
| 21 NOTREACHED() << "Unable to find default monitor"; | 21 if (hmon) { |
| 22 // No monitor available. | 22 MONITORINFO mi; |
| 23 return false; | 23 mi.cbSize = sizeof(mi); |
| 24 } | 24 GetMonitorInfo(hmon, &mi); |
| 25 gfx::Rect window_rect(bounds); |
| 26 gfx::Rect monitor_rect(mi.rcWork); |
| 27 gfx::Rect new_window_rect = window_rect.AdjustToFit(monitor_rect); |
| 28 if (!new_window_rect.Equals(window_rect)) { |
| 29 // Window doesn't fit on monitor, move and possibly resize. |
| 30 SetWindowPos(hwnd, 0, new_window_rect.x(), new_window_rect.y(), |
| 31 new_window_rect.width(), new_window_rect.height(), |
| 32 SWP_NOACTIVATE | SWP_NOZORDER); |
| 33 return; |
| 34 } |
| 35 // Else fall through. |
| 36 } else { |
| 37 NOTREACHED() << "Unable to find default monitor"; |
| 38 // Fall through. |
| 39 } |
| 40 } // Else fall through. |
| 25 | 41 |
| 26 MONITORINFO mi; | 42 // The window is not being fit to monitor, or the window fits on the monitor |
| 27 mi.cbSize = sizeof(mi); | 43 // as is, or we have no monitor info; reset the bounds. |
| 28 GetMonitorInfo(hmon, &mi); | 44 ::SetWindowPos(hwnd, 0, bounds.left, bounds.top, |
| 29 gfx::Rect window_rect(bounds); | 45 bounds.right - bounds.left, bounds.bottom - bounds.top, |
| 30 gfx::Rect monitor_rect(mi.rcWork); | |
| 31 gfx::Rect new_window_rect = window_rect.AdjustToFit(monitor_rect); | |
| 32 if (!new_window_rect.Equals(window_rect)) { | |
| 33 // Window doesn't fit on monitor, move and possibly resize. | |
| 34 SetWindowPos(hwnd, 0, new_window_rect.x(), new_window_rect.y(), | |
| 35 new_window_rect.width(), new_window_rect.height(), | |
| 36 SWP_NOACTIVATE | SWP_NOZORDER); | 46 SWP_NOACTIVATE | SWP_NOZORDER); |
| 37 return true; | |
| 38 } else { | |
| 39 return false; | |
| 40 } | |
| 41 } | 47 } |
| 42 | 48 |
| 43 } // namespace | 49 } // namespace |
| 44 | 50 |
| 45 string16 GetClassName(HWND window) { | 51 string16 GetClassName(HWND window) { |
| 46 // GetClassNameW will return a truncated result (properly null terminated) if | 52 // GetClassNameW will return a truncated result (properly null terminated) if |
| 47 // the given buffer is not large enough. So, it is not possible to determine | 53 // the given buffer is not large enough. So, it is not possible to determine |
| 48 // that we got the entire class name if the result is exactly equal to the | 54 // that we got the entire class name if the result is exactly equal to the |
| 49 // size of the buffer minus one. | 55 // size of the buffer minus one. |
| 50 DWORD buffer_size = MAX_PATH; | 56 DWORD buffer_size = MAX_PATH; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 HWND top_window = ::GetAncestor(window, GA_ROOT); | 104 HWND top_window = ::GetAncestor(window, GA_ROOT); |
| 99 if (!top_window) | 105 if (!top_window) |
| 100 return false; | 106 return false; |
| 101 | 107 |
| 102 HWND active_top_window = ::GetAncestor(::GetForegroundWindow(), GA_ROOT); | 108 HWND active_top_window = ::GetAncestor(::GetForegroundWindow(), GA_ROOT); |
| 103 return (top_window == active_top_window); | 109 return (top_window == active_top_window); |
| 104 } | 110 } |
| 105 | 111 |
| 106 void CenterAndSizeWindow(HWND parent, | 112 void CenterAndSizeWindow(HWND parent, |
| 107 HWND window, | 113 HWND window, |
| 108 const gfx::Size& pref, | 114 const gfx::Size& pref) { |
| 109 bool pref_is_client) { | |
| 110 DCHECK(window && pref.width() > 0 && pref.height() > 0); | 115 DCHECK(window && pref.width() > 0 && pref.height() > 0); |
| 111 | 116 |
| 112 // Calculate the ideal bounds. | 117 // Calculate the ideal bounds. |
| 113 RECT window_bounds; | 118 RECT window_bounds; |
| 114 RECT center_bounds = {0}; | 119 RECT center_bounds = {0}; |
| 115 if (parent) { | 120 if (parent) { |
| 116 // If there is a parent, center over the parents bounds. | 121 // If there is a parent, center over the parents bounds. |
| 117 ::GetWindowRect(parent, ¢er_bounds); | 122 ::GetWindowRect(parent, ¢er_bounds); |
| 118 } else { | 123 } else { |
| 119 // No parent. Center over the monitor the window is on. | 124 // No parent. Center over the monitor the window is on. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 140 if (::GetWindowLong(window, GWL_STYLE) & WS_CHILD) { | 145 if (::GetWindowLong(window, GWL_STYLE) & WS_CHILD) { |
| 141 DCHECK(parent && ::GetParent(window) == parent); | 146 DCHECK(parent && ::GetParent(window) == parent); |
| 142 POINT topleft = { window_bounds.left, window_bounds.top }; | 147 POINT topleft = { window_bounds.left, window_bounds.top }; |
| 143 ::MapWindowPoints(HWND_DESKTOP, parent, &topleft, 1); | 148 ::MapWindowPoints(HWND_DESKTOP, parent, &topleft, 1); |
| 144 window_bounds.left = topleft.x; | 149 window_bounds.left = topleft.x; |
| 145 window_bounds.top = topleft.y; | 150 window_bounds.top = topleft.y; |
| 146 window_bounds.right = window_bounds.left + pref.width(); | 151 window_bounds.right = window_bounds.left + pref.width(); |
| 147 window_bounds.bottom = window_bounds.top + pref.height(); | 152 window_bounds.bottom = window_bounds.top + pref.height(); |
| 148 } | 153 } |
| 149 | 154 |
| 150 // Get the WINDOWINFO for window. We need the style to calculate the size | 155 AdjustWindowToFit(window, window_bounds, !parent); |
| 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 } | 156 } |
| 172 | 157 |
| 173 void CheckWindowCreated(HWND hwnd) { | 158 void CheckWindowCreated(HWND hwnd) { |
| 174 if (!hwnd) | 159 if (!hwnd) |
| 175 LOG_GETLASTERROR(FATAL); | 160 LOG_GETLASTERROR(FATAL); |
| 176 } | 161 } |
| 177 | 162 |
| 178 void ShowSystemMenu(HWND window, int screen_x, int screen_y) { | 163 void ShowSystemMenu(HWND window, int screen_x, int screen_y) { |
| 179 UINT flags = TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD; | 164 UINT flags = TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD; |
| 180 if (base::i18n::IsRTL()) | 165 if (base::i18n::IsRTL()) |
| 181 flags |= TPM_RIGHTALIGN; | 166 flags |= TPM_RIGHTALIGN; |
| 182 HMENU system_menu = GetSystemMenu(window, FALSE); | 167 HMENU system_menu = GetSystemMenu(window, FALSE); |
| 183 int command = TrackPopupMenu(system_menu, flags, screen_x, screen_y, 0, | 168 int command = TrackPopupMenu(system_menu, flags, screen_x, screen_y, 0, |
| 184 window, NULL); | 169 window, NULL); |
| 185 if (command) | 170 if (command) |
| 186 SendMessage(window, WM_SYSCOMMAND, command, 0); | 171 SendMessage(window, WM_SYSCOMMAND, command, 0); |
| 187 } | 172 } |
| 188 | 173 |
| 189 } // namespace ui | 174 } // namespace ui |
| OLD | NEW |