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* monitor = NULL; | |
Ben Goodger (Google)
2012/04/10 16:37:20
g_monitor
oshima
2012/04/10 21:31:46
Done.
| |
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() { | |
24 if (!monitor) { | |
25 monitor = new gfx::SimpleMonitor(); | |
Ben Goodger (Google)
2012/04/10 16:37:20
nit: no braces
oshima
2012/04/10 21:31:46
Done.
| |
26 } | |
27 monitor->set_bounds(gfx::Rect()); | |
Ben Goodger (Google)
2012/04/10 16:37:20
since you're using a global, if anyone stores a co
oshima
2012/04/10 21:31:46
This is temporary until we have MonitorObserver im
| |
28 monitor->set_work_area(gfx::Rect()); | |
29 return monitor; | |
30 } | |
31 | |
32 gfx::Monitor* GetMonitor(MONITORINFO& monitor_info) { | |
33 if (!monitor) { | |
34 monitor = new gfx::SimpleMonitor(); | |
Ben Goodger (Google)
2012/04/10 16:37:20
nit: no braces
oshima
2012/04/10 21:31:46
Done.
| |
35 } | |
36 monitor->set_bounds(gfx::Rect(monitor_info.rcMonitor)); | |
37 monitor->set_work_area(gfx::Rect(monitor_info.rcWork)); | |
38 return monitor; | |
39 } | |
40 | |
18 } // namespace | 41 } // namespace |
19 | 42 |
20 namespace gfx { | 43 namespace gfx { |
21 | 44 |
22 // static | 45 // static |
23 gfx::Point Screen::GetCursorScreenPoint() { | 46 gfx::Point Screen::GetCursorScreenPoint() { |
24 POINT pt; | 47 POINT pt; |
25 GetCursorPos(&pt); | 48 GetCursorPos(&pt); |
26 return gfx::Point(pt); | 49 return gfx::Point(pt); |
27 } | 50 } |
28 | 51 |
29 // static | 52 // 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() { | 53 gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() { |
90 POINT location; | 54 POINT location; |
91 return GetCursorPos(&location) ? WindowFromPoint(location) : NULL; | 55 return GetCursorPos(&location) ? WindowFromPoint(location) : NULL; |
92 } | 56 } |
93 | 57 |
94 // static | 58 // 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() { | 59 int Screen::GetNumMonitors() { |
102 return GetSystemMetrics(SM_CMONITORS); | 60 return GetSystemMetrics(SM_CMONITORS); |
103 } | 61 } |
104 | 62 |
63 // static | |
64 const gfx::Monitor* Screen::GetMonitorNearestWindow(gfx::NativeWindow window) { | |
65 MONITORINFO monitor_info; | |
66 monitor_info.cbSize = sizeof(monitor_info); | |
67 GetMonitorInfo(MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST), | |
68 &monitor_info); | |
69 return GetMonitor(monitor_info); | |
70 } | |
71 | |
72 // static | |
73 const gfx::Monitor* Screen::GetMonitorNearestPoint(const gfx::Point& point) { | |
74 POINT initial_loc = { point.x(), point.y() }; | |
75 HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST); | |
76 MONITORINFO mi = {0}; | |
77 mi.cbSize = sizeof(mi); | |
78 if (monitor && GetMonitorInfo(monitor, &mi)) | |
79 return GetMonitor(mi); | |
80 return GetEmptyMonitor(); | |
81 } | |
82 | |
83 // static | |
84 const gfx::Monitor* Screen::GetPrimaryMonitor() { | |
85 MONITORINFO mi = GetMonitorInfoForMonitor( | |
86 MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY)); | |
87 gfx::Monitor* monitor = GetMonitor(mi); | |
88 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), monitor->GetSize().width()); | |
89 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), monitor->GetSize().height()); | |
90 return monitor; | |
91 } | |
92 | |
93 // static | |
94 const gfx::Monitor* Screen::GetMonitorMatching(const gfx::Rect& match_rect) { | |
95 RECT other_bounds_rect = match_rect.ToRECT(); | |
96 MONITORINFO monitor_info = GetMonitorInfoForMonitor(MonitorFromRect( | |
97 &other_bounds_rect, MONITOR_DEFAULTTONEAREST)); | |
98 return GetMonitor(monitor_info); | |
99 } | |
100 | |
105 } // namespace gfx | 101 } // namespace gfx |
OLD | NEW |