+
+ wxDocument* docToClose = NULL;
+
+ // If we've reached the max number of docs, close the
+ // first one.
+ if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
+ {
+ wxDocument *doc = (wxDocument *)GetDocuments().GetFirst()->GetData();
+ docToClose = doc;
+ }
+
+ // New document: user chooses a template, unless there's only one.
+ if (flags & wxDOC_NEW)
+ {
+ if (n == 1)
+ {
+ if (docToClose)
+ {
+ if (!CloseDocument(docToClose, FALSE))
+ {
+ delete[] templates;
+ return NULL;
+ }
+ }
+
+ 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)
+ {
+ if (docToClose)
+ {
+ if (!CloseDocument(docToClose, FALSE))
+ {
+ return NULL;
+ }
+ }
+
+ 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);
+ if (!temp)
+ {
+ // 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());
+ }
+ }
+ else
+ temp = SelectDocumentPath(templates, n, path2, flags);
+
+ delete[] templates;
+
+ if (temp)
+ {
+ if (docToClose)
+ {
+ if (!CloseDocument(docToClose, FALSE))
+ {
+ return NULL;
+ }
+ }
+
+ 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;
+ }
+ // 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 (temp->FileMatchesTemplate(path2))
+ AddFileToHistory(path2);
+ }
+ return newDoc;
+ }
+