| 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 #import <ApplicationServices/ApplicationServices.h> | 7 #import <ApplicationServices/ApplicationServices.h> |
| 8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
| 9 | 9 |
| 10 #include "base/logging.h" |
| 11 #include "ui/gfx/monitor.h" |
| 12 |
| 10 namespace { | 13 namespace { |
| 11 | 14 |
| 12 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) { | 15 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) { |
| 13 // Primary monitor is defined as the monitor with the menubar, | 16 // Primary monitor is defined as the monitor with the menubar, |
| 14 // which is always at index 0. | 17 // which is always at index 0. |
| 15 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0]; | 18 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0]; |
| 16 float primary_screen_height = [primary_screen frame].size.height; | 19 float primary_screen_height = [primary_screen frame].size.height; |
| 17 gfx::Rect rect(NSRectToCGRect(ns_rect)); | 20 gfx::Rect rect(NSRectToCGRect(ns_rect)); |
| 18 rect.set_y(primary_screen_height - rect.y() - rect.height()); | 21 rect.set_y(primary_screen_height - rect.y() - rect.height()); |
| 19 return rect; | 22 return rect; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 31 int area = intersection.width() * intersection.height(); | 34 int area = intersection.width() * intersection.height(); |
| 32 if (area > max_area) { | 35 if (area > max_area) { |
| 33 max_area = area; | 36 max_area = area; |
| 34 max_screen = screen; | 37 max_screen = screen; |
| 35 } | 38 } |
| 36 } | 39 } |
| 37 | 40 |
| 38 return max_screen; | 41 return max_screen; |
| 39 } | 42 } |
| 40 | 43 |
| 44 gfx::Monitor GetMonitorForScreen(NSScreen* screen, bool is_primary) { |
| 45 NSRect frame = [screen frame]; |
| 46 // TODO(oshima): Implement ID and Observer. |
| 47 gfx::Monitor monitor(0, gfx::Rect(NSRectToCGRect(frame))); |
| 48 |
| 49 NSRect visible_frame = [screen visibleFrame]; |
| 50 |
| 51 // Convert work area's coordinate systems. |
| 52 if (is_primary) { |
| 53 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame)); |
| 54 work_area.set_y(frame.size.height - visible_frame.origin.y - |
| 55 visible_frame.size.height); |
| 56 monitor.set_work_area(work_area); |
| 57 } else { |
| 58 monitor.set_work_area(ConvertCoordinateSystem(visible_frame)); |
| 59 } |
| 60 return monitor; |
| 61 } |
| 62 |
| 41 } // namespace | 63 } // namespace |
| 42 | 64 |
| 43 namespace gfx { | 65 namespace gfx { |
| 44 | 66 |
| 45 // static | 67 // static |
| 46 gfx::Point Screen::GetCursorScreenPoint() { | 68 gfx::Point Screen::GetCursorScreenPoint() { |
| 47 NSPoint mouseLocation = [NSEvent mouseLocation]; | 69 NSPoint mouseLocation = [NSEvent mouseLocation]; |
| 48 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. | 70 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. |
| 49 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | 71 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| 50 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; | 72 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; |
| 51 return gfx::Point(mouseLocation.x, mouseLocation.y); | 73 return gfx::Point(mouseLocation.x, mouseLocation.y); |
| 52 } | 74 } |
| 53 | 75 |
| 54 // static | 76 // static |
| 55 gfx::Rect Screen::GetPrimaryMonitorWorkArea() { | 77 gfx::Monitor Screen::GetPrimaryMonitor() { |
| 56 // Primary monitor is defined as the monitor with the menubar, | 78 // Primary monitor is defined as the monitor with the menubar, |
| 57 // which is always at index 0. | 79 // which is always at index 0. |
| 58 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; | 80 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; |
| 59 NSRect frame = [primary frame]; | 81 gfx::Monitor monitor = GetMonitorForScreen(primary, true /* primary */); |
| 60 NSRect visible_frame = [primary visibleFrame]; | |
| 61 | 82 |
| 62 // Convert coordinate systems. | 83 CGDirectDisplayID main_display = CGMainDisplayID(); |
| 63 gfx::Rect rect = gfx::Rect(NSRectToCGRect(visible_frame)); | 84 CHECK_EQ(static_cast<const int>(CGDisplayPixelsWide(main_display)), |
| 64 rect.set_y(frame.size.height - visible_frame.origin.y - | 85 monitor.size().width()); |
| 65 visible_frame.size.height); | 86 CHECK_EQ(static_cast<const int>(CGDisplayPixelsHigh(main_display)), |
| 66 return rect; | 87 monitor.size().height()); |
| 88 return monitor; |
| 67 } | 89 } |
| 68 | 90 |
| 69 // static | 91 // static |
| 70 gfx::Rect Screen::GetPrimaryMonitorBounds() { | 92 gfx::Monitor Screen::GetMonitorMatching(const gfx::Rect& match_rect) { |
| 71 // Primary monitor is defined as the monitor with the menubar, | 93 NSScreen* match_screen = GetMatchingScreen(match_rect); |
| 72 // which is always at index 0. | 94 return GetMonitorForScreen(match_screen, false /* may not be primary */); |
| 73 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; | |
| 74 return gfx::Rect(NSRectToCGRect([primary frame])); | |
| 75 } | 95 } |
| 76 | 96 |
| 77 // static | 97 // static |
| 78 gfx::Rect Screen::GetMonitorWorkAreaMatching(const gfx::Rect& match_rect) { | |
| 79 NSScreen* match_screen = GetMatchingScreen(match_rect); | |
| 80 return ConvertCoordinateSystem([match_screen visibleFrame]); | |
| 81 } | |
| 82 | |
| 83 // static | |
| 84 gfx::Size Screen::GetPrimaryMonitorSize() { | |
| 85 CGDirectDisplayID main_display = CGMainDisplayID(); | |
| 86 return gfx::Size(CGDisplayPixelsWide(main_display), | |
| 87 CGDisplayPixelsHigh(main_display)); | |
| 88 } | |
| 89 | |
| 90 // static | |
| 91 int Screen::GetNumMonitors() { | 98 int Screen::GetNumMonitors() { |
| 92 // Don't just return the number of online displays. It includes displays | 99 // Don't just return the number of online displays. It includes displays |
| 93 // that mirror other displays, which are not desired in the count. It's | 100 // that mirror other displays, which are not desired in the count. It's |
| 94 // tempting to use the count returned by CGGetActiveDisplayList, but active | 101 // tempting to use the count returned by CGGetActiveDisplayList, but active |
| 95 // displays exclude sleeping displays, and those are desired in the count. | 102 // displays exclude sleeping displays, and those are desired in the count. |
| 96 | 103 |
| 97 // It would be ridiculous to have this many displays connected, but | 104 // It would be ridiculous to have this many displays connected, but |
| 98 // CGDirectDisplayID is just an integer, so supporting up to this many | 105 // CGDirectDisplayID is just an integer, so supporting up to this many |
| 99 // doesn't hurt. | 106 // doesn't hurt. |
| 100 CGDirectDisplayID online_displays[128]; | 107 CGDirectDisplayID online_displays[128]; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 116 // The primary display in a mirrored set will be counted, but those that | 123 // The primary display in a mirrored set will be counted, but those that |
| 117 // mirror it will not be. | 124 // mirror it will not be. |
| 118 ++display_count; | 125 ++display_count; |
| 119 } | 126 } |
| 120 } | 127 } |
| 121 | 128 |
| 122 return display_count; | 129 return display_count; |
| 123 } | 130 } |
| 124 | 131 |
| 125 } // namespace gfx | 132 } // namespace gfx |
| OLD | NEW |