#include "wx/cmdproc.h"
#include "wx/tokenzr.h"
#include "wx/filename.h"
+#include "wx/stdpaths.h"
#include "wx/vector.h"
-#include "wx/ptr_scpd.h"
+#include "wx/scopedarray.h"
+#include "wx/scopedptr.h"
+#include "wx/except.h"
#if wxUSE_STD_IOSTREAM
#include "wx/ioswrap.h"
IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout, wxPrintout)
#endif
-IMPLEMENT_DYNAMIC_CLASS(wxFileHistory, wxObject)
-
// ============================================================================
// implementation
// ============================================================================
namespace
{
-wxWindow *wxFindSuitableParent()
-{
- wxWindow * const win = wxGetTopLevelParent(wxWindow::FindFocus());
-
- return win ? win : wxTheApp->GetTopWindow();
-}
-
wxString FindExtension(const wxString& path)
{
wxString ext;
return ext.MakeLower();
}
-// return the string used for the MRU list items in the menu
-//
-// NB: the index n is 0-based, as usual, but the strings start from 1
-wxString GetMRUEntryLabel(int n, const wxString& path)
-{
- // we need to quote '&' characters which are used for mnemonics
- wxString pathInMenu(path);
- pathInMenu.Replace("&", "&&");
-
- return wxString::Format("&%d %s", n + 1, pathInMenu);
-}
-
} // anonymous namespace
// ----------------------------------------------------------------------------
wxDocument::~wxDocument()
{
- DeleteContents();
-
- if (m_commandProcessor)
- delete m_commandProcessor;
+ delete m_commandProcessor;
if (GetDocumentManager())
GetDocumentManager()->RemoveDocument(this);
bool wxDocument::Close()
{
- if (OnSaveModified())
- return OnCloseDocument();
- else
+ if ( !OnSaveModified() )
return false;
+
+ return OnCloseDocument();
}
bool wxDocument::OnCloseDocument()
wxView *wxDocument::GetFirstView() const
{
- if (m_documentViews.GetCount() == 0)
+ if ( m_documentViews.empty() )
return NULL;
- return (wxView *)m_documentViews.GetFirst()->GetData();
+
+ return static_cast<wxView *>(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
bool wxDocument::OnNewDocument()
{
- if ( !OnSaveModified() )
- return false;
+ // notice that there is no need to neither reset nor even check the
+ // modified flag here as the document itself is a new object (this is only
+ // called from CreateDocument()) and so it shouldn't be saved anyhow even
+ // if it is modified -- this could happen if the user code creates
+ // documents pre-filled with some user-entered (and which hence must not be
+ // lost) information
- DeleteContents();
- Modify(false);
SetDocumentSaved(false);
const wxString name = GetDocumentManager()->MakeNewDocumentName();
if (!docTemplate)
return false;
-#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
- wxString filter = docTemplate->GetDescription() + wxT(" (") + docTemplate->GetFileFilter() + wxT(")|") + docTemplate->GetFileFilter();
+#ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
+ 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 = docTemplate->GetDocumentManager()->GetTemplates().GetFirst();
+ wxList::compatibility_iterator
+ node = docTemplate->GetDocumentManager()->GetTemplates().GetFirst();
while (node)
{
wxDocTemplate *t = (wxDocTemplate*) node->GetData();
if ( !filter.empty() )
filter << wxT('|');
- filter << t->GetDescription() << wxT(" (") << t->GetFileFilter() << wxT(") |")
+ filter << t->GetDescription()
+ << wxT(" (") << t->GetFileFilter() << wxT(") |")
<< t->GetFileFilter();
}
#else
wxString filter = docTemplate->GetFileFilter() ;
#endif
+
wxString defaultDir = docTemplate->GetDirectory();
- if (defaultDir.IsEmpty())
+ if ( defaultDir.empty() )
+ {
defaultDir = wxPathOnly(GetFilename());
+ if ( defaultDir.empty() )
+ defaultDir = GetDocumentManager()->GetLastDirectory();
+ }
wxString fileName = wxFileSelector(_("Save As"),
defaultDir,
GetDocumentWindow());
if (fileName.empty())
- return false;
-
- wxString ext;
- wxFileName::SplitPath(fileName, NULL, NULL, &ext);
-
- if (ext.empty())
- {
- fileName += wxT(".");
- fileName += docTemplate->GetDefaultExtension();
- }
+ return false; // cancelled by user
// Files that were not saved correctly are not added to the FileHistory.
if (!OnSaveDocument(fileName))
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.
- }
- return true;
+ // 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)
bool wxDocument::OnOpenDocument(const wxString& file)
{
- if ( !OnSaveModified() )
- return false;
+ // notice that there is no need to check the modified flag here for the
+ // reasons explained in OnNewDocument()
if ( !DoOpenDocument(file) )
return false;
SetFilename(file, true);
- Modify(false);
- m_savedYet = true;
+
+ // stretching the logic a little this does make sense because the document
+ // had been saved into the file we just loaded it from, it just could have
+ // happened during a previous program execution, it's just that the name of
+ // this method is a bit unfortunate, it should probably have been called
+ // HasAssociatedFileName()
+ SetDocumentSaved(true);
UpdateAllViews();
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;
}
wxWindow *wxDocument::GetDocumentWindow() const
{
- wxView *view = GetFirstView();
- if (view)
- return view->GetFrame();
- else
- return wxTheApp->GetTopWindow();
+ wxView * const view = GetFirstView();
+
+ return view ? view->GetFrame() : wxTheApp->GetTopWindow();
}
wxCommandProcessor *wxDocument::OnCreateCommandProcessor()
(
wxString::Format
(
- _("Do you want to save changes to document %s?"),
+ _("Do you want to save changes to %s?"),
GetUserReadableName()
),
wxTheApp->GetAppDisplayName(),
- wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE,
- wxFindSuitableParent()
+ wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE
) )
{
case wxNO:
m_viewDocument = NULL;
m_viewFrame = NULL;
+
+ m_docChildFrame = NULL;
}
wxView::~wxView()
{
- GetDocumentManager()->ActivateView(this, false);
- m_viewDocument->RemoveView(this);
+ 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
+ // code being executed and this user code can, for example, show a message
+ // 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::TryValidator(wxEvent& event)
+bool wxView::TryBefore(wxEvent& event)
{
wxDocument * const doc = GetDocument();
- return doc && doc->ProcessEventHere(event);
+ return doc && doc->ProcessEventLocally(event);
}
-void wxView::OnActivateView(bool WXUNUSED(activate), wxView *WXUNUSED(activeView), wxView *WXUNUSED(deactiveView))
+void wxView::OnActivateView(bool WXUNUSED(activate),
+ wxView *WXUNUSED(activeView),
+ wxView *WXUNUSED(deactiveView))
{
}
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)
// Tries to dynamically construct an object of the right class.
wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags)
{
+ // 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();
- // VZ: this code doesn't delete doc if InitDocument() (i.e. doc->OnCreate())
- // fails, is this intentional?
-
return doc && InitDocument(doc, path, flags) ? doc : NULL;
}
if (doc->OnCreate(path, flags))
return true;
- else
- {
- if (GetDocumentManager()->GetDocuments().Member(doc))
- doc->DeleteAllViews();
- return false;
- }
+
+ if (GetDocumentManager()->GetDocuments().Member(doc))
+ doc->DeleteAllViews();
+ return false;
}
wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags)
if (!m_docClassInfo)
return NULL;
- return (wxDocument *)m_docClassInfo->CreateObject();
+ return static_cast<wxDocument *>(m_docClassInfo->CreateObject());
}
wxView *wxDocTemplate::DoCreateView()
if (!m_viewClassInfo)
return NULL;
- return (wxView *)m_viewClassInfo->CreateObject();
+ return static_cast<wxView *>(m_viewClassInfo->CreateObject());
}
// ----------------------------------------------------------------------------
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)
wxDocManager::wxDocManager(long WXUNUSED(flags), bool initialize)
{
- wxASSERT_MSG( !sm_docManager, "multiple wxDocManagers not allowed" );
-
sm_docManager = this;
m_defaultDocumentNameCounter = 1;
// closes the specified document
bool wxDocManager::CloseDocument(wxDocument* doc, bool force)
{
- if (doc->Close() || force)
- {
- // Implicitly deletes the document when
- // the last view is deleted
- doc->DeleteAllViews();
+ if ( !doc->Close() && !force )
+ return false;
- // Check we're really deleted
- if (m_docs.Member(doc))
- delete doc;
+ // Implicitly deletes the document when
+ // the last view is deleted
+ doc->DeleteAllViews();
- return true;
- }
- return false;
+ // Check we're really deleted
+ if (m_docs.Member(doc))
+ delete doc;
+
+ return true;
}
bool wxDocManager::CloseDocuments(bool force)
return true;
}
+wxString wxDocManager::GetLastDirectory() const
+{
+ // 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<wxDocManager *>(this);
+
+ // 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;
+}
+
wxFileHistory *wxDocManager::OnCreateFileHistory()
{
return new wxFileHistory;
void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event))
{
- if ( !CreateDocument( wxEmptyString, 0) )
+ if ( !CreateDocument("") )
{
OnOpenFileFailure();
}
void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event))
{
#if wxUSE_PRINTING_ARCHITECTURE
- wxView *view = GetCurrentView();
+ wxView *view = GetActiveView();
if (!view)
return;
#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
- wxView *view = GetCurrentView();
+ wxBusyCursor busy;
+ wxView *view = GetActiveView();
if (!view)
return;
if (printout)
{
// Pass two printout objects: for preview, and possible printing.
- wxPrintPreviewBase *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.") );
+ wxLogError(_("Print preview creation failed."));
return;
}
- wxPreviewFrame *frame = new wxPreviewFrame(preview, (wxFrame *)wxTheApp->GetTopWindow(), _("Print Preview"),
- wxPoint(100, 100), wxSize(600, 650));
+ 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);
void wxDocManager::OnUndo(wxCommandEvent& event)
{
- wxDocument *doc = GetCurrentDocument();
- if (!doc)
- return;
- if (doc->GetCommandProcessor())
- doc->GetCommandProcessor()->Undo();
- else
+ wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
+ if ( !cmdproc )
+ {
event.Skip();
+ return;
+ }
+
+ cmdproc->Undo();
}
void wxDocManager::OnRedo(wxCommandEvent& event)
{
- wxDocument *doc = GetCurrentDocument();
- if (!doc)
- return;
- if (doc->GetCommandProcessor())
- doc->GetCommandProcessor()->Redo();
- else
+ wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
+ if ( !cmdproc )
+ {
event.Skip();
+ return;
+ }
+
+ cmdproc->Redo();
}
// Handlers for UI update commands
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
void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event)
{
- wxDocument *doc = GetCurrentDocument();
- if (!doc)
- event.Enable(false);
- else if (!doc->GetCommandProcessor())
- event.Skip();
- else
+ wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
+ if ( !cmdproc )
{
- event.Enable( doc->GetCommandProcessor()->CanUndo() );
- doc->GetCommandProcessor()->SetMenuStrings();
+ event.Enable(false);
+ return;
}
+
+ event.Enable(cmdproc->CanUndo());
+ cmdproc->SetMenuStrings();
}
void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
{
- wxDocument *doc = GetCurrentDocument();
- if (!doc)
- event.Enable(false);
- else if (!doc->GetCommandProcessor())
- event.Skip();
- else
+ wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
+ if ( !cmdproc )
{
- event.Enable( doc->GetCommandProcessor()->CanRedo() );
- doc->GetCommandProcessor()->SetMenuStrings();
+ event.Enable(false);
+ return;
}
+
+ event.Enable(cmdproc->CanRedo());
+ cmdproc->SetMenuStrings();
}
-wxView *wxDocManager::GetCurrentView() const
+wxView *wxDocManager::GetActiveView() const
{
- if (m_currentView)
- return m_currentView;
- if (m_docs.GetCount() == 1)
+ wxView *view = GetCurrentView();
+
+ if ( !view && !m_docs.empty() )
{
- wxDocument* doc = (wxDocument*) m_docs.GetFirst()->GetData();
- return doc->GetFirstView();
+ // if we have exactly one document, consider its view to be the current
+ // one
+ //
+ // VZ: I'm not exactly sure why is this needed but this is how this
+ // code used to behave before the bug #9518 was fixed and it seems
+ // safer to preserve the old logic
+ wxList::compatibility_iterator node = m_docs.GetFirst();
+ if ( !node->GetNext() )
+ {
+ wxDocument *doc = static_cast<wxDocument *>(node->GetData());
+ view = doc->GetFirstView();
+ }
+ //else: we have more than one document
}
- return NULL;
+
+ return view;
}
-bool wxDocManager::TryValidator(wxEvent& event)
+bool wxDocManager::TryBefore(wxEvent& event)
{
- wxView * const view = GetCurrentView();
- return view && view->ProcessEventHere(event);
+ wxView * const view = GetActiveView();
+ return view && view->ProcessEventLocally(event);
}
namespace
docNew->SetDocumentName(temp->GetDocumentName());
docNew->SetDocumentTemplate(temp);
- // 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)) )
+ wxTRY
{
- // Document is implicitly deleted by DeleteAllViews
- docNew->DeleteAllViews();
- return NULL;
+ // 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
wxDocument *wxDocManager::GetCurrentDocument() const
{
- wxView *view = GetCurrentView();
- if (view)
- return view->GetDocument();
- else
- return NULL;
+ wxView * const view = GetActiveView();
+ return view ? view->GetDocument() : NULL;
+}
+
+wxCommandProcessor *wxDocManager::GetCurrentCommandProcessor() const
+{
+ wxDocument * const doc = GetCurrentDocument();
+ return doc ? doc->GetCommandProcessor() : NULL;
}
// Make a default name for a new document
long WXUNUSED(flags),
bool WXUNUSED(save))
{
- // We can only have multiple filters in Windows and GTK
-#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
+#ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
wxString descrBuf;
- int i;
- for (i = 0; i < noTemplates; i++)
+ for (int i = 0; i < noTemplates; i++)
{
if (templates[i]->IsVisible())
{
int FilterIndex = -1;
- wxWindow* parent = wxFindSuitableParent();
-
wxString pathTmp = wxFileSelectorEx(_("Open File"),
- m_lastDirectory,
+ GetLastDirectory(),
wxEmptyString,
&FilterIndex,
descrBuf,
- 0,
- parent);
+ wxFD_OPEN | wxFD_FILE_MUST_EXIST);
wxDocTemplate *theTemplate = NULL;
if (!pathTmp.empty())
else
msgTitle = wxString(_("File error"));
- (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION | wxCENTRE,
- parent);
+ wxMessageBox(_("Sorry, could not open this file."),
+ msgTitle,
+ wxOK | wxICON_EXCLAMATION | wxCENTRE);
path = wxEmptyString;
return NULL;
}
- m_lastDirectory = wxPathOnly(pathTmp);
+
+ SetLastDirectory(wxPathOnly(pathTmp));
path = pathTmp;
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 | wxCENTRE, wxFindSuitableParent());
+ // Since we do not add files with non-default extensions to the
+ // file history this can only happen if the application changes the
+ // allowed templates in runtime.
+ wxMessageBox(_("Sorry, the format for this file is unknown."),
+ _("Open File"),
+ wxOK | wxICON_EXCLAMATION | wxCENTRE);
}
}
else
{
- path = wxEmptyString;
+ path.clear();
}
return theTemplate;
int noTemplates, bool sort)
{
wxArrayString strings;
- wxDocTemplate **data = new wxDocTemplate *[noTemplates];
+ wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]);
int i;
int n = 0;
_("Select a document template"),
_("Templates"),
strings,
- (void **)data,
- wxFindSuitableParent()
+ (void **)data.get()
);
}
- delete[] data;
-
return theTemplate;
}
int noTemplates, bool sort)
{
wxArrayString strings;
- wxDocTemplate **data = new wxDocTemplate *[noTemplates];
+ wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]);
int i;
int n = 0;
_("Select a document view"),
_("Views"),
strings,
- (void **)data,
- wxFindSuitableParent()
+ (void **)data.get()
);
}
- delete[] data;
return theTemplate;
}
}
// ----------------------------------------------------------------------------
-// Default document child frame
+// wxDocChildFrameAnyBase
// ----------------------------------------------------------------------------
-BEGIN_EVENT_TABLE(wxDocChildFrame, wxFrame)
- EVT_ACTIVATE(wxDocChildFrame::OnActivate)
- EVT_CLOSE(wxDocChildFrame::OnCloseWindow)
-END_EVENT_TABLE()
-
-wxDocChildFrame::wxDocChildFrame(wxDocument *doc,
- wxView *view,
- wxFrame *frame,
- wxWindowID id,
- const wxString& title,
- const wxPoint& pos,
- const wxSize& size,
- long style,
- const wxString& name)
- : wxFrame(frame, id, title, pos, size, style, name)
-{
- m_childDocument = doc;
- m_childView = view;
- if (view)
- view->SetFrame(this);
-}
-
-bool wxDocChildFrame::TryValidator(wxEvent& event)
-{
- if ( !m_childView )
- return false;
-
- // FIXME: why is this needed here?
- m_childView->Activate(true);
-
- return m_childView->ProcessEventHere(event);
-}
-
-void wxDocChildFrame::OnActivate(wxActivateEvent& event)
-{
- wxFrame::OnActivate(event);
-
- if (m_childView)
- m_childView->Activate(event.GetActive());
-}
-
-void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event)
+bool wxDocChildFrameAnyBase::CloseView(wxCloseEvent& event)
{
- if (m_childView)
+ if ( m_childView )
{
- bool ans = event.CanVeto()
- ? m_childView->Close(false) // false means don't delete associated window
- : true; // Must delete.
-
- if (ans)
+ // 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() )
{
- m_childView->Activate(false);
- delete m_childView;
- m_childView = NULL;
- m_childDocument = NULL;
-
- this->Destroy();
- }
- else
event.Veto();
- }
- else
- event.Veto();
-}
-
-// ----------------------------------------------------------------------------
-// Default parent frame
-// ----------------------------------------------------------------------------
+ return false;
+ }
-BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame)
- EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit)
- EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile)
- EVT_CLOSE(wxDocParentFrame::OnCloseWindow)
-END_EVENT_TABLE()
+ m_childView->Activate(false);
-wxDocParentFrame::wxDocParentFrame()
-{
- m_docManager = NULL;
-}
+ // 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;
+ }
-wxDocParentFrame::wxDocParentFrame(wxDocManager *manager,
- wxFrame *frame,
- wxWindowID id,
- const wxString& title,
- const wxPoint& pos,
- const wxSize& size,
- long style,
- const wxString& name)
- : wxFrame(frame, id, title, pos, size, style, name)
-{
- m_docManager = manager;
-}
+ m_childDocument = NULL;
-bool wxDocParentFrame::Create(wxDocManager *manager,
- wxFrame *frame,
- wxWindowID id,
- const wxString& title,
- const wxPoint& pos,
- const wxSize& size,
- long style,
- const wxString& name)
-{
- m_docManager = manager;
- return base_type::Create(frame, id, title, pos, size, style, name);
+ return true;
}
-void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
-{
- Close();
-}
+// ----------------------------------------------------------------------------
+// wxDocParentFrameAnyBase
+// ----------------------------------------------------------------------------
-void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
+void wxDocParentFrameAnyBase::DoOpenMRUFile(unsigned n)
{
- int n = event.GetId() - wxID_FILE1; // the index in MRU list
wxString filename(m_docManager->GetHistoryFile(n));
if ( filename.empty() )
return;
filename);
}
-// Extend event processing to search the view's event table
-bool wxDocParentFrame::TryValidator(wxEvent& event)
-{
- return m_docManager && m_docManager->ProcessEventHere(event);
-}
-
-// Define the behaviour for the frame closing
-// - must delete all frames except for the main one.
-void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event)
-{
- if (m_docManager->Clear(!event.CanVeto()))
- {
- this->Destroy();
- }
- else
- event.Veto();
-}
-
#if wxUSE_PRINTING_ARCHITECTURE
wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
return true;
}
-void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
+void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage,
+ int *selPageFrom, int *selPageTo)
{
*minPage = 1;
*maxPage = 1;
#endif // wxUSE_PRINTING_ARCHITECTURE
-// ----------------------------------------------------------------------------
-// File history (a.k.a. MRU, most recently used, files list)
-// ----------------------------------------------------------------------------
-
-wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase)
-{
- m_fileMaxFiles = maxFiles;
- m_idBase = idBase;
-}
-
-void wxFileHistory::AddFileToHistory(const wxString& file)
-{
- // check if we don't already have this file
- const wxFileName fnNew(file);
- size_t i,
- numFiles = m_fileHistory.size();
- for ( i = 0; i < numFiles; i++ )
- {
- if ( fnNew == m_fileHistory[i] )
- {
- // we do have it, move it to the top of the history
- RemoveFileFromHistory(i);
- numFiles--;
- break;
- }
- }
-
- // if we already have a full history, delete the one at the end
- if ( numFiles == m_fileMaxFiles )
- {
- RemoveFileFromHistory(--numFiles);
- }
-
- // add a new menu item to all file menus (they will be updated below)
- for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- node;
- node = node->GetNext() )
- {
- wxMenu * const menu = (wxMenu *)node->GetData();
-
- if ( !numFiles && menu->GetMenuItemCount() )
- menu->AppendSeparator();
-
- // label doesn't matter, it will be set below anyhow, but it can't
- // be empty (this is supposed to indicate a stock item)
- menu->Append(m_idBase + numFiles, " ");
- }
-
- // insert the new file in the beginning of the file history
- m_fileHistory.insert(m_fileHistory.begin(), file);
- numFiles++;
-
- // update the labels in all menus
- for ( i = 0; i < numFiles; i++ )
- {
- // if in same directory just show the filename; otherwise the full path
- const wxFileName fnOld(m_fileHistory[i]);
-
- wxString pathInMenu;
- if ( fnOld.GetPath() == fnNew.GetPath() )
- {
- pathInMenu = fnOld.GetFullName();
- }
- else // file in different directory
- {
- // absolute path; could also set relative path
- pathInMenu = m_fileHistory[i];
- }
-
- for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- node;
- node = node->GetNext() )
- {
- wxMenu * const menu = (wxMenu *)node->GetData();
-
- menu->SetLabel(m_idBase + i, GetMRUEntryLabel(i, pathInMenu));
- }
- }
-}
-
-void wxFileHistory::RemoveFileFromHistory(size_t i)
-{
- size_t numFiles = m_fileHistory.size();
- wxCHECK_RET( i < numFiles,
- wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
-
- // delete the element from the array
- m_fileHistory.RemoveAt(i);
- numFiles--;
-
- for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- node;
- node = node->GetNext() )
- {
- wxMenu * const menu = (wxMenu *) node->GetData();
-
- // shift filenames up
- for ( size_t j = i; j < numFiles; j++ )
- {
- menu->SetLabel(m_idBase + j, GetMRUEntryLabel(j, m_fileHistory[j]));
- }
-
- // delete the last menu item which is unused now
- const wxWindowID lastItemId = m_idBase + numFiles;
- if ( menu->FindItem(lastItemId) )
- menu->Delete(lastItemId);
-
- // delete the last separator too if no more files are left
- if ( m_fileHistory.empty() )
- {
- const wxMenuItemList::compatibility_iterator
- nodeLast = menu->GetMenuItems().GetLast();
- if ( nodeLast )
- {
- wxMenuItem * const lastMenuItem = nodeLast->GetData();
- if ( lastMenuItem->IsSeparator() )
- menu->Delete(lastMenuItem);
- }
- //else: menu is empty somehow
- }
- }
-}
-
-void wxFileHistory::UseMenu(wxMenu *menu)
-{
- if ( !m_fileMenus.Member(menu) )
- m_fileMenus.Append(menu);
-}
-
-void wxFileHistory::RemoveMenu(wxMenu *menu)
-{
- m_fileMenus.DeleteObject(menu);
-}
-
-#if wxUSE_CONFIG
-void wxFileHistory::Load(const wxConfigBase& config)
-{
- m_fileHistory.Clear();
-
- wxString buf;
- buf.Printf(wxT("file%d"), 1);
-
- wxString historyFile;
- while ((m_fileHistory.GetCount() < m_fileMaxFiles) &&
- config.Read(buf, &historyFile) && !historyFile.empty())
- {
- m_fileHistory.Add(historyFile);
-
- buf.Printf(wxT("file%d"), (int)m_fileHistory.GetCount()+1);
- historyFile = wxEmptyString;
- }
-
- AddFilesToMenu();
-}
-
-void wxFileHistory::Save(wxConfigBase& config)
-{
- size_t i;
- for (i = 0; i < m_fileMaxFiles; i++)
- {
- wxString buf;
- buf.Printf(wxT("file%d"), (int)i+1);
- if (i < m_fileHistory.GetCount())
- config.Write(buf, wxString(m_fileHistory[i]));
- else
- config.Write(buf, wxEmptyString);
- }
-}
-#endif // wxUSE_CONFIG
-
-void wxFileHistory::AddFilesToMenu()
-{
- if ( m_fileHistory.empty() )
- return;
-
- for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- node;
- node = node->GetNext() )
- {
- AddFilesToMenu((wxMenu *) node->GetData());
- }
-}
-
-void wxFileHistory::AddFilesToMenu(wxMenu* menu)
-{
- if ( m_fileHistory.empty() )
- return;
-
- if ( menu->GetMenuItemCount() )
- menu->AppendSeparator();
-
- for ( size_t i = 0; i < m_fileHistory.GetCount(); i++ )
- {
- menu->Append(m_idBase + i, GetMRUEntryLabel(i, m_fileHistory[i]));
- }
-}
-
// ----------------------------------------------------------------------------
// Permits compatibility with existing file formats and functions that
// manipulate files directly
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;
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;
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;
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;