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:
Steve Lamerton 2011-07-10 18:11:43 +00:00
parent 63a6507091
commit c9355a3df6
8 changed files with 71 additions and 6 deletions

View 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;

View File

@ -101,6 +101,7 @@ public:
virtual void SelectAll();
virtual bool HasSelection();
virtual void DeleteSelection();
virtual wxString GetSelectedText();
// ---- IE-specific methods

View 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();

View 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?

View 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.

View 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

View 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();

View 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