From 05685b5faa76c4a3b3cc28f22499e6911e3d8a60 Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Thu, 18 Mar 2021 01:11:14 +0000 Subject: [PATCH 1/8] Add a file history menu to the menu sample --- samples/menu/menu.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index 5189ec0161..31b70b1d8f 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -23,6 +23,8 @@ #ifndef WX_PRECOMP #include "wx/app.h" #include "wx/bitmap.h" + #include "wx/filehistory.h" + #include "wx/filename.h" #include "wx/frame.h" #include "wx/image.h" #include "wx/menu.h" @@ -157,6 +159,10 @@ protected: void OnSize(wxSizeEvent& event); +#if wxUSE_FILE_HISTORY + void OnFileHistoryMenuItem(wxCommandEvent& event); +#endif + private: #if USE_LOG_WINDOW void LogMenuOpenCloseOrHighlight(const wxMenuEvent& event, const wxString& what); @@ -178,6 +184,11 @@ private: wxTextCtrl *m_textctrl; #endif +#if wxUSE_FILE_HISTORY + wxMenu* m_fileHistoryMenu; + wxFileHistory* m_fileHistory; +#endif + // the previous log target wxLog *m_logOld; @@ -365,6 +376,12 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Menu_Test_Radio2, MyFrame::OnTestRadio) EVT_MENU(Menu_Test_Radio3, MyFrame::OnTestRadio) +#if wxUSE_FILE_HISTORY + EVT_MENU(wxID_FILE1, MyFrame::OnFileHistoryMenuItem) + EVT_MENU(wxID_FILE2, MyFrame::OnFileHistoryMenuItem) + EVT_MENU(wxID_FILE3, MyFrame::OnFileHistoryMenuItem) +#endif + EVT_UPDATE_UI(Menu_SubMenu_Normal, MyFrame::OnUpdateSubMenuNormal) EVT_UPDATE_UI(Menu_SubMenu_Check, MyFrame::OnUpdateSubMenuCheck) EVT_UPDATE_UI(Menu_SubMenu_Radio1, MyFrame::OnUpdateSubMenuRadio) @@ -539,6 +556,27 @@ MyFrame::MyFrame() "Show a dialog"); fileMenu->AppendSeparator(); +#if wxUSE_FILE_HISTORY + m_fileHistoryMenu = new wxMenu(); + + m_fileHistory = new wxFileHistory(); + m_fileHistory->UseMenu(m_fileHistoryMenu); + + wxFileName fn( "menu.cpp" ); + fn.Normalize(); + m_fileHistory->AddFileToHistory( fn.GetFullPath() ); + + fn = "Makefile.in"; + fn.Normalize(); + m_fileHistory->AddFileToHistory( fn.GetFullPath() ); + + fn.Assign("minimal", "minimal", "cpp"); + fn.Normalize(); + m_fileHistory->AddFileToHistory( fn.GetFullPath() ); + + fileMenu->AppendSubMenu(m_fileHistoryMenu, "Sample file history"); +#endif + fileMenu->Append(Menu_File_Quit, "E&xit\tAlt-X", "Quit menu sample"); wxMenu *menubarMenu = new wxMenu; @@ -1317,6 +1355,20 @@ void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event)) #endif // __WXUNIVERSAL__ } +#if wxUSE_FILE_HISTORY +void MyFrame::OnFileHistoryMenuItem(wxCommandEvent& event) +{ + int eventID = event.GetId(); + + wxString fname = m_fileHistory->GetHistoryFile(eventID - wxID_FILE1); + + wxMessageBox(wxString::Format("Selected file %s", fname), "File history activated", + wxOK | wxICON_INFORMATION); + + m_fileHistory->AddFileToHistory(fname); +} +#endif + // ---------------------------------------------------------------------------- // MyDialog // ---------------------------------------------------------------------------- From 71c26ec4da9c9773d641542a4140533f1a6727c0 Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Thu, 18 Mar 2021 02:11:33 +0000 Subject: [PATCH 2/8] Add different menu label styles to wxFileHistory menus These styles allow the program to choose to display file paths in the menu or not --- include/wx/filehistory.h | 21 ++++++++++++++--- interface/wx/filehistory.h | 43 +++++++++++++++++++++++++++++++++++ samples/menu/menu.cpp | 39 +++++++++++++++++++++++++++++++ src/common/filehistorycmn.cpp | 38 +++++++++++++++++++++++++------ 4 files changed, 131 insertions(+), 10 deletions(-) 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]; } From 036e35bf2887f4b3038eb23b4ffd0c25da09ad38 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 5 Apr 2021 16:09:34 +0200 Subject: [PATCH 3/8] Rename wxFileHistoryMenuLabelStyle to wxFileHistoryMenuPathStyle It seems useful to have the word "Path" in the name of this enum to indicate that it applies to the paths shown in the menu labels. Also rename the methods using this enum. --- include/wx/filehistory.h | 10 +++++----- interface/wx/filehistory.h | 22 +++++++++++++++------- samples/menu/menu.cpp | 6 +++--- src/common/filehistorycmn.cpp | 6 +++--- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/include/wx/filehistory.h b/include/wx/filehistory.h index f7f0183ad3..ce2a031f36 100644 --- a/include/wx/filehistory.h +++ b/include/wx/filehistory.h @@ -24,7 +24,7 @@ class WXDLLIMPEXP_FWD_CORE wxMenu; class WXDLLIMPEXP_FWD_BASE wxConfigBase; class WXDLLIMPEXP_FWD_BASE wxFileName; -enum wxFileHistoryMenuLabelStyle +enum wxFileHistoryMenuPathStyle { wxFH_HIDE_CURRENT_PATH = 0, wxFH_HIDE_ALL_PATHS = 1, @@ -67,8 +67,8 @@ 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 SetMenuPathStyle(wxFileHistoryMenuPathStyle style) { m_menuPathStyle = style; } + wxFileHistoryMenuPathStyle GetMenuPathStyle() const { return m_menuPathStyle; } void RefreshLabels(); @@ -82,8 +82,8 @@ protected: // Max files to maintain size_t m_fileMaxFiles; - // Style of the labels in the menu - wxFileHistoryMenuLabelStyle m_menuLabelStyle; + // Style of the paths in the menu labels + wxFileHistoryMenuPathStyle m_menuPathStyle; 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 29c68fafe8..445dc16c35 100644 --- a/interface/wx/filehistory.h +++ b/interface/wx/filehistory.h @@ -6,11 +6,14 @@ ///////////////////////////////////////////////////////////////////////////// /** - Styles for the labels on menu items for wxFileHistory menus + Styles for the paths shown in wxFileHistory menus. - @since 3.1.5 + The default style is wxFH_HIDE_CURRENT_PATH, i.e. the path of the file is + only shown in the menu if it's different from the path of the first file. + + @since 3.1.5 */ -enum wxFileHistoryMenuLabelStyle +enum wxFileHistoryMenuPathStyle { /** Hide the file path if it matches the path of the first item */ wxFH_HIDE_CURRENT_PATH, @@ -32,8 +35,6 @@ enum wxFileHistoryMenuLabelStyle 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} @@ -151,16 +152,23 @@ public: /** Set the style of the menu item labels. + By default, the menu item label style is ::wxFH_HIDE_CURRENT_PATH. + @remarks Use RefreshLabels() to update any existing menu items to the new style. @since 3.1.5 */ - void SetMenuLabelStyle(wxFileHistoryMenuLabelStyle style); + void SetMenuPathStyle(wxFileHistoryMenuPathStyle style); /** Get the current style of the menu item labels. + + Initially returns ::wxFH_HIDE_CURRENT_PATH. + + @see SetMenuPathStyle() + @since 3.1.5 */ - wxFileHistoryMenuLabelStyle GetMenuLabelStyle() const; + wxFileHistoryMenuPathStyle GetMenuPathStyle() const; }; diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index a32e5a3693..271bba1e3f 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -1394,13 +1394,13 @@ void MyFrame::OnFileHistoryStyleItem(wxCommandEvent& event) switch( event.GetId() ) { case Menu_Menu_FileHistory1: - m_fileHistory->SetMenuLabelStyle(wxFH_HIDE_CURRENT_PATH); + m_fileHistory->SetMenuPathStyle(wxFH_HIDE_CURRENT_PATH); break; case Menu_Menu_FileHistory2: - m_fileHistory->SetMenuLabelStyle(wxFH_HIDE_ALL_PATHS); + m_fileHistory->SetMenuPathStyle(wxFH_HIDE_ALL_PATHS); break; case Menu_Menu_FileHistory3: - m_fileHistory->SetMenuLabelStyle(wxFH_SHOW_FULL_PATH); + m_fileHistory->SetMenuPathStyle(wxFH_SHOW_FULL_PATH); break; } diff --git a/src/common/filehistorycmn.cpp b/src/common/filehistorycmn.cpp index b77bdf0a61..0fa0c8d303 100644 --- a/src/common/filehistorycmn.cpp +++ b/src/common/filehistorycmn.cpp @@ -70,7 +70,7 @@ wxFileHistoryBase::wxFileHistoryBase(size_t maxFiles, wxWindowID idBase) { m_fileMaxFiles = maxFiles; m_idBase = idBase; - m_menuLabelStyle = wxFH_HIDE_CURRENT_PATH; + m_menuPathStyle = wxFH_HIDE_CURRENT_PATH; } /* static */ @@ -158,12 +158,12 @@ void wxFileHistoryBase::RefreshLabels() wxString pathInMenu; - if ( m_menuLabelStyle == wxFH_HIDE_ALL_PATHS ) + if ( m_menuPathStyle == wxFH_HIDE_ALL_PATHS ) { // Only show the filename + extension and not the path pathInMenu = currFn.GetFullName(); } - else if ( ( m_menuLabelStyle == wxFH_HIDE_CURRENT_PATH ) && + else if ( ( m_menuPathStyle == wxFH_HIDE_CURRENT_PATH ) && ( currFn.GetPath() == firstFn.GetPath() ) && currFn.HasName() ) { // Hide the path if it is in the same folder as the first file From c89921d26d7401d058865c5ecb55f9f779c90667 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 5 Apr 2021 16:15:49 +0200 Subject: [PATCH 4/8] Rename wxFileHistoryMenuPathStyle values too Use common wxFH_PATH_ prefix for consistency and try to make the values more clear. --- include/wx/filehistory.h | 6 +++--- interface/wx/filehistory.h | 35 +++++++++++++++++++++++++---------- samples/menu/menu.cpp | 6 +++--- src/common/filehistorycmn.cpp | 8 ++++---- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/include/wx/filehistory.h b/include/wx/filehistory.h index ce2a031f36..e5bf059ea7 100644 --- a/include/wx/filehistory.h +++ b/include/wx/filehistory.h @@ -26,9 +26,9 @@ class WXDLLIMPEXP_FWD_BASE wxFileName; enum wxFileHistoryMenuPathStyle { - wxFH_HIDE_CURRENT_PATH = 0, - wxFH_HIDE_ALL_PATHS = 1, - wxFH_SHOW_FULL_PATH = 2 + wxFH_PATH_SHOW_IF_DIFFERENT, + wxFH_PATH_SHOW_NEVER, + wxFH_PATH_SHOW_ALWAYS }; // ---------------------------------------------------------------------------- diff --git a/interface/wx/filehistory.h b/interface/wx/filehistory.h index 445dc16c35..7cc92718b8 100644 --- a/interface/wx/filehistory.h +++ b/interface/wx/filehistory.h @@ -8,21 +8,36 @@ /** Styles for the paths shown in wxFileHistory menus. - The default style is wxFH_HIDE_CURRENT_PATH, i.e. the path of the file is - only shown in the menu if it's different from the path of the first file. + The values of this enum determine whether the labels in the menu managed by + wxFileHistory show the full path for the corresponding file or just the + base name. + + The default style is wxFH_PATH_SHOW_IF_DIFFERENT, i.e. the full path of the + file is only shown in the menu if it's different from the path of the first + file. @since 3.1.5 */ enum wxFileHistoryMenuPathStyle { - /** Hide the file path if it matches the path of the first item */ - wxFH_HIDE_CURRENT_PATH, + /** + Show the full path if it's different from the path of the first file. - /** Hide all paths and show only filenames */ - wxFH_HIDE_ALL_PATHS, + Otherwise show just the file name. - /** Show the full path for all files */ - wxFH_SHOW_FULL_PATH + This value corresponds to the default behaviour. + */ + wxFH_PATH_SHOW_IF_DIFFERENT, + + /** + Never show full path, always show just the base file name. + */ + wxFH_PATH_SHOW_NEVER, + + /** + Always show the full path for all files. + */ + wxFH_PATH_SHOW_ALWAYS }; /** @@ -152,7 +167,7 @@ public: /** Set the style of the menu item labels. - By default, the menu item label style is ::wxFH_HIDE_CURRENT_PATH. + By default, the menu item label style is ::wxFH_PATH_SHOW_IF_DIFFERENT. @remarks Use RefreshLabels() to update any existing menu items to the new style. @since 3.1.5 @@ -162,7 +177,7 @@ public: /** Get the current style of the menu item labels. - Initially returns ::wxFH_HIDE_CURRENT_PATH. + Initially returns ::wxFH_PATH_SHOW_IF_DIFFERENT. @see SetMenuPathStyle() diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index 271bba1e3f..6113f8de77 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -1394,13 +1394,13 @@ void MyFrame::OnFileHistoryStyleItem(wxCommandEvent& event) switch( event.GetId() ) { case Menu_Menu_FileHistory1: - m_fileHistory->SetMenuPathStyle(wxFH_HIDE_CURRENT_PATH); + m_fileHistory->SetMenuPathStyle(wxFH_PATH_SHOW_IF_DIFFERENT); break; case Menu_Menu_FileHistory2: - m_fileHistory->SetMenuPathStyle(wxFH_HIDE_ALL_PATHS); + m_fileHistory->SetMenuPathStyle(wxFH_PATH_SHOW_NEVER); break; case Menu_Menu_FileHistory3: - m_fileHistory->SetMenuPathStyle(wxFH_SHOW_FULL_PATH); + m_fileHistory->SetMenuPathStyle(wxFH_PATH_SHOW_ALWAYS); break; } diff --git a/src/common/filehistorycmn.cpp b/src/common/filehistorycmn.cpp index 0fa0c8d303..d49fb2b39b 100644 --- a/src/common/filehistorycmn.cpp +++ b/src/common/filehistorycmn.cpp @@ -70,7 +70,7 @@ wxFileHistoryBase::wxFileHistoryBase(size_t maxFiles, wxWindowID idBase) { m_fileMaxFiles = maxFiles; m_idBase = idBase; - m_menuPathStyle = wxFH_HIDE_CURRENT_PATH; + m_menuPathStyle = wxFH_PATH_SHOW_IF_DIFFERENT; } /* static */ @@ -158,12 +158,12 @@ void wxFileHistoryBase::RefreshLabels() wxString pathInMenu; - if ( m_menuPathStyle == wxFH_HIDE_ALL_PATHS ) + if ( m_menuPathStyle == wxFH_PATH_SHOW_NEVER ) { // Only show the filename + extension and not the path pathInMenu = currFn.GetFullName(); } - else if ( ( m_menuPathStyle == wxFH_HIDE_CURRENT_PATH ) && + else if ( ( m_menuPathStyle == wxFH_PATH_SHOW_IF_DIFFERENT ) && ( currFn.GetPath() == firstFn.GetPath() ) && currFn.HasName() ) { // Hide the path if it is in the same folder as the first file @@ -171,7 +171,7 @@ void wxFileHistoryBase::RefreshLabels() } else { - // Either has wxFH_SHOW_FULL_PATH menu style, or the file is in a different directory + // Either has wxFH_PATH_SHOW_ALWAYS menu style, or the file is in a different directory // from the first file pathInMenu = m_fileHistory[i]; } From f8f0b8b50ca9803398eff2582e2bd170cdc1e419 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 5 Apr 2021 16:18:41 +0200 Subject: [PATCH 5/8] Remove public wxFileHistory::RefreshLabels() Replace it with a private DoRefreshLabels() and call it ourselves from SetMenuPathStyle() to make the class simpler (and less error-prone, as it's now impossible to forget to call RefreshLabels() any more) to use. --- include/wx/filehistory.h | 7 ++++--- interface/wx/filehistory.h | 9 --------- samples/menu/menu.cpp | 2 -- src/common/filehistorycmn.cpp | 13 +++++++++++-- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/include/wx/filehistory.h b/include/wx/filehistory.h index e5bf059ea7..a5756a7e09 100644 --- a/include/wx/filehistory.h +++ b/include/wx/filehistory.h @@ -67,11 +67,9 @@ public: void SetBaseId(wxWindowID baseId) { m_idBase = baseId; } wxWindowID GetBaseId() const { return m_idBase; } - void SetMenuPathStyle(wxFileHistoryMenuPathStyle style) { m_menuPathStyle = style; } + void SetMenuPathStyle(wxFileHistoryMenuPathStyle style); wxFileHistoryMenuPathStyle GetMenuPathStyle() const { return m_menuPathStyle; } - void RefreshLabels(); - protected: // Last n files wxArrayString m_fileHistory; @@ -86,6 +84,9 @@ protected: wxFileHistoryMenuPathStyle m_menuPathStyle; private: + void DoRefreshLabels(); + + // The ID of the first history menu item (Doesn't have to be wxID_FILE1) wxWindowID m_idBase; diff --git a/interface/wx/filehistory.h b/interface/wx/filehistory.h index 7cc92718b8..21d38b686b 100644 --- a/interface/wx/filehistory.h +++ b/interface/wx/filehistory.h @@ -125,14 +125,6 @@ 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. */ @@ -169,7 +161,6 @@ public: By default, the menu item label style is ::wxFH_PATH_SHOW_IF_DIFFERENT. - @remarks Use RefreshLabels() to update any existing menu items to the new style. @since 3.1.5 */ void SetMenuPathStyle(wxFileHistoryMenuPathStyle style); diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index 6113f8de77..53f3c0deb5 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -1403,8 +1403,6 @@ void MyFrame::OnFileHistoryStyleItem(wxCommandEvent& event) m_fileHistory->SetMenuPathStyle(wxFH_PATH_SHOW_ALWAYS); break; } - - m_fileHistory->RefreshLabels(); } #endif diff --git a/src/common/filehistorycmn.cpp b/src/common/filehistorycmn.cpp index d49fb2b39b..a0cf56efd1 100644 --- a/src/common/filehistorycmn.cpp +++ b/src/common/filehistorycmn.cpp @@ -136,10 +136,10 @@ void wxFileHistoryBase::AddFileToHistory(const wxString& file) m_fileHistory.insert(m_fileHistory.begin(), file); numFiles++; - RefreshLabels(); + DoRefreshLabels(); } -void wxFileHistoryBase::RefreshLabels() +void wxFileHistoryBase::DoRefreshLabels() { size_t i; size_t numFiles = m_fileHistory.size(); @@ -187,6 +187,15 @@ void wxFileHistoryBase::RefreshLabels() } } +void wxFileHistoryBase::SetMenuPathStyle(wxFileHistoryMenuPathStyle style) +{ + if ( style != m_menuPathStyle ) + { + m_menuPathStyle = style; + DoRefreshLabels(); + } +} + void wxFileHistoryBase::RemoveFileFromHistory(size_t i) { size_t numFiles = m_fileHistory.size(); From d46ba0b43562f003ede62e807a6a7bae29d38127 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 5 Apr 2021 16:21:19 +0200 Subject: [PATCH 6/8] Very minor code cleanup in AddFileToHistory() Declare "i" inside the loop and make a variable that could be const actually const. No real changes. --- src/common/filehistorycmn.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/common/filehistorycmn.cpp b/src/common/filehistorycmn.cpp index a0cf56efd1..732a95a372 100644 --- a/src/common/filehistorycmn.cpp +++ b/src/common/filehistorycmn.cpp @@ -141,8 +141,7 @@ void wxFileHistoryBase::AddFileToHistory(const wxString& file) void wxFileHistoryBase::DoRefreshLabels() { - size_t i; - size_t numFiles = m_fileHistory.size(); + const size_t numFiles = m_fileHistory.size(); // If no files, then no need to refresh the menu if ( numFiles == 0 ) @@ -152,7 +151,7 @@ void wxFileHistoryBase::DoRefreshLabels() wxFileName firstFn(m_fileHistory[0]); // Update the labels in all menus - for ( i = 0; i < numFiles; i++ ) + for ( size_t i = 0; i < numFiles; i++ ) { const wxFileName currFn(m_fileHistory[i]); From 979c1bccc9ece646f152faa41b3c8fb502a2f29d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 5 Apr 2021 16:30:51 +0200 Subject: [PATCH 7/8] Make DoRefreshLabels() code more obviously correct Use switch over enum instead of a series of chained ifs. This has the same effect, but can be read and understood more easily. No real changes. --- src/common/filehistorycmn.cpp | 37 ++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/common/filehistorycmn.cpp b/src/common/filehistorycmn.cpp index 732a95a372..0c0070c938 100644 --- a/src/common/filehistorycmn.cpp +++ b/src/common/filehistorycmn.cpp @@ -147,8 +147,8 @@ void wxFileHistoryBase::DoRefreshLabels() if ( numFiles == 0 ) return; - // Use the first file as the current path - wxFileName firstFn(m_fileHistory[0]); + // Remember the path in case we need to compare with it below. + const wxString firstPath(wxFileName(m_fileHistory[0]).GetPath()); // Update the labels in all menus for ( size_t i = 0; i < numFiles; i++ ) @@ -156,23 +156,24 @@ void wxFileHistoryBase::DoRefreshLabels() const wxFileName currFn(m_fileHistory[i]); wxString pathInMenu; + switch ( m_menuPathStyle ) + { + case wxFH_PATH_SHOW_IF_DIFFERENT: + if ( currFn.HasName() && currFn.GetPath() == firstPath ) + pathInMenu = currFn.GetFullName(); + else + pathInMenu = currFn.GetFullPath(); + break; - if ( m_menuPathStyle == wxFH_PATH_SHOW_NEVER ) - { - // Only show the filename + extension and not the path - pathInMenu = currFn.GetFullName(); - } - else if ( ( m_menuPathStyle == wxFH_PATH_SHOW_IF_DIFFERENT ) && - ( currFn.GetPath() == firstFn.GetPath() ) && currFn.HasName() ) - { - // Hide the path if it is in the same folder as the first file - pathInMenu = currFn.GetFullName(); - } - else - { - // Either has wxFH_PATH_SHOW_ALWAYS menu style, or the file is in a different directory - // from the first file - pathInMenu = m_fileHistory[i]; + case wxFH_PATH_SHOW_NEVER: + // Only show the filename + extension and not the path. + pathInMenu = currFn.GetFullName(); + break; + + case wxFH_PATH_SHOW_ALWAYS: + // Always show full path. + pathInMenu = currFn.GetFullPath(); + break; } for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst(); From a77f390ae35bd6e70200a9cfecf88c2124ab44a6 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 5 Apr 2021 16:32:34 +0200 Subject: [PATCH 8/8] Fix wxFileHistory memory leak in the sample We need to delete the object we allocate. --- samples/menu/menu.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index 53f3c0deb5..f186b34966 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -748,6 +748,7 @@ MyFrame::MyFrame() MyFrame::~MyFrame() { + delete m_fileHistory; delete m_menu; // delete the event handler installed in ctor