OLD | NEW |
1 // Copyright (c) 2012 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/gfx/win/hwnd_util.h" |
6 | 6 |
7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/win/metro.h" | 9 #include "base/win/metro.h" |
10 #include "base/win/win_util.h" | 10 #include "base/win/win_util.h" |
11 #include "ui/gfx/point.h" | 11 #include "ui/gfx/point.h" |
12 #include "ui/gfx/rect.h" | 12 #include "ui/gfx/rect.h" |
13 #include "ui/gfx/size.h" | 13 #include "ui/gfx/size.h" |
14 | 14 |
15 namespace ui { | 15 namespace gfx { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // Adjust the window to fit. | 19 // Adjust the window to fit. |
20 void AdjustWindowToFit(HWND hwnd, const RECT& bounds, bool fit_to_monitor) { | 20 void AdjustWindowToFit(HWND hwnd, const RECT& bounds, bool fit_to_monitor) { |
21 if (fit_to_monitor) { | 21 if (fit_to_monitor) { |
22 // Get the monitor. | 22 // Get the monitor. |
23 HMONITOR hmon = MonitorFromRect(&bounds, MONITOR_DEFAULTTONEAREST); | 23 HMONITOR hmon = MonitorFromRect(&bounds, MONITOR_DEFAULTTONEAREST); |
24 if (hmon) { | 24 if (hmon) { |
25 MONITORINFO mi; | 25 MONITORINFO mi; |
26 mi.cbSize = sizeof(mi); | 26 mi.cbSize = sizeof(mi); |
27 base::win::GetMonitorInfoWrapper(hmon, &mi); | 27 base::win::GetMonitorInfoWrapper(hmon, &mi); |
28 gfx::Rect window_rect(bounds); | 28 Rect window_rect(bounds); |
29 gfx::Rect monitor_rect(mi.rcWork); | 29 Rect monitor_rect(mi.rcWork); |
30 gfx::Rect new_window_rect = window_rect; | 30 Rect new_window_rect = window_rect; |
31 new_window_rect.AdjustToFit(monitor_rect); | 31 new_window_rect.AdjustToFit(monitor_rect); |
32 if (new_window_rect != window_rect) { | 32 if (new_window_rect != window_rect) { |
33 // Window doesn't fit on monitor, move and possibly resize. | 33 // Window doesn't fit on monitor, move and possibly resize. |
34 SetWindowPos(hwnd, 0, new_window_rect.x(), new_window_rect.y(), | 34 SetWindowPos(hwnd, 0, new_window_rect.x(), new_window_rect.y(), |
35 new_window_rect.width(), new_window_rect.height(), | 35 new_window_rect.width(), new_window_rect.height(), |
36 SWP_NOACTIVATE | SWP_NOZORDER); | 36 SWP_NOACTIVATE | SWP_NOZORDER); |
37 return; | 37 return; |
38 } | 38 } |
39 // Else fall through. | 39 // Else fall through. |
40 } else { | 40 } else { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 HWND top_window = ::GetAncestor(window, GA_ROOT); | 108 HWND top_window = ::GetAncestor(window, GA_ROOT); |
109 if (!top_window) | 109 if (!top_window) |
110 return false; | 110 return false; |
111 | 111 |
112 HWND active_top_window = ::GetAncestor(::GetForegroundWindow(), GA_ROOT); | 112 HWND active_top_window = ::GetAncestor(::GetForegroundWindow(), GA_ROOT); |
113 return (top_window == active_top_window); | 113 return (top_window == active_top_window); |
114 } | 114 } |
115 | 115 |
116 void CenterAndSizeWindow(HWND parent, | 116 void CenterAndSizeWindow(HWND parent, |
117 HWND window, | 117 HWND window, |
118 const gfx::Size& pref) { | 118 const Size& pref) { |
119 DCHECK(window && pref.width() > 0 && pref.height() > 0); | 119 DCHECK(window && pref.width() > 0 && pref.height() > 0); |
120 | 120 |
121 // Calculate the ideal bounds. | 121 // Calculate the ideal bounds. |
122 RECT window_bounds; | 122 RECT window_bounds; |
123 RECT center_bounds = {0}; | 123 RECT center_bounds = {0}; |
124 if (parent) { | 124 if (parent) { |
125 // If there is a parent, center over the parents bounds. | 125 // If there is a parent, center over the parents bounds. |
126 ::GetWindowRect(parent, ¢er_bounds); | 126 ::GetWindowRect(parent, ¢er_bounds); |
127 } | 127 } |
128 | 128 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 } | 170 } |
171 | 171 |
172 void CheckWindowCreated(HWND hwnd) { | 172 void CheckWindowCreated(HWND hwnd) { |
173 if (!hwnd) | 173 if (!hwnd) |
174 LOG_GETLASTERROR(FATAL); | 174 LOG_GETLASTERROR(FATAL); |
175 } | 175 } |
176 | 176 |
177 void ShowSystemMenu(HWND window) { | 177 void ShowSystemMenu(HWND window) { |
178 RECT rect; | 178 RECT rect; |
179 GetWindowRect(window, &rect); | 179 GetWindowRect(window, &rect); |
180 gfx::Point point = gfx::Point(rect.left, rect.top); | 180 Point point = Point(rect.left, rect.top); |
181 static const int kSystemMenuOffset = 10; | 181 static const int kSystemMenuOffset = 10; |
182 point.Offset(kSystemMenuOffset, kSystemMenuOffset); | 182 point.Offset(kSystemMenuOffset, kSystemMenuOffset); |
183 ShowSystemMenuAtPoint(window, point); | 183 ShowSystemMenuAtPoint(window, point); |
184 } | 184 } |
185 | 185 |
186 void ShowSystemMenuAtPoint(HWND window, const gfx::Point& point) { | 186 void ShowSystemMenuAtPoint(HWND window, const Point& point) { |
187 // In the Metro process, we never want to show the system menu. | 187 // In the Metro process, we never want to show the system menu. |
188 if (base::win::IsMetroProcess()) | 188 if (base::win::IsMetroProcess()) |
189 return; | 189 return; |
190 UINT flags = TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD; | 190 UINT flags = TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD; |
191 if (base::i18n::IsRTL()) | 191 if (base::i18n::IsRTL()) |
192 flags |= TPM_RIGHTALIGN; | 192 flags |= TPM_RIGHTALIGN; |
193 HMENU menu = GetSystemMenu(window, FALSE); | 193 HMENU menu = GetSystemMenu(window, FALSE); |
194 const int command = | 194 const int command = |
195 TrackPopupMenu(menu, flags, point.x(), point.y(), 0, window, NULL); | 195 TrackPopupMenu(menu, flags, point.x(), point.y(), 0, window, NULL); |
196 if (command) | 196 if (command) |
197 SendMessage(window, WM_SYSCOMMAND, command, 0); | 197 SendMessage(window, WM_SYSCOMMAND, command, 0); |
198 } | 198 } |
199 | 199 |
200 extern "C" { | 200 extern "C" { |
201 typedef HWND (*RootWindow)(); | 201 typedef HWND (*RootWindow)(); |
202 } | 202 } |
203 | 203 |
204 HWND GetWindowToParentTo(bool get_real_hwnd) { | 204 HWND GetWindowToParentTo(bool get_real_hwnd) { |
205 HMODULE metro = base::win::GetMetroModule(); | 205 HMODULE metro = base::win::GetMetroModule(); |
206 if (!metro) | 206 if (!metro) |
207 return get_real_hwnd ? ::GetDesktopWindow() : HWND_DESKTOP; | 207 return get_real_hwnd ? ::GetDesktopWindow() : HWND_DESKTOP; |
208 // In windows 8 metro-mode the root window is not the desktop. | 208 // In windows 8 metro-mode the root window is not the desktop. |
209 RootWindow root_window = | 209 RootWindow root_window = |
210 reinterpret_cast<RootWindow>(::GetProcAddress(metro, "GetRootWindow")); | 210 reinterpret_cast<RootWindow>(::GetProcAddress(metro, "GetRootWindow")); |
211 return root_window(); | 211 return root_window(); |
212 } | 212 } |
213 | 213 |
214 } // namespace ui | 214 } // namespace gfx |
OLD | NEW |