OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/gfx/screen_win.h" |
| 6 |
| 7 #include <windows.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #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" |
| 16 #include "ui/gfx/win/screen_win_display.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 std::vector<gfx::win::ScreenWinDisplay> DisplayInfosToScreenWinDisplays( |
| 21 const std::vector<gfx::win::DisplayInfo>& display_infos) { |
| 22 std::vector<gfx::win::ScreenWinDisplay> screen_win_displays; |
| 23 for (const auto& display_info : display_infos) |
| 24 screen_win_displays.push_back(gfx::win::ScreenWinDisplay(display_info)); |
| 25 |
| 26 return screen_win_displays; |
| 27 } |
| 28 |
| 29 std::vector<gfx::Display> ScreenWinDisplaysToDisplays( |
| 30 const std::vector<gfx::win::ScreenWinDisplay>& screen_win_displays) { |
| 31 std::vector<gfx::Display> displays; |
| 32 for (const auto& screen_win_display : screen_win_displays) |
| 33 displays.push_back(screen_win_display.display()); |
| 34 |
| 35 return displays; |
| 36 } |
| 37 |
| 38 MONITORINFOEX MonitorInfoFromHMONITOR(HMONITOR monitor) { |
| 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 } |
| 45 |
| 46 BOOL CALLBACK EnumMonitorCallback(HMONITOR monitor, |
| 47 HDC hdc, |
| 48 LPRECT rect, |
| 49 LPARAM data) { |
| 50 std::vector<gfx::win::DisplayInfo>* display_infos = |
| 51 reinterpret_cast<std::vector<gfx::win::DisplayInfo>*>(data); |
| 52 DCHECK(display_infos); |
| 53 display_infos->push_back( |
| 54 gfx::win::DisplayInfo(MonitorInfoFromHMONITOR(monitor), |
| 55 gfx::GetDPIScale())); |
| 56 return TRUE; |
| 57 } |
| 58 |
| 59 std::vector<gfx::win::DisplayInfo> GetDisplayInfosFromSystem() { |
| 60 std::vector<gfx::win::DisplayInfo> display_infos; |
| 61 EnumDisplayMonitors(nullptr, nullptr, EnumMonitorCallback, |
| 62 reinterpret_cast<LPARAM>(&display_infos)); |
| 63 DCHECK_EQ(static_cast<size_t>(::GetSystemMetrics(SM_CMONITORS)), |
| 64 display_infos.size()); |
| 65 return display_infos; |
| 66 } |
| 67 |
| 68 } // namespace |
| 69 |
| 70 namespace gfx { |
| 71 |
| 72 ScreenWin::ScreenWin() { |
| 73 Initialize(); |
| 74 } |
| 75 |
| 76 ScreenWin::~ScreenWin() = default; |
| 77 |
| 78 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const { |
| 79 NOTREACHED(); |
| 80 return nullptr; |
| 81 } |
| 82 |
| 83 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { |
| 84 NOTREACHED(); |
| 85 return nullptr; |
| 86 } |
| 87 |
| 88 gfx::Point ScreenWin::GetCursorScreenPoint() { |
| 89 POINT pt; |
| 90 ::GetCursorPos(&pt); |
| 91 gfx::Point cursor_pos_pixels(pt); |
| 92 return gfx::win::ScreenToDIPPoint(cursor_pos_pixels); |
| 93 } |
| 94 |
| 95 gfx::NativeWindow ScreenWin::GetWindowUnderCursor() { |
| 96 POINT cursor_loc; |
| 97 HWND hwnd = |
| 98 ::GetCursorPos(&cursor_loc) ? ::WindowFromPoint(cursor_loc) : nullptr; |
| 99 return GetNativeWindowFromHWND(hwnd); |
| 100 } |
| 101 |
| 102 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) { |
| 103 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point); |
| 104 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT())); |
| 105 } |
| 106 |
| 107 int ScreenWin::GetNumDisplays() const { |
| 108 return static_cast<int>(screen_win_displays_.size()); |
| 109 } |
| 110 |
| 111 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const { |
| 112 return ScreenWinDisplaysToDisplays(screen_win_displays_); |
| 113 } |
| 114 |
| 115 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { |
| 116 HWND window_hwnd = GetHWNDFromNativeView(window); |
| 117 if (!window_hwnd) { |
| 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 |
| 120 // scaling factor. |
| 121 return GetPrimaryDisplay(); |
| 122 } |
| 123 gfx::win::ScreenWinDisplay screen_win_display = |
| 124 GetScreenWinDisplayNearestHWND(window_hwnd); |
| 125 return screen_win_display.display(); |
| 126 } |
| 127 |
| 128 gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const { |
| 129 gfx::Point screen_point(gfx::win::DIPToScreenPoint(point)); |
| 130 gfx::win::ScreenWinDisplay screen_win_display = |
| 131 GetScreenWinDisplayNearestScreenPoint(screen_point); |
| 132 return screen_win_display.display(); |
| 133 } |
| 134 |
| 135 gfx::Display ScreenWin::GetDisplayMatching(const gfx::Rect& match_rect) const { |
| 136 gfx::win::ScreenWinDisplay screen_win_display = |
| 137 GetScreenWinDisplayNearestScreenRect(match_rect); |
| 138 return screen_win_display.display(); |
| 139 } |
| 140 |
| 141 gfx::Display ScreenWin::GetPrimaryDisplay() const { |
| 142 return GetPrimaryScreenWinDisplay().display(); |
| 143 } |
| 144 |
| 145 void ScreenWin::AddObserver(DisplayObserver* observer) { |
| 146 change_notifier_.AddObserver(observer); |
| 147 } |
| 148 |
| 149 void ScreenWin::RemoveObserver(DisplayObserver* observer) { |
| 150 change_notifier_.RemoveObserver(observer); |
| 151 } |
| 152 |
| 153 void ScreenWin::UpdateFromDisplayInfos( |
| 154 const std::vector<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, |
| 189 UINT message, |
| 190 WPARAM wparam, |
| 191 LPARAM lparam) { |
| 192 if (message != WM_DISPLAYCHANGE) |
| 193 return; |
| 194 |
| 195 std::vector<gfx::Display> old_displays = GetAllDisplays(); |
| 196 UpdateFromDisplayInfos(GetDisplayInfosFromSystem()); |
| 197 change_notifier_.NotifyDisplaysChanged(old_displays, GetAllDisplays()); |
| 198 } |
| 199 |
| 200 gfx::win::ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestHWND(HWND hwnd) |
| 201 const { |
| 202 return GetScreenWinDisplay(MonitorInfoFromWindow(hwnd, |
| 203 MONITOR_DEFAULTTONEAREST)); |
| 204 } |
| 205 |
| 206 gfx::win::ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestScreenRect( |
| 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 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 } |
| 242 |
| 243 } // namespace gfx |
OLD | NEW |