Merge "Use EwkView's variables instead of drawingScaleFactor and drawingScrollPositio...
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / FindIndicator.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 "FindIndicator.h"
28
29 #include "ShareableBitmap.h"
30 #include <WebCore/Gradient.h>
31 #include <WebCore/GraphicsContext.h>
32 #include <WebCore/IntRect.h>
33 #include <WebCore/Path.h>
34
35 using namespace WebCore;
36
37 static const float cornerRadius = 3.0;
38
39 static const float shadowOffsetX = 0.0;
40 static const float shadowOffsetY = 1.0;
41 static const float shadowBlurRadius = 3.0;
42
43 static const int shadowRed = 0;
44 static const int shadowGreen = 0;
45 static const int shadowBlue = 0;
46 static const int shadowAlpha = 204;
47
48 static const float lightBorderThickness = 1.0;
49 static const float horizontalPaddingInsideLightBorder = 3.0;
50 static const float verticalPaddingInsideLightBorder = 1.0;
51
52 static const float horizontalBorderInsideShadow = lightBorderThickness + horizontalPaddingInsideLightBorder;
53 static const float verticalBorderInsideShadow = lightBorderThickness + verticalPaddingInsideLightBorder;
54
55 static const float leftBorderThickness = horizontalBorderInsideShadow + shadowOffsetX + shadowBlurRadius / 2.0;
56 static const float topBorderThickness = verticalBorderInsideShadow - shadowOffsetY + shadowBlurRadius / 2.0;
57 static const float rightBorderThickness = horizontalBorderInsideShadow - shadowOffsetX + shadowBlurRadius / 2.0;
58 static const float bottomBorderThickness = verticalBorderInsideShadow + shadowOffsetY + shadowBlurRadius / 2.0;
59
60 static const float horizontalOutsetToCenterOfLightBorder = horizontalBorderInsideShadow - lightBorderThickness / 2.0;
61 static const float verticalOutsetToCenterOfLightBorder = verticalBorderInsideShadow - lightBorderThickness / 2.0;
62
63 static const int lightBorderRed = 245;
64 static const int lightBorderGreen = 230;
65 static const int lightBorderBlue = 0;
66 static const int lightBorderAlpha = 255;
67
68 static const int gradientDarkRed = 237;
69 static const int gradientDarkGreen = 204;
70 static const int gradientDarkBlue = 0;
71 static const int gradientDarkAlpha = 255;
72
73 static const int gradientLightRed = 242;
74 static const int gradientLightGreen = 239;
75 static const int gradientLightBlue = 0;
76 static const int gradientLightAlpha = 255;
77
78 namespace WebKit {
79
80 PassRefPtr<FindIndicator> FindIndicator::create(const FloatRect& selectionRectInWindowCoordinates, const Vector<FloatRect>& textRectsInSelectionRectCoordinates, float contentImageScaleFactor, const ShareableBitmap::Handle& contentImageHandle)
81 {
82     RefPtr<ShareableBitmap> contentImage = ShareableBitmap::create(contentImageHandle);
83     if (!contentImage)
84         return 0;
85     ASSERT(contentImageScaleFactor != 1 || contentImage->size() == enclosingIntRect(selectionRectInWindowCoordinates).size());
86
87     return adoptRef(new FindIndicator(selectionRectInWindowCoordinates, textRectsInSelectionRectCoordinates, contentImageScaleFactor, contentImage.release()));
88 }
89
90 static bool findIndicatorsForTextRectsOverlap(const Vector<FloatRect>& textRects)
91 {
92     size_t count = textRects.size();
93     if (count <= 1)
94         return false;
95
96     Vector<FloatRect> indicatorRects;
97     indicatorRects.reserveInitialCapacity(count);
98
99     for (size_t i = 0; i < count; ++i) {
100         FloatRect indicatorRect = textRects[i];
101         indicatorRect.move(-leftBorderThickness, -topBorderThickness);
102         indicatorRect.expand(leftBorderThickness + rightBorderThickness, topBorderThickness + bottomBorderThickness);
103
104         for (size_t j = indicatorRects.size(); j; ) {
105             --j;
106             if (indicatorRect.intersects(indicatorRects[j]))
107                 return true;
108         }
109
110         indicatorRects.uncheckedAppend(indicatorRect);
111     }
112
113     return false;
114 }
115
116 FindIndicator::FindIndicator(const WebCore::FloatRect& selectionRectInWindowCoordinates, const Vector<WebCore::FloatRect>& textRectsInSelectionRectCoordinates, float contentImageScaleFactor, PassRefPtr<ShareableBitmap> contentImage)
117     : m_selectionRectInWindowCoordinates(selectionRectInWindowCoordinates)
118     , m_textRectsInSelectionRectCoordinates(textRectsInSelectionRectCoordinates)
119     , m_contentImageScaleFactor(contentImageScaleFactor)
120     , m_contentImage(contentImage)
121 {
122     if (findIndicatorsForTextRectsOverlap(m_textRectsInSelectionRectCoordinates)) {
123         m_textRectsInSelectionRectCoordinates[0] = unionRect(m_textRectsInSelectionRectCoordinates);
124         m_textRectsInSelectionRectCoordinates.shrink(1);
125     }
126 }
127
128 FindIndicator::~FindIndicator()
129 {
130 }
131
132 static FloatRect inflateRect(const FloatRect& rect, float inflateX, float inflateY)
133 {
134     FloatRect inflatedRect = rect;
135     inflatedRect.inflateX(inflateX);
136     inflatedRect.inflateY(inflateY);
137     
138     return inflatedRect;
139 }
140
141 FloatRect FindIndicator::frameRect() const
142 {
143     return FloatRect(m_selectionRectInWindowCoordinates.x() - leftBorderThickness, m_selectionRectInWindowCoordinates.y() - topBorderThickness,
144                      m_selectionRectInWindowCoordinates.width() + rightBorderThickness + leftBorderThickness,
145                      m_selectionRectInWindowCoordinates.height() + topBorderThickness + bottomBorderThickness);
146 }
147
148 static Color lightBorderColor()
149 {
150     return Color(lightBorderRed, lightBorderGreen, lightBorderBlue, lightBorderAlpha);
151 }
152
153 static Color shadowColor()
154 {
155     return Color(shadowRed, shadowGreen, shadowBlue, shadowAlpha);
156 }
157
158 static Color gradientLightColor()
159 {
160     return Color(gradientLightRed, gradientLightGreen, gradientLightBlue, gradientLightAlpha);
161 }
162
163 static Color gradientDarkColor()
164 {
165     return Color(gradientDarkRed, gradientDarkGreen, gradientDarkBlue, gradientDarkAlpha);
166 }
167
168 static Path pathWithRoundedRect(const FloatRect& pathRect, float radius)
169 {
170     Path path;
171     path.addRoundedRect(pathRect, FloatSize(radius, radius));
172
173     return path;
174 }
175     
176 void FindIndicator::draw(GraphicsContext& graphicsContext, const IntRect& dirtyRect)
177 {
178     for (size_t i = 0; i < m_textRectsInSelectionRectCoordinates.size(); ++i) {
179         FloatRect textRect = m_textRectsInSelectionRectCoordinates[i];
180         textRect.move(leftBorderThickness, topBorderThickness);
181
182         FloatRect outerPathRect = inflateRect(textRect, horizontalOutsetToCenterOfLightBorder, verticalOutsetToCenterOfLightBorder);
183         FloatRect innerPathRect = inflateRect(textRect, horizontalPaddingInsideLightBorder, verticalPaddingInsideLightBorder);
184
185         {
186             GraphicsContextStateSaver stateSaver(graphicsContext);
187             graphicsContext.setShadow(FloatSize(shadowOffsetX, shadowOffsetY), shadowBlurRadius, shadowColor(), ColorSpaceSRGB);
188             graphicsContext.setFillColor(lightBorderColor(), ColorSpaceDeviceRGB);
189             graphicsContext.fillPath(pathWithRoundedRect(outerPathRect, cornerRadius));
190         }
191
192         {
193             GraphicsContextStateSaver stateSaver(graphicsContext);
194             graphicsContext.clip(pathWithRoundedRect(innerPathRect, cornerRadius));
195             RefPtr<Gradient> gradient = Gradient::create(FloatPoint(innerPathRect.x(), innerPathRect.y()), FloatPoint(innerPathRect.x(), innerPathRect.maxY()));
196             gradient->addColorStop(0, gradientLightColor());
197             gradient->addColorStop(1, gradientDarkColor());
198             graphicsContext.setFillGradient(gradient);
199             graphicsContext.fillRect(outerPathRect);
200         }
201
202         {
203             GraphicsContextStateSaver stateSaver(graphicsContext);
204             graphicsContext.translate(FloatSize(roundf(leftBorderThickness), roundf(topBorderThickness)));
205
206             IntRect contentImageRect = enclosingIntRect(m_textRectsInSelectionRectCoordinates[i]);
207             m_contentImage->paint(graphicsContext, m_contentImageScaleFactor, contentImageRect.location(), contentImageRect);
208         }
209     }
210 }
211
212 } // namespace WebKit