X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b1d4dd7add411ebff1ce4a8d1d992e0e43ee26b1..b31ce22480aa50a1f018ce62522a2faecba38ea5:/src/common/docview.cpp diff --git a/src/common/docview.cpp b/src/common/docview.cpp index 40e21501fe..767a07f93c 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -5,7 +5,7 @@ // Modified by: // Created: 01/02/97 // RCS-ID: $Id$ -// Copyright: (c) Julian Smart and Markus Holzem +// Copyright: (c) Julian Smart // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -17,7 +17,7 @@ // headers // ---------------------------------------------------------------------------- -#ifdef __GNUG__ +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "docview.h" #endif @@ -43,6 +43,8 @@ #include "wx/log.h" #endif +#include "wx/ffile.h" + #ifdef __WXMAC__ #include "wx/filename.h" #endif @@ -78,7 +80,7 @@ #endif // ---------------------------------------------------------------------------- -// wxWindows macros +// wxWidgets macros // ---------------------------------------------------------------------------- IMPLEMENT_ABSTRACT_CLASS(wxDocument, wxEvtHandler) @@ -99,6 +101,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxFileHistory, wxObject) // ---------------------------------------------------------------------------- static inline wxString FindExtension(const wxChar *path); +static wxWindow* wxFindSuitableParent(void); // ---------------------------------------------------------------------------- // local constants @@ -169,7 +172,7 @@ bool wxDocument::Close() bool wxDocument::OnCloseDocument() { // Tell all views that we're about to close - NotifyClosing(); + NotifyClosing(); DeleteContents(); Modify(FALSE); return TRUE; @@ -180,18 +183,20 @@ bool wxDocument::OnCloseDocument() bool wxDocument::DeleteAllViews() { wxDocManager* manager = GetDocumentManager(); + wxList::iterator it, en; - wxNode *node = m_documentViews.GetFirst(); - while (node) + for ( it = m_documentViews.begin(), en = m_documentViews.end(); + it != en; + ) { - wxView *view = (wxView *)node->GetData(); + wxView *view = (wxView *)*it; if (!view->Close()) return FALSE; - wxNode *next = node->GetNext(); + wxList::iterator next = it; ++next; delete view; // Deletes node implicitly - node = next; + it = next; } // If we haven't yet deleted the document (for example // if there were no views) then delete it. @@ -248,11 +253,42 @@ bool wxDocument::SaveAs() if (!docTemplate) return FALSE; +#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__) + wxString filter = docTemplate->GetDescription() + wxT(" (") + docTemplate->GetFileFilter() + wxT(")|") + docTemplate->GetFileFilter(); + + // Now see if there are some other template with identical view and document + // classes, whose filters may also be used. + + if (docTemplate->GetViewClassInfo() && docTemplate->GetDocClassInfo()) + { + wxList::compatibility_iterator node = wxDocManager::GetDocumentManager()->GetTemplates().GetFirst(); + while (node) + { + wxDocTemplate *t = (wxDocTemplate*) node->GetData(); + + if (t->IsVisible() && t != docTemplate && + t->GetViewClassInfo() == docTemplate->GetViewClassInfo() && + t->GetDocClassInfo() == docTemplate->GetDocClassInfo()) + { + // add a '|' to separate this filter from the previous one + if ( !filter.IsEmpty() ) + filter << wxT('|'); + + filter << t->GetDescription() << wxT(" (") << t->GetFileFilter() << wxT(") |") + << t->GetFileFilter(); + } + + node = node->GetNext(); + } + } +#else + wxString filter = docTemplate->GetFileFilter() ; +#endif wxString tmp = wxFileSelector(_("Save as"), docTemplate->GetDirectory(), wxFileNameFromPath(GetFilename()), docTemplate->GetDefaultExtension(), - docTemplate->GetFileFilter(), + filter, wxSAVE | wxOVERWRITE_PROMPT, GetDocumentWindow()); @@ -272,10 +308,8 @@ bool wxDocument::SaveAs() SetFilename(fileName); SetTitle(wxFileNameFromPath(fileName)); - GetDocumentManager()->AddFileToHistory(fileName); - // Notify the views that the filename has changed - wxNode *node = m_documentViews.GetFirst(); + wxList::compatibility_iterator node = m_documentViews.GetFirst(); while (node) { wxView *view = (wxView *)node->GetData(); @@ -283,7 +317,22 @@ bool wxDocument::SaveAs() node = node->GetNext(); } - return OnSaveDocument(m_documentFile); + // Files that were not saved correctly are not added to the FileHistory. + if (!OnSaveDocument(m_documentFile)) + return FALSE; + + // 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; } bool wxDocument::OnSaveDocument(const wxString& file) @@ -291,32 +340,9 @@ bool wxDocument::OnSaveDocument(const wxString& file) if ( !file ) return FALSE; - wxString msgTitle; - if (wxTheApp->GetAppName() != wxT("")) - msgTitle = wxTheApp->GetAppName(); - else - msgTitle = wxString(_("File error")); + if ( !DoSaveDocument(file) ) + return FALSE; -#if wxUSE_STD_IOSTREAM - wxSTD ofstream store(wxString(file.fn_str()).mb_str()); // ????? - if (store.fail() || store.bad()) -#else - wxFileOutputStream store( file ); - if (store.GetLastError() != wxSTREAM_NO_ERROR) -#endif - { - (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle, wxOK | wxICON_EXCLAMATION, - GetDocumentWindow()); - // Saving error - return FALSE; - } - if (!SaveObject(store)) - { - (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle, wxOK | wxICON_EXCLAMATION, - GetDocumentWindow()); - // Saving error - return FALSE; - } Modify(FALSE); SetFilename(file); SetDocumentSaved(TRUE); @@ -332,37 +358,9 @@ bool wxDocument::OnOpenDocument(const wxString& file) if (!OnSaveModified()) return FALSE; - wxString msgTitle; - if (wxTheApp->GetAppName() != wxT("")) - msgTitle = wxTheApp->GetAppName(); - else - msgTitle = wxString(_("File error")); + if ( !DoOpenDocument(file) ) + return FALSE; -#if wxUSE_STD_IOSTREAM - wxSTD ifstream store(wxString(file.fn_str()).mb_str()); // ???? - if (store.fail() || store.bad()) -#else - wxFileInputStream store( file ); - 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() ) -#else - 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; - } SetFilename(file, TRUE); Modify(FALSE); m_savedYet = TRUE; @@ -509,11 +507,11 @@ void wxDocument::OnChangedViewList() void wxDocument::UpdateAllViews(wxView *sender, wxObject *hint) { - wxNode *node = m_documentViews.GetFirst(); + wxList::compatibility_iterator node = m_documentViews.GetFirst(); while (node) { wxView *view = (wxView *)node->GetData(); - if (view != sender) + if (view != sender) view->OnUpdate(sender, hint); node = node->GetNext(); } @@ -521,7 +519,7 @@ void wxDocument::UpdateAllViews(wxView *sender, wxObject *hint) void wxDocument::NotifyClosing() { - wxNode *node = m_documentViews.GetFirst(); + wxList::compatibility_iterator node = m_documentViews.GetFirst(); while (node) { wxView *view = (wxView *)node->GetData(); @@ -536,7 +534,7 @@ void wxDocument::SetFilename(const wxString& filename, bool notifyViews) if ( notifyViews ) { // Notify the views that the filename has changed - wxNode *node = m_documentViews.GetFirst(); + wxList::compatibility_iterator node = m_documentViews.GetFirst(); while (node) { wxView *view = (wxView *)node->GetData(); @@ -546,22 +544,90 @@ void wxDocument::SetFilename(const wxString& filename, bool notifyViews) } } +bool wxDocument::DoSaveDocument(const wxString& file) +{ + wxString msgTitle; + if (wxTheApp->GetAppName() != wxT("")) + msgTitle = wxTheApp->GetAppName(); + else + msgTitle = wxString(_("File error")); + +#if wxUSE_STD_IOSTREAM + wxSTD ofstream store(file.mb_str()); + if (store.fail() || store.bad()) +#else + wxFileOutputStream store(file); + if (store.GetLastError() != wxSTREAM_NO_ERROR) +#endif + { + (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle, wxOK | wxICON_EXCLAMATION, + GetDocumentWindow()); + // Saving error + return FALSE; + } + if (!SaveObject(store)) + { + (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle, wxOK | wxICON_EXCLAMATION, + GetDocumentWindow()); + // Saving error + return FALSE; + } + + return TRUE; +} + +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()) +#else + wxFileInputStream store(file); + 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() ) +#else + 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; +} + + // ---------------------------------------------------------------------------- // Document view // ---------------------------------------------------------------------------- wxView::wxView() { - // SetDocument(doc); m_viewDocument = (wxDocument*) NULL; - m_viewTypeName = wxT(""); m_viewFrame = (wxFrame *) NULL; } wxView::~wxView() { -// GetDocumentManager()->ActivateView(this, FALSE, TRUE); + GetDocumentManager()->ActivateView(this, FALSE); m_viewDocument->RemoveView(this); } @@ -570,8 +636,8 @@ bool wxView::ProcessEvent(wxEvent& event) { if ( !GetDocument() || !GetDocument()->ProcessEvent(event) ) return wxEvtHandler::ProcessEvent(event); - else - return TRUE; + + return TRUE; } void wxView::OnActivateView(bool WXUNUSED(activate), wxView *WXUNUSED(activeView), wxView *WXUNUSED(deactiveView)) @@ -672,29 +738,43 @@ wxDocTemplate::~wxDocTemplate() // Tries to dynamically construct an object of the right class. wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags) { - if (!m_docClassInfo) + wxDocument *doc = DoCreateDocument(); + if ( doc == NULL ) + return (wxDocument *) NULL; + + if (InitDocument(doc, path, flags)) + { + return doc; + } + else + { return (wxDocument *) NULL; - wxDocument *doc = (wxDocument *)m_docClassInfo->CreateObject(); + } +} + +bool wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags) +{ doc->SetFilename(path); doc->SetDocumentTemplate(this); GetDocumentManager()->AddDocument(doc); doc->SetCommandProcessor(doc->OnCreateCommandProcessor()); if (doc->OnCreate(path, flags)) - return doc; + return true; else { if (GetDocumentManager()->GetDocuments().Member(doc)) doc->DeleteAllViews(); - return (wxDocument *) NULL; + return false; } } wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags) { - if (!m_viewClassInfo) + wxView *view = DoCreateView(); + if ( view == NULL ) return (wxView *) NULL; - wxView *view = (wxView *)m_viewClassInfo->CreateObject(); + view->SetDocument(doc); if (view->OnCreate(doc, flags)) { @@ -714,6 +794,22 @@ bool wxDocTemplate::FileMatchesTemplate(const wxString& path) return GetDefaultExtension().IsSameAs(FindExtension(path)); } +wxDocument *wxDocTemplate::DoCreateDocument() +{ + if (!m_docClassInfo) + return (wxDocument *) NULL; + + return (wxDocument *)m_docClassInfo->CreateObject(); +} + +wxView *wxDocTemplate::DoCreateView() +{ + if (!m_viewClassInfo) + return (wxView *) NULL; + + return (wxView *)m_viewClassInfo->CreateObject(); +} + // ---------------------------------------------------------------------------- // wxDocManager // ---------------------------------------------------------------------------- @@ -772,25 +868,35 @@ wxDocManager::~wxDocManager() sm_docManager = (wxDocManager*) NULL; } -bool wxDocManager::CloseDocuments(bool force) +// closes the specified document +bool wxDocManager::CloseDocument(wxDocument* doc, bool force) { - wxNode *node = m_docs.GetFirst(); - while (node) + if (doc->Close() || force) { - wxDocument *doc = (wxDocument *)node->GetData(); - wxNode *next = node->GetNext(); - - if (!doc->Close() && !force) - return FALSE; - - // Implicitly deletes the document when the last - // view is removed (deleted) + // Implicitly deletes the document when + // the last view is deleted doc->DeleteAllViews(); - // Check document is deleted + // Check we're really deleted if (m_docs.Member(doc)) delete doc; + return TRUE; + } + return FALSE; +} + +bool wxDocManager::CloseDocuments(bool force) +{ + wxList::compatibility_iterator node = m_docs.GetFirst(); + while (node) + { + wxDocument *doc = (wxDocument *)node->GetData(); + wxList::compatibility_iterator next = node->GetNext(); + + if (!CloseDocument(doc, force)) + return FALSE; + // This assumes that documents are not connected in // any way, i.e. deleting one document does NOT // delete another. @@ -804,11 +910,11 @@ bool wxDocManager::Clear(bool force) if (!CloseDocuments(force)) return FALSE; - wxNode *node = m_templates.GetFirst(); + wxList::compatibility_iterator node = m_templates.GetFirst(); while (node) { wxDocTemplate *templ = (wxDocTemplate*) node->GetData(); - wxNode* next = node->GetNext(); + wxList::compatibility_iterator next = node->GetNext(); delete templ; node = next; } @@ -926,8 +1032,13 @@ void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event)) if (printout) { // Pass two printout objects: for preview, and possible printing. - wxPrintPreviewBase *preview = (wxPrintPreviewBase *) NULL; - preview = new wxPrintPreview(printout, view->OnCreatePrintout()); + wxPrintPreviewBase *preview = new wxPrintPreview(printout, view->OnCreatePrintout()); + if ( !preview->Ok() ) + { + delete preview; + wxMessageBox( _("Sorry, print preview needs a printer to be installed.") ); + return; + } wxPreviewFrame *frame = new wxPreviewFrame(preview, (wxFrame *)wxTheApp->GetTopWindow(), _("Print Preview"), wxPoint(100, 100), wxSize(600, 650)); @@ -938,22 +1049,26 @@ void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event)) #endif // wxUSE_PRINTING_ARCHITECTURE } -void wxDocManager::OnUndo(wxCommandEvent& WXUNUSED(event)) +void wxDocManager::OnUndo(wxCommandEvent& event) { wxDocument *doc = GetCurrentDocument(); if (!doc) return; if (doc->GetCommandProcessor()) doc->GetCommandProcessor()->Undo(); + else + event.Skip(); } -void wxDocManager::OnRedo(wxCommandEvent& WXUNUSED(event)) +void wxDocManager::OnRedo(wxCommandEvent& event) { wxDocument *doc = GetCurrentDocument(); if (!doc) return; if (doc->GetCommandProcessor()) doc->GetCommandProcessor()->Redo(); + else + event.Skip(); } // Handlers for UI update commands @@ -995,17 +1110,29 @@ void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent& event) void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event) { wxDocument *doc = GetCurrentDocument(); - event.Enable( (doc && doc->GetCommandProcessor() && doc->GetCommandProcessor()->CanUndo()) ); - if (doc && doc->GetCommandProcessor()) + if (!doc) + event.Enable(FALSE); + else if (!doc->GetCommandProcessor()) + event.Skip(); + else + { + event.Enable( doc->GetCommandProcessor()->CanUndo() ); doc->GetCommandProcessor()->SetMenuStrings(); + } } void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event) { wxDocument *doc = GetCurrentDocument(); - event.Enable( (doc && doc->GetCommandProcessor() && doc->GetCommandProcessor()->CanRedo()) ); - if (doc && doc->GetCommandProcessor()) + if (!doc) + event.Enable(FALSE); + else if (!doc->GetCommandProcessor()) + event.Skip(); + else + { + event.Enable( doc->GetCommandProcessor()->CanRedo() ); doc->GetCommandProcessor()->SetMenuStrings(); + } } void wxDocManager::OnUpdatePrint(wxUpdateUIEvent& event) @@ -1069,26 +1196,14 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) return (wxDocument *) NULL; } + 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(); - if (doc->Close()) - { - // Implicitly deletes the document when - // the last view is deleted - doc->DeleteAllViews(); - - // Check we're really deleted - if (m_docs.Member(doc)) - delete doc; - } - else - { - delete[] templates; - return (wxDocument *) NULL; - } + docToClose = doc; } // New document: user chooses a template, unless there's only one. @@ -1096,9 +1211,19 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) { 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()); @@ -1112,7 +1237,16 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) delete[] templates; if (temp) { + if (docToClose) + { + if (!CloseDocument(docToClose, FALSE)) + { + return NULL; + } + } + wxDocument *newDoc = temp->CreateDocument(path, flags); + if (newDoc) { newDoc->SetDocumentName(temp->GetDocumentName()); @@ -1126,14 +1260,24 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) } // Existing document - wxDocTemplate *temp = (wxDocTemplate *) NULL; + wxDocTemplate *temp; 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); @@ -1141,6 +1285,14 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) if (temp) { + if (docToClose) + { + if (!CloseDocument(docToClose, FALSE)) + { + return NULL; + } + } + wxDocument *newDoc = temp->CreateDocument(path2, flags); if (newDoc) { @@ -1152,7 +1304,11 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) // delete newDoc; // Implicitly deleted by DeleteAllViews return (wxDocument *) NULL; } - AddFileToHistory(path2); + // 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; } @@ -1265,13 +1421,13 @@ void wxDocManager::AddFileToHistory(const wxString& file) m_fileHistory->AddFileToHistory(file); } -void wxDocManager::RemoveFileFromHistory(int i) +void wxDocManager::RemoveFileFromHistory(size_t i) { if (m_fileHistory) m_fileHistory->RemoveFileFromHistory(i); } -wxString wxDocManager::GetHistoryFile(int i) const +wxString wxDocManager::GetHistoryFile(size_t i) const { wxString histFile; @@ -1319,12 +1475,9 @@ void wxDocManager::FileHistoryAddFilesToMenu() m_fileHistory->AddFilesToMenu(); } -int wxDocManager::GetNoHistoryFiles() const +size_t wxDocManager::GetHistoryFilesCount() const { - if (m_fileHistory) - return m_fileHistory->GetNoHistoryFiles(); - else - return 0; + return m_fileHistory ? m_fileHistory->GetCount() : 0; } @@ -1370,9 +1523,8 @@ static wxWindow* wxFindSuitableParent() } // Prompts user to open a file, using file specs in templates. -// How to implement in wxWindows? Must extend the file selector -// dialog or implement own; OR match the extension to the -// template extension. +// Must extend the file selector dialog or implement own; OR +// match the extension to the template extension. wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__) @@ -1445,6 +1597,14 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, theTemplate = templates[FilterIndex]; if ( !theTemplate ) theTemplate = FindTemplateForPath(path); + if ( !theTemplate ) + { + // 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 { @@ -1452,78 +1612,57 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, } return theTemplate; - -#if 0 - // In all other windowing systems, until we have more advanced - // file selectors, we must select the document type (template) first, and - // _then_ pop up the file selector. - wxDocTemplate *temp = SelectDocumentType(templates, noTemplates); - if (!temp) - return (wxDocTemplate *) NULL; - - wxChar *pathTmp = wxFileSelector(_("Select a file"), wxT(""), wxT(""), - temp->GetDefaultExtension(), - temp->GetFileFilter(), - 0, wxTheApp->GetTopWindow()); - - if (pathTmp) - { - path = pathTmp; - return temp; - } - else - return (wxDocTemplate *) NULL; -#endif // 0 } wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates, int noTemplates, bool sort) { - wxArrayString strings(sort); + wxArrayString strings; wxDocTemplate **data = new wxDocTemplate *[noTemplates]; int i; int n = 0; - - for (i = 0; i < noTemplates; i++) - { - if (templates[i]->IsVisible()) - { - int j; + + for (i = 0; i < noTemplates; i++) + { + if (templates[i]->IsVisible()) + { + int j; bool want = TRUE; - for (j = 0; j < n; j++) - { + for (j = 0; j < n; j++) + { //filter out NOT unique documents + view combinations - if ( templates[i]->m_docTypeName == data[j]->m_docTypeName && + if ( templates[i]->m_docTypeName == data[j]->m_docTypeName && templates[i]->m_viewTypeName == data[j]->m_viewTypeName ) want = FALSE; - } + } if ( want ) - { - strings.Add(templates[i]->m_description); - - data[n] = templates[i]; - n ++; - } - } - } // for - - if (sort) - { - // Yes, this will be slow, but template lists - // are typically short. - int j; - n = strings.Count(); - for (i = 0; i < n; i++) - { - for (j = 0; j < noTemplates; j++) - { - if (strings[i] == templates[j]->m_description) - data[i] = templates[j]; - } - } - } + { + strings.Add(templates[i]->m_description); + + data[n] = templates[i]; + n ++; + } + } + } // for + + if (sort) + { + strings.Sort(); // ascending sort + // Yes, this will be slow, but template lists + // are typically short. + int j; + n = strings.Count(); + for (i = 0; i < n; i++) + { + for (j = 0; j < noTemplates; j++) + { + if (strings[i] == templates[j]->m_description) + data[i] = templates[j]; + } + } + } wxDocTemplate *theTemplate; @@ -1559,49 +1698,50 @@ wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates, wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates, int noTemplates, bool sort) { - wxArrayString strings(sort); + wxArrayString strings; wxDocTemplate **data = new wxDocTemplate *[noTemplates]; int i; int n = 0; - + for (i = 0; i < noTemplates; i++) { wxDocTemplate *templ = templates[i]; if ( templ->IsVisible() && !templ->GetViewName().empty() ) { - int j; + int j; bool want = TRUE; - for (j = 0; j < n; j++) - { + for (j = 0; j < n; j++) + { //filter out NOT unique views - if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName ) + if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName ) want = FALSE; - } + } if ( want ) { - strings.Add(templ->m_viewTypeName); - data[n] = templ; - n ++; - } + strings.Add(templ->m_viewTypeName); + data[n] = templ; + n ++; + } } } - if (sort) - { - // Yes, this will be slow, but template lists - // are typically short. - int j; - n = strings.Count(); - for (i = 0; i < n; i++) - { - for (j = 0; j < noTemplates; j++) - { - if (strings[i] == templates[j]->m_viewTypeName) - data[i] = templates[j]; - } - } - } + if (sort) + { + strings.Sort(); // ascending sort + // Yes, this will be slow, but template lists + // are typically short. + int j; + n = strings.Count(); + for (i = 0; i < n; i++) + { + for (j = 0; j < noTemplates; j++) + { + if (strings[i] == templates[j]->m_viewTypeName) + data[i] = templates[j]; + } + } + } wxDocTemplate *theTemplate; @@ -1657,25 +1797,19 @@ void wxDocManager::RemoveDocument(wxDocument *doc) // Views or windows should inform the document manager // when a view is going in or out of focus -void wxDocManager::ActivateView(wxView *view, bool activate, bool WXUNUSED(deleting)) -{ - // If we're deactiving, and if we're not actually deleting the view, then - // don't reset the current view because we may be going to - // a window without a view. - // WHAT DID I MEAN BY THAT EXACTLY? - /* - if (deleting) - { - if (m_currentView == view) - m_currentView = NULL; - } - else - */ - { - if (activate) - m_currentView = view; - else +void wxDocManager::ActivateView(wxView *view, bool activate) +{ + if ( activate ) + { + m_currentView = view; + } + else // deactivate + { + if ( m_currentView == view ) + { + // don't keep stale pointer m_currentView = (wxView *) NULL; + } } } @@ -1739,11 +1873,9 @@ void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event) { if (m_childView) { - bool ans = FALSE; - if (!event.CanVeto()) - ans = TRUE; // Must delete. - else - ans = m_childView->Close(FALSE); // FALSE means don't delete associated window + bool ans = event.CanVeto() + ? m_childView->Close(FALSE) // FALSE means don't delete associated window + : TRUE; // Must delete. if (ans) { @@ -1799,7 +1931,14 @@ void wxDocParentFrame::OnMRUFile(wxCommandEvent& event) if ( wxFile::Exists(filename) ) { // try to open it - (void)m_docManager->CreateDocument(filename, wxDOC_SILENT); + if (!m_docManager->CreateDocument(filename, wxDOC_SILENT)) + { + // remove the file from the MRU list. The user should already be notified. + m_docManager->RemoveFileFromHistory(n); + + wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."), + filename.c_str()); + } } else { @@ -1850,8 +1989,10 @@ bool wxDocPrintout::OnPrintPage(int WXUNUSED(page)) // Get the logical pixels per inch of screen and printer int ppiScreenX, ppiScreenY; GetPPIScreen(&ppiScreenX, &ppiScreenY); + wxUnusedVar(ppiScreenY); int ppiPrinterX, ppiPrinterY; GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); + wxUnusedVar(ppiPrinterY); // This scales the DC so that the printout roughly represents the // the screen scaling. The text point size _should_ be the right size @@ -1866,6 +2007,7 @@ bool wxDocPrintout::OnPrintPage(int WXUNUSED(page)) int w, h; dc->GetSize(&w, &h); GetPageSizePixels(&pageWidth, &pageHeight); + wxUnusedVar(pageHeight); // If printer pageWidth == current DC width, then this doesn't // change. But w might be the preview bitmap width, so scale down. @@ -1906,16 +2048,29 @@ void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, in // File history processor // ---------------------------------------------------------------------------- -wxFileHistory::wxFileHistory(int maxFiles) +static inline wxChar* MYcopystring(const wxString& s) +{ + wxChar* copy = new wxChar[s.length() + 1]; + return wxStrcpy(copy, s.c_str()); +} + +static inline wxChar* MYcopystring(const wxChar* s) +{ + wxChar* copy = new wxChar[wxStrlen(s) + 1]; + return wxStrcpy(copy, s); +} + +wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase) { m_fileMaxFiles = maxFiles; + m_idBase = idBase; m_fileHistoryN = 0; m_fileHistory = new wxChar *[m_fileMaxFiles]; } wxFileHistory::~wxFileHistory() { - int i; + size_t i; for (i = 0; i < m_fileHistoryN; i++) delete[] m_fileHistory[i]; delete[] m_fileHistory; @@ -1924,12 +2079,19 @@ wxFileHistory::~wxFileHistory() // File history management void wxFileHistory::AddFileToHistory(const wxString& file) { - int i; + size_t i; // Check we don't already have this file for (i = 0; i < m_fileHistoryN; i++) { - if ( m_fileHistory[i] && (file == m_fileHistory[i]) ) +#if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names + wxString testString; + if ( m_fileHistory[i] ) + testString = m_fileHistory[i]; + if ( m_fileHistory[i] && ( file.Lower() == testString.Lower() ) ) +#else + if ( m_fileHistory[i] && ( file == m_fileHistory[i] ) ) +#endif { // we do have it, move it to the top of the history RemoveFileFromHistory (i); @@ -1950,13 +2112,15 @@ void wxFileHistory::AddFileToHistory(const wxString& file) // Move existing files (if any) down so we can insert file at beginning. if (m_fileHistoryN < m_fileMaxFiles) { - wxNode* node = m_fileMenus.GetFirst(); + wxList::compatibility_iterator node = m_fileMenus.GetFirst(); while (node) { wxMenu* menu = (wxMenu*) node->GetData(); - if (m_fileHistoryN == 0) + if ( m_fileHistoryN == 0 && menu->GetMenuItemCount() ) + { menu->AppendSeparator(); - menu->Append(wxID_FILE1+m_fileHistoryN, _("[EMPTY]")); + } + menu->Append(m_idBase+m_fileHistoryN, _("[EMPTY]")); node = node->GetNext(); } m_fileHistoryN ++; @@ -1966,7 +2130,7 @@ void wxFileHistory::AddFileToHistory(const wxString& file) { m_fileHistory[i] = m_fileHistory[i-1]; } - m_fileHistory[0] = copystring(file); + m_fileHistory[0] = MYcopystring(file); // this is the directory of the last opened file wxString pathCurrent; @@ -1993,55 +2157,57 @@ void wxFileHistory::AddFileToHistory(const wxString& file) wxString buf; buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str()); - wxNode* node = m_fileMenus.GetFirst(); + wxList::compatibility_iterator node = m_fileMenus.GetFirst(); while (node) { wxMenu* menu = (wxMenu*) node->GetData(); - menu->SetLabel(wxID_FILE1 + i, buf); + menu->SetLabel(m_idBase + i, buf); node = node->GetNext(); } } } } -void wxFileHistory::RemoveFileFromHistory(int i) +void wxFileHistory::RemoveFileFromHistory(size_t i) { wxCHECK_RET( i < m_fileHistoryN, wxT("invalid index in wxFileHistory::RemoveFileFromHistory") ); - // delete the element from the array (could use memmove() too...) - delete [] m_fileHistory[i]; + // delete the element from the array (could use memmove() too...) + delete [] m_fileHistory[i]; - int j; - for ( j = i; j < m_fileHistoryN - 1; j++ ) - { - m_fileHistory[j] = m_fileHistory[j + 1]; - } + size_t j; + for ( j = i; j < m_fileHistoryN - 1; j++ ) + { + m_fileHistory[j] = m_fileHistory[j + 1]; + } - wxNode* node = m_fileMenus.GetFirst(); + 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(wxID_FILE1 + j, buf); - } - - node = node->GetNext(); + node = node->GetNext(); // delete the last menu item which is unused now - if (menu->FindItem(wxID_FILE1 + m_fileHistoryN - 1)) - menu->Delete(wxID_FILE1 + m_fileHistoryN - 1); + wxWindowID lastItemId = m_idBase + m_fileHistoryN - 1; + if (menu->FindItem(lastItemId)) + { + menu->Delete(lastItemId); + } // delete the last separator too if no more files are left if ( m_fileHistoryN == 1 ) { - wxMenuItemList::Node *node = menu->GetMenuItems().GetLast(); + wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetLast(); if ( node ) { wxMenuItem *menuItem = node->GetData(); @@ -2058,7 +2224,7 @@ void wxFileHistory::RemoveFileFromHistory(int i) m_fileHistoryN--; } -wxString wxFileHistory::GetHistoryFile(int i) const +wxString wxFileHistory::GetHistoryFile(size_t i) const { wxString s; if ( i < m_fileHistoryN ) @@ -2089,13 +2255,13 @@ void wxFileHistory::Load(wxConfigBase& config) { m_fileHistoryN = 0; wxString buf; - buf.Printf(wxT("file%d"), m_fileHistoryN+1); + buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1); wxString historyFile; while ((m_fileHistoryN < m_fileMaxFiles) && config.Read(buf, &historyFile) && (historyFile != wxT(""))) { - m_fileHistory[m_fileHistoryN] = copystring((const wxChar*) historyFile); + m_fileHistory[m_fileHistoryN] = MYcopystring((const wxChar*) historyFile); m_fileHistoryN ++; - buf.Printf(wxT("file%d"), m_fileHistoryN+1); + buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1); historyFile = wxT(""); } AddFilesToMenu(); @@ -2103,12 +2269,15 @@ void wxFileHistory::Load(wxConfigBase& config) void wxFileHistory::Save(wxConfigBase& config) { - int i; - for (i = 0; i < m_fileHistoryN; i++) + size_t i; + for (i = 0; i < m_fileMaxFiles; i++) { wxString buf; - buf.Printf(wxT("file%d"), i+1); - config.Write(buf, wxString(m_fileHistory[i])); + buf.Printf(wxT("file%d"), (int)i+1); + if (i < m_fileHistoryN) + config.Write(buf, wxString(m_fileHistory[i])); + else + config.Write(buf, wxEmptyString); } } #endif // wxUSE_CONFIG @@ -2117,19 +2286,23 @@ void wxFileHistory::AddFilesToMenu() { if (m_fileHistoryN > 0) { - wxNode* node = m_fileMenus.GetFirst(); + wxList::compatibility_iterator node = m_fileMenus.GetFirst(); while (node) { wxMenu* menu = (wxMenu*) node->GetData(); - menu->AppendSeparator(); - int i; + if (menu->GetMenuItemCount()) + { + menu->AppendSeparator(); + } + + size_t i; for (i = 0; i < m_fileHistoryN; i++) { if (m_fileHistory[i]) { wxString buf; buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]); - menu->Append(wxID_FILE1+i, buf); + menu->Append(m_idBase+i, buf); } } node = node->GetNext(); @@ -2141,15 +2314,19 @@ void wxFileHistory::AddFilesToMenu(wxMenu* menu) { if (m_fileHistoryN > 0) { - menu->AppendSeparator(); - int i; + if (menu->GetMenuItemCount()) + { + menu->AppendSeparator(); + } + + size_t i; for (i = 0; i < m_fileHistoryN; i++) { if (m_fileHistory[i]) { wxString buf; buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]); - menu->Append(wxID_FILE1+i, buf); + menu->Append(m_idBase+i, buf); } } } @@ -2161,77 +2338,99 @@ void wxFileHistory::AddFilesToMenu(wxMenu* menu) // ---------------------------------------------------------------------------- #if wxUSE_STD_IOSTREAM + bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream) { - FILE *fd1; - int ch; - - if ((fd1 = wxFopen (filename.fn_str(), _T("rb"))) == NULL) + wxFFile file(filename, _T("rb")); + if ( !file.IsOpened() ) return FALSE; - while ((ch = getc (fd1)) != EOF) - stream << (unsigned char)ch; + char buf[4096]; + + size_t nRead; + do + { + nRead = file.Read(buf, WXSIZEOF(buf)); + if ( file.Error() ) + return FALSE; + + stream.write(buf, nRead); + if ( !stream ) + return FALSE; + } + while ( !file.Eof() ); - fclose (fd1); return TRUE; } bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename) { - FILE *fd1; - int ch; - - if ((fd1 = wxFopen (filename.fn_str(), _T("wb"))) == NULL) - { + wxFFile file(filename, _T("wb")); + if ( !file.IsOpened() ) return FALSE; - } - while (!stream.eof()) + char buf[4096]; + do { - ch = stream.get(); - if (!stream.eof()) - putc (ch, fd1); + stream.read(buf, WXSIZEOF(buf)); + if ( !stream.bad() ) // fail may be set on EOF, don't use operator!() + { + if ( !file.Write(buf, stream.gcount()) ) + return FALSE; + } } - fclose (fd1); + while ( !stream.eof() ); + return TRUE; } -#else + +#else // !wxUSE_STD_IOSTREAM + bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream) { - FILE *fd1; - int ch; - - if ((fd1 = wxFopen (filename, wxT("rb"))) == NULL) + wxFFile file(filename, _T("rb")); + if ( !file.IsOpened() ) return FALSE; - while ((ch = getc (fd1)) != EOF) - stream.PutC((char) ch); + char buf[4096]; + + size_t nRead; + do + { + nRead = file.Read(buf, WXSIZEOF(buf)); + if ( file.Error() ) + return FALSE; + + stream.Write(buf, nRead); + if ( !stream ) + return FALSE; + } + while ( !file.Eof() ); - fclose (fd1); return TRUE; } bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename) { - FILE *fd1; - char ch; - - if ((fd1 = wxFopen (filename, wxT("wb"))) == NULL) - { + wxFFile file(filename, _T("wb")); + if ( !file.IsOpened() ) return FALSE; - } - int len = stream.GetSize(); - // TODO: is this the correct test for EOF? - while (stream.TellI() < (len - 1)) + char buf[4096]; + do { - ch = stream.GetC(); - putc (ch, fd1); + stream.Read(buf, WXSIZEOF(buf)); + + const size_t nRead = stream.LastRead(); + if ( !nRead || !file.Write(buf, nRead) ) + return FALSE; } - fclose (fd1); + while ( !stream.Eof() ); + return TRUE; } -#endif + +#endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM #endif // wxUSE_DOC_VIEW_ARCHITECTURE