#include "wx/cmdproc.h"
#include "wx/tokenzr.h"
#include "wx/filename.h"
+#include "wx/stdpaths.h"
#include "wx/vector.h"
+#include "wx/ptr_scpd.h"
#if wxUSE_STD_IOSTREAM
#include "wx/ioswrap.h"
wxString FindExtension(const wxString& path)
{
wxString ext;
- wxSplitPath(path, NULL, NULL, &ext);
+ wxFileName::SplitPath(path, NULL, NULL, &ext);
// VZ: extensions are considered not case sensitive - is this really a good
// idea?
{
DeleteContents();
- if (m_commandProcessor)
- delete m_commandProcessor;
+ delete m_commandProcessor;
if (GetDocumentManager())
GetDocumentManager()->RemoveDocument(this);
bool wxDocument::Close()
{
- if (OnSaveModified())
- return OnCloseDocument();
- else
+ if ( !OnSaveModified() )
return false;
+
+ return OnCloseDocument();
}
bool wxDocument::OnCloseDocument()
wxView *wxDocument::GetFirstView() const
{
- if (m_documentViews.GetCount() == 0)
+ if ( m_documentViews.empty() )
return NULL;
- return (wxView *)m_documentViews.GetFirst()->GetData();
+
+ return static_cast<wxView *>(m_documentViews.GetFirst()->GetData());
}
wxDocManager *wxDocument::GetDocumentManager() const
bool wxDocument::Save()
{
- if (!IsModified() && m_savedYet)
+ if ( AlreadySaved() )
return true;
if ( m_documentFile.empty() || !m_savedYet )
if (!docTemplate)
return false;
-#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
- wxString filter = docTemplate->GetDescription() + wxT(" (") + docTemplate->GetFileFilter() + wxT(")|") + docTemplate->GetFileFilter();
+#ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
+ wxString filter = docTemplate->GetDescription() + wxT(" (") +
+ docTemplate->GetFileFilter() + wxT(")|") +
+ docTemplate->GetFileFilter();
// Now see if there are some other template with identical view and document
// classes, whose filters may also be used.
-
if (docTemplate->GetViewClassInfo() && docTemplate->GetDocClassInfo())
{
- wxList::compatibility_iterator node = docTemplate->GetDocumentManager()->GetTemplates().GetFirst();
+ wxList::compatibility_iterator
+ node = docTemplate->GetDocumentManager()->GetTemplates().GetFirst();
while (node)
{
wxDocTemplate *t = (wxDocTemplate*) node->GetData();
if ( !filter.empty() )
filter << wxT('|');
- filter << t->GetDescription() << wxT(" (") << t->GetFileFilter() << wxT(") |")
+ filter << t->GetDescription()
+ << wxT(" (") << t->GetFileFilter() << wxT(") |")
<< t->GetFileFilter();
}
#else
wxString filter = docTemplate->GetFileFilter() ;
#endif
+
wxString defaultDir = docTemplate->GetDirectory();
- if (defaultDir.IsEmpty())
+ if ( defaultDir.empty() )
+ {
defaultDir = wxPathOnly(GetFilename());
+ if ( defaultDir.empty() )
+ defaultDir = GetDocumentManager()->GetLastDirectory();
+ }
- wxString tmp = wxFileSelector(_("Save As"),
+ wxString fileName = wxFileSelector(_("Save As"),
defaultDir,
wxFileNameFromPath(GetFilename()),
docTemplate->GetDefaultExtension(),
wxFD_SAVE | wxFD_OVERWRITE_PROMPT,
GetDocumentWindow());
- if (tmp.empty())
- return false;
+ if (fileName.empty())
+ return false; // cancelled by user
- wxString fileName(tmp);
- wxString path, name, ext;
- wxSplitPath(fileName, & path, & name, & ext);
+ wxString ext;
+ wxFileName::SplitPath(fileName, NULL, NULL, &ext);
if (ext.empty())
{
fileName += docTemplate->GetDefaultExtension();
}
- SetFilename(fileName);
- SetTitle(wxFileNameFromPath(fileName));
-
- // Notify the views that the filename has changed
- wxList::compatibility_iterator node = m_documentViews.GetFirst();
- while (node)
- {
- wxView *view = (wxView *)node->GetData();
- view->OnChangeFilename();
- node = node->GetNext();
- }
-
// Files that were not saved correctly are not added to the FileHistory.
- if (!OnSaveDocument(m_documentFile))
+ if (!OnSaveDocument(fileName))
return false;
- // A file that doesn't use the default extension of its document template cannot be opened
- // via the FileHistory, so we do not add it.
+ SetTitle(wxFileNameFromPath(fileName));
+ SetFilename(fileName, true); // will call OnChangeFileName automatically
+
+ // A file that doesn't use the default extension of its document template
+ // cannot be opened via the FileHistory, so we do not add it.
if (docTemplate->FileMatchesTemplate(fileName))
{
GetDocumentManager()->AddFileToHistory(fileName);
}
- else
- {
- // The user will probably not be able to open the file again, so
- // we could warn about the wrong file-extension here.
- }
+ //else: the user will probably not be able to open the file again, so we
+ // could warn about the wrong file-extension here
+
return true;
}
wxWindow *wxDocument::GetDocumentWindow() const
{
- wxView *view = GetFirstView();
- if (view)
- return view->GetFrame();
- else
- return wxTheApp->GetTopWindow();
+ wxView * const view = GetFirstView();
+
+ return view ? view->GetFrame() : wxTheApp->GetTopWindow();
}
wxCommandProcessor *wxDocument::OnCreateCommandProcessor()
(
wxString::Format
(
- _("Do you want to save changes to document %s?"),
+ _("Do you want to save changes to %s?"),
GetUserReadableName()
),
wxTheApp->GetAppDisplayName(),
- wxYES_NO | wxCANCEL | wxICON_QUESTION,
- GetDocumentWindow()
+ wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE,
+ wxFindSuitableParent()
) )
{
case wxNO:
void wxDocument::SetFilename(const wxString& filename, bool notifyViews)
{
m_documentFile = filename;
+ OnChangeFilename(notifyViews);
+}
+
+void wxDocument::OnChangeFilename(bool notifyViews)
+{
if ( notifyViews )
{
// Notify the views that the filename has changed
bool wxDocument::DoSaveDocument(const wxString& file)
{
- wxString msgTitle;
- if (!wxTheApp->GetAppDisplayName().empty())
- msgTitle = wxTheApp->GetAppDisplayName();
- else
- msgTitle = wxString(_("File error"));
-
#if wxUSE_STD_IOSTREAM
wxSTD ofstream store(file.mb_str(), wxSTD ios::binary);
- if (store.fail() || store.bad())
+ if ( !store )
#else
wxFileOutputStream store(file);
- if (store.GetLastError() != wxSTREAM_NO_ERROR)
+ if ( store.GetLastError() != wxSTREAM_NO_ERROR )
#endif
{
- (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle, wxOK | wxICON_EXCLAMATION,
- GetDocumentWindow());
- // Saving error
+ wxLogError(_("File \"%s\" could not be opened for writing."), file);
return false;
}
+
if (!SaveObject(store))
{
- (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
- GetDocumentWindow());
- // Saving error
+ wxLogError(_("Failed to save document to the file \"%s\"."), file);
return false;
}
{
#if wxUSE_STD_IOSTREAM
wxSTD ifstream store(file.mb_str(), wxSTD ios::binary);
- if (!store.fail() && !store.bad())
+ if ( !store )
#else
wxFileInputStream store(file);
- if (store.GetLastError() == wxSTREAM_NO_ERROR)
+ if (store.GetLastError() != wxSTREAM_NO_ERROR || !store.IsOk())
#endif
{
+ wxLogError(_("File \"%s\" could not be opened for reading."), file);
+ return false;
+ }
+
#if wxUSE_STD_IOSTREAM
- LoadObject(store);
- if ( !!store || store.eof() )
+ LoadObject(store);
+ if ( !store )
#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
- return true;
+ {
+ wxLogError(_("Failed to read document from the file \"%s\"."), file);
+ return false;
}
- wxLogError(_("Sorry, could not open this file."));
- return false;
+ return true;
}
return doc && doc->ProcessEventHere(event);
}
-void wxView::OnActivateView(bool WXUNUSED(activate), wxView *WXUNUSED(activeView), wxView *WXUNUSED(deactiveView))
+void wxView::OnActivateView(bool WXUNUSED(activate),
+ wxView *WXUNUSED(activeView),
+ wxView *WXUNUSED(deactiveView))
{
}
if (doc->OnCreate(path, flags))
return true;
- else
- {
- if (GetDocumentManager()->GetDocuments().Member(doc))
- doc->DeleteAllViews();
- return false;
- }
+
+ if (GetDocumentManager()->GetDocuments().Member(doc))
+ doc->DeleteAllViews();
+ return false;
}
wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags)
{
- wxView *view = DoCreateView();
- if ( view == NULL )
+ wxScopedPtr<wxView> view(DoCreateView());
+ if ( !view )
return NULL;
view->SetDocument(doc);
- if (view->OnCreate(doc, flags))
- {
- return view;
- }
- else
- {
- delete view;
+ if ( !view->OnCreate(doc, flags) )
return NULL;
- }
+
+ return view.release();
}
// The default (very primitive) format detection: check is the extension is
if (!m_docClassInfo)
return NULL;
- return (wxDocument *)m_docClassInfo->CreateObject();
+ return static_cast<wxDocument *>(m_docClassInfo->CreateObject());
}
wxView *wxDocTemplate::DoCreateView()
if (!m_viewClassInfo)
return NULL;
- return (wxView *)m_viewClassInfo->CreateObject();
+ return static_cast<wxView *>(m_viewClassInfo->CreateObject());
}
// ----------------------------------------------------------------------------
// closes the specified document
bool wxDocManager::CloseDocument(wxDocument* doc, bool force)
{
- if (doc->Close() || force)
- {
- // Implicitly deletes the document when
- // the last view is deleted
- doc->DeleteAllViews();
+ if ( !doc->Close() && !force )
+ return false;
- // Check we're really deleted
- if (m_docs.Member(doc))
- delete doc;
+ // Implicitly deletes the document when
+ // the last view is deleted
+ doc->DeleteAllViews();
- return true;
- }
- return false;
+ // Check we're really deleted
+ if (m_docs.Member(doc))
+ delete doc;
+
+ return true;
}
bool wxDocManager::CloseDocuments(bool force)
return true;
}
+wxString wxDocManager::GetLastDirectory() const
+{
+ // use the system-dependent default location for the document files if
+ // we're being opened for the first time
+ if ( m_lastDirectory.empty() )
+ {
+ wxDocManager * const self = const_cast<wxDocManager *>(this);
+ self->m_lastDirectory = wxStandardPaths::Get().GetAppDocumentsDir();
+ }
+
+ return m_lastDirectory;
+}
+
wxFileHistory *wxDocManager::OnCreateFileHistory()
{
return new wxFileHistory;
void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event))
{
- CreateDocument( wxEmptyString, wxDOC_NEW );
+ CreateNewDocument();
}
void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event))
{
- if ( !CreateDocument( wxEmptyString, 0) )
+ if ( !CreateDocument("") )
{
OnOpenFileFailure();
}
void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event))
{
#if wxUSE_PRINTING_ARCHITECTURE
- wxView *view = GetCurrentView();
+ wxView *view = GetActiveView();
if (!view)
return;
void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event))
{
#if wxUSE_PRINTING_ARCHITECTURE
- wxView *view = GetCurrentView();
+ wxView *view = GetActiveView();
if (!view)
return;
if (printout)
{
// Pass two printout objects: for preview, and possible printing.
- wxPrintPreviewBase *preview = new wxPrintPreview(printout, view->OnCreatePrintout());
+ wxPrintPreviewBase *
+ preview = new wxPrintPreview(printout, view->OnCreatePrintout());
if ( !preview->Ok() )
{
delete preview;
- wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
+ wxLogError(_("Print preview creation failed."));
return;
}
- wxPreviewFrame *frame = new wxPreviewFrame(preview, (wxFrame *)wxTheApp->GetTopWindow(), _("Print Preview"),
- wxPoint(100, 100), wxSize(600, 650));
+ wxPreviewFrame *
+ frame = new wxPreviewFrame(preview, wxTheApp->GetTopWindow(),
+ _("Print Preview"));
frame->Centre(wxBOTH);
frame->Initialize();
frame->Show(true);
void wxDocManager::OnUndo(wxCommandEvent& event)
{
- wxDocument *doc = GetCurrentDocument();
- if (!doc)
- return;
- if (doc->GetCommandProcessor())
- doc->GetCommandProcessor()->Undo();
- else
+ wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
+ if ( !cmdproc )
+ {
event.Skip();
+ return;
+ }
+
+ cmdproc->Undo();
}
void wxDocManager::OnRedo(wxCommandEvent& event)
{
- wxDocument *doc = GetCurrentDocument();
- if (!doc)
- return;
- if (doc->GetCommandProcessor())
- doc->GetCommandProcessor()->Redo();
- else
+ wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
+ if ( !cmdproc )
+ {
event.Skip();
+ return;
+ }
+
+ cmdproc->Redo();
}
// Handlers for UI update commands
void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event)
{
- event.Enable( true );
+ // CreateDocument() (which is called from OnFileOpen) may succeed
+ // only when there is at least a template:
+ event.Enable( GetTemplates().GetCount()>0 );
}
void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event)
void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event)
{
- event.Enable( true );
+ // CreateDocument() (which is called from OnFileNew) may succeed
+ // only when there is at least a template:
+ event.Enable( GetTemplates().GetCount()>0 );
}
void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event)
{
- wxDocument *doc = GetCurrentDocument();
- event.Enable( doc && doc->IsModified() );
+ wxDocument * const doc = GetCurrentDocument();
+ event.Enable( doc && !doc->AlreadySaved() );
}
void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event)
{
- wxDocument *doc = GetCurrentDocument();
- if (!doc)
- event.Enable(false);
- else if (!doc->GetCommandProcessor())
- event.Skip();
- else
+ wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
+ if ( !cmdproc )
{
- event.Enable( doc->GetCommandProcessor()->CanUndo() );
- doc->GetCommandProcessor()->SetMenuStrings();
+ event.Enable(false);
+ return;
}
+
+ event.Enable(cmdproc->CanUndo());
+ cmdproc->SetMenuStrings();
}
void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
{
- wxDocument *doc = GetCurrentDocument();
- if (!doc)
- event.Enable(false);
- else if (!doc->GetCommandProcessor())
- event.Skip();
- else
+ wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
+ if ( !cmdproc )
{
- event.Enable( doc->GetCommandProcessor()->CanRedo() );
- doc->GetCommandProcessor()->SetMenuStrings();
+ event.Enable(false);
+ return;
}
+
+ event.Enable(cmdproc->CanRedo());
+ cmdproc->SetMenuStrings();
}
-wxView *wxDocManager::GetCurrentView() const
+wxView *wxDocManager::GetActiveView() const
{
- if (m_currentView)
- return m_currentView;
- if (m_docs.GetCount() == 1)
+ wxView *view = GetCurrentView();
+
+ if ( !view && !m_docs.empty() )
{
- wxDocument* doc = (wxDocument*) m_docs.GetFirst()->GetData();
- return doc->GetFirstView();
+ // if we have exactly one document, consider its view to be the current
+ // one
+ //
+ // VZ: I'm not exactly sure why is this needed but this is how this
+ // code used to behave before the bug #9518 was fixed and it seems
+ // safer to preserve the old logic
+ wxList::compatibility_iterator node = m_docs.GetFirst();
+ if ( !node->GetNext() )
+ {
+ wxDocument *doc = static_cast<wxDocument *>(node->GetData());
+ view = doc->GetFirstView();
+ }
+ //else: we have more than one document
}
- return NULL;
+
+ return view;
}
bool wxDocManager::TryValidator(wxEvent& event)
{
- wxView * const view = GetCurrentView();
+ wxView * const view = GetActiveView();
return view && view->ProcessEventHere(event);
}
wxDocument *wxDocManager::GetCurrentDocument() const
{
- wxView *view = GetCurrentView();
- if (view)
- return view->GetDocument();
- else
- return NULL;
+ wxView * const view = GetActiveView();
+ return view ? view->GetDocument() : NULL;
+}
+
+wxCommandProcessor *wxDocManager::GetCurrentCommandProcessor() const
+{
+ wxDocument * const doc = GetCurrentDocument();
+ return doc ? doc->GetCommandProcessor() : NULL;
}
// Make a default name for a new document
long WXUNUSED(flags),
bool WXUNUSED(save))
{
- // We can only have multiple filters in Windows and GTK
-#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
+#ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
wxString descrBuf;
- int i;
- for (i = 0; i < noTemplates; i++)
+ for (int i = 0; i < noTemplates; i++)
{
if (templates[i]->IsVisible())
{
wxWindow* parent = wxFindSuitableParent();
wxString pathTmp = wxFileSelectorEx(_("Open File"),
- m_lastDirectory,
+ GetLastDirectory(),
wxEmptyString,
&FilterIndex,
descrBuf,
else
msgTitle = wxString(_("File error"));
- (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
- parent);
+ wxMessageBox(_("Sorry, could not open this file."),
+ msgTitle,
+ wxOK | wxICON_EXCLAMATION | wxCENTRE,
+ parent);
path = wxEmptyString;
return NULL;
}
- m_lastDirectory = wxPathOnly(pathTmp);
+
+ SetLastDirectory(wxPathOnly(pathTmp));
path = pathTmp;
theTemplate = FindTemplateForPath(path);
if ( !theTemplate )
{
- // Since we do not add files with non-default extensions to the FileHistory this
- // can only happen if the application changes the allowed templates in runtime.
- (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
- _("Open File"),
- wxOK | wxICON_EXCLAMATION, wxFindSuitableParent());
+ // Since we do not add files with non-default extensions to the
+ // file history this can only happen if the application changes the
+ // allowed templates in runtime.
+ wxMessageBox(_("Sorry, the format for this file is unknown."),
+ _("Open File"),
+ wxOK | wxICON_EXCLAMATION | wxCENTRE,
+ parent);
}
}
else
{
- path = wxEmptyString;
+ path.clear();
}
return theTemplate;
int noTemplates, bool sort)
{
wxArrayString strings;
- wxDocTemplate **data = new wxDocTemplate *[noTemplates];
+ wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]);
int i;
int n = 0;
_("Select a document template"),
_("Templates"),
strings,
- (void **)data,
+ (void **)data.get(),
wxFindSuitableParent()
);
}
- delete[] data;
-
return theTemplate;
}
int noTemplates, bool sort)
{
wxArrayString strings;
- wxDocTemplate **data = new wxDocTemplate *[noTemplates];
+ wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]);
int i;
int n = 0;
_("Select a document view"),
_("Views"),
strings,
- (void **)data,
+ (void **)data.get(),
wxFindSuitableParent()
);
}
- delete[] data;
return theTemplate;
}
void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event)
{
- if (m_childView)
+ if ( !m_childView )
+ return;
+
+ // passing false to Close() means to not delete associated window
+ if ( event.CanVeto() && !m_childView->Close(false) )
{
- bool ans = event.CanVeto()
- ? m_childView->Close(false) // false means don't delete associated window
- : true; // Must delete.
+ event.Veto();
+ return;
+ }
- if (ans)
- {
- m_childView->Activate(false);
- delete m_childView;
- m_childView = NULL;
- m_childDocument = NULL;
+ m_childView->Activate(false);
+ delete m_childView;
+ m_childView = NULL;
+ m_childDocument = NULL;
- this->Destroy();
- }
- else
- event.Veto();
- }
- else
- event.Veto();
+ Destroy();
}
// ----------------------------------------------------------------------------
{
if (m_docManager->Clear(!event.CanVeto()))
{
- this->Destroy();
+ Destroy();
}
else
event.Veto();
return true;
}
-void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
+void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage,
+ int *selPageFrom, int *selPageTo)
{
*minPage = 1;
*maxPage = 1;
{
RemoveFileFromHistory(--numFiles);
}
- else // add a new menu item to all file menus (will be updated below)
+
+ // add a new menu item to all file menus (they will be updated below)
+ for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
+ node;
+ node = node->GetNext() )
{
- for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- node;
- node = node->GetNext() )
- {
- wxMenu * const menu = (wxMenu *)node->GetData();
+ wxMenu * const menu = (wxMenu *)node->GetData();
- if ( !numFiles && menu->GetMenuItemCount() )
- menu->AppendSeparator();
+ if ( !numFiles && menu->GetMenuItemCount() )
+ menu->AppendSeparator();
- // label doesn't matter, it will be set below anyhow, but it can't
- // be empty (this is supposed to indicate a stock item)
- menu->Append(m_idBase + numFiles, " ");
- }
+ // label doesn't matter, it will be set below anyhow, but it can't
+ // be empty (this is supposed to indicate a stock item)
+ menu->Append(m_idBase + numFiles, " ");
}
-
// insert the new file in the beginning of the file history
m_fileHistory.insert(m_fileHistory.begin(), file);
numFiles++;