1/*
2 * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#import "config.h"
27#import "PlatformScreen.h"
28
29#import "FloatRect.h"
30#import "Frame.h"
31#import "FrameView.h"
32#import "Page.h"
33
34namespace WebCore {
35
36int screenDepth(Widget*)
37{
38    return NSBitsPerPixelFromDepth([[NSScreen deepestScreen] depth]);
39}
40
41int screenDepthPerComponent(Widget*)
42{
43    return NSBitsPerSampleFromDepth([[NSScreen deepestScreen] depth]);
44}
45
46bool screenIsMonochrome(Widget*)
47{
48    return false;
49}
50
51// These functions scale between screen and page coordinates because JavaScript/DOM operations
52// assume that the screen and the page share the same coordinate system.
53
54FloatRect screenRect(Widget* widget)
55{
56    NSWindow *window = widget ? [widget->platformWidget() window] : nil;
57    return toUserSpace([screenForWindow(window) frame], window);
58}
59
60FloatRect screenAvailableRect(Widget* widget)
61{
62    NSWindow *window = widget ? [widget->platformWidget() window] : nil;
63    return toUserSpace([screenForWindow(window) visibleFrame], window);
64}
65
66NSScreen *screenForWindow(NSWindow *window)
67{
68    NSScreen *screen = [window screen]; // nil if the window is off-screen
69    if (screen)
70        return screen;
71
72    NSArray *screens = [NSScreen screens];
73    if ([screens count] > 0)
74        return [screens objectAtIndex:0]; // screen containing the menubar
75
76    return nil;
77}
78
79static CGFloat windowScaleFactor(NSWindow *window)
80{
81#if !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
82    return [window backingScaleFactor];
83#else
84    return [window userSpaceScaleFactor];
85#endif
86}
87
88FloatRect toUserSpace(const NSRect& rect, NSWindow *destination)
89{
90    FloatRect userRect = rect;
91    userRect.setY(NSMaxY([screenForWindow(destination) frame]) - (userRect.y() + userRect.height())); // flip
92    if (destination)
93        userRect.scale(1 / windowScaleFactor(destination)); // scale down
94    return userRect;
95}
96
97NSRect toDeviceSpace(const FloatRect& rect, NSWindow *source)
98{
99    FloatRect deviceRect = rect;
100    if (source)
101        deviceRect.scale(windowScaleFactor(source)); // scale up
102    deviceRect.setY(NSMaxY([screenForWindow(source) frame]) - (deviceRect.y() + deviceRect.height())); // flip
103    return deviceRect;
104}
105
106NSPoint flipScreenPoint(const NSPoint& screenPoint, NSScreen *screen)
107{
108    NSPoint flippedPoint = screenPoint;
109    flippedPoint.y = NSMaxY([screen frame]) - flippedPoint.y;
110    return flippedPoint;
111}
112
113} // namespace WebCore
114