Audacity  2.2.2
FileHistory.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  FileHistory.cpp
6 
7  Leland Lucius
8 
9 *******************************************************************//*******************************************************************/
15 
16 #include "../Audacity.h"
17 
18 #include <wx/defs.h>
19 #include <wx/fileconf.h>
20 #include <wx/menu.h>
21 
22 #include "FileHistory.h"
23 #include "../Internat.h"
24 
25 FileHistory::FileHistory(size_t maxfiles, wxWindowID base)
26 {
27  mMaxFiles = maxfiles;
28  mIDBase = base;
29 }
30 
32 {
33 }
34 
35 // File history management
36 void FileHistory::AddFileToHistory(const wxString & file, bool update)
37 {
38  // Needed to transition from wxFileHistory to FileHistory since there
39  // can be empty history "slots".
40  if (file.IsEmpty()) {
41  return;
42  }
43 
44 #if defined(__WXMSW__)
45  int i = mHistory.Index(file, false);
46 #else
47  int i = mHistory.Index(file, true);
48 #endif
49 
50  if (i != wxNOT_FOUND) {
51  mHistory.RemoveAt(i);
52  }
53 
54  if (mMaxFiles == mHistory.GetCount()) {
55  mHistory.RemoveAt(mHistory.GetCount() - 1);
56  }
57 
58  mHistory.Insert(file, 0);
59 
60  if (update) {
62  }
63 }
64 
65 void FileHistory::RemoveFileFromHistory(size_t i, bool update)
66 {
67  wxASSERT(i < mHistory.GetCount());
68 
69  if (i < mHistory.GetCount()) {
70  mHistory.RemoveAt(i);
71 
72  if (update) {
74  }
75  }
76 }
77 
79 {
80  mHistory.Clear();
81 
83 }
84 
85 const wxString &FileHistory::GetHistoryFile(size_t i) const
86 {
87  wxASSERT(i < mHistory.GetCount());
88 
89  if (i < mHistory.GetCount()) {
90  return mHistory[i];
91  }
92 
93  static const wxString empty;
94  return empty;
95 }
96 
98 {
99  return mHistory.GetCount();
100 }
101 
102 void FileHistory::UseMenu(wxMenu *menu)
103 {
104  auto end = mMenus.end();
105  auto iter = std::find(mMenus.begin(), end, menu);
106  auto found = (iter != end);
107 
108  if (!found)
109  mMenus.push_back(menu);
110  else {
111  wxASSERT(false);
112  }
113 }
114 
115 void FileHistory::RemoveMenu(wxMenu *menu)
116 {
117  auto end = mMenus.end();
118  auto iter = std::find(mMenus.begin(), end, menu);
119  auto found = (iter != end);
120 
121  if (found)
122  mMenus.erase(iter);
123  else {
124  wxASSERT(false);
125  }
126 }
127 
128 void FileHistory::Load(wxConfigBase & config, const wxString & group)
129 {
130  mHistory.Clear();
131 
132  config.SetPath(group);
133 
134  wxString file;
135  long ndx;
136  bool got = config.GetFirstEntry(file, ndx);
137  while (got) {
138  AddFileToHistory(config.Read(file), false);
139  got = config.GetNextEntry(file, ndx);
140  }
141 
142  config.SetPath(wxT(".."));
143 
144  AddFilesToMenu();
145 }
146 
147 void FileHistory::Save(wxConfigBase & config, const wxString & group)
148 {
149  config.DeleteGroup(group);
150  config.SetPath(group);
151 
152  // Stored in reverse order
153  int n = mHistory.GetCount() - 1;
154  for (size_t i = 1; i <= mHistory.GetCount(); i++) {
155  config.Write(wxString::Format(wxT("file%02d"), (int)i), mHistory[n--]);
156  }
157 
158  config.SetPath(wxT(".."));
159 }
160 
162 {
163  for (auto pMenu : mMenus)
164  AddFilesToMenu(pMenu);
165 }
166 
167 void FileHistory::AddFilesToMenu(wxMenu *menu)
168 {
169  wxMenuItemList items = menu->GetMenuItems();
170  for (auto end = items.end(), iter = items.begin(); iter != end;)
171  menu->Destroy(*iter++);
172 
173  for (size_t i = 0; i < mHistory.GetCount(); i++) {
174  menu->Append(mIDBase + 1 + i, mHistory[i]);
175  }
176 
177  if (mHistory.GetCount() > 0) {
178  menu->AppendSeparator();
179  }
180  menu->Append(mIDBase, _("&Clear"));
181  menu->Enable(mIDBase, mHistory.GetCount() > 0);
182 }
size_t mMaxFiles
Definition: FileHistory.h:44
void UseMenu(wxMenu *menu)
std::vector< wxMenu * > mMenus
Definition: FileHistory.h:47
FileHistory(size_t maxfiles=12, wxWindowID idbase=wxID_FILE)
Definition: FileHistory.cpp:25
void AddFilesToMenu()
void Clear()
Definition: FileHistory.cpp:78
const wxString & GetHistoryFile(size_t i) const
Definition: FileHistory.cpp:85
void RemoveFileFromHistory(size_t i, bool update=true)
Definition: FileHistory.cpp:65
void Save(wxConfigBase &config, const wxString &group)
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom"))), OnMoveTrack) void TrackMenuTable::OnSetName(wxCommandEvent &)
void RemoveMenu(wxMenu *menu)
void Load(wxConfigBase &config, const wxString &group)
wxArrayString mHistory
Definition: FileHistory.h:48
wxWindowID mIDBase
Definition: FileHistory.h:45
virtual ~FileHistory()
Definition: FileHistory.cpp:31
void AddFileToHistory(const wxString &file, bool update=true)
Definition: FileHistory.cpp:36
size_t GetCount()
Definition: FileHistory.cpp:97