Chromium Code Reviews| 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 |
| 14 gfx::SimpleMonitor* g_monitor = NULL; | |
| 15 | |
| 11 MONITORINFO GetMonitorInfoForMonitor(HMONITOR monitor) { | 16 MONITORINFO GetMonitorInfoForMonitor(HMONITOR monitor) { |
| 12 MONITORINFO monitor_info = { 0 }; | 17 MONITORINFO monitor_info = { 0 }; |
| 13 monitor_info.cbSize = sizeof(monitor_info); | 18 monitor_info.cbSize = sizeof(monitor_info); |
| 14 GetMonitorInfo(monitor, &monitor_info); | 19 GetMonitorInfo(monitor, &monitor_info); |
| 15 return monitor_info; | 20 return monitor_info; |
| 16 } | 21 } |
| 17 | 22 |
| 23 gfx::Monitor* GetEmptyMonitor() { | |
|
Ben Goodger (Google)
2012/04/11 21:05:58
So even though you say it's temporary, it really t
oshima
2012/04/11 22:05:07
Ok, that's good suggestion. I'll make that change.
| |
| 24 if (!g_monitor) | |
| 25 g_monitor = new gfx::SimpleMonitor(); | |
| 26 g_monitor->set_bounds(gfx::Rect()); | |
| 27 g_monitor->set_work_area(gfx::Rect()); | |
| 28 return g_monitor; | |
| 29 } | |
| 30 | |
| 31 gfx::Monitor* GetMonitor(MONITORINFO& monitor_info) { | |
| 32 if (!g_monitor) | |
| 33 g_monitor = new gfx::SimpleMonitor(); | |
| 34 g_monitor->set_bounds(gfx::Rect(monitor_info.rcMonitor)); | |
| 35 g_monitor->set_work_area(gfx::Rect(monitor_info.rcWork)); | |
| 36 return g_monitor; | |
| 37 } | |
| 38 | |
| 18 } // namespace | 39 } // namespace |
| 19 | 40 |
| 20 namespace gfx { | 41 namespace gfx { |
| 21 | 42 |
| 22 // static | 43 // static |
| 23 gfx::Point Screen::GetCursorScreenPoint() { | 44 gfx::Point Screen::GetCursorScreenPoint() { |
| 24 POINT pt; | 45 POINT pt; |
| 25 GetCursorPos(&pt); | 46 GetCursorPos(&pt); |
| 26 return gfx::Point(pt); | 47 return gfx::Point(pt); |
| 27 } | 48 } |
| 28 | 49 |
| 29 // static | 50 // 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() { | 51 gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() { |
| 90 POINT location; | 52 POINT location; |
| 91 return GetCursorPos(&location) ? WindowFromPoint(location) : NULL; | 53 return GetCursorPos(&location) ? WindowFromPoint(location) : NULL; |
| 92 } | 54 } |
| 93 | 55 |
| 94 // static | 56 // 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() { | 57 int Screen::GetNumMonitors() { |
| 102 return GetSystemMetrics(SM_CMONITORS); | 58 return GetSystemMetrics(SM_CMONITORS); |
| 103 } | 59 } |
| 104 | 60 |
| 61 // static | |
| 62 const gfx::Monitor* Screen::GetMonitorNearestWindow(gfx::NativeWindow window) { | |
| 63 MONITORINFO monitor_info; | |
| 64 monitor_info.cbSize = sizeof(monitor_info); | |
| 65 GetMonitorInfo(MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST), | |
| 66 &monitor_info); | |
| 67 return GetMonitor(monitor_info); | |
| 68 } | |
| 69 | |
| 70 // static | |
| 71 const gfx::Monitor* Screen::GetMonitorNearestPoint(const gfx::Point& point) { | |
| 72 POINT initial_loc = { point.x(), point.y() }; | |
| 73 HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST); | |
| 74 MONITORINFO mi = {0}; | |
| 75 mi.cbSize = sizeof(mi); | |
| 76 if (monitor && GetMonitorInfo(monitor, &mi)) | |
| 77 return GetMonitor(mi); | |
| 78 return GetEmptyMonitor(); | |
| 79 } | |
| 80 | |
| 81 // static | |
| 82 const gfx::Monitor* Screen::GetPrimaryMonitor() { | |
| 83 MONITORINFO mi = GetMonitorInfoForMonitor( | |
| 84 MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY)); | |
| 85 gfx::Monitor* monitor = GetMonitor(mi); | |
| 86 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), monitor->GetSize().width()); | |
| 87 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), monitor->GetSize().height()); | |
| 88 return monitor; | |
| 89 } | |
| 90 | |
| 91 // static | |
| 92 const gfx::Monitor* Screen::GetMonitorMatching(const gfx::Rect& match_rect) { | |
| 93 RECT other_bounds_rect = match_rect.ToRECT(); | |
| 94 MONITORINFO monitor_info = GetMonitorInfoForMonitor(MonitorFromRect( | |
| 95 &other_bounds_rect, MONITOR_DEFAULTTONEAREST)); | |
| 96 return GetMonitor(monitor_info); | |
| 97 } | |
| 98 | |
| 105 } // namespace gfx | 99 } // namespace gfx |
| OLD | NEW |