- add sources.
[platform/framework/web/crosswalk.git] / src / ui / gfx / screen_mac.mm
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.h"
6
7 #import <ApplicationServices/ApplicationServices.h>
8 #import <Cocoa/Cocoa.h>
9
10 #include "base/logging.h"
11 #include "base/mac/sdk_forward_declarations.h"
12 #include "ui/gfx/display.h"
13
14 namespace {
15
16 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) {
17   // Primary monitor is defined as the monitor with the menubar,
18   // which is always at index 0.
19   NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0];
20   float primary_screen_height = [primary_screen frame].size.height;
21   gfx::Rect rect(NSRectToCGRect(ns_rect));
22   rect.set_y(primary_screen_height - rect.y() - rect.height());
23   return rect;
24 }
25
26 NSScreen* GetMatchingScreen(const gfx::Rect& match_rect) {
27   // Default to the monitor with the current keyboard focus, in case
28   // |match_rect| is not on any screen at all.
29   NSScreen* max_screen = [NSScreen mainScreen];
30   int max_area = 0;
31
32   for (NSScreen* screen in [NSScreen screens]) {
33     gfx::Rect monitor_area = ConvertCoordinateSystem([screen frame]);
34     gfx::Rect intersection = gfx::IntersectRects(monitor_area, match_rect);
35     int area = intersection.width() * intersection.height();
36     if (area > max_area) {
37       max_area = area;
38       max_screen = screen;
39     }
40   }
41
42   return max_screen;
43 }
44
45 gfx::Display GetDisplayForScreen(NSScreen* screen) {
46   NSRect frame = [screen frame];
47   // TODO(oshima): Implement ID and Observer.
48   gfx::Display display(0, gfx::Rect(NSRectToCGRect(frame)));
49
50   NSRect visible_frame = [screen visibleFrame];
51   NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
52
53   // Convert work area's coordinate systems.
54   if ([screen isEqual:primary]) {
55     gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame));
56     work_area.set_y(frame.size.height - visible_frame.origin.y -
57                     visible_frame.size.height);
58     display.set_work_area(work_area);
59   } else {
60     display.set_bounds(ConvertCoordinateSystem(frame));
61     display.set_work_area(ConvertCoordinateSystem(visible_frame));
62   }
63   CGFloat scale;
64   if ([screen respondsToSelector:@selector(backingScaleFactor)])
65     scale = [screen backingScaleFactor];
66   else
67     scale = [screen userSpaceScaleFactor];
68   display.set_device_scale_factor(scale);
69   return display;
70 }
71
72 class ScreenMac : public gfx::Screen {
73  public:
74   ScreenMac() {}
75
76   virtual bool IsDIPEnabled() OVERRIDE {
77     return true;
78   }
79
80   virtual gfx::Point GetCursorScreenPoint() OVERRIDE {
81     NSPoint mouseLocation  = [NSEvent mouseLocation];
82     // Flip coordinates to gfx (0,0 in top-left corner) using primary screen.
83     NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
84     mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y;
85     return gfx::Point(mouseLocation.x, mouseLocation.y);
86   }
87
88   virtual gfx::NativeWindow GetWindowUnderCursor() OVERRIDE {
89     NOTIMPLEMENTED();
90     return gfx::NativeWindow();
91   }
92
93   virtual gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point)
94       OVERRIDE {
95     NOTIMPLEMENTED();
96     return gfx::NativeWindow();
97   }
98
99   virtual int GetNumDisplays() const OVERRIDE {
100     // Don't just return the number of online displays.  It includes displays
101     // that mirror other displays, which are not desired in the count.  It's
102     // tempting to use the count returned by CGGetActiveDisplayList, but active
103     // displays exclude sleeping displays, and those are desired in the count.
104
105     // It would be ridiculous to have this many displays connected, but
106     // CGDirectDisplayID is just an integer, so supporting up to this many
107     // doesn't hurt.
108     CGDirectDisplayID online_displays[128];
109     CGDisplayCount online_display_count = 0;
110     if (CGGetOnlineDisplayList(arraysize(online_displays),
111                               online_displays,
112                               &online_display_count) != kCGErrorSuccess) {
113       // 1 is a reasonable assumption.
114       return 1;
115     }
116
117     int display_count = 0;
118     for (CGDisplayCount online_display_index = 0;
119         online_display_index < online_display_count;
120         ++online_display_index) {
121       CGDirectDisplayID online_display = online_displays[online_display_index];
122       if (CGDisplayMirrorsDisplay(online_display) == kCGNullDirectDisplay) {
123         // If this display doesn't mirror any other, include it in the count.
124         // The primary display in a mirrored set will be counted, but those that
125         // mirror it will not be.
126         ++display_count;
127       }
128     }
129
130     return display_count;
131   }
132
133   virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
134     NOTIMPLEMENTED();
135     return std::vector<gfx::Display>(1, GetPrimaryDisplay());
136   }
137
138   virtual gfx::Display GetDisplayNearestWindow(
139       gfx::NativeView view) const OVERRIDE {
140     NSWindow* window = [view window];
141     if (!window)
142       return GetPrimaryDisplay();
143     NSScreen* match_screen = [window screen];
144     if (!match_screen)
145       return GetPrimaryDisplay();
146     return GetDisplayForScreen(match_screen);
147   }
148
149   virtual gfx::Display GetDisplayNearestPoint(
150       const gfx::Point& point) const OVERRIDE {
151     NSPoint ns_point = NSPointFromCGPoint(point.ToCGPoint());
152
153     NSArray* screens = [NSScreen screens];
154     NSScreen* primary = [screens objectAtIndex:0];
155     ns_point.y = NSMaxY([primary frame]) - ns_point.y;
156     for (NSScreen* screen in screens) {
157       if (NSMouseInRect(ns_point, [screen frame], NO))
158         return GetDisplayForScreen(screen);
159     }
160     return GetPrimaryDisplay();
161   }
162
163   // Returns the display that most closely intersects the provided bounds.
164   virtual gfx::Display GetDisplayMatching(
165       const gfx::Rect& match_rect) const OVERRIDE {
166     NSScreen* match_screen = GetMatchingScreen(match_rect);
167     return GetDisplayForScreen(match_screen);
168   }
169
170   // Returns the primary display.
171   virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
172     // Primary display is defined as the display with the menubar,
173     // which is always at index 0.
174     NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
175     gfx::Display display = GetDisplayForScreen(primary);
176     return display;
177   }
178
179   virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE {
180     // TODO(oshima): crbug.com/122863.
181   }
182
183   virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE {
184     // TODO(oshima): crbug.com/122863.
185   }
186
187  private:
188   DISALLOW_COPY_AND_ASSIGN(ScreenMac);
189 };
190
191 }  // namespace
192
193 namespace gfx {
194
195 Screen* CreateNativeScreen() {
196   return new ScreenMac;
197 }
198
199 }