// Modified by:
// Created: 01/02/97
// RCS-ID: $Id$
-// Copyright: (c) Julian Smart and Markus Holzem
+// Copyright: (c) Julian Smart
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include "wx/log.h"
#endif
+#include "wx/ffile.h"
+
+#ifdef __WXMAC__
+ #include "wx/filename.h"
+#endif
#ifdef __WXGTK__
#include "wx/mdi.h"
{
wxDocManager* manager = GetDocumentManager();
- wxNode *node = m_documentViews.First();
+ wxNode *node = m_documentViews.GetFirst();
while (node)
{
- wxView *view = (wxView *)node->Data();
+ wxView *view = (wxView *)node->GetData();
if (!view->Close())
return FALSE;
- wxNode *next = node->Next();
+ wxNode *next = node->GetNext();
delete view; // Deletes node implicitly
node = next;
wxView *wxDocument::GetFirstView() const
{
- if (m_documentViews.Number() == 0)
+ if (m_documentViews.GetCount() == 0)
return (wxView *) NULL;
- return (wxView *)m_documentViews.First()->Data();
+ return (wxView *)m_documentViews.GetFirst()->GetData();
}
wxDocManager *wxDocument::GetDocumentManager() const
GetDocumentManager()->AddFileToHistory(fileName);
// Notify the views that the filename has changed
- wxNode *node = m_documentViews.First();
+ wxNode *node = m_documentViews.GetFirst();
while (node)
{
- wxView *view = (wxView *)node->Data();
+ wxView *view = (wxView *)node->GetData();
view->OnChangeFilename();
- node = node->Next();
+ node = node->GetNext();
}
return OnSaveDocument(m_documentFile);
msgTitle = wxString(_("File error"));
#if wxUSE_STD_IOSTREAM
- wxSTD ofstream store(wxString(file.fn_str()).mb_str()); // ?????
+ wxSTD ofstream store(file.mb_str());
if (store.fail() || store.bad())
#else
- wxFileOutputStream store( file );
+ wxFileOutputStream store(file);
if (store.GetLastError() != wxSTREAM_NO_ERROR)
#endif
{
Modify(FALSE);
SetFilename(file);
SetDocumentSaved(TRUE);
+#ifdef __WXMAC__
+ wxFileName fn(file) ;
+ fn.MacSetDefaultTypeAndCreator() ;
+#endif
return TRUE;
}
msgTitle = wxString(_("File error"));
#if wxUSE_STD_IOSTREAM
- wxSTD ifstream store(wxString(file.fn_str()).mb_str()); // ????
+ wxSTD ifstream store(file.mb_str());
if (store.fail() || store.bad())
#else
- wxFileInputStream store( file );
+ wxFileInputStream store(file);
if (store.GetLastError() != wxSTREAM_NO_ERROR)
#endif
{
// there are no more views.
void wxDocument::OnChangedViewList()
{
- if (m_documentViews.Number() == 0)
+ if (m_documentViews.GetCount() == 0)
{
if (OnSaveModified())
{
void wxDocument::UpdateAllViews(wxView *sender, wxObject *hint)
{
- wxNode *node = m_documentViews.First();
+ wxNode *node = m_documentViews.GetFirst();
while (node)
{
- wxView *view = (wxView *)node->Data();
+ wxView *view = (wxView *)node->GetData();
if (view != sender)
view->OnUpdate(sender, hint);
- node = node->Next();
+ node = node->GetNext();
}
}
void wxDocument::NotifyClosing()
{
- wxNode *node = m_documentViews.First();
+ wxNode *node = m_documentViews.GetFirst();
while (node)
{
- wxView *view = (wxView *)node->Data();
+ wxView *view = (wxView *)node->GetData();
view->OnClosingDocument();
- node = node->Next();
+ node = node->GetNext();
}
}
if ( notifyViews )
{
// Notify the views that the filename has changed
- wxNode *node = m_documentViews.First();
+ wxNode *node = m_documentViews.GetFirst();
while (node)
{
- wxView *view = (wxView *)node->Data();
+ wxView *view = (wxView *)node->GetData();
view->OnChangeFilename();
- node = node->Next();
+ node = node->GetNext();
}
}
}
bool wxDocManager::CloseDocuments(bool force)
{
- wxNode *node = m_docs.First();
+ wxNode *node = m_docs.GetFirst();
while (node)
{
- wxDocument *doc = (wxDocument *)node->Data();
- wxNode *next = node->Next();
+ wxDocument *doc = (wxDocument *)node->GetData();
+ wxNode *next = node->GetNext();
if (!doc->Close() && !force)
return FALSE;
if (!CloseDocuments(force))
return FALSE;
- wxNode *node = m_templates.First();
+ wxNode *node = m_templates.GetFirst();
while (node)
{
- wxDocTemplate *templ = (wxDocTemplate*) node->Data();
- wxNode* next = node->Next();
+ wxDocTemplate *templ = (wxDocTemplate*) node->GetData();
+ wxNode* next = node->GetNext();
delete templ;
node = next;
}
{
if (m_currentView)
return m_currentView;
- if (m_docs.Number() == 1)
+ if (m_docs.GetCount() == 1)
{
- wxDocument* doc = (wxDocument*) m_docs.First()->Data();
+ wxDocument* doc = (wxDocument*) m_docs.GetFirst()->GetData();
return doc->GetFirstView();
}
return (wxView *) NULL;
wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags)
{
- wxDocTemplate **templates = new wxDocTemplate *[m_templates.Number()];
- int i;
- int n = 0;
- for (i = 0; i < m_templates.Number(); i++)
+ wxDocTemplate **templates = new wxDocTemplate *[m_templates.GetCount()];
+ int n = 0;
+
+ for (size_t i = 0; i < m_templates.GetCount(); i++)
{
- wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Nth(i)->Data());
+ wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData());
if (temp->IsVisible())
{
templates[n] = temp;
// If we've reached the max number of docs, close the
// first one.
- if (GetDocuments().Number() >= m_maxDocsOpen)
+ if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
{
- wxDocument *doc = (wxDocument *)GetDocuments().First()->Data();
+ wxDocument *doc = (wxDocument *)GetDocuments().GetFirst()->GetData();
if (doc->Close())
{
// Implicitly deletes the document when
wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
{
- wxDocTemplate **templates = new wxDocTemplate *[m_templates.Number()];
- int n =0;
- int i;
- for (i = 0; i < m_templates.Number(); i++)
+ wxDocTemplate **templates = new wxDocTemplate *[m_templates.GetCount()];
+ int n =0;
+
+ for (size_t i = 0; i < m_templates.GetCount(); i++)
{
- wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Nth(i)->Data());
+ wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData());
if (temp->IsVisible())
{
if (temp->GetDocumentName() == doc->GetDocumentName())
m_fileHistory->AddFileToHistory(file);
}
-void wxDocManager::RemoveFileFromHistory(int i)
+void wxDocManager::RemoveFileFromHistory(size_t i)
{
if (m_fileHistory)
m_fileHistory->RemoveFileFromHistory(i);
}
-wxString wxDocManager::GetHistoryFile(int i) const
+wxString wxDocManager::GetHistoryFile(size_t i) const
{
wxString histFile;
m_fileHistory->AddFilesToMenu();
}
-int wxDocManager::GetNoHistoryFiles() const
+size_t wxDocManager::GetNoHistoryFiles() const
{
if (m_fileHistory)
return m_fileHistory->GetNoHistoryFiles();
wxDocTemplate *theTemplate = (wxDocTemplate *) NULL;
// Find the template which this extension corresponds to
- int i;
- for (i = 0; i < m_templates.Number(); i++)
+ for (size_t i = 0; i < m_templates.GetCount(); i++)
{
- wxDocTemplate *temp = (wxDocTemplate *)m_templates.Nth(i)->Data();
+ wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData();
if ( temp->FileMatchesTemplate(path) )
{
theTemplate = temp;
// File history processor
// ----------------------------------------------------------------------------
-wxFileHistory::wxFileHistory(int maxFiles)
+wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase)
{
m_fileMaxFiles = maxFiles;
+ m_idBase = idBase;
m_fileHistoryN = 0;
m_fileHistory = new wxChar *[m_fileMaxFiles];
}
wxFileHistory::~wxFileHistory()
{
- int i;
+ 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)
{
- int i;
+ size_t i;
// Check we don't already have this file
for (i = 0; i < m_fileHistoryN; i++)
// Move existing files (if any) down so we can insert file at beginning.
if (m_fileHistoryN < m_fileMaxFiles)
{
- wxNode* node = m_fileMenus.First();
+ wxNode* node = m_fileMenus.GetFirst();
while (node)
{
- wxMenu* menu = (wxMenu*) node->Data();
- if (m_fileHistoryN == 0)
+ wxMenu* menu = (wxMenu*) node->GetData();
+ if ( m_fileHistoryN == 0 && menu->GetMenuItemCount() )
+ {
menu->AppendSeparator();
- menu->Append(wxID_FILE1+m_fileHistoryN, _("[EMPTY]"));
- node = node->Next();
+ }
+ menu->Append(m_idBase+m_fileHistoryN, _("[EMPTY]"));
+ node = node->GetNext();
}
m_fileHistoryN ++;
}
wxString buf;
buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str());
- wxNode* node = m_fileMenus.First();
+ wxNode* node = m_fileMenus.GetFirst();
while (node)
{
- wxMenu* menu = (wxMenu*) node->Data();
- menu->SetLabel(wxID_FILE1 + i, buf);
- node = node->Next();
+ wxMenu* menu = (wxMenu*) node->GetData();
+ menu->SetLabel(m_idBase + i, buf);
+ node = node->GetNext();
}
}
}
}
-void wxFileHistory::RemoveFileFromHistory(int i)
+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];
+ // delete the element from the array (could use memmove() too...)
+ delete [] m_fileHistory[i];
- int j;
- for ( j = i; j < m_fileHistoryN - 1; j++ )
- {
- m_fileHistory[j] = m_fileHistory[j + 1];
- }
+ size_t j;
+ for ( j = i; j < m_fileHistoryN - 1; j++ )
+ {
+ m_fileHistory[j] = m_fileHistory[j + 1];
+ }
- wxNode* node = m_fileMenus.First();
+ wxNode* node = m_fileMenus.GetFirst();
while ( node )
{
- wxMenu* menu = (wxMenu*) node->Data();
+ 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(wxID_FILE1 + j, buf);
- }
+ // 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->Next();
+ node = node->GetNext();
// delete the last menu item which is unused now
- if (menu->FindItem(wxID_FILE1 + m_fileHistoryN - 1))
- menu->Delete(wxID_FILE1 + m_fileHistoryN - 1);
+ 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 )
m_fileHistoryN--;
}
-wxString wxFileHistory::GetHistoryFile(int i) const
+wxString wxFileHistory::GetHistoryFile(size_t i) const
{
wxString s;
if ( i < m_fileHistoryN )
{
m_fileHistoryN = 0;
wxString buf;
- buf.Printf(wxT("file%d"), m_fileHistoryN+1);
+ 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] = copystring((const wxChar*) historyFile);
m_fileHistoryN ++;
- buf.Printf(wxT("file%d"), m_fileHistoryN+1);
+ buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
historyFile = wxT("");
}
AddFilesToMenu();
void wxFileHistory::Save(wxConfigBase& config)
{
- int i;
+ size_t i;
for (i = 0; i < m_fileHistoryN; i++)
{
wxString buf;
- buf.Printf(wxT("file%d"), i+1);
+ buf.Printf(wxT("file%d"), (int)i+1);
config.Write(buf, wxString(m_fileHistory[i]));
}
}
{
if (m_fileHistoryN > 0)
{
- wxNode* node = m_fileMenus.First();
+ wxNode* node = m_fileMenus.GetFirst();
while (node)
{
- wxMenu* menu = (wxMenu*) node->Data();
- menu->AppendSeparator();
- int i;
+ 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(wxID_FILE1+i, buf);
+ menu->Append(m_idBase+i, buf);
}
}
- node = node->Next();
+ node = node->GetNext();
}
}
}
{
if (m_fileHistoryN > 0)
{
- menu->AppendSeparator();
- int i;
+ 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(wxID_FILE1+i, buf);
+ menu->Append(m_idBase+i, buf);
}
}
}
// ----------------------------------------------------------------------------
#if wxUSE_STD_IOSTREAM
+
bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
{
- FILE *fd1;
- int ch;
-
- if ((fd1 = wxFopen (filename.fn_str(), _T("rb"))) == NULL)
+ wxFFile file(filename, _T("rb"));
+ if ( !file.IsOpened() )
return FALSE;
- while ((ch = getc (fd1)) != EOF)
- stream << (unsigned char)ch;
+ char buf[4096];
+
+ size_t nRead;
+ do
+ {
+ nRead = file.Read(buf, WXSIZEOF(buf));
+ if ( file.Error() )
+ return FALSE;
+
+ stream.write(buf, nRead);
+ if ( !stream )
+ return FALSE;
+ }
+ while ( !file.Eof() );
- fclose (fd1);
return TRUE;
}
bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
{
- FILE *fd1;
- int ch;
-
- if ((fd1 = wxFopen (filename.fn_str(), _T("wb"))) == NULL)
- {
+ wxFFile file(filename, _T("wb"));
+ if ( !file.IsOpened() )
return FALSE;
- }
- while (!stream.eof())
+ char buf[4096];
+ do
{
- ch = stream.get();
- if (!stream.eof())
- putc (ch, fd1);
+ stream.read(buf, WXSIZEOF(buf));
+ if ( !stream.bad() ) // fail may be set on EOF, don't use operator!()
+ {
+ if ( !file.Write(buf, stream.gcount()) )
+ return FALSE;
+ }
}
- fclose (fd1);
+ while ( !stream.eof() );
+
return TRUE;
}
-#else
+
+#else // !wxUSE_STD_IOSTREAM
+
bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
{
- FILE *fd1;
- int ch;
-
- if ((fd1 = wxFopen (filename, wxT("rb"))) == NULL)
+ wxFFile file(filename, _T("rb"));
+ if ( !file.IsOpened() )
return FALSE;
- while ((ch = getc (fd1)) != EOF)
- stream.PutC((char) ch);
+ char buf[4096];
+
+ size_t nRead;
+ do
+ {
+ nRead = file.Read(buf, WXSIZEOF(buf));
+ if ( file.Error() )
+ return FALSE;
+
+ stream.Write(buf, nRead);
+ if ( !stream )
+ return FALSE;
+ }
+ while ( !file.Eof() );
- fclose (fd1);
return TRUE;
}
bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
{
- FILE *fd1;
- char ch;
-
- if ((fd1 = wxFopen (filename, wxT("wb"))) == NULL)
- {
+ wxFFile file(filename, _T("wb"));
+ if ( !file.IsOpened() )
return FALSE;
- }
- int len = stream.GetSize();
- // TODO: is this the correct test for EOF?
- while (stream.TellI() < (len - 1))
+ char buf[4096];
+ do
{
- ch = stream.GetC();
- putc (ch, fd1);
+ stream.Read(buf, WXSIZEOF(buf));
+
+ const size_t nRead = stream.LastRead();
+ if ( !nRead || !file.Write(buf, nRead) )
+ return FALSE;
}
- fclose (fd1);
+ while ( !stream.Eof() );
+
return TRUE;
}
-#endif
+
+#endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
#endif // wxUSE_DOC_VIEW_ARCHITECTURE