-// ----------------------------------------------------------------------------
-// File history processor
-// ----------------------------------------------------------------------------
-
-static inline wxChar* MYcopystring(const wxString& s)
-{
- wxChar* copy = new wxChar[s.length() + 1];
- return wxStrcpy(copy, s.c_str());
-}
-
-static inline wxChar* MYcopystring(const wxChar* s)
-{
- wxChar* copy = new wxChar[wxStrlen(s) + 1];
- return wxStrcpy(copy, s);
-}
-
-wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase)
-{
- m_fileMaxFiles = maxFiles;
- m_idBase = idBase;
- m_fileHistoryN = 0;
- m_fileHistory = new wxChar *[m_fileMaxFiles];
-}
-
-wxFileHistory::~wxFileHistory()
-{
- size_t i;
- for (i = 0; i < m_fileHistoryN; i++)
- delete[] m_fileHistory[i];
- delete[] m_fileHistory;
-}
-
-// File history management
-void wxFileHistory::AddFileToHistory(const wxString& file)
-{
- size_t i;
-
- // Check we don't already have this file
- for (i = 0; i < m_fileHistoryN; i++)
- {
-#if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
- wxString testString;
- if ( m_fileHistory[i] )
- testString = m_fileHistory[i];
- if ( m_fileHistory[i] && ( file.Lower() == testString.Lower() ) )
-#else
- if ( m_fileHistory[i] && ( file == m_fileHistory[i] ) )
-#endif
- {
- // we do have it, move it to the top of the history
- RemoveFileFromHistory (i);
- AddFileToHistory (file);
- return;
- }
- }
-
- // if we already have a full history, delete the one at the end
- if ( m_fileMaxFiles == m_fileHistoryN )
- {
- RemoveFileFromHistory (m_fileHistoryN - 1);
- AddFileToHistory (file);
- return;
- }
-
- // Add to the project file history:
- // Move existing files (if any) down so we can insert file at beginning.
- if (m_fileHistoryN < m_fileMaxFiles)
- {
- wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- while (node)
- {
- wxMenu* menu = (wxMenu*) node->GetData();
- if ( m_fileHistoryN == 0 && menu->GetMenuItemCount() )
- {
- menu->AppendSeparator();
- }
- menu->Append(m_idBase+m_fileHistoryN, _("[EMPTY]"));
- node = node->GetNext();
- }
- m_fileHistoryN ++;
- }
- // Shuffle filenames down
- for (i = (m_fileHistoryN-1); i > 0; i--)
- {
- m_fileHistory[i] = m_fileHistory[i-1];
- }
- m_fileHistory[0] = MYcopystring(file);
-
- // this is the directory of the last opened file
- wxString pathCurrent;
- wxSplitPath( m_fileHistory[0], &pathCurrent, NULL, NULL );
- for (i = 0; i < m_fileHistoryN; i++)
- {
- if ( m_fileHistory[i] )
- {
- // if in same directory just show the filename; otherwise the full
- // path
- wxString pathInMenu, path, filename, ext;
- wxSplitPath( m_fileHistory[i], &path, &filename, &ext );
- if ( path == pathCurrent )
- {
- pathInMenu = filename;
- if ( !ext.empty() )
- pathInMenu = pathInMenu + wxFILE_SEP_EXT + ext;
- }
- else
- {
- // absolute path; could also set relative path
- pathInMenu = m_fileHistory[i];
- }
-
- wxString buf;
- buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str());
- wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- while (node)
- {
- wxMenu* menu = (wxMenu*) node->GetData();
- menu->SetLabel(m_idBase + i, buf);
- node = node->GetNext();
- }
- }
- }
-}
-
-void wxFileHistory::RemoveFileFromHistory(size_t i)
-{
- wxCHECK_RET( i < m_fileHistoryN,
- wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
-
- // delete the element from the array (could use memmove() too...)
- delete [] m_fileHistory[i];
-
- size_t j;
- for ( j = i; j < m_fileHistoryN - 1; j++ )
- {
- m_fileHistory[j] = m_fileHistory[j + 1];
- }
-
- wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- while ( node )
- {
- wxMenu* menu = (wxMenu*) node->GetData();
-
- // shuffle filenames up
- wxString buf;
- for ( j = i; j < m_fileHistoryN - 1; j++ )
- {
- buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]);
- menu->SetLabel(m_idBase + j, buf);
- }
-
- node = node->GetNext();
-
- // delete the last menu item which is unused now
- wxWindowID lastItemId = m_idBase + m_fileHistoryN - 1;
- if (menu->FindItem(lastItemId))
- {
- menu->Delete(lastItemId);
- }
-
- // delete the last separator too if no more files are left
- if ( m_fileHistoryN == 1 )
- {
- wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetLast();
- if ( node )
- {
- wxMenuItem *menuItem = node->GetData();
- if ( menuItem->IsSeparator() )
- {
- menu->Delete(menuItem);
- }
- //else: should we search backwards for the last separator?
- }
- //else: menu is empty somehow
- }
- }
-
- m_fileHistoryN--;
-}
-
-wxString wxFileHistory::GetHistoryFile(size_t i) const
-{
- wxString s;
- if ( i < m_fileHistoryN )
- {
- s = m_fileHistory[i];
- }
- else
- {
- wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
- }
-
- return s;
-}
-
-void wxFileHistory::UseMenu(wxMenu *menu)
-{
- if (!m_fileMenus.Member(menu))
- m_fileMenus.Append(menu);
-}
-
-void wxFileHistory::RemoveMenu(wxMenu *menu)
-{
- m_fileMenus.DeleteObject(menu);
-}
-
-#if wxUSE_CONFIG
-void wxFileHistory::Load(wxConfigBase& config)
-{
- m_fileHistoryN = 0;
- wxString buf;
- buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
- wxString historyFile;
- while ((m_fileHistoryN < m_fileMaxFiles) && config.Read(buf, &historyFile) && (historyFile != wxT("")))
- {
- m_fileHistory[m_fileHistoryN] = MYcopystring((const wxChar*) historyFile);
- m_fileHistoryN ++;
- buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
- historyFile = wxT("");
- }
- AddFilesToMenu();
-}
-
-void wxFileHistory::Save(wxConfigBase& config)
-{
- size_t i;
- for (i = 0; i < m_fileMaxFiles; i++)
- {
- wxString buf;
- buf.Printf(wxT("file%d"), (int)i+1);
- if (i < m_fileHistoryN)
- config.Write(buf, wxString(m_fileHistory[i]));
- else
- config.Write(buf, wxEmptyString);
- }
-}
-#endif // wxUSE_CONFIG
-
-void wxFileHistory::AddFilesToMenu()
-{
- if (m_fileHistoryN > 0)
- {
- wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- while (node)
- {
- wxMenu* menu = (wxMenu*) node->GetData();
- if (menu->GetMenuItemCount())
- {
- menu->AppendSeparator();
- }
-
- size_t i;
- for (i = 0; i < m_fileHistoryN; i++)
- {
- if (m_fileHistory[i])
- {
- wxString buf;
- buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
- menu->Append(m_idBase+i, buf);
- }
- }
- node = node->GetNext();
- }
- }
-}
-
-void wxFileHistory::AddFilesToMenu(wxMenu* menu)
-{
- if (m_fileHistoryN > 0)
- {
- if (menu->GetMenuItemCount())
- {
- menu->AppendSeparator();
- }
-
- size_t i;
- for (i = 0; i < m_fileHistoryN; i++)
- {
- if (m_fileHistory[i])
- {
- wxString buf;
- buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
- menu->Append(m_idBase+i, buf);
- }
- }
- }
-}
-