screen_win.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 3// found in the LICENSE file. 4 5#include "ui/gfx/screen_win.h" 6 7#include <windows.h> 8 9#include "base/logging.h" 10#include "ui/gfx/display.h" 11 12namespace { 13 14MONITORINFO GetMonitorInfoForMonitor(HMONITOR monitor) { 15 MONITORINFO monitor_info = { 0 }; 16 monitor_info.cbSize = sizeof(monitor_info); 17 GetMonitorInfo(monitor, &monitor_info); 18 return monitor_info; 19} 20 21gfx::Display GetDisplay(MONITORINFO& monitor_info) { 22 // TODO(oshima): Implement ID and Observer. 23 gfx::Display display(0, gfx::Rect(monitor_info.rcMonitor)); 24 display.set_work_area(gfx::Rect(monitor_info.rcWork)); 25 return display; 26} 27 28} // namespace 29 30namespace gfx { 31 32ScreenWin::ScreenWin() { 33} 34 35ScreenWin::~ScreenWin() { 36} 37 38bool ScreenWin::IsDIPEnabled() { 39 return false; 40} 41 42gfx::Point ScreenWin::GetCursorScreenPoint() { 43 POINT pt; 44 GetCursorPos(&pt); 45 return gfx::Point(pt); 46} 47 48gfx::NativeWindow ScreenWin::GetWindowAtCursorScreenPoint() { 49 POINT location; 50 HWND window_hwnd = GetCursorPos(&location) ? WindowFromPoint(location) : NULL; 51 return GetNativeWindowFromHWND(window_hwnd); 52} 53 54int ScreenWin::GetNumDisplays() { 55 return GetSystemMetrics(SM_CMONITORS); 56} 57 58gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { 59 HWND window_hwnd = GetHWNDFromNativeView(window); 60 MONITORINFO monitor_info; 61 monitor_info.cbSize = sizeof(monitor_info); 62 GetMonitorInfo(MonitorFromWindow(window_hwnd, MONITOR_DEFAULTTONEAREST), 63 &monitor_info); 64 return GetDisplay(monitor_info); 65} 66 67gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const { 68 POINT initial_loc = { point.x(), point.y() }; 69 HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST); 70 MONITORINFO mi = {0}; 71 mi.cbSize = sizeof(mi); 72 if (monitor && GetMonitorInfo(monitor, &mi)) 73 return GetDisplay(mi); 74 return gfx::Display(); 75} 76 77gfx::Display ScreenWin::GetDisplayMatching(const gfx::Rect& match_rect) const { 78 RECT other_bounds_rect = match_rect.ToRECT(); 79 MONITORINFO monitor_info = GetMonitorInfoForMonitor(MonitorFromRect( 80 &other_bounds_rect, MONITOR_DEFAULTTONEAREST)); 81 return GetDisplay(monitor_info); 82} 83 84gfx::Display ScreenWin::GetPrimaryDisplay() const { 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} 92 93HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const { 94#if defined(USE_AURA) 95 NOTREACHED(); 96 return NULL; 97#else 98 return window; 99#endif // USE_AURA 100} 101 102NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { 103#if defined(USE_AURA) 104 NOTREACHED(); 105 return NULL; 106#else 107 return hwnd; 108#endif // USE_AURA 109} 110 111#if !defined(USE_AURA) 112Screen* CreateNativeScreen() { 113 return new ScreenWin; 114} 115#endif // !USE_AURA 116 117} // namespace gfx 118