| 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> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/bind_helpers.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" |
| 11 #include "ui/gfx/display.h" | 16 #include "ui/gfx/display.h" |
| 12 #include "ui/gfx/geometry/point.h" | |
| 13 #include "ui/gfx/geometry/rect.h" | |
| 14 #include "ui/gfx/win/display_info.h" | |
| 15 #include "ui/gfx/win/dpi.h" | 17 #include "ui/gfx/win/dpi.h" |
| 16 #include "ui/gfx/win/screen_win_display.h" | |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 std::vector<const gfx::win::ScreenWinDisplay> DisplayInfosToScreenWinDisplays( | 21 MONITORINFOEX GetMonitorInfoForMonitor(HMONITOR monitor) { |
| 21 const std::vector<const gfx::win::DisplayInfo>& display_infos) { | 22 MONITORINFOEX monitor_info; |
| 22 std::vector<const gfx::win::ScreenWinDisplay> screen_win_displays; | 23 ZeroMemory(&monitor_info, sizeof(MONITORINFOEX)); |
| 23 for (const auto& display_info : display_infos) | 24 monitor_info.cbSize = sizeof(monitor_info); |
| 24 screen_win_displays.push_back(gfx::win::ScreenWinDisplay(display_info)); | 25 GetMonitorInfo(monitor, &monitor_info); |
| 25 | 26 return monitor_info; |
| 26 return screen_win_displays; | |
| 27 } | 27 } |
| 28 | 28 |
| 29 std::vector<gfx::Display> ScreenWinDisplaysToDisplays( | 29 gfx::Display GetDisplay(const MONITORINFOEX& monitor_info) { |
| 30 const std::vector<const gfx::win::ScreenWinDisplay>& screen_win_displays) { | 30 int64_t id = |
| 31 std::vector<gfx::Display> displays; | 31 static_cast<int64_t>(base::Hash(base::WideToUTF8(monitor_info.szDevice))); |
| 32 for (const auto& screen_win_display : screen_win_displays) | 32 gfx::Rect bounds = gfx::Rect(monitor_info.rcMonitor); |
| 33 displays.push_back(screen_win_display.display); | 33 gfx::Display display(id); |
| 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); |
| 34 | 38 |
| 35 return displays; | 39 DEVMODE mode; |
| 36 } | 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 } |
| 37 | 63 |
| 38 MONITORINFOEX MonitorInfoFromHMONITOR(HMONITOR monitor) { | 64 return display; |
| 39 MONITORINFOEX monitor_info; | |
| 40 ::ZeroMemory(&monitor_info, sizeof(monitor_info)); | |
| 41 monitor_info.cbSize = sizeof(monitor_info); | |
| 42 ::GetMonitorInfo(monitor, &monitor_info); | |
| 43 return monitor_info; | |
| 44 } | 65 } |
| 45 | 66 |
| 46 BOOL CALLBACK EnumMonitorCallback(HMONITOR monitor, | 67 BOOL CALLBACK EnumMonitorCallback(HMONITOR monitor, |
| 47 HDC hdc, | 68 HDC hdc, |
| 48 LPRECT rect, | 69 LPRECT rect, |
| 49 LPARAM data) { | 70 LPARAM data) { |
| 50 std::vector<const gfx::win::DisplayInfo>* display_infos = | 71 std::vector<gfx::Display>* all_displays = |
| 51 reinterpret_cast<std::vector<const gfx::win::DisplayInfo>*>(data); | 72 reinterpret_cast<std::vector<gfx::Display>*>(data); |
| 52 DCHECK(display_infos); | 73 DCHECK(all_displays); |
| 53 display_infos->push_back( | 74 |
| 54 gfx::win::DisplayInfo(MonitorInfoFromHMONITOR(monitor), | 75 MONITORINFOEX monitor_info = GetMonitorInfoForMonitor(monitor); |
| 55 gfx::GetDPIScale())); | 76 gfx::Display display = GetDisplay(monitor_info); |
| 77 all_displays->push_back(display); |
| 56 return TRUE; | 78 return TRUE; |
| 57 } | 79 } |
| 58 | 80 |
| 59 std::vector<const gfx::win::DisplayInfo> GetDisplayInfosFromSystem() { | 81 std::vector<gfx::Display> GetDisplays() { |
| 60 std::vector<const gfx::win::DisplayInfo> display_infos; | 82 std::vector<gfx::Display> displays; |
| 61 EnumDisplayMonitors(nullptr, nullptr, EnumMonitorCallback, | 83 EnumDisplayMonitors(NULL, NULL, EnumMonitorCallback, |
| 62 reinterpret_cast<LPARAM>(&display_infos)); | 84 reinterpret_cast<LPARAM>(&displays)); |
| 63 DCHECK_EQ(static_cast<size_t>(::GetSystemMetrics(SM_CMONITORS)), | 85 return displays; |
| 64 display_infos.size()); | |
| 65 return display_infos; | |
| 66 } | 86 } |
| 67 | 87 |
| 68 } // namespace | 88 } // namespace |
| 69 | 89 |
| 70 namespace gfx { | 90 namespace gfx { |
| 71 | 91 |
| 72 ScreenWin::ScreenWin() { | 92 ScreenWin::ScreenWin() |
| 73 Initialize(); | 93 : singleton_hwnd_observer_(new SingletonHwndObserver( |
| 94 base::Bind(&ScreenWin::OnWndProc, base::Unretained(this)))), |
| 95 displays_(GetDisplays()) { |
| 74 } | 96 } |
| 75 | 97 |
| 76 ScreenWin::~ScreenWin() = default; | 98 ScreenWin::~ScreenWin() {} |
| 77 | 99 |
| 78 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const { | 100 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const { |
| 79 NOTREACHED(); | 101 NOTREACHED(); |
| 80 return nullptr; | 102 return NULL; |
| 81 } | 103 } |
| 82 | 104 |
| 83 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { | 105 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { |
| 84 NOTREACHED(); | 106 NOTREACHED(); |
| 85 return nullptr; | 107 return NULL; |
| 86 } | 108 } |
| 87 | 109 |
| 88 gfx::Point ScreenWin::GetCursorScreenPoint() { | 110 gfx::Point ScreenWin::GetCursorScreenPoint() { |
| 89 POINT pt; | 111 POINT pt; |
| 90 ::GetCursorPos(&pt); | 112 GetCursorPos(&pt); |
| 91 gfx::Point cursor_pos_pixels(pt); | 113 gfx::Point cursor_pos_pixels(pt); |
| 92 return gfx::win::ScreenToDIPPoint(cursor_pos_pixels); | 114 return gfx::win::ScreenToDIPPoint(cursor_pos_pixels); |
| 93 } | 115 } |
| 94 | 116 |
| 95 gfx::NativeWindow ScreenWin::GetWindowUnderCursor() { | 117 gfx::NativeWindow ScreenWin::GetWindowUnderCursor() { |
| 96 POINT cursor_loc; | 118 POINT cursor_loc; |
| 97 HWND hwnd = | 119 HWND hwnd = GetCursorPos(&cursor_loc) ? WindowFromPoint(cursor_loc) : NULL; |
| 98 ::GetCursorPos(&cursor_loc) ? ::WindowFromPoint(cursor_loc) : nullptr; | |
| 99 return GetNativeWindowFromHWND(hwnd); | 120 return GetNativeWindowFromHWND(hwnd); |
| 100 } | 121 } |
| 101 | 122 |
| 102 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) { | 123 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) { |
| 103 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point); | 124 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point); |
| 104 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT())); | 125 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT())); |
| 105 } | 126 } |
| 106 | 127 |
| 107 int ScreenWin::GetNumDisplays() const { | 128 int ScreenWin::GetNumDisplays() const { |
| 108 return static_cast<int>(screen_win_displays_.size()); | 129 return GetSystemMetrics(SM_CMONITORS); |
| 109 } | 130 } |
| 110 | 131 |
| 111 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const { | 132 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const { |
| 112 return ScreenWinDisplaysToDisplays(screen_win_displays_); | 133 return displays_; |
| 113 } | 134 } |
| 114 | 135 |
| 115 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { | 136 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { |
| 116 HWND window_hwnd = GetHWNDFromNativeView(window); | 137 HWND window_hwnd = GetHWNDFromNativeView(window); |
| 117 if (!window_hwnd) { | 138 if (!window_hwnd) { |
| 118 // When |window| isn't rooted to a display, we should just return the | 139 // 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 | 140 // default display so we get some correct display information like the |
| 120 // scaling factor. | 141 // scaling factor. |
| 121 return GetPrimaryDisplay(); | 142 return GetPrimaryDisplay(); |
| 122 } | 143 } |
| 123 const gfx::win::ScreenWinDisplay screen_win_display = | 144 |
| 124 GetScreenWinDisplayNearestHWND(window_hwnd); | 145 MONITORINFOEX monitor_info; |
| 125 return screen_win_display.display; | 146 monitor_info.cbSize = sizeof(monitor_info); |
| 147 GetMonitorInfo(MonitorFromWindow(window_hwnd, MONITOR_DEFAULTTONEAREST), |
| 148 &monitor_info); |
| 149 return GetDisplay(monitor_info); |
| 126 } | 150 } |
| 127 | 151 |
| 128 gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const { | 152 gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const { |
| 129 gfx::Point screen_point(gfx::win::DIPToScreenPoint(point)); | 153 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point); |
| 130 const gfx::win::ScreenWinDisplay screen_win_display = | 154 POINT initial_loc = { point_in_pixels.x(), point_in_pixels.y() }; |
| 131 GetScreenWinDisplayNearestScreenPoint(screen_point); | 155 HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST); |
| 132 return screen_win_display.display; | 156 MONITORINFOEX mi; |
| 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(); |
| 133 } | 163 } |
| 134 | 164 |
| 135 gfx::Display ScreenWin::GetDisplayMatching(const gfx::Rect& match_rect) const { | 165 gfx::Display ScreenWin::GetDisplayMatching(const gfx::Rect& match_rect) const { |
| 136 const gfx::win::ScreenWinDisplay screen_win_display = | 166 RECT other_bounds_rect = match_rect.ToRECT(); |
| 137 GetScreenWinDisplayNearestScreenRect(match_rect); | 167 MONITORINFOEX monitor_info = GetMonitorInfoForMonitor(MonitorFromRect( |
| 138 return screen_win_display.display; | 168 &other_bounds_rect, MONITOR_DEFAULTTONEAREST)); |
| 169 return GetDisplay(monitor_info); |
| 139 } | 170 } |
| 140 | 171 |
| 141 gfx::Display ScreenWin::GetPrimaryDisplay() const { | 172 gfx::Display ScreenWin::GetPrimaryDisplay() const { |
| 142 return GetPrimaryScreenWinDisplay().display; | 173 MONITORINFOEX mi = GetMonitorInfoForMonitor( |
| 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; |
| 143 } | 183 } |
| 144 | 184 |
| 145 void ScreenWin::AddObserver(DisplayObserver* observer) { | 185 void ScreenWin::AddObserver(DisplayObserver* observer) { |
| 146 change_notifier_.AddObserver(observer); | 186 change_notifier_.AddObserver(observer); |
| 147 } | 187 } |
| 148 | 188 |
| 149 void ScreenWin::RemoveObserver(DisplayObserver* observer) { | 189 void ScreenWin::RemoveObserver(DisplayObserver* observer) { |
| 150 change_notifier_.RemoveObserver(observer); | 190 change_notifier_.RemoveObserver(observer); |
| 151 } | 191 } |
| 152 | 192 |
| 153 void ScreenWin::UpdateFromDisplayInfos( | |
| 154 const std::vector<const gfx::win::DisplayInfo>& display_infos) { | |
| 155 screen_win_displays_ = DisplayInfosToScreenWinDisplays(display_infos); | |
| 156 } | |
| 157 | |
| 158 void ScreenWin::Initialize() { | |
| 159 singleton_hwnd_observer_.reset( | |
| 160 new gfx::SingletonHwndObserver( | |
| 161 base::Bind(&ScreenWin::OnWndProc, base::Unretained(this)))); | |
| 162 UpdateFromDisplayInfos(GetDisplayInfosFromSystem()); | |
| 163 } | |
| 164 | |
| 165 MONITORINFOEX ScreenWin::MonitorInfoFromScreenPoint( | |
| 166 const gfx::Point& screen_point) const { | |
| 167 POINT initial_loc = { screen_point.x(), screen_point.y() }; | |
| 168 return MonitorInfoFromHMONITOR(::MonitorFromPoint(initial_loc, | |
| 169 MONITOR_DEFAULTTONEAREST)); | |
| 170 } | |
| 171 | |
| 172 MONITORINFOEX ScreenWin::MonitorInfoFromScreenRect(const gfx::Rect& screen_rect) | |
| 173 const { | |
| 174 RECT win_rect = screen_rect.ToRECT(); | |
| 175 return MonitorInfoFromHMONITOR(::MonitorFromRect(&win_rect, | |
| 176 MONITOR_DEFAULTTONEAREST)); | |
| 177 } | |
| 178 | |
| 179 MONITORINFOEX ScreenWin::MonitorInfoFromWindow(HWND hwnd, | |
| 180 DWORD default_options) const { | |
| 181 return MonitorInfoFromHMONITOR(::MonitorFromWindow(hwnd, default_options)); | |
| 182 } | |
| 183 | |
| 184 HWND ScreenWin::GetRootWindow(HWND hwnd) const { | |
| 185 return ::GetAncestor(hwnd, GA_ROOT); | |
| 186 } | |
| 187 | |
| 188 void ScreenWin::OnWndProc(HWND hwnd, | 193 void ScreenWin::OnWndProc(HWND hwnd, |
| 189 UINT message, | 194 UINT message, |
| 190 WPARAM wparam, | 195 WPARAM wparam, |
| 191 LPARAM lparam) { | 196 LPARAM lparam) { |
| 192 if (message != WM_DISPLAYCHANGE) | 197 if (message != WM_DISPLAYCHANGE) |
| 193 return; | 198 return; |
| 194 | 199 |
| 195 std::vector<gfx::Display> old_displays = GetAllDisplays(); | 200 std::vector<gfx::Display> old_displays = displays_; |
| 196 UpdateFromDisplayInfos(GetDisplayInfosFromSystem()); | 201 displays_ = GetDisplays(); |
| 197 change_notifier_.NotifyDisplaysChanged(old_displays, GetAllDisplays()); | 202 |
| 203 change_notifier_.NotifyDisplaysChanged(old_displays, displays_); |
| 198 } | 204 } |
| 199 | 205 |
| 200 gfx::win::ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestHWND(HWND hwnd) | 206 // static |
| 201 const { | 207 std::vector<gfx::Display> ScreenWin::GetDisplaysForMonitorInfos( |
| 202 return GetScreenWinDisplay(MonitorInfoFromWindow(hwnd, | 208 const std::vector<MONITORINFOEX>& monitor_infos) { |
| 203 MONITOR_DEFAULTTONEAREST)); | 209 std::vector<gfx::Display> displays; |
| 204 } | 210 for (const MONITORINFOEX& monitor_info : monitor_infos) |
| 211 displays.push_back(GetDisplay(monitor_info)); |
| 205 | 212 |
| 206 gfx::win::ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestScreenRect( | 213 return displays; |
| 207 const Rect& screen_rect) const { | |
| 208 return GetScreenWinDisplay(MonitorInfoFromScreenRect(screen_rect)); | |
| 209 } | |
| 210 | |
| 211 gfx::win::ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestScreenPoint( | |
| 212 const Point& screen_point) const { | |
| 213 return GetScreenWinDisplay(MonitorInfoFromScreenPoint(screen_point)); | |
| 214 } | |
| 215 | |
| 216 gfx::win::ScreenWinDisplay ScreenWin::GetPrimaryScreenWinDisplay() const { | |
| 217 MONITORINFOEX monitor_info = MonitorInfoFromWindow(nullptr, | |
| 218 MONITOR_DEFAULTTOPRIMARY); | |
| 219 const gfx::win::ScreenWinDisplay screen_win_display = | |
| 220 GetScreenWinDisplay(monitor_info); | |
| 221 gfx::Display display = screen_win_display.display; | |
| 222 // The Windows primary monitor is defined to have an origin of (0, 0). | |
| 223 DCHECK_EQ(0, display.bounds().origin().x()); | |
| 224 DCHECK_EQ(0, display.bounds().origin().y()); | |
| 225 return screen_win_display; | |
| 226 } | |
| 227 | |
| 228 gfx::win::ScreenWinDisplay ScreenWin::GetScreenWinDisplay( | |
| 229 const MONITORINFOEX& monitor_info) const { | |
| 230 int64_t id = | |
| 231 gfx::win::DisplayInfo::DeviceIdFromDeviceName(monitor_info.szDevice); | |
| 232 for (const auto& screen_win_display : screen_win_displays_) { | |
| 233 if (screen_win_display.display.id() == id) | |
| 234 return screen_win_display; | |
| 235 } | |
| 236 // 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 | |
| 238 // default display. | |
| 239 DCHECK_EQ(screen_win_displays_.size(), 0u); | |
| 240 return gfx::win::ScreenWinDisplay(); | |
| 241 } | 214 } |
| 242 | 215 |
| 243 } // namespace gfx | 216 } // namespace gfx |
| OLD | NEW |