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()
138 delete m_commandProcessor
;
140 if (GetDocumentManager())
141 GetDocumentManager()->RemoveDocument(this);
143 // Not safe to do here, since it'll invoke virtual view functions
144 // expecting to see valid derived objects: and by the time we get here,
145 // we've called destructors higher up.
149 bool wxDocument::Close()
151 if ( !OnSaveModified() )
154 return OnCloseDocument();
157 bool wxDocument::OnCloseDocument()
159 // Tell all views that we're about to close
166 // Note that this implicitly deletes the document when the last view is
168 bool wxDocument::DeleteAllViews()
170 wxDocManager
* manager
= GetDocumentManager();
172 // first check if all views agree to be closed
173 const wxList::iterator end
= m_documentViews
.end();
174 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
176 wxView
*view
= (wxView
*)*i
;
177 if ( !view
->Close() )
181 // all views agreed to close, now do close them
182 if ( m_documentViews
.empty() )
184 // normally the document would be implicitly deleted when the last view
185 // is, but if don't have any views, do it here instead
186 if ( manager
&& manager
->GetDocuments().Member(this) )
191 // as we delete elements we iterate over, don't use the usual "from
192 // begin to end" loop
195 wxView
*view
= (wxView
*)*m_documentViews
.begin();
197 bool isLastOne
= m_documentViews
.size() == 1;
199 // this always deletes the node implicitly and if this is the last
200 // view also deletes this object itself (also implicitly, great),
201 // so we can't test for m_documentViews.empty() after calling this!
212 wxView
*wxDocument::GetFirstView() const
214 if ( m_documentViews
.empty() )
217 return static_cast<wxView
*>(m_documentViews
.GetFirst()->GetData());
220 void wxDocument::Modify(bool mod
)
222 if (mod
!= m_documentModified
)
224 m_documentModified
= mod
;
226 // Allow views to append asterix to the title
227 wxView
* view
= GetFirstView();
228 if (view
) view
->OnChangeFilename();
232 wxDocManager
*wxDocument::GetDocumentManager() const
234 return m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : NULL
;
237 bool wxDocument::OnNewDocument()
239 // notice that there is no need to neither reset nor even check the
240 // modified flag here as the document itself is a new object (this is only
241 // called from CreateDocument()) and so it shouldn't be saved anyhow even
242 // if it is modified -- this could happen if the user code creates
243 // documents pre-filled with some user-entered (and which hence must not be
246 SetDocumentSaved(false);
248 const wxString name
= GetDocumentManager()->MakeNewDocumentName();
250 SetFilename(name
, true);
255 bool wxDocument::Save()
257 if ( AlreadySaved() )
260 if ( m_documentFile
.empty() || !m_savedYet
)
263 return OnSaveDocument(m_documentFile
);
266 bool wxDocument::SaveAs()
268 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
272 #ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
273 wxString filter
= docTemplate
->GetDescription() + wxT(" (") +
274 docTemplate
->GetFileFilter() + wxT(")|") +
275 docTemplate
->GetFileFilter();
277 // Now see if there are some other template with identical view and document
278 // classes, whose filters may also be used.
279 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
281 wxList::compatibility_iterator
282 node
= docTemplate
->GetDocumentManager()->GetTemplates().GetFirst();
285 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
287 if (t
->IsVisible() && t
!= docTemplate
&&
288 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
289 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
291 // add a '|' to separate this filter from the previous one
292 if ( !filter
.empty() )
295 filter
<< t
->GetDescription()
296 << wxT(" (") << t
->GetFileFilter() << wxT(") |")
297 << t
->GetFileFilter();
300 node
= node
->GetNext();
304 wxString filter
= docTemplate
->GetFileFilter() ;
307 wxString defaultDir
= docTemplate
->GetDirectory();
308 if ( defaultDir
.empty() )
310 defaultDir
= wxPathOnly(GetFilename());
311 if ( defaultDir
.empty() )
312 defaultDir
= GetDocumentManager()->GetLastDirectory();
315 wxString fileName
= wxFileSelector(_("Save As"),
317 wxFileNameFromPath(GetFilename()),
318 docTemplate
->GetDefaultExtension(),
320 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
321 GetDocumentWindow());
323 if (fileName
.empty())
324 return false; // cancelled by user
326 // Files that were not saved correctly are not added to the FileHistory.
327 if (!OnSaveDocument(fileName
))
330 SetTitle(wxFileNameFromPath(fileName
));
331 SetFilename(fileName
, true); // will call OnChangeFileName automatically
333 // A file that doesn't use the default extension of its document template
334 // cannot be opened via the FileHistory, so we do not add it.
335 if (docTemplate
->FileMatchesTemplate(fileName
))
337 GetDocumentManager()->AddFileToHistory(fileName
);
339 //else: the user will probably not be able to open the file again, so we
340 // could warn about the wrong file-extension here
345 bool wxDocument::OnSaveDocument(const wxString
& file
)
350 if ( !DoSaveDocument(file
) )
355 SetDocumentSaved(true);
356 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
357 wxFileName
fn(file
) ;
358 fn
.MacSetDefaultTypeAndCreator() ;
363 bool wxDocument::OnOpenDocument(const wxString
& file
)
365 // notice that there is no need to check the modified flag here for the
366 // reasons explained in OnNewDocument()
368 if ( !DoOpenDocument(file
) )
371 SetFilename(file
, true);
373 // stretching the logic a little this does make sense because the document
374 // had been saved into the file we just loaded it from, it just could have
375 // happened during a previous program execution, it's just that the name of
376 // this method is a bit unfortunate, it should probably have been called
377 // HasAssociatedFileName()
378 SetDocumentSaved(true);
385 #if wxUSE_STD_IOSTREAM
386 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
388 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
394 #if wxUSE_STD_IOSTREAM
395 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
397 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
403 bool wxDocument::Revert()
407 _("Discard changes and reload the last saved version?"),
408 wxTheApp
->GetAppDisplayName(),
409 wxYES_NO
| wxCANCEL
| wxICON_QUESTION
,
414 if ( !DoOpenDocument(GetFilename()) )
424 // Get title, or filename if no title, else unnamed
425 #if WXWIN_COMPATIBILITY_2_8
426 bool wxDocument::GetPrintableName(wxString
& buf
) const
428 // this function can not only be overridden by the user code but also
429 // called by it so we need to ensure that we return the same thing as
430 // GetUserReadableName() but we can't call it because this would result in
431 // an infinite recursion, hence we use the helper DoGetUserReadableName()
432 buf
= DoGetUserReadableName();
436 #endif // WXWIN_COMPATIBILITY_2_8
438 wxString
wxDocument::GetUserReadableName() const
440 #if WXWIN_COMPATIBILITY_2_8
441 // we need to call the old virtual function to ensure that the overridden
442 // version of it is still called
444 if ( GetPrintableName(name
) )
446 #endif // WXWIN_COMPATIBILITY_2_8
448 return DoGetUserReadableName();
451 wxString
wxDocument::DoGetUserReadableName() const
453 if ( !m_documentTitle
.empty() )
454 return m_documentTitle
;
456 if ( !m_documentFile
.empty() )
457 return wxFileNameFromPath(m_documentFile
);
462 wxWindow
*wxDocument::GetDocumentWindow() const
464 wxView
* const view
= GetFirstView();
466 return view
? view
->GetFrame() : wxTheApp
->GetTopWindow();
469 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
471 return new wxCommandProcessor
;
474 // true if safe to close
475 bool wxDocument::OnSaveModified()
479 switch ( wxMessageBox
483 _("Do you want to save changes to %s?"),
484 GetUserReadableName()
486 wxTheApp
->GetAppDisplayName(),
487 wxYES_NO
| wxCANCEL
| wxICON_QUESTION
| wxCENTRE
505 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
510 bool wxDocument::AddView(wxView
*view
)
512 if ( !m_documentViews
.Member(view
) )
514 m_documentViews
.Append(view
);
520 bool wxDocument::RemoveView(wxView
*view
)
522 (void)m_documentViews
.DeleteObject(view
);
527 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
529 return GetDocumentTemplate()->CreateView(this, flags
) != NULL
;
532 // Called after a view is added or removed.
533 // The default implementation deletes the document if
534 // there are no more views.
535 void wxDocument::OnChangedViewList()
537 if ( m_documentViews
.empty() && OnSaveModified() )
541 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
543 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
546 wxView
*view
= (wxView
*)node
->GetData();
548 view
->OnUpdate(sender
, hint
);
549 node
= node
->GetNext();
553 void wxDocument::NotifyClosing()
555 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
558 wxView
*view
= (wxView
*)node
->GetData();
559 view
->OnClosingDocument();
560 node
= node
->GetNext();
564 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
566 m_documentFile
= filename
;
567 OnChangeFilename(notifyViews
);
570 void wxDocument::OnChangeFilename(bool notifyViews
)
574 // Notify the views that the filename has changed
575 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
578 wxView
*view
= (wxView
*)node
->GetData();
579 view
->OnChangeFilename();
580 node
= node
->GetNext();
585 bool wxDocument::DoSaveDocument(const wxString
& file
)
587 #if wxUSE_STD_IOSTREAM
588 wxSTD ofstream
store(file
.mb_str(), wxSTD
ios::binary
);
591 wxFileOutputStream
store(file
);
592 if ( store
.GetLastError() != wxSTREAM_NO_ERROR
)
595 wxLogError(_("File \"%s\" could not be opened for writing."), file
);
599 if (!SaveObject(store
))
601 wxLogError(_("Failed to save document to the file \"%s\"."), file
);
608 bool wxDocument::DoOpenDocument(const wxString
& file
)
610 #if wxUSE_STD_IOSTREAM
611 wxSTD ifstream
store(file
.mb_str(), wxSTD
ios::binary
);
614 wxFileInputStream
store(file
);
615 if (store
.GetLastError() != wxSTREAM_NO_ERROR
|| !store
.IsOk())
618 wxLogError(_("File \"%s\" could not be opened for reading."), file
);
622 #if wxUSE_STD_IOSTREAM
626 int res
= LoadObject(store
).GetLastError();
627 if ( res
!= wxSTREAM_NO_ERROR
&& res
!= wxSTREAM_EOF
)
630 wxLogError(_("Failed to read document from the file \"%s\"."), file
);
638 // ----------------------------------------------------------------------------
640 // ----------------------------------------------------------------------------
644 m_viewDocument
= NULL
;
648 m_docChildFrame
= NULL
;
653 if (m_viewDocument
&& GetDocumentManager())
654 GetDocumentManager()->ActivateView(this, false);
656 // reset our frame view first, before removing it from the document as
657 // SetView(NULL) is a simple call while RemoveView() may result in user
658 // code being executed and this user code can, for example, show a message
659 // box which would result in an activation event for m_docChildFrame and so
660 // could reactivate the view being destroyed -- unless we reset it first
661 if ( m_docChildFrame
&& m_docChildFrame
->GetView() == this )
663 // prevent it from doing anything with us
664 m_docChildFrame
->SetView(NULL
);
666 // it doesn't make sense to leave the frame alive if its associated
667 // view doesn't exist any more so unconditionally close it as well
669 // notice that we only get here if m_docChildFrame is non-NULL in the
670 // first place and it will be always NULL if we're deleted because our
671 // frame was closed, so this only catches the case of directly deleting
672 // the view, as it happens if its creation fails in wxDocTemplate::
673 // CreateView() for example
674 m_docChildFrame
->GetWindow()->Destroy();
677 if ( m_viewDocument
)
678 m_viewDocument
->RemoveView(this);
681 void wxView::SetDocChildFrame(wxDocChildFrameAnyBase
*docChildFrame
)
683 SetFrame(docChildFrame
? docChildFrame
->GetWindow() : NULL
);
684 m_docChildFrame
= docChildFrame
;
687 bool wxView::TryBefore(wxEvent
& event
)
689 wxDocument
* const doc
= GetDocument();
690 return doc
&& doc
->ProcessEventLocally(event
);
693 void wxView::OnActivateView(bool WXUNUSED(activate
),
694 wxView
*WXUNUSED(activeView
),
695 wxView
*WXUNUSED(deactiveView
))
699 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
704 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
708 void wxView::OnChangeFilename()
710 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
711 // generic MDI implementation so use SetLabel rather than SetTitle.
712 // It should cause SetTitle() for top level windows.
713 wxWindow
*win
= GetFrame();
716 wxDocument
*doc
= GetDocument();
719 wxString label
= doc
->GetUserReadableName();
720 if (doc
->IsModified())
724 win
->SetLabel(label
);
727 void wxView::SetDocument(wxDocument
*doc
)
729 m_viewDocument
= doc
;
734 bool wxView::Close(bool deleteWindow
)
736 return OnClose(deleteWindow
);
739 void wxView::Activate(bool activate
)
741 if (GetDocument() && GetDocumentManager())
743 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
744 GetDocumentManager()->ActivateView(this, activate
);
748 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
750 return GetDocument() ? GetDocument()->Close() : true;
753 #if wxUSE_PRINTING_ARCHITECTURE
754 wxPrintout
*wxView::OnCreatePrintout()
756 return new wxDocPrintout(this);
758 #endif // wxUSE_PRINTING_ARCHITECTURE
760 // ----------------------------------------------------------------------------
762 // ----------------------------------------------------------------------------
764 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
765 const wxString
& descr
,
766 const wxString
& filter
,
769 const wxString
& docTypeName
,
770 const wxString
& viewTypeName
,
771 wxClassInfo
*docClassInfo
,
772 wxClassInfo
*viewClassInfo
,
775 m_documentManager
= manager
;
776 m_description
= descr
;
779 m_fileFilter
= filter
;
781 m_docTypeName
= docTypeName
;
782 m_viewTypeName
= viewTypeName
;
783 m_documentManager
->AssociateTemplate(this);
785 m_docClassInfo
= docClassInfo
;
786 m_viewClassInfo
= viewClassInfo
;
789 wxDocTemplate::~wxDocTemplate()
791 m_documentManager
->DisassociateTemplate(this);
794 // Tries to dynamically construct an object of the right class.
795 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
797 // InitDocument() is supposed to delete the document object if its
798 // initialization fails so don't use wxScopedPtr<> here: this is fragile
799 // but unavoidable because the default implementation uses CreateView()
800 // which may -- or not -- create a wxView and if it does create it and its
801 // initialization fails then the view destructor will delete the document
802 // (via RemoveView()) and as we can't distinguish between the two cases we
803 // just have to assume that it always deletes it in case of failure
804 wxDocument
* const doc
= DoCreateDocument();
806 return doc
&& InitDocument(doc
, path
, flags
) ? doc
: NULL
;
810 wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
812 doc
->SetFilename(path
);
813 doc
->SetDocumentTemplate(this);
814 GetDocumentManager()->AddDocument(doc
);
815 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
817 if (doc
->OnCreate(path
, flags
))
820 if (GetDocumentManager()->GetDocuments().Member(doc
))
821 doc
->DeleteAllViews();
825 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
827 wxScopedPtr
<wxView
> view(DoCreateView());
831 view
->SetDocument(doc
);
832 if ( !view
->OnCreate(doc
, flags
) )
835 return view
.release();
838 // The default (very primitive) format detection: check is the extension is
839 // that of the template
840 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
842 wxStringTokenizer
parser (GetFileFilter(), wxT(";"));
843 wxString anything
= wxT ("*");
844 while (parser
.HasMoreTokens())
846 wxString filter
= parser
.GetNextToken();
847 wxString filterExt
= FindExtension (filter
);
848 if ( filter
.IsSameAs (anything
) ||
849 filterExt
.IsSameAs (anything
) ||
850 filterExt
.IsSameAs (FindExtension (path
)) )
853 return GetDefaultExtension().IsSameAs(FindExtension(path
));
856 wxDocument
*wxDocTemplate::DoCreateDocument()
861 return static_cast<wxDocument
*>(m_docClassInfo
->CreateObject());
864 wxView
*wxDocTemplate::DoCreateView()
866 if (!m_viewClassInfo
)
869 return static_cast<wxView
*>(m_viewClassInfo
->CreateObject());
872 // ----------------------------------------------------------------------------
874 // ----------------------------------------------------------------------------
876 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
877 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
878 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
879 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
880 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
881 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
882 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
883 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
884 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
885 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
887 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
888 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateDisableIfNoDoc
)
889 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateDisableIfNoDoc
)
890 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
891 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
892 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
893 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateDisableIfNoDoc
)
894 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
895 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
897 #if wxUSE_PRINTING_ARCHITECTURE
898 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
899 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
901 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdateDisableIfNoDoc
)
902 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdateDisableIfNoDoc
)
906 wxDocManager
* wxDocManager::sm_docManager
= NULL
;
908 wxDocManager::wxDocManager(long WXUNUSED(flags
), bool initialize
)
910 sm_docManager
= this;
912 m_defaultDocumentNameCounter
= 1;
913 m_currentView
= NULL
;
914 m_maxDocsOpen
= INT_MAX
;
915 m_fileHistory
= NULL
;
920 wxDocManager::~wxDocManager()
923 delete m_fileHistory
;
924 sm_docManager
= NULL
;
927 // closes the specified document
928 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
930 if ( !doc
->Close() && !force
)
933 // Implicitly deletes the document when
934 // the last view is deleted
935 doc
->DeleteAllViews();
937 // Check we're really deleted
938 if (m_docs
.Member(doc
))
944 bool wxDocManager::CloseDocuments(bool force
)
946 wxList::compatibility_iterator node
= m_docs
.GetFirst();
949 wxDocument
*doc
= (wxDocument
*)node
->GetData();
950 wxList::compatibility_iterator next
= node
->GetNext();
952 if (!CloseDocument(doc
, force
))
955 // This assumes that documents are not connected in
956 // any way, i.e. deleting one document does NOT
963 bool wxDocManager::Clear(bool force
)
965 if (!CloseDocuments(force
))
968 m_currentView
= NULL
;
970 wxList::compatibility_iterator node
= m_templates
.GetFirst();
973 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
974 wxList::compatibility_iterator next
= node
->GetNext();
981 bool wxDocManager::Initialize()
983 m_fileHistory
= OnCreateFileHistory();
987 wxString
wxDocManager::GetLastDirectory() const
989 // if we haven't determined the last used directory yet, do it now
990 if ( m_lastDirectory
.empty() )
992 // we're going to modify m_lastDirectory in this const method, so do it
993 // via non-const self pointer instead of const this one
994 wxDocManager
* const self
= const_cast<wxDocManager
*>(this);
996 // first try to reuse the directory of the most recently opened file:
997 // this ensures that if the user opens a file, closes the program and
998 // runs it again the "Open file" dialog will open in the directory of
999 // the last file he used
1000 if ( m_fileHistory
&& m_fileHistory
->GetCount() )
1002 const wxString lastOpened
= m_fileHistory
->GetHistoryFile(0);
1003 const wxFileName
fn(lastOpened
);
1004 if ( fn
.DirExists() )
1006 self
->m_lastDirectory
= fn
.GetPath();
1008 //else: should we try the next one?
1010 //else: no history yet
1012 // if we don't have any files in the history (yet?), use the
1013 // system-dependent default location for the document files
1014 if ( m_lastDirectory
.empty() )
1016 self
->m_lastDirectory
= wxStandardPaths::Get().GetAppDocumentsDir();
1020 return m_lastDirectory
;
1023 wxFileHistory
*wxDocManager::OnCreateFileHistory()
1025 return new wxFileHistory
;
1028 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
1030 wxDocument
*doc
= GetCurrentDocument();
1035 doc
->DeleteAllViews();
1036 if (m_docs
.Member(doc
))
1041 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
1043 CloseDocuments(false);
1046 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
1048 CreateNewDocument();
1051 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
1053 if ( !CreateDocument("") )
1055 OnOpenFileFailure();
1059 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
1061 wxDocument
*doc
= GetCurrentDocument();
1067 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1069 wxDocument
*doc
= GetCurrentDocument();
1075 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
1077 wxDocument
*doc
= GetCurrentDocument();
1083 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
1085 #if wxUSE_PRINTING_ARCHITECTURE
1086 wxView
*view
= GetActiveView();
1090 wxPrintout
*printout
= view
->OnCreatePrintout();
1094 printer
.Print(view
->GetFrame(), printout
, true);
1098 #endif // wxUSE_PRINTING_ARCHITECTURE
1101 #if wxUSE_PRINTING_ARCHITECTURE
1102 wxPreviewFrame
* wxDocManager::CreatePreviewFrame(wxPrintPreviewBase
* preview
,
1104 const wxString
& title
)
1106 return new wxPreviewFrame(preview
, parent
, title
);
1108 #endif // wxUSE_PRINTING_ARCHITECTURE
1110 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1112 #if wxUSE_PRINTING_ARCHITECTURE
1114 wxView
*view
= GetActiveView();
1118 wxPrintout
*printout
= view
->OnCreatePrintout();
1121 // Pass two printout objects: for preview, and possible printing.
1122 wxPrintPreviewBase
*
1123 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1124 if ( !preview
->Ok() )
1127 wxLogError(_("Print preview creation failed."));
1131 wxPreviewFrame
* frame
= CreatePreviewFrame(preview
,
1132 wxTheApp
->GetTopWindow(),
1133 _("Print Preview"));
1134 wxCHECK_RET( frame
, "should create a print preview frame" );
1136 frame
->Centre(wxBOTH
);
1137 frame
->Initialize();
1140 #endif // wxUSE_PRINTING_ARCHITECTURE
1143 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1145 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1155 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1157 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1167 // Handlers for UI update commands
1169 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1171 // CreateDocument() (which is called from OnFileOpen) may succeed
1172 // only when there is at least a template:
1173 event
.Enable( GetTemplates().GetCount()>0 );
1176 void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent
& event
)
1178 event
.Enable( GetCurrentDocument() != NULL
);
1181 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1183 wxDocument
* doc
= GetCurrentDocument();
1184 event
.Enable(doc
&& doc
->IsModified() && doc
->GetDocumentSaved());
1187 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1189 // CreateDocument() (which is called from OnFileNew) may succeed
1190 // only when there is at least a template:
1191 event
.Enable( GetTemplates().GetCount()>0 );
1194 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1196 wxDocument
* const doc
= GetCurrentDocument();
1197 event
.Enable( doc
&& !doc
->AlreadySaved() );
1200 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1202 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1205 event
.Enable(false);
1209 event
.Enable(cmdproc
->CanUndo());
1210 cmdproc
->SetMenuStrings();
1213 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1215 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1218 event
.Enable(false);
1222 event
.Enable(cmdproc
->CanRedo());
1223 cmdproc
->SetMenuStrings();
1226 wxView
*wxDocManager::GetActiveView() const
1228 wxView
*view
= GetCurrentView();
1230 if ( !view
&& !m_docs
.empty() )
1232 // if we have exactly one document, consider its view to be the current
1235 // VZ: I'm not exactly sure why is this needed but this is how this
1236 // code used to behave before the bug #9518 was fixed and it seems
1237 // safer to preserve the old logic
1238 wxList::compatibility_iterator node
= m_docs
.GetFirst();
1239 if ( !node
->GetNext() )
1241 wxDocument
*doc
= static_cast<wxDocument
*>(node
->GetData());
1242 view
= doc
->GetFirstView();
1244 //else: we have more than one document
1250 bool wxDocManager::TryBefore(wxEvent
& event
)
1252 wxView
* const view
= GetActiveView();
1253 return view
&& view
->ProcessEventLocally(event
);
1259 // helper function: return only the visible templates
1260 wxDocTemplates
GetVisibleTemplates(const wxList
& allTemplates
)
1262 // select only the visible templates
1263 const size_t totalNumTemplates
= allTemplates
.GetCount();
1264 wxDocTemplates templates
;
1265 if ( totalNumTemplates
)
1267 templates
.reserve(totalNumTemplates
);
1269 for ( wxList::const_iterator i
= allTemplates
.begin(),
1270 end
= allTemplates
.end();
1274 wxDocTemplate
* const temp
= (wxDocTemplate
*)*i
;
1275 if ( temp
->IsVisible() )
1276 templates
.push_back(temp
);
1283 } // anonymous namespace
1285 wxDocument
*wxDocManager::CreateDocument(const wxString
& pathOrig
, long flags
)
1287 // this ought to be const but SelectDocumentType/Path() are not
1288 // const-correct and can't be changed as, being virtual, this risks
1289 // breaking user code overriding them
1290 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1291 const size_t numTemplates
= templates
.size();
1292 if ( !numTemplates
)
1294 // no templates can be used, can't create document
1299 // normally user should select the template to use but wxDOC_SILENT flag we
1300 // choose one ourselves
1301 wxString path
= pathOrig
; // may be modified below
1302 wxDocTemplate
*temp
;
1303 if ( flags
& wxDOC_SILENT
)
1305 wxASSERT_MSG( !path
.empty(),
1306 "using empty path with wxDOC_SILENT doesn't make sense" );
1308 temp
= FindTemplateForPath(path
);
1311 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1315 else // not silent, ask the user
1317 // for the new file we need just the template, for an existing one we
1318 // need the template and the path, unless it's already specified
1319 if ( (flags
& wxDOC_NEW
) || !path
.empty() )
1320 temp
= SelectDocumentType(&templates
[0], numTemplates
);
1322 temp
= SelectDocumentPath(&templates
[0], numTemplates
, path
, flags
);
1328 // check whether the document with this path is already opened
1329 if ( !path
.empty() )
1331 const wxFileName
fn(path
);
1332 for ( wxList::const_iterator i
= m_docs
.begin(); i
!= m_docs
.end(); ++i
)
1334 wxDocument
* const doc
= (wxDocument
*)*i
;
1336 if ( fn
== doc
->GetFilename() )
1338 // file already open, just activate it and return
1339 if ( doc
->GetFirstView() )
1341 ActivateView(doc
->GetFirstView());
1342 if ( doc
->GetDocumentWindow() )
1343 doc
->GetDocumentWindow()->SetFocus();
1351 // no, we need to create a new document
1354 // if we've reached the max number of docs, close the first one.
1355 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1357 if ( !CloseDocument((wxDocument
*)GetDocuments().GetFirst()->GetData()) )
1359 // can't open the new document if closing the old one failed
1365 // do create and initialize the new document finally
1366 wxDocument
* const docNew
= temp
->CreateDocument(path
, flags
);
1370 docNew
->SetDocumentName(temp
->GetDocumentName());
1371 docNew
->SetDocumentTemplate(temp
);
1375 // call the appropriate function depending on whether we're creating a
1376 // new file or opening an existing one
1377 if ( !(flags
& wxDOC_NEW
? docNew
->OnNewDocument()
1378 : docNew
->OnOpenDocument(path
)) )
1380 docNew
->DeleteAllViews();
1384 wxCATCH_ALL( docNew
->DeleteAllViews(); throw; )
1386 // add the successfully opened file to MRU, but only if we're going to be
1387 // able to reopen it successfully later which requires the template for
1388 // this document to be retrievable from the file extension
1389 if ( !(flags
& wxDOC_NEW
) && temp
->FileMatchesTemplate(path
) )
1390 AddFileToHistory(path
);
1395 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1397 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1398 const size_t numTemplates
= templates
.size();
1400 if ( numTemplates
== 0 )
1403 wxDocTemplate
* const
1404 temp
= numTemplates
== 1 ? templates
[0]
1405 : SelectViewType(&templates
[0], numTemplates
);
1410 wxView
*view
= temp
->CreateView(doc
, flags
);
1412 view
->SetViewName(temp
->GetViewName());
1416 // Not yet implemented
1418 wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1422 // Not yet implemented
1423 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1428 wxDocument
*wxDocManager::GetCurrentDocument() const
1430 wxView
* const view
= GetActiveView();
1431 return view
? view
->GetDocument() : NULL
;
1434 wxCommandProcessor
*wxDocManager::GetCurrentCommandProcessor() const
1436 wxDocument
* const doc
= GetCurrentDocument();
1437 return doc
? doc
->GetCommandProcessor() : NULL
;
1440 // Make a default name for a new document
1441 #if WXWIN_COMPATIBILITY_2_8
1442 bool wxDocManager::MakeDefaultName(wxString
& WXUNUSED(name
))
1444 // we consider that this function can only be overridden by the user code,
1445 // not called by it as it only makes sense to call it internally, so we
1446 // don't bother to return anything from here
1449 #endif // WXWIN_COMPATIBILITY_2_8
1451 wxString
wxDocManager::MakeNewDocumentName()
1455 #if WXWIN_COMPATIBILITY_2_8
1456 if ( !MakeDefaultName(name
) )
1457 #endif // WXWIN_COMPATIBILITY_2_8
1459 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1460 m_defaultDocumentNameCounter
++;
1466 // Make a frame title (override this to do something different)
1467 // If docName is empty, a document is not currently active.
1468 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1470 wxString appName
= wxTheApp
->GetAppDisplayName();
1476 wxString docName
= doc
->GetUserReadableName();
1477 title
= docName
+ wxString(_(" - ")) + appName
;
1483 // Not yet implemented
1484 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1489 // File history management
1490 void wxDocManager::AddFileToHistory(const wxString
& file
)
1493 m_fileHistory
->AddFileToHistory(file
);
1496 void wxDocManager::RemoveFileFromHistory(size_t i
)
1499 m_fileHistory
->RemoveFileFromHistory(i
);
1502 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1507 histFile
= m_fileHistory
->GetHistoryFile(i
);
1512 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1515 m_fileHistory
->UseMenu(menu
);
1518 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1521 m_fileHistory
->RemoveMenu(menu
);
1525 void wxDocManager::FileHistoryLoad(const wxConfigBase
& config
)
1528 m_fileHistory
->Load(config
);
1531 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1534 m_fileHistory
->Save(config
);
1538 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1541 m_fileHistory
->AddFilesToMenu(menu
);
1544 void wxDocManager::FileHistoryAddFilesToMenu()
1547 m_fileHistory
->AddFilesToMenu();
1550 size_t wxDocManager::GetHistoryFilesCount() const
1552 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1556 // Find out the document template via matching in the document file format
1557 // against that of the template
1558 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1560 wxDocTemplate
*theTemplate
= NULL
;
1562 // Find the template which this extension corresponds to
1563 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1565 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1566 if ( temp
->FileMatchesTemplate(path
) )
1575 // Prompts user to open a file, using file specs in templates.
1576 // Must extend the file selector dialog or implement own; OR
1577 // match the extension to the template extension.
1579 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1582 long WXUNUSED(flags
),
1583 bool WXUNUSED(save
))
1585 #ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
1588 for (int i
= 0; i
< noTemplates
; i
++)
1590 if (templates
[i
]->IsVisible())
1592 // add a '|' to separate this filter from the previous one
1593 if ( !descrBuf
.empty() )
1594 descrBuf
<< wxT('|');
1596 descrBuf
<< templates
[i
]->GetDescription()
1597 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1598 << templates
[i
]->GetFileFilter();
1602 wxString descrBuf
= wxT("*.*");
1603 wxUnusedVar(noTemplates
);
1606 int FilterIndex
= -1;
1608 wxString pathTmp
= wxFileSelectorEx(_("Open File"),
1613 wxFD_OPEN
| wxFD_FILE_MUST_EXIST
);
1615 wxDocTemplate
*theTemplate
= NULL
;
1616 if (!pathTmp
.empty())
1618 if (!wxFileExists(pathTmp
))
1621 if (!wxTheApp
->GetAppDisplayName().empty())
1622 msgTitle
= wxTheApp
->GetAppDisplayName();
1624 msgTitle
= wxString(_("File error"));
1626 wxMessageBox(_("Sorry, could not open this file."),
1628 wxOK
| wxICON_EXCLAMATION
| wxCENTRE
);
1630 path
= wxEmptyString
;
1634 SetLastDirectory(wxPathOnly(pathTmp
));
1638 // first choose the template using the extension, if this fails (i.e.
1639 // wxFileSelectorEx() didn't fill it), then use the path
1640 if ( FilterIndex
!= -1 )
1641 theTemplate
= templates
[FilterIndex
];
1643 theTemplate
= FindTemplateForPath(path
);
1646 // Since we do not add files with non-default extensions to the
1647 // file history this can only happen if the application changes the
1648 // allowed templates in runtime.
1649 wxMessageBox(_("Sorry, the format for this file is unknown."),
1651 wxOK
| wxICON_EXCLAMATION
| wxCENTRE
);
1662 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1663 int noTemplates
, bool sort
)
1665 wxArrayString strings
;
1666 wxScopedArray
<wxDocTemplate
*> data(new wxDocTemplate
*[noTemplates
]);
1670 for (i
= 0; i
< noTemplates
; i
++)
1672 if (templates
[i
]->IsVisible())
1676 for (j
= 0; j
< n
; j
++)
1678 //filter out NOT unique documents + view combinations
1679 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1680 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1687 strings
.Add(templates
[i
]->m_description
);
1689 data
[n
] = templates
[i
];
1697 strings
.Sort(); // ascending sort
1698 // Yes, this will be slow, but template lists
1699 // are typically short.
1701 n
= strings
.Count();
1702 for (i
= 0; i
< n
; i
++)
1704 for (j
= 0; j
< noTemplates
; j
++)
1706 if (strings
[i
] == templates
[j
]->m_description
)
1707 data
[i
] = templates
[j
];
1712 wxDocTemplate
*theTemplate
;
1717 // no visible templates, hence nothing to choose from
1722 // don't propose the user to choose if he has no choice
1723 theTemplate
= data
[0];
1727 // propose the user to choose one of several
1728 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1730 _("Select a document template"),
1740 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1741 int noTemplates
, bool sort
)
1743 wxArrayString strings
;
1744 wxScopedArray
<wxDocTemplate
*> data(new wxDocTemplate
*[noTemplates
]);
1748 for (i
= 0; i
< noTemplates
; i
++)
1750 wxDocTemplate
*templ
= templates
[i
];
1751 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1755 for (j
= 0; j
< n
; j
++)
1757 //filter out NOT unique views
1758 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1764 strings
.Add(templ
->m_viewTypeName
);
1773 strings
.Sort(); // ascending sort
1774 // Yes, this will be slow, but template lists
1775 // are typically short.
1777 n
= strings
.Count();
1778 for (i
= 0; i
< n
; i
++)
1780 for (j
= 0; j
< noTemplates
; j
++)
1782 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1783 data
[i
] = templates
[j
];
1788 wxDocTemplate
*theTemplate
;
1790 // the same logic as above
1798 theTemplate
= data
[0];
1802 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1804 _("Select a document view"),
1815 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1817 if (!m_templates
.Member(temp
))
1818 m_templates
.Append(temp
);
1821 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1823 m_templates
.DeleteObject(temp
);
1826 // Add and remove a document from the manager's list
1827 void wxDocManager::AddDocument(wxDocument
*doc
)
1829 if (!m_docs
.Member(doc
))
1833 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1835 m_docs
.DeleteObject(doc
);
1838 // Views or windows should inform the document manager
1839 // when a view is going in or out of focus
1840 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1844 m_currentView
= view
;
1848 if ( m_currentView
== view
)
1850 // don't keep stale pointer
1851 m_currentView
= NULL
;
1856 // ----------------------------------------------------------------------------
1857 // wxDocChildFrameAnyBase
1858 // ----------------------------------------------------------------------------
1860 bool wxDocChildFrameAnyBase::CloseView(wxCloseEvent
& event
)
1864 // notice that we must call wxView::Close() and OnClose() called from
1865 // it in any case, even if we know that we are going to close anyhow
1866 if ( !m_childView
->Close(false) && event
.CanVeto() )
1872 m_childView
->Activate(false);
1874 // it is important to reset m_childView frame pointer to NULL before
1875 // deleting it because while normally it is the frame which deletes the
1876 // view when it's closed, the view also closes the frame if it is
1877 // deleted directly not by us as indicated by its doc child frame
1878 // pointer still being set
1879 m_childView
->SetDocChildFrame(NULL
);
1884 m_childDocument
= NULL
;
1889 // ----------------------------------------------------------------------------
1890 // wxDocParentFrameAnyBase
1891 // ----------------------------------------------------------------------------
1893 void wxDocParentFrameAnyBase::DoOpenMRUFile(unsigned n
)
1895 wxString
filename(m_docManager
->GetHistoryFile(n
));
1896 if ( filename
.empty() )
1899 wxString errMsg
; // must contain exactly one "%s" if non-empty
1900 if ( wxFile::Exists(filename
) )
1903 if ( m_docManager
->CreateDocument(filename
, wxDOC_SILENT
) )
1906 errMsg
= _("The file '%s' couldn't be opened.");
1908 else // file doesn't exist
1910 errMsg
= _("The file '%s' doesn't exist and couldn't be opened.");
1914 wxASSERT_MSG( !errMsg
.empty(), "should have an error message" );
1916 // remove the file which we can't open from the MRU list
1917 m_docManager
->RemoveFileFromHistory(n
);
1919 // and tell the user about it
1920 wxLogError(errMsg
+ '\n' +
1921 _("It has been removed from the most recently used files list."),
1925 #if wxUSE_PRINTING_ARCHITECTURE
1927 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1930 m_printoutView
= view
;
1933 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1937 // Get the logical pixels per inch of screen and printer
1938 int ppiScreenX
, ppiScreenY
;
1939 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1940 wxUnusedVar(ppiScreenY
);
1941 int ppiPrinterX
, ppiPrinterY
;
1942 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1943 wxUnusedVar(ppiPrinterY
);
1945 // This scales the DC so that the printout roughly represents the
1946 // the screen scaling. The text point size _should_ be the right size
1947 // but in fact is too small for some reason. This is a detail that will
1948 // need to be addressed at some point but can be fudged for the
1950 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1952 // Now we have to check in case our real page size is reduced
1953 // (e.g. because we're drawing to a print preview memory DC)
1954 int pageWidth
, pageHeight
;
1956 dc
->GetSize(&w
, &h
);
1957 GetPageSizePixels(&pageWidth
, &pageHeight
);
1958 wxUnusedVar(pageHeight
);
1960 // If printer pageWidth == current DC width, then this doesn't
1961 // change. But w might be the preview bitmap width, so scale down.
1962 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1963 dc
->SetUserScale(overallScale
, overallScale
);
1967 m_printoutView
->OnDraw(dc
);
1972 bool wxDocPrintout::HasPage(int pageNum
)
1974 return (pageNum
== 1);
1977 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1979 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1985 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
,
1986 int *selPageFrom
, int *selPageTo
)
1994 #endif // wxUSE_PRINTING_ARCHITECTURE
1996 // ----------------------------------------------------------------------------
1997 // Permits compatibility with existing file formats and functions that
1998 // manipulate files directly
1999 // ----------------------------------------------------------------------------
2001 #if wxUSE_STD_IOSTREAM
2003 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2006 wxFFile
file(filename
, wxT("rb"));
2008 wxFile
file(filename
, wxFile::read
);
2010 if ( !file
.IsOpened() )
2018 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2022 stream
.write(buf
, nRead
);
2026 while ( !file
.Eof() );
2031 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2034 wxFFile
file(filename
, wxT("wb"));
2036 wxFile
file(filename
, wxFile::write
);
2038 if ( !file
.IsOpened() )
2044 stream
.read(buf
, WXSIZEOF(buf
));
2045 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2047 if ( !file
.Write(buf
, stream
.gcount()) )
2051 while ( !stream
.eof() );
2056 #else // !wxUSE_STD_IOSTREAM
2058 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2061 wxFFile
file(filename
, wxT("rb"));
2063 wxFile
file(filename
, wxFile::read
);
2065 if ( !file
.IsOpened() )
2073 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2077 stream
.Write(buf
, nRead
);
2081 while ( !file
.Eof() );
2086 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2089 wxFFile
file(filename
, wxT("wb"));
2091 wxFile
file(filename
, wxFile::write
);
2093 if ( !file
.IsOpened() )
2099 stream
.Read(buf
, WXSIZEOF(buf
));
2101 const size_t nRead
= stream
.LastRead();
2110 if ( !file
.Write(buf
, nRead
) )
2117 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2119 #endif // wxUSE_DOC_VIEW_ARCHITECTURE