X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1d156af3247c862e51a7c62f569a3fd302052a42..6496e8e32e4cba6320bd315d310d158ae709f7f2:/samples/webview/webview.cpp diff --git a/samples/webview/webview.cpp b/samples/webview/webview.cpp index 17dca1d8c5..6e7ab1435a 100644 --- a/samples/webview/webview.cpp +++ b/samples/webview/webview.cpp @@ -22,7 +22,12 @@ #include "wx/wx.h" #endif +#if !wxUSE_WEBVIEW_WEBKIT && !wxUSE_WEBVIEW_IE +#error "A wxWebView backend is required by this sample" +#endif + #include "wx/artprov.h" +#include "wx/cmdline.h" #include "wx/notifmsg.h" #include "wx/settings.h" #include "wx/webview.h" @@ -31,7 +36,7 @@ #include "wx/filesys.h" #include "wx/fs_arc.h" -#if !defined(__WXMSW__) && !defined(__WXPM__) +#ifndef wxHAS_IMAGES_IN_RESOURCES #include "../sample.xpm" #endif @@ -56,16 +61,47 @@ WX_DECLARE_HASH_MAP(int, wxSharedPtr, class WebApp : public wxApp { public: + WebApp() : + m_url("http://www.wxwidgets.org") + { + } + virtual bool OnInit(); + +#if wxUSE_CMDLINE_PARSER + virtual void OnInitCmdLine(wxCmdLineParser& parser) + { + wxApp::OnInitCmdLine(parser); + + parser.AddParam("URL to open", + wxCMD_LINE_VAL_STRING, + wxCMD_LINE_PARAM_OPTIONAL); + } + + virtual bool OnCmdLineParsed(wxCmdLineParser& parser) + { + if ( !wxApp::OnCmdLineParsed(parser) ) + return false; + + if ( parser.GetParamCount() ) + m_url = parser.GetParam(0); + + return true; + } +#endif // wxUSE_CMDLINE_PARSER + +private: + wxString m_url; }; class WebFrame : public wxFrame { public: - WebFrame(); + WebFrame(const wxString& url); + virtual ~WebFrame(); - void OnAnimationTimer(wxTimerEvent& evt); void UpdateState(); + void OnIdle(wxIdleEvent& evt); void OnUrl(wxCommandEvent& evt); void OnBack(wxCommandEvent& evt); void OnForward(wxCommandEvent& evt); @@ -91,6 +127,10 @@ public: void OnMode(wxCommandEvent& evt); void OnZoomLayout(wxCommandEvent& evt); void OnHistory(wxCommandEvent& evt); + void OnScrollLineUp(wxCommandEvent&) { m_browser->LineUp(); } + void OnScrollLineDown(wxCommandEvent&) { m_browser->LineDown(); } + void OnScrollPageUp(wxCommandEvent&) { m_browser->PageUp(); } + void OnScrollPageDown(wxCommandEvent&) { m_browser->PageDown(); } void OnRunScript(wxCommandEvent& evt); void OnClearSelection(wxCommandEvent& evt); void OnDeleteSelection(wxCommandEvent& evt); @@ -125,12 +165,13 @@ private: wxMenuItem* m_edit_undo; wxMenuItem* m_edit_redo; wxMenuItem* m_edit_mode; + wxMenuItem* m_scroll_line_up; + wxMenuItem* m_scroll_line_down; + wxMenuItem* m_scroll_page_up; + wxMenuItem* m_scroll_page_down; wxMenuItem* m_selection_clear; wxMenuItem* m_selection_delete; - wxTimer* m_timer; - int m_animation_angle; - wxInfoBar *m_info; wxStaticText* m_info_text; @@ -154,13 +195,14 @@ bool WebApp::OnInit() if ( !wxApp::OnInit() ) return false; - WebFrame *frame = new WebFrame(); + WebFrame *frame = new WebFrame(m_url); frame->Show(); return true; } -WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample") +WebFrame::WebFrame(const wxString& url) : + wxFrame(NULL, wxID_ANY, "wxWebView Sample") { //Required from virtual file system archive support wxFileSystem::AddHandler(new wxArchiveFSHandler); @@ -169,10 +211,6 @@ WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample") SetIcon(wxICON(sample)); SetTitle("wxWebView Sample"); - m_timer = NULL; - m_animation_angle = 0; - - wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL); // Create the toolbar @@ -207,7 +245,7 @@ WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample") topsizer->Add(m_info, wxSizerFlags().Expand()); // Create the webview - m_browser = wxWebView::New(this, wxID_ANY, "http://www.wxwidgets.org"); + m_browser = wxWebView::New(this, wxID_ANY, url); topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1)); //We register the wxfs:// protocol for testing purposes @@ -259,6 +297,13 @@ WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample") m_tools_menu->AppendSeparator(); m_tools_menu->AppendSubMenu(editmenu, "Edit"); + wxMenu* scroll_menu = new wxMenu; + m_scroll_line_up = scroll_menu->Append(wxID_ANY, "Line &up"); + m_scroll_line_down = scroll_menu->Append(wxID_ANY, "Line &down"); + m_scroll_page_up = scroll_menu->Append(wxID_ANY, "Page u&p"); + m_scroll_page_down = scroll_menu->Append(wxID_ANY, "Page d&own"); + m_tools_menu->AppendSubMenu(scroll_menu, "Scroll"); + wxMenuItem* script = m_tools_menu->Append(wxID_ANY, _("Run Script")); //Selection menu @@ -341,6 +386,14 @@ WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample") wxCommandEventHandler(WebFrame::OnRedo), NULL, this ); Connect(m_edit_mode->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WebFrame::OnMode), NULL, this ); + Connect(m_scroll_line_up->GetId(), wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(WebFrame::OnScrollLineUp), NULL, this ); + Connect(m_scroll_line_down->GetId(), wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(WebFrame::OnScrollLineDown), NULL, this ); + Connect(m_scroll_page_up->GetId(), wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(WebFrame::OnScrollPageUp), NULL, this ); + Connect(m_scroll_page_down->GetId(), wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(WebFrame::OnScrollPageDown), NULL, this ); Connect(script->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WebFrame::OnRunScript), NULL, this ); Connect(m_selection_clear->GetId(), wxEVT_COMMAND_MENU_SELECTED, @@ -351,40 +404,14 @@ WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample") wxCommandEventHandler(WebFrame::OnSelectAll), NULL, this ); Connect(loadscheme->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WebFrame::OnLoadScheme), NULL, this ); + + //Connect the idle events + Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(WebFrame::OnIdle), NULL, this); } -void WebFrame::OnAnimationTimer(wxTimerEvent& WXUNUSED(evt)) +WebFrame::~WebFrame() { - m_animation_angle += 15; - if (m_animation_angle > 360) m_animation_angle -= 360; - - wxBitmap image(24, 24); - { - wxMemoryDC dc; - dc.SelectObject(image); - dc.SetBackground(wxBrush(wxColour(255,0,255))); - dc.Clear(); - - if (m_animation_angle >= 0 && m_animation_angle <= 180) - { - dc.SetBrush(*wxYELLOW_BRUSH); - dc.SetPen(*wxYELLOW_PEN); - dc.DrawCircle(16 - int(sin(m_animation_angle*0.01745f /* convert to radians */)*14.0f), - 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 ); - } - - dc.DrawBitmap(wxBitmap(wxlogo_xpm), 0, 0, true); - - if (m_animation_angle > 180) - { - dc.SetBrush(*wxYELLOW_BRUSH); - dc.SetPen(*wxYELLOW_PEN); - dc.DrawCircle(16 - int(sin(m_animation_angle*0.01745f /* convert to radians */)*14.0f), - 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 ); - } - } - image.SetMask(new wxMask(image, wxColour(255,0,255))); - m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), image); + delete m_tools_menu; } /** @@ -398,19 +425,10 @@ void WebFrame::UpdateState() if (m_browser->IsBusy()) { - if (m_timer == NULL) - { - m_timer = new wxTimer(this); - this->Connect(wxEVT_TIMER, wxTimerEventHandler(WebFrame::OnAnimationTimer), NULL, this); - } - m_timer->Start(100); // start animation timer - m_toolbar->EnableTool( m_toolbar_stop->GetId(), true ); } else { - if (m_timer != NULL) m_timer->Stop(); // stop animation timer - m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), wxBitmap(wxlogo_xpm)); m_toolbar->EnableTool( m_toolbar_stop->GetId(), false ); } @@ -418,12 +436,27 @@ void WebFrame::UpdateState() m_url->SetValue( m_browser->GetCurrentURL() ); } +void WebFrame::OnIdle(wxIdleEvent& WXUNUSED(evt)) +{ + if(m_browser->IsBusy()) + { + wxSetCursor(wxCURSOR_ARROWWAIT); + m_toolbar->EnableTool(m_toolbar_stop->GetId(), true); + } + else + { + wxSetCursor(wxNullCursor); + m_toolbar->EnableTool(m_toolbar_stop->GetId(), false); + } +} + /** * Callback invoked when user entered an URL and pressed enter */ void WebFrame::OnUrl(wxCommandEvent& WXUNUSED(evt)) { m_browser->LoadURL( m_url->GetValue() ); + m_browser->SetFocus(); UpdateState(); } @@ -537,8 +570,6 @@ void WebFrame::OnNavigationRequest(wxWebViewEvent& evt) if(!m_tools_handle_navigation->IsChecked()) { evt.Veto(); - if (m_timer != NULL) m_timer->Stop(); // stop animation timer - m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), wxBitmap(wxlogo_xpm)); m_toolbar->EnableTool( m_toolbar_stop->GetId(), false ); } else @@ -586,8 +617,8 @@ void WebFrame::OnNewWindow(wxWebViewEvent& evt) void WebFrame::OnTitleChanged(wxWebViewEvent& evt) { + SetTitle(evt.GetString()); wxLogMessage("%s", "Title changed; title='" + evt.GetString() + "'"); - UpdateState(); } /** @@ -665,7 +696,10 @@ void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt)) wxCommandEventHandler(WebFrame::OnHistory), NULL, this ); } - item = m_tools_history_menu->AppendRadioItem(wxID_ANY, m_browser->GetCurrentTitle()); + wxString title = m_browser->GetCurrentTitle(); + if ( title.empty() ) + title = "(untitled)"; + item = m_tools_history_menu->AppendRadioItem(wxID_ANY, title); item->Check(); //No need to connect the current item @@ -792,7 +826,7 @@ void WebFrame::OnError(wxWebViewEvent& evt) break; } - wxLogMessage("Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")"); + wxLogMessage("%s", "Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")"); //Show the info bar with an error m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" +