2011-04-08 Varun Jain <varunjain@chromium.org>
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 8 Apr 2011 18:53:21 +0000 (18:53 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 8 Apr 2011 18:53:21 +0000 (18:53 +0000)
        Reviewed by Darin Fisher.

        Need to extend WebKit chromium API to access text selection
        https://bugs.webkit.org/show_bug.cgi?id=57888

        *  Source/WebKit/chromium/public/WebFrame.h:
        *  Source/WebKit/chromium/public/WebWidget.h:
        *  Source/WebKit/chromium/src/WebFrameImpl.cpp:
        *  Source/WebKit/chromium/src/WebFrameImpl.h:
        *  Source/WebKit/chromium/src/WebPopupMenuImpl.h:
        *  Source/WebKit/chromium/src/WebViewImpl.cpp:
        *  Source/WebKit/chromium/src/WebViewImpl.h:
        *  Source/WebKit/chromium/tests/PopupMenuTest.cpp:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@83320 268f45cc-cd09-0410-ab3c-d52691b4dbfc

ChangeLog
Source/WebKit/chromium/public/WebFrame.h
Source/WebKit/chromium/public/WebWidget.h
Source/WebKit/chromium/src/WebFrameImpl.cpp
Source/WebKit/chromium/src/WebFrameImpl.h
Source/WebKit/chromium/src/WebPopupMenuImpl.h
Source/WebKit/chromium/src/WebViewImpl.cpp
Source/WebKit/chromium/src/WebViewImpl.h
Source/WebKit/chromium/tests/PopupMenuTest.cpp

index c2d4d65..f7eac9b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2011-04-08  Varun Jain  <varunjain@chromium.org>
+
+        Reviewed by Darin Fisher.
+
+        Need to extend WebKit chromium API to access text selection
+        https://bugs.webkit.org/show_bug.cgi?id=57888
+
+        *  Source/WebKit/chromium/public/WebFrame.h:
+        *  Source/WebKit/chromium/public/WebWidget.h:
+        *  Source/WebKit/chromium/src/WebFrameImpl.cpp:
+        *  Source/WebKit/chromium/src/WebFrameImpl.h:
+        *  Source/WebKit/chromium/src/WebPopupMenuImpl.h:
+        *  Source/WebKit/chromium/src/WebViewImpl.cpp:
+        *  Source/WebKit/chromium/src/WebViewImpl.h:
+        *  Source/WebKit/chromium/tests/PopupMenuTest.cpp:
+
 2011-04-08  Jon Lee  <jonlee@apple.com>
 
         Reviewed by David Levin.
index d5d2ff4..934cf08 100644 (file)
@@ -67,6 +67,7 @@ class WebURLRequest;
 class WebView;
 struct WebConsoleMessage;
 struct WebFindOptions;
+struct WebPoint;
 struct WebRect;
 struct WebScriptSource;
 struct WebSize;
@@ -408,6 +409,8 @@ public:
     // there is ranged selection.
     virtual bool selectWordAroundCaret() = 0;
 
+    virtual void selectRange(const WebPoint& start, const WebPoint& end) = 0;
+
 
     // Printing ------------------------------------------------------------
 
index 36fbf31..c67762f 100644 (file)
@@ -41,6 +41,7 @@ namespace WebKit {
 
 class WebInputEvent;
 class WebString;
+struct WebPoint;
 struct WebRect;
 struct WebSize;
 template <typename T> class WebVector;
@@ -126,6 +127,15 @@ public:
     // will be returned if a selection range is available.
     virtual WebRect caretOrSelectionBounds() = 0;
 
+    // Returns the start and end point for the current selection, aligned to the
+    // bottom of the selected line.
+    // FIXME: make this pure virtual after all downstream classes have
+    // implemented it.
+    virtual bool selectionRange(WebPoint& start, WebPoint& end) const
+    {
+        return false;
+    }
+
     // Changes the text direction of the selected input node.
     virtual void setTextDirection(WebTextDirection) = 0;
 
index 9c5e05f..14db974 100644 (file)
@@ -90,6 +90,7 @@
 #include "FrameLoader.h"
 #include "FrameTree.h"
 #include "FrameView.h"
+#include "HitTestResult.h"
 #include "HTMLCollection.h"
 #include "HTMLFormElement.h"
 #include "HTMLFrameOwnerElement.h"
 #include "PluginDocument.h"
 #include "PrintContext.h"
 #include "RenderFrame.h"
+#include "RenderLayer.h"
 #include "RenderObject.h"
 #include "RenderTreeAsText.h"
 #include "RenderView.h"
 #include "WebPerformance.h"
 #include "WebPlugin.h"
 #include "WebPluginContainerImpl.h"
+#include "WebPoint.h"
 #include "WebRange.h"
 #include "WebRect.h"
 #include "WebScriptSource.h"
@@ -1305,6 +1308,39 @@ bool WebFrameImpl::selectWordAroundCaret()
     return true;
 }
 
+void WebFrameImpl::selectRange(const WebPoint& start, const WebPoint& end)
+{
+    VisibleSelection selection(visiblePositionForWindowPoint(start),
+                               visiblePositionForWindowPoint(end));
+
+    if (frame()->selection()->shouldChangeSelection(selection))
+        frame()->selection()->setSelection(selection, CharacterGranularity,
+                                           MakeNonDirectionalSelection);
+}
+
+VisiblePosition WebFrameImpl::visiblePositionForWindowPoint(const WebPoint& point)
+{
+    HitTestRequest::HitTestRequestType hitType = HitTestRequest::MouseMove;
+    hitType |= HitTestRequest::ReadOnly;
+    hitType |= HitTestRequest::Active;
+    HitTestRequest request(hitType);
+    FrameView* view = frame()->view();
+    HitTestResult result(view->windowToContents(
+        view->convertFromContainingWindow(IntPoint(point.x, point.y))));
+
+    frame()->document()->renderView()->layer()->hitTest(request, result);
+
+    // Matching the logic in MouseEventWithHitTestResults::targetNode()
+    Node* node = result.innerNode();
+    if (!node)
+        return VisiblePosition();
+    Element* element = node->parentElement();
+    if (!node->inDocument() && element && element->inDocument())
+        node = element;
+
+    return node->renderer()->positionForPoint(result.localPoint());
+}
+
 int WebFrameImpl::printBegin(const WebSize& pageSize,
                              const WebNode& constrainToNode,
                              int printerDPI,
index 6129de1..a52b0f7 100644 (file)
@@ -157,6 +157,7 @@ public:
     virtual WebString selectionAsText() const;
     virtual WebString selectionAsMarkup() const;
     virtual bool selectWordAroundCaret();
+    virtual void selectRange(const WebPoint& start, const WebPoint& end);
     virtual int printBegin(const WebSize& pageSize,
                            const WebNode& constrainToNode,
                            int printerDPI,
@@ -326,6 +327,9 @@ private:
 
     void loadJavaScriptURL(const WebCore::KURL&);
 
+    // Returns a hit-tested VisiblePosition for the given point
+    WebCore::VisiblePosition visiblePositionForWindowPoint(const WebPoint&);
+
     FrameLoaderClientImpl m_frameLoaderClient;
 
     WebFrameClient* m_client;
index 7bb9f7e..58a883f 100644 (file)
@@ -78,6 +78,7 @@ public:
     virtual bool confirmComposition(const WebString& text);
     virtual WebTextInputType textInputType();
     virtual WebRect caretOrSelectionBounds();
+    virtual bool selectionRange(WebPoint& start, WebPoint& end) const { return false; }
     virtual void setTextDirection(WebTextDirection direction);
     virtual bool isAcceleratedCompositingActive() const { return false; }
 
index 669efaf..407a8dc 100644 (file)
@@ -1454,6 +1454,37 @@ WebRect WebViewImpl::caretOrSelectionBounds()
     return rect;
 }
 
+bool WebViewImpl::selectionRange(WebPoint& start, WebPoint& end) const
+{
+    const Frame* frame = focusedWebCoreFrame();
+    if (!frame)
+        return false;
+    RefPtr<Range> selectedRange = frame->selection()->toNormalizedRange();
+    RefPtr<Range> range(Range::create(selectedRange->startContainer()->document(),
+                                      selectedRange->startContainer(),
+                                      selectedRange->startOffset(),
+                                      selectedRange->startContainer(),
+                                      selectedRange->startOffset()));
+
+    IntRect rect = frame->editor()->firstRectForRange(range.get());
+    start.x = rect.x();
+    start.y = rect.y() + rect.height() - 1;
+
+    range = Range::create(selectedRange->endContainer()->document(),
+                          selectedRange->endContainer(),
+                          selectedRange->endOffset(),
+                          selectedRange->endContainer(),
+                          selectedRange->endOffset());
+
+    rect = frame->editor()->firstRectForRange(range.get());
+    end.x = rect.x() + rect.width() - 1;
+    end.y = rect.y() + rect.height() - 1;
+
+    start = frame->view()->contentsToWindow(start);
+    end = frame->view()->contentsToWindow(end);
+    return true;
+}
+
 void WebViewImpl::setTextDirection(WebTextDirection direction)
 {
     // The Editor::setBaseWritingDirection() function checks if we can change
index 66b2908..06027ea 100644 (file)
@@ -109,6 +109,7 @@ public:
     virtual bool confirmComposition(const WebString& text);
     virtual WebTextInputType textInputType();
     virtual WebRect caretOrSelectionBounds();
+    virtual bool selectionRange(WebPoint& start, WebPoint& end) const;
     virtual void setTextDirection(WebTextDirection direction);
     virtual bool isAcceleratedCompositingActive() const;
 
index 8c53ffc..5a18708 100644 (file)
@@ -142,6 +142,7 @@ public:
     virtual bool confirmComposition(const WebString& text) { return true; }
     virtual WebTextInputType textInputType() { return WebKit::WebTextInputTypeNone; }
     virtual WebRect caretOrSelectionBounds() { return WebRect(); }
+    virtual bool selectionRange(WebPoint& start, WebPoint& end) const { return false; }
     virtual void setTextDirection(WebTextDirection) { }
 };