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"
64 #if wxUSE_STD_IOSTREAM
65 #include "wx/ioswrap.h"
66 #include "wx/beforestd.h"
72 #include "wx/afterstd.h"
74 #include "wx/wfstream.h"
77 typedef wxVector
<wxDocTemplate
*> wxDocTemplates
;
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
84 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
85 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
86 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
87 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
88 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
90 #if wxUSE_PRINTING_ARCHITECTURE
91 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
94 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
96 // ============================================================================
98 // ============================================================================
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
107 wxWindow
*wxFindSuitableParent()
109 wxWindow
* const win
= wxGetTopLevelParent(wxWindow::FindFocus());
111 return win
? win
: wxTheApp
->GetTopWindow();
114 wxString
FindExtension(const wxString
& path
)
117 wxFileName::SplitPath(path
, NULL
, NULL
, &ext
);
119 // VZ: extensions are considered not case sensitive - is this really a good
121 return ext
.MakeLower();
124 // return the string used for the MRU list items in the menu
126 // NB: the index n is 0-based, as usual, but the strings start from 1
127 wxString
GetMRUEntryLabel(int n
, const wxString
& path
)
129 // we need to quote '&' characters which are used for mnemonics
130 wxString
pathInMenu(path
);
131 pathInMenu
.Replace("&", "&&");
133 return wxString::Format("&%d %s", n
+ 1, pathInMenu
);
136 } // anonymous namespace
138 // ----------------------------------------------------------------------------
139 // Definition of wxDocument
140 // ----------------------------------------------------------------------------
142 wxDocument::wxDocument(wxDocument
*parent
)
144 m_documentModified
= false;
145 m_documentParent
= parent
;
146 m_documentTemplate
= NULL
;
147 m_commandProcessor
= NULL
;
151 bool wxDocument::DeleteContents()
156 wxDocument::~wxDocument()
160 delete m_commandProcessor
;
162 if (GetDocumentManager())
163 GetDocumentManager()->RemoveDocument(this);
165 // Not safe to do here, since it'll invoke virtual view functions
166 // expecting to see valid derived objects: and by the time we get here,
167 // we've called destructors higher up.
171 bool wxDocument::Close()
173 if ( !OnSaveModified() )
176 return OnCloseDocument();
179 bool wxDocument::OnCloseDocument()
181 // Tell all views that we're about to close
188 // Note that this implicitly deletes the document when the last view is
190 bool wxDocument::DeleteAllViews()
192 wxDocManager
* manager
= GetDocumentManager();
194 // first check if all views agree to be closed
195 const wxList::iterator end
= m_documentViews
.end();
196 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
198 wxView
*view
= (wxView
*)*i
;
199 if ( !view
->Close() )
203 // all views agreed to close, now do close them
204 if ( m_documentViews
.empty() )
206 // normally the document would be implicitly deleted when the last view
207 // is, but if don't have any views, do it here instead
208 if ( manager
&& manager
->GetDocuments().Member(this) )
213 // as we delete elements we iterate over, don't use the usual "from
214 // begin to end" loop
217 wxView
*view
= (wxView
*)*m_documentViews
.begin();
219 bool isLastOne
= m_documentViews
.size() == 1;
221 // this always deletes the node implicitly and if this is the last
222 // view also deletes this object itself (also implicitly, great),
223 // so we can't test for m_documentViews.empty() after calling this!
234 wxView
*wxDocument::GetFirstView() const
236 if ( m_documentViews
.empty() )
239 return static_cast<wxView
*>(m_documentViews
.GetFirst()->GetData());
242 wxDocManager
*wxDocument::GetDocumentManager() const
244 return m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : NULL
;
247 bool wxDocument::OnNewDocument()
249 if ( !OnSaveModified() )
254 SetDocumentSaved(false);
256 const wxString name
= GetDocumentManager()->MakeNewDocumentName();
258 SetFilename(name
, true);
263 bool wxDocument::Save()
265 if ( AlreadySaved() )
268 if ( m_documentFile
.empty() || !m_savedYet
)
271 return OnSaveDocument(m_documentFile
);
274 bool wxDocument::SaveAs()
276 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
280 #ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
281 wxString filter
= docTemplate
->GetDescription() + wxT(" (") +
282 docTemplate
->GetFileFilter() + wxT(")|") +
283 docTemplate
->GetFileFilter();
285 // Now see if there are some other template with identical view and document
286 // classes, whose filters may also be used.
287 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
289 wxList::compatibility_iterator
290 node
= docTemplate
->GetDocumentManager()->GetTemplates().GetFirst();
293 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
295 if (t
->IsVisible() && t
!= docTemplate
&&
296 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
297 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
299 // add a '|' to separate this filter from the previous one
300 if ( !filter
.empty() )
303 filter
<< t
->GetDescription()
304 << wxT(" (") << t
->GetFileFilter() << wxT(") |")
305 << t
->GetFileFilter();
308 node
= node
->GetNext();
312 wxString filter
= docTemplate
->GetFileFilter() ;
315 wxString defaultDir
= docTemplate
->GetDirectory();
316 if ( defaultDir
.empty() )
318 defaultDir
= wxPathOnly(GetFilename());
319 if ( defaultDir
.empty() )
320 defaultDir
= GetDocumentManager()->GetLastDirectory();
323 wxString fileName
= wxFileSelector(_("Save As"),
325 wxFileNameFromPath(GetFilename()),
326 docTemplate
->GetDefaultExtension(),
328 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
329 GetDocumentWindow());
331 if (fileName
.empty())
332 return false; // cancelled by user
335 wxFileName::SplitPath(fileName
, NULL
, NULL
, &ext
);
339 fileName
+= wxT(".");
340 fileName
+= docTemplate
->GetDefaultExtension();
343 // Files that were not saved correctly are not added to the FileHistory.
344 if (!OnSaveDocument(fileName
))
347 SetTitle(wxFileNameFromPath(fileName
));
348 SetFilename(fileName
, true); // will call OnChangeFileName automatically
350 // A file that doesn't use the default extension of its document template
351 // cannot be opened via the FileHistory, so we do not add it.
352 if (docTemplate
->FileMatchesTemplate(fileName
))
354 GetDocumentManager()->AddFileToHistory(fileName
);
356 //else: the user will probably not be able to open the file again, so we
357 // could warn about the wrong file-extension here
362 bool wxDocument::OnSaveDocument(const wxString
& file
)
367 if ( !DoSaveDocument(file
) )
372 SetDocumentSaved(true);
373 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
374 wxFileName
fn(file
) ;
375 fn
.MacSetDefaultTypeAndCreator() ;
380 bool wxDocument::OnOpenDocument(const wxString
& file
)
382 if ( !OnSaveModified() )
385 if ( !DoOpenDocument(file
) )
388 SetFilename(file
, true);
397 #if wxUSE_STD_IOSTREAM
398 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
400 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
406 #if wxUSE_STD_IOSTREAM
407 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
409 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
415 bool wxDocument::Revert()
421 // Get title, or filename if no title, else unnamed
422 #if WXWIN_COMPATIBILITY_2_8
423 bool wxDocument::GetPrintableName(wxString
& buf
) const
425 // this function can not only be overridden by the user code but also
426 // called by it so we need to ensure that we return the same thing as
427 // GetUserReadableName() but we can't call it because this would result in
428 // an infinite recursion, hence we use the helper DoGetUserReadableName()
429 buf
= DoGetUserReadableName();
433 #endif // WXWIN_COMPATIBILITY_2_8
435 wxString
wxDocument::GetUserReadableName() const
437 #if WXWIN_COMPATIBILITY_2_8
438 // we need to call the old virtual function to ensure that the overridden
439 // version of it is still called
441 if ( GetPrintableName(name
) )
443 #endif // WXWIN_COMPATIBILITY_2_8
445 return DoGetUserReadableName();
448 wxString
wxDocument::DoGetUserReadableName() const
450 if ( !m_documentTitle
.empty() )
451 return m_documentTitle
;
453 if ( !m_documentFile
.empty() )
454 return wxFileNameFromPath(m_documentFile
);
459 wxWindow
*wxDocument::GetDocumentWindow() const
461 wxView
* const view
= GetFirstView();
463 return view
? view
->GetFrame() : wxTheApp
->GetTopWindow();
466 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
468 return new wxCommandProcessor
;
471 // true if safe to close
472 bool wxDocument::OnSaveModified()
476 switch ( wxMessageBox
480 _("Do you want to save changes to %s?"),
481 GetUserReadableName()
483 wxTheApp
->GetAppDisplayName(),
484 wxYES_NO
| wxCANCEL
| wxICON_QUESTION
| wxCENTRE
,
485 wxFindSuitableParent()
503 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
508 bool wxDocument::AddView(wxView
*view
)
510 if ( !m_documentViews
.Member(view
) )
512 m_documentViews
.Append(view
);
518 bool wxDocument::RemoveView(wxView
*view
)
520 (void)m_documentViews
.DeleteObject(view
);
525 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
527 return GetDocumentTemplate()->CreateView(this, flags
) != NULL
;
530 // Called after a view is added or removed.
531 // The default implementation deletes the document if
532 // there are no more views.
533 void wxDocument::OnChangedViewList()
535 if ( m_documentViews
.empty() && OnSaveModified() )
539 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
541 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
544 wxView
*view
= (wxView
*)node
->GetData();
546 view
->OnUpdate(sender
, hint
);
547 node
= node
->GetNext();
551 void wxDocument::NotifyClosing()
553 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
556 wxView
*view
= (wxView
*)node
->GetData();
557 view
->OnClosingDocument();
558 node
= node
->GetNext();
562 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
564 m_documentFile
= filename
;
565 OnChangeFilename(notifyViews
);
568 void wxDocument::OnChangeFilename(bool notifyViews
)
572 // Notify the views that the filename has changed
573 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
576 wxView
*view
= (wxView
*)node
->GetData();
577 view
->OnChangeFilename();
578 node
= node
->GetNext();
583 bool wxDocument::DoSaveDocument(const wxString
& file
)
585 #if wxUSE_STD_IOSTREAM
586 wxSTD ofstream
store(file
.mb_str(), wxSTD
ios::binary
);
589 wxFileOutputStream
store(file
);
590 if ( store
.GetLastError() != wxSTREAM_NO_ERROR
)
593 wxLogError(_("File \"%s\" could not be opened for writing."), file
);
597 if (!SaveObject(store
))
599 wxLogError(_("Failed to save document to the file \"%s\"."), file
);
606 bool wxDocument::DoOpenDocument(const wxString
& file
)
608 #if wxUSE_STD_IOSTREAM
609 wxSTD ifstream
store(file
.mb_str(), wxSTD
ios::binary
);
612 wxFileInputStream
store(file
);
613 if (store
.GetLastError() != wxSTREAM_NO_ERROR
|| !store
.IsOk())
616 wxLogError(_("File \"%s\" could not be opened for reading."), file
);
620 #if wxUSE_STD_IOSTREAM
624 int res
= LoadObject(store
).GetLastError();
625 if ( res
!= wxSTREAM_NO_ERROR
&& res
!= wxSTREAM_EOF
)
628 wxLogError(_("Failed to read document from the file \"%s\"."), file
);
636 // ----------------------------------------------------------------------------
638 // ----------------------------------------------------------------------------
642 m_viewDocument
= NULL
;
649 GetDocumentManager()->ActivateView(this, false);
650 m_viewDocument
->RemoveView(this);
653 bool wxView::TryValidator(wxEvent
& event
)
655 wxDocument
* const doc
= GetDocument();
656 return doc
&& doc
->ProcessEventHere(event
);
659 void wxView::OnActivateView(bool WXUNUSED(activate
),
660 wxView
*WXUNUSED(activeView
),
661 wxView
*WXUNUSED(deactiveView
))
665 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
670 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
674 void wxView::OnChangeFilename()
676 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
677 // generic MDI implementation so use SetLabel rather than SetTitle.
678 // It should cause SetTitle() for top level windows.
679 wxWindow
*win
= GetFrame();
682 wxDocument
*doc
= GetDocument();
685 win
->SetLabel(doc
->GetUserReadableName());
688 void wxView::SetDocument(wxDocument
*doc
)
690 m_viewDocument
= doc
;
695 bool wxView::Close(bool deleteWindow
)
697 return OnClose(deleteWindow
);
700 void wxView::Activate(bool activate
)
702 if (GetDocument() && GetDocumentManager())
704 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
705 GetDocumentManager()->ActivateView(this, activate
);
709 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
711 return GetDocument() ? GetDocument()->Close() : true;
714 #if wxUSE_PRINTING_ARCHITECTURE
715 wxPrintout
*wxView::OnCreatePrintout()
717 return new wxDocPrintout(this);
719 #endif // wxUSE_PRINTING_ARCHITECTURE
721 // ----------------------------------------------------------------------------
723 // ----------------------------------------------------------------------------
725 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
726 const wxString
& descr
,
727 const wxString
& filter
,
730 const wxString
& docTypeName
,
731 const wxString
& viewTypeName
,
732 wxClassInfo
*docClassInfo
,
733 wxClassInfo
*viewClassInfo
,
736 m_documentManager
= manager
;
737 m_description
= descr
;
740 m_fileFilter
= filter
;
742 m_docTypeName
= docTypeName
;
743 m_viewTypeName
= viewTypeName
;
744 m_documentManager
->AssociateTemplate(this);
746 m_docClassInfo
= docClassInfo
;
747 m_viewClassInfo
= viewClassInfo
;
750 wxDocTemplate::~wxDocTemplate()
752 m_documentManager
->DisassociateTemplate(this);
755 // Tries to dynamically construct an object of the right class.
756 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
758 wxDocument
* const doc
= DoCreateDocument();
760 // VZ: this code doesn't delete doc if InitDocument() (i.e. doc->OnCreate())
761 // fails, is this intentional?
763 return doc
&& InitDocument(doc
, path
, flags
) ? doc
: NULL
;
767 wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
769 doc
->SetFilename(path
);
770 doc
->SetDocumentTemplate(this);
771 GetDocumentManager()->AddDocument(doc
);
772 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
774 if (doc
->OnCreate(path
, flags
))
777 if (GetDocumentManager()->GetDocuments().Member(doc
))
778 doc
->DeleteAllViews();
782 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
784 wxScopedPtr
<wxView
> view(DoCreateView());
788 view
->SetDocument(doc
);
789 if ( !view
->OnCreate(doc
, flags
) )
792 return view
.release();
795 // The default (very primitive) format detection: check is the extension is
796 // that of the template
797 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
799 wxStringTokenizer
parser (GetFileFilter(), wxT(";"));
800 wxString anything
= wxT ("*");
801 while (parser
.HasMoreTokens())
803 wxString filter
= parser
.GetNextToken();
804 wxString filterExt
= FindExtension (filter
);
805 if ( filter
.IsSameAs (anything
) ||
806 filterExt
.IsSameAs (anything
) ||
807 filterExt
.IsSameAs (FindExtension (path
)) )
810 return GetDefaultExtension().IsSameAs(FindExtension(path
));
813 wxDocument
*wxDocTemplate::DoCreateDocument()
818 return static_cast<wxDocument
*>(m_docClassInfo
->CreateObject());
821 wxView
*wxDocTemplate::DoCreateView()
823 if (!m_viewClassInfo
)
826 return static_cast<wxView
*>(m_viewClassInfo
->CreateObject());
829 // ----------------------------------------------------------------------------
831 // ----------------------------------------------------------------------------
833 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
834 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
835 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
836 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
837 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
838 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
839 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
840 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
841 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
842 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
844 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
845 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateDisableIfNoDoc
)
846 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateDisableIfNoDoc
)
847 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateDisableIfNoDoc
)
848 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
849 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
850 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateDisableIfNoDoc
)
851 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
852 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
854 #if wxUSE_PRINTING_ARCHITECTURE
855 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
856 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
858 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdateDisableIfNoDoc
)
859 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdateDisableIfNoDoc
)
863 wxDocManager
* wxDocManager::sm_docManager
= NULL
;
865 wxDocManager::wxDocManager(long WXUNUSED(flags
), bool initialize
)
867 wxASSERT_MSG( !sm_docManager
, "multiple wxDocManagers not allowed" );
869 sm_docManager
= this;
871 m_defaultDocumentNameCounter
= 1;
872 m_currentView
= NULL
;
873 m_maxDocsOpen
= INT_MAX
;
874 m_fileHistory
= NULL
;
879 wxDocManager::~wxDocManager()
882 delete m_fileHistory
;
883 sm_docManager
= NULL
;
886 // closes the specified document
887 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
889 if ( !doc
->Close() && !force
)
892 // Implicitly deletes the document when
893 // the last view is deleted
894 doc
->DeleteAllViews();
896 // Check we're really deleted
897 if (m_docs
.Member(doc
))
903 bool wxDocManager::CloseDocuments(bool force
)
905 wxList::compatibility_iterator node
= m_docs
.GetFirst();
908 wxDocument
*doc
= (wxDocument
*)node
->GetData();
909 wxList::compatibility_iterator next
= node
->GetNext();
911 if (!CloseDocument(doc
, force
))
914 // This assumes that documents are not connected in
915 // any way, i.e. deleting one document does NOT
922 bool wxDocManager::Clear(bool force
)
924 if (!CloseDocuments(force
))
927 m_currentView
= NULL
;
929 wxList::compatibility_iterator node
= m_templates
.GetFirst();
932 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
933 wxList::compatibility_iterator next
= node
->GetNext();
940 bool wxDocManager::Initialize()
942 m_fileHistory
= OnCreateFileHistory();
946 wxString
wxDocManager::GetLastDirectory() const
948 // use the system-dependent default location for the document files if
949 // we're being opened for the first time
950 if ( m_lastDirectory
.empty() )
952 wxDocManager
* const self
= const_cast<wxDocManager
*>(this);
953 self
->m_lastDirectory
= wxStandardPaths::Get().GetAppDocumentsDir();
956 return m_lastDirectory
;
959 wxFileHistory
*wxDocManager::OnCreateFileHistory()
961 return new wxFileHistory
;
964 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
966 wxDocument
*doc
= GetCurrentDocument();
971 doc
->DeleteAllViews();
972 if (m_docs
.Member(doc
))
977 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
979 CloseDocuments(false);
982 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
987 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
989 if ( !CreateDocument("") )
995 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
997 wxDocument
*doc
= GetCurrentDocument();
1003 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1005 wxDocument
*doc
= GetCurrentDocument();
1011 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
1013 wxDocument
*doc
= GetCurrentDocument();
1019 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
1021 #if wxUSE_PRINTING_ARCHITECTURE
1022 wxView
*view
= GetActiveView();
1026 wxPrintout
*printout
= view
->OnCreatePrintout();
1030 printer
.Print(view
->GetFrame(), printout
, true);
1034 #endif // wxUSE_PRINTING_ARCHITECTURE
1037 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1039 #if wxUSE_PRINTING_ARCHITECTURE
1040 wxView
*view
= GetActiveView();
1044 wxPrintout
*printout
= view
->OnCreatePrintout();
1047 // Pass two printout objects: for preview, and possible printing.
1048 wxPrintPreviewBase
*
1049 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1050 if ( !preview
->Ok() )
1053 wxLogError(_("Print preview creation failed."));
1058 frame
= new wxPreviewFrame(preview
, wxTheApp
->GetTopWindow(),
1059 _("Print Preview"));
1060 frame
->Centre(wxBOTH
);
1061 frame
->Initialize();
1064 #endif // wxUSE_PRINTING_ARCHITECTURE
1067 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1069 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1079 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1081 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1091 // Handlers for UI update commands
1093 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1095 // CreateDocument() (which is called from OnFileOpen) may succeed
1096 // only when there is at least a template:
1097 event
.Enable( GetTemplates().GetCount()>0 );
1100 void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent
& event
)
1102 event
.Enable( GetCurrentDocument() != NULL
);
1105 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1107 // CreateDocument() (which is called from OnFileNew) may succeed
1108 // only when there is at least a template:
1109 event
.Enable( GetTemplates().GetCount()>0 );
1112 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1114 wxDocument
* const doc
= GetCurrentDocument();
1115 event
.Enable( doc
&& !doc
->AlreadySaved() );
1118 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1120 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1123 event
.Enable(false);
1127 event
.Enable(cmdproc
->CanUndo());
1128 cmdproc
->SetMenuStrings();
1131 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1133 wxCommandProcessor
* const cmdproc
= GetCurrentCommandProcessor();
1136 event
.Enable(false);
1140 event
.Enable(cmdproc
->CanRedo());
1141 cmdproc
->SetMenuStrings();
1144 wxView
*wxDocManager::GetActiveView() const
1146 wxView
*view
= GetCurrentView();
1148 if ( !view
&& !m_docs
.empty() )
1150 // if we have exactly one document, consider its view to be the current
1153 // VZ: I'm not exactly sure why is this needed but this is how this
1154 // code used to behave before the bug #9518 was fixed and it seems
1155 // safer to preserve the old logic
1156 wxList::compatibility_iterator node
= m_docs
.GetFirst();
1157 if ( !node
->GetNext() )
1159 wxDocument
*doc
= static_cast<wxDocument
*>(node
->GetData());
1160 view
= doc
->GetFirstView();
1162 //else: we have more than one document
1168 bool wxDocManager::TryValidator(wxEvent
& event
)
1170 wxView
* const view
= GetActiveView();
1171 return view
&& view
->ProcessEventHere(event
);
1177 // helper function: return only the visible templates
1178 wxDocTemplates
GetVisibleTemplates(const wxList
& allTemplates
)
1180 // select only the visible templates
1181 const size_t totalNumTemplates
= allTemplates
.GetCount();
1182 wxDocTemplates templates
;
1183 if ( totalNumTemplates
)
1185 templates
.reserve(totalNumTemplates
);
1187 for ( wxList::const_iterator i
= allTemplates
.begin(),
1188 end
= allTemplates
.end();
1192 wxDocTemplate
* const temp
= (wxDocTemplate
*)*i
;
1193 if ( temp
->IsVisible() )
1194 templates
.push_back(temp
);
1201 } // anonymous namespace
1203 wxDocument
*wxDocManager::CreateDocument(const wxString
& pathOrig
, long flags
)
1205 // this ought to be const but SelectDocumentType/Path() are not
1206 // const-correct and can't be changed as, being virtual, this risks
1207 // breaking user code overriding them
1208 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1209 const size_t numTemplates
= templates
.size();
1210 if ( !numTemplates
)
1212 // no templates can be used, can't create document
1217 // normally user should select the template to use but wxDOC_SILENT flag we
1218 // choose one ourselves
1219 wxString path
= pathOrig
; // may be modified below
1220 wxDocTemplate
*temp
;
1221 if ( flags
& wxDOC_SILENT
)
1223 wxASSERT_MSG( !path
.empty(),
1224 "using empty path with wxDOC_SILENT doesn't make sense" );
1226 temp
= FindTemplateForPath(path
);
1229 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1233 else // not silent, ask the user
1235 // for the new file we need just the template, for an existing one we
1236 // need the template and the path, unless it's already specified
1237 if ( (flags
& wxDOC_NEW
) || !path
.empty() )
1238 temp
= SelectDocumentType(&templates
[0], numTemplates
);
1240 temp
= SelectDocumentPath(&templates
[0], numTemplates
, path
, flags
);
1246 // check whether the document with this path is already opened
1247 if ( !path
.empty() )
1249 const wxFileName
fn(path
);
1250 for ( wxList::const_iterator i
= m_docs
.begin(); i
!= m_docs
.end(); ++i
)
1252 wxDocument
* const doc
= (wxDocument
*)*i
;
1254 if ( fn
== doc
->GetFilename() )
1256 // file already open, just activate it and return
1257 if ( doc
->GetFirstView() )
1259 ActivateView(doc
->GetFirstView());
1260 if ( doc
->GetDocumentWindow() )
1261 doc
->GetDocumentWindow()->SetFocus();
1269 // no, we need to create a new document
1272 // if we've reached the max number of docs, close the first one.
1273 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1275 if ( !CloseDocument((wxDocument
*)GetDocuments().GetFirst()->GetData()) )
1277 // can't open the new document if closing the old one failed
1283 // do create and initialize the new document finally
1284 wxDocument
* const docNew
= temp
->CreateDocument(path
, flags
);
1288 docNew
->SetDocumentName(temp
->GetDocumentName());
1289 docNew
->SetDocumentTemplate(temp
);
1291 // call the appropriate function depending on whether we're creating a new
1292 // file or opening an existing one
1293 if ( !(flags
& wxDOC_NEW
? docNew
->OnNewDocument()
1294 : docNew
->OnOpenDocument(path
)) )
1296 // Document is implicitly deleted by DeleteAllViews
1297 docNew
->DeleteAllViews();
1301 // add the successfully opened file to MRU, but only if we're going to be
1302 // able to reopen it successfully later which requires the template for
1303 // this document to be retrievable from the file extension
1304 if ( !(flags
& wxDOC_NEW
) && temp
->FileMatchesTemplate(path
) )
1305 AddFileToHistory(path
);
1310 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1312 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1313 const size_t numTemplates
= templates
.size();
1315 if ( numTemplates
== 0 )
1318 wxDocTemplate
* const
1319 temp
= numTemplates
== 1 ? templates
[0]
1320 : SelectViewType(&templates
[0], numTemplates
);
1325 wxView
*view
= temp
->CreateView(doc
, flags
);
1327 view
->SetViewName(temp
->GetViewName());
1331 // Not yet implemented
1333 wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1337 // Not yet implemented
1338 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1343 wxDocument
*wxDocManager::GetCurrentDocument() const
1345 wxView
* const view
= GetActiveView();
1346 return view
? view
->GetDocument() : NULL
;
1349 wxCommandProcessor
*wxDocManager::GetCurrentCommandProcessor() const
1351 wxDocument
* const doc
= GetCurrentDocument();
1352 return doc
? doc
->GetCommandProcessor() : NULL
;
1355 // Make a default name for a new document
1356 #if WXWIN_COMPATIBILITY_2_8
1357 bool wxDocManager::MakeDefaultName(wxString
& WXUNUSED(name
))
1359 // we consider that this function can only be overridden by the user code,
1360 // not called by it as it only makes sense to call it internally, so we
1361 // don't bother to return anything from here
1364 #endif // WXWIN_COMPATIBILITY_2_8
1366 wxString
wxDocManager::MakeNewDocumentName()
1370 #if WXWIN_COMPATIBILITY_2_8
1371 if ( !MakeDefaultName(name
) )
1372 #endif // WXWIN_COMPATIBILITY_2_8
1374 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1375 m_defaultDocumentNameCounter
++;
1381 // Make a frame title (override this to do something different)
1382 // If docName is empty, a document is not currently active.
1383 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1385 wxString appName
= wxTheApp
->GetAppDisplayName();
1391 wxString docName
= doc
->GetUserReadableName();
1392 title
= docName
+ wxString(_(" - ")) + appName
;
1398 // Not yet implemented
1399 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1404 // File history management
1405 void wxDocManager::AddFileToHistory(const wxString
& file
)
1408 m_fileHistory
->AddFileToHistory(file
);
1411 void wxDocManager::RemoveFileFromHistory(size_t i
)
1414 m_fileHistory
->RemoveFileFromHistory(i
);
1417 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1422 histFile
= m_fileHistory
->GetHistoryFile(i
);
1427 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1430 m_fileHistory
->UseMenu(menu
);
1433 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1436 m_fileHistory
->RemoveMenu(menu
);
1440 void wxDocManager::FileHistoryLoad(const wxConfigBase
& config
)
1443 m_fileHistory
->Load(config
);
1446 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1449 m_fileHistory
->Save(config
);
1453 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1456 m_fileHistory
->AddFilesToMenu(menu
);
1459 void wxDocManager::FileHistoryAddFilesToMenu()
1462 m_fileHistory
->AddFilesToMenu();
1465 size_t wxDocManager::GetHistoryFilesCount() const
1467 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1471 // Find out the document template via matching in the document file format
1472 // against that of the template
1473 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1475 wxDocTemplate
*theTemplate
= NULL
;
1477 // Find the template which this extension corresponds to
1478 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1480 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1481 if ( temp
->FileMatchesTemplate(path
) )
1490 // Prompts user to open a file, using file specs in templates.
1491 // Must extend the file selector dialog or implement own; OR
1492 // match the extension to the template extension.
1494 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1497 long WXUNUSED(flags
),
1498 bool WXUNUSED(save
))
1500 #ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
1503 for (int i
= 0; i
< noTemplates
; i
++)
1505 if (templates
[i
]->IsVisible())
1507 // add a '|' to separate this filter from the previous one
1508 if ( !descrBuf
.empty() )
1509 descrBuf
<< wxT('|');
1511 descrBuf
<< templates
[i
]->GetDescription()
1512 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1513 << templates
[i
]->GetFileFilter();
1517 wxString descrBuf
= wxT("*.*");
1518 wxUnusedVar(noTemplates
);
1521 int FilterIndex
= -1;
1523 wxWindow
* parent
= wxFindSuitableParent();
1525 wxString pathTmp
= wxFileSelectorEx(_("Open File"),
1533 wxDocTemplate
*theTemplate
= NULL
;
1534 if (!pathTmp
.empty())
1536 if (!wxFileExists(pathTmp
))
1539 if (!wxTheApp
->GetAppDisplayName().empty())
1540 msgTitle
= wxTheApp
->GetAppDisplayName();
1542 msgTitle
= wxString(_("File error"));
1544 wxMessageBox(_("Sorry, could not open this file."),
1546 wxOK
| wxICON_EXCLAMATION
| wxCENTRE
,
1549 path
= wxEmptyString
;
1553 SetLastDirectory(wxPathOnly(pathTmp
));
1557 // first choose the template using the extension, if this fails (i.e.
1558 // wxFileSelectorEx() didn't fill it), then use the path
1559 if ( FilterIndex
!= -1 )
1560 theTemplate
= templates
[FilterIndex
];
1562 theTemplate
= FindTemplateForPath(path
);
1565 // Since we do not add files with non-default extensions to the
1566 // file history this can only happen if the application changes the
1567 // allowed templates in runtime.
1568 wxMessageBox(_("Sorry, the format for this file is unknown."),
1570 wxOK
| wxICON_EXCLAMATION
| wxCENTRE
,
1582 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1583 int noTemplates
, bool sort
)
1585 wxArrayString strings
;
1586 wxScopedArray
<wxDocTemplate
*> data(new wxDocTemplate
*[noTemplates
]);
1590 for (i
= 0; i
< noTemplates
; i
++)
1592 if (templates
[i
]->IsVisible())
1596 for (j
= 0; j
< n
; j
++)
1598 //filter out NOT unique documents + view combinations
1599 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1600 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1607 strings
.Add(templates
[i
]->m_description
);
1609 data
[n
] = templates
[i
];
1617 strings
.Sort(); // ascending sort
1618 // Yes, this will be slow, but template lists
1619 // are typically short.
1621 n
= strings
.Count();
1622 for (i
= 0; i
< n
; i
++)
1624 for (j
= 0; j
< noTemplates
; j
++)
1626 if (strings
[i
] == templates
[j
]->m_description
)
1627 data
[i
] = templates
[j
];
1632 wxDocTemplate
*theTemplate
;
1637 // no visible templates, hence nothing to choose from
1642 // don't propose the user to choose if he has no choice
1643 theTemplate
= data
[0];
1647 // propose the user to choose one of several
1648 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1650 _("Select a document template"),
1653 (void **)data
.get(),
1654 wxFindSuitableParent()
1661 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1662 int noTemplates
, bool sort
)
1664 wxArrayString strings
;
1665 wxScopedArray
<wxDocTemplate
*> data(new wxDocTemplate
*[noTemplates
]);
1669 for (i
= 0; i
< noTemplates
; i
++)
1671 wxDocTemplate
*templ
= templates
[i
];
1672 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1676 for (j
= 0; j
< n
; j
++)
1678 //filter out NOT unique views
1679 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1685 strings
.Add(templ
->m_viewTypeName
);
1694 strings
.Sort(); // ascending sort
1695 // Yes, this will be slow, but template lists
1696 // are typically short.
1698 n
= strings
.Count();
1699 for (i
= 0; i
< n
; i
++)
1701 for (j
= 0; j
< noTemplates
; j
++)
1703 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1704 data
[i
] = templates
[j
];
1709 wxDocTemplate
*theTemplate
;
1711 // the same logic as above
1719 theTemplate
= data
[0];
1723 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1725 _("Select a document view"),
1728 (void **)data
.get(),
1729 wxFindSuitableParent()
1737 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1739 if (!m_templates
.Member(temp
))
1740 m_templates
.Append(temp
);
1743 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1745 m_templates
.DeleteObject(temp
);
1748 // Add and remove a document from the manager's list
1749 void wxDocManager::AddDocument(wxDocument
*doc
)
1751 if (!m_docs
.Member(doc
))
1755 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1757 m_docs
.DeleteObject(doc
);
1760 // Views or windows should inform the document manager
1761 // when a view is going in or out of focus
1762 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1766 m_currentView
= view
;
1770 if ( m_currentView
== view
)
1772 // don't keep stale pointer
1773 m_currentView
= NULL
;
1778 // ----------------------------------------------------------------------------
1779 // Default document child frame
1780 // ----------------------------------------------------------------------------
1782 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1783 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1784 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1787 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1791 const wxString
& title
,
1795 const wxString
& name
)
1796 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1798 m_childDocument
= doc
;
1801 view
->SetFrame(this);
1804 bool wxDocChildFrame::TryValidator(wxEvent
& event
)
1809 // FIXME: why is this needed here?
1810 m_childView
->Activate(true);
1812 return m_childView
->ProcessEventHere(event
);
1815 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1817 wxFrame::OnActivate(event
);
1820 m_childView
->Activate(event
.GetActive());
1823 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1828 // passing false to Close() means to not delete associated window
1829 if ( event
.CanVeto() && !m_childView
->Close(false) )
1835 m_childView
->Activate(false);
1838 m_childDocument
= NULL
;
1843 // ----------------------------------------------------------------------------
1844 // Default parent frame
1845 // ----------------------------------------------------------------------------
1847 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1848 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1849 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1850 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1853 wxDocParentFrame::wxDocParentFrame()
1855 m_docManager
= NULL
;
1858 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1861 const wxString
& title
,
1865 const wxString
& name
)
1866 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1868 m_docManager
= manager
;
1871 bool wxDocParentFrame::Create(wxDocManager
*manager
,
1874 const wxString
& title
,
1878 const wxString
& name
)
1880 m_docManager
= manager
;
1881 return base_type::Create(frame
, id
, title
, pos
, size
, style
, name
);
1884 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1889 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1891 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1892 wxString
filename(m_docManager
->GetHistoryFile(n
));
1893 if ( filename
.empty() )
1896 wxString errMsg
; // must contain exactly one "%s" if non-empty
1897 if ( wxFile::Exists(filename
) )
1900 if ( m_docManager
->CreateDocument(filename
, wxDOC_SILENT
) )
1903 errMsg
= _("The file '%s' couldn't be opened.");
1905 else // file doesn't exist
1907 errMsg
= _("The file '%s' doesn't exist and couldn't be opened.");
1911 wxASSERT_MSG( !errMsg
.empty(), "should have an error message" );
1913 // remove the file which we can't open from the MRU list
1914 m_docManager
->RemoveFileFromHistory(n
);
1916 // and tell the user about it
1917 wxLogError(errMsg
+ '\n' +
1918 _("It has been removed from the most recently used files list."),
1922 // Extend event processing to search the view's event table
1923 bool wxDocParentFrame::TryValidator(wxEvent
& event
)
1925 return m_docManager
&& m_docManager
->ProcessEventHere(event
);
1928 // Define the behaviour for the frame closing
1929 // - must delete all frames except for the main one.
1930 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1932 if (m_docManager
->Clear(!event
.CanVeto()))
1940 #if wxUSE_PRINTING_ARCHITECTURE
1942 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1945 m_printoutView
= view
;
1948 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1952 // Get the logical pixels per inch of screen and printer
1953 int ppiScreenX
, ppiScreenY
;
1954 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1955 wxUnusedVar(ppiScreenY
);
1956 int ppiPrinterX
, ppiPrinterY
;
1957 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1958 wxUnusedVar(ppiPrinterY
);
1960 // This scales the DC so that the printout roughly represents the
1961 // the screen scaling. The text point size _should_ be the right size
1962 // but in fact is too small for some reason. This is a detail that will
1963 // need to be addressed at some point but can be fudged for the
1965 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1967 // Now we have to check in case our real page size is reduced
1968 // (e.g. because we're drawing to a print preview memory DC)
1969 int pageWidth
, pageHeight
;
1971 dc
->GetSize(&w
, &h
);
1972 GetPageSizePixels(&pageWidth
, &pageHeight
);
1973 wxUnusedVar(pageHeight
);
1975 // If printer pageWidth == current DC width, then this doesn't
1976 // change. But w might be the preview bitmap width, so scale down.
1977 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1978 dc
->SetUserScale(overallScale
, overallScale
);
1982 m_printoutView
->OnDraw(dc
);
1987 bool wxDocPrintout::HasPage(int pageNum
)
1989 return (pageNum
== 1);
1992 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1994 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2000 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
,
2001 int *selPageFrom
, int *selPageTo
)
2009 #endif // wxUSE_PRINTING_ARCHITECTURE
2011 // ----------------------------------------------------------------------------
2012 // File history (a.k.a. MRU, most recently used, files list)
2013 // ----------------------------------------------------------------------------
2015 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2017 m_fileMaxFiles
= maxFiles
;
2021 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2023 // check if we don't already have this file
2024 const wxFileName
fnNew(file
);
2026 numFiles
= m_fileHistory
.size();
2027 for ( i
= 0; i
< numFiles
; i
++ )
2029 if ( fnNew
== m_fileHistory
[i
] )
2031 // we do have it, move it to the top of the history
2032 RemoveFileFromHistory(i
);
2038 // if we already have a full history, delete the one at the end
2039 if ( numFiles
== m_fileMaxFiles
)
2041 RemoveFileFromHistory(--numFiles
);
2044 // add a new menu item to all file menus (they will be updated below)
2045 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2047 node
= node
->GetNext() )
2049 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2051 if ( !numFiles
&& menu
->GetMenuItemCount() )
2052 menu
->AppendSeparator();
2054 // label doesn't matter, it will be set below anyhow, but it can't
2055 // be empty (this is supposed to indicate a stock item)
2056 menu
->Append(m_idBase
+ numFiles
, " ");
2059 // insert the new file in the beginning of the file history
2060 m_fileHistory
.insert(m_fileHistory
.begin(), file
);
2063 // update the labels in all menus
2064 for ( i
= 0; i
< numFiles
; i
++ )
2066 // if in same directory just show the filename; otherwise the full path
2067 const wxFileName
fnOld(m_fileHistory
[i
]);
2069 wxString pathInMenu
;
2070 if ( fnOld
.GetPath() == fnNew
.GetPath() )
2072 pathInMenu
= fnOld
.GetFullName();
2074 else // file in different directory
2076 // absolute path; could also set relative path
2077 pathInMenu
= m_fileHistory
[i
];
2080 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2082 node
= node
->GetNext() )
2084 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2086 menu
->SetLabel(m_idBase
+ i
, GetMRUEntryLabel(i
, pathInMenu
));
2091 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2093 size_t numFiles
= m_fileHistory
.size();
2094 wxCHECK_RET( i
< numFiles
,
2095 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2097 // delete the element from the array
2098 m_fileHistory
.RemoveAt(i
);
2101 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2103 node
= node
->GetNext() )
2105 wxMenu
* const menu
= (wxMenu
*) node
->GetData();
2107 // shift filenames up
2108 for ( size_t j
= i
; j
< numFiles
; j
++ )
2110 menu
->SetLabel(m_idBase
+ j
, GetMRUEntryLabel(j
, m_fileHistory
[j
]));
2113 // delete the last menu item which is unused now
2114 const wxWindowID lastItemId
= m_idBase
+ numFiles
;
2115 if ( menu
->FindItem(lastItemId
) )
2116 menu
->Delete(lastItemId
);
2118 // delete the last separator too if no more files are left
2119 if ( m_fileHistory
.empty() )
2121 const wxMenuItemList::compatibility_iterator
2122 nodeLast
= menu
->GetMenuItems().GetLast();
2125 wxMenuItem
* const lastMenuItem
= nodeLast
->GetData();
2126 if ( lastMenuItem
->IsSeparator() )
2127 menu
->Delete(lastMenuItem
);
2129 //else: menu is empty somehow
2134 void wxFileHistory::UseMenu(wxMenu
*menu
)
2136 if ( !m_fileMenus
.Member(menu
) )
2137 m_fileMenus
.Append(menu
);
2140 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2142 m_fileMenus
.DeleteObject(menu
);
2146 void wxFileHistory::Load(const wxConfigBase
& config
)
2148 m_fileHistory
.Clear();
2151 buf
.Printf(wxT("file%d"), 1);
2153 wxString historyFile
;
2154 while ((m_fileHistory
.GetCount() < m_fileMaxFiles
) &&
2155 config
.Read(buf
, &historyFile
) && !historyFile
.empty())
2157 m_fileHistory
.Add(historyFile
);
2159 buf
.Printf(wxT("file%d"), (int)m_fileHistory
.GetCount()+1);
2160 historyFile
= wxEmptyString
;
2166 void wxFileHistory::Save(wxConfigBase
& config
)
2169 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2172 buf
.Printf(wxT("file%d"), (int)i
+1);
2173 if (i
< m_fileHistory
.GetCount())
2174 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2176 config
.Write(buf
, wxEmptyString
);
2179 #endif // wxUSE_CONFIG
2181 void wxFileHistory::AddFilesToMenu()
2183 if ( m_fileHistory
.empty() )
2186 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2188 node
= node
->GetNext() )
2190 AddFilesToMenu((wxMenu
*) node
->GetData());
2194 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2196 if ( m_fileHistory
.empty() )
2199 if ( menu
->GetMenuItemCount() )
2200 menu
->AppendSeparator();
2202 for ( size_t i
= 0; i
< m_fileHistory
.GetCount(); i
++ )
2204 menu
->Append(m_idBase
+ i
, GetMRUEntryLabel(i
, m_fileHistory
[i
]));
2208 // ----------------------------------------------------------------------------
2209 // Permits compatibility with existing file formats and functions that
2210 // manipulate files directly
2211 // ----------------------------------------------------------------------------
2213 #if wxUSE_STD_IOSTREAM
2215 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2217 wxFFile
file(filename
, _T("rb"));
2218 if ( !file
.IsOpened() )
2226 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2230 stream
.write(buf
, nRead
);
2234 while ( !file
.Eof() );
2239 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2241 wxFFile
file(filename
, _T("wb"));
2242 if ( !file
.IsOpened() )
2248 stream
.read(buf
, WXSIZEOF(buf
));
2249 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2251 if ( !file
.Write(buf
, stream
.gcount()) )
2255 while ( !stream
.eof() );
2260 #else // !wxUSE_STD_IOSTREAM
2262 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2264 wxFFile
file(filename
, _T("rb"));
2265 if ( !file
.IsOpened() )
2273 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2277 stream
.Write(buf
, nRead
);
2281 while ( !file
.Eof() );
2286 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2288 wxFFile
file(filename
, _T("wb"));
2289 if ( !file
.IsOpened() )
2295 stream
.Read(buf
, WXSIZEOF(buf
));
2297 const size_t nRead
= stream
.LastRead();
2306 if ( !file
.Write(buf
, nRead
) )
2313 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2315 #endif // wxUSE_DOC_VIEW_ARCHITECTURE