2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "FloatRect.h"
35 #include "FrameView.h"
36 #include "InspectorInstrumentation.h"
37 #include "PlatformScreen.h"
40 #if ENABLE(SCREEN_ORIENTATION_SUPPORT)
42 #include "ChromeClient.h"
48 #if ENABLE(SCREEN_ORIENTATION_SUPPORT)
49 bool Screen::gNaturalOrientationIsPortrait = false;
52 Screen::Screen(Frame* frame)
53 : DOMWindowProperty(frame)
54 #if ENABLE(SCREEN_ORIENTATION_SUPPORT)
55 , ActiveDOMObject(frame->document(), this)
56 , m_orientation(OrientationPortraitPrimary)
61 unsigned Screen::horizontalDPI() const
65 return static_cast<unsigned>(screenHorizontalDPI(m_frame->view()));
68 unsigned Screen::verticalDPI() const
72 return static_cast<unsigned>(screenVerticalDPI(m_frame->view()));
75 unsigned Screen::height() const
80 long height = static_cast<long>(screenRect(m_frame->view()).height());
81 InspectorInstrumentation::applyScreenHeightOverride(m_frame, &height);
82 return static_cast<unsigned>(height);
85 unsigned Screen::width() const
90 long width = static_cast<long>(screenRect(m_frame->view()).width());
91 InspectorInstrumentation::applyScreenWidthOverride(m_frame, &width);
92 return static_cast<unsigned>(width);
95 unsigned Screen::colorDepth() const
99 return static_cast<unsigned>(screenDepth(m_frame->view()));
102 unsigned Screen::pixelDepth() const
106 return static_cast<unsigned>(screenDepth(m_frame->view()));
109 int Screen::availLeft() const
113 return static_cast<int>(screenAvailableRect(m_frame->view()).x());
116 int Screen::availTop() const
120 return static_cast<int>(screenAvailableRect(m_frame->view()).y());
123 unsigned Screen::availHeight() const
127 return static_cast<unsigned>(screenAvailableRect(m_frame->view()).height());
130 unsigned Screen::availWidth() const
134 return static_cast<unsigned>(screenAvailableRect(m_frame->view()).width());
137 #if ENABLE(SCREEN_ORIENTATION_SUPPORT)
138 struct ScreenOrientationInfo {
140 Screen::Orientation orientation;
143 static ScreenOrientationInfo orientationMap[] = {
144 {"portrait-primary", Screen::OrientationPortraitPrimary},
145 {"landscape-primary", Screen::OrientationLandscapePrimary},
146 {"portrait-secondary", Screen::OrientationPortraitSecondary},
147 {"landscape-secondary", Screen::OrientationLandscapeSecondary},
148 {"portrait", Screen::OrientationPortrait},
149 {"landscape", Screen::OrientationLandscape}
152 String Screen::orientation() const
154 for (size_t i = 0;i < sizeof(orientationMap); ++i) {
155 if (m_orientation == orientationMap[i].orientation)
156 return orientationMap[i].name;
158 ASSERT_NOT_REACHED();
162 bool Screen::lockOrientation(String orientation)
164 Orientation willLockOrientation = InvalidOrientation;
166 String lowerOrientation = orientation.lower();
167 for (size_t i = 0;i < sizeof(orientationMap); ++i) {
168 if (lowerOrientation == orientationMap[i].name) {
169 willLockOrientation = orientationMap[i].orientation;
174 if (willLockOrientation == InvalidOrientation)
180 Page* page = m_frame->page();
184 if (!page->chrome()->client()->lockOrientation(willLockOrientation))
190 void Screen::unlockOrientation()
195 Page* page = m_frame->page();
199 page->chrome()->client()->unlockOrientation();
202 void Screen::sendOrientationChangeEvent(int rawOrientation)
204 if (naturalOrientationIsPortrait())
205 rawOrientation += 90;
207 switch (rawOrientation) {
209 m_orientation = OrientationPortraitPrimary;
212 m_orientation = OrientationLandscapePrimary;
215 m_orientation = OrientationPortraitSecondary;
219 m_orientation = OrientationLandscapeSecondary;
222 ASSERT_NOT_REACHED();
226 RefPtr<Event> event = Event::create(WebCore::eventNames().orientationchangeEvent, false, false);
227 dispatchEvent(event);
231 } // namespace WebCore