-// ----------------------------------------------------------------------------
-// File history processor
-// ----------------------------------------------------------------------------
-
-wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase)
-{
- m_fileMaxFiles = maxFiles;
- m_idBase = idBase;
-}
-
-wxFileHistory::~wxFileHistory()
-{
-}
-
-void wxFileHistory::AddFileToHistory(const wxString& file)
-{
- wxFileName fn(file);
- size_t i;
-
- // Check we don't already have this file
- for (i = 0; i < m_fileHistory.GetCount(); i++)
- {
- // we need to do a comparison using wxFileNames because it knows
- // how to exactly compare files on the different platforms
- // (e.g. handle case [in]sensitive filesystems)
- if ( fn == wxFileName(m_fileHistory[i]) )
- {
- // 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_fileHistory.GetCount() )
- {
- RemoveFileFromHistory (m_fileHistory.GetCount() - 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_fileHistory.GetCount() < m_fileMaxFiles)
- {
- wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- while (node)
- {
- wxMenu* menu = (wxMenu*) node->GetData();
- if ( m_fileHistory.IsEmpty() && menu->GetMenuItemCount() )
- {
- menu->AppendSeparator();
- }
- menu->Append(m_idBase+m_fileHistory.GetCount(), _("[EMPTY]"));
- node = node->GetNext();
- }
- }
-
- m_fileHistory.Insert(file, 0);
-
- // this is the directory of the last opened file
- wxString pathCurrent;
- wxSplitPath( m_fileHistory[0], &pathCurrent, NULL, NULL );
- for (i = 0; i < m_fileHistory.GetCount(); 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];
- }
-
- // we need to quote '&' characters which are used for mnemonics
- pathInMenu.Replace(_T("&"), _T("&&"));
-
- 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_fileHistory.GetCount(),
- wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
-
- // delete the element from the array
- m_fileHistory.RemoveAt(i);
-
- wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- while ( node )
- {
- wxMenu* menu = (wxMenu*) node->GetData();
-
- // shuffle filenames up
- wxString buf;
- for ( size_t j = i; j < m_fileHistory.GetCount(); j++ )
- {
- buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j].c_str());
- menu->SetLabel(m_idBase + j, buf);
- }
-
- node = node->GetNext();
-
- // delete the last menu item which is unused now
- wxWindowID lastItemId = m_idBase + wx_truncate_cast(wxWindowID, m_fileHistory.GetCount());
- if (menu->FindItem(lastItemId))
- {
- menu->Delete(lastItemId);
- }
-
- // delete the last separator too if no more files are left
- if ( m_fileHistory.GetCount() == 0 )
- {
- wxMenuItemList::compatibility_iterator nodeLast = menu->GetMenuItems().GetLast();
- if ( nodeLast )
- {
- wxMenuItem *menuItem = nodeLast->GetData();
- if ( menuItem->IsSeparator() )
- {
- menu->Delete(menuItem);
- }
- //else: should we search backwards for the last separator?
- }
- //else: menu is empty somehow
- }
- }
-}
-
-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_fileHistory.Clear();
-
- wxString buf;
- buf.Printf(wxT("file%d"), 1);
-
- wxString historyFile;
- while ((m_fileHistory.GetCount() < m_fileMaxFiles) &&
- config.Read(buf, &historyFile) && !historyFile.empty())
- {
- m_fileHistory.Add(historyFile);
-
- buf.Printf(wxT("file%d"), (int)m_fileHistory.GetCount()+1);
- historyFile = wxEmptyString;
- }
-
- 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_fileHistory.GetCount())
- config.Write(buf, wxString(m_fileHistory[i]));
- else
- config.Write(buf, wxEmptyString);
- }
-}
-#endif // wxUSE_CONFIG
-
-void wxFileHistory::AddFilesToMenu()
-{
- if (m_fileHistory.GetCount() > 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_fileHistory.GetCount(); i++)
- {
- wxString buf;
- buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i].c_str());
- menu->Append(m_idBase+i, buf);
- }
- node = node->GetNext();
- }
- }
-}
-
-void wxFileHistory::AddFilesToMenu(wxMenu* menu)
-{
- if (m_fileHistory.GetCount() > 0)
- {
- if (menu->GetMenuItemCount())
- {
- menu->AppendSeparator();
- }
-
- size_t i;
- for (i = 0; i < m_fileHistory.GetCount(); i++)
- {
- wxString buf;
- buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i].c_str());
- menu->Append(m_idBase+i, buf);
- }
- }
-}
-