diff --git a/include/wx/filehistory.h b/include/wx/filehistory.h index 9cd4be4034..f7f0183ad3 100644 --- a/include/wx/filehistory.h +++ b/include/wx/filehistory.h @@ -24,6 +24,13 @@ class WXDLLIMPEXP_FWD_CORE wxMenu; class WXDLLIMPEXP_FWD_BASE wxConfigBase; class WXDLLIMPEXP_FWD_BASE wxFileName; +enum wxFileHistoryMenuLabelStyle +{ + wxFH_HIDE_CURRENT_PATH = 0, + wxFH_HIDE_ALL_PATHS = 1, + wxFH_SHOW_FULL_PATH = 2 +}; + // ---------------------------------------------------------------------------- // File history management // ---------------------------------------------------------------------------- @@ -60,15 +67,23 @@ public: void SetBaseId(wxWindowID baseId) { m_idBase = baseId; } wxWindowID GetBaseId() const { return m_idBase; } + void SetMenuLabelStyle(wxFileHistoryMenuLabelStyle style) { m_menuLabelStyle = style; } + wxFileHistoryMenuLabelStyle GetMenuLabelStyle() const { return m_menuLabelStyle; } + + void RefreshLabels(); + protected: // Last n files - wxArrayString m_fileHistory; + wxArrayString m_fileHistory; // Menus to maintain (may need several for an MDI app) - wxList m_fileMenus; + wxList m_fileMenus; // Max files to maintain - size_t m_fileMaxFiles; + size_t m_fileMaxFiles; + + // Style of the labels in the menu + wxFileHistoryMenuLabelStyle m_menuLabelStyle; private: // The ID of the first history menu item (Doesn't have to be wxID_FILE1) diff --git a/interface/wx/filehistory.h b/interface/wx/filehistory.h index 992a0a3174..29c68fafe8 100644 --- a/interface/wx/filehistory.h +++ b/interface/wx/filehistory.h @@ -5,6 +5,23 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// +/** + Styles for the labels on menu items for wxFileHistory menus + + @since 3.1.5 +*/ +enum wxFileHistoryMenuLabelStyle +{ + /** Hide the file path if it matches the path of the first item */ + wxFH_HIDE_CURRENT_PATH, + + /** Hide all paths and show only filenames */ + wxFH_HIDE_ALL_PATHS, + + /** Show the full path for all files */ + wxFH_SHOW_FULL_PATH +}; + /** @class wxFileHistory @@ -15,6 +32,8 @@ required in an MDI application, where the file history should appear on each MDI child menu as well as the MDI parent frame. + By default, the menu item label style will be @c wxFH_HIDE_CURRENT_PATH. + @library{wxcore} @category{docview} @@ -90,6 +109,14 @@ public: */ virtual void Load(const wxConfigBase& config); + /** + Refresh the labels on all the menu items in the menus used by this + file history. + + @since 3.1.5 + */ + void RefreshLabels(); + /** Removes the specified file from the history. */ @@ -120,4 +147,20 @@ public: called, as this is not done automatically. */ virtual void UseMenu(wxMenu* menu); + + /** + Set the style of the menu item labels. + + @remarks Use RefreshLabels() to update any existing menu items to the new style. + @since 3.1.5 + */ + void SetMenuLabelStyle(wxFileHistoryMenuLabelStyle style); + + /** + Get the current style of the menu item labels. + @since 3.1.5 + */ + wxFileHistoryMenuLabelStyle GetMenuLabelStyle() const; + + }; diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index 31b70b1d8f..a32e5a3693 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -161,6 +161,8 @@ protected: #if wxUSE_FILE_HISTORY void OnFileHistoryMenuItem(wxCommandEvent& event); + + void OnFileHistoryStyleItem(wxCommandEvent& event); #endif private: @@ -297,6 +299,11 @@ enum #if wxUSE_TEXTDLG Menu_Menu_FindItem, #endif +#if wxUSE_FILE_HISTORY + Menu_Menu_FileHistory1, + Menu_Menu_FileHistory2, + Menu_Menu_FileHistory3, +#endif Menu_Test_Normal = 400, Menu_Test_Check, @@ -380,6 +387,10 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(wxID_FILE1, MyFrame::OnFileHistoryMenuItem) EVT_MENU(wxID_FILE2, MyFrame::OnFileHistoryMenuItem) EVT_MENU(wxID_FILE3, MyFrame::OnFileHistoryMenuItem) + + EVT_MENU(Menu_Menu_FileHistory1, MyFrame::OnFileHistoryStyleItem) + EVT_MENU(Menu_Menu_FileHistory2, MyFrame::OnFileHistoryStyleItem) + EVT_MENU(Menu_Menu_FileHistory3, MyFrame::OnFileHistoryStyleItem) #endif EVT_UPDATE_UI(Menu_SubMenu_Normal, MyFrame::OnUpdateSubMenuNormal) @@ -647,6 +658,16 @@ MyFrame::MyFrame() menuMenu->Append(Menu_Menu_FindItem, "Find menu item from label", "Find a menu item by searching for its label"); #endif +#if wxUSE_FILE_HISTORY + wxMenu* menuFileHistoryStyle = new wxMenu(); + + menuFileHistoryStyle->AppendRadioItem(Menu_Menu_FileHistory1, "Hide current path"); + menuFileHistoryStyle->AppendRadioItem(Menu_Menu_FileHistory2, "Hide all paths"); + menuFileHistoryStyle->AppendRadioItem(Menu_Menu_FileHistory3, "Show all paths"); + + menuMenu->AppendSeparator(); + menuMenu->AppendSubMenu(menuFileHistoryStyle, "Select file history menu style"); +#endif wxMenu *testMenu = new wxMenu; testMenu->Append(Menu_Test_Normal, "&Normal item"); @@ -1367,6 +1388,24 @@ void MyFrame::OnFileHistoryMenuItem(wxCommandEvent& event) m_fileHistory->AddFileToHistory(fname); } + +void MyFrame::OnFileHistoryStyleItem(wxCommandEvent& event) +{ + switch( event.GetId() ) + { + case Menu_Menu_FileHistory1: + m_fileHistory->SetMenuLabelStyle(wxFH_HIDE_CURRENT_PATH); + break; + case Menu_Menu_FileHistory2: + m_fileHistory->SetMenuLabelStyle(wxFH_HIDE_ALL_PATHS); + break; + case Menu_Menu_FileHistory3: + m_fileHistory->SetMenuLabelStyle(wxFH_SHOW_FULL_PATH); + break; + } + + m_fileHistory->RefreshLabels(); +} #endif // ---------------------------------------------------------------------------- diff --git a/src/common/filehistorycmn.cpp b/src/common/filehistorycmn.cpp index 929327768d..b77bdf0a61 100644 --- a/src/common/filehistorycmn.cpp +++ b/src/common/filehistorycmn.cpp @@ -70,6 +70,7 @@ wxFileHistoryBase::wxFileHistoryBase(size_t maxFiles, wxWindowID idBase) { m_fileMaxFiles = maxFiles; m_idBase = idBase; + m_menuLabelStyle = wxFH_HIDE_CURRENT_PATH; } /* static */ @@ -135,20 +136,43 @@ void wxFileHistoryBase::AddFileToHistory(const wxString& file) m_fileHistory.insert(m_fileHistory.begin(), file); numFiles++; - // update the labels in all menus + RefreshLabels(); +} + +void wxFileHistoryBase::RefreshLabels() +{ + size_t i; + size_t numFiles = m_fileHistory.size(); + + // If no files, then no need to refresh the menu + if ( numFiles == 0 ) + return; + + // Use the first file as the current path + wxFileName firstFn(m_fileHistory[0]); + + // Update the labels in all menus for ( i = 0; i < numFiles; i++ ) { - // if in same directory just show the filename; otherwise the full path - const wxFileName fnOld(m_fileHistory[i]); + const wxFileName currFn(m_fileHistory[i]); wxString pathInMenu; - if ( (fnOld.GetPath() == fnNew.GetPath()) && fnOld.HasName() ) + + if ( m_menuLabelStyle == wxFH_HIDE_ALL_PATHS ) { - pathInMenu = fnOld.GetFullName(); + // Only show the filename + extension and not the path + pathInMenu = currFn.GetFullName(); } - else // file in different directory or it's not a file but a directory + else if ( ( m_menuLabelStyle == wxFH_HIDE_CURRENT_PATH ) && + ( currFn.GetPath() == firstFn.GetPath() ) && currFn.HasName() ) { - // absolute path; could also set relative path + // Hide the path if it is in the same folder as the first file + pathInMenu = currFn.GetFullName(); + } + else + { + // Either has wxFH_SHOW_FULL_PATH menu style, or the file is in a different directory + // from the first file pathInMenu = m_fileHistory[i]; }