Merge "Fixed paste issue after selecting word in inputbox" into tizen_2.1
[framework/web/webkit-efl.git] / Source / WebKit2 / WebProcess / WebPage / PageOverlay.cpp
1 /*
2  * Copyright (C) 2010 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  * 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "PageOverlay.h"
28
29 #include "WebPage.h"
30 #include "WebProcess.h"
31 #include <WebCore/Frame.h>
32 #include <WebCore/FrameView.h>
33 #include <WebCore/GraphicsContext.h>
34 #include <WebCore/Page.h>
35 #include <WebCore/ScrollbarTheme.h>
36
37 using namespace WebCore;
38
39 namespace WebKit {
40
41 static const double fadeAnimationDuration = 0.2;
42 static const double fadeAnimationFrameRate = 30;
43
44 PassRefPtr<PageOverlay> PageOverlay::create(Client* client)
45 {
46     return adoptRef(new PageOverlay(client));
47 }
48
49 PageOverlay::PageOverlay(Client* client)
50     : m_client(client)
51     , m_webPage(0)
52     , m_fadeAnimationTimer(WebProcess::shared().runLoop(), this, &PageOverlay::fadeAnimationTimerFired)
53     , m_fadeAnimationStartTime(0.0)
54     , m_fadeAnimationDuration(fadeAnimationDuration)
55     , m_fadeAnimationType(NoAnimation)
56     , m_fractionFadedIn(1.0)
57     , m_pageOverlayShouldApplyFadeWhenPainting(true)
58 {
59 }
60
61 PageOverlay::~PageOverlay()
62 {
63 }
64
65 IntRect PageOverlay::bounds() const
66 {
67     FrameView* frameView = m_webPage->corePage()->mainFrame()->view();
68
69     int width = frameView->width();
70     int height = frameView->height();
71
72     if (!ScrollbarTheme::theme()->usesOverlayScrollbars()) {
73         if (frameView->verticalScrollbar())
74             width -= frameView->verticalScrollbar()->width();
75         if (frameView->horizontalScrollbar())
76             height -= frameView->horizontalScrollbar()->height();
77     }    
78     return IntRect(0, 0, width, height);
79 }
80
81 void PageOverlay::setPage(WebPage* webPage)
82 {
83     m_client->willMoveToWebPage(this, webPage);
84     m_webPage = webPage;
85     m_client->didMoveToWebPage(this, webPage);
86
87     if (m_webPage)
88         m_pageOverlayShouldApplyFadeWhenPainting = m_webPage->drawingArea()->pageOverlayShouldApplyFadeWhenPainting();
89
90     m_fadeAnimationTimer.stop();
91 }
92
93 void PageOverlay::setNeedsDisplay(const IntRect& dirtyRect)
94 {
95     if (m_webPage) {
96         if (!m_pageOverlayShouldApplyFadeWhenPainting)
97             m_webPage->drawingArea()->setPageOverlayOpacity(m_fractionFadedIn);
98         m_webPage->drawingArea()->setPageOverlayNeedsDisplay(dirtyRect);
99     }
100 }
101
102 void PageOverlay::setNeedsDisplay()
103 {
104     setNeedsDisplay(bounds());
105 }
106
107 void PageOverlay::drawRect(GraphicsContext& graphicsContext, const IntRect& dirtyRect)
108 {
109     // If the dirty rect is outside the bounds, ignore it.
110     IntRect paintRect = intersection(dirtyRect, bounds());
111     if (paintRect.isEmpty())
112         return;
113
114     GraphicsContextStateSaver stateSaver(graphicsContext);
115     graphicsContext.beginTransparencyLayer(1);
116     graphicsContext.setCompositeOperation(CompositeCopy);
117
118     m_client->drawRect(this, graphicsContext, paintRect);
119
120     graphicsContext.endTransparencyLayer();
121 }
122     
123 bool PageOverlay::mouseEvent(const WebMouseEvent& mouseEvent)
124 {
125     // Ignore events outside the bounds.
126     if (!bounds().contains(mouseEvent.position()))
127         return false;
128
129     return m_client->mouseEvent(this, mouseEvent);
130 }
131
132 void PageOverlay::startFadeInAnimation()
133 {
134     m_fractionFadedIn = 0.0;
135     m_fadeAnimationType = FadeInAnimation;
136
137     startFadeAnimation();
138 }
139
140 void PageOverlay::startFadeOutAnimation()
141 {
142     m_fractionFadedIn = 1.0;
143     m_fadeAnimationType = FadeOutAnimation;
144
145     startFadeAnimation();
146 }
147
148 void PageOverlay::startFadeAnimation()
149 {
150     m_fadeAnimationStartTime = currentTime();
151     
152     // Start the timer
153     m_fadeAnimationTimer.startRepeating(1 / fadeAnimationFrameRate);
154 }
155
156 void PageOverlay::fadeAnimationTimerFired()
157 {
158     float animationProgress = (currentTime() - m_fadeAnimationStartTime) / m_fadeAnimationDuration;
159
160     if (animationProgress >= 1.0)
161         animationProgress = 1.0;
162
163     double sine = sin(piOverTwoFloat * animationProgress);
164     float fadeAnimationValue = sine * sine;
165
166     m_fractionFadedIn = (m_fadeAnimationType == FadeInAnimation) ? fadeAnimationValue : 1 - fadeAnimationValue;
167
168     if (m_pageOverlayShouldApplyFadeWhenPainting)
169         setNeedsDisplay();
170     else
171         m_webPage->drawingArea()->setPageOverlayOpacity(m_fractionFadedIn);
172
173     if (animationProgress == 1.0) {
174         m_fadeAnimationTimer.stop();
175
176         bool wasFadingOut = m_fadeAnimationType == FadeOutAnimation;
177         m_fadeAnimationType = NoAnimation;
178
179         if (wasFadingOut) {
180             // If this was a fade out, go ahead and uninstall the page overlay.
181             m_webPage->uninstallPageOverlay(this, false);
182         }
183     }
184 }
185
186 } // namespace WebKit