Merge "Fixed paste issue after selecting word in inputbox" into tizen_2.1
[framework/web/webkit-efl.git] / Source / WebKit2 / WebProcess / WebPage / FindController.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 "FindController.h"
28
29 #include "ShareableBitmap.h"
30 #include "WKPage.h"
31 #include "WebCoreArgumentCoders.h"
32 #include "WebPage.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>
42
43 using namespace std;
44 using namespace WebCore;
45
46 namespace WebKit {
47
48 static WebCore::FindOptions core(FindOptions options)
49 {
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);
55 }
56
57 FindController::FindController(WebPage* webPage)
58     : m_webPage(webPage)
59     , m_findPageOverlay(0)
60     , m_isShowingFindIndicator(false)
61 #if ENABLE(TIZEN_FIND_STRING)
62     , m_activeMatchIndexInCurrentFrame(0)
63 #endif
64 {
65 }
66
67 FindController::~FindController()
68 {
69 }
70
71 void FindController::countStringMatches(const String& string, FindOptions options, unsigned maxMatchCount)
72 {
73     if (maxMatchCount == numeric_limits<unsigned>::max())
74         --maxMatchCount;
75     
76     unsigned matchCount = m_webPage->corePage()->markAllMatchesForText(string, core(options), false, maxMatchCount + 1);
77     m_webPage->corePage()->unmarkAllTextMatches();
78
79     // Check if we have more matches than allowed.
80     if (matchCount > maxMatchCount)
81         matchCount = static_cast<unsigned>(kWKMoreThanMaximumMatchCount);
82     
83     m_webPage->send(Messages::WebPageProxy::DidCountStringMatches(string, matchCount));
84 }
85
86 static Frame* frameWithSelection(Page* page)
87 {
88     for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
89         if (frame->selection()->isRange())
90             return frame;
91     }
92
93     return 0;
94 }
95
96 void FindController::updateFindUIAfterPageScroll(bool found, const String& string, FindOptions options, unsigned maxMatchCount)
97 {
98     Frame* selectedFrame = frameWithSelection(m_webPage->corePage());
99
100     bool shouldShowOverlay = false;
101
102     if (!found) {
103         m_webPage->corePage()->unmarkAllTextMatches();
104
105         // Clear the selection.
106         if (selectedFrame)
107             selectedFrame->selection()->clear();
108
109         hideFindIndicator();
110
111         m_webPage->send(Messages::WebPageProxy::DidFailToFindString(string));
112     } else {
113         shouldShowOverlay = options & FindOptionsShowOverlay;
114         bool shouldShowHighlight = options & FindOptionsShowHighlight;
115         unsigned matchCount = 1;
116
117         if (shouldShowOverlay || shouldShowHighlight) {
118
119             if (maxMatchCount == numeric_limits<unsigned>::max())
120                 --maxMatchCount;
121
122             m_webPage->corePage()->unmarkAllTextMatches();
123             matchCount = m_webPage->corePage()->markAllMatchesForText(string, core(options), shouldShowHighlight, maxMatchCount + 1);
124
125             // Check if we have more matches than allowed.
126             if (matchCount > maxMatchCount) {
127                 shouldShowOverlay = false;
128                 matchCount = static_cast<unsigned>(kWKMoreThanMaximumMatchCount);
129             }
130         }
131
132         m_webPage->send(Messages::WebPageProxy::DidFindString(string, matchCount));
133
134         if (!(options & FindOptionsShowFindIndicator) || !updateFindIndicator(selectedFrame, shouldShowOverlay)) {
135             // Either we shouldn't show the find indicator, or we couldn't update it.
136             hideFindIndicator();
137         }
138     }
139
140     if (!shouldShowOverlay) {
141         if (m_findPageOverlay) {
142             // Get rid of the overlay.
143             m_webPage->uninstallPageOverlay(m_findPageOverlay, false);
144         }
145         
146         ASSERT(!m_findPageOverlay);
147     } else {
148         if (!m_findPageOverlay) {
149             RefPtr<PageOverlay> findPageOverlay = PageOverlay::create(this);
150             m_findPageOverlay = findPageOverlay.get();
151             m_webPage->installPageOverlay(findPageOverlay.release());
152         } else {
153             // The page overlay needs to be repainted.
154             m_findPageOverlay->setNeedsDisplay();
155         }
156     }
157 }
158
159 #if ENABLE(TIZEN_FIND_STRING)
160 void FindController::setActiveMatchHighlight(int activeMatchIndex)
161 {
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;
166     else
167         m_activeMatchIndexInCurrentFrame = activeMatchIndex;
168
169     Frame* frame = m_findMatches[m_activeMatchIndexInCurrentFrame]->startContainer()->document()->frame();
170     if (!frame)
171         return;
172
173     m_activeMatch = m_findMatches[m_activeMatchIndexInCurrentFrame];
174
175     // Scroll to the position of the active matching text.
176     m_activeMatch->firstNode()->renderer()->scrollRectToVisible(m_activeMatch->boundingBox(),
177         ScrollAlignment::alignCenterIfNeeded, ScrollAlignment::alignCenterIfNeeded);
178
179     frame->document()->markers()->addTextMatchMarker(m_activeMatch.get(), true);
180     frame->document()->markers()->setMarkersActive(m_activeMatch.get(), true);
181 }
182 #endif
183
184 void FindController::findString(const String& string, FindOptions options, unsigned maxMatchCount)
185 {
186 #if ENABLE(TIZEN_FIND_STRING)
187     m_findMatches.clear();
188     int indexForSelection;
189
190     m_webPage->corePage()->findStringMatchingRanges(string, core(options), maxMatchCount, &m_findMatches, indexForSelection);
191
192     bool found = !m_findMatches.isEmpty();
193
194     // highlight the text matches found.
195     m_webPage->drawingArea()->dispatchAfterEnsuringUpdatedScrollPosition(WTF::bind(&FindController::updateFindUIAfterPageScroll, this, found, string, options, maxMatchCount));
196
197     if (!found) {
198         m_activeMatchIndexInCurrentFrame = -1;
199         return;
200     }
201
202     if (!m_oldString || m_oldString != string) {
203         m_oldString = string;
204         if (options & FindOptionsBackwards)
205             setActiveMatchHighlight(m_findMatches.size() - 1); // Should we use m_lastMatchCount instead of m_findMatches.size()
206         else
207             setActiveMatchHighlight(0);
208     } else {
209         if (options & FindOptionsBackwards)
210             setActiveMatchHighlight(m_activeMatchIndexInCurrentFrame - 1);
211         else
212             setActiveMatchHighlight(m_activeMatchIndexInCurrentFrame + 1);
213     }
214 #else
215     bool found = m_webPage->corePage()->findString(string, core(options));
216
217     m_webPage->drawingArea()->dispatchAfterEnsuringUpdatedScrollPosition(WTF::bind(&FindController::updateFindUIAfterPageScroll, this, found, string, options, maxMatchCount));
218 #endif
219 }
220
221 void FindController::findStringMatches(const String& string, FindOptions options, unsigned maxMatchCount)
222 {
223     m_findMatches.clear();
224     int indexForSelection;
225
226     m_webPage->corePage()->findStringMatchingRanges(string, core(options), maxMatchCount, &m_findMatches, indexForSelection);
227
228     Vector<Vector<IntRect> > matchRects;
229     for (size_t i = 0; i < m_findMatches.size(); ++i) {
230         Vector<IntRect> rects;
231         m_findMatches[i]->textRects(rects);
232         matchRects.append(rects);
233     }
234
235     m_webPage->send(Messages::WebPageProxy::DidFindStringMatches(string, matchRects, indexForSelection));
236 }
237
238 bool FindController::getFindIndicatorBitmapAndRect(Frame* frame, ShareableBitmap::Handle& handle, IntRect& selectionRect)
239 {
240     selectionRect = enclosingIntRect(frame->selection()->bounds());
241
242     // Selection rect can be empty for matches that are currently obscured from view.
243     if (selectionRect.isEmpty())
244         return false;
245
246     IntSize backingStoreSize = selectionRect.size();
247     backingStoreSize.scale(m_webPage->corePage()->deviceScaleFactor());
248
249     // Create a backing store and paint the find indicator text into it.
250     RefPtr<ShareableBitmap> findIndicatorTextBackingStore = ShareableBitmap::createShareable(backingStoreSize, ShareableBitmap::SupportsAlpha);
251     if (!findIndicatorTextBackingStore)
252         return false;
253
254     OwnPtr<GraphicsContext> graphicsContext = findIndicatorTextBackingStore->createGraphicsContext();
255     graphicsContext->scale(FloatSize(m_webPage->corePage()->deviceScaleFactor(), m_webPage->corePage()->deviceScaleFactor()));
256
257     IntRect paintRect = selectionRect;
258     paintRect.move(frame->view()->frameRect().x(), frame->view()->frameRect().y());
259     paintRect.move(-frame->view()->scrollOffset());
260
261     graphicsContext->translate(-paintRect.x(), -paintRect.y());
262     frame->view()->setPaintBehavior(PaintBehaviorSelectionOnly | PaintBehaviorForceBlackText | PaintBehaviorFlattenCompositingLayers);
263     frame->document()->updateLayout();
264
265     frame->view()->paint(graphicsContext.get(), paintRect);
266     frame->view()->setPaintBehavior(PaintBehaviorNormal);
267
268     if (!findIndicatorTextBackingStore->createHandle(handle))
269         return false;
270     return true;
271 }
272
273 void FindController::getImageForFindMatch(uint32_t matchIndex)
274 {
275     if (matchIndex >= m_findMatches.size())
276         return;
277     Frame* frame = m_findMatches[matchIndex]->startContainer()->document()->frame();
278     if (!frame)
279         return;
280
281     VisibleSelection oldSelection = frame->selection()->selection();
282     frame->selection()->setSelection(VisibleSelection(m_findMatches[matchIndex].get()));
283
284     IntRect selectionRect;
285     ShareableBitmap::Handle handle;
286     getFindIndicatorBitmapAndRect(frame, handle, selectionRect);
287
288     frame->selection()->setSelection(oldSelection);
289
290     m_webPage->send(Messages::WebPageProxy::DidGetImageForFindMatch(handle, matchIndex));
291 }
292
293 void FindController::selectFindMatch(uint32_t matchIndex)
294 {
295     if (matchIndex >= m_findMatches.size())
296         return;
297     Frame* frame = m_findMatches[matchIndex]->startContainer()->document()->frame();
298     if (!frame)
299         return;
300     frame->selection()->setSelection(VisibleSelection(m_findMatches[matchIndex].get()));
301 }
302
303 void FindController::hideFindUI()
304 {
305     m_findMatches.clear();
306     if (m_findPageOverlay)
307         m_webPage->uninstallPageOverlay(m_findPageOverlay, false);
308
309     m_webPage->corePage()->unmarkAllTextMatches();
310     hideFindIndicator();
311 }
312
313 bool FindController::updateFindIndicator(Frame* selectedFrame, bool isShowingOverlay, bool shouldAnimate)
314 {
315     if (!selectedFrame)
316         return false;
317
318     IntRect selectionRect;
319     ShareableBitmap::Handle handle;
320     if (!getFindIndicatorBitmapAndRect(selectedFrame, handle, selectionRect))
321         return false;
322
323     // We want the selection rect in window coordinates.
324     IntRect selectionRectInWindowCoordinates = selectedFrame->view()->contentsToWindow(selectionRect);
325
326     Vector<FloatRect> textRects;
327     selectedFrame->selection()->getClippedVisibleTextRectangles(textRects);
328
329     // We want the text rects in selection rect coordinates.
330     Vector<FloatRect> textRectsInSelectionRectCoordinates;
331     
332     for (size_t i = 0; i < textRects.size(); ++i) {
333         IntRect textRectInSelectionRectCoordinates = selectedFrame->view()->contentsToWindow(enclosingIntRect(textRects[i]));
334         textRectInSelectionRectCoordinates.move(-selectionRectInWindowCoordinates.x(), -selectionRectInWindowCoordinates.y());
335
336         textRectsInSelectionRectCoordinates.append(textRectInSelectionRectCoordinates);
337     }            
338
339     m_webPage->send(Messages::WebPageProxy::SetFindIndicator(selectionRectInWindowCoordinates, textRectsInSelectionRectCoordinates, m_webPage->corePage()->deviceScaleFactor(), handle, !isShowingOverlay, shouldAnimate));
340     m_findIndicatorRect = selectionRectInWindowCoordinates;
341     m_isShowingFindIndicator = true;
342
343     return true;
344 }
345
346 void FindController::hideFindIndicator()
347 {
348     if (!m_isShowingFindIndicator)
349         return;
350
351     ShareableBitmap::Handle handle;
352     m_webPage->send(Messages::WebPageProxy::SetFindIndicator(FloatRect(), Vector<FloatRect>(), m_webPage->corePage()->deviceScaleFactor(), handle, false, true));
353     m_isShowingFindIndicator = false;
354 }
355
356 void FindController::showFindIndicatorInSelection()
357 {
358     Frame* selectedFrame = m_webPage->corePage()->focusController()->focusedOrMainFrame();
359     if (!selectedFrame)
360         return;
361     
362     updateFindIndicator(selectedFrame, false);
363 }
364
365 void FindController::deviceScaleFactorDidChange()
366 {
367     ASSERT(isShowingOverlay());
368
369     Frame* selectedFrame = frameWithSelection(m_webPage->corePage());
370     if (!selectedFrame)
371         return;
372
373     updateFindIndicator(selectedFrame, true, false);
374 }
375
376 Vector<IntRect> FindController::rectsForTextMatches()
377 {
378     Vector<IntRect> rects;
379
380     for (Frame* frame = m_webPage->corePage()->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
381         Document* document = frame->document();
382         if (!document)
383             continue;
384
385         IntRect visibleRect = frame->view()->visibleContentRect();
386         Vector<IntRect> frameRects = document->markers()->renderedRectsForMarkers(DocumentMarker::TextMatch);
387
388 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC)
389         IntPoint frameOffset(-frame->view()->scrollOffset().width(), -frame->view()->scrollOffset().height());
390         frameOffset = frame->view()->convertToContainingWindow(frameOffset);
391 #else
392         IntPoint frameOffset;
393         if (!frame->view()->delegatesScrolling()) {
394             frameOffset = IntPoint(-frame->view()->scrollOffset().width(), -frame->view()->scrollOffset().height());
395             frameOffset = frame->view()->convertToContainingWindow(frameOffset);
396         }
397 #endif
398
399         for (Vector<IntRect>::iterator it = frameRects.begin(), end = frameRects.end(); it != end; ++it) {
400             it->intersect(visibleRect);
401             it->move(frameOffset.x(), frameOffset.y());
402             rects.append(*it);
403         }
404     }
405
406     return rects;
407 }
408
409 void FindController::pageOverlayDestroyed(PageOverlay*)
410 {
411 }
412
413 void FindController::willMoveToWebPage(PageOverlay*, WebPage* webPage)
414 {
415     if (webPage)
416         return;
417
418     // The page overlay is moving away from the web page, reset it.
419     ASSERT(m_findPageOverlay);
420     m_findPageOverlay = 0;
421 }
422     
423 void FindController::didMoveToWebPage(PageOverlay*, WebPage*)
424 {
425 }
426
427 static const float shadowOffsetX = 0.0;
428 static const float shadowOffsetY = 1.0;
429 static const float shadowBlurRadius = 2.0;
430 static const float whiteFrameThickness = 1.0;
431
432 static const float overlayBackgroundRed = 0.1;
433 static const float overlayBackgroundGreen = 0.1;
434 static const float overlayBackgroundBlue = 0.1;
435 static const float overlayBackgroundAlpha = 0.25;
436
437 static Color overlayBackgroundColor(float fractionFadedIn)
438 {
439     return Color(overlayBackgroundRed, overlayBackgroundGreen, overlayBackgroundBlue, overlayBackgroundAlpha * fractionFadedIn);
440 }
441
442 static Color holeShadowColor(float fractionFadedIn)
443 {
444     return Color(0.0f, 0.0f, 0.0f, fractionFadedIn);
445 }
446
447 static Color holeFillColor(float fractionFadedIn)
448 {
449     return Color(1.0f, 1.0f, 1.0f, fractionFadedIn);
450 }
451
452 void FindController::drawRect(PageOverlay* pageOverlay, GraphicsContext& graphicsContext, const IntRect& dirtyRect)
453 {
454     float fractionFadedIn = pageOverlay->fractionFadedIn();
455
456     Vector<IntRect> rects = rectsForTextMatches();
457
458     // Draw the background.
459     graphicsContext.fillRect(dirtyRect, overlayBackgroundColor(fractionFadedIn), ColorSpaceSRGB);
460
461     {
462         GraphicsContextStateSaver stateSaver(graphicsContext);
463
464         graphicsContext.setShadow(FloatSize(shadowOffsetX, shadowOffsetY), shadowBlurRadius, holeShadowColor(fractionFadedIn), ColorSpaceSRGB);
465         graphicsContext.setFillColor(holeFillColor(fractionFadedIn), ColorSpaceSRGB);
466
467         // Draw white frames around the holes.
468         for (size_t i = 0; i < rects.size(); ++i) {
469             IntRect whiteFrameRect = rects[i];
470             whiteFrameRect.inflate(1);
471
472             graphicsContext.fillRect(whiteFrameRect);
473         }
474     }
475
476     graphicsContext.setFillColor(Color::transparent, ColorSpaceSRGB);
477
478     // Clear out the holes.
479     for (size_t i = 0; i < rects.size(); ++i)
480         graphicsContext.fillRect(rects[i]);
481
482     if (!m_isShowingFindIndicator)
483         return;
484
485     if (Frame* selectedFrame = frameWithSelection(m_webPage->corePage())) {
486         IntRect findIndicatorRect = selectedFrame->view()->contentsToWindow(enclosingIntRect(selectedFrame->selection()->bounds()));
487
488         if (findIndicatorRect != m_findIndicatorRect)
489             hideFindIndicator();
490     }
491 }
492
493 bool FindController::mouseEvent(PageOverlay* pageOverlay, const WebMouseEvent& mouseEvent)
494 {
495     // If we get a mouse down event inside the page overlay we should hide the find UI.
496     if (mouseEvent.type() == WebEvent::MouseDown) {
497         // Dismiss the overlay.
498         hideFindUI();
499     }
500
501     return false;
502 }
503
504 } // namespace WebKit