+ return templates;
+}
+
+} // anonymous namespace
+
+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));
+ const size_t numTemplates = templates.size();
+ if ( !numTemplates )
+ {
+ // no templates can be used, can't create document
+ return NULL;
+ }
+
+
+ // normally user should select the template to use but wxDOC_SILENT flag we
+ // choose one ourselves
+ wxString path = pathOrig; // may be modified below
+ wxDocTemplate *temp;
+ if ( flags & wxDOC_SILENT )
+ {
+ wxASSERT_MSG( !path.empty(),
+ "using empty path with wxDOC_SILENT doesn't make sense" );
+
+ temp = FindTemplateForPath(path);
+ if ( !temp )
+ {
+ wxLogWarning(_("The format of file '%s' couldn't be determined."),
+ path);
+ }
+ }
+ else // not silent, ask the user
+ {
+ // for the new file we need just the template, for an existing one we
+ // need the template and the path, unless it's already specified
+ if ( (flags & wxDOC_NEW) || !path.empty() )
+ temp = SelectDocumentType(&templates[0], numTemplates);
+ else
+ temp = SelectDocumentPath(&templates[0], numTemplates, path, flags);
+ }
+
+ if ( !temp )
+ return NULL;
+
+ // check whether the document with this path is already opened
+ if ( !path.empty() )
+ {
+ const wxFileName fn(path);
+ for ( wxList::const_iterator i = m_docs.begin(); i != m_docs.end(); ++i )
+ {
+ wxDocument * const doc = (wxDocument*)*i;
+
+ if ( fn == doc->GetFilename() )
+ {
+ // file already open, just activate it and return
+ if ( doc->GetFirstView() )
+ {
+ ActivateView(doc->GetFirstView());
+ if ( doc->GetDocumentWindow() )
+ doc->GetDocumentWindow()->SetFocus();
+ return doc;
+ }
+ }
+ }
+ }
+
+
+ // no, we need to create a new document
+
+
+ // if we've reached the max number of docs, close the first one.
+ if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
+ {
+ if ( !CloseDocument((wxDocument *)GetDocuments().GetFirst()->GetData()) )
+ {
+ // can't open the new document if closing the old one failed
+ return NULL;
+ }
+ }