| OLD | NEW |
| 1 // Copyright (c) 2011 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.h" | 5 #include "ui/gfx/screen.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/logging.h" |
| 10 #include "ui/gfx/monitor.h" |
| 11 |
| 9 namespace { | 12 namespace { |
| 10 | 13 |
| 11 MONITORINFO GetMonitorInfoForMonitor(HMONITOR monitor) { | 14 MONITORINFO GetMonitorInfoForMonitor(HMONITOR monitor) { |
| 12 MONITORINFO monitor_info = { 0 }; | 15 MONITORINFO monitor_info = { 0 }; |
| 13 monitor_info.cbSize = sizeof(monitor_info); | 16 monitor_info.cbSize = sizeof(monitor_info); |
| 14 GetMonitorInfo(monitor, &monitor_info); | 17 GetMonitorInfo(monitor, &monitor_info); |
| 15 return monitor_info; | 18 return monitor_info; |
| 16 } | 19 } |
| 17 | 20 |
| 21 void EmptyMonitor(gfx::Monitor* monitor_out) { |
| 22 monitor_out->set_bounds(gfx::Rect()); |
| 23 monitor_out->set_work_area(gfx::Rect()); |
| 24 } |
| 25 |
| 26 void CopyMonitor(MONITORINFO& monitor_info, gfx::Monitor* monitor_out) { |
| 27 monitor_out->set_bounds(gfx::Rect(monitor_info.rcMonitor)); |
| 28 monitor_out->set_work_area(gfx::Rect(monitor_info.rcWork)); |
| 29 } |
| 30 |
| 18 } // namespace | 31 } // namespace |
| 19 | 32 |
| 20 namespace gfx { | 33 namespace gfx { |
| 21 | 34 |
| 22 // static | 35 // static |
| 23 gfx::Point Screen::GetCursorScreenPoint() { | 36 gfx::Point Screen::GetCursorScreenPoint() { |
| 24 POINT pt; | 37 POINT pt; |
| 25 GetCursorPos(&pt); | 38 GetCursorPos(&pt); |
| 26 return gfx::Point(pt); | 39 return gfx::Point(pt); |
| 27 } | 40 } |
| 28 | 41 |
| 29 // static | 42 // static |
| 30 gfx::Rect Screen::GetMonitorWorkAreaNearestWindow(gfx::NativeWindow window) { | |
| 31 MONITORINFO monitor_info; | |
| 32 monitor_info.cbSize = sizeof(monitor_info); | |
| 33 GetMonitorInfo(MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST), | |
| 34 &monitor_info); | |
| 35 return gfx::Rect(monitor_info.rcWork); | |
| 36 } | |
| 37 | |
| 38 // static | |
| 39 gfx::Rect Screen::GetMonitorAreaNearestWindow(gfx::NativeWindow window) { | |
| 40 MONITORINFO monitor_info; | |
| 41 monitor_info.cbSize = sizeof(monitor_info); | |
| 42 GetMonitorInfo(MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST), | |
| 43 &monitor_info); | |
| 44 return gfx::Rect(monitor_info.rcMonitor); | |
| 45 } | |
| 46 | |
| 47 static gfx::Rect GetMonitorAreaOrWorkAreaNearestPoint(const gfx::Point& point, | |
| 48 bool work_area) { | |
| 49 POINT initial_loc = { point.x(), point.y() }; | |
| 50 HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST); | |
| 51 MONITORINFO mi = {0}; | |
| 52 mi.cbSize = sizeof(mi); | |
| 53 if (monitor && GetMonitorInfo(monitor, &mi)) | |
| 54 return gfx::Rect(work_area ? mi.rcWork : mi.rcMonitor); | |
| 55 return gfx::Rect(); | |
| 56 } | |
| 57 | |
| 58 // static | |
| 59 gfx::Rect Screen::GetMonitorWorkAreaNearestPoint(const gfx::Point& point) { | |
| 60 return GetMonitorAreaOrWorkAreaNearestPoint(point, true); | |
| 61 } | |
| 62 | |
| 63 // static | |
| 64 gfx::Rect Screen::GetMonitorAreaNearestPoint(const gfx::Point& point) { | |
| 65 return GetMonitorAreaOrWorkAreaNearestPoint(point, false); | |
| 66 } | |
| 67 | |
| 68 // static | |
| 69 gfx::Rect Screen::GetPrimaryMonitorWorkArea() { | |
| 70 return gfx::Rect(GetMonitorInfoForMonitor(MonitorFromWindow(NULL, | |
| 71 MONITOR_DEFAULTTOPRIMARY)).rcWork); | |
| 72 } | |
| 73 | |
| 74 // static | |
| 75 gfx::Rect Screen::GetPrimaryMonitorBounds() { | |
| 76 return gfx::Rect(GetMonitorInfoForMonitor(MonitorFromWindow(NULL, | |
| 77 MONITOR_DEFAULTTOPRIMARY)).rcMonitor); | |
| 78 } | |
| 79 | |
| 80 // static | |
| 81 gfx::Rect Screen::GetMonitorWorkAreaMatching(const gfx::Rect& match_rect) { | |
| 82 RECT other_bounds_rect = match_rect.ToRECT(); | |
| 83 MONITORINFO monitor_info = GetMonitorInfoForMonitor(MonitorFromRect( | |
| 84 &other_bounds_rect, MONITOR_DEFAULTTONEAREST)); | |
| 85 return gfx::Rect(monitor_info.rcWork); | |
| 86 } | |
| 87 | |
| 88 // static | |
| 89 gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() { | 43 gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() { |
| 90 POINT location; | 44 POINT location; |
| 91 return GetCursorPos(&location) ? WindowFromPoint(location) : NULL; | 45 return GetCursorPos(&location) ? WindowFromPoint(location) : NULL; |
| 92 } | 46 } |
| 93 | 47 |
| 94 // static | 48 // static |
| 95 gfx::Size Screen::GetPrimaryMonitorSize() { | |
| 96 return gfx::Size(GetSystemMetrics(SM_CXSCREEN), | |
| 97 GetSystemMetrics(SM_CYSCREEN)); | |
| 98 } | |
| 99 | |
| 100 // static | |
| 101 int Screen::GetNumMonitors() { | 49 int Screen::GetNumMonitors() { |
| 102 return GetSystemMetrics(SM_CMONITORS); | 50 return GetSystemMetrics(SM_CMONITORS); |
| 103 } | 51 } |
| 104 | 52 |
| 53 // static |
| 54 void Screen::GetMonitorNearestWindow(gfx::NativeWindow window, |
| 55 gfx::Monitor* monitor_out) { |
| 56 MONITORINFO monitor_info; |
| 57 monitor_info.cbSize = sizeof(monitor_info); |
| 58 GetMonitorInfo(MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST), |
| 59 &monitor_info); |
| 60 CopyMonitor(monitor_info, monitor_out); |
| 61 } |
| 62 |
| 63 // static |
| 64 void Screen::GetMonitorNearestPoint(const gfx::Point& point, |
| 65 gfx::Monitor* monitor_out) { |
| 66 POINT initial_loc = { point.x(), point.y() }; |
| 67 HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST); |
| 68 MONITORINFO mi = {0}; |
| 69 mi.cbSize = sizeof(mi); |
| 70 if (monitor && GetMonitorInfo(monitor, &mi)) |
| 71 CopyMonitor(mi, monitor_out); |
| 72 else |
| 73 EmptyMonitor(monitor_out); |
| 74 } |
| 75 |
| 76 // static |
| 77 void Screen::GetPrimaryMonitor(gfx::Monitor* monitor_out) { |
| 78 MONITORINFO mi = GetMonitorInfoForMonitor( |
| 79 MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY)); |
| 80 CopyMonitor(mi, monitor_out); |
| 81 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), monitor_out->size().width()); |
| 82 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), monitor_out->size().height()); |
| 83 } |
| 84 |
| 85 // static |
| 86 void Screen::GetMonitorMatching(const gfx::Rect& match_rect, |
| 87 gfx::Monitor* monitor_out) { |
| 88 RECT other_bounds_rect = match_rect.ToRECT(); |
| 89 MONITORINFO monitor_info = GetMonitorInfoForMonitor(MonitorFromRect( |
| 90 &other_bounds_rect, MONITOR_DEFAULTTONEAREST)); |
| 91 CopyMonitor(monitor_info, monitor_out); |
| 92 } |
| 93 |
| 105 } // namespace gfx | 94 } // namespace gfx |
| OLD | NEW |