#include "wx/vector.h"
#include "wx/scopedarray.h"
#include "wx/scopedptr.h"
+#include "wx/except.h"
#if wxUSE_STD_IOSTREAM
#include "wx/ioswrap.h"
namespace
{
-wxWindow *wxFindSuitableParent()
-{
- wxWindow * const win = wxGetTopLevelParent(wxWindow::FindFocus());
-
- return win ? win : wxTheApp->GetTopWindow();
-}
-
wxString FindExtension(const wxString& path)
{
wxString ext;
GetUserReadableName()
),
wxTheApp->GetAppDisplayName(),
- wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE,
- wxFindSuitableParent()
+ wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE
) )
{
case wxNO:
// 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();
// Tries to dynamically construct an object of the right class.
wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags)
{
- wxScopedPtr<wxDocument> 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
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<wxDocManager *>(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
+ wxString lastOpened = GetHistoryFile(0);
+ if ( !lastOpened.empty() )
+ {
+ const wxFileName fn(lastOpened);
+ if ( fn.DirExists() )
+ {
+ self->m_lastDirectory = fn.GetPath();
+ }
+ //else: should we try the next one?
+ }
+
+ // 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;
int FilterIndex = -1;
- wxWindow* parent = wxFindSuitableParent();
-
wxString pathTmp = wxFileSelectorEx(_("Open File"),
GetLastDirectory(),
wxEmptyString,
&FilterIndex,
- descrBuf,
- 0,
- parent);
+ descrBuf);
wxDocTemplate *theTemplate = NULL;
if (!pathTmp.empty())
wxMessageBox(_("Sorry, could not open this file."),
msgTitle,
- wxOK | wxICON_EXCLAMATION | wxCENTRE,
- parent);
+ wxOK | wxICON_EXCLAMATION | wxCENTRE);
path = wxEmptyString;
return NULL;
// 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
_("Select a document template"),
_("Templates"),
strings,
- (void **)data.get(),
- wxFindSuitableParent()
+ (void **)data.get()
);
}
_("Select a document view"),
_("Views"),
strings,
- (void **)data.get(),
- wxFindSuitableParent()
+ (void **)data.get()
);
}
}
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;
}