1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/docview.cpp
3 // Purpose: Document/view classes
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_DOC_VIEW_ARCHITECTURE
29 #include "wx/docview.h"
33 #include "wx/string.h"
37 #include "wx/dialog.h"
39 #include "wx/filedlg.h"
42 #include "wx/msgdlg.h"
44 #include "wx/choicdlg.h"
47 #if wxUSE_PRINTING_ARCHITECTURE
48 #include "wx/prntbase.h"
49 #include "wx/printdlg.h"
52 #include "wx/confbase.h"
53 #include "wx/filename.h"
56 #include "wx/cmdproc.h"
57 #include "wx/tokenzr.h"
58 #include "wx/filename.h"
59 #include "wx/stdpaths.h"
60 #include "wx/vector.h"
61 #include "wx/scopedarray.h"
62 #include "wx/scopedptr.h"
63 #include "wx/except.h"
65 #if wxUSE_STD_IOSTREAM
66 #include "wx/ioswrap.h"
67 #include "wx/beforestd.h"
73 #include "wx/afterstd.h"
75 #include "wx/wfstream.h"
78 typedef wxVector
<wxDocTemplate
*> wxDocTemplates
;
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
85 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
86 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
87 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
88 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
89 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
91 #if wxUSE_PRINTING_ARCHITECTURE
92 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
95 // ============================================================================
97 // ============================================================================
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
106 wxString
FindExtension(const wxString
& path
)
109 wxFileName::SplitPath(path
, NULL
, NULL
, &ext
);
111 // VZ: extensions are considered not case sensitive - is this really a good
113 return ext
.MakeLower();
116 } // anonymous namespace
118 // ----------------------------------------------------------------------------
119 // Definition of wxDocument
120 // ----------------------------------------------------------------------------
122 wxDocument::wxDocument(wxDocument
*parent
)
124 m_documentModified
= false;
125 m_documentParent
= parent
;
126 m_documentTemplate
= NULL
;
127 m_commandProcessor
= NULL
;
131 bool wxDocument::DeleteContents()
136 wxDocument::~wxDocument()
140 delete m_commandProcessor
;
142 if (GetDocumentManager())
143 GetDocumentManager()->RemoveDocument(this);
145 // Not safe to do here, since it'll invoke virtual view functions
146 // expecting to see valid derived objects: and by the time we get here,
147 // we've called destructors higher up.
151 bool wxDocument::Close()
153 if ( !OnSaveModified() )
156 return OnCloseDocument();
159 bool wxDocument::OnCloseDocument()
161 // Tell all views that we're about to close
168 // Note that this implicitly deletes the document when the last view is
170 bool wxDocument::DeleteAllViews()
172 wxDocManager
* manager
= GetDocumentManager();
174 // first check if all views agree to be closed
175 const wxList::iterator end
= m_documentViews
.end();
176 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
178 wxView
*view
= (wxView
*)*i
;
179 if ( !view
->Close() )
183 // all views agreed to close, now do close them
184 if ( m_documentViews
.empty() )
186 // normally the document would be implicitly deleted when the last view
187 // is, but if don't have any views, do it here instead
188 if ( manager
&& manager
->GetDocuments().Member(this) )
193 // as we delete elements we iterate over, don't use the usual "from
194 // begin to end" loop
197 wxView
*view
= (wxView
*)*m_documentViews
.begin();
199 bool isLastOne
= m_documentViews
.size() == 1;
201 // this always deletes the node implicitly and if this is the last
202 // view also deletes this object itself (also implicitly, great),
203 // so we can't test for m_documentViews.empty() after calling this!
214 wxView
*wxDocument::GetFirstView() const
216 if ( m_documentViews
.empty() )
219 return static_cast<wxView
*>(m_documentViews
.GetFirst()->GetData());
222 void wxDocument::Modify(bool mod
)
224 if (mod
!= m_documentModified
)
226 m_documentModified
= mod
;
228 // Allow views to append asterix to the title
229 wxView
* view
= GetFirstView();
230 if (view
) view
->OnChangeFilename();
234 wxDocManager
*wxDocument::GetDocumentManager() const
236 return m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : NULL
;
239 bool wxDocument::OnNewDocument()
241 // notice that there is no need to neither reset nor even check the
242 // modified flag here as the document itself is a new object (this is only
243 // called from CreateDocument()) and so it shouldn't be saved anyhow even
244 // if it is modified -- this could happen if the user code creates
245 // documents pre-filled with some user-entered (and which hence must not be
248 SetDocumentSaved(false);
250 const wxString name
= GetDocumentManager()->MakeNewDocumentName();
252 SetFilename(name
, true);
257 bool wxDocument::Save()
259 if ( AlreadySaved() )
262 if ( m_documentFile
.empty() || !m_savedYet
)
265 return OnSaveDocument(m_documentFile
);
268 bool wxDocument::SaveAs()
270 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
274 #ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
275 wxString filter
= docTemplate
->GetDescription() + wxT(" (") +
276 docTemplate
->GetFileFilter() + wxT(")|") +
277 docTemplate
->GetFileFilter();
279 // Now see if there are some other template with identical view and document
280 // classes, whose filters may also be used.
281 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
283 wxList::compatibility_iterator
284 node
= docTemplate
->GetDocumentManager()->GetTemplates().GetFirst();
287 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
289 if (t
->IsVisible() && t
!= docTemplate
&&
290 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
291 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
293 // add a '|' to separate this filter from the previous one
294 if ( !filter
.empty() )
297 filter
<< t
->GetDescription()
298 << wxT(" (") << t
->GetFileFilter() << wxT(") |")
299 << t
->GetFileFilter();
302 node
= node
->GetNext();
306 wxString filter
= docTemplate
->GetFileFilter() ;
309 wxString defaultDir
= docTemplate
->GetDirectory();
310 if ( defaultDir
.empty() )
312 defaultDir
= wxPathOnly(GetFilename());
313 if ( defaultDir
.empty() )
314 defaultDir
= GetDocumentManager()->GetLastDirectory();
317 wxString fileName
= wxFileSelector(_("Save As"),
319 wxFileNameFromPath(GetFilename()),
320 docTemplate
->GetDefaultExtension(),
322 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
323 GetDocumentWindow());
325 if (fileName
.empty())
326 return false; // cancelled by user
328 // Files that were not saved correctly are not added to the FileHistory.
329 if (!OnSaveDocument(fileName
))
332 SetTitle(wxFileNameFromPath(fileName
));
333 SetFilename(fileName
, true); // will call OnChangeFileName automatically
335 // A file that doesn't use the default extension of its document template
336 // cannot be opened via the FileHistory, so we do not add it.
337 if (docTemplate
->FileMatchesTemplate(fileName
))
339 GetDocumentManager()->AddFileToHistory(fileName
);
341 //else: the user will probably not be able to open the file again, so we
342 // could warn about the wrong file-extension here
347 bool wxDocument::OnSaveDocument(const wxString
& file
)
352 if ( !DoSaveDocument(file
) )
357 SetDocumentSaved(true);
358 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
359 wxFileName
fn(file
) ;
360 fn
.MacSetDefaultTypeAndCreator() ;
365 bool wxDocument::OnOpenDocument(const wxString
& file
)
367 // notice that there is no need to check the modified flag here for the
368 // reasons explained in OnNewDocument()
370 if ( !DoOpenDocument(file
) )
373 SetFilename(file
, true);
375 // stretching the logic a little this does make sense because the document
376 // had been saved into the file we just loaded it from, it just could have
377 // happened during a previous program execution, it's just that the name of
378 // this method is a bit unfortunate, it should probably have been called
379 // HasAssociatedFileName()
380 SetDocumentSaved(true);
387 #if wxUSE_STD_IOSTREAM
388 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
390 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
396 #if wxUSE_STD_IOSTREAM
397 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
399 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
405 bool wxDocument::Revert()
409 _("Discard changes and reload the last saved version?"),
410 wxTheApp
->GetAppDisplayName(),
411 wxYES_NO
| wxCANCEL
| wxICON_QUESTION
,
416 if ( !DoOpenDocument(GetFilename()) )
426 // Get title, or filename if no title, else unnamed
427 #if WXWIN_COMPATIBILITY_2_8
428 bool wxDocument::GetPrintableName(wxString
& buf
) const
430 // this function can not only be overridden by the user code but also
431 // called by it so we need to ensure that we return the same thing as
432 // GetUserReadableName() but we can't call it because this would result in
433 // an infinite recursion, hence we use the helper DoGetUserReadableName()
434 buf
= DoGetUserReadableName();
438 #endif // WXWIN_COMPATIBILITY_2_8
440 wxString
wxDocument::GetUserReadableName() const
442 #if WXWIN_COMPATIBILITY_2_8
443 // we need to call the old virtual function to ensure that the overridden
444 // version of it is still called
446 if ( GetPrintableName(name
) )
448 #endif // WXWIN_COMPATIBILITY_2_8
450 return DoGetUserReadableName();
453 wxString
wxDocument::DoGetUserReadableName() const
455 if ( !m_documentTitle
.empty() )
456 return m_documentTitle
;
458 if ( !m_documentFile
.empty() )
459 return wxFileNameFromPath(m_documentFile
);
464 wxWindow
*wxDocument::GetDocumentWindow() const
466 wxView
* const view
= GetFirstView();
468 return view
? view
->GetFrame() : wxTheApp
->GetTopWindow();
471 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
473 return new wxCommandProcessor
;
476 // true if safe to close
477 bool wxDocument::OnSaveModified()
481 switch ( wxMessageBox
485 _("Do you want to save changes to %s?"),
486 GetUserReadableName()
488 wxTheApp
->GetAppDisplayName(),
489 wxYES_NO
| wxCANCEL
| wxICON_QUESTION
| wxCENTRE
507 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
512 bool wxDocument::AddView(wxView
*view
)
514 if ( !m_documentViews
.Member(view
) )
516 m_documentViews
.Append(view
);
522 bool wxDocument::RemoveView(wxView
*view
)
524 (void)m_documentViews
.DeleteObject(view
);
529 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
531 return GetDocumentTemplate()->CreateView(this, flags
) != NULL
;
534 // Called after a view is added or removed.
535 // The default implementation deletes the document if
536 // there are no more views.
537 void wxDocument::OnChangedViewList()
539 if ( m_documentViews
.empty() && OnSaveModified() )
543 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
545 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
548 wxView
*view
= (wxView
*)node
->GetData();
550 view
->OnUpdate(sender
, hint
);
551 node
= node
->GetNext();
555 void wxDocument::NotifyClosing()
557 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
560 wxView
*view
= (wxView
*)node
->GetData();
561 view
->OnClosingDocument();
562 node
= node
->GetNext();
566 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
568 m_documentFile
= filename
;
569 OnChangeFilename(notifyViews
);
572 void wxDocument::OnChangeFilename(bool notifyViews
)
576 // Notify the views that the filename has changed
577 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
580 wxView
*view
= (wxView
*)node
->GetData();
581 view
->OnChangeFilename();
582 node
= node
->GetNext();
587 bool wxDocument::DoSaveDocument(const wxString
& file
)
589 #if wxUSE_STD_IOSTREAM
590 wxSTD ofstream
store(file
.mb_str(), wxSTD
ios::binary
);
593 wxFileOutputStream
store(file
);
594 if ( store
.GetLastError() != wxSTREAM_NO_ERROR
)
597 wxLogError(_("File \"%s\" could not be opened for writing."), file
);
601 if (!SaveObject(store
))
603 wxLogError(_("Failed to save document to the file \"%s\"."), file
);
610 bool wxDocument::DoOpenDocument(const wxString
& file
)
612 #if wxUSE_STD_IOSTREAM
613 wxSTD ifstream
store(file
.mb_str(), wxSTD
ios::binary
);
616 wxFileInputStream
store(file
);
617 if (store
.GetLastError() != wxSTREAM_NO_ERROR
|| !store
.IsOk())
620 wxLogError(_("File \"%s\" could not be opened for reading."), file
);
624 #if wxUSE_STD_IOSTREAM
628 int res
= LoadObject(store
).GetLastError();
629 if ( res
!= wxSTREAM_NO_ERROR
&& res
!= wxSTREAM_EOF
)
632 wxLogError(_("Failed to read document from the file \"%s\"."), file
);
640 // ----------------------------------------------------------------------------
642 // ----------------------------------------------------------------------------
646 m_viewDocument
= NULL
;
650 m_docChildFrame
= NULL
;
655 if (m_viewDocument
&& GetDocumentManager())
656 GetDocumentManager()->ActivateView(this, false);
658 // reset our frame view first, before removing it from the document as
659 // SetView(NULL) is a simple call while RemoveView() may result in user
660 // code being executed and this user code can, for example, show a message
661 // box which would result in an activation event for m_docChildFrame and so
662 // could reactivate the view being destroyed -- unless we reset it first
663 if ( m_docChildFrame
&& m_docChildFrame
->GetView() == this )
665 // prevent it from doing anything with us
666 m_docChildFrame
->SetView(NULL
);
668 // it doesn't make sense to leave the frame alive if its associated
669 // view doesn't exist any more so unconditionally close it as well
671 // notice that we only get here if m_docChildFrame is non-NULL in the
672 // first place and it will be always NULL if we're deleted because our
673 // frame was closed, so this only catches the case of directly deleting
674 // the view, as it happens if its creation fails in wxDocTemplate::
675 // CreateView() for example
676 m_docChildFrame
->GetWindow()->Destroy();
679 if ( m_viewDocument
)
680 m_viewDocument
->RemoveView(this);
683 void wxView::SetDocChildFrame(wxDocChildFrameAnyBase
*docChildFrame
)
685 SetFrame(docChildFrame
? docChildFrame
->GetWindow() : NULL
);
686 m_docChildFrame
= docChildFrame
;
689 bool wxView::TryBefore(wxEvent
& event
)
691 wxDocument
* const doc
= GetDocument();
692 return doc
&& doc
->ProcessEventHere(event
);
695 void wxView::OnActivateView(bool WXUNUSED(activate
),
696 wxView
*WXUNUSED(activeView
),
697 wxView
*WXUNUSED(deactiveView
))
701 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
706 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
710 void wxView::OnChangeFilename()
712 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
713 // generic MDI implementation so use SetLabel rather than SetTitle.
714 // It should cause SetTitle() for top level windows.
715 wxWindow
*win
= GetFrame();
718 wxDocument
*doc
= GetDocument();
721 wxString label
= doc
->GetUserReadableName();
722 if (doc
->IsModified())
726 win
->SetLabel(label
);
729 void wxView::SetDocument(wxDocument
*doc
)
731 m_viewDocument
= doc
;
736 bool wxView::Close(bool deleteWindow
)
738 return OnClose(deleteWindow
);
741 void wxView::Activate(bool activate
)
743 if (GetDocument() && GetDocumentManager())
745 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
746 GetDocumentManager()->ActivateView(this, activate
);
750 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
752 return GetDocument() ? GetDocument()->Close() : true;
755 #if wxUSE_PRINTING_ARCHITECTURE
756 wxPrintout
*wxView::OnCreatePrintout()
758 return new wxDocPrintout(this);
760 #endif // wxUSE_PRINTING_ARCHITECTURE
762 // ----------------------------------------------------------------------------
764 // ----------------------------------------------------------------------------
766 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
767 const wxString
& descr
,
768 const wxString
& filter
,
771 const wxString
& docTypeName
,
772 const wxString
& viewTypeName
,
773 wxClassInfo
*docClassInfo
,
774 wxClassInfo
*viewClassInfo
,
777 m_documentManager
= manager
;
778 m_description
= descr
;
781 m_fileFilter
= filter
;
783 m_docTypeName
= docTypeName
;
784 m_viewTypeName
= viewTypeName
;
785 m_documentManager
->AssociateTemplate(this);
787 m_docClassInfo
= docClassInfo
;
788 m_viewClassInfo
= viewClassInfo
;
791 wxDocTemplate::~wxDocTemplate()
793 m_documentManager
->DisassociateTemplate(this);
796 // Tries to dynamically construct an object of the right class.
797 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
799 // InitDocument() is supposed to delete the document object if its
800 // initialization fails so don't use wxScopedPtr<> here: this is fragile
801 // but unavoidable because the default implementation uses CreateView()
802 // which may -- or not -- create a wxView and if it does create it and its
803 // initialization fails then the view destructor will delete the document
804 // (via RemoveView()) and as we can't distinguish between the two cases we
805 // just have to assume that it always deletes it in case of failure
806 wxDocument
* const doc
= DoCreateDocument();
808 return doc
&& InitDocument(doc
, path
, flags
) ? doc
: NULL
;
812 wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
814 doc
->SetFilename(path
);
815 doc
->SetDocumentTemplate(this);
816 GetDocumentManager()->AddDocument(doc
);
817 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
819 if (doc
->OnCreate(path
, flags
))
822 if (GetDocumentManager()->GetDocuments().Member(doc
))
823 doc
->DeleteAllViews();
827 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
829 wxScopedPtr
<wxView
> view(DoCreateView());
833 view
->SetDocument(doc
);
834 if ( !view
->OnCreate(doc
, flags
) )
837 return view
.release();
840 // The default (very primitive) format detection: check is the extension is
841 // that of the template
842 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
844 wxStringTokenizer
parser (GetFileFilter(), wxT(";"));
845 wxString anything
= wxT ("*");
846 while (parser
.HasMoreTokens())
848 wxString filter
= parser
.GetNextToken();
849 wxString filterExt
= FindExtension (filter
);
850 if ( filter
.IsSameAs (anything
) ||
851 filterExt
.IsSameAs (anything
) ||
852 filterExt
.IsSameAs (FindExtension (path
)) )
855 return GetDefaultExtension().IsSameAs(FindExtension(path
));
858 wxDocument
*wxDocTemplate::DoCreateDocument()
863 return static_cast<wxDocument
*>(m_docClassInfo
->CreateObject());
866 wxView
*wxDocTemplate::DoCreateView()
868 if (!m_viewClassInfo
)
871 return static_cast<wxView
*>(m_viewClassInfo
->CreateObject());
874 // ----------------------------------------------------------------------------
876 // ----------------------------------------------------------------------------
878 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
879 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
880 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
881 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
882 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
883 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
884 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
885 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
886 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
887 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
889 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
890 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateDisableIfNoDoc
)
891 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateDisableIfNoDoc
)
892 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
893 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
894 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
895 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateDisableIfNoDoc
)
896 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
897 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
899 #if wxUSE_PRINTING_ARCHITECTURE
900 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
901 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
903 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdateDisableIfNoDoc
)
904 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdateDisableIfNoDoc
)
908 wxDocManager
* wxDocManager::sm_docManager
= NULL
;
910 wxDocManager::wxDocManager(long WXUNUSED(flags
), bool initialize
)
912 sm_docManager
= this;
914 m_defaultDocumentNameCounter
= 1;
915 m_currentView
= NULL
;
916 m_maxDocsOpen
= INT_MAX
;
917 m_fileHistory
= NULL
;
922 wxDocManager::~wxDocManager()
925 delete m_fileHistory
;
926 sm_docManager
= NULL
;
929 // closes the specified document
930 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
932 if ( !doc
->Close() && !force
)
935 // Implicitly deletes the document when
936 // the last view is deleted
937 doc
->DeleteAllViews();
939 // Check we're really deleted
940 if (m_docs
.Member(doc
))
946 bool wxDocManager::CloseDocuments(bool force
)
948 wxList::compatibility_iterator node
= m_docs
.GetFirst();
951 wxDocument
*doc
= (wxDocument
*)node
->GetData();
952 wxList::compatibility_iterator next
= node
->GetNext();
954 if (!CloseDocument(doc
, force
))
957 // This assumes that documents are not connected in
958 // any way, i.e. deleting one document does NOT
965 bool wxDocManager::Clear(bool force
)
967 if (!CloseDocuments(force
))
970 m_currentView
= NULL
;
972 wxList::compatibility_iterator node
= m_templates
.GetFirst();
975 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
976 wxList::compatibility_iterator next
= node
->GetNext();
983 bool wxDocManager::Initialize()
985 m_fileHistory
= OnCreateFileHistory();
989 wxString
wxDocManager::GetLastDirectory() const
991 // if we haven't determined the last used directory yet, do it now
992 if ( m_lastDirectory
.empty() )
994 // we're going to modify m_lastDirectory in this const method, so do it
995 // via non-const self pointer instead of const this one
996 wxDocManager
* const self
= const_cast<wxDocManager
*>(this);
998 // first try to reuse the directory of the most recently opened file:
999 // this ensures that if the user opens a file, closes the program and
1000 // runs it again the "Open file" dialog will open in the directory of
1001 // the last file he used
1002 if ( m_fileHistory
&& m_fileHistory
->GetCount() )
1004 const wxString lastOpened
= m_fileHistory
->GetHistoryFile(0);
1005 const wxFileName
fn(lastOpened
);
1006 if ( fn
.DirExists() )
1008 self
->m_lastDirectory
= fn
.GetPath();
1010 //else: should we try the next one?
1012 //else: no history yet
1014 // if we don't have any files in the history (yet?), use the
1015 // system-dependent default location for the document files
1016 if ( m_lastDirectory
.empty() )
1018 self
->m_lastDirectory
= wxStandardPaths::Get().GetAppDocumentsDir();
1022 return m_lastDirectory
;
1025 wxFileHistory
*wxDocManager::OnCreateFileHistory()
1027 return new wxFileHistory
;
1030 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
1032 wxDocument
*doc
= GetCurrentDocument();
1037 doc
->DeleteAllViews();
1038 if (m_docs
.Member(doc
))
1043 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
1045 CloseDocuments(false);
1048 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
1050 CreateNewDocument();
1053 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
1055 if ( !CreateDocument("") )
1057 OnOpenFileFailure();
1061 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
1063 wxDocument
*doc
= GetCurrentDocument();
1069 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1071 wxDocument
*doc
= GetCurrentDocument();
1077 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
1079 wxDocument
*doc
= GetCurrentDocument();
1085 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
1087 #if wxUSE_PRINTING_ARCHITECTURE
1088 wxView
*view
= GetActiveView();
1092 wxPrintout
*printout
= view
->OnCreatePrintout();
1096 printer
.Print(view
->GetFrame(), printout
, true);
1100 #endif // wxUSE_PRINTING_ARCHITECTURE
1103 #if wxUSE_PRINTING_ARCHITECTURE
1104 wxPreviewFrame
* wxDocManager::CreatePreviewFrame(wxPrintPreviewBase
* preview
,
1106 const wxString
& title
)
1108 return new wxPreviewFrame(preview
, parent
, title
);
1110 #endif // wxUSE_PRINTING_ARCHITECTURE
1112 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1114 #if wxUSE_PRINTING_ARCHITECTURE
1116 wxView
*view
= GetActiveView();
1120 wxPrintout
*printout
= view
->OnCreatePrintout();
1123 // Pass two printout objects: for preview, and possible printing.
1124 wxPrintPreviewBase
*
1125 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1126 if ( !preview
->Ok() )
1129 wxLogError(_("Print preview creation failed."));
1133 wxPreviewFrame
* frame
= CreatePreviewFrame(preview
,
1134 wxTheApp
->GetTopWindow(),
1135 _("Print Preview"));
1136 wxCHECK_RET( frame
, "should create a print preview frame" );
1138 frame
->Centre(wxBOTH
);
1139 frame
->Initialize();
1142 #endif // wxUSE_PRINTING_ARCHITECTURE
1145 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1147 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1157 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1159 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1169 // Handlers for UI update commands
1171 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1173 // CreateDocument() (which is called from OnFileOpen) may succeed
1174 // only when there is at least a template:
1175 event
.Enable( GetTemplates().GetCount()>0 );
1178 void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent
& event
)
1180 event
.Enable( GetCurrentDocument() != NULL
);
1183 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1185 wxDocument
* doc
= GetCurrentDocument();
1186 event
.Enable(doc
&& doc
->IsModified() && doc
->GetDocumentSaved());
1189 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1191 // CreateDocument() (which is called from OnFileNew) may succeed
1192 // only when there is at least a template:
1193 event
.Enable( GetTemplates().GetCount()>0 );
1196 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1198 wxDocument
* const doc
= GetCurrentDocument();
1199 event
.Enable( doc
&& !doc
->AlreadySaved() );
1202 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1204 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1207 event
.Enable(false);
1211 event
.Enable(cmdproc
->CanUndo());
1212 cmdproc
->SetMenuStrings();
1215 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1217 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1220 event
.Enable(false);
1224 event
.Enable(cmdproc
->CanRedo());
1225 cmdproc
->SetMenuStrings();
1228 wxView
*wxDocManager::GetActiveView() const
1230 wxView
*view
= GetCurrentView();
1232 if ( !view
&& !m_docs
.empty() )
1234 // if we have exactly one document, consider its view to be the current
1237 // VZ: I'm not exactly sure why is this needed but this is how this
1238 // code used to behave before the bug #9518 was fixed and it seems
1239 // safer to preserve the old logic
1240 wxList::compatibility_iterator node
= m_docs
.GetFirst();
1241 if ( !node
->GetNext() )
1243 wxDocument
*doc
= static_cast<wxDocument
*>(node
->GetData());
1244 view
= doc
->GetFirstView();
1246 //else: we have more than one document
1252 bool wxDocManager::TryBefore(wxEvent
& event
)
1254 wxView
* const view
= GetActiveView();
1255 return view
&& view
->ProcessEventHere(event
);
1261 // helper function: return only the visible templates
1262 wxDocTemplates
GetVisibleTemplates(const wxList
& allTemplates
)
1264 // select only the visible templates
1265 const size_t totalNumTemplates
= allTemplates
.GetCount();
1266 wxDocTemplates templates
;
1267 if ( totalNumTemplates
)
1269 templates
.reserve(totalNumTemplates
);
1271 for ( wxList::const_iterator i
= allTemplates
.begin(),
1272 end
= allTemplates
.end();
1276 wxDocTemplate
* const temp
= (wxDocTemplate
*)*i
;
1277 if ( temp
->IsVisible() )
1278 templates
.push_back(temp
);
1285 } // anonymous namespace
1287 wxDocument
*wxDocManager::CreateDocument(const wxString
& pathOrig
, long flags
)
1289 // this ought to be const but SelectDocumentType/Path() are not
1290 // const-correct and can't be changed as, being virtual, this risks
1291 // breaking user code overriding them
1292 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1293 const size_t numTemplates
= templates
.size();
1294 if ( !numTemplates
)
1296 // no templates can be used, can't create document
1301 // normally user should select the template to use but wxDOC_SILENT flag we
1302 // choose one ourselves
1303 wxString path
= pathOrig
; // may be modified below
1304 wxDocTemplate
*temp
;
1305 if ( flags
& wxDOC_SILENT
)
1307 wxASSERT_MSG( !path
.empty(),
1308 "using empty path with wxDOC_SILENT doesn't make sense" );
1310 temp
= FindTemplateForPath(path
);
1313 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1317 else // not silent, ask the user
1319 // for the new file we need just the template, for an existing one we
1320 // need the template and the path, unless it's already specified
1321 if ( (flags
& wxDOC_NEW
) || !path
.empty() )
1322 temp
= SelectDocumentType(&templates
[0], numTemplates
);
1324 temp
= SelectDocumentPath(&templates
[0], numTemplates
, path
, flags
);
1330 // check whether the document with this path is already opened
1331 if ( !path
.empty() )
1333 const wxFileName
fn(path
);
1334 for ( wxList::const_iterator i
= m_docs
.begin(); i
!= m_docs
.end(); ++i
)
1336 wxDocument
* const doc
= (wxDocument
*)*i
;
1338 if ( fn
== doc
->GetFilename() )
1340 // file already open, just activate it and return
1341 if ( doc
->GetFirstView() )
1343 ActivateView(doc
->GetFirstView());
1344 if ( doc
->GetDocumentWindow() )
1345 doc
->GetDocumentWindow()->SetFocus();
1353 // no, we need to create a new document
1356 // if we've reached the max number of docs, close the first one.
1357 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1359 if ( !CloseDocument((wxDocument
*)GetDocuments().GetFirst()->GetData()) )
1361 // can't open the new document if closing the old one failed
1367 // do create and initialize the new document finally
1368 wxDocument
* const docNew
= temp
->CreateDocument(path
, flags
);
1372 docNew
->SetDocumentName(temp
->GetDocumentName());
1373 docNew
->SetDocumentTemplate(temp
);
1377 // call the appropriate function depending on whether we're creating a
1378 // new file or opening an existing one
1379 if ( !(flags
& wxDOC_NEW
? docNew
->OnNewDocument()
1380 : docNew
->OnOpenDocument(path
)) )
1382 docNew
->DeleteAllViews();
1386 wxCATCH_ALL( docNew
->DeleteAllViews(); throw; )
1388 // add the successfully opened file to MRU, but only if we're going to be
1389 // able to reopen it successfully later which requires the template for
1390 // this document to be retrievable from the file extension
1391 if ( !(flags
& wxDOC_NEW
) && temp
->FileMatchesTemplate(path
) )
1392 AddFileToHistory(path
);
1397 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1399 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1400 const size_t numTemplates
= templates
.size();
1402 if ( numTemplates
== 0 )
1405 wxDocTemplate
* const
1406 temp
= numTemplates
== 1 ? templates
[0]
1407 : SelectViewType(&templates
[0], numTemplates
);
1412 wxView
*view
= temp
->CreateView(doc
, flags
);
1414 view
->SetViewName(temp
->GetViewName());
1418 // Not yet implemented
1420 wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1424 // Not yet implemented
1425 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1430 wxDocument
*wxDocManager::GetCurrentDocument() const
1432 wxView
* const view
= GetActiveView();
1433 return view
? view
->GetDocument() : NULL
;
1436 wxCommandProcessor
*wxDocManager::GetCurrentCommandProcessor() const
1438 wxDocument
* const doc
= GetCurrentDocument();
1439 return doc
? doc
->GetCommandProcessor() : NULL
;
1442 // Make a default name for a new document
1443 #if WXWIN_COMPATIBILITY_2_8
1444 bool wxDocManager::MakeDefaultName(wxString
& WXUNUSED(name
))
1446 // we consider that this function can only be overridden by the user code,
1447 // not called by it as it only makes sense to call it internally, so we
1448 // don't bother to return anything from here
1451 #endif // WXWIN_COMPATIBILITY_2_8
1453 wxString
wxDocManager::MakeNewDocumentName()
1457 #if WXWIN_COMPATIBILITY_2_8
1458 if ( !MakeDefaultName(name
) )
1459 #endif // WXWIN_COMPATIBILITY_2_8
1461 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1462 m_defaultDocumentNameCounter
++;
1468 // Make a frame title (override this to do something different)
1469 // If docName is empty, a document is not currently active.
1470 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1472 wxString appName
= wxTheApp
->GetAppDisplayName();
1478 wxString docName
= doc
->GetUserReadableName();
1479 title
= docName
+ wxString(_(" - ")) + appName
;
1485 // Not yet implemented
1486 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1491 // File history management
1492 void wxDocManager::AddFileToHistory(const wxString
& file
)
1495 m_fileHistory
->AddFileToHistory(file
);
1498 void wxDocManager::RemoveFileFromHistory(size_t i
)
1501 m_fileHistory
->RemoveFileFromHistory(i
);
1504 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1509 histFile
= m_fileHistory
->GetHistoryFile(i
);
1514 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1517 m_fileHistory
->UseMenu(menu
);
1520 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1523 m_fileHistory
->RemoveMenu(menu
);
1527 void wxDocManager::FileHistoryLoad(const wxConfigBase
& config
)
1530 m_fileHistory
->Load(config
);
1533 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1536 m_fileHistory
->Save(config
);
1540 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1543 m_fileHistory
->AddFilesToMenu(menu
);
1546 void wxDocManager::FileHistoryAddFilesToMenu()
1549 m_fileHistory
->AddFilesToMenu();
1552 size_t wxDocManager::GetHistoryFilesCount() const
1554 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1558 // Find out the document template via matching in the document file format
1559 // against that of the template
1560 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1562 wxDocTemplate
*theTemplate
= NULL
;
1564 // Find the template which this extension corresponds to
1565 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1567 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1568 if ( temp
->FileMatchesTemplate(path
) )
1577 // Prompts user to open a file, using file specs in templates.
1578 // Must extend the file selector dialog or implement own; OR
1579 // match the extension to the template extension.
1581 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1584 long WXUNUSED(flags
),
1585 bool WXUNUSED(save
))
1587 #ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
1590 for (int i
= 0; i
< noTemplates
; i
++)
1592 if (templates
[i
]->IsVisible())
1594 // add a '|' to separate this filter from the previous one
1595 if ( !descrBuf
.empty() )
1596 descrBuf
<< wxT('|');
1598 descrBuf
<< templates
[i
]->GetDescription()
1599 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1600 << templates
[i
]->GetFileFilter();
1604 wxString descrBuf
= wxT("*.*");
1605 wxUnusedVar(noTemplates
);
1608 int FilterIndex
= -1;
1610 wxString pathTmp
= wxFileSelectorEx(_("Open File"),
1615 wxFD_OPEN
| wxFD_FILE_MUST_EXIST
);
1617 wxDocTemplate
*theTemplate
= NULL
;
1618 if (!pathTmp
.empty())
1620 if (!wxFileExists(pathTmp
))
1623 if (!wxTheApp
->GetAppDisplayName().empty())
1624 msgTitle
= wxTheApp
->GetAppDisplayName();
1626 msgTitle
= wxString(_("File error"));
1628 wxMessageBox(_("Sorry, could not open this file."),
1630 wxOK
| wxICON_EXCLAMATION
| wxCENTRE
);
1632 path
= wxEmptyString
;
1636 SetLastDirectory(wxPathOnly(pathTmp
));
1640 // first choose the template using the extension, if this fails (i.e.
1641 // wxFileSelectorEx() didn't fill it), then use the path
1642 if ( FilterIndex
!= -1 )
1643 theTemplate
= templates
[FilterIndex
];
1645 theTemplate
= FindTemplateForPath(path
);
1648 // Since we do not add files with non-default extensions to the
1649 // file history this can only happen if the application changes the
1650 // allowed templates in runtime.
1651 wxMessageBox(_("Sorry, the format for this file is unknown."),
1653 wxOK
| wxICON_EXCLAMATION
| wxCENTRE
);
1664 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1665 int noTemplates
, bool sort
)
1667 wxArrayString strings
;
1668 wxScopedArray
<wxDocTemplate
*> data(new wxDocTemplate
*[noTemplates
]);
1672 for (i
= 0; i
< noTemplates
; i
++)
1674 if (templates
[i
]->IsVisible())
1678 for (j
= 0; j
< n
; j
++)
1680 //filter out NOT unique documents + view combinations
1681 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1682 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1689 strings
.Add(templates
[i
]->m_description
);
1691 data
[n
] = templates
[i
];
1699 strings
.Sort(); // ascending sort
1700 // Yes, this will be slow, but template lists
1701 // are typically short.
1703 n
= strings
.Count();
1704 for (i
= 0; i
< n
; i
++)
1706 for (j
= 0; j
< noTemplates
; j
++)
1708 if (strings
[i
] == templates
[j
]->m_description
)
1709 data
[i
] = templates
[j
];
1714 wxDocTemplate
*theTemplate
;
1719 // no visible templates, hence nothing to choose from
1724 // don't propose the user to choose if he has no choice
1725 theTemplate
= data
[0];
1729 // propose the user to choose one of several
1730 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1732 _("Select a document template"),
1742 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1743 int noTemplates
, bool sort
)
1745 wxArrayString strings
;
1746 wxScopedArray
<wxDocTemplate
*> data(new wxDocTemplate
*[noTemplates
]);
1750 for (i
= 0; i
< noTemplates
; i
++)
1752 wxDocTemplate
*templ
= templates
[i
];
1753 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1757 for (j
= 0; j
< n
; j
++)
1759 //filter out NOT unique views
1760 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1766 strings
.Add(templ
->m_viewTypeName
);
1775 strings
.Sort(); // ascending sort
1776 // Yes, this will be slow, but template lists
1777 // are typically short.
1779 n
= strings
.Count();
1780 for (i
= 0; i
< n
; i
++)
1782 for (j
= 0; j
< noTemplates
; j
++)
1784 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1785 data
[i
] = templates
[j
];
1790 wxDocTemplate
*theTemplate
;
1792 // the same logic as above
1800 theTemplate
= data
[0];
1804 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1806 _("Select a document view"),
1817 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1819 if (!m_templates
.Member(temp
))
1820 m_templates
.Append(temp
);
1823 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1825 m_templates
.DeleteObject(temp
);
1828 // Add and remove a document from the manager's list
1829 void wxDocManager::AddDocument(wxDocument
*doc
)
1831 if (!m_docs
.Member(doc
))
1835 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1837 m_docs
.DeleteObject(doc
);
1840 // Views or windows should inform the document manager
1841 // when a view is going in or out of focus
1842 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1846 m_currentView
= view
;
1850 if ( m_currentView
== view
)
1852 // don't keep stale pointer
1853 m_currentView
= NULL
;
1858 // ----------------------------------------------------------------------------
1859 // wxDocChildFrameAnyBase
1860 // ----------------------------------------------------------------------------
1862 bool wxDocChildFrameAnyBase::CloseView(wxCloseEvent
& event
)
1866 // notice that we must call wxView::Close() and OnClose() called from
1867 // it in any case, even if we know that we are going to close anyhow
1868 if ( !m_childView
->Close(false) && event
.CanVeto() )
1874 m_childView
->Activate(false);
1876 // it is important to reset m_childView frame pointer to NULL before
1877 // deleting it because while normally it is the frame which deletes the
1878 // view when it's closed, the view also closes the frame if it is
1879 // deleted directly not by us as indicated by its doc child frame
1880 // pointer still being set
1881 m_childView
->SetDocChildFrame(NULL
);
1886 m_childDocument
= NULL
;
1891 // ----------------------------------------------------------------------------
1892 // Default parent frame
1893 // ----------------------------------------------------------------------------
1895 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1896 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1897 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1898 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1901 wxDocParentFrame::wxDocParentFrame()
1903 m_docManager
= NULL
;
1906 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1909 const wxString
& title
,
1913 const wxString
& name
)
1914 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1916 m_docManager
= manager
;
1919 bool wxDocParentFrame::Create(wxDocManager
*manager
,
1922 const wxString
& title
,
1926 const wxString
& name
)
1928 m_docManager
= manager
;
1929 return base_type::Create(frame
, id
, title
, pos
, size
, style
, name
);
1932 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1937 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1939 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1940 wxString
filename(m_docManager
->GetHistoryFile(n
));
1941 if ( filename
.empty() )
1944 wxString errMsg
; // must contain exactly one "%s" if non-empty
1945 if ( wxFile::Exists(filename
) )
1948 if ( m_docManager
->CreateDocument(filename
, wxDOC_SILENT
) )
1951 errMsg
= _("The file '%s' couldn't be opened.");
1953 else // file doesn't exist
1955 errMsg
= _("The file '%s' doesn't exist and couldn't be opened.");
1959 wxASSERT_MSG( !errMsg
.empty(), "should have an error message" );
1961 // remove the file which we can't open from the MRU list
1962 m_docManager
->RemoveFileFromHistory(n
);
1964 // and tell the user about it
1965 wxLogError(errMsg
+ '\n' +
1966 _("It has been removed from the most recently used files list."),
1970 // Extend event processing to search the view's event table
1971 bool wxDocParentFrame::TryBefore(wxEvent
& event
)
1973 if ( m_docManager
&& m_docManager
->ProcessEventHere(event
) )
1976 return wxFrame::TryBefore(event
);
1979 // Define the behaviour for the frame closing
1980 // - must delete all frames except for the main one.
1981 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1983 if (m_docManager
->Clear(!event
.CanVeto()))
1991 #if wxUSE_PRINTING_ARCHITECTURE
1993 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1996 m_printoutView
= view
;
1999 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
2003 // Get the logical pixels per inch of screen and printer
2004 int ppiScreenX
, ppiScreenY
;
2005 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
2006 wxUnusedVar(ppiScreenY
);
2007 int ppiPrinterX
, ppiPrinterY
;
2008 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
2009 wxUnusedVar(ppiPrinterY
);
2011 // This scales the DC so that the printout roughly represents the
2012 // the screen scaling. The text point size _should_ be the right size
2013 // but in fact is too small for some reason. This is a detail that will
2014 // need to be addressed at some point but can be fudged for the
2016 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
2018 // Now we have to check in case our real page size is reduced
2019 // (e.g. because we're drawing to a print preview memory DC)
2020 int pageWidth
, pageHeight
;
2022 dc
->GetSize(&w
, &h
);
2023 GetPageSizePixels(&pageWidth
, &pageHeight
);
2024 wxUnusedVar(pageHeight
);
2026 // If printer pageWidth == current DC width, then this doesn't
2027 // change. But w might be the preview bitmap width, so scale down.
2028 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
2029 dc
->SetUserScale(overallScale
, overallScale
);
2033 m_printoutView
->OnDraw(dc
);
2038 bool wxDocPrintout::HasPage(int pageNum
)
2040 return (pageNum
== 1);
2043 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
2045 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2051 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
,
2052 int *selPageFrom
, int *selPageTo
)
2060 #endif // wxUSE_PRINTING_ARCHITECTURE
2062 // ----------------------------------------------------------------------------
2063 // Permits compatibility with existing file formats and functions that
2064 // manipulate files directly
2065 // ----------------------------------------------------------------------------
2067 #if wxUSE_STD_IOSTREAM
2069 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2072 wxFFile
file(filename
, wxT("rb"));
2074 wxFile
file(filename
, wxFile::read
);
2076 if ( !file
.IsOpened() )
2084 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2088 stream
.write(buf
, nRead
);
2092 while ( !file
.Eof() );
2097 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2100 wxFFile
file(filename
, wxT("wb"));
2102 wxFile
file(filename
, wxFile::write
);
2104 if ( !file
.IsOpened() )
2110 stream
.read(buf
, WXSIZEOF(buf
));
2111 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2113 if ( !file
.Write(buf
, stream
.gcount()) )
2117 while ( !stream
.eof() );
2122 #else // !wxUSE_STD_IOSTREAM
2124 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2127 wxFFile
file(filename
, wxT("rb"));
2129 wxFile
file(filename
, wxFile::read
);
2131 if ( !file
.IsOpened() )
2139 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2143 stream
.Write(buf
, nRead
);
2147 while ( !file
.Eof() );
2152 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2155 wxFFile
file(filename
, wxT("wb"));
2157 wxFile
file(filename
, wxFile::write
);
2159 if ( !file
.IsOpened() )
2165 stream
.Read(buf
, WXSIZEOF(buf
));
2167 const size_t nRead
= stream
.LastRead();
2176 if ( !file
.Write(buf
, nRead
) )
2183 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2185 #endif // wxUSE_DOC_VIEW_ARCHITECTURE