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.h" | 5 #include "ui/gfx/screen.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "ui/gfx/display.h" | 10 #include "ui/gfx/display.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 MONITORINFO GetMonitorInfoForMonitor(HMONITOR monitor) { | 14 MONITORINFO GetMonitorInfoForMonitor(HMONITOR monitor) { |
15 MONITORINFO monitor_info = { 0 }; | 15 MONITORINFO monitor_info = { 0 }; |
16 monitor_info.cbSize = sizeof(monitor_info); | 16 monitor_info.cbSize = sizeof(monitor_info); |
17 GetMonitorInfo(monitor, &monitor_info); | 17 GetMonitorInfo(monitor, &monitor_info); |
18 return monitor_info; | 18 return monitor_info; |
19 } | 19 } |
20 | 20 |
21 gfx::Display GetDisplay(MONITORINFO& monitor_info) { | 21 gfx::Display GetDisplay(MONITORINFO& monitor_info) { |
22 // TODO(oshima): Implement ID and Observer. | 22 // TODO(oshima): Implement ID and Observer. |
23 gfx::Display display(0, gfx::Rect(monitor_info.rcMonitor)); | 23 gfx::Display display(0, gfx::Rect(monitor_info.rcMonitor)); |
24 display.set_work_area(gfx::Rect(monitor_info.rcWork)); | 24 display.set_work_area(gfx::Rect(monitor_info.rcWork)); |
25 return display; | 25 return display; |
26 } | 26 } |
27 | 27 |
| 28 class ScreenWin : public gfx::Screen { |
| 29 public: |
| 30 ScreenWin() {} |
| 31 |
| 32 bool IsDIPEnabled() OVERRIDE { |
| 33 return false; |
| 34 } |
| 35 |
| 36 gfx::Point GetCursorScreenPoint() OVERRIDE { |
| 37 POINT pt; |
| 38 GetCursorPos(&pt); |
| 39 return gfx::Point(pt); |
| 40 } |
| 41 |
| 42 gfx::NativeWindow GetWindowAtCursorScreenPoint() OVERRIDE { |
| 43 POINT location; |
| 44 return GetCursorPos(&location) ? WindowFromPoint(location) : NULL; |
| 45 } |
| 46 |
| 47 int GetNumDisplays() OVERRIDE { |
| 48 return GetSystemMetrics(SM_CMONITORS); |
| 49 } |
| 50 |
| 51 gfx::Display GetDisplayNearestWindow(gfx::NativeView window) const OVERRIDE { |
| 52 MONITORINFO monitor_info; |
| 53 monitor_info.cbSize = sizeof(monitor_info); |
| 54 GetMonitorInfo(MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST), |
| 55 &monitor_info); |
| 56 return GetDisplay(monitor_info); |
| 57 } |
| 58 |
| 59 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const OVERRIDE { |
| 60 POINT initial_loc = { point.x(), point.y() }; |
| 61 HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST); |
| 62 MONITORINFO mi = {0}; |
| 63 mi.cbSize = sizeof(mi); |
| 64 if (monitor && GetMonitorInfo(monitor, &mi)) |
| 65 return GetDisplay(mi); |
| 66 return gfx::Display(); |
| 67 } |
| 68 |
| 69 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const OVERRIDE { |
| 70 RECT other_bounds_rect = match_rect.ToRECT(); |
| 71 MONITORINFO monitor_info = GetMonitorInfoForMonitor(MonitorFromRect( |
| 72 &other_bounds_rect, MONITOR_DEFAULTTONEAREST)); |
| 73 return GetDisplay(monitor_info); |
| 74 } |
| 75 |
| 76 gfx::Display GetPrimaryDisplay() const OVERRIDE { |
| 77 MONITORINFO mi = GetMonitorInfoForMonitor( |
| 78 MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY)); |
| 79 gfx::Display display = GetDisplay(mi); |
| 80 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), display.size().width()); |
| 81 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), display.size().height()); |
| 82 return display; |
| 83 } |
| 84 |
| 85 private: |
| 86 DISALLOW_COPY_AND_ASSIGN(ScreenWin); |
| 87 }; |
| 88 |
28 } // namespace | 89 } // namespace |
29 | 90 |
30 namespace gfx { | 91 namespace gfx { |
31 | 92 |
32 // static | 93 Screen* CreateNativeScreen() { |
33 bool Screen::IsDIPEnabled() { | 94 return new ScreenWin; |
34 return false; | |
35 } | |
36 | |
37 // static | |
38 gfx::Point Screen::GetCursorScreenPoint() { | |
39 POINT pt; | |
40 GetCursorPos(&pt); | |
41 return gfx::Point(pt); | |
42 } | |
43 | |
44 // static | |
45 gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() { | |
46 POINT location; | |
47 return GetCursorPos(&location) ? WindowFromPoint(location) : NULL; | |
48 } | |
49 | |
50 // static | |
51 int Screen::GetNumDisplays() { | |
52 return GetSystemMetrics(SM_CMONITORS); | |
53 } | |
54 | |
55 // static | |
56 gfx::Display Screen::GetDisplayNearestWindow(gfx::NativeView window) { | |
57 MONITORINFO monitor_info; | |
58 monitor_info.cbSize = sizeof(monitor_info); | |
59 GetMonitorInfo(MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST), | |
60 &monitor_info); | |
61 return GetDisplay(monitor_info); | |
62 } | |
63 | |
64 // static | |
65 gfx::Display Screen::GetDisplayNearestPoint(const gfx::Point& point) { | |
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 return GetDisplay(mi); | |
72 return gfx::Display(); | |
73 } | |
74 | |
75 // static | |
76 gfx::Display Screen::GetDisplayMatching(const gfx::Rect& match_rect) { | |
77 RECT other_bounds_rect = match_rect.ToRECT(); | |
78 MONITORINFO monitor_info = GetMonitorInfoForMonitor(MonitorFromRect( | |
79 &other_bounds_rect, MONITOR_DEFAULTTONEAREST)); | |
80 return GetDisplay(monitor_info); | |
81 } | |
82 | |
83 // static | |
84 gfx::Display Screen::GetPrimaryDisplay() { | |
85 MONITORINFO mi = GetMonitorInfoForMonitor( | |
86 MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY)); | |
87 gfx::Display display = GetDisplay(mi); | |
88 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), display.size().width()); | |
89 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), display.size().height()); | |
90 return display; | |
91 } | 95 } |
92 | 96 |
93 } // namespace gfx | 97 } // namespace gfx |
OLD | NEW |