From: Steve Lamerton Date: Sat, 2 Jul 2011 11:45:19 +0000 (+0000) Subject: Add new clipboard api and support for it in the ie backend. Also extend the sample... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/4681a3ea0d0502ec7a787bc4dd24e43718a02b7c Add new clipboard api and support for it in the ie backend. Also extend the sample to demonstrate the new functions. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/SOC2011_WEBVIEW@68129 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/msw/webview_ie.h b/include/wx/msw/webview_ie.h index f71e6d5297..c288314f03 100644 --- a/include/wx/msw/webview_ie.h +++ b/include/wx/msw/webview_ie.h @@ -77,6 +77,14 @@ public: virtual wxWebViewZoom GetZoom(); virtual void SetZoom(wxWebViewZoom zoom); + //Clipboard functions + virtual bool CanCut(); + virtual bool CanCopy(); + virtual bool CanPaste(); + virtual void Cut(); + virtual void Copy(); + virtual void Paste(); + // ---- IE-specific methods // FIXME: I seem to be able to access remote webpages even in offline mode... @@ -124,6 +132,10 @@ private: bool m_historyLoadingFromList; bool m_historyEnabled; + //Generic helper functions for IHtmlDocument commands + bool CanExecCommand(wxString command); + void ExecCommand(wxString command); + }; #endif // wxUSE_WEBVIEW_IE diff --git a/include/wx/webview.h b/include/wx/webview.h index 57812cbf81..b3bb60f223 100644 --- a/include/wx/webview.h +++ b/include/wx/webview.h @@ -324,6 +324,14 @@ public: * Returns whether the web control is currently busy (e.g. loading a page) */ virtual bool IsBusy() = 0; + + //Clipboard functions + virtual bool CanCut() = 0; + virtual bool CanCopy() = 0; + virtual bool CanPaste() = 0; + virtual void Cut() = 0; + virtual void Copy() = 0; + virtual void Paste() = 0; }; class WXDLLIMPEXP_WEB wxWebNavigationEvent : public wxCommandEvent diff --git a/samples/web/web.cpp b/samples/web/web.cpp index cd2fa488fa..f2f8f6a04b 100644 --- a/samples/web/web.cpp +++ b/samples/web/web.cpp @@ -71,6 +71,9 @@ public: void OnSetZoom(wxCommandEvent& evt); void OnError(wxWebNavigationEvent& evt); void OnPrint(wxCommandEvent& evt); + void OnCut(wxCommandEvent& evt); + void OnCopy(wxCommandEvent& evt); + void OnPaste(wxCommandEvent& evt); private: wxTextCtrl* m_url; @@ -92,6 +95,9 @@ private: wxMenuItem* m_tools_handle_navigation; wxMenuItem* m_tools_handle_new_window; wxMenuItem* m_tools_enable_history; + wxMenuItem* m_edit_cut; + wxMenuItem* m_edit_copy; + wxMenuItem* m_edit_paste; wxTimer* m_timer; int m_animation_angle; @@ -195,6 +201,15 @@ WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample") wxMenuItem* clearhist = m_tools_menu->Append(wxID_ANY, _("Clear History")); m_tools_enable_history = m_tools_menu->AppendCheckItem(wxID_ANY, _("Enable History")); + //Create an editing menu + wxMenu* editmenu = new wxMenu(); + m_edit_cut = editmenu->Append(wxID_ANY, _("Cut")); + m_edit_copy = editmenu->Append(wxID_ANY, _("Copy")); + m_edit_paste = editmenu->Append(wxID_ANY, _("Paste")); + + m_tools_menu->AppendSeparator(); + m_tools_menu->AppendSubMenu(editmenu, "Edit"); + //By default we want to handle navigation and new windows m_tools_handle_navigation->Check(); m_tools_handle_new_window->Check(); @@ -247,6 +262,12 @@ WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample") wxCommandEventHandler(WebFrame::OnClearHistory), NULL, this ); Connect(m_tools_enable_history->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WebFrame::OnEnableHistory), NULL, this ); + Connect(m_edit_cut->GetId(), wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(WebFrame::OnCut), NULL, this ); + Connect(m_edit_copy->GetId(), wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(WebFrame::OnCopy), NULL, this ); + Connect(m_edit_paste->GetId(), wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(WebFrame::OnPaste), NULL, this ); } void WebFrame::OnAnimationTimer(wxTimerEvent& evt) @@ -371,6 +392,21 @@ void WebFrame::OnEnableHistory(wxCommandEvent& evt) UpdateState(); } +void WebFrame::OnCut(wxCommandEvent& evt) +{ + m_browser->Cut(); +} + +void WebFrame::OnCopy(wxCommandEvent& evt) +{ + m_browser->Copy(); +} + +void WebFrame::OnPaste(wxCommandEvent& evt) +{ + m_browser->Paste(); +} + /** * Callback invoked when there is a request to load a new page (for instance * when the user clicks a link) @@ -469,6 +505,10 @@ void WebFrame::OnToolsClicked(wxCommandEvent& evt) m_tools_largest->Check(); break; } + + m_edit_cut->Enable(m_browser->CanCut()); + m_edit_copy->Enable(m_browser->CanCopy()); + m_edit_paste->Enable(m_browser->CanPaste()); wxPoint position = ScreenToClient( wxGetMousePosition() ); PopupMenu(m_tools_menu, position.x, position.y); diff --git a/src/msw/webview_ie.cpp b/src/msw/webview_ie.cpp index 2823949274..dcb8437654 100644 --- a/src/msw/webview_ie.cpp +++ b/src/msw/webview_ie.cpp @@ -510,6 +510,60 @@ wxString wxWebViewIE::GetCurrentTitle() return out.GetString(); } +bool wxWebViewIE::CanCut() +{ + return CanExecCommand("Cut"); +} + +bool wxWebViewIE::CanCopy() +{ + return CanExecCommand("Copy"); +} +bool wxWebViewIE::CanPaste() +{ + return CanExecCommand("Paste"); +} + +void wxWebViewIE::Cut() +{ + ExecCommand("Cut"); +} + +void wxWebViewIE::Copy() +{ + ExecCommand("Copy"); +} + +void wxWebViewIE::Paste() +{ + ExecCommand("Paste"); +} + +bool wxWebViewIE::CanExecCommand(wxString command) +{ + wxVariant documentVariant = m_ie.GetProperty("Document"); + void* documentPtr = documentVariant.GetVoidPtr(); + IHTMLDocument2* document = (IHTMLDocument2*)documentPtr; + + wxASSERT(documentPtr && document); + + VARIANT_BOOL enabled; + document->queryCommandEnabled(SysAllocString(command.wc_str()), &enabled); + + return (enabled == VARIANT_TRUE); +} + +void wxWebViewIE::ExecCommand(wxString command) +{ + wxVariant documentVariant = m_ie.GetProperty("Document"); + void* documentPtr = documentVariant.GetVoidPtr(); + IHTMLDocument2* document = (IHTMLDocument2*)documentPtr; + + wxASSERT(documentPtr && document); + + document->execCommand(SysAllocString(command.wc_str()), VARIANT_FALSE, VARIANT(), NULL); +} + void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt) { if (m_webBrowser == NULL) return;