X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/66ac04006071ae2043c483ae93f8d4fa6888f33f..f155075229d771430f0793700f5048ad4be00e9d:/samples/webview/webview.cpp diff --git a/samples/webview/webview.cpp b/samples/webview/webview.cpp index 6c0773b6fe..fc134a9498 100644 --- a/samples/webview/webview.cpp +++ b/samples/webview/webview.cpp @@ -32,9 +32,11 @@ #include "wx/settings.h" #include "wx/webview.h" #include "wx/webviewarchivehandler.h" +#include "wx/webviewfshandler.h" #include "wx/infobar.h" #include "wx/filesys.h" #include "wx/fs_arc.h" +#include "wx/fs_mem.h" #ifndef wxHAS_IMAGES_IN_RESOURCES #include "../sample.xpm" @@ -136,10 +138,12 @@ public: void OnDeleteSelection(wxCommandEvent& evt); void OnSelectAll(wxCommandEvent& evt); void OnLoadScheme(wxCommandEvent& evt); + void OnUseMemoryFS(wxCommandEvent& evt); void OnFind(wxCommandEvent& evt); void OnFindDone(wxCommandEvent& evt); void OnFindText(wxCommandEvent& evt); void OnFindOptions(wxCommandEvent& evt); + void OnEnableContextMenu(wxCommandEvent& evt); private: wxTextCtrl* m_url; @@ -185,6 +189,7 @@ private: wxMenuItem* m_selection_clear; wxMenuItem* m_selection_delete; wxMenuItem* m_find; + wxMenuItem* m_context_menu; wxInfoBar *m_info; wxStaticText* m_info_text; @@ -213,6 +218,27 @@ bool WebApp::OnInit() if ( !wxApp::OnInit() ) return false; + //Required for virtual file system archive and memory support + wxFileSystem::AddHandler(new wxArchiveFSHandler); + wxFileSystem::AddHandler(new wxMemoryFSHandler); + + // Create the memory files + wxImage::AddHandler(new wxPNGHandler); + wxMemoryFSHandler::AddFile("logo.png", + wxBitmap(wxlogo_xpm), wxBITMAP_TYPE_PNG); + wxMemoryFSHandler::AddFile("page1.htm", + "File System Example" + "" + "

Page 1

" + "

" + "

Some text about Page 2.

"); + wxMemoryFSHandler::AddFile("page2.htm", + "File System Example" + "" + "

Page 2

" + "

Page 1 was better.

