X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b45dfd0a8bfb8ad1199f8a6c28f473aaa86004b5..665b537f51f768e19cff57fa9b351a530d6b2015:/src/common/docview.cpp?ds=sidebyside diff --git a/src/common/docview.cpp b/src/common/docview.cpp index 7fa1b7299d..86736878ff 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -17,10 +17,6 @@ // headers // ---------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "docview.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -183,25 +179,43 @@ bool wxDocument::OnCloseDocument() bool wxDocument::DeleteAllViews() { wxDocManager* manager = GetDocumentManager(); - wxList::iterator it, en; - for ( it = m_documentViews.begin(), en = m_documentViews.end(); - it != en; - ) + // first check if all views agree to be closed + const wxList::iterator end = m_documentViews.end(); + for ( wxList::iterator i = m_documentViews.begin(); i != end; ++i ) { - wxView *view = (wxView *)*it; - if (!view->Close()) + wxView *view = (wxView *)*i; + if ( !view->Close() ) return false; + } - wxList::iterator next = it; ++next; + // all views agreed to close, now do close them + if ( m_documentViews.empty() ) + { + // normally the document would be implicitly deleted when the last view + // is, but if don't have any views, do it here instead + if ( manager && manager->GetDocuments().Member(this) ) + delete this; + } + else // have views + { + // as we delete elements we iterate over, don't use the usual "from + // begin to end" loop + for ( ;; ) + { + wxView *view = (wxView *)*m_documentViews.begin(); + + bool isLastOne = m_documentViews.size() == 1; + + // this always deletes the node implicitly and if this is the last + // view also deletes this object itself (also implicitly, great), + // so we can't test for m_documentViews.empty() after calling this! + delete view; - delete view; // Deletes node implicitly - it = next; + if ( isLastOne ) + break; + } } - // If we haven't yet deleted the document (for example - // if there were no views) then delete it. - if (manager && manager->GetDocuments().Member(this)) - delete this; return true; } @@ -271,7 +285,7 @@ bool wxDocument::SaveAs() t->GetDocClassInfo() == docTemplate->GetDocClassInfo()) { // add a '|' to separate this filter from the previous one - if ( !filter.IsEmpty() ) + if ( !filter.empty() ) filter << wxT('|'); filter << t->GetDescription() << wxT(" (") << t->GetFileFilter() << wxT(") |") @@ -292,14 +306,14 @@ bool wxDocument::SaveAs() wxSAVE | wxOVERWRITE_PROMPT, GetDocumentWindow()); - if (tmp.IsEmpty()) + if (tmp.empty()) return false; wxString fileName(tmp); wxString path, name, ext; wxSplitPath(fileName, & path, & name, & ext); - if (ext.IsEmpty() || ext == wxT("")) + if (ext.empty()) { fileName += wxT("."); fileName += docTemplate->GetDefaultExtension(); @@ -397,12 +411,12 @@ bool wxDocument::Revert() // Get title, or filename if no title, else unnamed bool wxDocument::GetPrintableName(wxString& buf) const { - if (m_documentTitle != wxT("")) + if (!m_documentTitle.empty()) { buf = m_documentTitle; return true; } - else if (m_documentFile != wxT("")) + else if (!m_documentFile.empty()) { buf = wxFileNameFromPath(m_documentFile); return true; @@ -437,7 +451,7 @@ bool wxDocument::OnSaveModified() GetPrintableName(title); wxString msgTitle; - if (wxTheApp->GetAppName() != wxT("")) + if (!wxTheApp->GetAppName().empty()) msgTitle = wxTheApp->GetAppName(); else msgTitle = wxString(_("Warning")); @@ -547,13 +561,13 @@ void wxDocument::SetFilename(const wxString& filename, bool notifyViews) bool wxDocument::DoSaveDocument(const wxString& file) { wxString msgTitle; - if (wxTheApp->GetAppName() != wxT("")) + if (!wxTheApp->GetAppName().empty()) msgTitle = wxTheApp->GetAppName(); else msgTitle = wxString(_("File error")); #if wxUSE_STD_IOSTREAM - wxSTD ofstream store(file.mb_str()); + wxSTD ofstream store(file.mb_str(), wxSTD ios::binary); if (store.fail() || store.bad()) #else wxFileOutputStream store(file); @@ -578,39 +592,26 @@ bool wxDocument::DoSaveDocument(const wxString& file) 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()) + wxSTD ifstream store(file.mb_str(), wxSTD ios::binary); + if (!store.fail() && !store.bad()) #else wxFileInputStream store(file); - if (store.GetLastError() != wxSTREAM_NO_ERROR) + 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() ) + LoadObject(store); + if ( !!store || store.eof() ) #else - int res = LoadObject(store).GetLastError(); - if ((res != wxSTREAM_NO_ERROR) && - (res != wxSTREAM_EOF)) + 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; } - return true; + wxLogError(_("Sorry, could not open this file.")); + return false; } @@ -655,14 +656,18 @@ void wxView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint)) void wxView::OnChangeFilename() { - if (GetFrame() && GetDocument()) - { - wxString title; + // GetFrame can return wxWindow rather than wxTopLevelWindow due to + // generic MDI implementation so use SetLabel rather than SetTitle. + // It should cause SetTitle() for top level windows. + wxWindow *win = GetFrame(); + if (!win) return; - GetDocument()->GetPrintableName(title); + wxDocument *doc = GetDocument(); + if (!doc) return; - GetFrame()->SetTitle(title); - } + wxString name; + doc->GetPrintableName(name); + win->SetLabel(name); } void wxView::SetDocument(wxDocument *doc) @@ -908,6 +913,8 @@ bool wxDocManager::Clear(bool force) if (!CloseDocuments(force)) return false; + m_currentView = NULL; + wxList::compatibility_iterator node = m_templates.GetFirst(); while (node) { @@ -950,12 +957,12 @@ void wxDocManager::OnFileCloseAll(wxCommandEvent& WXUNUSED(event)) void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event)) { - CreateDocument( wxT(""), wxDOC_NEW ); + CreateDocument( wxEmptyString, wxDOC_NEW ); } void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event)) { - if ( !CreateDocument( wxT(""), 0) ) + if ( !CreateDocument( wxEmptyString, 0) ) { OnOpenFileFailure(); } @@ -1205,7 +1212,12 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) { newDoc->SetDocumentName(temp->GetDocumentName()); newDoc->SetDocumentTemplate(temp); - newDoc->OnNewDocument(); + if (!newDoc->OnNewDocument() ) + { + // Document is implicitly deleted by DeleteAllViews + newDoc->DeleteAllViews(); + return NULL; + } } return newDoc; } @@ -1228,7 +1240,12 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) { newDoc->SetDocumentName(temp->GetDocumentName()); newDoc->SetDocumentTemplate(temp); - newDoc->OnNewDocument(); + if (!newDoc->OnNewDocument() ) + { + // Document is implicitly deleted by DeleteAllViews + newDoc->DeleteAllViews(); + return NULL; + } } return newDoc; } @@ -1239,9 +1256,7 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) // Existing document wxDocTemplate *temp; - wxString path2(wxT("")); - if (path != wxT("")) - path2 = path; + wxString path2 = path; if (flags & wxDOC_SILENT) { @@ -1270,6 +1285,28 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) } } + //see if this file is already open + for (size_t i = 0; i < GetDocuments().GetCount(); ++i) + { + wxDocument* currentDoc = (wxDocument*)(GetDocuments().Item(i)->GetData()); +#ifdef __WXMSW__ + //file paths are case-insensitive on Windows + if (path2.CmpNoCase(currentDoc->GetFilename()) == 0) +#else + if (path2.Cmp(currentDoc->GetFilename()) == 0) +#endif + { + //file already open. Just activate it and return + if (currentDoc->GetFirstView()) + { + ActivateView(currentDoc->GetFirstView(), true); + if (currentDoc->GetDocumentWindow()) + currentDoc->GetDocumentWindow()->SetFocus(); + return currentDoc; + } + } + } + wxDocument *newDoc = temp->CreateDocument(path2, flags); if (newDoc) { @@ -1523,7 +1560,7 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, if (templates[i]->IsVisible()) { // add a '|' to separate this filter from the previous one - if ( !descrBuf.IsEmpty() ) + if ( !descrBuf.empty() ) descrBuf << wxT('|'); descrBuf << templates[i]->GetDescription() @@ -1541,19 +1578,19 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, wxString pathTmp = wxFileSelectorEx(_("Select a file"), m_lastDirectory, - wxT(""), + wxEmptyString, &FilterIndex, descrBuf, 0, parent); wxDocTemplate *theTemplate = (wxDocTemplate *)NULL; - if (!pathTmp.IsEmpty()) + if (!pathTmp.empty()) { if (!wxFileExists(pathTmp)) { wxString msgTitle; - if (!wxTheApp->GetAppName().IsEmpty()) + if (!wxTheApp->GetAppName().empty()) msgTitle = wxTheApp->GetAppName(); else msgTitle = wxString(_("File error")); @@ -1561,7 +1598,7 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION, parent); - path = wxT(""); + path = wxEmptyString; return (wxDocTemplate *) NULL; } m_lastDirectory = wxPathOnly(pathTmp); @@ -1585,7 +1622,7 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, } else { - path = wxT(""); + path = wxEmptyString; } return theTemplate; @@ -1898,7 +1935,7 @@ void wxDocParentFrame::OnMRUFile(wxCommandEvent& event) { int n = event.GetId() - wxID_FILE1; // the index in MRU list wxString filename(m_docManager->GetHistoryFile(n)); - if ( !filename.IsEmpty() ) + if ( !filename.empty() ) { // verify that the file exists before doing anything else if ( wxFile::Exists(filename) ) @@ -2128,6 +2165,8 @@ void wxFileHistory::AddFileToHistory(const wxString& file) pathInMenu = m_fileHistory[i]; } + // we need to quote '&' characters which are used for mnemonics + pathInMenu.Replace(_T("&"), _T("&&")); wxString buf; buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str()); wxList::compatibility_iterator node = m_fileMenus.GetFirst(); @@ -2158,20 +2197,20 @@ void wxFileHistory::RemoveFileFromHistory(size_t i) wxList::compatibility_iterator node = m_fileMenus.GetFirst(); while ( node ) { - wxMenu* menu = (wxMenu*) node->GetData(); + wxMenu* menu = (wxMenu*) node->GetData(); - // shuffle filenames up - wxString buf; - for ( j = i; j < m_fileHistoryN - 1; j++ ) - { - buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]); - menu->SetLabel(m_idBase + j, buf); - } + // shuffle filenames up + wxString buf; + for ( j = i; j < m_fileHistoryN - 1; j++ ) + { + buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]); + menu->SetLabel(m_idBase + j, buf); + } - node = node->GetNext(); + node = node->GetNext(); // delete the last menu item which is unused now - wxWindowID lastItemId = m_idBase + m_fileHistoryN - 1; + wxWindowID lastItemId = m_idBase + wx_truncate_cast(wxWindowID, m_fileHistoryN) - 1; if (menu->FindItem(lastItemId)) { menu->Delete(lastItemId); @@ -2180,10 +2219,10 @@ void wxFileHistory::RemoveFileFromHistory(size_t i) // delete the last separator too if no more files are left if ( m_fileHistoryN == 1 ) { - wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetLast(); - if ( node ) + wxMenuItemList::compatibility_iterator nodeLast = menu->GetMenuItems().GetLast(); + if ( nodeLast ) { - wxMenuItem *menuItem = node->GetData(); + wxMenuItem *menuItem = nodeLast->GetData(); if ( menuItem->IsSeparator() ) { menu->Delete(menuItem); @@ -2230,12 +2269,12 @@ void wxFileHistory::Load(wxConfigBase& config) wxString buf; buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1); wxString historyFile; - while ((m_fileHistoryN < m_fileMaxFiles) && config.Read(buf, &historyFile) && (historyFile != wxT(""))) + while ((m_fileHistoryN < m_fileMaxFiles) && config.Read(buf, &historyFile) && (!historyFile.empty())) { m_fileHistory[m_fileHistoryN] = MYcopystring((const wxChar*) historyFile); m_fileHistoryN ++; buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1); - historyFile = wxT(""); + historyFile = wxEmptyString; } AddFilesToMenu(); } @@ -2406,4 +2445,3 @@ bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename) #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM #endif // wxUSE_DOC_VIEW_ARCHITECTURE -