Add support for retrieving the currently selected text. Implement on all backends, document and unit test.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/SOC2011_WEBVIEW@68220 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
		@@ -142,6 +142,7 @@ public:
 | 
				
			|||||||
    virtual void DeleteSelection();
 | 
					    virtual void DeleteSelection();
 | 
				
			||||||
    virtual bool HasSelection();
 | 
					    virtual bool HasSelection();
 | 
				
			||||||
    virtual void SelectAll();
 | 
					    virtual void SelectAll();
 | 
				
			||||||
 | 
					    virtual wxString GetSelectedText();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /** FIXME: hack to work around signals being received too early */
 | 
					    /** FIXME: hack to work around signals being received too early */
 | 
				
			||||||
    bool m_ready;
 | 
					    bool m_ready;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -101,6 +101,7 @@ public:
 | 
				
			|||||||
    virtual void SelectAll();
 | 
					    virtual void SelectAll();
 | 
				
			||||||
    virtual bool HasSelection();
 | 
					    virtual bool HasSelection();
 | 
				
			||||||
    virtual void DeleteSelection();
 | 
					    virtual void DeleteSelection();
 | 
				
			||||||
 | 
					    virtual wxString GetSelectedText();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // ---- IE-specific methods
 | 
					    // ---- IE-specific methods
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -106,10 +106,9 @@ public:
 | 
				
			|||||||
    virtual void DeleteSelection();
 | 
					    virtual void DeleteSelection();
 | 
				
			||||||
    virtual bool HasSelection() { return false };
 | 
					    virtual bool HasSelection() { return false };
 | 
				
			||||||
    virtual void SelectAll() {};
 | 
					    virtual void SelectAll() {};
 | 
				
			||||||
 | 
					    virtual wxString GetSelectedText();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // ---- methods not from the parent (common) interface
 | 
					    // ---- methods not from the parent (common) interface
 | 
				
			||||||
    wxString GetSelectedText();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    wxString RunScript(const wxString& javascript);
 | 
					    wxString RunScript(const wxString& javascript);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    bool  CanGetPageSource();
 | 
					    bool  CanGetPageSource();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -287,11 +287,9 @@ public:
 | 
				
			|||||||
    virtual void SelectAll() = 0;
 | 
					    virtual void SelectAll() = 0;
 | 
				
			||||||
    virtual bool HasSelection() = 0;
 | 
					    virtual bool HasSelection() = 0;
 | 
				
			||||||
    virtual void DeleteSelection() = 0;
 | 
					    virtual void DeleteSelection() = 0;
 | 
				
			||||||
 | 
					    virtual wxString GetSelectedText() = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO:
 | 
					    // TODO:
 | 
				
			||||||
    //     wxString GetSelection();                         // maybe?
 | 
					 | 
				
			||||||
    //     void SetSelection(...);                          // maybe?
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    //     void EnableJavascript(bool enabled);             // maybe?
 | 
					    //     void EnableJavascript(bool enabled);             // maybe?
 | 
				
			||||||
    //     wxString RunScript(const wxString& javascript);  // maybe?
 | 
					    //     wxString RunScript(const wxString& javascript);  // maybe?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -392,11 +392,16 @@ public:
 | 
				
			|||||||
        correct HTML attribute.
 | 
					        correct HTML attribute.
 | 
				
			||||||
    */
 | 
					    */
 | 
				
			||||||
    virtual void DeleteSelection() = 0;
 | 
					    virtual void DeleteSelection() = 0;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					        Returns the currently selected text, if any.
 | 
				
			||||||
 | 
					    */
 | 
				
			||||||
 | 
					    virtual wxString GetSelectedText() = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
        Returns @true if there is a current selection.
 | 
					        Returns @true if there is a current selection.
 | 
				
			||||||
    */
 | 
					    */
 | 
				
			||||||
    virtual bool HasSelection = 0;
 | 
					    virtual bool HasSelection() = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
        Selects the entire page.
 | 
					        Selects the entire page.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -727,6 +727,21 @@ void wxWebViewWebKit::SelectAll()
 | 
				
			|||||||
    webkit_web_view_select_all(WEBKIT_WEB_VIEW(web_view));
 | 
					    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
 | 
					// static
 | 
				
			||||||
wxVisualAttributes
 | 
					wxVisualAttributes
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -584,6 +584,35 @@ void wxWebViewIE::DeleteSelection()
 | 
				
			|||||||
    ExecCommand("Delete");
 | 
					    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)
 | 
					bool wxWebViewIE::CanExecCommand(wxString command)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    IHTMLDocument2* document = GetDocument();
 | 
					    IHTMLDocument2* document = GetDocument();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -41,6 +41,7 @@ private:
 | 
				
			|||||||
        CPPUNIT_TEST( HistoryClear );
 | 
					        CPPUNIT_TEST( HistoryClear );
 | 
				
			||||||
        CPPUNIT_TEST( HistoryList );
 | 
					        CPPUNIT_TEST( HistoryList );
 | 
				
			||||||
        CPPUNIT_TEST( Editable );
 | 
					        CPPUNIT_TEST( Editable );
 | 
				
			||||||
 | 
					        CPPUNIT_TEST( Selection );
 | 
				
			||||||
    CPPUNIT_TEST_SUITE_END();
 | 
					    CPPUNIT_TEST_SUITE_END();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void Title();
 | 
					    void Title();
 | 
				
			||||||
@@ -50,6 +51,7 @@ private:
 | 
				
			|||||||
    void HistoryClear();
 | 
					    void HistoryClear();
 | 
				
			||||||
    void HistoryList();
 | 
					    void HistoryList();
 | 
				
			||||||
    void Editable();
 | 
					    void Editable();
 | 
				
			||||||
 | 
					    void Selection();
 | 
				
			||||||
    void LoadUrl(const wxString& url, int times = 1);
 | 
					    void LoadUrl(const wxString& url, int times = 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    wxWebView* m_browser;
 | 
					    wxWebView* m_browser;
 | 
				
			||||||
@@ -181,4 +183,19 @@ void WebTestCase::Editable()
 | 
				
			|||||||
    CPPUNIT_ASSERT(!m_browser->IsEditable());
 | 
					    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
 | 
					#endif //wxUSE_WEB
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user