Add different menu label styles to wxFileHistory menus

These styles allow the program to choose to display file paths
in the menu or not
This commit is contained in:
Ian McInerney
2021-03-18 02:11:33 +00:00
parent 05685b5faa
commit 71c26ec4da
4 changed files with 131 additions and 10 deletions

View File

@@ -24,6 +24,13 @@ class WXDLLIMPEXP_FWD_CORE wxMenu;
class WXDLLIMPEXP_FWD_BASE wxConfigBase; class WXDLLIMPEXP_FWD_BASE wxConfigBase;
class WXDLLIMPEXP_FWD_BASE wxFileName; 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 // File history management
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -60,6 +67,11 @@ public:
void SetBaseId(wxWindowID baseId) { m_idBase = baseId; } void SetBaseId(wxWindowID baseId) { m_idBase = baseId; }
wxWindowID GetBaseId() const { return m_idBase; } wxWindowID GetBaseId() const { return m_idBase; }
void SetMenuLabelStyle(wxFileHistoryMenuLabelStyle style) { m_menuLabelStyle = style; }
wxFileHistoryMenuLabelStyle GetMenuLabelStyle() const { return m_menuLabelStyle; }
void RefreshLabels();
protected: protected:
// Last n files // Last n files
wxArrayString m_fileHistory; wxArrayString m_fileHistory;
@@ -70,6 +82,9 @@ protected:
// Max files to maintain // Max files to maintain
size_t m_fileMaxFiles; size_t m_fileMaxFiles;
// Style of the labels in the menu
wxFileHistoryMenuLabelStyle m_menuLabelStyle;
private: private:
// The ID of the first history menu item (Doesn't have to be wxID_FILE1) // The ID of the first history menu item (Doesn't have to be wxID_FILE1)
wxWindowID m_idBase; wxWindowID m_idBase;

View File

@@ -5,6 +5,23 @@
// Licence: wxWindows licence // 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 @class wxFileHistory
@@ -15,6 +32,8 @@
required in an MDI application, where the file history should appear on required in an MDI application, where the file history should appear on
each MDI child menu as well as the MDI parent frame. 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} @library{wxcore}
@category{docview} @category{docview}
@@ -90,6 +109,14 @@ public:
*/ */
virtual void Load(const wxConfigBase& config); 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. Removes the specified file from the history.
*/ */
@@ -120,4 +147,20 @@ public:
called, as this is not done automatically. called, as this is not done automatically.
*/ */
virtual void UseMenu(wxMenu* menu); 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;
}; };

View File

@@ -161,6 +161,8 @@ protected:
#if wxUSE_FILE_HISTORY #if wxUSE_FILE_HISTORY
void OnFileHistoryMenuItem(wxCommandEvent& event); void OnFileHistoryMenuItem(wxCommandEvent& event);
void OnFileHistoryStyleItem(wxCommandEvent& event);
#endif #endif
private: private:
@@ -297,6 +299,11 @@ enum
#if wxUSE_TEXTDLG #if wxUSE_TEXTDLG
Menu_Menu_FindItem, Menu_Menu_FindItem,
#endif #endif
#if wxUSE_FILE_HISTORY
Menu_Menu_FileHistory1,
Menu_Menu_FileHistory2,
Menu_Menu_FileHistory3,
#endif
Menu_Test_Normal = 400, Menu_Test_Normal = 400,
Menu_Test_Check, Menu_Test_Check,
@@ -380,6 +387,10 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_FILE1, MyFrame::OnFileHistoryMenuItem) EVT_MENU(wxID_FILE1, MyFrame::OnFileHistoryMenuItem)
EVT_MENU(wxID_FILE2, MyFrame::OnFileHistoryMenuItem) EVT_MENU(wxID_FILE2, MyFrame::OnFileHistoryMenuItem)
EVT_MENU(wxID_FILE3, 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 #endif
EVT_UPDATE_UI(Menu_SubMenu_Normal, MyFrame::OnUpdateSubMenuNormal) EVT_UPDATE_UI(Menu_SubMenu_Normal, MyFrame::OnUpdateSubMenuNormal)
@@ -647,6 +658,16 @@ MyFrame::MyFrame()
menuMenu->Append(Menu_Menu_FindItem, "Find menu item from label", menuMenu->Append(Menu_Menu_FindItem, "Find menu item from label",
"Find a menu item by searching for its label"); "Find a menu item by searching for its label");
#endif #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; wxMenu *testMenu = new wxMenu;
testMenu->Append(Menu_Test_Normal, "&Normal item"); testMenu->Append(Menu_Test_Normal, "&Normal item");
@@ -1367,6 +1388,24 @@ void MyFrame::OnFileHistoryMenuItem(wxCommandEvent& event)
m_fileHistory->AddFileToHistory(fname); 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 #endif
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -70,6 +70,7 @@ wxFileHistoryBase::wxFileHistoryBase(size_t maxFiles, wxWindowID idBase)
{ {
m_fileMaxFiles = maxFiles; m_fileMaxFiles = maxFiles;
m_idBase = idBase; m_idBase = idBase;
m_menuLabelStyle = wxFH_HIDE_CURRENT_PATH;
} }
/* static */ /* static */
@@ -135,20 +136,43 @@ void wxFileHistoryBase::AddFileToHistory(const wxString& file)
m_fileHistory.insert(m_fileHistory.begin(), file); m_fileHistory.insert(m_fileHistory.begin(), file);
numFiles++; 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++ ) for ( i = 0; i < numFiles; i++ )
{ {
// if in same directory just show the filename; otherwise the full path const wxFileName currFn(m_fileHistory[i]);
const wxFileName fnOld(m_fileHistory[i]);
wxString pathInMenu; 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]; pathInMenu = m_fileHistory[i];
} }