Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(469)

Side by Side Diff: ui/display/win/screen_win.cc

Issue 1834243003: Revert of Move ScreenWin to ui/display (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/display/win/screen_win.h ('k') | ui/display/win/screen_win_display.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/display/win/screen_win.h"
6
7 #include <windows.h>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "ui/display/win/display_info.h"
12 #include "ui/display/win/screen_win_display.h"
13 #include "ui/gfx/display.h"
14 #include "ui/gfx/geometry/point.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/win/dpi.h"
17
18 namespace display {
19 namespace win {
20
21 namespace {
22
23 std::vector<ScreenWinDisplay> DisplayInfosToScreenWinDisplays(
24 const std::vector<DisplayInfo>& display_infos) {
25 std::vector<ScreenWinDisplay> screen_win_displays;
26 for (const auto& display_info : display_infos)
27 screen_win_displays.push_back(ScreenWinDisplay(display_info));
28
29 return screen_win_displays;
30 }
31
32 std::vector<gfx::Display> ScreenWinDisplaysToDisplays(
33 const std::vector<ScreenWinDisplay>& screen_win_displays) {
34 std::vector<gfx::Display> displays;
35 for (const auto& screen_win_display : screen_win_displays)
36 displays.push_back(screen_win_display.display());
37
38 return displays;
39 }
40
41 MONITORINFOEX MonitorInfoFromHMONITOR(HMONITOR monitor) {
42 MONITORINFOEX monitor_info;
43 ::ZeroMemory(&monitor_info, sizeof(monitor_info));
44 monitor_info.cbSize = sizeof(monitor_info);
45 ::GetMonitorInfo(monitor, &monitor_info);
46 return monitor_info;
47 }
48
49 BOOL CALLBACK EnumMonitorCallback(HMONITOR monitor,
50 HDC hdc,
51 LPRECT rect,
52 LPARAM data) {
53 std::vector<DisplayInfo>* display_infos =
54 reinterpret_cast<std::vector<DisplayInfo>*>(data);
55 DCHECK(display_infos);
56 display_infos->push_back(DisplayInfo(MonitorInfoFromHMONITOR(monitor),
57 gfx::GetDPIScale()));
58 return TRUE;
59 }
60
61 std::vector<DisplayInfo> GetDisplayInfosFromSystem() {
62 std::vector<DisplayInfo> display_infos;
63 EnumDisplayMonitors(nullptr, nullptr, EnumMonitorCallback,
64 reinterpret_cast<LPARAM>(&display_infos));
65 DCHECK_EQ(static_cast<size_t>(::GetSystemMetrics(SM_CMONITORS)),
66 display_infos.size());
67 return display_infos;
68 }
69
70 } // namespace
71
72 ScreenWin::ScreenWin() {
73 Initialize();
74 }
75
76 ScreenWin::~ScreenWin() = default;
77
78 HWND ScreenWin::GetHWNDFromNativeView(gfx::NativeView window) const {
79 NOTREACHED();
80 return nullptr;
81 }
82
83 gfx::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 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 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 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(gfx::DisplayObserver* observer) {
146 change_notifier_.AddObserver(observer);
147 }
148
149 void ScreenWin::RemoveObserver(gfx::DisplayObserver* observer) {
150 change_notifier_.RemoveObserver(observer);
151 }
152
153 void ScreenWin::UpdateFromDisplayInfos(
154 const std::vector<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 ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestHWND(HWND hwnd)
201 const {
202 return GetScreenWinDisplay(MonitorInfoFromWindow(hwnd,
203 MONITOR_DEFAULTTONEAREST));
204 }
205
206 ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestScreenRect(
207 const gfx::Rect& screen_rect) const {
208 return GetScreenWinDisplay(MonitorInfoFromScreenRect(screen_rect));
209 }
210
211 ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestScreenPoint(
212 const gfx::Point& screen_point) const {
213 return GetScreenWinDisplay(MonitorInfoFromScreenPoint(screen_point));
214 }
215
216 ScreenWinDisplay ScreenWin::GetPrimaryScreenWinDisplay() const {
217 MONITORINFOEX monitor_info = MonitorInfoFromWindow(nullptr,
218 MONITOR_DEFAULTTOPRIMARY);
219 ScreenWinDisplay screen_win_display = GetScreenWinDisplay(monitor_info);
220 gfx::Display display = screen_win_display.display();
221 // The Windows primary monitor is defined to have an origin of (0, 0).
222 DCHECK_EQ(0, display.bounds().origin().x());
223 DCHECK_EQ(0, display.bounds().origin().y());
224 return screen_win_display;
225 }
226
227 ScreenWinDisplay ScreenWin::GetScreenWinDisplay(
228 const MONITORINFOEX& monitor_info) const {
229 int64_t id = DisplayInfo::DeviceIdFromDeviceName(monitor_info.szDevice);
230 for (const auto& screen_win_display : screen_win_displays_) {
231 if (screen_win_display.display().id() == id)
232 return screen_win_display;
233 }
234 // There is 1:1 correspondence between MONITORINFOEX and ScreenWinDisplay.
235 // If we make it here, it means we have no displays and we should hand out the
236 // default display.
237 DCHECK_EQ(screen_win_displays_.size(), 0u);
238 return ScreenWinDisplay();
239 }
240
241 } // namespace win
242 } // namespace display
OLDNEW
« no previous file with comments | « ui/display/win/screen_win.h ('k') | ui/display/win/screen_win_display.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698