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
= GetActiveView();
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
= GetActiveView();
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::GetActiveView() const
1145 wxView
*view
= GetCurrentView();
1147 if ( !view
&& !m_docs
.empty() )
1149 // if we have exactly one document, consider its view to be the current
1152 // VZ: I'm not exactly sure why is this needed but this is how this
1153 // code used to behave before the bug #9518 was fixed and it seems
1154 // safer to preserve the old logic
1155 wxList::compatibility_iterator node
= m_docs
.GetFirst();
1156 if ( !node
->GetNext() )
1158 wxDocument
*doc
= static_cast<wxDocument
*>(node
->GetData());
1159 view
= doc
->GetFirstView();
1161 //else: we have more than one document
1167 bool wxDocManager::TryValidator(wxEvent
& event
)
1169 wxView
* const view
= GetActiveView();
1170 return view
&& view
->ProcessEventHere(event
);
1176 // helper function: return only the visible templates
1177 wxDocTemplates
GetVisibleTemplates(const wxList
& allTemplates
)
1179 // select only the visible templates
1180 const size_t totalNumTemplates
= allTemplates
.GetCount();
1181 wxDocTemplates templates
;
1182 if ( totalNumTemplates
)
1184 templates
.reserve(totalNumTemplates
);
1186 for ( wxList::const_iterator i
= allTemplates
.begin(),
1187 end
= allTemplates
.end();
1191 wxDocTemplate
* const temp
= (wxDocTemplate
*)*i
;
1192 if ( temp
->IsVisible() )
1193 templates
.push_back(temp
);
1200 } // anonymous namespace
1202 wxDocument
*wxDocManager::CreateDocument(const wxString
& pathOrig
, long flags
)
1204 // this ought to be const but SelectDocumentType/Path() are not
1205 // const-correct and can't be changed as, being virtual, this risks
1206 // breaking user code overriding them
1207 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1208 const size_t numTemplates
= templates
.size();
1209 if ( !numTemplates
)
1211 // no templates can be used, can't create document
1216 // normally user should select the template to use but wxDOC_SILENT flag we
1217 // choose one ourselves
1218 wxString path
= pathOrig
; // may be modified below
1219 wxDocTemplate
*temp
;
1220 if ( flags
& wxDOC_SILENT
)
1222 wxASSERT_MSG( !path
.empty(),
1223 "using empty path with wxDOC_SILENT doesn't make sense" );
1225 temp
= FindTemplateForPath(path
);
1228 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1232 else // not silent, ask the user
1234 // for the new file we need just the template, for an existing one we
1235 // need the template and the path, unless it's already specified
1236 if ( (flags
& wxDOC_NEW
) || !path
.empty() )
1237 temp
= SelectDocumentType(&templates
[0], numTemplates
);
1239 temp
= SelectDocumentPath(&templates
[0], numTemplates
, path
, flags
);
1245 // check whether the document with this path is already opened
1246 if ( !path
.empty() )
1248 const wxFileName
fn(path
);
1249 for ( wxList::const_iterator i
= m_docs
.begin(); i
!= m_docs
.end(); ++i
)
1251 wxDocument
* const doc
= (wxDocument
*)*i
;
1253 if ( fn
== doc
->GetFilename() )
1255 // file already open, just activate it and return
1256 if ( doc
->GetFirstView() )
1258 ActivateView(doc
->GetFirstView());
1259 if ( doc
->GetDocumentWindow() )
1260 doc
->GetDocumentWindow()->SetFocus();
1268 // no, we need to create a new document
1271 // if we've reached the max number of docs, close the first one.
1272 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1274 if ( !CloseDocument((wxDocument
*)GetDocuments().GetFirst()->GetData()) )
1276 // can't open the new document if closing the old one failed
1282 // do create and initialize the new document finally
1283 wxDocument
* const docNew
= temp
->CreateDocument(path
, flags
);
1287 docNew
->SetDocumentName(temp
->GetDocumentName());
1288 docNew
->SetDocumentTemplate(temp
);
1290 // call the appropriate function depending on whether we're creating a new
1291 // file or opening an existing one
1292 if ( !(flags
& wxDOC_NEW
? docNew
->OnNewDocument()
1293 : docNew
->OnOpenDocument(path
)) )
1295 // Document is implicitly deleted by DeleteAllViews
1296 docNew
->DeleteAllViews();
1300 // add the successfully opened file to MRU, but only if we're going to be
1301 // able to reopen it successfully later which requires the template for
1302 // this document to be retrievable from the file extension
1303 if ( !(flags
& wxDOC_NEW
) && temp
->FileMatchesTemplate(path
) )
1304 AddFileToHistory(path
);
1309 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1311 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1312 const size_t numTemplates
= templates
.size();
1314 if ( numTemplates
== 0 )
1317 wxDocTemplate
* const
1318 temp
= numTemplates
== 1 ? templates
[0]
1319 : SelectViewType(&templates
[0], numTemplates
);
1324 wxView
*view
= temp
->CreateView(doc
, flags
);
1326 view
->SetViewName(temp
->GetViewName());
1330 // Not yet implemented
1332 wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1336 // Not yet implemented
1337 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1342 wxDocument
*wxDocManager::GetCurrentDocument() const
1344 wxView
* const view
= GetActiveView();
1345 return view
? view
->GetDocument() : NULL
;
1348 wxCommandProcessor
*wxDocManager::GetCurrentCommandProcessor() const
1350 wxDocument
* const doc
= GetCurrentDocument();
1351 return doc
? doc
->GetCommandProcessor() : NULL
;
1354 // Make a default name for a new document
1355 #if WXWIN_COMPATIBILITY_2_8
1356 bool wxDocManager::MakeDefaultName(wxString
& WXUNUSED(name
))
1358 // we consider that this function can only be overridden by the user code,
1359 // not called by it as it only makes sense to call it internally, so we
1360 // don't bother to return anything from here
1363 #endif // WXWIN_COMPATIBILITY_2_8
1365 wxString
wxDocManager::MakeNewDocumentName()
1369 #if WXWIN_COMPATIBILITY_2_8
1370 if ( !MakeDefaultName(name
) )
1371 #endif // WXWIN_COMPATIBILITY_2_8
1373 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1374 m_defaultDocumentNameCounter
++;
1380 // Make a frame title (override this to do something different)
1381 // If docName is empty, a document is not currently active.
1382 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1384 wxString appName
= wxTheApp
->GetAppDisplayName();
1390 wxString docName
= doc
->GetUserReadableName();
1391 title
= docName
+ wxString(_(" - ")) + appName
;
1397 // Not yet implemented
1398 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1403 // File history management
1404 void wxDocManager::AddFileToHistory(const wxString
& file
)
1407 m_fileHistory
->AddFileToHistory(file
);
1410 void wxDocManager::RemoveFileFromHistory(size_t i
)
1413 m_fileHistory
->RemoveFileFromHistory(i
);
1416 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1421 histFile
= m_fileHistory
->GetHistoryFile(i
);
1426 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1429 m_fileHistory
->UseMenu(menu
);
1432 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1435 m_fileHistory
->RemoveMenu(menu
);
1439 void wxDocManager::FileHistoryLoad(const wxConfigBase
& config
)
1442 m_fileHistory
->Load(config
);
1445 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1448 m_fileHistory
->Save(config
);
1452 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1455 m_fileHistory
->AddFilesToMenu(menu
);
1458 void wxDocManager::FileHistoryAddFilesToMenu()
1461 m_fileHistory
->AddFilesToMenu();
1464 size_t wxDocManager::GetHistoryFilesCount() const
1466 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1470 // Find out the document template via matching in the document file format
1471 // against that of the template
1472 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1474 wxDocTemplate
*theTemplate
= NULL
;
1476 // Find the template which this extension corresponds to
1477 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1479 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1480 if ( temp
->FileMatchesTemplate(path
) )
1489 // Prompts user to open a file, using file specs in templates.
1490 // Must extend the file selector dialog or implement own; OR
1491 // match the extension to the template extension.
1493 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1496 long WXUNUSED(flags
),
1497 bool WXUNUSED(save
))
1499 #ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
1502 for (int i
= 0; i
< noTemplates
; i
++)
1504 if (templates
[i
]->IsVisible())
1506 // add a '|' to separate this filter from the previous one
1507 if ( !descrBuf
.empty() )
1508 descrBuf
<< wxT('|');
1510 descrBuf
<< templates
[i
]->GetDescription()
1511 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1512 << templates
[i
]->GetFileFilter();
1516 wxString descrBuf
= wxT("*.*");
1517 wxUnusedVar(noTemplates
);
1520 int FilterIndex
= -1;
1522 wxWindow
* parent
= wxFindSuitableParent();
1524 wxString pathTmp
= wxFileSelectorEx(_("Open File"),
1532 wxDocTemplate
*theTemplate
= NULL
;
1533 if (!pathTmp
.empty())
1535 if (!wxFileExists(pathTmp
))
1538 if (!wxTheApp
->GetAppDisplayName().empty())
1539 msgTitle
= wxTheApp
->GetAppDisplayName();
1541 msgTitle
= wxString(_("File error"));
1543 wxMessageBox(_("Sorry, could not open this file."),
1545 wxOK
| wxICON_EXCLAMATION
| wxCENTRE
,
1548 path
= wxEmptyString
;
1552 SetLastDirectory(wxPathOnly(pathTmp
));
1556 // first choose the template using the extension, if this fails (i.e.
1557 // wxFileSelectorEx() didn't fill it), then use the path
1558 if ( FilterIndex
!= -1 )
1559 theTemplate
= templates
[FilterIndex
];
1561 theTemplate
= FindTemplateForPath(path
);
1564 // Since we do not add files with non-default extensions to the
1565 // file history this can only happen if the application changes the
1566 // allowed templates in runtime.
1567 wxMessageBox(_("Sorry, the format for this file is unknown."),
1569 wxOK
| wxICON_EXCLAMATION
| wxCENTRE
,
1581 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1582 int noTemplates
, bool sort
)
1584 wxArrayString strings
;
1585 wxScopedArray
<wxDocTemplate
*> data(new wxDocTemplate
*[noTemplates
]);
1589 for (i
= 0; i
< noTemplates
; i
++)
1591 if (templates
[i
]->IsVisible())
1595 for (j
= 0; j
< n
; j
++)
1597 //filter out NOT unique documents + view combinations
1598 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1599 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1606 strings
.Add(templates
[i
]->m_description
);
1608 data
[n
] = templates
[i
];
1616 strings
.Sort(); // ascending sort
1617 // Yes, this will be slow, but template lists
1618 // are typically short.
1620 n
= strings
.Count();
1621 for (i
= 0; i
< n
; i
++)
1623 for (j
= 0; j
< noTemplates
; j
++)
1625 if (strings
[i
] == templates
[j
]->m_description
)
1626 data
[i
] = templates
[j
];
1631 wxDocTemplate
*theTemplate
;
1636 // no visible templates, hence nothing to choose from
1641 // don't propose the user to choose if he has no choice
1642 theTemplate
= data
[0];
1646 // propose the user to choose one of several
1647 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1649 _("Select a document template"),
1652 (void **)data
.get(),
1653 wxFindSuitableParent()
1660 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1661 int noTemplates
, bool sort
)
1663 wxArrayString strings
;
1664 wxScopedArray
<wxDocTemplate
*> data(new wxDocTemplate
*[noTemplates
]);
1668 for (i
= 0; i
< noTemplates
; i
++)
1670 wxDocTemplate
*templ
= templates
[i
];
1671 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1675 for (j
= 0; j
< n
; j
++)
1677 //filter out NOT unique views
1678 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1684 strings
.Add(templ
->m_viewTypeName
);
1693 strings
.Sort(); // ascending sort
1694 // Yes, this will be slow, but template lists
1695 // are typically short.
1697 n
= strings
.Count();
1698 for (i
= 0; i
< n
; i
++)
1700 for (j
= 0; j
< noTemplates
; j
++)
1702 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1703 data
[i
] = templates
[j
];
1708 wxDocTemplate
*theTemplate
;
1710 // the same logic as above
1718 theTemplate
= data
[0];
1722 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1724 _("Select a document view"),
1727 (void **)data
.get(),
1728 wxFindSuitableParent()
1736 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1738 if (!m_templates
.Member(temp
))
1739 m_templates
.Append(temp
);
1742 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1744 m_templates
.DeleteObject(temp
);
1747 // Add and remove a document from the manager's list
1748 void wxDocManager::AddDocument(wxDocument
*doc
)
1750 if (!m_docs
.Member(doc
))
1754 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1756 m_docs
.DeleteObject(doc
);
1759 // Views or windows should inform the document manager
1760 // when a view is going in or out of focus
1761 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1765 m_currentView
= view
;
1769 if ( m_currentView
== view
)
1771 // don't keep stale pointer
1772 m_currentView
= NULL
;
1777 // ----------------------------------------------------------------------------
1778 // Default document child frame
1779 // ----------------------------------------------------------------------------
1781 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1782 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1783 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1786 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1790 const wxString
& title
,
1794 const wxString
& name
)
1795 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1797 m_childDocument
= doc
;
1800 view
->SetFrame(this);
1803 bool wxDocChildFrame::TryValidator(wxEvent
& event
)
1808 // FIXME: why is this needed here?
1809 m_childView
->Activate(true);
1811 return m_childView
->ProcessEventHere(event
);
1814 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1816 wxFrame::OnActivate(event
);
1819 m_childView
->Activate(event
.GetActive());
1822 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1827 // passing false to Close() means to not delete associated window
1828 if ( event
.CanVeto() && !m_childView
->Close(false) )
1834 m_childView
->Activate(false);
1837 m_childDocument
= NULL
;
1842 // ----------------------------------------------------------------------------
1843 // Default parent frame
1844 // ----------------------------------------------------------------------------
1846 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1847 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1848 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1849 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1852 wxDocParentFrame::wxDocParentFrame()
1854 m_docManager
= NULL
;
1857 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1860 const wxString
& title
,
1864 const wxString
& name
)
1865 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1867 m_docManager
= manager
;
1870 bool wxDocParentFrame::Create(wxDocManager
*manager
,
1873 const wxString
& title
,
1877 const wxString
& name
)
1879 m_docManager
= manager
;
1880 return base_type::Create(frame
, id
, title
, pos
, size
, style
, name
);
1883 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1888 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1890 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1891 wxString
filename(m_docManager
->GetHistoryFile(n
));
1892 if ( filename
.empty() )
1895 wxString errMsg
; // must contain exactly one "%s" if non-empty
1896 if ( wxFile::Exists(filename
) )
1899 if ( m_docManager
->CreateDocument(filename
, wxDOC_SILENT
) )
1902 errMsg
= _("The file '%s' couldn't be opened.");
1904 else // file doesn't exist
1906 errMsg
= _("The file '%s' doesn't exist and couldn't be opened.");
1910 wxASSERT_MSG( !errMsg
.empty(), "should have an error message" );
1912 // remove the file which we can't open from the MRU list
1913 m_docManager
->RemoveFileFromHistory(n
);
1915 // and tell the user about it
1916 wxLogError(errMsg
+ '\n' +
1917 _("It has been removed from the most recently used files list."),
1921 // Extend event processing to search the view's event table
1922 bool wxDocParentFrame::TryValidator(wxEvent
& event
)
1924 return m_docManager
&& m_docManager
->ProcessEventHere(event
);
1927 // Define the behaviour for the frame closing
1928 // - must delete all frames except for the main one.
1929 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1931 if (m_docManager
->Clear(!event
.CanVeto()))
1939 #if wxUSE_PRINTING_ARCHITECTURE
1941 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1944 m_printoutView
= view
;
1947 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1951 // Get the logical pixels per inch of screen and printer
1952 int ppiScreenX
, ppiScreenY
;
1953 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1954 wxUnusedVar(ppiScreenY
);
1955 int ppiPrinterX
, ppiPrinterY
;
1956 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1957 wxUnusedVar(ppiPrinterY
);
1959 // This scales the DC so that the printout roughly represents the
1960 // the screen scaling. The text point size _should_ be the right size
1961 // but in fact is too small for some reason. This is a detail that will
1962 // need to be addressed at some point but can be fudged for the
1964 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1966 // Now we have to check in case our real page size is reduced
1967 // (e.g. because we're drawing to a print preview memory DC)
1968 int pageWidth
, pageHeight
;
1970 dc
->GetSize(&w
, &h
);
1971 GetPageSizePixels(&pageWidth
, &pageHeight
);
1972 wxUnusedVar(pageHeight
);
1974 // If printer pageWidth == current DC width, then this doesn't
1975 // change. But w might be the preview bitmap width, so scale down.
1976 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1977 dc
->SetUserScale(overallScale
, overallScale
);
1981 m_printoutView
->OnDraw(dc
);
1986 bool wxDocPrintout::HasPage(int pageNum
)
1988 return (pageNum
== 1);
1991 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1993 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1999 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
,
2000 int *selPageFrom
, int *selPageTo
)
2008 #endif // wxUSE_PRINTING_ARCHITECTURE
2010 // ----------------------------------------------------------------------------
2011 // File history (a.k.a. MRU, most recently used, files list)
2012 // ----------------------------------------------------------------------------
2014 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2016 m_fileMaxFiles
= maxFiles
;
2020 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2022 // check if we don't already have this file
2023 const wxFileName
fnNew(file
);
2025 numFiles
= m_fileHistory
.size();
2026 for ( i
= 0; i
< numFiles
; i
++ )
2028 if ( fnNew
== m_fileHistory
[i
] )
2030 // we do have it, move it to the top of the history
2031 RemoveFileFromHistory(i
);
2037 // if we already have a full history, delete the one at the end
2038 if ( numFiles
== m_fileMaxFiles
)
2040 RemoveFileFromHistory(--numFiles
);
2043 // add a new menu item to all file menus (they will be updated below)
2044 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2046 node
= node
->GetNext() )
2048 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2050 if ( !numFiles
&& menu
->GetMenuItemCount() )
2051 menu
->AppendSeparator();
2053 // label doesn't matter, it will be set below anyhow, but it can't
2054 // be empty (this is supposed to indicate a stock item)
2055 menu
->Append(m_idBase
+ numFiles
, " ");
2058 // insert the new file in the beginning of the file history
2059 m_fileHistory
.insert(m_fileHistory
.begin(), file
);
2062 // update the labels in all menus
2063 for ( i
= 0; i
< numFiles
; i
++ )
2065 // if in same directory just show the filename; otherwise the full path
2066 const wxFileName
fnOld(m_fileHistory
[i
]);
2068 wxString pathInMenu
;
2069 if ( fnOld
.GetPath() == fnNew
.GetPath() )
2071 pathInMenu
= fnOld
.GetFullName();
2073 else // file in different directory
2075 // absolute path; could also set relative path
2076 pathInMenu
= m_fileHistory
[i
];
2079 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2081 node
= node
->GetNext() )
2083 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2085 menu
->SetLabel(m_idBase
+ i
, GetMRUEntryLabel(i
, pathInMenu
));
2090 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2092 size_t numFiles
= m_fileHistory
.size();
2093 wxCHECK_RET( i
< numFiles
,
2094 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2096 // delete the element from the array
2097 m_fileHistory
.RemoveAt(i
);
2100 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2102 node
= node
->GetNext() )
2104 wxMenu
* const menu
= (wxMenu
*) node
->GetData();
2106 // shift filenames up
2107 for ( size_t j
= i
; j
< numFiles
; j
++ )
2109 menu
->SetLabel(m_idBase
+ j
, GetMRUEntryLabel(j
, m_fileHistory
[j
]));
2112 // delete the last menu item which is unused now
2113 const wxWindowID lastItemId
= m_idBase
+ numFiles
;
2114 if ( menu
->FindItem(lastItemId
) )
2115 menu
->Delete(lastItemId
);
2117 // delete the last separator too if no more files are left
2118 if ( m_fileHistory
.empty() )
2120 const wxMenuItemList::compatibility_iterator
2121 nodeLast
= menu
->GetMenuItems().GetLast();
2124 wxMenuItem
* const lastMenuItem
= nodeLast
->GetData();
2125 if ( lastMenuItem
->IsSeparator() )
2126 menu
->Delete(lastMenuItem
);
2128 //else: menu is empty somehow
2133 void wxFileHistory::UseMenu(wxMenu
*menu
)
2135 if ( !m_fileMenus
.Member(menu
) )
2136 m_fileMenus
.Append(menu
);
2139 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2141 m_fileMenus
.DeleteObject(menu
);
2145 void wxFileHistory::Load(const wxConfigBase
& config
)
2147 m_fileHistory
.Clear();
2150 buf
.Printf(wxT("file%d"), 1);
2152 wxString historyFile
;
2153 while ((m_fileHistory
.GetCount() < m_fileMaxFiles
) &&
2154 config
.Read(buf
, &historyFile
) && !historyFile
.empty())
2156 m_fileHistory
.Add(historyFile
);
2158 buf
.Printf(wxT("file%d"), (int)m_fileHistory
.GetCount()+1);
2159 historyFile
= wxEmptyString
;
2165 void wxFileHistory::Save(wxConfigBase
& config
)
2168 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2171 buf
.Printf(wxT("file%d"), (int)i
+1);
2172 if (i
< m_fileHistory
.GetCount())
2173 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2175 config
.Write(buf
, wxEmptyString
);
2178 #endif // wxUSE_CONFIG
2180 void wxFileHistory::AddFilesToMenu()
2182 if ( m_fileHistory
.empty() )
2185 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2187 node
= node
->GetNext() )
2189 AddFilesToMenu((wxMenu
*) node
->GetData());
2193 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2195 if ( m_fileHistory
.empty() )
2198 if ( menu
->GetMenuItemCount() )
2199 menu
->AppendSeparator();
2201 for ( size_t i
= 0; i
< m_fileHistory
.GetCount(); i
++ )
2203 menu
->Append(m_idBase
+ i
, GetMRUEntryLabel(i
, m_fileHistory
[i
]));
2207 // ----------------------------------------------------------------------------
2208 // Permits compatibility with existing file formats and functions that
2209 // manipulate files directly
2210 // ----------------------------------------------------------------------------
2212 #if wxUSE_STD_IOSTREAM
2214 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2216 wxFFile
file(filename
, _T("rb"));
2217 if ( !file
.IsOpened() )
2225 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2229 stream
.write(buf
, nRead
);
2233 while ( !file
.Eof() );
2238 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2240 wxFFile
file(filename
, _T("wb"));
2241 if ( !file
.IsOpened() )
2247 stream
.read(buf
, WXSIZEOF(buf
));
2248 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2250 if ( !file
.Write(buf
, stream
.gcount()) )
2254 while ( !stream
.eof() );
2259 #else // !wxUSE_STD_IOSTREAM
2261 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2263 wxFFile
file(filename
, _T("rb"));
2264 if ( !file
.IsOpened() )
2272 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2276 stream
.Write(buf
, nRead
);
2280 while ( !file
.Eof() );
2285 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2287 wxFFile
file(filename
, _T("wb"));
2288 if ( !file
.IsOpened() )
2294 stream
.Read(buf
, WXSIZEOF(buf
));
2296 const size_t nRead
= stream
.LastRead();
2305 if ( !file
.Write(buf
, nRead
) )
2312 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2314 #endif // wxUSE_DOC_VIEW_ARCHITECTURE