From 10ad4ba66945e4209cbbf7e00c7b83f3b4257030 Mon Sep 17 00:00:00 2001 From: Steve Lamerton Date: Sat, 13 Aug 2011 14:11:32 +0000 Subject: [PATCH] Add history list to the sample. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/SOC2011_WEBVIEW@68681 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- samples/web/web.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/samples/web/web.cpp b/samples/web/web.cpp index 6f9955dbe1..99a275488b 100644 --- a/samples/web/web.cpp +++ b/samples/web/web.cpp @@ -48,6 +48,11 @@ #include "wxlogo.xpm" + +//We map menu items to their history items +WX_DECLARE_HASH_MAP(int, wxSharedPtr, + wxIntegerHash, wxIntegerEqual, wxMenuHistoryMap); + class WebApp : public wxApp { public: @@ -85,6 +90,7 @@ public: void OnRedo(wxCommandEvent& evt); void OnMode(wxCommandEvent& evt); void OnZoomLayout(wxCommandEvent& evt); + void OnHistory(wxCommandEvent& evt); private: wxTextCtrl* m_url; @@ -98,6 +104,7 @@ private: wxToolBarToolBase* m_toolbar_tools; wxMenu* m_tools_menu; + wxMenu* m_tools_history_menu; wxMenuItem* m_tools_layout; wxMenuItem* m_tools_tiny; wxMenuItem* m_tools_small; @@ -119,6 +126,8 @@ private: wxInfoBar *m_info; wxStaticText* m_info_text; + + wxMenuHistoryMap m_histMenuItems; }; class SourceViewDialog : public wxDialog @@ -220,8 +229,14 @@ WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample") m_tools_handle_navigation = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle Navigation")); m_tools_handle_new_window = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle New Windows")); m_tools_menu->AppendSeparator(); - wxMenuItem* clearhist = m_tools_menu->Append(wxID_ANY, _("Clear History")); - m_tools_enable_history = m_tools_menu->AppendCheckItem(wxID_ANY, _("Enable History")); + + //History menu + m_tools_history_menu = new wxMenu(); + wxMenuItem* clearhist = m_tools_history_menu->Append(wxID_ANY, _("Clear History")); + m_tools_enable_history = m_tools_history_menu->AppendCheckItem(wxID_ANY, _("Enable History")); + m_tools_history_menu->AppendSeparator(); + + wxMenuItem* history = m_tools_menu->AppendSubMenu(m_tools_history_menu, "History"); //Create an editing menu wxMenu* editmenu = new wxMenu(); @@ -578,6 +593,41 @@ void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt)) m_edit_undo->Enable(m_browser->CanUndo()); m_edit_redo->Enable(m_browser->CanRedo()); + + //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 ) + { + m_tools_history_menu->Destroy(it->first); + } + m_histMenuItems.clear(); + + wxVector > back = m_browser->GetBackwardHistory(); + wxVector > forward = m_browser->GetForwardHistory(); + + wxMenuItem* item; + + for(unsigned int i = 0; i < back.size(); i++) + { + item = m_tools_history_menu->AppendRadioItem(wxID_ANY, back[i]->GetTitle()); + m_histMenuItems[item->GetId()] = back[i]; + Connect(item->GetId(), wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(WebFrame::OnHistory), NULL, this ); + } + + item = m_tools_history_menu->AppendRadioItem(wxID_ANY, m_browser->GetCurrentTitle()); + item->Check(); + + //No need to connect the current item + m_histMenuItems[item->GetId()] = wxSharedPtr(new wxWebHistoryItem(m_browser->GetCurrentURL(), m_browser->GetCurrentTitle())); + + for(unsigned int i = 0; i < forward.size(); i++) + { + item = m_tools_history_menu->AppendRadioItem(wxID_ANY, forward[i]->GetTitle()); + m_histMenuItems[item->GetId()] = forward[i]; + Connect(item->GetId(), wxEVT_COMMAND_TOOL_CLICKED, + wxCommandEventHandler(WebFrame::OnHistory), NULL, this ); + } wxPoint position = ScreenToClient( wxGetMousePosition() ); PopupMenu(m_tools_menu, position.x, position.y); @@ -622,6 +672,11 @@ void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt)) m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT); } +void WebFrame::OnHistory(wxCommandEvent& evt) +{ + m_browser->LoadHistoryItem(m_histMenuItems[evt.GetId()]); +} + /** * Callback invoked when a loading error occurs */ -- 2.45.2