// headers
// ----------------------------------------------------------------------------
-#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
- #pragma implementation "docview.h"
-#endif
-
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#include "wx/confbase.h"
#include "wx/file.h"
#include "wx/cmdproc.h"
+#include "wx/tokenzr.h"
#include <stdio.h>
#include <string.h>
bool wxDocument::DeleteAllViews()
{
wxDocManager* manager = GetDocumentManager();
- wxList::iterator it, en;
- for ( it = m_documentViews.begin(), en = m_documentViews.end();
- it != en;
- ++it )
+ // first check if all views agree to be closed
+ const wxList::iterator end = m_documentViews.end();
+ for ( wxList::iterator i = m_documentViews.begin(); i != end; ++i )
{
- wxView *view = (wxView *)*it;
- if (!view->Close())
+ wxView *view = (wxView *)*i;
+ if ( !view->Close() )
return false;
+ }
- delete view; // Deletes node implicitly
+ // all views agreed to close, now do close them
+ if ( m_documentViews.empty() )
+ {
+ // normally the document would be implicitly deleted when the last view
+ // is, but if don't have any views, do it here instead
+ if ( manager && manager->GetDocuments().Member(this) )
+ delete this;
}
+ else // have views
+ {
+ // as we delete elements we iterate over, don't use the usual "from
+ // begin to end" loop
+ for ( ;; )
+ {
+ wxView *view = (wxView *)*m_documentViews.begin();
- // If we haven't yet deleted the document (for example
- // if there were no views) then delete it.
- if (manager && manager->GetDocuments().Member(this))
- delete this;
+ bool isLastOne = m_documentViews.size() == 1;
+
+ // this always deletes the node implicitly and if this is the last
+ // view also deletes this object itself (also implicitly, great),
+ // so we can't test for m_documentViews.empty() after calling this!
+ delete view;
+
+ if ( isLastOne )
+ break;
+ }
+ }
return true;
}
t->GetDocClassInfo() == docTemplate->GetDocClassInfo())
{
// add a '|' to separate this filter from the previous one
- if ( !filter.IsEmpty() )
+ if ( !filter.empty() )
filter << wxT('|');
filter << t->GetDescription() << wxT(" (") << t->GetFileFilter() << wxT(") |")
wxSAVE | wxOVERWRITE_PROMPT,
GetDocumentWindow());
- if (tmp.IsEmpty())
+ if (tmp.empty())
return false;
wxString fileName(tmp);
wxString path, name, ext;
wxSplitPath(fileName, & path, & name, & ext);
- if (ext.IsEmpty() || ext == wxT(""))
+ if (ext.empty())
{
fileName += wxT(".");
fileName += docTemplate->GetDefaultExtension();
// Get title, or filename if no title, else unnamed
bool wxDocument::GetPrintableName(wxString& buf) const
{
- if (m_documentTitle != wxT(""))
+ if (!m_documentTitle.empty())
{
buf = m_documentTitle;
return true;
}
- else if (m_documentFile != wxT(""))
+ else if (!m_documentFile.empty())
{
buf = wxFileNameFromPath(m_documentFile);
return true;
GetPrintableName(title);
wxString msgTitle;
- if (wxTheApp->GetAppName() != wxT(""))
+ if (!wxTheApp->GetAppName().empty())
msgTitle = wxTheApp->GetAppName();
else
msgTitle = wxString(_("Warning"));
bool wxDocument::DoSaveDocument(const wxString& file)
{
wxString msgTitle;
- if (wxTheApp->GetAppName() != wxT(""))
+ if (!wxTheApp->GetAppName().empty())
msgTitle = wxTheApp->GetAppName();
else
msgTitle = wxString(_("File error"));
#if wxUSE_STD_IOSTREAM
- wxSTD ofstream store(file.mb_str());
+ wxSTD ofstream store(file.mb_str(), wxSTD ios::binary);
if (store.fail() || store.bad())
#else
wxFileOutputStream store(file);
bool wxDocument::DoOpenDocument(const wxString& file)
{
- wxString msgTitle;
- if (wxTheApp->GetAppName() != wxT(""))
- msgTitle = wxTheApp->GetAppName();
- else
- msgTitle = wxString(_("File error"));
-
#if wxUSE_STD_IOSTREAM
- wxSTD ifstream store(file.mb_str());
- if (store.fail() || store.bad())
+ wxSTD ifstream store(file.mb_str(), wxSTD ios::binary);
+ if (!store.fail() && !store.bad())
#else
wxFileInputStream store(file);
- if (store.GetLastError() != wxSTREAM_NO_ERROR)
+ if (store.GetLastError() == wxSTREAM_NO_ERROR)
#endif
{
- (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK|wxICON_EXCLAMATION,
- GetDocumentWindow());
- return false;
- }
#if wxUSE_STD_IOSTREAM
- LoadObject(store);
- if ( !store && !store.eof() )
+ LoadObject(store);
+ if ( !!store || store.eof() )
#else
- int res = LoadObject(store).GetLastError();
- if ((res != wxSTREAM_NO_ERROR) &&
- (res != wxSTREAM_EOF))
+ int res = LoadObject(store).GetLastError();
+ if ( res == wxSTREAM_NO_ERROR || res == wxSTREAM_EOF )
#endif
- {
- (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK|wxICON_EXCLAMATION,
- GetDocumentWindow());
- return false;
+ return true;
}
- return true;
+ wxLogError(_("Sorry, could not open this file."));
+ return false;
}
void wxView::OnChangeFilename()
{
- if (GetFrame() && GetDocument())
- {
- wxString title;
+ // GetFrame can return wxWindow rather than wxTopLevelWindow due to
+ // generic MDI implementation so use SetLabel rather than SetTitle.
+ // It should cause SetTitle() for top level windows.
+ wxWindow *win = GetFrame();
+ if (!win) return;
- GetDocument()->GetPrintableName(title);
+ wxDocument *doc = GetDocument();
+ if (!doc) return;
- GetFrame()->SetTitle(title);
- }
+ wxString name;
+ doc->GetPrintableName(name);
+ win->SetLabel(name);
}
void wxView::SetDocument(wxDocument *doc)
// that of the template
bool wxDocTemplate::FileMatchesTemplate(const wxString& path)
{
+ wxStringTokenizer parser (GetFileFilter(), wxT(";"));
+ wxString anything = wxT ("*");
+ while (parser.HasMoreTokens())
+ {
+ wxString filter = parser.GetNextToken();
+ wxString filterExt = FindExtension (filter);
+ if ( filter.IsSameAs (anything) ||
+ filterExt.IsSameAs (anything) ||
+ filterExt.IsSameAs (FindExtension (path)) )
+ return true;
+ }
return GetDefaultExtension().IsSameAs(FindExtension(path));
}
void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event))
{
- CreateDocument( wxT(""), wxDOC_NEW );
+ CreateDocument( wxEmptyString, wxDOC_NEW );
}
void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event))
{
- if ( !CreateDocument( wxT(""), 0) )
+ if ( !CreateDocument( wxEmptyString, 0) )
{
OnOpenFileFailure();
}
{
newDoc->SetDocumentName(temp->GetDocumentName());
newDoc->SetDocumentTemplate(temp);
- newDoc->OnNewDocument();
+ if (!newDoc->OnNewDocument() )
+ {
+ // Document is implicitly deleted by DeleteAllViews
+ newDoc->DeleteAllViews();
+ return NULL;
+ }
}
return newDoc;
}
{
newDoc->SetDocumentName(temp->GetDocumentName());
newDoc->SetDocumentTemplate(temp);
- newDoc->OnNewDocument();
+ if (!newDoc->OnNewDocument() )
+ {
+ // Document is implicitly deleted by DeleteAllViews
+ newDoc->DeleteAllViews();
+ return NULL;
+ }
}
return newDoc;
}
// Existing document
wxDocTemplate *temp;
- wxString path2(wxT(""));
- if (path != wxT(""))
- path2 = path;
+ wxString path2 = path;
if (flags & wxDOC_SILENT)
{
}
}
+ //see if this file is already open
+ for (size_t i = 0; i < GetDocuments().GetCount(); ++i)
+ {
+ wxDocument* currentDoc = (wxDocument*)(GetDocuments().Item(i)->GetData());
+#ifdef __WXMSW__
+ //file paths are case-insensitive on Windows
+ if (path2.CmpNoCase(currentDoc->GetFilename()) == 0)
+#else
+ if (path2.Cmp(currentDoc->GetFilename()) == 0)
+#endif
+ {
+ //file already open. Just activate it and return
+ if (currentDoc->GetFirstView())
+ {
+ ActivateView(currentDoc->GetFirstView(), true);
+ if (currentDoc->GetDocumentWindow())
+ currentDoc->GetDocumentWindow()->SetFocus();
+ return currentDoc;
+ }
+ }
+ }
+
wxDocument *newDoc = temp->CreateDocument(path2, flags);
if (newDoc)
{
if (templates[i]->IsVisible())
{
// add a '|' to separate this filter from the previous one
- if ( !descrBuf.IsEmpty() )
+ if ( !descrBuf.empty() )
descrBuf << wxT('|');
descrBuf << templates[i]->GetDescription()
wxString pathTmp = wxFileSelectorEx(_("Select a file"),
m_lastDirectory,
- wxT(""),
+ wxEmptyString,
&FilterIndex,
descrBuf,
0,
parent);
wxDocTemplate *theTemplate = (wxDocTemplate *)NULL;
- if (!pathTmp.IsEmpty())
+ if (!pathTmp.empty())
{
if (!wxFileExists(pathTmp))
{
wxString msgTitle;
- if (!wxTheApp->GetAppName().IsEmpty())
+ if (!wxTheApp->GetAppName().empty())
msgTitle = wxTheApp->GetAppName();
else
msgTitle = wxString(_("File error"));
(void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
parent);
- path = wxT("");
+ path = wxEmptyString;
return (wxDocTemplate *) NULL;
}
m_lastDirectory = wxPathOnly(pathTmp);
}
else
{
- path = wxT("");
+ path = wxEmptyString;
}
return theTemplate;
{
int n = event.GetId() - wxID_FILE1; // the index in MRU list
wxString filename(m_docManager->GetHistoryFile(n));
- if ( !filename.IsEmpty() )
+ if ( !filename.empty() )
{
// verify that the file exists before doing anything else
if ( wxFile::Exists(filename) )
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();
wxList::compatibility_iterator node = m_fileMenus.GetFirst();
while ( node )
{
- wxMenu* menu = (wxMenu*) node->GetData();
+ 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);
- }
+ // 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();
+ node = node->GetNext();
// delete the last menu item which is unused now
- wxWindowID lastItemId = m_idBase + m_fileHistoryN - 1;
+ wxWindowID lastItemId = m_idBase + wx_truncate_cast(wxWindowID, 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 )
+ wxMenuItemList::compatibility_iterator nodeLast = menu->GetMenuItems().GetLast();
+ if ( nodeLast )
{
- wxMenuItem *menuItem = node->GetData();
+ wxMenuItem *menuItem = nodeLast->GetData();
if ( menuItem->IsSeparator() )
{
menu->Delete(menuItem);
wxString buf;
buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
wxString historyFile;
- while ((m_fileHistoryN < m_fileMaxFiles) && config.Read(buf, &historyFile) && (historyFile != wxT("")))
+ while ((m_fileHistoryN < 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);
- historyFile = wxT("");
+ historyFile = wxEmptyString;
}
AddFilesToMenu();
}
#endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
#endif // wxUSE_DOC_VIEW_ARCHITECTURE
-