summaryrefslogtreecommitdiff
path: root/chromium/ui/gfx/screen_win.cc
blob: 06c14bd173283af2cfa8add19013848cc6a3e2c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/screen_win.h"

#include <windows.h>

#include "base/logging.h"
#include "ui/base/win/dpi.h"
#include "ui/gfx/display.h"

namespace {

MONITORINFO GetMonitorInfoForMonitor(HMONITOR monitor) {
  MONITORINFO monitor_info = { 0 };
  monitor_info.cbSize = sizeof(monitor_info);
  GetMonitorInfo(monitor, &monitor_info);
  return monitor_info;
}

gfx::Display GetDisplay(MONITORINFO& monitor_info) {
  // TODO(oshima): Implement ID and Observer.
  gfx::Rect bounds = gfx::Rect(monitor_info.rcMonitor);
  gfx::Display display(0, bounds);
  display.set_work_area(gfx::Rect(monitor_info.rcWork));
  display.SetScaleAndBounds(ui::win::GetDeviceScaleFactor(), bounds);
  return display;
}

}  // namespace

namespace gfx {

ScreenWin::ScreenWin() {
}

ScreenWin::~ScreenWin() {
}

bool ScreenWin::IsDIPEnabled() {
  return ui::IsInHighDPIMode();
}

gfx::Point ScreenWin::GetCursorScreenPoint() {
  POINT pt;
  GetCursorPos(&pt);
  return gfx::Point(pt);
}

gfx::NativeWindow ScreenWin::GetWindowAtCursorScreenPoint() {
  POINT location;
  HWND window_hwnd = GetCursorPos(&location) ? WindowFromPoint(location) : NULL;
  return GetNativeWindowFromHWND(window_hwnd);
}

int ScreenWin::GetNumDisplays() {
  return GetSystemMetrics(SM_CMONITORS);
}

gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const {
  HWND window_hwnd = GetHWNDFromNativeView(window);
  if (!window_hwnd) {
    // When |window| isn't rooted to a display, we should just return the
    // default display so we get some correct display information like the
    // scaling factor.
    return GetPrimaryDisplay();
  }

  MONITORINFO monitor_info;
  monitor_info.cbSize = sizeof(monitor_info);
  GetMonitorInfo(MonitorFromWindow(window_hwnd, MONITOR_DEFAULTTONEAREST),
                 &monitor_info);
  return GetDisplay(monitor_info);
}

gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const {
  POINT initial_loc = { point.x(), point.y() };
  HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST);
  MONITORINFO mi = {0};
  mi.cbSize = sizeof(mi);
  if (monitor && GetMonitorInfo(monitor, &mi))
    return GetDisplay(mi);
  return gfx::Display();
}

gfx::Display ScreenWin::GetDisplayMatching(const gfx::Rect& match_rect) const {
  RECT other_bounds_rect = match_rect.ToRECT();
  MONITORINFO monitor_info = GetMonitorInfoForMonitor(MonitorFromRect(
      &other_bounds_rect, MONITOR_DEFAULTTONEAREST));
  return GetDisplay(monitor_info);
}

gfx::Display ScreenWin::GetPrimaryDisplay() const {
  MONITORINFO mi = GetMonitorInfoForMonitor(
      MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY));
  gfx::Display display = GetDisplay(mi);
  // TODO(kevers|girard): Test if these checks can be reintroduced for high-DIP
  // once more of the app is DIP-aware.
  if (!ui::IsInHighDPIMode()) {
    DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), display.size().width());
    DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), display.size().height());
  }
  return display;
}

void ScreenWin::AddObserver(DisplayObserver* observer) {
  // TODO(oshima): crbug.com/122863.
}

void ScreenWin::RemoveObserver(DisplayObserver* observer) {
  // TODO(oshima): crbug.com/122863.
}

HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const {
#if defined(USE_AURA)
  NOTREACHED();
  return NULL;
#else
  return window;
#endif  // USE_AURA
}

NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const {
#if defined(USE_AURA)
  NOTREACHED();
  return NULL;
#else
  return hwnd;
#endif  // USE_AURA
}

#if !defined(USE_AURA)
Screen* CreateNativeScreen() {
  return new ScreenWin;
}
#endif  // !USE_AURA

}  // namespace gfx