2 * Copyright (C) 2010 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
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.
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.
27 #include "FindController.h"
29 #include "ShareableBitmap.h"
31 #include "WebCoreArgumentCoders.h"
33 #include "WebPageProxyMessages.h"
34 #include "WebProcess.h"
35 #include <WebCore/DocumentMarkerController.h>
36 #include <WebCore/FloatQuad.h>
37 #include <WebCore/FocusController.h>
38 #include <WebCore/Frame.h>
39 #include <WebCore/FrameView.h>
40 #include <WebCore/GraphicsContext.h>
41 #include <WebCore/Page.h>
44 using namespace WebCore;
48 static WebCore::FindOptions core(FindOptions options)
50 return (options & FindOptionsCaseInsensitive ? CaseInsensitive : 0)
51 | (options & FindOptionsAtWordStarts ? AtWordStarts : 0)
52 | (options & FindOptionsTreatMedialCapitalAsWordStart ? TreatMedialCapitalAsWordStart : 0)
53 | (options & FindOptionsBackwards ? Backwards : 0)
54 | (options & FindOptionsWrapAround ? WrapAround : 0);
57 FindController::FindController(WebPage* webPage)
59 , m_findPageOverlay(0)
60 , m_isShowingFindIndicator(false)
61 #if ENABLE(TIZEN_FIND_STRING)
62 , m_activeMatchIndexInCurrentFrame(0)
67 FindController::~FindController()
71 void FindController::countStringMatches(const String& string, FindOptions options, unsigned maxMatchCount)
73 if (maxMatchCount == numeric_limits<unsigned>::max())
76 unsigned matchCount = m_webPage->corePage()->markAllMatchesForText(string, core(options), false, maxMatchCount + 1);
77 m_webPage->corePage()->unmarkAllTextMatches();
79 // Check if we have more matches than allowed.
80 if (matchCount > maxMatchCount)
81 matchCount = static_cast<unsigned>(kWKMoreThanMaximumMatchCount);
83 m_webPage->send(Messages::WebPageProxy::DidCountStringMatches(string, matchCount));
86 static Frame* frameWithSelection(Page* page)
88 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
89 if (frame->selection()->isRange())
96 void FindController::updateFindUIAfterPageScroll(bool found, const String& string, FindOptions options, unsigned maxMatchCount)
98 Frame* selectedFrame = frameWithSelection(m_webPage->corePage());
100 bool shouldShowOverlay = false;
103 m_webPage->corePage()->unmarkAllTextMatches();
105 // Clear the selection.
107 selectedFrame->selection()->clear();
111 m_webPage->send(Messages::WebPageProxy::DidFailToFindString(string));
113 shouldShowOverlay = options & FindOptionsShowOverlay;
114 bool shouldShowHighlight = options & FindOptionsShowHighlight;
115 unsigned matchCount = 1;
117 if (shouldShowOverlay || shouldShowHighlight) {
119 if (maxMatchCount == numeric_limits<unsigned>::max())
122 m_webPage->corePage()->unmarkAllTextMatches();
123 matchCount = m_webPage->corePage()->markAllMatchesForText(string, core(options), shouldShowHighlight, maxMatchCount + 1);
125 // Check if we have more matches than allowed.
126 if (matchCount > maxMatchCount) {
127 shouldShowOverlay = false;
128 matchCount = static_cast<unsigned>(kWKMoreThanMaximumMatchCount);
132 m_webPage->send(Messages::WebPageProxy::DidFindString(string, matchCount));
134 if (!(options & FindOptionsShowFindIndicator) || !updateFindIndicator(selectedFrame, shouldShowOverlay)) {
135 // Either we shouldn't show the find indicator, or we couldn't update it.
140 if (!shouldShowOverlay) {
141 if (m_findPageOverlay) {
142 // Get rid of the overlay.
143 m_webPage->uninstallPageOverlay(m_findPageOverlay, false);
146 ASSERT(!m_findPageOverlay);
148 if (!m_findPageOverlay) {
149 RefPtr<PageOverlay> findPageOverlay = PageOverlay::create(this);
150 m_findPageOverlay = findPageOverlay.get();
151 m_webPage->installPageOverlay(findPageOverlay.release());
153 // The page overlay needs to be repainted.
154 m_findPageOverlay->setNeedsDisplay();
159 #if ENABLE(TIZEN_FIND_STRING)
160 void FindController::setActiveMatchHighlight(int activeMatchIndex)
162 if (activeMatchIndex >= static_cast<int>(m_findMatches.size()))
163 m_activeMatchIndexInCurrentFrame = 0;
164 else if (activeMatchIndex < 0)
165 m_activeMatchIndexInCurrentFrame = m_findMatches.size() - 1;
167 m_activeMatchIndexInCurrentFrame = activeMatchIndex;
169 Frame* frame = m_findMatches[m_activeMatchIndexInCurrentFrame]->startContainer()->document()->frame();
173 m_activeMatch = m_findMatches[m_activeMatchIndexInCurrentFrame];
175 // Scroll to the position of the active matching text.
176 m_activeMatch->firstNode()->renderer()->scrollRectToVisible(m_activeMatch->boundingBox(),
177 ScrollAlignment::alignCenterIfNeeded, ScrollAlignment::alignCenterIfNeeded);
179 frame->document()->markers()->setMarkersActive(m_activeMatch.get(), true);
183 void FindController::findString(const String& string, FindOptions options, unsigned maxMatchCount)
185 #if ENABLE(TIZEN_FIND_STRING)
186 m_findMatches.clear();
187 int indexForSelection;
189 m_webPage->corePage()->findStringMatchingRanges(string, core(options), maxMatchCount, &m_findMatches, indexForSelection);
191 bool found = !m_findMatches.isEmpty();
193 // highlight the text matches found.
194 m_webPage->drawingArea()->dispatchAfterEnsuringUpdatedScrollPosition(WTF::bind(&FindController::updateFindUIAfterPageScroll, this, found, string, options, maxMatchCount));
197 m_activeMatchIndexInCurrentFrame = -1;
201 if (!m_oldString || m_oldString != string) {
202 m_oldString = string;
203 if (options & FindOptionsBackwards)
204 setActiveMatchHighlight(m_findMatches.size() - 1); // Should we use m_lastMatchCount instead of m_findMatches.size()
206 setActiveMatchHighlight(0);
208 if (options & FindOptionsBackwards)
209 setActiveMatchHighlight(m_activeMatchIndexInCurrentFrame - 1);
211 setActiveMatchHighlight(m_activeMatchIndexInCurrentFrame + 1);
214 bool found = m_webPage->corePage()->findString(string, core(options));
216 m_webPage->drawingArea()->dispatchAfterEnsuringUpdatedScrollPosition(WTF::bind(&FindController::updateFindUIAfterPageScroll, this, found, string, options, maxMatchCount));
220 void FindController::findStringMatches(const String& string, FindOptions options, unsigned maxMatchCount)
222 m_findMatches.clear();
223 int indexForSelection;
225 m_webPage->corePage()->findStringMatchingRanges(string, core(options), maxMatchCount, &m_findMatches, indexForSelection);
227 Vector<Vector<IntRect> > matchRects;
228 for (size_t i = 0; i < m_findMatches.size(); ++i) {
229 Vector<IntRect> rects;
230 m_findMatches[i]->textRects(rects);
231 matchRects.append(rects);
234 m_webPage->send(Messages::WebPageProxy::DidFindStringMatches(string, matchRects, indexForSelection));
237 bool FindController::getFindIndicatorBitmapAndRect(Frame* frame, ShareableBitmap::Handle& handle, IntRect& selectionRect)
239 selectionRect = enclosingIntRect(frame->selection()->bounds());
241 // Selection rect can be empty for matches that are currently obscured from view.
242 if (selectionRect.isEmpty())
245 IntSize backingStoreSize = selectionRect.size();
246 backingStoreSize.scale(m_webPage->corePage()->deviceScaleFactor());
248 // Create a backing store and paint the find indicator text into it.
249 RefPtr<ShareableBitmap> findIndicatorTextBackingStore = ShareableBitmap::createShareable(backingStoreSize, ShareableBitmap::SupportsAlpha);
250 if (!findIndicatorTextBackingStore)
253 OwnPtr<GraphicsContext> graphicsContext = findIndicatorTextBackingStore->createGraphicsContext();
254 graphicsContext->scale(FloatSize(m_webPage->corePage()->deviceScaleFactor(), m_webPage->corePage()->deviceScaleFactor()));
256 IntRect paintRect = selectionRect;
257 paintRect.move(frame->view()->frameRect().x(), frame->view()->frameRect().y());
258 paintRect.move(-frame->view()->scrollOffset());
260 graphicsContext->translate(-paintRect.x(), -paintRect.y());
261 frame->view()->setPaintBehavior(PaintBehaviorSelectionOnly | PaintBehaviorForceBlackText | PaintBehaviorFlattenCompositingLayers);
262 frame->document()->updateLayout();
264 frame->view()->paint(graphicsContext.get(), paintRect);
265 frame->view()->setPaintBehavior(PaintBehaviorNormal);
267 if (!findIndicatorTextBackingStore->createHandle(handle))
272 void FindController::getImageForFindMatch(uint32_t matchIndex)
274 if (matchIndex >= m_findMatches.size())
276 Frame* frame = m_findMatches[matchIndex]->startContainer()->document()->frame();
280 VisibleSelection oldSelection = frame->selection()->selection();
281 frame->selection()->setSelection(VisibleSelection(m_findMatches[matchIndex].get()));
283 IntRect selectionRect;
284 ShareableBitmap::Handle handle;
285 getFindIndicatorBitmapAndRect(frame, handle, selectionRect);
287 frame->selection()->setSelection(oldSelection);
289 m_webPage->send(Messages::WebPageProxy::DidGetImageForFindMatch(handle, matchIndex));
292 void FindController::selectFindMatch(uint32_t matchIndex)
294 if (matchIndex >= m_findMatches.size())
296 Frame* frame = m_findMatches[matchIndex]->startContainer()->document()->frame();
299 frame->selection()->setSelection(VisibleSelection(m_findMatches[matchIndex].get()));
302 void FindController::hideFindUI()
304 m_findMatches.clear();
305 if (m_findPageOverlay)
306 m_webPage->uninstallPageOverlay(m_findPageOverlay, false);
308 m_webPage->corePage()->unmarkAllTextMatches();
312 bool FindController::updateFindIndicator(Frame* selectedFrame, bool isShowingOverlay, bool shouldAnimate)
317 IntRect selectionRect;
318 ShareableBitmap::Handle handle;
319 if (!getFindIndicatorBitmapAndRect(selectedFrame, handle, selectionRect))
322 // We want the selection rect in window coordinates.
323 IntRect selectionRectInWindowCoordinates = selectedFrame->view()->contentsToWindow(selectionRect);
325 Vector<FloatRect> textRects;
326 selectedFrame->selection()->getClippedVisibleTextRectangles(textRects);
328 // We want the text rects in selection rect coordinates.
329 Vector<FloatRect> textRectsInSelectionRectCoordinates;
331 for (size_t i = 0; i < textRects.size(); ++i) {
332 IntRect textRectInSelectionRectCoordinates = selectedFrame->view()->contentsToWindow(enclosingIntRect(textRects[i]));
333 textRectInSelectionRectCoordinates.move(-selectionRectInWindowCoordinates.x(), -selectionRectInWindowCoordinates.y());
335 textRectsInSelectionRectCoordinates.append(textRectInSelectionRectCoordinates);
338 m_webPage->send(Messages::WebPageProxy::SetFindIndicator(selectionRectInWindowCoordinates, textRectsInSelectionRectCoordinates, m_webPage->corePage()->deviceScaleFactor(), handle, !isShowingOverlay, shouldAnimate));
339 m_findIndicatorRect = selectionRectInWindowCoordinates;
340 m_isShowingFindIndicator = true;
345 void FindController::hideFindIndicator()
347 if (!m_isShowingFindIndicator)
350 ShareableBitmap::Handle handle;
351 m_webPage->send(Messages::WebPageProxy::SetFindIndicator(FloatRect(), Vector<FloatRect>(), m_webPage->corePage()->deviceScaleFactor(), handle, false, true));
352 m_isShowingFindIndicator = false;
355 void FindController::showFindIndicatorInSelection()
357 Frame* selectedFrame = m_webPage->corePage()->focusController()->focusedOrMainFrame();
361 updateFindIndicator(selectedFrame, false);
364 void FindController::deviceScaleFactorDidChange()
366 ASSERT(isShowingOverlay());
368 Frame* selectedFrame = frameWithSelection(m_webPage->corePage());
372 updateFindIndicator(selectedFrame, true, false);
375 Vector<IntRect> FindController::rectsForTextMatches()
377 Vector<IntRect> rects;
379 for (Frame* frame = m_webPage->corePage()->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
380 Document* document = frame->document();
384 IntRect visibleRect = frame->view()->visibleContentRect();
385 Vector<IntRect> frameRects = document->markers()->renderedRectsForMarkers(DocumentMarker::TextMatch);
387 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC)
388 IntPoint frameOffset(-frame->view()->scrollOffset().width(), -frame->view()->scrollOffset().height());
389 frameOffset = frame->view()->convertToContainingWindow(frameOffset);
391 IntPoint frameOffset;
392 if (!frame->view()->delegatesScrolling()) {
393 frameOffset = IntPoint(-frame->view()->scrollOffset().width(), -frame->view()->scrollOffset().height());
394 frameOffset = frame->view()->convertToContainingWindow(frameOffset);
398 for (Vector<IntRect>::iterator it = frameRects.begin(), end = frameRects.end(); it != end; ++it) {
399 it->intersect(visibleRect);
400 it->move(frameOffset.x(), frameOffset.y());
408 void FindController::pageOverlayDestroyed(PageOverlay*)
412 void FindController::willMoveToWebPage(PageOverlay*, WebPage* webPage)
417 // The page overlay is moving away from the web page, reset it.
418 ASSERT(m_findPageOverlay);
419 m_findPageOverlay = 0;
422 void FindController::didMoveToWebPage(PageOverlay*, WebPage*)
426 static const float shadowOffsetX = 0.0;
427 static const float shadowOffsetY = 1.0;
428 static const float shadowBlurRadius = 2.0;
429 static const float whiteFrameThickness = 1.0;
431 static const float overlayBackgroundRed = 0.1;
432 static const float overlayBackgroundGreen = 0.1;
433 static const float overlayBackgroundBlue = 0.1;
434 static const float overlayBackgroundAlpha = 0.25;
436 static Color overlayBackgroundColor(float fractionFadedIn)
438 return Color(overlayBackgroundRed, overlayBackgroundGreen, overlayBackgroundBlue, overlayBackgroundAlpha * fractionFadedIn);
441 static Color holeShadowColor(float fractionFadedIn)
443 return Color(0.0f, 0.0f, 0.0f, fractionFadedIn);
446 static Color holeFillColor(float fractionFadedIn)
448 return Color(1.0f, 1.0f, 1.0f, fractionFadedIn);
451 void FindController::drawRect(PageOverlay* pageOverlay, GraphicsContext& graphicsContext, const IntRect& dirtyRect)
453 float fractionFadedIn = pageOverlay->fractionFadedIn();
455 Vector<IntRect> rects = rectsForTextMatches();
457 // Draw the background.
458 graphicsContext.fillRect(dirtyRect, overlayBackgroundColor(fractionFadedIn), ColorSpaceSRGB);
461 GraphicsContextStateSaver stateSaver(graphicsContext);
463 graphicsContext.setShadow(FloatSize(shadowOffsetX, shadowOffsetY), shadowBlurRadius, holeShadowColor(fractionFadedIn), ColorSpaceSRGB);
464 graphicsContext.setFillColor(holeFillColor(fractionFadedIn), ColorSpaceSRGB);
466 // Draw white frames around the holes.
467 for (size_t i = 0; i < rects.size(); ++i) {
468 IntRect whiteFrameRect = rects[i];
469 whiteFrameRect.inflate(1);
471 graphicsContext.fillRect(whiteFrameRect);
475 graphicsContext.setFillColor(Color::transparent, ColorSpaceSRGB);
477 // Clear out the holes.
478 for (size_t i = 0; i < rects.size(); ++i)
479 graphicsContext.fillRect(rects[i]);
481 if (!m_isShowingFindIndicator)
484 if (Frame* selectedFrame = frameWithSelection(m_webPage->corePage())) {
485 IntRect findIndicatorRect = selectedFrame->view()->contentsToWindow(enclosingIntRect(selectedFrame->selection()->bounds()));
487 if (findIndicatorRect != m_findIndicatorRect)
492 bool FindController::mouseEvent(PageOverlay* pageOverlay, const WebMouseEvent& mouseEvent)
494 // If we get a mouse down event inside the page overlay we should hide the find UI.
495 if (mouseEvent.type() == WebEvent::MouseDown) {
496 // Dismiss the overlay.
503 } // namespace WebKit