Merge "[CherryPick] Refactoring: Move the content of HTMLInputElement::subtreeHasChan...
[framework/web/webkit-efl.git] / Source / WebCore / page / Screen.cpp
1 /*
2  * Copyright (C) 2007 Apple 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  *
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. 
16  *
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.
27  */
28
29
30 #include "config.h"
31 #include "Screen.h"
32
33 #include "FloatRect.h"
34 #include "Frame.h"
35 #include "FrameView.h"
36 #include "InspectorInstrumentation.h"
37 #include "PlatformScreen.h"
38 #include "Widget.h"
39
40 #if ENABLE(SCREEN_ORIENTATION_SUPPORT)
41 #include "Chrome.h"
42 #include "ChromeClient.h"
43 #include "Settings.h"
44 #endif
45
46 namespace WebCore {
47
48 #if ENABLE(SCREEN_ORIENTATION_SUPPORT)
49 bool Screen::gNaturalOrientationIsPortrait = false;
50 #endif
51
52 Screen::Screen(Frame* frame)
53     : DOMWindowProperty(frame)
54 #if ENABLE(SCREEN_ORIENTATION_SUPPORT)
55     , ActiveDOMObject(frame->document(), this)
56     , m_orientation(OrientationPortraitPrimary)
57 #endif
58 {
59 }
60
61 unsigned Screen::horizontalDPI() const
62 {
63     if (!m_frame)
64         return 0;
65     return static_cast<unsigned>(screenHorizontalDPI(m_frame->view()));
66 }
67
68 unsigned Screen::verticalDPI() const
69 {
70     if (!m_frame)
71         return 0;
72     return static_cast<unsigned>(screenVerticalDPI(m_frame->view()));
73 }
74
75 unsigned Screen::height() const
76 {
77     if (!m_frame)
78         return 0;
79
80     long height = static_cast<long>(screenRect(m_frame->view()).height());
81     InspectorInstrumentation::applyScreenHeightOverride(m_frame, &height);
82     return static_cast<unsigned>(height);
83 }
84
85 unsigned Screen::width() const
86 {
87     if (!m_frame)
88         return 0;
89
90     long width = static_cast<long>(screenRect(m_frame->view()).width());
91     InspectorInstrumentation::applyScreenWidthOverride(m_frame, &width);
92     return static_cast<unsigned>(width);
93 }
94
95 unsigned Screen::colorDepth() const
96 {
97     if (!m_frame)
98         return 0;
99     return static_cast<unsigned>(screenDepth(m_frame->view()));
100 }
101
102 unsigned Screen::pixelDepth() const
103 {
104     if (!m_frame)
105         return 0;
106     return static_cast<unsigned>(screenDepth(m_frame->view()));
107 }
108
109 int Screen::availLeft() const
110 {
111     if (!m_frame)
112         return 0;
113     return static_cast<int>(screenAvailableRect(m_frame->view()).x());
114 }
115
116 int Screen::availTop() const
117 {
118     if (!m_frame)
119         return 0;
120     return static_cast<int>(screenAvailableRect(m_frame->view()).y());
121 }
122
123 unsigned Screen::availHeight() const
124 {
125     if (!m_frame)
126         return 0;
127     return static_cast<unsigned>(screenAvailableRect(m_frame->view()).height());
128 }
129
130 unsigned Screen::availWidth() const
131 {
132     if (!m_frame)
133         return 0;
134     return static_cast<unsigned>(screenAvailableRect(m_frame->view()).width());
135 }
136
137 #if ENABLE(SCREEN_ORIENTATION_SUPPORT)
138 struct ScreenOrientationInfo {
139     const char* name;
140     Screen::Orientation orientation;
141 };
142
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}
150 };
151
152 String Screen::orientation() const
153 {
154     for (size_t i = 0;i < sizeof(orientationMap); ++i) {
155         if (m_orientation == orientationMap[i].orientation)
156             return orientationMap[i].name;
157     }
158     ASSERT_NOT_REACHED();
159     return "";
160 }
161
162 bool Screen::lockOrientation(String orientation)
163 {
164     Orientation willLockOrientation = InvalidOrientation;
165
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;
170             break;
171         }
172     }
173
174     if (willLockOrientation == InvalidOrientation)
175         return false;
176
177     if (!m_frame)
178         return false;
179
180     Page* page = m_frame->page();
181     if (!page)
182         return false;
183
184     if (!page->chrome()->client()->lockOrientation(willLockOrientation))
185         return false;
186
187     return true;
188 }
189
190 void Screen::unlockOrientation()
191 {
192     if (!m_frame)
193         return;
194
195     Page* page = m_frame->page();
196     if (!page)
197         return;
198
199     page->chrome()->client()->unlockOrientation();
200 }
201
202 void Screen::sendOrientationChangeEvent(int rawOrientation)
203 {
204     if (naturalOrientationIsPortrait())
205         rawOrientation += 90;
206
207     switch (rawOrientation) {
208     case 0:
209         m_orientation = OrientationPortraitPrimary;
210         break;
211     case 90:
212         m_orientation = OrientationLandscapePrimary;
213         break;
214     case 180:
215         m_orientation = OrientationPortraitSecondary;
216         break;
217     case 270:
218     case -90:
219         m_orientation = OrientationLandscapeSecondary;
220         break;
221     default:
222         ASSERT_NOT_REACHED();
223         return;
224     }
225
226     RefPtr<Event> event = Event::create(WebCore::eventNames().orientationchangeEvent, false, false);
227     dispatchEvent(event);
228 }
229 #endif
230
231 } // namespace WebCore