| 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 // The delay to handle the display changes notifications. |
| 19 // See comments in ScreenWin::OnDisplayChanged. |
| 20 const int64 kConfigureDelayMs = 500; |
| 21 |
| 18 MONITORINFOEX GetMonitorInfoForMonitor(HMONITOR monitor) { | 22 MONITORINFOEX GetMonitorInfoForMonitor(HMONITOR monitor) { |
| 19 MONITORINFOEX monitor_info; | 23 MONITORINFOEX monitor_info; |
| 20 ZeroMemory(&monitor_info, sizeof(MONITORINFOEX)); | 24 ZeroMemory(&monitor_info, sizeof(MONITORINFOEX)); |
| 21 monitor_info.cbSize = sizeof(monitor_info); | 25 monitor_info.cbSize = sizeof(monitor_info); |
| 22 GetMonitorInfo(monitor, &monitor_info); | 26 GetMonitorInfo(monitor, &monitor_info); |
| 23 return monitor_info; | 27 return monitor_info; |
| 24 } | 28 } |
| 25 | 29 |
| 26 gfx::Display GetDisplay(MONITORINFOEX& monitor_info) { | 30 gfx::Display GetDisplay(MONITORINFOEX& monitor_info) { |
| 27 // TODO(oshima): Implement Observer. | |
| 28 int64 id = static_cast<int64>( | 31 int64 id = static_cast<int64>( |
| 29 base::Hash(base::WideToUTF8(monitor_info.szDevice))); | 32 base::Hash(base::WideToUTF8(monitor_info.szDevice))); |
| 30 gfx::Rect bounds = gfx::Rect(monitor_info.rcMonitor); | 33 gfx::Rect bounds = gfx::Rect(monitor_info.rcMonitor); |
| 31 gfx::Display display(id, bounds); | 34 gfx::Display display(id, bounds); |
| 32 display.set_work_area(gfx::Rect(monitor_info.rcWork)); | 35 display.set_work_area(gfx::Rect(monitor_info.rcWork)); |
| 33 display.SetScaleAndBounds(gfx::win::GetDeviceScaleFactor(), bounds); | 36 display.SetScaleAndBounds(gfx::win::GetDeviceScaleFactor(), bounds); |
| 34 | 37 |
| 35 DEVMODE mode; | 38 DEVMODE mode; |
| 36 memset(&mode, 0, sizeof(DEVMODE)); | 39 memset(&mode, 0, sizeof(DEVMODE)); |
| 37 mode.dmSize = sizeof(DEVMODE); | 40 mode.dmSize = sizeof(DEVMODE); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 gfx::Display display = GetDisplay(monitor_info); | 75 gfx::Display display = GetDisplay(monitor_info); |
| 73 all_displays->push_back(display); | 76 all_displays->push_back(display); |
| 74 return TRUE; | 77 return TRUE; |
| 75 } | 78 } |
| 76 | 79 |
| 77 } // namespace | 80 } // namespace |
| 78 | 81 |
| 79 namespace gfx { | 82 namespace gfx { |
| 80 | 83 |
| 81 ScreenWin::ScreenWin() { | 84 ScreenWin::ScreenWin() { |
| 85 displays_ = BuildDisplays(); |
| 82 } | 86 } |
| 83 | 87 |
| 84 ScreenWin::~ScreenWin() { | 88 ScreenWin::~ScreenWin() { |
| 85 } | 89 } |
| 86 | 90 |
| 87 bool ScreenWin::IsDIPEnabled() { | 91 bool ScreenWin::IsDIPEnabled() { |
| 88 return IsInHighDPIMode(); | 92 return IsInHighDPIMode(); |
| 89 } | 93 } |
| 90 | 94 |
| 91 gfx::Point ScreenWin::GetCursorScreenPoint() { | 95 gfx::Point ScreenWin::GetCursorScreenPoint() { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 104 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) { | 108 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) { |
| 105 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point); | 109 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point); |
| 106 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT())); | 110 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT())); |
| 107 } | 111 } |
| 108 | 112 |
| 109 int ScreenWin::GetNumDisplays() const { | 113 int ScreenWin::GetNumDisplays() const { |
| 110 return GetSystemMetrics(SM_CMONITORS); | 114 return GetSystemMetrics(SM_CMONITORS); |
| 111 } | 115 } |
| 112 | 116 |
| 113 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const { | 117 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const { |
| 114 std::vector<gfx::Display> all_displays; | 118 return displays_; |
| 115 EnumDisplayMonitors(NULL, NULL, EnumMonitorCallback, | |
| 116 reinterpret_cast<LPARAM>(&all_displays)); | |
| 117 return all_displays; | |
| 118 } | 119 } |
| 119 | 120 |
| 120 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { | 121 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { |
| 121 HWND window_hwnd = GetHWNDFromNativeView(window); | 122 HWND window_hwnd = GetHWNDFromNativeView(window); |
| 122 if (!window_hwnd) { | 123 if (!window_hwnd) { |
| 123 // When |window| isn't rooted to a display, we should just return the | 124 // 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 | 125 // default display so we get some correct display information like the |
| 125 // scaling factor. | 126 // scaling factor. |
| 126 return GetPrimaryDisplay(); | 127 return GetPrimaryDisplay(); |
| 127 } | 128 } |
| (...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 | 160 // TODO(kevers|girard): Test if these checks can be reintroduced for high-DIP |
| 160 // once more of the app is DIP-aware. | 161 // once more of the app is DIP-aware. |
| 161 if (!(IsInHighDPIMode() || IsHighDPIEnabled())) { | 162 if (!(IsInHighDPIMode() || IsHighDPIEnabled())) { |
| 162 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), display.size().width()); | 163 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), display.size().width()); |
| 163 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), display.size().height()); | 164 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), display.size().height()); |
| 164 } | 165 } |
| 165 return display; | 166 return display; |
| 166 } | 167 } |
| 167 | 168 |
| 168 void ScreenWin::AddObserver(DisplayObserver* observer) { | 169 void ScreenWin::AddObserver(DisplayObserver* observer) { |
| 169 // TODO(oshima): crbug.com/122863. | 170 change_notifier_.AddObserver(observer); |
| 170 } | 171 } |
| 171 | 172 |
| 172 void ScreenWin::RemoveObserver(DisplayObserver* observer) { | 173 void ScreenWin::RemoveObserver(DisplayObserver* observer) { |
| 173 // TODO(oshima): crbug.com/122863. | 174 change_notifier_.RemoveObserver(observer); |
| 174 } | 175 } |
| 175 | 176 |
| 176 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const { | 177 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const { |
| 177 NOTREACHED(); | 178 NOTREACHED(); |
| 178 return NULL; | 179 return NULL; |
| 179 } | 180 } |
| 180 | 181 |
| 181 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { | 182 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { |
| 182 NOTREACHED(); | 183 NOTREACHED(); |
| 183 return NULL; | 184 return NULL; |
| 184 } | 185 } |
| 185 | 186 |
| 187 std::vector<gfx::Display> ScreenWin::BuildDisplays() const { |
| 188 std::vector<gfx::Display> all_displays; |
| 189 EnumDisplayMonitors(NULL, NULL, EnumMonitorCallback, |
| 190 reinterpret_cast<LPARAM>(&all_displays)); |
| 191 return all_displays; |
| 192 } |
| 193 |
| 194 void ScreenWin::OnDisplayChanged() { |
| 195 // Windows send a WM_DISPLAYCHANGE message to all windows when the displays |
| 196 // change. This method will be called when this happen. In order to prevent |
| 197 // doing expensive work multiple time, we use a timer that will be reset |
| 198 // every time we get a new call and will be fulfilled kConfigureDelayMs after |
| 199 // the latest. |
| 200 if (configure_timer_.get() && configure_timer_->IsRunning()) { |
| 201 configure_timer_->Reset(); |
| 202 return; |
| 203 } |
| 204 |
| 205 configure_timer_.reset(new base::OneShotTimer<ScreenWin>()); |
| 206 configure_timer_->Start( |
| 207 FROM_HERE, |
| 208 base::TimeDelta::FromMilliseconds(kConfigureDelayMs), |
| 209 this, |
| 210 &ScreenWin::ConfigureTimerFired); |
| 211 } |
| 212 |
| 213 void ScreenWin::ConfigureTimerFired() { |
| 214 std::vector<gfx::Display> old_displays = displays_; |
| 215 displays_ = BuildDisplays(); |
| 216 |
| 217 change_notifier_.NotifyDisplaysChanged(old_displays, displays_); |
| 218 } |
| 219 |
| 186 } // namespace gfx | 220 } // namespace gfx |
| OLD | NEW |