| 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/display/win/screen_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "ui/display/win/display_info.h" |
| 12 #include "ui/display/win/screen_win_display.h" |
| 11 #include "ui/gfx/display.h" | 13 #include "ui/gfx/display.h" |
| 12 #include "ui/gfx/geometry/point.h" | 14 #include "ui/gfx/geometry/point.h" |
| 13 #include "ui/gfx/geometry/rect.h" | 15 #include "ui/gfx/geometry/rect.h" |
| 14 #include "ui/gfx/win/display_info.h" | |
| 15 #include "ui/gfx/win/dpi.h" | 16 #include "ui/gfx/win/dpi.h" |
| 16 #include "ui/gfx/win/screen_win_display.h" | 17 |
| 18 namespace display { |
| 19 namespace win { |
| 17 | 20 |
| 18 namespace { | 21 namespace { |
| 19 | 22 |
| 20 std::vector<gfx::win::ScreenWinDisplay> DisplayInfosToScreenWinDisplays( | 23 std::vector<ScreenWinDisplay> DisplayInfosToScreenWinDisplays( |
| 21 const std::vector<gfx::win::DisplayInfo>& display_infos) { | 24 const std::vector<DisplayInfo>& display_infos) { |
| 22 std::vector<gfx::win::ScreenWinDisplay> screen_win_displays; | 25 std::vector<ScreenWinDisplay> screen_win_displays; |
| 23 for (const auto& display_info : display_infos) | 26 for (const auto& display_info : display_infos) |
| 24 screen_win_displays.push_back(gfx::win::ScreenWinDisplay(display_info)); | 27 screen_win_displays.push_back(ScreenWinDisplay(display_info)); |
| 25 | 28 |
| 26 return screen_win_displays; | 29 return screen_win_displays; |
| 27 } | 30 } |
| 28 | 31 |
| 29 std::vector<gfx::Display> ScreenWinDisplaysToDisplays( | 32 std::vector<gfx::Display> ScreenWinDisplaysToDisplays( |
| 30 const std::vector<gfx::win::ScreenWinDisplay>& screen_win_displays) { | 33 const std::vector<ScreenWinDisplay>& screen_win_displays) { |
| 31 std::vector<gfx::Display> displays; | 34 std::vector<gfx::Display> displays; |
| 32 for (const auto& screen_win_display : screen_win_displays) | 35 for (const auto& screen_win_display : screen_win_displays) |
| 33 displays.push_back(screen_win_display.display()); | 36 displays.push_back(screen_win_display.display()); |
| 34 | 37 |
| 35 return displays; | 38 return displays; |
| 36 } | 39 } |
| 37 | 40 |
| 38 MONITORINFOEX MonitorInfoFromHMONITOR(HMONITOR monitor) { | 41 MONITORINFOEX MonitorInfoFromHMONITOR(HMONITOR monitor) { |
| 39 MONITORINFOEX monitor_info; | 42 MONITORINFOEX monitor_info; |
| 40 ::ZeroMemory(&monitor_info, sizeof(monitor_info)); | 43 ::ZeroMemory(&monitor_info, sizeof(monitor_info)); |
| 41 monitor_info.cbSize = sizeof(monitor_info); | 44 monitor_info.cbSize = sizeof(monitor_info); |
| 42 ::GetMonitorInfo(monitor, &monitor_info); | 45 ::GetMonitorInfo(monitor, &monitor_info); |
| 43 return monitor_info; | 46 return monitor_info; |
| 44 } | 47 } |
| 45 | 48 |
| 46 BOOL CALLBACK EnumMonitorCallback(HMONITOR monitor, | 49 BOOL CALLBACK EnumMonitorCallback(HMONITOR monitor, |
| 47 HDC hdc, | 50 HDC hdc, |
| 48 LPRECT rect, | 51 LPRECT rect, |
| 49 LPARAM data) { | 52 LPARAM data) { |
| 50 std::vector<gfx::win::DisplayInfo>* display_infos = | 53 std::vector<DisplayInfo>* display_infos = |
| 51 reinterpret_cast<std::vector<gfx::win::DisplayInfo>*>(data); | 54 reinterpret_cast<std::vector<DisplayInfo>*>(data); |
| 52 DCHECK(display_infos); | 55 DCHECK(display_infos); |
| 53 display_infos->push_back( | 56 display_infos->push_back(DisplayInfo(MonitorInfoFromHMONITOR(monitor), |
| 54 gfx::win::DisplayInfo(MonitorInfoFromHMONITOR(monitor), | 57 gfx::GetDPIScale())); |
| 55 gfx::GetDPIScale())); | |
| 56 return TRUE; | 58 return TRUE; |
| 57 } | 59 } |
| 58 | 60 |
| 59 std::vector<gfx::win::DisplayInfo> GetDisplayInfosFromSystem() { | 61 std::vector<DisplayInfo> GetDisplayInfosFromSystem() { |
| 60 std::vector<gfx::win::DisplayInfo> display_infos; | 62 std::vector<DisplayInfo> display_infos; |
| 61 EnumDisplayMonitors(nullptr, nullptr, EnumMonitorCallback, | 63 EnumDisplayMonitors(nullptr, nullptr, EnumMonitorCallback, |
| 62 reinterpret_cast<LPARAM>(&display_infos)); | 64 reinterpret_cast<LPARAM>(&display_infos)); |
| 63 DCHECK_EQ(static_cast<size_t>(::GetSystemMetrics(SM_CMONITORS)), | 65 DCHECK_EQ(static_cast<size_t>(::GetSystemMetrics(SM_CMONITORS)), |
| 64 display_infos.size()); | 66 display_infos.size()); |
| 65 return display_infos; | 67 return display_infos; |
| 66 } | 68 } |
| 67 | 69 |
| 68 } // namespace | 70 } // namespace |
| 69 | 71 |
| 70 namespace gfx { | |
| 71 | |
| 72 ScreenWin::ScreenWin() { | 72 ScreenWin::ScreenWin() { |
| 73 Initialize(); | 73 Initialize(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 ScreenWin::~ScreenWin() = default; | 76 ScreenWin::~ScreenWin() = default; |
| 77 | 77 |
| 78 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const { | 78 HWND ScreenWin::GetHWNDFromNativeView(gfx::NativeView window) const { |
| 79 NOTREACHED(); | 79 NOTREACHED(); |
| 80 return nullptr; | 80 return nullptr; |
| 81 } | 81 } |
| 82 | 82 |
| 83 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { | 83 gfx::NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { |
| 84 NOTREACHED(); | 84 NOTREACHED(); |
| 85 return nullptr; | 85 return nullptr; |
| 86 } | 86 } |
| 87 | 87 |
| 88 gfx::Point ScreenWin::GetCursorScreenPoint() { | 88 gfx::Point ScreenWin::GetCursorScreenPoint() { |
| 89 POINT pt; | 89 POINT pt; |
| 90 ::GetCursorPos(&pt); | 90 ::GetCursorPos(&pt); |
| 91 gfx::Point cursor_pos_pixels(pt); | 91 gfx::Point cursor_pos_pixels(pt); |
| 92 return gfx::win::ScreenToDIPPoint(cursor_pos_pixels); | 92 return gfx::win::ScreenToDIPPoint(cursor_pos_pixels); |
| 93 } | 93 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 113 } | 113 } |
| 114 | 114 |
| 115 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { | 115 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { |
| 116 HWND window_hwnd = GetHWNDFromNativeView(window); | 116 HWND window_hwnd = GetHWNDFromNativeView(window); |
| 117 if (!window_hwnd) { | 117 if (!window_hwnd) { |
| 118 // When |window| isn't rooted to a display, we should just return the | 118 // When |window| isn't rooted to a display, we should just return the |
| 119 // default display so we get some correct display information like the | 119 // default display so we get some correct display information like the |
| 120 // scaling factor. | 120 // scaling factor. |
| 121 return GetPrimaryDisplay(); | 121 return GetPrimaryDisplay(); |
| 122 } | 122 } |
| 123 gfx::win::ScreenWinDisplay screen_win_display = | 123 ScreenWinDisplay screen_win_display = |
| 124 GetScreenWinDisplayNearestHWND(window_hwnd); | 124 GetScreenWinDisplayNearestHWND(window_hwnd); |
| 125 return screen_win_display.display(); | 125 return screen_win_display.display(); |
| 126 } | 126 } |
| 127 | 127 |
| 128 gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const { | 128 gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const { |
| 129 gfx::Point screen_point(gfx::win::DIPToScreenPoint(point)); | 129 gfx::Point screen_point(gfx::win::DIPToScreenPoint(point)); |
| 130 gfx::win::ScreenWinDisplay screen_win_display = | 130 ScreenWinDisplay screen_win_display = |
| 131 GetScreenWinDisplayNearestScreenPoint(screen_point); | 131 GetScreenWinDisplayNearestScreenPoint(screen_point); |
| 132 return screen_win_display.display(); | 132 return screen_win_display.display(); |
| 133 } | 133 } |
| 134 | 134 |
| 135 gfx::Display ScreenWin::GetDisplayMatching(const gfx::Rect& match_rect) const { | 135 gfx::Display ScreenWin::GetDisplayMatching(const gfx::Rect& match_rect) const { |
| 136 gfx::win::ScreenWinDisplay screen_win_display = | 136 ScreenWinDisplay screen_win_display = |
| 137 GetScreenWinDisplayNearestScreenRect(match_rect); | 137 GetScreenWinDisplayNearestScreenRect(match_rect); |
| 138 return screen_win_display.display(); | 138 return screen_win_display.display(); |
| 139 } | 139 } |
| 140 | 140 |
| 141 gfx::Display ScreenWin::GetPrimaryDisplay() const { | 141 gfx::Display ScreenWin::GetPrimaryDisplay() const { |
| 142 return GetPrimaryScreenWinDisplay().display(); | 142 return GetPrimaryScreenWinDisplay().display(); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void ScreenWin::AddObserver(DisplayObserver* observer) { | 145 void ScreenWin::AddObserver(gfx::DisplayObserver* observer) { |
| 146 change_notifier_.AddObserver(observer); | 146 change_notifier_.AddObserver(observer); |
| 147 } | 147 } |
| 148 | 148 |
| 149 void ScreenWin::RemoveObserver(DisplayObserver* observer) { | 149 void ScreenWin::RemoveObserver(gfx::DisplayObserver* observer) { |
| 150 change_notifier_.RemoveObserver(observer); | 150 change_notifier_.RemoveObserver(observer); |
| 151 } | 151 } |
| 152 | 152 |
| 153 void ScreenWin::UpdateFromDisplayInfos( | 153 void ScreenWin::UpdateFromDisplayInfos( |
| 154 const std::vector<gfx::win::DisplayInfo>& display_infos) { | 154 const std::vector<DisplayInfo>& display_infos) { |
| 155 screen_win_displays_ = DisplayInfosToScreenWinDisplays(display_infos); | 155 screen_win_displays_ = DisplayInfosToScreenWinDisplays(display_infos); |
| 156 } | 156 } |
| 157 | 157 |
| 158 void ScreenWin::Initialize() { | 158 void ScreenWin::Initialize() { |
| 159 singleton_hwnd_observer_.reset( | 159 singleton_hwnd_observer_.reset( |
| 160 new gfx::SingletonHwndObserver( | 160 new gfx::SingletonHwndObserver( |
| 161 base::Bind(&ScreenWin::OnWndProc, base::Unretained(this)))); | 161 base::Bind(&ScreenWin::OnWndProc, base::Unretained(this)))); |
| 162 UpdateFromDisplayInfos(GetDisplayInfosFromSystem()); | 162 UpdateFromDisplayInfos(GetDisplayInfosFromSystem()); |
| 163 } | 163 } |
| 164 | 164 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 190 WPARAM wparam, | 190 WPARAM wparam, |
| 191 LPARAM lparam) { | 191 LPARAM lparam) { |
| 192 if (message != WM_DISPLAYCHANGE) | 192 if (message != WM_DISPLAYCHANGE) |
| 193 return; | 193 return; |
| 194 | 194 |
| 195 std::vector<gfx::Display> old_displays = GetAllDisplays(); | 195 std::vector<gfx::Display> old_displays = GetAllDisplays(); |
| 196 UpdateFromDisplayInfos(GetDisplayInfosFromSystem()); | 196 UpdateFromDisplayInfos(GetDisplayInfosFromSystem()); |
| 197 change_notifier_.NotifyDisplaysChanged(old_displays, GetAllDisplays()); | 197 change_notifier_.NotifyDisplaysChanged(old_displays, GetAllDisplays()); |
| 198 } | 198 } |
| 199 | 199 |
| 200 gfx::win::ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestHWND(HWND hwnd) | 200 ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestHWND(HWND hwnd) |
| 201 const { | 201 const { |
| 202 return GetScreenWinDisplay(MonitorInfoFromWindow(hwnd, | 202 return GetScreenWinDisplay(MonitorInfoFromWindow(hwnd, |
| 203 MONITOR_DEFAULTTONEAREST)); | 203 MONITOR_DEFAULTTONEAREST)); |
| 204 } | 204 } |
| 205 | 205 |
| 206 gfx::win::ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestScreenRect( | 206 ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestScreenRect( |
| 207 const Rect& screen_rect) const { | 207 const gfx::Rect& screen_rect) const { |
| 208 return GetScreenWinDisplay(MonitorInfoFromScreenRect(screen_rect)); | 208 return GetScreenWinDisplay(MonitorInfoFromScreenRect(screen_rect)); |
| 209 } | 209 } |
| 210 | 210 |
| 211 gfx::win::ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestScreenPoint( | 211 ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestScreenPoint( |
| 212 const Point& screen_point) const { | 212 const gfx::Point& screen_point) const { |
| 213 return GetScreenWinDisplay(MonitorInfoFromScreenPoint(screen_point)); | 213 return GetScreenWinDisplay(MonitorInfoFromScreenPoint(screen_point)); |
| 214 } | 214 } |
| 215 | 215 |
| 216 gfx::win::ScreenWinDisplay ScreenWin::GetPrimaryScreenWinDisplay() const { | 216 ScreenWinDisplay ScreenWin::GetPrimaryScreenWinDisplay() const { |
| 217 MONITORINFOEX monitor_info = MonitorInfoFromWindow(nullptr, | 217 MONITORINFOEX monitor_info = MonitorInfoFromWindow(nullptr, |
| 218 MONITOR_DEFAULTTOPRIMARY); | 218 MONITOR_DEFAULTTOPRIMARY); |
| 219 gfx::win::ScreenWinDisplay screen_win_display = | 219 ScreenWinDisplay screen_win_display = GetScreenWinDisplay(monitor_info); |
| 220 GetScreenWinDisplay(monitor_info); | |
| 221 gfx::Display display = screen_win_display.display(); | 220 gfx::Display display = screen_win_display.display(); |
| 222 // The Windows primary monitor is defined to have an origin of (0, 0). | 221 // The Windows primary monitor is defined to have an origin of (0, 0). |
| 223 DCHECK_EQ(0, display.bounds().origin().x()); | 222 DCHECK_EQ(0, display.bounds().origin().x()); |
| 224 DCHECK_EQ(0, display.bounds().origin().y()); | 223 DCHECK_EQ(0, display.bounds().origin().y()); |
| 225 return screen_win_display; | 224 return screen_win_display; |
| 226 } | 225 } |
| 227 | 226 |
| 228 gfx::win::ScreenWinDisplay ScreenWin::GetScreenWinDisplay( | 227 ScreenWinDisplay ScreenWin::GetScreenWinDisplay( |
| 229 const MONITORINFOEX& monitor_info) const { | 228 const MONITORINFOEX& monitor_info) const { |
| 230 int64_t id = | 229 int64_t id = DisplayInfo::DeviceIdFromDeviceName(monitor_info.szDevice); |
| 231 gfx::win::DisplayInfo::DeviceIdFromDeviceName(monitor_info.szDevice); | |
| 232 for (const auto& screen_win_display : screen_win_displays_) { | 230 for (const auto& screen_win_display : screen_win_displays_) { |
| 233 if (screen_win_display.display().id() == id) | 231 if (screen_win_display.display().id() == id) |
| 234 return screen_win_display; | 232 return screen_win_display; |
| 235 } | 233 } |
| 236 // There is 1:1 correspondence between MONITORINFOEX and ScreenWinDisplay. | 234 // There is 1:1 correspondence between MONITORINFOEX and ScreenWinDisplay. |
| 237 // If we make it here, it means we have no displays and we should hand out the | 235 // If we make it here, it means we have no displays and we should hand out the |
| 238 // default display. | 236 // default display. |
| 239 DCHECK_EQ(screen_win_displays_.size(), 0u); | 237 DCHECK_EQ(screen_win_displays_.size(), 0u); |
| 240 return gfx::win::ScreenWinDisplay(); | 238 return ScreenWinDisplay(); |
| 241 } | 239 } |
| 242 | 240 |
| 243 } // namespace gfx | 241 } // namespace win |
| 242 } // namespace display |
| OLD | NEW |