#include "wx/vector.h"
#include "wx/scopedarray.h"
#include "wx/scopedptr.h"
+#include "wx/scopeguard.h"
#include "wx/except.h"
#if wxUSE_STD_IOSTREAM
#include "wx/wfstream.h"
#endif
-typedef wxVector<wxDocTemplate *> wxDocTemplates;
-
// ----------------------------------------------------------------------------
// wxWidgets macros
// ----------------------------------------------------------------------------
if ( !DoSaveDocument(file) )
return false;
+ if ( m_commandProcessor )
+ m_commandProcessor->MarkAsSaved();
+
Modify(false);
SetFilename(file);
SetDocumentSaved(true);
bool
wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags)
{
+ wxScopeGuard g = wxMakeObjGuard(*doc, &wxDocument::DeleteAllViews);
+
doc->SetFilename(path);
doc->SetDocumentTemplate(this);
GetDocumentManager()->AddDocument(doc);
doc->SetCommandProcessor(doc->OnCreateCommandProcessor());
- if (doc->OnCreate(path, flags))
- return true;
+ if ( !doc->OnCreate(path, flags) )
+ return false;
- if (GetDocumentManager()->GetDocuments().Member(doc))
- doc->DeleteAllViews();
- return false;
+ g.Dismiss(); // no need to call DeleteAllViews() anymore
+
+ return true;
}
wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags)
wxString errMsg; // must contain exactly one "%s" if non-empty
if ( wxFile::Exists(filename) )
{
- // try to open it
- if ( CreateDocument(filename, wxDOC_SILENT) )
- return;
-
- errMsg = _("The file '%s' couldn't be opened.");
+ // Try to open it but don't give an error if it failed: this could be
+ // normal, e.g. because the user cancelled opening it, and we don't
+ // have any useful information to put in the error message anyhow, so
+ // we assume that in case of an error the appropriate message had been
+ // already logged.
+ (void)CreateDocument(filename, wxDOC_SILENT);
}
else // file doesn't exist
{
- errMsg = _("The file '%s' doesn't exist and couldn't be opened.");
+ OnMRUFileNotExist(n, filename);
}
+}
-
- wxASSERT_MSG( !errMsg.empty(), "should have an error message" );
-
+void wxDocManager::OnMRUFileNotExist(unsigned n, const wxString& filename)
+{
// remove the file which we can't open from the MRU list
RemoveFileFromHistory(n);
// and tell the user about it
- wxLogError(errMsg + '\n' +
- _("It has been removed from the most recently used files list."),
+ wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\n"
+ "It has been removed from the most recently used files list."),
filename);
}
wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
if ( !cmdproc )
{
- event.Enable(false);
+ // If we don't have any document at all, the menu item should really be
+ // disabled.
+ if ( !GetCurrentDocument() )
+ event.Enable(false);
+ else // But if we do have it, it might handle wxID_UNDO on its own
+ event.Skip();
return;
}
-
event.Enable(cmdproc->CanUndo());
cmdproc->SetMenuStrings();
}
wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
if ( !cmdproc )
{
- event.Enable(false);
+ // Use same logic as in OnUpdateUndo() above.
+ if ( !GetCurrentDocument() )
+ event.Enable(false);
+ else
+ event.Skip();
return;
}
-
event.Enable(cmdproc->CanRedo());
cmdproc->SetMenuStrings();
}
{
// helper function: return only the visible templates
-wxDocTemplates GetVisibleTemplates(const wxList& allTemplates)
+wxDocTemplateVector GetVisibleTemplates(const wxList& allTemplates)
{
// select only the visible templates
const size_t totalNumTemplates = allTemplates.GetCount();
- wxDocTemplates templates;
+ wxDocTemplateVector templates;
if ( totalNumTemplates )
{
templates.reserve(totalNumTemplates);
view->Activate(true);
if ( wxWindow *win = view->GetFrame() )
- win->SetFocus();
+ win->Raise();
}
wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags)
// this ought to be const but SelectDocumentType/Path() are not
// const-correct and can't be changed as, being virtual, this risks
// breaking user code overriding them
- wxDocTemplates templates(GetVisibleTemplates(m_templates));
+ wxDocTemplateVector templates(GetVisibleTemplates(m_templates));
const size_t numTemplates = templates.size();
if ( !numTemplates )
{
wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
{
- wxDocTemplates templates(GetVisibleTemplates(m_templates));
+ wxDocTemplateVector templates(GetVisibleTemplates(m_templates));
const size_t numTemplates = templates.size();
if ( numTemplates == 0 )