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 if (m_commandProcessor
)
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())
174 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
.GetCount() == 0)
238 return (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 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
280 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
282 // Now see if there are some other template with identical view and document
283 // classes, whose filters may also be used.
285 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
287 wxList::compatibility_iterator node
= docTemplate
->GetDocumentManager()->GetTemplates().GetFirst();
290 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
292 if (t
->IsVisible() && t
!= docTemplate
&&
293 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
294 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
296 // add a '|' to separate this filter from the previous one
297 if ( !filter
.empty() )
300 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
301 << t
->GetFileFilter();
304 node
= node
->GetNext();
308 wxString filter
= docTemplate
->GetFileFilter() ;
310 wxString defaultDir
= docTemplate
->GetDirectory();
311 if ( defaultDir
.empty() )
313 defaultDir
= wxPathOnly(GetFilename());
314 if ( defaultDir
.empty() )
315 defaultDir
= GetDocumentManager()->GetLastDirectory();
318 wxString fileName
= wxFileSelector(_("Save As"),
320 wxFileNameFromPath(GetFilename()),
321 docTemplate
->GetDefaultExtension(),
323 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
324 GetDocumentWindow());
326 if (fileName
.empty())
330 wxFileName::SplitPath(fileName
, NULL
, NULL
, &ext
);
334 fileName
+= wxT(".");
335 fileName
+= docTemplate
->GetDefaultExtension();
338 // Files that were not saved correctly are not added to the FileHistory.
339 if (!OnSaveDocument(fileName
))
342 SetTitle(wxFileNameFromPath(fileName
));
343 SetFilename(fileName
, true); // will call OnChangeFileName automatically
345 // A file that doesn't use the default extension of its document template cannot be opened
346 // via the FileHistory, so we do not add it.
347 if (docTemplate
->FileMatchesTemplate(fileName
))
349 GetDocumentManager()->AddFileToHistory(fileName
);
353 // The user will probably not be able to open the file again, so
354 // we could warn about the wrong file-extension here.
359 bool wxDocument::OnSaveDocument(const wxString
& file
)
364 if ( !DoSaveDocument(file
) )
369 SetDocumentSaved(true);
370 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
371 wxFileName
fn(file
) ;
372 fn
.MacSetDefaultTypeAndCreator() ;
377 bool wxDocument::OnOpenDocument(const wxString
& file
)
379 if ( !OnSaveModified() )
382 if ( !DoOpenDocument(file
) )
385 SetFilename(file
, true);
394 #if wxUSE_STD_IOSTREAM
395 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
397 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
403 #if wxUSE_STD_IOSTREAM
404 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
406 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
412 bool wxDocument::Revert()
418 // Get title, or filename if no title, else unnamed
419 #if WXWIN_COMPATIBILITY_2_8
420 bool wxDocument::GetPrintableName(wxString
& buf
) const
422 // this function can not only be overridden by the user code but also
423 // called by it so we need to ensure that we return the same thing as
424 // GetUserReadableName() but we can't call it because this would result in
425 // an infinite recursion, hence we use the helper DoGetUserReadableName()
426 buf
= DoGetUserReadableName();
430 #endif // WXWIN_COMPATIBILITY_2_8
432 wxString
wxDocument::GetUserReadableName() const
434 #if WXWIN_COMPATIBILITY_2_8
435 // we need to call the old virtual function to ensure that the overridden
436 // version of it is still called
438 if ( GetPrintableName(name
) )
440 #endif // WXWIN_COMPATIBILITY_2_8
442 return DoGetUserReadableName();
445 wxString
wxDocument::DoGetUserReadableName() const
447 if ( !m_documentTitle
.empty() )
448 return m_documentTitle
;
450 if ( !m_documentFile
.empty() )
451 return wxFileNameFromPath(m_documentFile
);
456 wxWindow
*wxDocument::GetDocumentWindow() const
458 wxView
*view
= GetFirstView();
460 return view
->GetFrame();
462 return 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
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
662 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
667 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
671 void wxView::OnChangeFilename()
673 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
674 // generic MDI implementation so use SetLabel rather than SetTitle.
675 // It should cause SetTitle() for top level windows.
676 wxWindow
*win
= GetFrame();
679 wxDocument
*doc
= GetDocument();
682 win
->SetLabel(doc
->GetUserReadableName());
685 void wxView::SetDocument(wxDocument
*doc
)
687 m_viewDocument
= doc
;
692 bool wxView::Close(bool deleteWindow
)
694 return OnClose(deleteWindow
);
697 void wxView::Activate(bool activate
)
699 if (GetDocument() && GetDocumentManager())
701 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
702 GetDocumentManager()->ActivateView(this, activate
);
706 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
708 return GetDocument() ? GetDocument()->Close() : true;
711 #if wxUSE_PRINTING_ARCHITECTURE
712 wxPrintout
*wxView::OnCreatePrintout()
714 return new wxDocPrintout(this);
716 #endif // wxUSE_PRINTING_ARCHITECTURE
718 // ----------------------------------------------------------------------------
720 // ----------------------------------------------------------------------------
722 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
723 const wxString
& descr
,
724 const wxString
& filter
,
727 const wxString
& docTypeName
,
728 const wxString
& viewTypeName
,
729 wxClassInfo
*docClassInfo
,
730 wxClassInfo
*viewClassInfo
,
733 m_documentManager
= manager
;
734 m_description
= descr
;
737 m_fileFilter
= filter
;
739 m_docTypeName
= docTypeName
;
740 m_viewTypeName
= viewTypeName
;
741 m_documentManager
->AssociateTemplate(this);
743 m_docClassInfo
= docClassInfo
;
744 m_viewClassInfo
= viewClassInfo
;
747 wxDocTemplate::~wxDocTemplate()
749 m_documentManager
->DisassociateTemplate(this);
752 // Tries to dynamically construct an object of the right class.
753 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
755 wxDocument
* const doc
= DoCreateDocument();
757 // VZ: this code doesn't delete doc if InitDocument() (i.e. doc->OnCreate())
758 // fails, is this intentional?
760 return doc
&& InitDocument(doc
, path
, flags
) ? doc
: NULL
;
764 wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
766 doc
->SetFilename(path
);
767 doc
->SetDocumentTemplate(this);
768 GetDocumentManager()->AddDocument(doc
);
769 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
771 if (doc
->OnCreate(path
, flags
))
775 if (GetDocumentManager()->GetDocuments().Member(doc
))
776 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 (wxDocument
*)m_docClassInfo
->CreateObject();
820 wxView
*wxDocTemplate::DoCreateView()
822 if (!m_viewClassInfo
)
825 return (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
)
890 // Implicitly deletes the document when
891 // the last view is deleted
892 doc
->DeleteAllViews();
894 // Check we're really deleted
895 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( wxEmptyString
, 0) )
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
= GetCurrentView();
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
= GetCurrentView();
1044 wxPrintout
*printout
= view
->OnCreatePrintout();
1047 // Pass two printout objects: for preview, and possible printing.
1048 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1049 if ( !preview
->Ok() )
1052 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1056 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1057 wxPoint(100, 100), wxSize(600, 650));
1058 frame
->Centre(wxBOTH
);
1059 frame
->Initialize();
1062 #endif // wxUSE_PRINTING_ARCHITECTURE
1065 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1067 wxDocument
*doc
= GetCurrentDocument();
1070 if (doc
->GetCommandProcessor())
1071 doc
->GetCommandProcessor()->Undo();
1076 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1078 wxDocument
*doc
= GetCurrentDocument();
1081 if (doc
->GetCommandProcessor())
1082 doc
->GetCommandProcessor()->Redo();
1087 // Handlers for UI update commands
1089 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1091 // CreateDocument() (which is called from OnFileOpen) may succeed
1092 // only when there is at least a template:
1093 event
.Enable( GetTemplates().GetCount()>0 );
1096 void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent
& event
)
1098 event
.Enable( GetCurrentDocument() != NULL
);
1101 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1103 // CreateDocument() (which is called from OnFileNew) may succeed
1104 // only when there is at least a template:
1105 event
.Enable( GetTemplates().GetCount()>0 );
1108 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1110 wxDocument
* const doc
= GetCurrentDocument();
1111 event
.Enable( doc
&& !doc
->AlreadySaved() );
1114 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1116 wxDocument
*doc
= GetCurrentDocument();
1118 event
.Enable(false);
1119 else if (!doc
->GetCommandProcessor())
1123 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1124 doc
->GetCommandProcessor()->SetMenuStrings();
1128 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1130 wxDocument
*doc
= GetCurrentDocument();
1132 event
.Enable(false);
1133 else if (!doc
->GetCommandProcessor())
1137 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1138 doc
->GetCommandProcessor()->SetMenuStrings();
1142 wxView
*wxDocManager::GetCurrentView() const
1145 return m_currentView
;
1146 if (m_docs
.GetCount() == 1)
1148 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1149 return doc
->GetFirstView();
1154 bool wxDocManager::TryValidator(wxEvent
& event
)
1156 wxView
* const view
= GetCurrentView();
1157 return view
&& view
->ProcessEventHere(event
);
1163 // helper function: return only the visible templates
1164 wxDocTemplates
GetVisibleTemplates(const wxList
& allTemplates
)
1166 // select only the visible templates
1167 const size_t totalNumTemplates
= allTemplates
.GetCount();
1168 wxDocTemplates templates
;
1169 if ( totalNumTemplates
)
1171 templates
.reserve(totalNumTemplates
);
1173 for ( wxList::const_iterator i
= allTemplates
.begin(),
1174 end
= allTemplates
.end();
1178 wxDocTemplate
* const temp
= (wxDocTemplate
*)*i
;
1179 if ( temp
->IsVisible() )
1180 templates
.push_back(temp
);
1187 } // anonymous namespace
1189 wxDocument
*wxDocManager::CreateDocument(const wxString
& pathOrig
, long flags
)
1191 // this ought to be const but SelectDocumentType/Path() are not
1192 // const-correct and can't be changed as, being virtual, this risks
1193 // breaking user code overriding them
1194 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1195 const size_t numTemplates
= templates
.size();
1196 if ( !numTemplates
)
1198 // no templates can be used, can't create document
1203 // normally user should select the template to use but wxDOC_SILENT flag we
1204 // choose one ourselves
1205 wxString path
= pathOrig
; // may be modified below
1206 wxDocTemplate
*temp
;
1207 if ( flags
& wxDOC_SILENT
)
1209 wxASSERT_MSG( !path
.empty(),
1210 "using empty path with wxDOC_SILENT doesn't make sense" );
1212 temp
= FindTemplateForPath(path
);
1215 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1219 else // not silent, ask the user
1221 // for the new file we need just the template, for an existing one we
1222 // need the template and the path, unless it's already specified
1223 if ( (flags
& wxDOC_NEW
) || !path
.empty() )
1224 temp
= SelectDocumentType(&templates
[0], numTemplates
);
1226 temp
= SelectDocumentPath(&templates
[0], numTemplates
, path
, flags
);
1232 // check whether the document with this path is already opened
1233 if ( !path
.empty() )
1235 const wxFileName
fn(path
);
1236 for ( wxList::const_iterator i
= m_docs
.begin(); i
!= m_docs
.end(); ++i
)
1238 wxDocument
* const doc
= (wxDocument
*)*i
;
1240 if ( fn
== doc
->GetFilename() )
1242 // file already open, just activate it and return
1243 if ( doc
->GetFirstView() )
1245 ActivateView(doc
->GetFirstView());
1246 if ( doc
->GetDocumentWindow() )
1247 doc
->GetDocumentWindow()->SetFocus();
1255 // no, we need to create a new document
1258 // if we've reached the max number of docs, close the first one.
1259 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1261 if ( !CloseDocument((wxDocument
*)GetDocuments().GetFirst()->GetData()) )
1263 // can't open the new document if closing the old one failed
1269 // do create and initialize the new document finally
1270 wxDocument
* const docNew
= temp
->CreateDocument(path
, flags
);
1274 docNew
->SetDocumentName(temp
->GetDocumentName());
1275 docNew
->SetDocumentTemplate(temp
);
1277 // call the appropriate function depending on whether we're creating a new
1278 // file or opening an existing one
1279 if ( !(flags
& wxDOC_NEW
? docNew
->OnNewDocument()
1280 : docNew
->OnOpenDocument(path
)) )
1282 // Document is implicitly deleted by DeleteAllViews
1283 docNew
->DeleteAllViews();
1287 // add the successfully opened file to MRU, but only if we're going to be
1288 // able to reopen it successfully later which requires the template for
1289 // this document to be retrievable from the file extension
1290 if ( !(flags
& wxDOC_NEW
) && temp
->FileMatchesTemplate(path
) )
1291 AddFileToHistory(path
);
1296 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1298 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1299 const size_t numTemplates
= templates
.size();
1301 if ( numTemplates
== 0 )
1304 wxDocTemplate
* const
1305 temp
= numTemplates
== 1 ? templates
[0]
1306 : SelectViewType(&templates
[0], numTemplates
);
1311 wxView
*view
= temp
->CreateView(doc
, flags
);
1313 view
->SetViewName(temp
->GetViewName());
1317 // Not yet implemented
1319 wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1323 // Not yet implemented
1324 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1329 wxDocument
*wxDocManager::GetCurrentDocument() const
1331 wxView
*view
= GetCurrentView();
1333 return view
->GetDocument();
1338 // Make a default name for a new document
1339 #if WXWIN_COMPATIBILITY_2_8
1340 bool wxDocManager::MakeDefaultName(wxString
& WXUNUSED(name
))
1342 // we consider that this function can only be overridden by the user code,
1343 // not called by it as it only makes sense to call it internally, so we
1344 // don't bother to return anything from here
1347 #endif // WXWIN_COMPATIBILITY_2_8
1349 wxString
wxDocManager::MakeNewDocumentName()
1353 #if WXWIN_COMPATIBILITY_2_8
1354 if ( !MakeDefaultName(name
) )
1355 #endif // WXWIN_COMPATIBILITY_2_8
1357 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1358 m_defaultDocumentNameCounter
++;
1364 // Make a frame title (override this to do something different)
1365 // If docName is empty, a document is not currently active.
1366 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1368 wxString appName
= wxTheApp
->GetAppDisplayName();
1374 wxString docName
= doc
->GetUserReadableName();
1375 title
= docName
+ wxString(_(" - ")) + appName
;
1381 // Not yet implemented
1382 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1387 // File history management
1388 void wxDocManager::AddFileToHistory(const wxString
& file
)
1391 m_fileHistory
->AddFileToHistory(file
);
1394 void wxDocManager::RemoveFileFromHistory(size_t i
)
1397 m_fileHistory
->RemoveFileFromHistory(i
);
1400 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1405 histFile
= m_fileHistory
->GetHistoryFile(i
);
1410 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1413 m_fileHistory
->UseMenu(menu
);
1416 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1419 m_fileHistory
->RemoveMenu(menu
);
1423 void wxDocManager::FileHistoryLoad(const wxConfigBase
& config
)
1426 m_fileHistory
->Load(config
);
1429 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1432 m_fileHistory
->Save(config
);
1436 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1439 m_fileHistory
->AddFilesToMenu(menu
);
1442 void wxDocManager::FileHistoryAddFilesToMenu()
1445 m_fileHistory
->AddFilesToMenu();
1448 size_t wxDocManager::GetHistoryFilesCount() const
1450 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1454 // Find out the document template via matching in the document file format
1455 // against that of the template
1456 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1458 wxDocTemplate
*theTemplate
= NULL
;
1460 // Find the template which this extension corresponds to
1461 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1463 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1464 if ( temp
->FileMatchesTemplate(path
) )
1473 // Prompts user to open a file, using file specs in templates.
1474 // Must extend the file selector dialog or implement own; OR
1475 // match the extension to the template extension.
1477 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1480 long WXUNUSED(flags
),
1481 bool WXUNUSED(save
))
1483 // We can only have multiple filters in Windows and GTK
1484 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1488 for (i
= 0; i
< noTemplates
; i
++)
1490 if (templates
[i
]->IsVisible())
1492 // add a '|' to separate this filter from the previous one
1493 if ( !descrBuf
.empty() )
1494 descrBuf
<< wxT('|');
1496 descrBuf
<< templates
[i
]->GetDescription()
1497 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1498 << templates
[i
]->GetFileFilter();
1502 wxString descrBuf
= wxT("*.*");
1503 wxUnusedVar(noTemplates
);
1506 int FilterIndex
= -1;
1508 wxWindow
* parent
= wxFindSuitableParent();
1510 wxString pathTmp
= wxFileSelectorEx(_("Open File"),
1518 wxDocTemplate
*theTemplate
= NULL
;
1519 if (!pathTmp
.empty())
1521 if (!wxFileExists(pathTmp
))
1524 if (!wxTheApp
->GetAppDisplayName().empty())
1525 msgTitle
= wxTheApp
->GetAppDisplayName();
1527 msgTitle
= wxString(_("File error"));
1529 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
| wxCENTRE
,
1532 path
= wxEmptyString
;
1536 SetLastDirectory(wxPathOnly(pathTmp
));
1540 // first choose the template using the extension, if this fails (i.e.
1541 // wxFileSelectorEx() didn't fill it), then use the path
1542 if ( FilterIndex
!= -1 )
1543 theTemplate
= templates
[FilterIndex
];
1545 theTemplate
= FindTemplateForPath(path
);
1548 // Since we do not add files with non-default extensions to the FileHistory this
1549 // can only happen if the application changes the allowed templates in runtime.
1550 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1552 wxOK
| wxICON_EXCLAMATION
| wxCENTRE
, wxFindSuitableParent());
1557 path
= wxEmptyString
;
1563 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1564 int noTemplates
, bool sort
)
1566 wxArrayString strings
;
1567 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1571 for (i
= 0; i
< noTemplates
; i
++)
1573 if (templates
[i
]->IsVisible())
1577 for (j
= 0; j
< n
; j
++)
1579 //filter out NOT unique documents + view combinations
1580 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1581 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1588 strings
.Add(templates
[i
]->m_description
);
1590 data
[n
] = templates
[i
];
1598 strings
.Sort(); // ascending sort
1599 // Yes, this will be slow, but template lists
1600 // are typically short.
1602 n
= strings
.Count();
1603 for (i
= 0; i
< n
; i
++)
1605 for (j
= 0; j
< noTemplates
; j
++)
1607 if (strings
[i
] == templates
[j
]->m_description
)
1608 data
[i
] = templates
[j
];
1613 wxDocTemplate
*theTemplate
;
1618 // no visible templates, hence nothing to choose from
1623 // don't propose the user to choose if he has no choice
1624 theTemplate
= data
[0];
1628 // propose the user to choose one of several
1629 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1631 _("Select a document template"),
1635 wxFindSuitableParent()
1644 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1645 int noTemplates
, bool sort
)
1647 wxArrayString strings
;
1648 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1652 for (i
= 0; i
< noTemplates
; i
++)
1654 wxDocTemplate
*templ
= templates
[i
];
1655 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1659 for (j
= 0; j
< n
; j
++)
1661 //filter out NOT unique views
1662 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1668 strings
.Add(templ
->m_viewTypeName
);
1677 strings
.Sort(); // ascending sort
1678 // Yes, this will be slow, but template lists
1679 // are typically short.
1681 n
= strings
.Count();
1682 for (i
= 0; i
< n
; i
++)
1684 for (j
= 0; j
< noTemplates
; j
++)
1686 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1687 data
[i
] = templates
[j
];
1692 wxDocTemplate
*theTemplate
;
1694 // the same logic as above
1702 theTemplate
= data
[0];
1706 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1708 _("Select a document view"),
1712 wxFindSuitableParent()
1721 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1723 if (!m_templates
.Member(temp
))
1724 m_templates
.Append(temp
);
1727 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1729 m_templates
.DeleteObject(temp
);
1732 // Add and remove a document from the manager's list
1733 void wxDocManager::AddDocument(wxDocument
*doc
)
1735 if (!m_docs
.Member(doc
))
1739 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1741 m_docs
.DeleteObject(doc
);
1744 // Views or windows should inform the document manager
1745 // when a view is going in or out of focus
1746 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1750 m_currentView
= view
;
1754 if ( m_currentView
== view
)
1756 // don't keep stale pointer
1757 m_currentView
= NULL
;
1762 // ----------------------------------------------------------------------------
1763 // Default document child frame
1764 // ----------------------------------------------------------------------------
1766 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1767 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1768 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1771 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1775 const wxString
& title
,
1779 const wxString
& name
)
1780 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1782 m_childDocument
= doc
;
1785 view
->SetFrame(this);
1788 bool wxDocChildFrame::TryValidator(wxEvent
& event
)
1793 // FIXME: why is this needed here?
1794 m_childView
->Activate(true);
1796 return m_childView
->ProcessEventHere(event
);
1799 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1801 wxFrame::OnActivate(event
);
1804 m_childView
->Activate(event
.GetActive());
1807 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1811 bool ans
= event
.CanVeto()
1812 ? m_childView
->Close(false) // false means don't delete associated window
1813 : true; // Must delete.
1817 m_childView
->Activate(false);
1820 m_childDocument
= NULL
;
1831 // ----------------------------------------------------------------------------
1832 // Default parent frame
1833 // ----------------------------------------------------------------------------
1835 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1836 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1837 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1838 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1841 wxDocParentFrame::wxDocParentFrame()
1843 m_docManager
= NULL
;
1846 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1849 const wxString
& title
,
1853 const wxString
& name
)
1854 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1856 m_docManager
= manager
;
1859 bool wxDocParentFrame::Create(wxDocManager
*manager
,
1862 const wxString
& title
,
1866 const wxString
& name
)
1868 m_docManager
= manager
;
1869 return base_type::Create(frame
, id
, title
, pos
, size
, style
, name
);
1872 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1877 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1879 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1880 wxString
filename(m_docManager
->GetHistoryFile(n
));
1881 if ( filename
.empty() )
1884 wxString errMsg
; // must contain exactly one "%s" if non-empty
1885 if ( wxFile::Exists(filename
) )
1888 if ( m_docManager
->CreateDocument(filename
, wxDOC_SILENT
) )
1891 errMsg
= _("The file '%s' couldn't be opened.");
1893 else // file doesn't exist
1895 errMsg
= _("The file '%s' doesn't exist and couldn't be opened.");
1899 wxASSERT_MSG( !errMsg
.empty(), "should have an error message" );
1901 // remove the file which we can't open from the MRU list
1902 m_docManager
->RemoveFileFromHistory(n
);
1904 // and tell the user about it
1905 wxLogError(errMsg
+ '\n' +
1906 _("It has been removed from the most recently used files list."),
1910 // Extend event processing to search the view's event table
1911 bool wxDocParentFrame::TryValidator(wxEvent
& event
)
1913 return m_docManager
&& m_docManager
->ProcessEventHere(event
);
1916 // Define the behaviour for the frame closing
1917 // - must delete all frames except for the main one.
1918 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1920 if (m_docManager
->Clear(!event
.CanVeto()))
1928 #if wxUSE_PRINTING_ARCHITECTURE
1930 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1933 m_printoutView
= view
;
1936 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1940 // Get the logical pixels per inch of screen and printer
1941 int ppiScreenX
, ppiScreenY
;
1942 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1943 wxUnusedVar(ppiScreenY
);
1944 int ppiPrinterX
, ppiPrinterY
;
1945 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1946 wxUnusedVar(ppiPrinterY
);
1948 // This scales the DC so that the printout roughly represents the
1949 // the screen scaling. The text point size _should_ be the right size
1950 // but in fact is too small for some reason. This is a detail that will
1951 // need to be addressed at some point but can be fudged for the
1953 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1955 // Now we have to check in case our real page size is reduced
1956 // (e.g. because we're drawing to a print preview memory DC)
1957 int pageWidth
, pageHeight
;
1959 dc
->GetSize(&w
, &h
);
1960 GetPageSizePixels(&pageWidth
, &pageHeight
);
1961 wxUnusedVar(pageHeight
);
1963 // If printer pageWidth == current DC width, then this doesn't
1964 // change. But w might be the preview bitmap width, so scale down.
1965 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1966 dc
->SetUserScale(overallScale
, overallScale
);
1970 m_printoutView
->OnDraw(dc
);
1975 bool wxDocPrintout::HasPage(int pageNum
)
1977 return (pageNum
== 1);
1980 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1982 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1988 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1996 #endif // wxUSE_PRINTING_ARCHITECTURE
1998 // ----------------------------------------------------------------------------
1999 // File history (a.k.a. MRU, most recently used, files list)
2000 // ----------------------------------------------------------------------------
2002 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2004 m_fileMaxFiles
= maxFiles
;
2008 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2010 // check if we don't already have this file
2011 const wxFileName
fnNew(file
);
2013 numFiles
= m_fileHistory
.size();
2014 for ( i
= 0; i
< numFiles
; i
++ )
2016 if ( fnNew
== m_fileHistory
[i
] )
2018 // we do have it, move it to the top of the history
2019 RemoveFileFromHistory(i
);
2025 // if we already have a full history, delete the one at the end
2026 if ( numFiles
== m_fileMaxFiles
)
2028 RemoveFileFromHistory(--numFiles
);
2031 // add a new menu item to all file menus (they will be updated below)
2032 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2034 node
= node
->GetNext() )
2036 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2038 if ( !numFiles
&& menu
->GetMenuItemCount() )
2039 menu
->AppendSeparator();
2041 // label doesn't matter, it will be set below anyhow, but it can't
2042 // be empty (this is supposed to indicate a stock item)
2043 menu
->Append(m_idBase
+ numFiles
, " ");
2046 // insert the new file in the beginning of the file history
2047 m_fileHistory
.insert(m_fileHistory
.begin(), file
);
2050 // update the labels in all menus
2051 for ( i
= 0; i
< numFiles
; i
++ )
2053 // if in same directory just show the filename; otherwise the full path
2054 const wxFileName
fnOld(m_fileHistory
[i
]);
2056 wxString pathInMenu
;
2057 if ( fnOld
.GetPath() == fnNew
.GetPath() )
2059 pathInMenu
= fnOld
.GetFullName();
2061 else // file in different directory
2063 // absolute path; could also set relative path
2064 pathInMenu
= m_fileHistory
[i
];
2067 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2069 node
= node
->GetNext() )
2071 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2073 menu
->SetLabel(m_idBase
+ i
, GetMRUEntryLabel(i
, pathInMenu
));
2078 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2080 size_t numFiles
= m_fileHistory
.size();
2081 wxCHECK_RET( i
< numFiles
,
2082 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2084 // delete the element from the array
2085 m_fileHistory
.RemoveAt(i
);
2088 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2090 node
= node
->GetNext() )
2092 wxMenu
* const menu
= (wxMenu
*) node
->GetData();
2094 // shift filenames up
2095 for ( size_t j
= i
; j
< numFiles
; j
++ )
2097 menu
->SetLabel(m_idBase
+ j
, GetMRUEntryLabel(j
, m_fileHistory
[j
]));
2100 // delete the last menu item which is unused now
2101 const wxWindowID lastItemId
= m_idBase
+ numFiles
;
2102 if ( menu
->FindItem(lastItemId
) )
2103 menu
->Delete(lastItemId
);
2105 // delete the last separator too if no more files are left
2106 if ( m_fileHistory
.empty() )
2108 const wxMenuItemList::compatibility_iterator
2109 nodeLast
= menu
->GetMenuItems().GetLast();
2112 wxMenuItem
* const lastMenuItem
= nodeLast
->GetData();
2113 if ( lastMenuItem
->IsSeparator() )
2114 menu
->Delete(lastMenuItem
);
2116 //else: menu is empty somehow
2121 void wxFileHistory::UseMenu(wxMenu
*menu
)
2123 if ( !m_fileMenus
.Member(menu
) )
2124 m_fileMenus
.Append(menu
);
2127 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2129 m_fileMenus
.DeleteObject(menu
);
2133 void wxFileHistory::Load(const wxConfigBase
& config
)
2135 m_fileHistory
.Clear();
2138 buf
.Printf(wxT("file%d"), 1);
2140 wxString historyFile
;
2141 while ((m_fileHistory
.GetCount() < m_fileMaxFiles
) &&
2142 config
.Read(buf
, &historyFile
) && !historyFile
.empty())
2144 m_fileHistory
.Add(historyFile
);
2146 buf
.Printf(wxT("file%d"), (int)m_fileHistory
.GetCount()+1);
2147 historyFile
= wxEmptyString
;
2153 void wxFileHistory::Save(wxConfigBase
& config
)
2156 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2159 buf
.Printf(wxT("file%d"), (int)i
+1);
2160 if (i
< m_fileHistory
.GetCount())
2161 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2163 config
.Write(buf
, wxEmptyString
);
2166 #endif // wxUSE_CONFIG
2168 void wxFileHistory::AddFilesToMenu()
2170 if ( m_fileHistory
.empty() )
2173 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2175 node
= node
->GetNext() )
2177 AddFilesToMenu((wxMenu
*) node
->GetData());
2181 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2183 if ( m_fileHistory
.empty() )
2186 if ( menu
->GetMenuItemCount() )
2187 menu
->AppendSeparator();
2189 for ( size_t i
= 0; i
< m_fileHistory
.GetCount(); i
++ )
2191 menu
->Append(m_idBase
+ i
, GetMRUEntryLabel(i
, m_fileHistory
[i
]));
2195 // ----------------------------------------------------------------------------
2196 // Permits compatibility with existing file formats and functions that
2197 // manipulate files directly
2198 // ----------------------------------------------------------------------------
2200 #if wxUSE_STD_IOSTREAM
2202 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2204 wxFFile
file(filename
, _T("rb"));
2205 if ( !file
.IsOpened() )
2213 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2217 stream
.write(buf
, nRead
);
2221 while ( !file
.Eof() );
2226 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2228 wxFFile
file(filename
, _T("wb"));
2229 if ( !file
.IsOpened() )
2235 stream
.read(buf
, WXSIZEOF(buf
));
2236 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2238 if ( !file
.Write(buf
, stream
.gcount()) )
2242 while ( !stream
.eof() );
2247 #else // !wxUSE_STD_IOSTREAM
2249 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2251 wxFFile
file(filename
, _T("rb"));
2252 if ( !file
.IsOpened() )
2260 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2264 stream
.Write(buf
, nRead
);
2268 while ( !file
.Eof() );
2273 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2275 wxFFile
file(filename
, _T("wb"));
2276 if ( !file
.IsOpened() )
2282 stream
.Read(buf
, WXSIZEOF(buf
));
2284 const size_t nRead
= stream
.LastRead();
2293 if ( !file
.Write(buf
, nRead
) )
2300 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2302 #endif // wxUSE_DOC_VIEW_ARCHITECTURE