X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3a393f8a52d4bfdc5ba9354cf973a36020b46045..852febd80ec724502cf2c6bea5ad898eb78c4dda:/src/common/docview.cpp diff --git a/src/common/docview.cpp b/src/common/docview.cpp index 5a16a9c826..635a2529bb 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -105,13 +105,6 @@ IMPLEMENT_DYNAMIC_CLASS(wxFileHistory, wxObject) namespace { -wxWindow *wxFindSuitableParent() -{ - wxWindow * const win = wxGetTopLevelParent(wxWindow::FindFocus()); - - return win ? win : wxTheApp->GetTopWindow(); -} - wxString FindExtension(const wxString& path) { wxString ext; @@ -240,6 +233,18 @@ wxView *wxDocument::GetFirstView() const return static_cast(m_documentViews.GetFirst()->GetData()); } +void wxDocument::Modify(bool mod) +{ + if (mod != m_documentModified) + { + m_documentModified = mod; + + // Allow views to append asterix to the title + wxView* view = GetFirstView(); + if (view) view->OnChangeFilename(); + } +} + wxDocManager *wxDocument::GetDocumentManager() const { return m_documentTemplate ? m_documentTemplate->GetDocumentManager() : NULL; @@ -334,15 +339,6 @@ bool wxDocument::SaveAs() if (fileName.empty()) return false; // cancelled by user - wxString ext; - wxFileName::SplitPath(fileName, NULL, NULL, &ext); - - if (ext.empty()) - { - fileName += wxT("."); - fileName += docTemplate->GetDefaultExtension(); - } - // Files that were not saved correctly are not added to the FileHistory. if (!OnSaveDocument(fileName)) return false; @@ -350,16 +346,16 @@ bool wxDocument::SaveAs() SetTitle(wxFileNameFromPath(fileName)); SetFilename(fileName, true); // will call OnChangeFileName automatically - // 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 (docTemplate->FileMatchesTemplate(fileName)) - { - GetDocumentManager()->AddFileToHistory(fileName); - } - //else: the user will probably not be able to open the file again, so we - // could warn about the wrong file-extension here + // 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 (docTemplate->FileMatchesTemplate(fileName)) + { + GetDocumentManager()->AddFileToHistory(fileName); + } + //else: the user will probably not be able to open the file again, so we + // could warn about the wrong file-extension here - return true; + return true; } bool wxDocument::OnSaveDocument(const wxString& file) @@ -422,7 +418,22 @@ wxOutputStream& wxDocument::SaveObject(wxOutputStream& stream) bool wxDocument::Revert() { - return false; + if ( wxMessageBox + ( + _("Discard changes and reload the last saved version?"), + wxTheApp->GetAppDisplayName(), + wxYES_NO | wxCANCEL | wxICON_QUESTION, + GetDocumentWindow() + ) != wxYES ) + return false; + + if ( !DoOpenDocument(GetFilename()) ) + return false; + + Modify(false); + UpdateAllViews(); + + return true; } @@ -489,8 +500,7 @@ bool wxDocument::OnSaveModified() GetUserReadableName() ), wxTheApp->GetAppDisplayName(), - wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE, - wxFindSuitableParent() + wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE ) ) { case wxNO: @@ -656,7 +666,8 @@ wxView::wxView() wxView::~wxView() { - GetDocumentManager()->ActivateView(this, false); + if (m_viewDocument && GetDocumentManager()) + GetDocumentManager()->ActivateView(this, false); // reset our frame view first, before removing it from the document as // SetView(NULL) is a simple call while RemoveView() may result in user @@ -664,12 +675,31 @@ wxView::~wxView() // box which would result in an activation event for m_docChildFrame and so // could reactivate the view being destroyed -- unless we reset it first if ( m_docChildFrame && m_docChildFrame->GetView() == this ) + { + // prevent it from doing anything with us m_docChildFrame->SetView(NULL); + // it doesn't make sense to leave the frame alive if its associated + // view doesn't exist any more so unconditionally close it as well + // + // notice that we only get here if m_docChildFrame is non-NULL in the + // first place and it will be always NULL if we're deleted because our + // frame was closed, so this only catches the case of directly deleting + // the view, as it happens if its creation fails in wxDocTemplate:: + // CreateView() for example + m_docChildFrame->GetWindow()->Destroy(); + } + if ( m_viewDocument ) m_viewDocument->RemoveView(this); } +void wxView::SetDocChildFrame(wxDocChildFrameAnyBase *docChildFrame) +{ + SetFrame(docChildFrame ? docChildFrame->GetWindow() : NULL); + m_docChildFrame = docChildFrame; +} + bool wxView::TryBefore(wxEvent& event) { wxDocument * const doc = GetDocument(); @@ -702,7 +732,12 @@ void wxView::OnChangeFilename() wxDocument *doc = GetDocument(); if (!doc) return; - win->SetLabel(doc->GetUserReadableName()); + wxString label = doc->GetUserReadableName(); + if (doc->IsModified()) + { + label += "*"; + } + win->SetLabel(label); } void wxView::SetDocument(wxDocument *doc) @@ -775,9 +810,16 @@ wxDocTemplate::~wxDocTemplate() // Tries to dynamically construct an object of the right class. wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags) { - wxScopedPtr doc(DoCreateDocument()); + // InitDocument() is supposed to delete the document object if its + // initialization fails so don't use wxScopedPtr<> here: this is fragile + // but unavoidable because the default implementation uses CreateView() + // which may -- or not -- create a wxView and if it does create it and its + // initialization fails then the view destructor will delete the document + // (via RemoveView()) and as we can't distinguish between the two cases we + // just have to assume that it always deletes it in case of failure + wxDocument * const doc = DoCreateDocument(); - return doc && InitDocument(doc.get(), path, flags) ? doc.release() : NULL; + return doc && InitDocument(doc, path, flags) ? doc : NULL; } bool @@ -861,7 +903,7 @@ BEGIN_EVENT_TABLE(wxDocManager, wxEvtHandler) EVT_UPDATE_UI(wxID_OPEN, wxDocManager::OnUpdateFileOpen) EVT_UPDATE_UI(wxID_CLOSE, wxDocManager::OnUpdateDisableIfNoDoc) EVT_UPDATE_UI(wxID_CLOSE_ALL, wxDocManager::OnUpdateDisableIfNoDoc) - EVT_UPDATE_UI(wxID_REVERT, wxDocManager::OnUpdateDisableIfNoDoc) + EVT_UPDATE_UI(wxID_REVERT, wxDocManager::OnUpdateFileRevert) EVT_UPDATE_UI(wxID_NEW, wxDocManager::OnUpdateFileNew) EVT_UPDATE_UI(wxID_SAVE, wxDocManager::OnUpdateFileSave) EVT_UPDATE_UI(wxID_SAVEAS, wxDocManager::OnUpdateDisableIfNoDoc) @@ -881,8 +923,6 @@ wxDocManager* wxDocManager::sm_docManager = NULL; wxDocManager::wxDocManager(long WXUNUSED(flags), bool initialize) { - wxASSERT_MSG( !sm_docManager, "multiple wxDocManagers not allowed" ); - sm_docManager = this; m_defaultDocumentNameCounter = 1; @@ -962,12 +1002,35 @@ bool wxDocManager::Initialize() wxString wxDocManager::GetLastDirectory() const { - // use the system-dependent default location for the document files if - // we're being opened for the first time + // if we haven't determined the last used directory yet, do it now if ( m_lastDirectory.empty() ) { + // we're going to modify m_lastDirectory in this const method, so do it + // via non-const self pointer instead of const this one wxDocManager * const self = const_cast(this); - self->m_lastDirectory = wxStandardPaths::Get().GetAppDocumentsDir(); + + // first try to reuse the directory of the most recently opened file: + // this ensures that if the user opens a file, closes the program and + // runs it again the "Open file" dialog will open in the directory of + // the last file he used + if ( m_fileHistory && m_fileHistory->GetCount() ) + { + const wxString lastOpened = m_fileHistory->GetHistoryFile(0); + const wxFileName fn(lastOpened); + if ( fn.DirExists() ) + { + self->m_lastDirectory = fn.GetPath(); + } + //else: should we try the next one? + } + //else: no history yet + + // if we don't have any files in the history (yet?), use the + // system-dependent default location for the document files + if ( m_lastDirectory.empty() ) + { + self->m_lastDirectory = wxStandardPaths::Get().GetAppDocumentsDir(); + } } return m_lastDirectory; @@ -1051,9 +1114,19 @@ void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event)) #endif // wxUSE_PRINTING_ARCHITECTURE } +#if wxUSE_PRINTING_ARCHITECTURE +wxPreviewFrame* wxDocManager::CreatePreviewFrame(wxPrintPreviewBase* preview, + wxWindow *parent, + const wxString& title) +{ + return new wxPreviewFrame(preview, parent, title); +} +#endif // wxUSE_PRINTING_ARCHITECTURE + void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event)) { #if wxUSE_PRINTING_ARCHITECTURE + wxBusyCursor busy; wxView *view = GetActiveView(); if (!view) return; @@ -1071,9 +1144,11 @@ void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event)) return; } - wxPreviewFrame * - frame = new wxPreviewFrame(preview, wxTheApp->GetTopWindow(), - _("Print Preview")); + wxPreviewFrame* frame = CreatePreviewFrame(preview, + wxTheApp->GetTopWindow(), + _("Print Preview")); + wxCHECK_RET( frame, "should create a print preview frame" ); + frame->Centre(wxBOTH); frame->Initialize(); frame->Show(true); @@ -1119,6 +1194,12 @@ void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event) event.Enable( GetCurrentDocument() != NULL ); } +void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent& event) +{ + wxDocument* doc = GetCurrentDocument(); + event.Enable(doc && doc->IsModified() && doc->GetDocumentSaved()); +} + void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event) { // CreateDocument() (which is called from OnFileNew) may succeed @@ -1540,15 +1621,12 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, int FilterIndex = -1; - wxWindow* parent = wxFindSuitableParent(); - wxString pathTmp = wxFileSelectorEx(_("Open File"), GetLastDirectory(), wxEmptyString, &FilterIndex, descrBuf, - 0, - parent); + wxFD_OPEN | wxFD_FILE_MUST_EXIST); wxDocTemplate *theTemplate = NULL; if (!pathTmp.empty()) @@ -1563,8 +1641,7 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, wxMessageBox(_("Sorry, could not open this file."), msgTitle, - wxOK | wxICON_EXCLAMATION | wxCENTRE, - parent); + wxOK | wxICON_EXCLAMATION | wxCENTRE); path = wxEmptyString; return NULL; @@ -1587,8 +1664,7 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, // allowed templates in runtime. wxMessageBox(_("Sorry, the format for this file is unknown."), _("Open File"), - wxOK | wxICON_EXCLAMATION | wxCENTRE, - parent); + wxOK | wxICON_EXCLAMATION | wxCENTRE); } } else @@ -1670,8 +1746,7 @@ wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates, _("Select a document template"), _("Templates"), strings, - (void **)data.get(), - wxFindSuitableParent() + (void **)data.get() ); } @@ -1745,8 +1820,7 @@ wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates, _("Select a document view"), _("Views"), strings, - (void **)data.get(), - wxFindSuitableParent() + (void **)data.get() ); } @@ -1803,13 +1877,22 @@ bool wxDocChildFrameAnyBase::CloseView(wxCloseEvent& event) { if ( m_childView ) { - if ( event.CanVeto() && !m_childView->Close(false) ) + // notice that we must call wxView::Close() and OnClose() called from + // it in any case, even if we know that we are going to close anyhow + if ( !m_childView->Close(false) && event.CanVeto() ) { event.Veto(); return false; } m_childView->Activate(false); + + // it is important to reset m_childView frame pointer to NULL before + // deleting it because while normally it is the frame which deletes the + // view when it's closed, the view also closes the frame if it is + // deleted directly not by us as indicated by its doc child frame + // pointer still being set + m_childView->SetDocChildFrame(NULL); delete m_childView; m_childView = NULL; } @@ -2196,7 +2279,11 @@ void wxFileHistory::AddFilesToMenu(wxMenu* menu) bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream) { - wxFFile file(filename, _T("rb")); +#if wxUSE_FFILE + wxFFile file(filename, wxT("rb")); +#elif wxUSE_FILE + wxFile file(filename, wxFile::read); +#endif if ( !file.IsOpened() ) return false; @@ -2220,7 +2307,11 @@ bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream) bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename) { - wxFFile file(filename, _T("wb")); +#if wxUSE_FFILE + wxFFile file(filename, wxT("wb")); +#elif wxUSE_FILE + wxFile file(filename, wxFile::write); +#endif if ( !file.IsOpened() ) return false; @@ -2243,7 +2334,11 @@ bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename) bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream) { - wxFFile file(filename, _T("rb")); +#if wxUSE_FFILE + wxFFile file(filename, wxT("rb")); +#elif wxUSE_FILE + wxFile file(filename, wxFile::read); +#endif if ( !file.IsOpened() ) return false; @@ -2267,7 +2362,11 @@ bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream) bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename) { - wxFFile file(filename, _T("wb")); +#if wxUSE_FFILE + wxFFile file(filename, wxT("wb")); +#elif wxUSE_FILE + wxFile file(filename, wxFile::write); +#endif if ( !file.IsOpened() ) return false;