#include "wx/file.h"
#include "wx/cmdproc.h"
#include "wx/tokenzr.h"
+#include "wx/filename.h"
#include <stdio.h>
#include <string.h>
#if wxUSE_STD_IOSTREAM
#include "wx/ioswrap.h"
+ #include "wx/beforestd.h"
#if wxUSE_IOSTREAMH
#include <fstream.h>
#else
#include <fstream>
#endif
+ #include "wx/afterstd.h"
#else
#include "wx/wfstream.h"
#endif
// local functions
// ----------------------------------------------------------------------------
-static wxString FindExtension(const wxChar *path)
+static wxString FindExtension(const wxString& path)
{
wxString ext;
wxSplitPath(path, NULL, NULL, &ext);
Modify(false);
SetDocumentSaved(false);
- wxString name;
- GetDocumentManager()->MakeDefaultName(name);
+ const wxString name = GetDocumentManager()->MakeNewDocumentName();
SetTitle(name);
SetFilename(name, true);
#else
wxString filter = docTemplate->GetFileFilter() ;
#endif
- wxString tmp = wxFileSelector(_("Save as"),
- docTemplate->GetDirectory(),
+ wxString defaultDir = docTemplate->GetDirectory();
+ if (defaultDir.IsEmpty())
+ defaultDir = wxPathOnly(GetFilename());
+
+ wxString tmp = wxFileSelector(_("Save As"),
+ defaultDir,
wxFileNameFromPath(GetFilename()),
docTemplate->GetDefaultExtension(),
filter,
// Get title, or filename if no title, else unnamed
+#if WXWIN_COMPATIBILITY_2_8
bool wxDocument::GetPrintableName(wxString& buf) const
{
- if (!m_documentTitle.empty())
- {
- buf = m_documentTitle;
- return true;
- }
- else if (!m_documentFile.empty())
- {
- buf = wxFileNameFromPath(m_documentFile);
- return true;
- }
- else
- {
- buf = _("unnamed");
- return true;
- }
+ // this function can not only be overridden by the user code but also
+ // called by it so we need to ensure that we return the same thing as
+ // GetUserReadableName() but we can't call it because this would result in
+ // an infinite recursion, hence we use the helper DoGetUserReadableName()
+ buf = DoGetUserReadableName();
+
+ return true;
+}
+#endif // WXWIN_COMPATIBILITY_2_8
+
+wxString wxDocument::GetUserReadableName() const
+{
+#if WXWIN_COMPATIBILITY_2_8
+ // we need to call the old virtual function to ensure that the overridden
+ // version of it is still called
+ wxString name;
+ if ( GetPrintableName(name) )
+ return name;
+#endif // WXWIN_COMPATIBILITY_2_8
+
+ return DoGetUserReadableName();
+}
+
+wxString wxDocument::DoGetUserReadableName() const
+{
+ if ( !m_documentTitle.empty() )
+ return m_documentTitle;
+
+ if ( !m_documentFile.empty() )
+ return wxFileNameFromPath(m_documentFile);
+
+ return _("unnamed");
}
wxWindow *wxDocument::GetDocumentWindow() const
{
if (IsModified())
{
- wxString title;
- GetPrintableName(title);
+ wxString title = GetUserReadableName();
wxString msgTitle;
- if (!wxTheApp->GetAppName().empty())
- msgTitle = wxTheApp->GetAppName();
+ if (!wxTheApp->GetAppDisplayName().empty())
+ msgTitle = wxTheApp->GetAppDisplayName();
else
msgTitle = wxString(_("Warning"));
wxString prompt;
- prompt.Printf(_("Do you want to save changes to document %s?"),
- (const wxChar *)title);
+ prompt.Printf(_("Do you want to save changes to document %s?"), title);
int res = wxMessageBox(prompt, msgTitle,
wxYES_NO|wxCANCEL|wxICON_QUESTION,
GetDocumentWindow());
bool wxDocument::DoSaveDocument(const wxString& file)
{
wxString msgTitle;
- if (!wxTheApp->GetAppName().empty())
- msgTitle = wxTheApp->GetAppName();
+ if (!wxTheApp->GetAppDisplayName().empty())
+ msgTitle = wxTheApp->GetAppDisplayName();
else
msgTitle = wxString(_("File error"));
wxDocument *doc = GetDocument();
if (!doc) return;
- wxString name;
- doc->GetPrintableName(name);
- win->SetLabel(name);
+ win->SetLabel(doc->GetUserReadableName());
}
void wxView::SetDocument(wxDocument *doc)
return (wxDocument *) NULL;
}
-// Make a default document name
-bool wxDocManager::MakeDefaultName(wxString& name)
+// Make a default name for a new document
+#if WXWIN_COMPATIBILITY_2_8
+bool wxDocManager::MakeDefaultName(wxString& WXUNUSED(name))
{
- name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
- m_defaultDocumentNameCounter++;
+ // we consider that this function can only be overridden by the user code,
+ // not called by it as it only makes sense to call it internally, so we
+ // don't bother to return anything from here
+ return false;
+}
+#endif // WXWIN_COMPATIBILITY_2_8
- return true;
+wxString wxDocManager::MakeNewDocumentName()
+{
+ wxString name;
+
+#if WXWIN_COMPATIBILITY_2_8
+ if ( !MakeDefaultName(name) )
+#endif // WXWIN_COMPATIBILITY_2_8
+ {
+ name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
+ m_defaultDocumentNameCounter++;
+ }
+
+ return name;
}
// Make a frame title (override this to do something different)
// If docName is empty, a document is not currently active.
wxString wxDocManager::MakeFrameTitle(wxDocument* doc)
{
- wxString appName = wxTheApp->GetAppName();
+ wxString appName = wxTheApp->GetAppDisplayName();
wxString title;
if (!doc)
title = appName;
else
{
- wxString docName;
- doc->GetPrintableName(docName);
+ wxString docName = doc->GetUserReadableName();
title = docName + wxString(_(" - ")) + appName;
}
return title;
wxWindow* parent = wxFindSuitableParent();
- wxString pathTmp = wxFileSelectorEx(_("Select a file"),
+ wxString pathTmp = wxFileSelectorEx(_("Open File"),
m_lastDirectory,
wxEmptyString,
&FilterIndex,
if (!wxFileExists(pathTmp))
{
wxString msgTitle;
- if (!wxTheApp->GetAppName().empty())
- msgTitle = wxTheApp->GetAppName();
+ if (!wxTheApp->GetAppDisplayName().empty())
+ msgTitle = wxTheApp->GetAppDisplayName();
else
msgTitle = wxString(_("File error"));
// 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)
{
+ wxFileName fn(file);
size_t i;
// Check we don't already have this file
- for (i = 0; i < m_fileHistoryN; i++)
+ for (i = 0; i < m_fileHistory.GetCount(); 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 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);
}
// if we already have a full history, delete the one at the end
- if ( m_fileMaxFiles == m_fileHistoryN )
+ if ( m_fileMaxFiles == m_fileHistory.GetCount() )
{
- RemoveFileFromHistory (m_fileHistoryN - 1);
+ 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_fileHistoryN < m_fileMaxFiles)
+ if (m_fileHistory.GetCount() < m_fileMaxFiles)
{
wxList::compatibility_iterator node = m_fileMenus.GetFirst();
while (node)
{
wxMenu* menu = (wxMenu*) node->GetData();
- if ( m_fileHistoryN == 0 && menu->GetMenuItemCount() )
+ if ( m_fileHistory.IsEmpty() && menu->GetMenuItemCount() )
{
menu->AppendSeparator();
}
- menu->Append(m_idBase+m_fileHistoryN, _("[EMPTY]"));
+ menu->Append(m_idBase+m_fileHistory.GetCount(), _("[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);
+
+ 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_fileHistoryN; i++)
- {
- if ( m_fileHistory[i] )
+ for (i = 0; i < m_fileHistory.GetCount(); i++)
{
// if in same directory just show the filename; otherwise the full
// path
// 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)
{
node = node->GetNext();
}
}
- }
}
void wxFileHistory::RemoveFileFromHistory(size_t i)
{
- wxCHECK_RET( i < m_fileHistoryN,
+ wxCHECK_RET( i < m_fileHistory.GetCount(),
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];
- }
+ // delete the element from the array
+ m_fileHistory.RemoveAt(i);
wxList::compatibility_iterator node = m_fileMenus.GetFirst();
while ( node )
// shuffle filenames up
wxString buf;
- for ( j = i; j < m_fileHistoryN - 1; j++ )
+ for ( size_t j = i; j < m_fileHistory.GetCount(); j++ )
{
- buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[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_fileHistoryN) - 1;
+ 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_fileHistoryN == 1 )
+ if ( m_fileHistory.GetCount() == 0 )
{
wxMenuItemList::compatibility_iterator nodeLast = menu->GetMenuItems().GetLast();
if ( nodeLast )
//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 wxUSE_CONFIG
void wxFileHistory::Load(wxConfigBase& config)
{
- m_fileHistoryN = 0;
+ m_fileHistory.Clear();
+
wxString buf;
- buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
+ buf.Printf(wxT("file%d"), 1);
+
wxString historyFile;
- while ((m_fileHistoryN < m_fileMaxFiles) && config.Read(buf, &historyFile) && (!historyFile.empty()))
+ while ((m_fileHistory.GetCount() < m_fileMaxFiles) &&
+ config.Read(buf, &historyFile) && !historyFile.empty())
{
- m_fileHistory[m_fileHistoryN] = MYcopystring((const wxChar*) historyFile);
- m_fileHistoryN ++;
- buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
+ m_fileHistory.Add(historyFile);
+
+ buf.Printf(wxT("file%d"), (int)m_fileHistory.GetCount()+1);
historyFile = wxEmptyString;
}
+
AddFilesToMenu();
}
{
wxString buf;
buf.Printf(wxT("file%d"), (int)i+1);
- if (i < m_fileHistoryN)
+ if (i < m_fileHistory.GetCount())
config.Write(buf, wxString(m_fileHistory[i]));
else
config.Write(buf, wxEmptyString);
void wxFileHistory::AddFilesToMenu()
{
- if (m_fileHistoryN > 0)
+ if (m_fileHistory.GetCount() > 0)
{
wxList::compatibility_iterator node = m_fileMenus.GetFirst();
while (node)
}
size_t i;
- for (i = 0; i < m_fileHistoryN; i++)
- {
- if (m_fileHistory[i])
+ for (i = 0; i < m_fileHistory.GetCount(); i++)
{
wxString buf;
- buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
+ 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_fileHistoryN > 0)
+ if (m_fileHistory.GetCount() > 0)
{
if (menu->GetMenuItemCount())
{
}
size_t i;
- for (i = 0; i < m_fileHistoryN; i++)
- {
- if (m_fileHistory[i])
+ for (i = 0; i < m_fileHistory.GetCount(); i++)
{
wxString buf;
- buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
+ buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i].c_str());
menu->Append(m_idBase+i, buf);
}
}
- }
}
// ----------------------------------------------------------------------------
return false;
char buf[4096];
- do
+ for ( ;; )
{
stream.Read(buf, WXSIZEOF(buf));
const size_t nRead = stream.LastRead();
- if ( !nRead || !file.Write(buf, nRead) )
+ if ( !nRead )
+ {
+ if ( stream.Eof() )
+ break;
+
+ return false;
+ }
+
+ if ( !file.Write(buf, nRead) )
return false;
}
- while ( !stream.Eof() );
return true;
}