if (!docTemplate)
return FALSE;
+#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
+ 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 = wxDocManager::GetDocumentManager()->GetTemplates().GetFirst();
+ while (node)
+ {
+ wxDocTemplate *t = (wxDocTemplate*) node->GetData();
+
+ if (t->IsVisible() && t != docTemplate &&
+ t->GetViewClassInfo() == docTemplate->GetViewClassInfo() &&
+ t->GetDocClassInfo() == docTemplate->GetDocClassInfo())
+ {
+ // add a '|' to separate this filter from the previous one
+ if ( !filter.IsEmpty() )
+ filter << wxT('|');
+
+ filter << t->GetDescription() << wxT(" (") << t->GetFileFilter() << wxT(") |")
+ << t->GetFileFilter();
+ }
+
+ node = node->GetNext();
+ }
+ }
+#else
+ wxString filter = docTemplate->GetFileFilter() ;
+#endif
wxString tmp = wxFileSelector(_("Save as"),
docTemplate->GetDirectory(),
wxFileNameFromPath(GetFilename()),
docTemplate->GetDefaultExtension(),
- docTemplate->GetFileFilter(),
+ filter,
wxSAVE | wxOVERWRITE_PROMPT,
GetDocumentWindow());
if ( !file )
return FALSE;
- wxString msgTitle;
- if (wxTheApp->GetAppName() != wxT(""))
- msgTitle = wxTheApp->GetAppName();
- else
- msgTitle = wxString(_("File error"));
+ if ( !DoSaveDocument(file) )
+ return FALSE;
-#if wxUSE_STD_IOSTREAM
- wxSTD ofstream store(file.mb_str());
- if (store.fail() || store.bad())
-#else
- wxFileOutputStream store(file);
- if (store.GetLastError() != wxSTREAM_NO_ERROR)
-#endif
- {
- (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle, wxOK | wxICON_EXCLAMATION,
- GetDocumentWindow());
- // Saving error
- return FALSE;
- }
- if (!SaveObject(store))
- {
- (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
- GetDocumentWindow());
- // Saving error
- return FALSE;
- }
Modify(FALSE);
SetFilename(file);
SetDocumentSaved(TRUE);
if (!OnSaveModified())
return FALSE;
- wxString msgTitle;
- if (wxTheApp->GetAppName() != wxT(""))
- msgTitle = wxTheApp->GetAppName();
- else
- msgTitle = wxString(_("File error"));
+ if ( !DoOpenDocument(file) )
+ return FALSE;
-#if wxUSE_STD_IOSTREAM
- wxSTD ifstream store(file.mb_str());
- if (store.fail() || store.bad())
-#else
- wxFileInputStream store(file);
- 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() )
-#else
- 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;
- }
SetFilename(file, TRUE);
Modify(FALSE);
m_savedYet = TRUE;
}
}
+bool wxDocument::DoSaveDocument(const wxString& file)
+{
+ wxString msgTitle;
+ if (wxTheApp->GetAppName() != wxT(""))
+ msgTitle = wxTheApp->GetAppName();
+ else
+ msgTitle = wxString(_("File error"));
+
+#if wxUSE_STD_IOSTREAM
+ wxSTD ofstream store(file.mb_str());
+ if (store.fail() || store.bad())
+#else
+ wxFileOutputStream store(file);
+ if (store.GetLastError() != wxSTREAM_NO_ERROR)
+#endif
+ {
+ (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle, wxOK | wxICON_EXCLAMATION,
+ GetDocumentWindow());
+ // Saving error
+ return FALSE;
+ }
+ if (!SaveObject(store))
+ {
+ (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
+ GetDocumentWindow());
+ // Saving error
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+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())
+#else
+ wxFileInputStream store(file);
+ 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() )
+#else
+ 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;
+}
+
+
// ----------------------------------------------------------------------------
// Document view
// ----------------------------------------------------------------------------
// Tries to dynamically construct an object of the right class.
wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags)
{
- if (!m_docClassInfo)
+ wxDocument *doc = DoCreateDocument();
+ if ( doc == NULL )
+ return (wxDocument *) NULL;
+
+ if (InitDocument(doc, path, flags))
+ {
+ return doc;
+ }
+ else
+ {
return (wxDocument *) NULL;
- wxDocument *doc = (wxDocument *)m_docClassInfo->CreateObject();
+ }
+}
+
+bool wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags)
+{
doc->SetFilename(path);
doc->SetDocumentTemplate(this);
GetDocumentManager()->AddDocument(doc);
doc->SetCommandProcessor(doc->OnCreateCommandProcessor());
if (doc->OnCreate(path, flags))
- return doc;
+ return true;
else
{
if (GetDocumentManager()->GetDocuments().Member(doc))
doc->DeleteAllViews();
- return (wxDocument *) NULL;
+ return false;
}
}
wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags)
{
- if (!m_viewClassInfo)
+ wxView *view = DoCreateView();
+ if ( view == NULL )
return (wxView *) NULL;
- wxView *view = (wxView *)m_viewClassInfo->CreateObject();
+
view->SetDocument(doc);
if (view->OnCreate(doc, flags))
{
return GetDefaultExtension().IsSameAs(FindExtension(path));
}
+wxDocument *wxDocTemplate::DoCreateDocument()
+{
+ if (!m_docClassInfo)
+ return (wxDocument *) NULL;
+
+ return (wxDocument *)m_docClassInfo->CreateObject();
+}
+
+wxView *wxDocTemplate::DoCreateView()
+{
+ if (!m_viewClassInfo)
+ return (wxView *) NULL;
+
+ return (wxView *)m_viewClassInfo->CreateObject();
+}
+
// ----------------------------------------------------------------------------
// wxDocManager
// ----------------------------------------------------------------------------
if (sort)
{
- strings.Sort(wxStringSortAscending);
+ strings.Sort(); // ascending sort
// Yes, this will be slow, but template lists
// are typically short.
int j;
if (sort)
{
- strings.Sort(wxStringSortAscending);
+ strings.Sort(); // ascending sort
// Yes, this will be slow, but template lists
// are typically short.
int j;