+ wxDocument *doc = (wxDocument *)GetDocuments().First()->Data();
+ if (doc->Close())
+ {
+ // Implicitly deletes the document when
+ // the last view is deleted
+ doc->DeleteAllViews();
+
+ // Check we're really deleted
+ if (m_docs.Member(doc))
+ delete doc;
+ }
+ else
+ {
+ delete[] templates;
+ return (wxDocument *) NULL;
+ }
+ }
+
+ // New document: user chooses a template, unless there's only one.
+ if (flags & wxDOC_NEW)
+ {
+ if (n == 1)
+ {
+ wxDocTemplate *temp = templates[0];
+ delete[] templates;
+ wxDocument *newDoc = temp->CreateDocument(path, flags);
+ if (newDoc)
+ {
+ newDoc->SetDocumentName(temp->GetDocumentName());
+ newDoc->SetDocumentTemplate(temp);
+ newDoc->OnNewDocument();
+ }
+ return newDoc;
+ }
+
+ wxDocTemplate *temp = SelectDocumentType(templates, n);
+ delete[] templates;
+ if (temp)
+ {
+ wxDocument *newDoc = temp->CreateDocument(path, flags);
+ if (newDoc)
+ {
+ newDoc->SetDocumentName(temp->GetDocumentName());
+ newDoc->SetDocumentTemplate(temp);
+ newDoc->OnNewDocument();
+ }
+ return newDoc;
+ }
+ else
+ return (wxDocument *) NULL;
+ }
+
+ // Existing document
+ wxDocTemplate *temp = (wxDocTemplate *) NULL;
+
+ wxString path2(wxT(""));
+ if (path != wxT(""))
+ path2 = path;
+
+ if (flags & wxDOC_SILENT)
+ temp = FindTemplateForPath(path2);
+ else
+ temp = SelectDocumentPath(templates, n, path2, flags);
+
+ delete[] templates;
+
+ if (temp)
+ {
+ wxDocument *newDoc = temp->CreateDocument(path2, flags);
+ if (newDoc)
+ {
+ newDoc->SetDocumentName(temp->GetDocumentName());
+ newDoc->SetDocumentTemplate(temp);
+ if (!newDoc->OnOpenDocument(path2))
+ {
+ newDoc->DeleteAllViews();
+ // delete newDoc; // Implicitly deleted by DeleteAllViews
+ return (wxDocument *) NULL;
+ }
+ AddFileToHistory(path2);
+ }
+ return newDoc;
+ }
+
+ return (wxDocument *) NULL;
+}
+
+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 *temp = (wxDocTemplate *)(m_templates.Nth(i)->Data());
+ if (temp->IsVisible())
+ {
+ if (temp->GetDocumentName() == doc->GetDocumentName())
+ {
+ templates[n] = temp;
+ n ++;
+ }
+ }