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/gfx/screen_win.h" | 5 #include "ui/gfx/screen_win.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <stdint.h> | |
9 | 8 |
10 #include "base/bind.h" | |
11 #include "base/bind_helpers.h" | |
12 #include "base/hash.h" | |
13 #include "base/logging.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "base/win/win_util.h" | |
16 #include "ui/gfx/display.h" | 9 #include "ui/gfx/display.h" |
17 #include "ui/gfx/win/dpi.h" | 10 #include "ui/gfx/geometry/insets.h" |
| 11 #include "ui/gfx/geometry/point.h" |
| 12 #include "ui/gfx/geometry/point_conversions.h" |
| 13 #include "ui/gfx/geometry/rect.h" |
| 14 #include "ui/gfx/geometry/size.h" |
| 15 #include "ui/gfx/geometry/vector2d.h" |
| 16 #include "ui/gfx/win/display_info.h" |
| 17 #include "ui/gfx/win/display_manager.h" |
| 18 #include "ui/gfx/win/rect_util.h" |
| 19 #include "ui/gfx/win/screen_win_display.h" |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 MONITORINFOEX GetMonitorInfoForMonitor(HMONITOR monitor) { | 23 // Returns a point in |to_origin|'s coordinates and position scaled by |
22 MONITORINFOEX monitor_info; | 24 // |scale_factor|. |
23 ZeroMemory(&monitor_info, sizeof(MONITORINFOEX)); | 25 gfx::Point ScalePointRelative(const gfx::Point& from_origin, |
24 monitor_info.cbSize = sizeof(monitor_info); | 26 const gfx::Point& to_origin, |
25 GetMonitorInfo(monitor, &monitor_info); | 27 const float scale_factor, |
26 return monitor_info; | 28 const gfx::Point& point) { |
| 29 gfx::Vector2d from_origin_vector(from_origin.x(), from_origin.y()); |
| 30 gfx::Vector2d to_origin_vector(to_origin.x(), to_origin.y()); |
| 31 gfx::Point scaled_relative_point( |
| 32 gfx::ScaleToFlooredPoint(point - from_origin_vector, scale_factor)); |
| 33 return scaled_relative_point + to_origin_vector; |
27 } | 34 } |
28 | 35 |
29 gfx::Display GetDisplay(const MONITORINFOEX& monitor_info) { | 36 std::vector<gfx::Display> ToVectorOfDisplays( |
30 int64_t id = | 37 const std::vector<gfx::win::ScreenWinDisplay>& screen_win_displays) { |
31 static_cast<int64_t>(base::Hash(base::WideToUTF8(monitor_info.szDevice))); | 38 std::vector<gfx::Display> displays; |
32 gfx::Rect bounds = gfx::Rect(monitor_info.rcMonitor); | 39 for (const auto& screen_win_display : screen_win_displays) |
33 gfx::Display display(id); | 40 displays.push_back(screen_win_display.display()); |
34 display.set_bounds(gfx::win::ScreenToDIPRect(bounds)); | |
35 display.set_work_area( | |
36 gfx::win::ScreenToDIPRect(gfx::Rect(monitor_info.rcWork))); | |
37 display.SetScaleAndBounds(gfx::GetDPIScale(), bounds); | |
38 | 41 |
39 DEVMODE mode; | 42 return displays; |
40 memset(&mode, 0, sizeof(DEVMODE)); | |
41 mode.dmSize = sizeof(DEVMODE); | |
42 mode.dmDriverExtra = 0; | |
43 if (EnumDisplaySettings(monitor_info.szDevice, | |
44 ENUM_CURRENT_SETTINGS, | |
45 &mode)) { | |
46 switch (mode.dmDisplayOrientation) { | |
47 case DMDO_DEFAULT: | |
48 display.set_rotation(gfx::Display::ROTATE_0); | |
49 break; | |
50 case DMDO_90: | |
51 display.set_rotation(gfx::Display::ROTATE_90); | |
52 break; | |
53 case DMDO_180: | |
54 display.set_rotation(gfx::Display::ROTATE_180); | |
55 break; | |
56 case DMDO_270: | |
57 display.set_rotation(gfx::Display::ROTATE_270); | |
58 break; | |
59 default: | |
60 NOTREACHED(); | |
61 } | |
62 } | |
63 | |
64 return display; | |
65 } | 43 } |
66 | 44 |
67 BOOL CALLBACK EnumMonitorCallback(HMONITOR monitor, | 45 inline gfx::win::DisplayManager* GetDisplayManager() { |
68 HDC hdc, | 46 return gfx::win::DisplayManager::GetInstance(); |
69 LPRECT rect, | |
70 LPARAM data) { | |
71 std::vector<gfx::Display>* all_displays = | |
72 reinterpret_cast<std::vector<gfx::Display>*>(data); | |
73 DCHECK(all_displays); | |
74 | |
75 MONITORINFOEX monitor_info = GetMonitorInfoForMonitor(monitor); | |
76 gfx::Display display = GetDisplay(monitor_info); | |
77 all_displays->push_back(display); | |
78 return TRUE; | |
79 } | |
80 | |
81 std::vector<gfx::Display> GetDisplays() { | |
82 std::vector<gfx::Display> displays; | |
83 EnumDisplayMonitors(NULL, NULL, EnumMonitorCallback, | |
84 reinterpret_cast<LPARAM>(&displays)); | |
85 return displays; | |
86 } | 47 } |
87 | 48 |
88 } // namespace | 49 } // namespace |
89 | 50 |
90 namespace gfx { | 51 namespace gfx { |
91 | 52 |
92 ScreenWin::ScreenWin() | 53 ScreenWin::ScreenWin() : display_manager_(GetDisplayManager()) { |
93 : singleton_hwnd_observer_(new SingletonHwndObserver( | 54 display_manager_->AddObserver(this); |
94 base::Bind(&ScreenWin::OnWndProc, base::Unretained(this)))), | |
95 displays_(GetDisplays()) { | |
96 } | 55 } |
97 | 56 |
98 ScreenWin::~ScreenWin() {} | 57 ScreenWin::~ScreenWin() { |
| 58 display_manager_->RemoveObserver(this); |
| 59 } |
| 60 |
| 61 // static |
| 62 Point ScreenWin::ScreenToDIPPoint(const Point& pixel_point) { |
| 63 gfx::win::DisplayManager* display_manager = GetDisplayManager(); |
| 64 gfx::win::ScreenWinDisplay screen_win_display = |
| 65 display_manager->GetScreenWinDisplayNearestScreenPoint(pixel_point); |
| 66 Display display = screen_win_display.display(); |
| 67 return ScalePointRelative(screen_win_display.physical_bounds().origin(), |
| 68 display.bounds().origin(), |
| 69 1.0f / display.device_scale_factor(), |
| 70 pixel_point); |
| 71 } |
| 72 |
| 73 // static |
| 74 Point ScreenWin::DIPToScreenPoint(const Point& dip_point) { |
| 75 gfx::win::DisplayManager* display_manager = GetDisplayManager(); |
| 76 gfx::win::ScreenWinDisplay screen_win_display = |
| 77 display_manager->GetScreenWinDisplayNearestDIPPoint(dip_point); |
| 78 Display display = screen_win_display.display(); |
| 79 return ScalePointRelative(display.bounds().origin(), |
| 80 screen_win_display.physical_bounds().origin(), |
| 81 display.device_scale_factor(), |
| 82 dip_point); |
| 83 } |
| 84 |
| 85 // static |
| 86 Point ScreenWin::ClientToDIPPoint(HWND hwnd, const Point& client_point) { |
| 87 gfx::win::DisplayManager* display_manager = GetDisplayManager(); |
| 88 float scale_factor = display_manager->GetScaleFactorForHWND(hwnd); |
| 89 return ScaleToFlooredPoint(client_point, 1.0f / scale_factor); |
| 90 } |
| 91 |
| 92 // static |
| 93 Point ScreenWin::DIPToClientPoint(HWND hwnd, const Point& dip_point) { |
| 94 gfx::win::DisplayManager* display_manager = GetDisplayManager(); |
| 95 float scale_factor = display_manager->GetScaleFactorForHWND(hwnd); |
| 96 return ScaleToFlooredPoint(dip_point, scale_factor); |
| 97 } |
| 98 |
| 99 // static |
| 100 Rect ScreenWin::ScreenToDIPRect(HWND hwnd, const Rect& pixel_bounds) { |
| 101 // It's important we scale the origin and size separately. If we instead |
| 102 // calculated the size from the floored origin and ceiled right the size could |
| 103 // vary depending upon where the two points land. That would cause problems |
| 104 // for the places this code is used (in particular mapping from native window |
| 105 // bounds to DIPs). |
| 106 if (hwnd) { |
| 107 return Rect(ScreenToDIPPoint(pixel_bounds.origin()), |
| 108 ScreenToDIPSize(hwnd, pixel_bounds.size())); |
| 109 } |
| 110 gfx::win::DisplayManager* display_manager = GetDisplayManager(); |
| 111 gfx::win::ScreenWinDisplay screen_win_display = |
| 112 display_manager->GetScreenWinDisplayNearestScreenRect(pixel_bounds); |
| 113 float scale_factor = screen_win_display.display().device_scale_factor(); |
| 114 return Rect(ScreenToDIPPoint(pixel_bounds.origin()), |
| 115 ScaleToCeiledSize(pixel_bounds.size(), 1.0f / scale_factor)); |
| 116 } |
| 117 |
| 118 // static |
| 119 Rect ScreenWin::DIPToScreenRect(HWND hwnd, const Rect& dip_bounds) { |
| 120 // See comment in ScreenToDIPRect for why we calculate size like this. |
| 121 if (hwnd) { |
| 122 return Rect(DIPToScreenPoint(dip_bounds.origin()), |
| 123 DIPToScreenSize(hwnd, dip_bounds.size())); |
| 124 } |
| 125 gfx::win::DisplayManager* display_manager = GetDisplayManager(); |
| 126 gfx::win::ScreenWinDisplay screen_win_display = |
| 127 display_manager->GetScreenWinDisplayNearestDIPRect(dip_bounds); |
| 128 float scale_factor = screen_win_display.display().device_scale_factor(); |
| 129 return Rect(DIPToScreenPoint(dip_bounds.origin()), |
| 130 ScaleToCeiledSize(dip_bounds.size(), scale_factor)); |
| 131 } |
| 132 |
| 133 // static |
| 134 Rect ScreenWin::ClientToDIPRect(HWND hwnd, const Rect& pixel_bounds) { |
| 135 // See comment in ScreenToDIPRect for why we calculate size like this. |
| 136 return Rect(ClientToDIPPoint(hwnd, pixel_bounds.origin()), |
| 137 ScreenToDIPSize(hwnd, pixel_bounds.size())); |
| 138 } |
| 139 |
| 140 // static |
| 141 Rect ScreenWin::DIPToClientRect(HWND hwnd, const Rect& dip_bounds) { |
| 142 // See comment in ScreenToDIPRect for why we calculate size like this. |
| 143 return Rect(DIPToClientPoint(hwnd, dip_bounds.origin()), |
| 144 DIPToScreenSize(hwnd, dip_bounds.size())); |
| 145 } |
| 146 |
| 147 // static |
| 148 Size ScreenWin::ScreenToDIPSize(HWND hwnd, const Size& size_in_pixels) { |
| 149 gfx::win::DisplayManager* display_manager = GetDisplayManager(); |
| 150 float scale_factor = display_manager->GetScaleFactorForHWND(hwnd); |
| 151 // Always ceil sizes. Otherwise we may be leaving off part of the bounds. |
| 152 return ScaleToCeiledSize(size_in_pixels, 1.0f / scale_factor); |
| 153 } |
| 154 |
| 155 // static |
| 156 Size ScreenWin::DIPToScreenSize(HWND hwnd, const Size& dip_size) { |
| 157 gfx::win::DisplayManager* display_manager = GetDisplayManager(); |
| 158 float scale_factor = display_manager->GetScaleFactorForHWND(hwnd); |
| 159 // Always ceil sizes. Otherwise we may be leaving off part of the bounds. |
| 160 return ScaleToCeiledSize(dip_size, scale_factor); |
| 161 } |
99 | 162 |
100 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const { | 163 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const { |
101 NOTREACHED(); | 164 NOTREACHED(); |
102 return NULL; | 165 return NULL; |
103 } | 166 } |
104 | 167 |
105 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { | 168 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { |
106 NOTREACHED(); | 169 NOTREACHED(); |
107 return NULL; | 170 return NULL; |
108 } | 171 } |
109 | 172 |
110 gfx::Point ScreenWin::GetCursorScreenPoint() { | 173 gfx::Point ScreenWin::GetCursorScreenPoint() { |
111 POINT pt; | 174 POINT pt; |
112 GetCursorPos(&pt); | 175 GetCursorPos(&pt); |
113 gfx::Point cursor_pos_pixels(pt); | 176 gfx::Point cursor_pos_pixels(pt); |
114 return gfx::win::ScreenToDIPPoint(cursor_pos_pixels); | 177 return ScreenToDIPPoint(cursor_pos_pixels); |
115 } | 178 } |
116 | 179 |
117 gfx::NativeWindow ScreenWin::GetWindowUnderCursor() { | 180 gfx::NativeWindow ScreenWin::GetWindowUnderCursor() { |
118 POINT cursor_loc; | 181 POINT cursor_loc; |
119 HWND hwnd = GetCursorPos(&cursor_loc) ? WindowFromPoint(cursor_loc) : NULL; | 182 HWND hwnd = GetCursorPos(&cursor_loc) ? WindowFromPoint(cursor_loc) : NULL; |
120 return GetNativeWindowFromHWND(hwnd); | 183 return GetNativeWindowFromHWND(hwnd); |
121 } | 184 } |
122 | 185 |
123 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) { | 186 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) { |
124 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point); | 187 gfx::Point point_in_pixels = DIPToScreenPoint(point); |
125 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT())); | 188 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT())); |
126 } | 189 } |
127 | 190 |
128 int ScreenWin::GetNumDisplays() const { | 191 int ScreenWin::GetNumDisplays() const { |
129 return GetSystemMetrics(SM_CMONITORS); | 192 return GetSystemMetrics(SM_CMONITORS); |
130 } | 193 } |
131 | 194 |
132 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const { | 195 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const { |
133 return displays_; | 196 return ToVectorOfDisplays(display_manager_->GetScreenWinDisplays()); |
134 } | 197 } |
135 | 198 |
136 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { | 199 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { |
137 HWND window_hwnd = GetHWNDFromNativeView(window); | 200 HWND window_hwnd = GetHWNDFromNativeView(window); |
138 if (!window_hwnd) { | 201 if (!window_hwnd) { |
139 // When |window| isn't rooted to a display, we should just return the | 202 // When |window| isn't rooted to a display, we should just return the |
140 // default display so we get some correct display information like the | 203 // default display so we get some correct display information like the |
141 // scaling factor. | 204 // scaling factor. |
142 return GetPrimaryDisplay(); | 205 return GetPrimaryDisplay(); |
143 } | 206 } |
144 | 207 gfx::win::ScreenWinDisplay screen_win_display = |
145 MONITORINFOEX monitor_info; | 208 display_manager_->GetScreenWinDisplayNearestHWND(window_hwnd); |
146 monitor_info.cbSize = sizeof(monitor_info); | 209 return screen_win_display.display(); |
147 GetMonitorInfo(MonitorFromWindow(window_hwnd, MONITOR_DEFAULTTONEAREST), | |
148 &monitor_info); | |
149 return GetDisplay(monitor_info); | |
150 } | 210 } |
151 | 211 |
152 gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const { | 212 gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const { |
153 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point); | 213 gfx::Point screen_point(DIPToScreenPoint(point)); |
154 POINT initial_loc = { point_in_pixels.x(), point_in_pixels.y() }; | 214 gfx::win::ScreenWinDisplay screen_win_display = |
155 HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST); | 215 display_manager_->GetScreenWinDisplayNearestScreenPoint(screen_point); |
156 MONITORINFOEX mi; | 216 return screen_win_display.display(); |
157 ZeroMemory(&mi, sizeof(MONITORINFOEX)); | |
158 mi.cbSize = sizeof(mi); | |
159 if (monitor && GetMonitorInfo(monitor, &mi)) { | |
160 return GetDisplay(mi); | |
161 } | |
162 return gfx::Display(); | |
163 } | 217 } |
164 | 218 |
165 gfx::Display ScreenWin::GetDisplayMatching(const gfx::Rect& match_rect) const { | 219 gfx::Display ScreenWin::GetDisplayMatching(const gfx::Rect& match_rect) const { |
166 RECT other_bounds_rect = match_rect.ToRECT(); | 220 gfx::win::ScreenWinDisplay screen_win_display = |
167 MONITORINFOEX monitor_info = GetMonitorInfoForMonitor(MonitorFromRect( | 221 display_manager_->GetScreenWinDisplayNearestScreenRect(match_rect); |
168 &other_bounds_rect, MONITOR_DEFAULTTONEAREST)); | 222 return screen_win_display.display(); |
169 return GetDisplay(monitor_info); | |
170 } | 223 } |
171 | 224 |
172 gfx::Display ScreenWin::GetPrimaryDisplay() const { | 225 gfx::Display ScreenWin::GetPrimaryDisplay() const { |
173 MONITORINFOEX mi = GetMonitorInfoForMonitor( | 226 return display_manager_->GetPrimaryScreenWinDisplay().display(); |
174 MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY)); | |
175 gfx::Display display = GetDisplay(mi); | |
176 // TODO(kevers|girard): Test if these checks can be reintroduced for high-DIP | |
177 // once more of the app is DIP-aware. | |
178 if (GetDPIScale() == 1.0) { | |
179 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), display.size().width()); | |
180 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), display.size().height()); | |
181 } | |
182 return display; | |
183 } | 227 } |
184 | 228 |
185 void ScreenWin::AddObserver(DisplayObserver* observer) { | 229 void ScreenWin::AddObserver(DisplayObserver* observer) { |
186 change_notifier_.AddObserver(observer); | 230 change_notifier_.AddObserver(observer); |
187 } | 231 } |
188 | 232 |
189 void ScreenWin::RemoveObserver(DisplayObserver* observer) { | 233 void ScreenWin::RemoveObserver(DisplayObserver* observer) { |
190 change_notifier_.RemoveObserver(observer); | 234 change_notifier_.RemoveObserver(observer); |
191 } | 235 } |
192 | 236 |
193 void ScreenWin::OnWndProc(HWND hwnd, | 237 gfx::Rect ScreenWin::ScreenToDIPRectInWindow( |
194 UINT message, | 238 NativeView view, const gfx::Rect& screen_rect) const { |
195 WPARAM wparam, | 239 HWND hwnd = view ? GetHWNDFromNativeView(view) : NULL; |
196 LPARAM lparam) { | 240 return ScreenWin::ScreenToDIPRect(hwnd, screen_rect); |
197 if (message != WM_DISPLAYCHANGE) | |
198 return; | |
199 | |
200 std::vector<gfx::Display> old_displays = displays_; | |
201 displays_ = GetDisplays(); | |
202 | |
203 change_notifier_.NotifyDisplaysChanged(old_displays, displays_); | |
204 } | 241 } |
205 | 242 |
206 // static | 243 gfx::Rect ScreenWin::DIPToScreenRectInWindow(NativeView view, |
207 std::vector<gfx::Display> ScreenWin::GetDisplaysForMonitorInfos( | 244 const gfx::Rect& dip_rect) const { |
208 const std::vector<MONITORINFOEX>& monitor_infos) { | 245 HWND hwnd = view ? GetHWNDFromNativeView(view) : NULL; |
209 std::vector<gfx::Display> displays; | 246 return ScreenWin::DIPToScreenRect(hwnd, dip_rect); |
210 for (const MONITORINFOEX& monitor_info : monitor_infos) | 247 } |
211 displays.push_back(GetDisplay(monitor_info)); | |
212 | 248 |
213 return displays; | 249 void ScreenWin::OnDisplaysChanged( |
| 250 const std::vector<gfx::win::ScreenWinDisplay> old_screen_win_displays, |
| 251 const std::vector<gfx::win::ScreenWinDisplay> new_screen_win_displays) { |
| 252 std::vector<gfx::Display> old_displays = |
| 253 ToVectorOfDisplays(old_screen_win_displays); |
| 254 std::vector<gfx::Display> new_displays = |
| 255 ToVectorOfDisplays(new_screen_win_displays); |
| 256 change_notifier_.NotifyDisplaysChanged(old_displays, new_displays); |
214 } | 257 } |
215 | 258 |
216 } // namespace gfx | 259 } // namespace gfx |
OLD | NEW |