Chromium Code Reviews| 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 | 8 |
| 9 #include "base/hash.h" | 9 #include "base/hash.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/win/win_util.h" | 12 #include "base/win/win_util.h" |
| 13 #include "ui/gfx/display.h" | 13 #include "ui/gfx/display.h" |
| 14 #include "ui/gfx/win/dpi.h" | 14 #include "ui/gfx/win/dpi.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 MONITORINFOEX GetMonitorInfoForMonitor(HMONITOR monitor) { | 18 MONITORINFOEX GetMonitorInfoForMonitor(HMONITOR monitor) { |
| 19 MONITORINFOEX monitor_info; | 19 MONITORINFOEX monitor_info; |
| 20 ZeroMemory(&monitor_info, sizeof(MONITORINFOEX)); | 20 ZeroMemory(&monitor_info, sizeof(MONITORINFOEX)); |
| 21 monitor_info.cbSize = sizeof(monitor_info); | 21 monitor_info.cbSize = sizeof(monitor_info); |
| 22 GetMonitorInfo(monitor, &monitor_info); | 22 GetMonitorInfo(monitor, &monitor_info); |
| 23 return monitor_info; | 23 return monitor_info; |
| 24 } | 24 } |
| 25 | 25 |
| 26 gfx::Display GetDisplay(MONITORINFOEX& monitor_info) { | 26 gfx::Display GetDisplay(MONITORINFOEX& monitor_info) { |
| 27 // TODO(oshima): Implement Observer. | |
| 28 int64 id = static_cast<int64>( | 27 int64 id = static_cast<int64>( |
| 29 base::Hash(base::WideToUTF8(monitor_info.szDevice))); | 28 base::Hash(base::WideToUTF8(monitor_info.szDevice))); |
| 30 gfx::Rect bounds = gfx::Rect(monitor_info.rcMonitor); | 29 gfx::Rect bounds = gfx::Rect(monitor_info.rcMonitor); |
| 31 gfx::Display display(id, bounds); | 30 gfx::Display display(id, bounds); |
| 32 display.set_work_area(gfx::Rect(monitor_info.rcWork)); | 31 display.set_work_area(gfx::Rect(monitor_info.rcWork)); |
| 33 display.SetScaleAndBounds(gfx::win::GetDeviceScaleFactor(), bounds); | 32 display.SetScaleAndBounds(gfx::win::GetDeviceScaleFactor(), bounds); |
| 34 | 33 |
| 35 DEVMODE mode; | 34 DEVMODE mode; |
| 36 memset(&mode, 0, sizeof(DEVMODE)); | 35 memset(&mode, 0, sizeof(DEVMODE)); |
| 37 mode.dmSize = sizeof(DEVMODE); | 36 mode.dmSize = sizeof(DEVMODE); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 gfx::Display display = GetDisplay(monitor_info); | 71 gfx::Display display = GetDisplay(monitor_info); |
| 73 all_displays->push_back(display); | 72 all_displays->push_back(display); |
| 74 return TRUE; | 73 return TRUE; |
| 75 } | 74 } |
| 76 | 75 |
| 77 } // namespace | 76 } // namespace |
| 78 | 77 |
| 79 namespace gfx { | 78 namespace gfx { |
| 80 | 79 |
| 81 ScreenWin::ScreenWin() { | 80 ScreenWin::ScreenWin() { |
| 81 displays_ = BuildDisplays(); | |
|
sky
2014/07/22 17:41:18
Move to member initializer list.
mlamouri (slow - plz ping)
2014/07/22 17:52:50
Done.
| |
| 82 SingletonHwnd::GetInstance()->AddObserver(this); | |
| 82 } | 83 } |
| 83 | 84 |
| 84 ScreenWin::~ScreenWin() { | 85 ScreenWin::~ScreenWin() { |
| 86 SingletonHwnd::GetInstance()->RemoveObserver(this); | |
| 85 } | 87 } |
| 86 | 88 |
| 87 bool ScreenWin::IsDIPEnabled() { | 89 bool ScreenWin::IsDIPEnabled() { |
| 88 return IsInHighDPIMode(); | 90 return IsInHighDPIMode(); |
| 89 } | 91 } |
| 90 | 92 |
| 91 gfx::Point ScreenWin::GetCursorScreenPoint() { | 93 gfx::Point ScreenWin::GetCursorScreenPoint() { |
| 92 POINT pt; | 94 POINT pt; |
| 93 GetCursorPos(&pt); | 95 GetCursorPos(&pt); |
| 94 gfx::Point cursor_pos_pixels(pt); | 96 gfx::Point cursor_pos_pixels(pt); |
| 95 return gfx::win::ScreenToDIPPoint(cursor_pos_pixels); | 97 return gfx::win::ScreenToDIPPoint(cursor_pos_pixels); |
| 96 } | 98 } |
| 97 | 99 |
| 98 gfx::NativeWindow ScreenWin::GetWindowUnderCursor() { | 100 gfx::NativeWindow ScreenWin::GetWindowUnderCursor() { |
| 99 POINT cursor_loc; | 101 POINT cursor_loc; |
| 100 HWND hwnd = GetCursorPos(&cursor_loc) ? WindowFromPoint(cursor_loc) : NULL; | 102 HWND hwnd = GetCursorPos(&cursor_loc) ? WindowFromPoint(cursor_loc) : NULL; |
| 101 return GetNativeWindowFromHWND(hwnd); | 103 return GetNativeWindowFromHWND(hwnd); |
| 102 } | 104 } |
| 103 | 105 |
| 104 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) { | 106 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) { |
| 105 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point); | 107 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point); |
| 106 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT())); | 108 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT())); |
| 107 } | 109 } |
| 108 | 110 |
| 109 int ScreenWin::GetNumDisplays() const { | 111 int ScreenWin::GetNumDisplays() const { |
| 110 return GetSystemMetrics(SM_CMONITORS); | 112 return GetSystemMetrics(SM_CMONITORS); |
| 111 } | 113 } |
| 112 | 114 |
| 113 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const { | 115 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const { |
| 114 std::vector<gfx::Display> all_displays; | 116 return displays_; |
| 115 EnumDisplayMonitors(NULL, NULL, EnumMonitorCallback, | |
| 116 reinterpret_cast<LPARAM>(&all_displays)); | |
| 117 return all_displays; | |
| 118 } | 117 } |
| 119 | 118 |
| 120 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { | 119 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { |
| 121 HWND window_hwnd = GetHWNDFromNativeView(window); | 120 HWND window_hwnd = GetHWNDFromNativeView(window); |
| 122 if (!window_hwnd) { | 121 if (!window_hwnd) { |
| 123 // When |window| isn't rooted to a display, we should just return the | 122 // When |window| isn't rooted to a display, we should just return the |
| 124 // default display so we get some correct display information like the | 123 // default display so we get some correct display information like the |
| 125 // scaling factor. | 124 // scaling factor. |
| 126 return GetPrimaryDisplay(); | 125 return GetPrimaryDisplay(); |
| 127 } | 126 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 // TODO(kevers|girard): Test if these checks can be reintroduced for high-DIP | 158 // TODO(kevers|girard): Test if these checks can be reintroduced for high-DIP |
| 160 // once more of the app is DIP-aware. | 159 // once more of the app is DIP-aware. |
| 161 if (!(IsInHighDPIMode() || IsHighDPIEnabled())) { | 160 if (!(IsInHighDPIMode() || IsHighDPIEnabled())) { |
| 162 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), display.size().width()); | 161 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), display.size().width()); |
| 163 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), display.size().height()); | 162 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), display.size().height()); |
| 164 } | 163 } |
| 165 return display; | 164 return display; |
| 166 } | 165 } |
| 167 | 166 |
| 168 void ScreenWin::AddObserver(DisplayObserver* observer) { | 167 void ScreenWin::AddObserver(DisplayObserver* observer) { |
| 169 // TODO(oshima): crbug.com/122863. | 168 change_notifier_.AddObserver(observer); |
| 170 } | 169 } |
| 171 | 170 |
| 172 void ScreenWin::RemoveObserver(DisplayObserver* observer) { | 171 void ScreenWin::RemoveObserver(DisplayObserver* observer) { |
| 173 // TODO(oshima): crbug.com/122863. | 172 change_notifier_.RemoveObserver(observer); |
| 173 } | |
| 174 | |
| 175 void ScreenWin::OnWndProc(HWND hwnd, | |
| 176 UINT message, | |
| 177 WPARAM wparam, | |
| 178 LPARAM lparam) { | |
| 179 if (message != WM_DISPLAYCHANGE) | |
| 180 return; | |
| 181 | |
| 182 std::vector<gfx::Display> old_displays = displays_; | |
| 183 displays_ = BuildDisplays(); | |
| 184 | |
| 185 change_notifier_.NotifyDisplaysChanged(old_displays, displays_); | |
| 174 } | 186 } |
| 175 | 187 |
| 176 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const { | 188 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const { |
| 177 NOTREACHED(); | 189 NOTREACHED(); |
| 178 return NULL; | 190 return NULL; |
| 179 } | 191 } |
| 180 | 192 |
| 181 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { | 193 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { |
| 182 NOTREACHED(); | 194 NOTREACHED(); |
| 183 return NULL; | 195 return NULL; |
| 184 } | 196 } |
| 185 | 197 |
| 198 std::vector<gfx::Display> ScreenWin::BuildDisplays() const { | |
|
sky
2014/07/22 17:41:18
Since this doesn't need anything from ScreenWin mo
mlamouri (slow - plz ping)
2014/07/22 17:52:51
Done.
| |
| 199 std::vector<gfx::Display> all_displays; | |
| 200 EnumDisplayMonitors(NULL, NULL, EnumMonitorCallback, | |
| 201 reinterpret_cast<LPARAM>(&all_displays)); | |
| 202 return all_displays; | |
| 203 } | |
| 204 | |
| 186 } // namespace gfx | 205 } // namespace gfx |
| OLD | NEW |