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/ptr_scpd.h"
63 #if wxUSE_STD_IOSTREAM
64 #include "wx/ioswrap.h"
65 #include "wx/beforestd.h"
71 #include "wx/afterstd.h"
73 #include "wx/wfstream.h"
76 typedef wxVector
<wxDocTemplate
*> wxDocTemplates
;
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
83 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
84 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
85 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
86 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
87 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
89 #if wxUSE_PRINTING_ARCHITECTURE
90 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
93 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
95 // ============================================================================
97 // ============================================================================
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
106 wxWindow
*wxFindSuitableParent()
108 wxWindow
* const win
= wxGetTopLevelParent(wxWindow::FindFocus());
110 return win
? win
: wxTheApp
->GetTopWindow();
113 wxString
FindExtension(const wxString
& path
)
116 wxFileName::SplitPath(path
, NULL
, NULL
, &ext
);
118 // VZ: extensions are considered not case sensitive - is this really a good
120 return ext
.MakeLower();
123 // return the string used for the MRU list items in the menu
125 // NB: the index n is 0-based, as usual, but the strings start from 1
126 wxString
GetMRUEntryLabel(int n
, const wxString
& path
)
128 // we need to quote '&' characters which are used for mnemonics
129 wxString
pathInMenu(path
);
130 pathInMenu
.Replace("&", "&&");
132 return wxString::Format("&%d %s", n
+ 1, pathInMenu
);
135 } // anonymous namespace
137 // ----------------------------------------------------------------------------
138 // Definition of wxDocument
139 // ----------------------------------------------------------------------------
141 wxDocument::wxDocument(wxDocument
*parent
)
143 m_documentModified
= false;
144 m_documentParent
= parent
;
145 m_documentTemplate
= NULL
;
146 m_commandProcessor
= NULL
;
150 bool wxDocument::DeleteContents()
155 wxDocument::~wxDocument()
159 delete m_commandProcessor
;
161 if (GetDocumentManager())
162 GetDocumentManager()->RemoveDocument(this);
164 // Not safe to do here, since it'll invoke virtual view functions
165 // expecting to see valid derived objects: and by the time we get here,
166 // we've called destructors higher up.
170 bool wxDocument::Close()
172 if ( !OnSaveModified() )
175 return OnCloseDocument();
178 bool wxDocument::OnCloseDocument()
180 // Tell all views that we're about to close
187 // Note that this implicitly deletes the document when the last view is
189 bool wxDocument::DeleteAllViews()
191 wxDocManager
* manager
= GetDocumentManager();
193 // first check if all views agree to be closed
194 const wxList::iterator end
= m_documentViews
.end();
195 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
197 wxView
*view
= (wxView
*)*i
;
198 if ( !view
->Close() )
202 // all views agreed to close, now do close them
203 if ( m_documentViews
.empty() )
205 // normally the document would be implicitly deleted when the last view
206 // is, but if don't have any views, do it here instead
207 if ( manager
&& manager
->GetDocuments().Member(this) )
212 // as we delete elements we iterate over, don't use the usual "from
213 // begin to end" loop
216 wxView
*view
= (wxView
*)*m_documentViews
.begin();
218 bool isLastOne
= m_documentViews
.size() == 1;
220 // this always deletes the node implicitly and if this is the last
221 // view also deletes this object itself (also implicitly, great),
222 // so we can't test for m_documentViews.empty() after calling this!
233 wxView
*wxDocument::GetFirstView() const
235 if ( m_documentViews
.empty() )
238 return static_cast<wxView
*>(m_documentViews
.GetFirst()->GetData());
241 wxDocManager
*wxDocument::GetDocumentManager() const
243 return m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : NULL
;
246 bool wxDocument::OnNewDocument()
248 if ( !OnSaveModified() )
253 SetDocumentSaved(false);
255 const wxString name
= GetDocumentManager()->MakeNewDocumentName();
257 SetFilename(name
, true);
262 bool wxDocument::Save()
264 if ( AlreadySaved() )
267 if ( m_documentFile
.empty() || !m_savedYet
)
270 return OnSaveDocument(m_documentFile
);
273 bool wxDocument::SaveAs()
275 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
279 #ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
280 wxString filter
= docTemplate
->GetDescription() + wxT(" (") +
281 docTemplate
->GetFileFilter() + wxT(")|") +
282 docTemplate
->GetFileFilter();
284 // Now see if there are some other template with identical view and document
285 // classes, whose filters may also be used.
286 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
288 wxList::compatibility_iterator
289 node
= docTemplate
->GetDocumentManager()->GetTemplates().GetFirst();
292 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
294 if (t
->IsVisible() && t
!= docTemplate
&&
295 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
296 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
298 // add a '|' to separate this filter from the previous one
299 if ( !filter
.empty() )
302 filter
<< t
->GetDescription()
303 << wxT(" (") << t
->GetFileFilter() << wxT(") |")
304 << t
->GetFileFilter();
307 node
= node
->GetNext();
311 wxString filter
= docTemplate
->GetFileFilter() ;
314 wxString defaultDir
= docTemplate
->GetDirectory();
315 if ( defaultDir
.empty() )
317 defaultDir
= wxPathOnly(GetFilename());
318 if ( defaultDir
.empty() )
319 defaultDir
= GetDocumentManager()->GetLastDirectory();
322 wxString fileName
= wxFileSelector(_("Save As"),
324 wxFileNameFromPath(GetFilename()),
325 docTemplate
->GetDefaultExtension(),
327 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
328 GetDocumentWindow());
330 if (fileName
.empty())
331 return false; // cancelled by user
334 wxFileName::SplitPath(fileName
, NULL
, NULL
, &ext
);
338 fileName
+= wxT(".");
339 fileName
+= docTemplate
->GetDefaultExtension();
342 // Files that were not saved correctly are not added to the FileHistory.
343 if (!OnSaveDocument(fileName
))
346 SetTitle(wxFileNameFromPath(fileName
));
347 SetFilename(fileName
, true); // will call OnChangeFileName automatically
349 // A file that doesn't use the default extension of its document template
350 // cannot be opened via the FileHistory, so we do not add it.
351 if (docTemplate
->FileMatchesTemplate(fileName
))
353 GetDocumentManager()->AddFileToHistory(fileName
);
355 //else: the user will probably not be able to open the file again, so we
356 // could warn about the wrong file-extension here
361 bool wxDocument::OnSaveDocument(const wxString
& file
)
366 if ( !DoSaveDocument(file
) )
371 SetDocumentSaved(true);
372 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
373 wxFileName
fn(file
) ;
374 fn
.MacSetDefaultTypeAndCreator() ;
379 bool wxDocument::OnOpenDocument(const wxString
& file
)
381 if ( !OnSaveModified() )
384 if ( !DoOpenDocument(file
) )
387 SetFilename(file
, true);
396 #if wxUSE_STD_IOSTREAM
397 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
399 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
405 #if wxUSE_STD_IOSTREAM
406 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
408 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
414 bool wxDocument::Revert()
420 // Get title, or filename if no title, else unnamed
421 #if WXWIN_COMPATIBILITY_2_8
422 bool wxDocument::GetPrintableName(wxString
& buf
) const
424 // this function can not only be overridden by the user code but also
425 // called by it so we need to ensure that we return the same thing as
426 // GetUserReadableName() but we can't call it because this would result in
427 // an infinite recursion, hence we use the helper DoGetUserReadableName()
428 buf
= DoGetUserReadableName();
432 #endif // WXWIN_COMPATIBILITY_2_8
434 wxString
wxDocument::GetUserReadableName() const
436 #if WXWIN_COMPATIBILITY_2_8
437 // we need to call the old virtual function to ensure that the overridden
438 // version of it is still called
440 if ( GetPrintableName(name
) )
442 #endif // WXWIN_COMPATIBILITY_2_8
444 return DoGetUserReadableName();
447 wxString
wxDocument::DoGetUserReadableName() const
449 if ( !m_documentTitle
.empty() )
450 return m_documentTitle
;
452 if ( !m_documentFile
.empty() )
453 return wxFileNameFromPath(m_documentFile
);
458 wxWindow
*wxDocument::GetDocumentWindow() const
460 wxView
* const view
= GetFirstView();
462 return view
? view
->GetFrame() : wxTheApp
->GetTopWindow();
465 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
467 return new wxCommandProcessor
;
470 // true if safe to close
471 bool wxDocument::OnSaveModified()
475 switch ( wxMessageBox
479 _("Do you want to save changes to %s?"),
480 GetUserReadableName()
482 wxTheApp
->GetAppDisplayName(),
483 wxYES_NO
| wxCANCEL
| wxICON_QUESTION
| wxCENTRE
,
484 wxFindSuitableParent()
502 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
507 bool wxDocument::AddView(wxView
*view
)
509 if ( !m_documentViews
.Member(view
) )
511 m_documentViews
.Append(view
);
517 bool wxDocument::RemoveView(wxView
*view
)
519 (void)m_documentViews
.DeleteObject(view
);
524 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
526 return GetDocumentTemplate()->CreateView(this, flags
) != NULL
;
529 // Called after a view is added or removed.
530 // The default implementation deletes the document if
531 // there are no more views.
532 void wxDocument::OnChangedViewList()
534 if ( m_documentViews
.empty() && OnSaveModified() )
538 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
540 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
543 wxView
*view
= (wxView
*)node
->GetData();
545 view
->OnUpdate(sender
, hint
);
546 node
= node
->GetNext();
550 void wxDocument::NotifyClosing()
552 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
555 wxView
*view
= (wxView
*)node
->GetData();
556 view
->OnClosingDocument();
557 node
= node
->GetNext();
561 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
563 m_documentFile
= filename
;
564 OnChangeFilename(notifyViews
);
567 void wxDocument::OnChangeFilename(bool notifyViews
)
571 // Notify the views that the filename has changed
572 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
575 wxView
*view
= (wxView
*)node
->GetData();
576 view
->OnChangeFilename();
577 node
= node
->GetNext();
582 bool wxDocument::DoSaveDocument(const wxString
& file
)
584 #if wxUSE_STD_IOSTREAM
585 wxSTD ofstream
store(file
.mb_str(), wxSTD
ios::binary
);
588 wxFileOutputStream
store(file
);
589 if ( store
.GetLastError() != wxSTREAM_NO_ERROR
)
592 wxLogError(_("File \"%s\" could not be opened for writing."), file
);
596 if (!SaveObject(store
))
598 wxLogError(_("Failed to save document to the file \"%s\"."), file
);
605 bool wxDocument::DoOpenDocument(const wxString
& file
)
607 #if wxUSE_STD_IOSTREAM
608 wxSTD ifstream
store(file
.mb_str(), wxSTD
ios::binary
);
611 wxFileInputStream
store(file
);
612 if (store
.GetLastError() != wxSTREAM_NO_ERROR
|| !store
.IsOk())
615 wxLogError(_("File \"%s\" could not be opened for reading."), file
);
619 #if wxUSE_STD_IOSTREAM
623 int res
= LoadObject(store
).GetLastError();
624 if ( res
!= wxSTREAM_NO_ERROR
&& res
!= wxSTREAM_EOF
)
627 wxLogError(_("Failed to read document from the file \"%s\"."), file
);
635 // ----------------------------------------------------------------------------
637 // ----------------------------------------------------------------------------
641 m_viewDocument
= NULL
;
648 GetDocumentManager()->ActivateView(this, false);
649 m_viewDocument
->RemoveView(this);
652 bool wxView::TryValidator(wxEvent
& event
)
654 wxDocument
* const doc
= GetDocument();
655 return doc
&& doc
->ProcessEventHere(event
);
658 void wxView::OnActivateView(bool WXUNUSED(activate
),
659 wxView
*WXUNUSED(activeView
),
660 wxView
*WXUNUSED(deactiveView
))
664 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
669 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
673 void wxView::OnChangeFilename()
675 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
676 // generic MDI implementation so use SetLabel rather than SetTitle.
677 // It should cause SetTitle() for top level windows.
678 wxWindow
*win
= GetFrame();
681 wxDocument
*doc
= GetDocument();
684 win
->SetLabel(doc
->GetUserReadableName());
687 void wxView::SetDocument(wxDocument
*doc
)
689 m_viewDocument
= doc
;
694 bool wxView::Close(bool deleteWindow
)
696 return OnClose(deleteWindow
);
699 void wxView::Activate(bool activate
)
701 if (GetDocument() && GetDocumentManager())
703 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
704 GetDocumentManager()->ActivateView(this, activate
);
708 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
710 return GetDocument() ? GetDocument()->Close() : true;
713 #if wxUSE_PRINTING_ARCHITECTURE
714 wxPrintout
*wxView::OnCreatePrintout()
716 return new wxDocPrintout(this);
718 #endif // wxUSE_PRINTING_ARCHITECTURE
720 // ----------------------------------------------------------------------------
722 // ----------------------------------------------------------------------------
724 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
725 const wxString
& descr
,
726 const wxString
& filter
,
729 const wxString
& docTypeName
,
730 const wxString
& viewTypeName
,
731 wxClassInfo
*docClassInfo
,
732 wxClassInfo
*viewClassInfo
,
735 m_documentManager
= manager
;
736 m_description
= descr
;
739 m_fileFilter
= filter
;
741 m_docTypeName
= docTypeName
;
742 m_viewTypeName
= viewTypeName
;
743 m_documentManager
->AssociateTemplate(this);
745 m_docClassInfo
= docClassInfo
;
746 m_viewClassInfo
= viewClassInfo
;
749 wxDocTemplate::~wxDocTemplate()
751 m_documentManager
->DisassociateTemplate(this);
754 // Tries to dynamically construct an object of the right class.
755 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
757 wxDocument
* const doc
= DoCreateDocument();
759 // VZ: this code doesn't delete doc if InitDocument() (i.e. doc->OnCreate())
760 // fails, is this intentional?
762 return doc
&& InitDocument(doc
, path
, flags
) ? doc
: NULL
;
766 wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
768 doc
->SetFilename(path
);
769 doc
->SetDocumentTemplate(this);
770 GetDocumentManager()->AddDocument(doc
);
771 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
773 if (doc
->OnCreate(path
, flags
))
776 if (GetDocumentManager()->GetDocuments().Member(doc
))
777 doc
->DeleteAllViews();
781 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
783 wxScopedPtr
<wxView
> view(DoCreateView());
787 view
->SetDocument(doc
);
788 if ( !view
->OnCreate(doc
, flags
) )
791 return view
.release();
794 // The default (very primitive) format detection: check is the extension is
795 // that of the template
796 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
798 wxStringTokenizer
parser (GetFileFilter(), wxT(";"));
799 wxString anything
= wxT ("*");
800 while (parser
.HasMoreTokens())
802 wxString filter
= parser
.GetNextToken();
803 wxString filterExt
= FindExtension (filter
);
804 if ( filter
.IsSameAs (anything
) ||
805 filterExt
.IsSameAs (anything
) ||
806 filterExt
.IsSameAs (FindExtension (path
)) )
809 return GetDefaultExtension().IsSameAs(FindExtension(path
));
812 wxDocument
*wxDocTemplate::DoCreateDocument()
817 return static_cast<wxDocument
*>(m_docClassInfo
->CreateObject());
820 wxView
*wxDocTemplate::DoCreateView()
822 if (!m_viewClassInfo
)
825 return static_cast<wxView
*>(m_viewClassInfo
->CreateObject());
828 // ----------------------------------------------------------------------------
830 // ----------------------------------------------------------------------------
832 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
833 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
834 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
835 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
836 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
837 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
838 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
839 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
840 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
841 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
843 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
844 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateDisableIfNoDoc
)
845 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateDisableIfNoDoc
)
846 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateDisableIfNoDoc
)
847 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
848 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
849 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateDisableIfNoDoc
)
850 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
851 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
853 #if wxUSE_PRINTING_ARCHITECTURE
854 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
855 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
857 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdateDisableIfNoDoc
)
858 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdateDisableIfNoDoc
)
862 wxDocManager
* wxDocManager::sm_docManager
= NULL
;
864 wxDocManager::wxDocManager(long WXUNUSED(flags
), bool initialize
)
866 wxASSERT_MSG( !sm_docManager
, "multiple wxDocManagers not allowed" );
868 sm_docManager
= this;
870 m_defaultDocumentNameCounter
= 1;
871 m_currentView
= NULL
;
872 m_maxDocsOpen
= INT_MAX
;
873 m_fileHistory
= NULL
;
878 wxDocManager::~wxDocManager()
881 delete m_fileHistory
;
882 sm_docManager
= NULL
;
885 // closes the specified document
886 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
888 if ( !doc
->Close() && !force
)
891 // Implicitly deletes the document when
892 // the last view is deleted
893 doc
->DeleteAllViews();
895 // Check we're really deleted
896 if (m_docs
.Member(doc
))
902 bool wxDocManager::CloseDocuments(bool force
)
904 wxList::compatibility_iterator node
= m_docs
.GetFirst();
907 wxDocument
*doc
= (wxDocument
*)node
->GetData();
908 wxList::compatibility_iterator next
= node
->GetNext();
910 if (!CloseDocument(doc
, force
))
913 // This assumes that documents are not connected in
914 // any way, i.e. deleting one document does NOT
921 bool wxDocManager::Clear(bool force
)
923 if (!CloseDocuments(force
))
926 m_currentView
= NULL
;
928 wxList::compatibility_iterator node
= m_templates
.GetFirst();
931 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
932 wxList::compatibility_iterator next
= node
->GetNext();
939 bool wxDocManager::Initialize()
941 m_fileHistory
= OnCreateFileHistory();
945 wxString
wxDocManager::GetLastDirectory() const
947 // use the system-dependent default location for the document files if
948 // we're being opened for the first time
949 if ( m_lastDirectory
.empty() )
951 wxDocManager
* const self
= const_cast<wxDocManager
*>(this);
952 self
->m_lastDirectory
= wxStandardPaths::Get().GetAppDocumentsDir();
955 return m_lastDirectory
;
958 wxFileHistory
*wxDocManager::OnCreateFileHistory()
960 return new wxFileHistory
;
963 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
965 wxDocument
*doc
= GetCurrentDocument();
970 doc
->DeleteAllViews();
971 if (m_docs
.Member(doc
))
976 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
978 CloseDocuments(false);
981 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
986 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
988 if ( !CreateDocument("") )
994 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
996 wxDocument
*doc
= GetCurrentDocument();
1002 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1004 wxDocument
*doc
= GetCurrentDocument();
1010 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
1012 wxDocument
*doc
= GetCurrentDocument();
1018 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
1020 #if wxUSE_PRINTING_ARCHITECTURE
1021 wxView
*view
= GetCurrentView();
1025 wxPrintout
*printout
= view
->OnCreatePrintout();
1029 printer
.Print(view
->GetFrame(), printout
, true);
1033 #endif // wxUSE_PRINTING_ARCHITECTURE
1036 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1038 #if wxUSE_PRINTING_ARCHITECTURE
1039 wxView
*view
= GetCurrentView();
1043 wxPrintout
*printout
= view
->OnCreatePrintout();
1046 // Pass two printout objects: for preview, and possible printing.
1047 wxPrintPreviewBase
*
1048 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1049 if ( !preview
->Ok() )
1052 wxLogError(_("Print preview creation failed."));
1057 frame
= new wxPreviewFrame(preview
, wxTheApp
->GetTopWindow(),
1058 _("Print Preview"));
1059 frame
->Centre(wxBOTH
);
1060 frame
->Initialize();
1063 #endif // wxUSE_PRINTING_ARCHITECTURE
1066 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1068 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1078 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1080 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1090 // Handlers for UI update commands
1092 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1094 // CreateDocument() (which is called from OnFileOpen) may succeed
1095 // only when there is at least a template:
1096 event
.Enable( GetTemplates().GetCount()>0 );
1099 void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent
& event
)
1101 event
.Enable( GetCurrentDocument() != NULL
);
1104 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1106 // CreateDocument() (which is called from OnFileNew) may succeed
1107 // only when there is at least a template:
1108 event
.Enable( GetTemplates().GetCount()>0 );
1111 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1113 wxDocument
* const doc
= GetCurrentDocument();
1114 event
.Enable( doc
&& !doc
->AlreadySaved() );
1117 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1119 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1122 event
.Enable(false);
1126 event
.Enable(cmdproc
->CanUndo());
1127 cmdproc
->SetMenuStrings();
1130 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1132 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1135 event
.Enable(false);
1139 event
.Enable(cmdproc
->CanRedo());
1140 cmdproc
->SetMenuStrings();
1143 wxView
*wxDocManager::GetCurrentView() const
1146 return m_currentView
;
1147 if (m_docs
.GetCount() == 1)
1149 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1150 return doc
->GetFirstView();
1155 bool wxDocManager::TryValidator(wxEvent
& event
)
1157 wxView
* const view
= GetCurrentView();
1158 return view
&& view
->ProcessEventHere(event
);
1164 // helper function: return only the visible templates
1165 wxDocTemplates
GetVisibleTemplates(const wxList
& allTemplates
)
1167 // select only the visible templates
1168 const size_t totalNumTemplates
= allTemplates
.GetCount();
1169 wxDocTemplates templates
;
1170 if ( totalNumTemplates
)
1172 templates
.reserve(totalNumTemplates
);
1174 for ( wxList::const_iterator i
= allTemplates
.begin(),
1175 end
= allTemplates
.end();
1179 wxDocTemplate
* const temp
= (wxDocTemplate
*)*i
;
1180 if ( temp
->IsVisible() )
1181 templates
.push_back(temp
);
1188 } // anonymous namespace
1190 wxDocument
*wxDocManager::CreateDocument(const wxString
& pathOrig
, long flags
)
1192 // this ought to be const but SelectDocumentType/Path() are not
1193 // const-correct and can't be changed as, being virtual, this risks
1194 // breaking user code overriding them
1195 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1196 const size_t numTemplates
= templates
.size();
1197 if ( !numTemplates
)
1199 // no templates can be used, can't create document
1204 // normally user should select the template to use but wxDOC_SILENT flag we
1205 // choose one ourselves
1206 wxString path
= pathOrig
; // may be modified below
1207 wxDocTemplate
*temp
;
1208 if ( flags
& wxDOC_SILENT
)
1210 wxASSERT_MSG( !path
.empty(),
1211 "using empty path with wxDOC_SILENT doesn't make sense" );
1213 temp
= FindTemplateForPath(path
);
1216 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1220 else // not silent, ask the user
1222 // for the new file we need just the template, for an existing one we
1223 // need the template and the path, unless it's already specified
1224 if ( (flags
& wxDOC_NEW
) || !path
.empty() )
1225 temp
= SelectDocumentType(&templates
[0], numTemplates
);
1227 temp
= SelectDocumentPath(&templates
[0], numTemplates
, path
, flags
);
1233 // check whether the document with this path is already opened
1234 if ( !path
.empty() )
1236 const wxFileName
fn(path
);
1237 for ( wxList::const_iterator i
= m_docs
.begin(); i
!= m_docs
.end(); ++i
)
1239 wxDocument
* const doc
= (wxDocument
*)*i
;
1241 if ( fn
== doc
->GetFilename() )
1243 // file already open, just activate it and return
1244 if ( doc
->GetFirstView() )
1246 ActivateView(doc
->GetFirstView());
1247 if ( doc
->GetDocumentWindow() )
1248 doc
->GetDocumentWindow()->SetFocus();
1256 // no, we need to create a new document
1259 // if we've reached the max number of docs, close the first one.
1260 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1262 if ( !CloseDocument((wxDocument
*)GetDocuments().GetFirst()->GetData()) )
1264 // can't open the new document if closing the old one failed
1270 // do create and initialize the new document finally
1271 wxDocument
* const docNew
= temp
->CreateDocument(path
, flags
);
1275 docNew
->SetDocumentName(temp
->GetDocumentName());
1276 docNew
->SetDocumentTemplate(temp
);
1278 // call the appropriate function depending on whether we're creating a new
1279 // file or opening an existing one
1280 if ( !(flags
& wxDOC_NEW
? docNew
->OnNewDocument()
1281 : docNew
->OnOpenDocument(path
)) )
1283 // Document is implicitly deleted by DeleteAllViews
1284 docNew
->DeleteAllViews();
1288 // add the successfully opened file to MRU, but only if we're going to be
1289 // able to reopen it successfully later which requires the template for
1290 // this document to be retrievable from the file extension
1291 if ( !(flags
& wxDOC_NEW
) && temp
->FileMatchesTemplate(path
) )
1292 AddFileToHistory(path
);
1297 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1299 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1300 const size_t numTemplates
= templates
.size();
1302 if ( numTemplates
== 0 )
1305 wxDocTemplate
* const
1306 temp
= numTemplates
== 1 ? templates
[0]
1307 : SelectViewType(&templates
[0], numTemplates
);
1312 wxView
*view
= temp
->CreateView(doc
, flags
);
1314 view
->SetViewName(temp
->GetViewName());
1318 // Not yet implemented
1320 wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1324 // Not yet implemented
1325 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1330 wxDocument
*wxDocManager::GetCurrentDocument() const
1332 wxView
* const view
= GetCurrentView();
1333 return view
? view
->GetDocument() : NULL
;
1336 wxCommandProcessor
*wxDocManager::GetCurrentCommandProcessor() const
1338 wxDocument
* const doc
= GetCurrentDocument();
1339 return doc
? doc
->GetCommandProcessor() : NULL
;
1342 // Make a default name for a new document
1343 #if WXWIN_COMPATIBILITY_2_8
1344 bool wxDocManager::MakeDefaultName(wxString
& WXUNUSED(name
))
1346 // we consider that this function can only be overridden by the user code,
1347 // not called by it as it only makes sense to call it internally, so we
1348 // don't bother to return anything from here
1351 #endif // WXWIN_COMPATIBILITY_2_8
1353 wxString
wxDocManager::MakeNewDocumentName()
1357 #if WXWIN_COMPATIBILITY_2_8
1358 if ( !MakeDefaultName(name
) )
1359 #endif // WXWIN_COMPATIBILITY_2_8
1361 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1362 m_defaultDocumentNameCounter
++;
1368 // Make a frame title (override this to do something different)
1369 // If docName is empty, a document is not currently active.
1370 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1372 wxString appName
= wxTheApp
->GetAppDisplayName();
1378 wxString docName
= doc
->GetUserReadableName();
1379 title
= docName
+ wxString(_(" - ")) + appName
;
1385 // Not yet implemented
1386 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1391 // File history management
1392 void wxDocManager::AddFileToHistory(const wxString
& file
)
1395 m_fileHistory
->AddFileToHistory(file
);
1398 void wxDocManager::RemoveFileFromHistory(size_t i
)
1401 m_fileHistory
->RemoveFileFromHistory(i
);
1404 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1409 histFile
= m_fileHistory
->GetHistoryFile(i
);
1414 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1417 m_fileHistory
->UseMenu(menu
);
1420 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1423 m_fileHistory
->RemoveMenu(menu
);
1427 void wxDocManager::FileHistoryLoad(const wxConfigBase
& config
)
1430 m_fileHistory
->Load(config
);
1433 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1436 m_fileHistory
->Save(config
);
1440 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1443 m_fileHistory
->AddFilesToMenu(menu
);
1446 void wxDocManager::FileHistoryAddFilesToMenu()
1449 m_fileHistory
->AddFilesToMenu();
1452 size_t wxDocManager::GetHistoryFilesCount() const
1454 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1458 // Find out the document template via matching in the document file format
1459 // against that of the template
1460 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1462 wxDocTemplate
*theTemplate
= NULL
;
1464 // Find the template which this extension corresponds to
1465 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1467 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1468 if ( temp
->FileMatchesTemplate(path
) )
1477 // Prompts user to open a file, using file specs in templates.
1478 // Must extend the file selector dialog or implement own; OR
1479 // match the extension to the template extension.
1481 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1484 long WXUNUSED(flags
),
1485 bool WXUNUSED(save
))
1487 #ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
1490 for (int i
= 0; i
< noTemplates
; i
++)
1492 if (templates
[i
]->IsVisible())
1494 // add a '|' to separate this filter from the previous one
1495 if ( !descrBuf
.empty() )
1496 descrBuf
<< wxT('|');
1498 descrBuf
<< templates
[i
]->GetDescription()
1499 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1500 << templates
[i
]->GetFileFilter();
1504 wxString descrBuf
= wxT("*.*");
1505 wxUnusedVar(noTemplates
);
1508 int FilterIndex
= -1;
1510 wxWindow
* parent
= wxFindSuitableParent();
1512 wxString pathTmp
= wxFileSelectorEx(_("Open File"),
1520 wxDocTemplate
*theTemplate
= NULL
;
1521 if (!pathTmp
.empty())
1523 if (!wxFileExists(pathTmp
))
1526 if (!wxTheApp
->GetAppDisplayName().empty())
1527 msgTitle
= wxTheApp
->GetAppDisplayName();
1529 msgTitle
= wxString(_("File error"));
1531 wxMessageBox(_("Sorry, could not open this file."),
1533 wxOK
| wxICON_EXCLAMATION
| wxCENTRE
,
1536 path
= wxEmptyString
;
1540 SetLastDirectory(wxPathOnly(pathTmp
));
1544 // first choose the template using the extension, if this fails (i.e.
1545 // wxFileSelectorEx() didn't fill it), then use the path
1546 if ( FilterIndex
!= -1 )
1547 theTemplate
= templates
[FilterIndex
];
1549 theTemplate
= FindTemplateForPath(path
);
1552 // Since we do not add files with non-default extensions to the
1553 // file history this can only happen if the application changes the
1554 // allowed templates in runtime.
1555 wxMessageBox(_("Sorry, the format for this file is unknown."),
1557 wxOK
| wxICON_EXCLAMATION
| wxCENTRE
,
1569 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1570 int noTemplates
, bool sort
)
1572 wxArrayString strings
;
1573 wxScopedArray
<wxDocTemplate
*> data(new wxDocTemplate
*[noTemplates
]);
1577 for (i
= 0; i
< noTemplates
; i
++)
1579 if (templates
[i
]->IsVisible())
1583 for (j
= 0; j
< n
; j
++)
1585 //filter out NOT unique documents + view combinations
1586 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1587 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1594 strings
.Add(templates
[i
]->m_description
);
1596 data
[n
] = templates
[i
];
1604 strings
.Sort(); // ascending sort
1605 // Yes, this will be slow, but template lists
1606 // are typically short.
1608 n
= strings
.Count();
1609 for (i
= 0; i
< n
; i
++)
1611 for (j
= 0; j
< noTemplates
; j
++)
1613 if (strings
[i
] == templates
[j
]->m_description
)
1614 data
[i
] = templates
[j
];
1619 wxDocTemplate
*theTemplate
;
1624 // no visible templates, hence nothing to choose from
1629 // don't propose the user to choose if he has no choice
1630 theTemplate
= data
[0];
1634 // propose the user to choose one of several
1635 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1637 _("Select a document template"),
1640 (void **)data
.get(),
1641 wxFindSuitableParent()
1648 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1649 int noTemplates
, bool sort
)
1651 wxArrayString strings
;
1652 wxScopedArray
<wxDocTemplate
*> data(new wxDocTemplate
*[noTemplates
]);
1656 for (i
= 0; i
< noTemplates
; i
++)
1658 wxDocTemplate
*templ
= templates
[i
];
1659 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1663 for (j
= 0; j
< n
; j
++)
1665 //filter out NOT unique views
1666 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1672 strings
.Add(templ
->m_viewTypeName
);
1681 strings
.Sort(); // ascending sort
1682 // Yes, this will be slow, but template lists
1683 // are typically short.
1685 n
= strings
.Count();
1686 for (i
= 0; i
< n
; i
++)
1688 for (j
= 0; j
< noTemplates
; j
++)
1690 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1691 data
[i
] = templates
[j
];
1696 wxDocTemplate
*theTemplate
;
1698 // the same logic as above
1706 theTemplate
= data
[0];
1710 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1712 _("Select a document view"),
1715 (void **)data
.get(),
1716 wxFindSuitableParent()
1724 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1726 if (!m_templates
.Member(temp
))
1727 m_templates
.Append(temp
);
1730 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1732 m_templates
.DeleteObject(temp
);
1735 // Add and remove a document from the manager's list
1736 void wxDocManager::AddDocument(wxDocument
*doc
)
1738 if (!m_docs
.Member(doc
))
1742 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1744 m_docs
.DeleteObject(doc
);
1747 // Views or windows should inform the document manager
1748 // when a view is going in or out of focus
1749 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1753 m_currentView
= view
;
1757 if ( m_currentView
== view
)
1759 // don't keep stale pointer
1760 m_currentView
= NULL
;
1765 // ----------------------------------------------------------------------------
1766 // Default document child frame
1767 // ----------------------------------------------------------------------------
1769 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1770 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1771 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1774 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1778 const wxString
& title
,
1782 const wxString
& name
)
1783 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1785 m_childDocument
= doc
;
1788 view
->SetFrame(this);
1791 bool wxDocChildFrame::TryValidator(wxEvent
& event
)
1796 // FIXME: why is this needed here?
1797 m_childView
->Activate(true);
1799 return m_childView
->ProcessEventHere(event
);
1802 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1804 wxFrame::OnActivate(event
);
1807 m_childView
->Activate(event
.GetActive());
1810 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1815 // passing false to Close() means to not delete associated window
1816 if ( event
.CanVeto() && !m_childView
->Close(false) )
1822 m_childView
->Activate(false);
1825 m_childDocument
= NULL
;
1830 // ----------------------------------------------------------------------------
1831 // Default parent frame
1832 // ----------------------------------------------------------------------------
1834 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1835 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1836 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1837 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1840 wxDocParentFrame::wxDocParentFrame()
1842 m_docManager
= NULL
;
1845 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1848 const wxString
& title
,
1852 const wxString
& name
)
1853 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1855 m_docManager
= manager
;
1858 bool wxDocParentFrame::Create(wxDocManager
*manager
,
1861 const wxString
& title
,
1865 const wxString
& name
)
1867 m_docManager
= manager
;
1868 return base_type::Create(frame
, id
, title
, pos
, size
, style
, name
);
1871 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1876 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1878 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1879 wxString
filename(m_docManager
->GetHistoryFile(n
));
1880 if ( filename
.empty() )
1883 wxString errMsg
; // must contain exactly one "%s" if non-empty
1884 if ( wxFile::Exists(filename
) )
1887 if ( m_docManager
->CreateDocument(filename
, wxDOC_SILENT
) )
1890 errMsg
= _("The file '%s' couldn't be opened.");
1892 else // file doesn't exist
1894 errMsg
= _("The file '%s' doesn't exist and couldn't be opened.");
1898 wxASSERT_MSG( !errMsg
.empty(), "should have an error message" );
1900 // remove the file which we can't open from the MRU list
1901 m_docManager
->RemoveFileFromHistory(n
);
1903 // and tell the user about it
1904 wxLogError(errMsg
+ '\n' +
1905 _("It has been removed from the most recently used files list."),
1909 // Extend event processing to search the view's event table
1910 bool wxDocParentFrame::TryValidator(wxEvent
& event
)
1912 return m_docManager
&& m_docManager
->ProcessEventHere(event
);
1915 // Define the behaviour for the frame closing
1916 // - must delete all frames except for the main one.
1917 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1919 if (m_docManager
->Clear(!event
.CanVeto()))
1927 #if wxUSE_PRINTING_ARCHITECTURE
1929 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1932 m_printoutView
= view
;
1935 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1939 // Get the logical pixels per inch of screen and printer
1940 int ppiScreenX
, ppiScreenY
;
1941 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1942 wxUnusedVar(ppiScreenY
);
1943 int ppiPrinterX
, ppiPrinterY
;
1944 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1945 wxUnusedVar(ppiPrinterY
);
1947 // This scales the DC so that the printout roughly represents the
1948 // the screen scaling. The text point size _should_ be the right size
1949 // but in fact is too small for some reason. This is a detail that will
1950 // need to be addressed at some point but can be fudged for the
1952 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1954 // Now we have to check in case our real page size is reduced
1955 // (e.g. because we're drawing to a print preview memory DC)
1956 int pageWidth
, pageHeight
;
1958 dc
->GetSize(&w
, &h
);
1959 GetPageSizePixels(&pageWidth
, &pageHeight
);
1960 wxUnusedVar(pageHeight
);
1962 // If printer pageWidth == current DC width, then this doesn't
1963 // change. But w might be the preview bitmap width, so scale down.
1964 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1965 dc
->SetUserScale(overallScale
, overallScale
);
1969 m_printoutView
->OnDraw(dc
);
1974 bool wxDocPrintout::HasPage(int pageNum
)
1976 return (pageNum
== 1);
1979 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1981 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1987 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
,
1988 int *selPageFrom
, int *selPageTo
)
1996 #endif // wxUSE_PRINTING_ARCHITECTURE
1998 // ----------------------------------------------------------------------------
1999 // File history (a.k.a. MRU, most recently used, files list)
2000 // ----------------------------------------------------------------------------
2002 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2004 m_fileMaxFiles
= maxFiles
;
2008 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2010 // check if we don't already have this file
2011 const wxFileName
fnNew(file
);
2013 numFiles
= m_fileHistory
.size();
2014 for ( i
= 0; i
< numFiles
; i
++ )
2016 if ( fnNew
== m_fileHistory
[i
] )
2018 // we do have it, move it to the top of the history
2019 RemoveFileFromHistory(i
);
2025 // if we already have a full history, delete the one at the end
2026 if ( numFiles
== m_fileMaxFiles
)
2028 RemoveFileFromHistory(--numFiles
);
2031 // add a new menu item to all file menus (they will be updated below)
2032 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2034 node
= node
->GetNext() )
2036 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2038 if ( !numFiles
&& menu
->GetMenuItemCount() )
2039 menu
->AppendSeparator();
2041 // label doesn't matter, it will be set below anyhow, but it can't
2042 // be empty (this is supposed to indicate a stock item)
2043 menu
->Append(m_idBase
+ numFiles
, " ");
2046 // insert the new file in the beginning of the file history
2047 m_fileHistory
.insert(m_fileHistory
.begin(), file
);
2050 // update the labels in all menus
2051 for ( i
= 0; i
< numFiles
; i
++ )
2053 // if in same directory just show the filename; otherwise the full path
2054 const wxFileName
fnOld(m_fileHistory
[i
]);
2056 wxString pathInMenu
;
2057 if ( fnOld
.GetPath() == fnNew
.GetPath() )
2059 pathInMenu
= fnOld
.GetFullName();
2061 else // file in different directory
2063 // absolute path; could also set relative path
2064 pathInMenu
= m_fileHistory
[i
];
2067 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2069 node
= node
->GetNext() )
2071 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2073 menu
->SetLabel(m_idBase
+ i
, GetMRUEntryLabel(i
, pathInMenu
));
2078 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2080 size_t numFiles
= m_fileHistory
.size();
2081 wxCHECK_RET( i
< numFiles
,
2082 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2084 // delete the element from the array
2085 m_fileHistory
.RemoveAt(i
);
2088 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2090 node
= node
->GetNext() )
2092 wxMenu
* const menu
= (wxMenu
*) node
->GetData();
2094 // shift filenames up
2095 for ( size_t j
= i
; j
< numFiles
; j
++ )
2097 menu
->SetLabel(m_idBase
+ j
, GetMRUEntryLabel(j
, m_fileHistory
[j
]));
2100 // delete the last menu item which is unused now
2101 const wxWindowID lastItemId
= m_idBase
+ numFiles
;
2102 if ( menu
->FindItem(lastItemId
) )
2103 menu
->Delete(lastItemId
);
2105 // delete the last separator too if no more files are left
2106 if ( m_fileHistory
.empty() )
2108 const wxMenuItemList::compatibility_iterator
2109 nodeLast
= menu
->GetMenuItems().GetLast();
2112 wxMenuItem
* const lastMenuItem
= nodeLast
->GetData();
2113 if ( lastMenuItem
->IsSeparator() )
2114 menu
->Delete(lastMenuItem
);
2116 //else: menu is empty somehow
2121 void wxFileHistory::UseMenu(wxMenu
*menu
)
2123 if ( !m_fileMenus
.Member(menu
) )
2124 m_fileMenus
.Append(menu
);
2127 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2129 m_fileMenus
.DeleteObject(menu
);
2133 void wxFileHistory::Load(const wxConfigBase
& config
)
2135 m_fileHistory
.Clear();
2138 buf
.Printf(wxT("file%d"), 1);
2140 wxString historyFile
;
2141 while ((m_fileHistory
.GetCount() < m_fileMaxFiles
) &&
2142 config
.Read(buf
, &historyFile
) && !historyFile
.empty())
2144 m_fileHistory
.Add(historyFile
);
2146 buf
.Printf(wxT("file%d"), (int)m_fileHistory
.GetCount()+1);
2147 historyFile
= wxEmptyString
;
2153 void wxFileHistory::Save(wxConfigBase
& config
)
2156 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2159 buf
.Printf(wxT("file%d"), (int)i
+1);
2160 if (i
< m_fileHistory
.GetCount())
2161 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2163 config
.Write(buf
, wxEmptyString
);
2166 #endif // wxUSE_CONFIG
2168 void wxFileHistory::AddFilesToMenu()
2170 if ( m_fileHistory
.empty() )
2173 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2175 node
= node
->GetNext() )
2177 AddFilesToMenu((wxMenu
*) node
->GetData());
2181 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2183 if ( m_fileHistory
.empty() )
2186 if ( menu
->GetMenuItemCount() )
2187 menu
->AppendSeparator();
2189 for ( size_t i
= 0; i
< m_fileHistory
.GetCount(); i
++ )
2191 menu
->Append(m_idBase
+ i
, GetMRUEntryLabel(i
, m_fileHistory
[i
]));
2195 // ----------------------------------------------------------------------------
2196 // Permits compatibility with existing file formats and functions that
2197 // manipulate files directly
2198 // ----------------------------------------------------------------------------
2200 #if wxUSE_STD_IOSTREAM
2202 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2204 wxFFile
file(filename
, _T("rb"));
2205 if ( !file
.IsOpened() )
2213 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2217 stream
.write(buf
, nRead
);
2221 while ( !file
.Eof() );
2226 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2228 wxFFile
file(filename
, _T("wb"));
2229 if ( !file
.IsOpened() )
2235 stream
.read(buf
, WXSIZEOF(buf
));
2236 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2238 if ( !file
.Write(buf
, stream
.gcount()) )
2242 while ( !stream
.eof() );
2247 #else // !wxUSE_STD_IOSTREAM
2249 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2251 wxFFile
file(filename
, _T("rb"));
2252 if ( !file
.IsOpened() )
2260 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2264 stream
.Write(buf
, nRead
);
2268 while ( !file
.Eof() );
2273 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2275 wxFFile
file(filename
, _T("wb"));
2276 if ( !file
.IsOpened() )
2282 stream
.Read(buf
, WXSIZEOF(buf
));
2284 const size_t nRead
= stream
.LastRead();
2293 if ( !file
.Write(buf
, nRead
) )
2300 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2302 #endif // wxUSE_DOC_VIEW_ARCHITECTURE