"); + wxMemoryFSHandler::AddFile("test.css", "h1 {color: red;}"); + WebFrame *frame = new WebFrame(m_url); frame->Show(); @@ -222,9 +248,6 @@ bool WebApp::OnInit() WebFrame::WebFrame(const wxString& url) : wxFrame(NULL, wxID_ANY, "wxWebView Sample") { - //Required from virtual file system archive support - wxFileSystem::AddHandler(new wxArchiveFSHandler); - // set the frame icon SetIcon(wxICON(sample)); SetTitle("wxWebView Sample"); @@ -259,7 +282,7 @@ WebFrame::WebFrame(const wxString& url) : m_toolbar->Realize(); // Set find values. - m_findFlags = wxWEB_VIEW_FIND_DEFAULT; + m_findFlags = wxWEBVIEW_FIND_DEFAULT; m_findText = wxEmptyString; m_findCount = 0; @@ -309,6 +332,8 @@ WebFrame::WebFrame(const wxString& url) : //We register the wxfs:// protocol for testing purposes m_browser->RegisterHandler(wxSharedPtr(new wxWebViewArchiveHandler("wxfs"))); + //And the memory: file system + m_browser->RegisterHandler(wxSharedPtr(new wxWebViewFSHandler("memory"))); SetSizer(topsizer); @@ -378,12 +403,15 @@ WebFrame::WebFrame(const wxString& url) : editmenu->AppendSubMenu(selection, "Selection"); wxMenuItem* loadscheme = m_tools_menu->Append(wxID_ANY, _("Custom Scheme Example")); + wxMenuItem* usememoryfs = m_tools_menu->Append(wxID_ANY, _("Memory File System Example")); + + m_context_menu = m_tools_menu->AppendCheckItem(wxID_ANY, _("Enable Context Menu")); //By default we want to handle navigation and new windows m_tools_handle_navigation->Check(); m_tools_handle_new_window->Check(); m_tools_enable_history->Check(); - if(!m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT)) + if(!m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT)) m_tools_layout->Enable(false); @@ -417,17 +445,17 @@ WebFrame::WebFrame(const wxString& url) : wxCommandEventHandler(WebFrame::OnFindText), NULL, this ); // Connect the webview events - Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING, + Connect(m_browser->GetId(), wxEVT_COMMAND_WEBVIEW_NAVIGATING, wxWebViewEventHandler(WebFrame::OnNavigationRequest), NULL, this); - Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED, + Connect(m_browser->GetId(), wxEVT_COMMAND_WEBVIEW_NAVIGATED, wxWebViewEventHandler(WebFrame::OnNavigationComplete), NULL, this); - Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED, + Connect(m_browser->GetId(), wxEVT_COMMAND_WEBVIEW_LOADED, wxWebViewEventHandler(WebFrame::OnDocumentLoaded), NULL, this); - Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR, + Connect(m_browser->GetId(), wxEVT_COMMAND_WEBVIEW_ERROR, wxWebViewEventHandler(WebFrame::OnError), NULL, this); - Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, + Connect(m_browser->GetId(), wxEVT_COMMAND_WEBVIEW_NEWWINDOW, wxWebViewEventHandler(WebFrame::OnNewWindow), NULL, this); - Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, + Connect(m_browser->GetId(), wxEVT_COMMAND_WEBVIEW_TITLE_CHANGED, wxWebViewEventHandler(WebFrame::OnTitleChanged), NULL, this); // Connect the menu events @@ -481,8 +509,12 @@ WebFrame::WebFrame(const wxString& url) : wxCommandEventHandler(WebFrame::OnSelectAll), NULL, this ); Connect(loadscheme->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WebFrame::OnLoadScheme), NULL, this ); + Connect(usememoryfs->GetId(), wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(WebFrame::OnUseMemoryFS), NULL, this ); Connect(m_find->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WebFrame::OnFind), NULL, this ); + Connect(m_context_menu->GetId(), wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(WebFrame::OnEnableContextMenu), NULL, this ); //Connect the idle events Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(WebFrame::OnIdle), NULL, this); @@ -628,6 +660,16 @@ void WebFrame::OnLoadScheme(wxCommandEvent& WXUNUSED(evt)) m_browser->LoadURL(path); } +void WebFrame::OnUseMemoryFS(wxCommandEvent& WXUNUSED(evt)) +{ + m_browser->LoadURL("memory:page1.htm"); +} + +void WebFrame::OnEnableContextMenu(wxCommandEvent& evt) +{ + m_browser->EnableContextMenu(evt.IsChecked()); +} + void WebFrame::OnFind(wxCommandEvent& WXUNUSED(evt)) { wxString value = m_browser->GetSelectedText(); @@ -655,16 +697,16 @@ void WebFrame::OnFindText(wxCommandEvent& evt) int flags = 0; if(m_find_toolbar_wrap->IsChecked()) - flags |= wxWEB_VIEW_FIND_WRAP; + flags |= wxWEBVIEW_FIND_WRAP; if(m_find_toolbar_wholeword->IsChecked()) - flags |= wxWEB_VIEW_FIND_ENTIRE_WORD; + flags |= wxWEBVIEW_FIND_ENTIRE_WORD; if(m_find_toolbar_matchcase->IsChecked()) - flags |= wxWEB_VIEW_FIND_MATCH_CASE; + flags |= wxWEBVIEW_FIND_MATCH_CASE; if(m_find_toolbar_highlight->IsChecked()) - flags |= wxWEB_VIEW_FIND_HIGHLIGHT_RESULT; + flags |= wxWEBVIEW_FIND_HIGHLIGHT_RESULT; if(m_find_toolbar_previous->GetId() == evt.GetId()) - flags |= wxWEB_VIEW_FIND_BACKWARDS; + flags |= wxWEBVIEW_FIND_BACKWARDS; wxString find_text = m_find_ctrl->GetValue(); long count = m_browser->Find(find_text, flags); @@ -792,19 +834,19 @@ void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt)) wxWebViewZoom zoom = m_browser->GetZoom(); switch (zoom) { - case wxWEB_VIEW_ZOOM_TINY: + case wxWEBVIEW_ZOOM_TINY: m_tools_tiny->Check(); break; - case wxWEB_VIEW_ZOOM_SMALL: + case wxWEBVIEW_ZOOM_SMALL: m_tools_small->Check(); break; - case wxWEB_VIEW_ZOOM_MEDIUM: + case wxWEBVIEW_ZOOM_MEDIUM: m_tools_medium->Check(); break; - case wxWEB_VIEW_ZOOM_LARGE: + case wxWEBVIEW_ZOOM_LARGE: m_tools_large->Check(); break; - case wxWEB_VIEW_ZOOM_LARGEST: + case wxWEBVIEW_ZOOM_LARGEST: m_tools_largest->Check(); break; } @@ -819,6 +861,8 @@ void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt)) m_selection_clear->Enable(m_browser->HasSelection()); m_selection_delete->Enable(m_browser->HasSelection()); + m_context_menu->Check(m_browser->IsContextMenuEnabled()); + //Firstly we clear the existing menu items, then we add the current ones wxMenuHistoryMap::const_iterator it; for( it = m_histMenuItems.begin(); it != m_histMenuItems.end(); ++it ) @@ -869,23 +913,23 @@ void WebFrame::OnSetZoom(wxCommandEvent& evt) { if (evt.GetId() == m_tools_tiny->GetId()) { - m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY); + m_browser->SetZoom(wxWEBVIEW_ZOOM_TINY); } else if (evt.GetId() == m_tools_small->GetId()) { - m_browser->SetZoom(wxWEB_VIEW_ZOOM_SMALL); + m_browser->SetZoom(wxWEBVIEW_ZOOM_SMALL); } else if (evt.GetId() == m_tools_medium->GetId()) { - m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM); + m_browser->SetZoom(wxWEBVIEW_ZOOM_MEDIUM); } else if (evt.GetId() == m_tools_large->GetId()) { - m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGE); + m_browser->SetZoom(wxWEBVIEW_ZOOM_LARGE); } else if (evt.GetId() == m_tools_largest->GetId()) { - m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGEST); + m_browser->SetZoom(wxWEBVIEW_ZOOM_LARGEST); } else { @@ -896,9 +940,9 @@ void WebFrame::OnSetZoom(wxCommandEvent& evt) void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt)) { if(m_tools_layout->IsChecked()) - m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT); + m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT); else - m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT); + m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT); } void WebFrame::OnHistory(wxCommandEvent& evt) @@ -934,48 +978,30 @@ void WebFrame::OnSelectAll(wxCommandEvent& WXUNUSED(evt)) * Callback invoked when a loading error occurs */ void WebFrame::OnError(wxWebViewEvent& evt) -{ - wxString errorCategory; - switch (evt.GetInt()) - { - case wxWEB_NAV_ERR_CONNECTION: - errorCategory = "wxWEB_NAV_ERR_CONNECTION"; +{ +#define WX_ERROR_CASE(type) \ + case type: \ + category = #type; \ break; - case wxWEB_NAV_ERR_CERTIFICATE: - errorCategory = "wxWEB_NAV_ERR_CERTIFICATE"; - break; - - case wxWEB_NAV_ERR_AUTH: - errorCategory = "wxWEB_NAV_ERR_AUTH"; - break; - - case wxWEB_NAV_ERR_SECURITY: - errorCategory = "wxWEB_NAV_ERR_SECURITY"; - break; - - case wxWEB_NAV_ERR_NOT_FOUND: - errorCategory = "wxWEB_NAV_ERR_NOT_FOUND"; - break; - - case wxWEB_NAV_ERR_REQUEST: - errorCategory = "wxWEB_NAV_ERR_REQUEST"; - break; - - case wxWEB_NAV_ERR_USER_CANCELLED: - errorCategory = "wxWEB_NAV_ERR_USER_CANCELLED"; - break; - - case wxWEB_NAV_ERR_OTHER: - errorCategory = "wxWEB_NAV_ERR_OTHER"; - break; + wxString category; + switch (evt.GetInt()) + { + WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_CONNECTION); + WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_CERTIFICATE); + WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_AUTH); + WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_SECURITY); + WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_NOT_FOUND); + WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_REQUEST); + WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_USER_CANCELLED); + WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_OTHER); } - wxLogMessage("%s", "Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")"); + wxLogMessage("%s", "Error; url='" + evt.GetURL() + "', error='" + category + " (" + evt.GetString() + ")'"); //Show the info bar with an error m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" + - "'" + errorCategory + "' (" + evt.GetString() + ")", wxICON_ERROR); + "'" + category + "'", wxICON_ERROR); UpdateState(); }