]> git.saurik.com Git - wxWidgets.git/commitdiff
Add support for retrieving the currently selected text. Implement on all backends...
authorSteve Lamerton <steve.lamerton@gmail.com>
Sun, 10 Jul 2011 18:11:43 +0000 (18:11 +0000)
committerSteve Lamerton <steve.lamerton@gmail.com>
Sun, 10 Jul 2011 18:11:43 +0000 (18:11 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/SOC2011_WEBVIEW@68220 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/gtk/webview_webkit.h
include/wx/msw/webview_ie.h
include/wx/osx/webview_webkit.h
include/wx/webview.h
interface/wx/webview.h
src/gtk/webview_webkit.cpp
src/msw/webview_ie.cpp
tests/controls/webtest.cpp

index 3e4e0f7c88610ecd25b931cc90d023c1a883208b..455007a3ec9a9027f8db4ecdbb0b1b71d2e543e2 100644 (file)
@@ -142,6 +142,7 @@ public:
     virtual void DeleteSelection();
     virtual bool HasSelection();
     virtual void SelectAll();
+    virtual wxString GetSelectedText();
 
     /** FIXME: hack to work around signals being received too early */
     bool m_ready;
index 2a8163c111780301ef7526da7213c15568241ccd..75f2a583ddd421c86bd4f5f5d602779e735d780a 100644 (file)
@@ -101,6 +101,7 @@ public:
     virtual void SelectAll();
     virtual bool HasSelection();
     virtual void DeleteSelection();
+    virtual wxString GetSelectedText();
 
 
     // ---- IE-specific methods
index 37fbf22acceccf0ac82673970dd3752981900f60..0a3ec2d638f62a753ada1dea7bc47bd2274b3ade 100644 (file)
@@ -106,10 +106,9 @@ public:
     virtual void DeleteSelection();
     virtual bool HasSelection() { return false };
     virtual void SelectAll() {};
+    virtual wxString GetSelectedText();
 
     // ---- methods not from the parent (common) interface
-    wxString GetSelectedText();
-
     wxString RunScript(const wxString& javascript);
 
     bool  CanGetPageSource();
index ea241d606cfe5f2d43375b8b879c1ff146d16824..06aa126ef3d3c87e59a0c69150ece5df2aa12b5b 100644 (file)
@@ -287,11 +287,9 @@ public:
     virtual void SelectAll() = 0;
     virtual bool HasSelection() = 0;
     virtual void DeleteSelection() = 0;
+    virtual wxString GetSelectedText() = 0;
 
     // TODO:
-    //     wxString GetSelection();                         // maybe?
-    //     void SetSelection(...);                          // maybe?
-
     //     void EnableJavascript(bool enabled);             // maybe?
     //     wxString RunScript(const wxString& javascript);  // maybe?
 
index 5927871edde34f9d22e8d940595c73c55738731d..441dd6894a390cd0a1334688072be2f2a9434c2a 100644 (file)
@@ -392,11 +392,16 @@ public:
         correct HTML attribute.
     */
     virtual void DeleteSelection() = 0;
+    
+    /**
+        Returns the currently selected text, if any.
+    */
+    virtual wxString GetSelectedText() = 0;
 
     /**
         Returns @true if there is a current selection.
     */
-    virtual bool HasSelection = 0;
+    virtual bool HasSelection() = 0;
 
     /**
         Selects the entire page.
index dd9a375a755e96f8bddc9b3f12a357675cbf244f..96765cfd985acf652a8f33e3460335fe4f25579c 100644 (file)
@@ -727,6 +727,21 @@ void wxWebViewWebKit::SelectAll()
     webkit_web_view_select_all(WEBKIT_WEB_VIEW(web_view));
 }
 
+wxString wxWebViewWebKit::GetSelectedText()
+{
+    WebKitDOMDocument* doc; 
+    WebKitDOMDOMWindow* win;
+    WebKitDOMDOMSelection* sel;
+    WebKitDOMRange* range;
+
+    doc = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(web_view));
+    win = webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc));
+    sel = webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win));
+    range = webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel), 
+                                                  0, NULL);
+    return wxString(webkit_dom_range_get_text(WEBKIT_DOM_RANGE(range)), 
+                    wxConvUTF8);
+}
 
 // static
 wxVisualAttributes
index 3e0ce6c5e2c24bc677e744304b744e10ac8b05c7..0735858ff6b8892e9acf374cf2ec6bf1fde9f555 100644 (file)
@@ -584,6 +584,35 @@ void wxWebViewIE::DeleteSelection()
     ExecCommand("Delete");
 }
 
+wxString wxWebViewIE::GetSelectedText()
+{
+    IHTMLDocument2* document = GetDocument();
+    IHTMLSelectionObject* selection;
+    wxString selected;
+    HRESULT hr = document->get_selection(&selection);
+    if(SUCCEEDED(hr))
+    {
+        IDispatch* disrange;
+        hr = selection->createRange(&disrange);
+        if(SUCCEEDED(hr))
+        {
+            IHTMLTxtRange* range;
+            hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
+            if(SUCCEEDED(hr))
+            {
+                BSTR text;
+                range->get_text(&text);
+                selected = wxString(text);
+                range->Release();
+            }
+            disrange->Release();
+        }
+        selection->Release();
+    }
+    document->Release();
+    return selected;
+}
+
 bool wxWebViewIE::CanExecCommand(wxString command)
 {
     IHTMLDocument2* document = GetDocument();
index d4cd6f341a27424fd2eacb6d80488d0c17b1bacf..d509c04a28116fb335abb6417038181b2a80f3cb 100644 (file)
@@ -41,6 +41,7 @@ private:
         CPPUNIT_TEST( HistoryClear );
         CPPUNIT_TEST( HistoryList );
         CPPUNIT_TEST( Editable );
+        CPPUNIT_TEST( Selection );
     CPPUNIT_TEST_SUITE_END();
 
     void Title();
@@ -50,6 +51,7 @@ private:
     void HistoryClear();
     void HistoryList();
     void Editable();
+    void Selection();
     void LoadUrl(const wxString& url, int times = 1);
 
     wxWebView* m_browser;
@@ -181,4 +183,19 @@ void WebTestCase::Editable()
     CPPUNIT_ASSERT(!m_browser->IsEditable());
 }
 
+void WebTestCase::Selection()
+{
+    m_browser->SetPage("<html><body>Some text</body></html>", "");
+    CPPUNIT_ASSERT(!m_browser->HasSelection());
+
+    m_browser->SelectAll();
+
+    CPPUNIT_ASSERT(m_browser->HasSelection());
+    CPPUNIT_ASSERT_EQUAL("Some text", m_browser->GetSelectedText());
+
+    m_browser->DeleteSelection();
+
+    CPPUNIT_ASSERT(!m_browser->HasSelection());
+}
+
 #endif //wxUSE_WEB