+
+
+ // 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;
+ }
+ }
+
+
+ // do create and initialize the new document finally
+ wxDocument * const docNew = temp->CreateDocument(path, flags);
+ if ( !docNew )
+ return NULL;
+
+ docNew->SetDocumentName(temp->GetDocumentName());
+ docNew->SetDocumentTemplate(temp);
+
+ wxTRY
+ {
+ // call the appropriate function depending on whether we're creating a
+ // new file or opening an existing one
+ if ( !(flags & wxDOC_NEW ? docNew->OnNewDocument()
+ : docNew->OnOpenDocument(path)) )
+ {
+ docNew->DeleteAllViews();
+ return NULL;
+ }
+ }
+ wxCATCH_ALL( docNew->DeleteAllViews(); throw; )
+
+ // add the successfully opened file to MRU, but only if we're going to be
+ // able to reopen it successfully later which requires the template for
+ // this document to be retrievable from the file extension
+ if ( !(flags & wxDOC_NEW) && temp->FileMatchesTemplate(path) )
+ AddFileToHistory(path);
+
+ return docNew;