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/vector.h"
61 #if wxUSE_STD_IOSTREAM
62 #include "wx/ioswrap.h"
63 #include "wx/beforestd.h"
69 #include "wx/afterstd.h"
71 #include "wx/wfstream.h"
74 typedef wxVector
<wxDocTemplate
*> wxDocTemplates
;
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
81 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
82 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
83 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
84 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
85 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
87 #if wxUSE_PRINTING_ARCHITECTURE
88 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
91 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
93 // ============================================================================
95 // ============================================================================
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
104 wxWindow
*wxFindSuitableParent()
106 wxWindow
* const win
= wxGetTopLevelParent(wxWindow::FindFocus());
108 return win
? win
: wxTheApp
->GetTopWindow();
111 wxString
FindExtension(const wxString
& path
)
114 wxSplitPath(path
, NULL
, NULL
, &ext
);
116 // VZ: extensions are considered not case sensitive - is this really a good
118 return ext
.MakeLower();
121 // return the string used for the MRU list items in the menu
123 // NB: the index n is 0-based, as usual, but the strings start from 1
124 wxString
GetMRUEntryLabel(int n
, const wxString
& path
)
126 // we need to quote '&' characters which are used for mnemonics
127 wxString
pathInMenu(path
);
128 pathInMenu
.Replace("&", "&&");
130 return wxString::Format("&%d %s", n
+ 1, pathInMenu
);
133 } // anonymous namespace
135 // ----------------------------------------------------------------------------
136 // Definition of wxDocument
137 // ----------------------------------------------------------------------------
139 wxDocument::wxDocument(wxDocument
*parent
)
141 m_documentModified
= false;
142 m_documentParent
= parent
;
143 m_documentTemplate
= NULL
;
144 m_commandProcessor
= NULL
;
148 bool wxDocument::DeleteContents()
153 wxDocument::~wxDocument()
157 if (m_commandProcessor
)
158 delete m_commandProcessor
;
160 if (GetDocumentManager())
161 GetDocumentManager()->RemoveDocument(this);
163 // Not safe to do here, since it'll invoke virtual view functions
164 // expecting to see valid derived objects: and by the time we get here,
165 // we've called destructors higher up.
169 bool wxDocument::Close()
171 if (OnSaveModified())
172 return OnCloseDocument();
177 bool wxDocument::OnCloseDocument()
179 // Tell all views that we're about to close
186 // Note that this implicitly deletes the document when the last view is
188 bool wxDocument::DeleteAllViews()
190 wxDocManager
* manager
= GetDocumentManager();
192 // first check if all views agree to be closed
193 const wxList::iterator end
= m_documentViews
.end();
194 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
196 wxView
*view
= (wxView
*)*i
;
197 if ( !view
->Close() )
201 // all views agreed to close, now do close them
202 if ( m_documentViews
.empty() )
204 // normally the document would be implicitly deleted when the last view
205 // is, but if don't have any views, do it here instead
206 if ( manager
&& manager
->GetDocuments().Member(this) )
211 // as we delete elements we iterate over, don't use the usual "from
212 // begin to end" loop
215 wxView
*view
= (wxView
*)*m_documentViews
.begin();
217 bool isLastOne
= m_documentViews
.size() == 1;
219 // this always deletes the node implicitly and if this is the last
220 // view also deletes this object itself (also implicitly, great),
221 // so we can't test for m_documentViews.empty() after calling this!
232 wxView
*wxDocument::GetFirstView() const
234 if (m_documentViews
.GetCount() == 0)
236 return (wxView
*)m_documentViews
.GetFirst()->GetData();
239 wxDocManager
*wxDocument::GetDocumentManager() const
241 return m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : NULL
;
244 bool wxDocument::OnNewDocument()
246 if ( !OnSaveModified() )
251 SetDocumentSaved(false);
253 const wxString name
= GetDocumentManager()->MakeNewDocumentName();
255 SetFilename(name
, true);
260 bool wxDocument::Save()
262 if (!IsModified() && m_savedYet
)
265 if ( m_documentFile
.empty() || !m_savedYet
)
268 return OnSaveDocument(m_documentFile
);
271 bool wxDocument::SaveAs()
273 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
277 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
278 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
280 // Now see if there are some other template with identical view and document
281 // classes, whose filters may also be used.
283 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
285 wxList::compatibility_iterator node
= docTemplate
->GetDocumentManager()->GetTemplates().GetFirst();
288 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
290 if (t
->IsVisible() && t
!= docTemplate
&&
291 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
292 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
294 // add a '|' to separate this filter from the previous one
295 if ( !filter
.empty() )
298 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
299 << t
->GetFileFilter();
302 node
= node
->GetNext();
306 wxString filter
= docTemplate
->GetFileFilter() ;
308 wxString defaultDir
= docTemplate
->GetDirectory();
309 if (defaultDir
.IsEmpty())
310 defaultDir
= wxPathOnly(GetFilename());
312 wxString tmp
= wxFileSelector(_("Save As"),
314 wxFileNameFromPath(GetFilename()),
315 docTemplate
->GetDefaultExtension(),
317 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
318 GetDocumentWindow());
323 wxString
fileName(tmp
);
324 wxString path
, name
, ext
;
325 wxSplitPath(fileName
, & path
, & name
, & ext
);
329 fileName
+= wxT(".");
330 fileName
+= docTemplate
->GetDefaultExtension();
333 SetFilename(fileName
);
334 SetTitle(wxFileNameFromPath(fileName
));
336 // Notify the views that the filename has changed
337 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
340 wxView
*view
= (wxView
*)node
->GetData();
341 view
->OnChangeFilename();
342 node
= node
->GetNext();
345 // Files that were not saved correctly are not added to the FileHistory.
346 if (!OnSaveDocument(m_documentFile
))
349 // A file that doesn't use the default extension of its document template cannot be opened
350 // via the FileHistory, so we do not add it.
351 if (docTemplate
->FileMatchesTemplate(fileName
))
353 GetDocumentManager()->AddFileToHistory(fileName
);
357 // The user will probably not be able to open the file again, so
358 // we could warn about the wrong file-extension here.
363 bool wxDocument::OnSaveDocument(const wxString
& file
)
368 if ( !DoSaveDocument(file
) )
373 SetDocumentSaved(true);
374 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
375 wxFileName
fn(file
) ;
376 fn
.MacSetDefaultTypeAndCreator() ;
381 bool wxDocument::OnOpenDocument(const wxString
& file
)
383 if ( !OnSaveModified() )
386 if ( !DoOpenDocument(file
) )
389 SetFilename(file
, true);
398 #if wxUSE_STD_IOSTREAM
399 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
401 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
407 #if wxUSE_STD_IOSTREAM
408 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
410 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
416 bool wxDocument::Revert()
422 // Get title, or filename if no title, else unnamed
423 #if WXWIN_COMPATIBILITY_2_8
424 bool wxDocument::GetPrintableName(wxString
& buf
) const
426 // this function can not only be overridden by the user code but also
427 // called by it so we need to ensure that we return the same thing as
428 // GetUserReadableName() but we can't call it because this would result in
429 // an infinite recursion, hence we use the helper DoGetUserReadableName()
430 buf
= DoGetUserReadableName();
434 #endif // WXWIN_COMPATIBILITY_2_8
436 wxString
wxDocument::GetUserReadableName() const
438 #if WXWIN_COMPATIBILITY_2_8
439 // we need to call the old virtual function to ensure that the overridden
440 // version of it is still called
442 if ( GetPrintableName(name
) )
444 #endif // WXWIN_COMPATIBILITY_2_8
446 return DoGetUserReadableName();
449 wxString
wxDocument::DoGetUserReadableName() const
451 if ( !m_documentTitle
.empty() )
452 return m_documentTitle
;
454 if ( !m_documentFile
.empty() )
455 return wxFileNameFromPath(m_documentFile
);
460 wxWindow
*wxDocument::GetDocumentWindow() const
462 wxView
*view
= GetFirstView();
464 return view
->GetFrame();
466 return wxTheApp
->GetTopWindow();
469 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
471 return new wxCommandProcessor
;
474 // true if safe to close
475 bool wxDocument::OnSaveModified()
479 switch ( wxMessageBox
483 _("Do you want to save changes to document %s?"),
484 GetUserReadableName()
486 wxTheApp
->GetAppDisplayName(),
487 wxYES_NO
| wxCANCEL
| wxICON_QUESTION
,
506 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
511 bool wxDocument::AddView(wxView
*view
)
513 if ( !m_documentViews
.Member(view
) )
515 m_documentViews
.Append(view
);
521 bool wxDocument::RemoveView(wxView
*view
)
523 (void)m_documentViews
.DeleteObject(view
);
528 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
530 return GetDocumentTemplate()->CreateView(this, flags
) != NULL
;
533 // Called after a view is added or removed.
534 // The default implementation deletes the document if
535 // there are no more views.
536 void wxDocument::OnChangedViewList()
538 if ( m_documentViews
.empty() && OnSaveModified() )
542 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
544 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
547 wxView
*view
= (wxView
*)node
->GetData();
549 view
->OnUpdate(sender
, hint
);
550 node
= node
->GetNext();
554 void wxDocument::NotifyClosing()
556 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
559 wxView
*view
= (wxView
*)node
->GetData();
560 view
->OnClosingDocument();
561 node
= node
->GetNext();
565 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
567 m_documentFile
= filename
;
570 // Notify the views that the filename has changed
571 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
574 wxView
*view
= (wxView
*)node
->GetData();
575 view
->OnChangeFilename();
576 node
= node
->GetNext();
581 bool wxDocument::DoSaveDocument(const wxString
& file
)
584 if (!wxTheApp
->GetAppDisplayName().empty())
585 msgTitle
= wxTheApp
->GetAppDisplayName();
587 msgTitle
= wxString(_("File error"));
589 #if wxUSE_STD_IOSTREAM
590 wxSTD ofstream
store(file
.mb_str(), wxSTD
ios::binary
);
591 if (store
.fail() || store
.bad())
593 wxFileOutputStream
store(file
);
594 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
597 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
598 GetDocumentWindow());
602 if (!SaveObject(store
))
604 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
605 GetDocumentWindow());
613 bool wxDocument::DoOpenDocument(const wxString
& file
)
615 #if wxUSE_STD_IOSTREAM
616 wxSTD ifstream
store(file
.mb_str(), wxSTD
ios::binary
);
617 if (!store
.fail() && !store
.bad())
619 wxFileInputStream
store(file
);
620 if (store
.GetLastError() == wxSTREAM_NO_ERROR
)
623 #if wxUSE_STD_IOSTREAM
625 if ( !!store
|| store
.eof() )
627 int res
= LoadObject(store
).GetLastError();
628 if ( res
== wxSTREAM_NO_ERROR
|| res
== wxSTREAM_EOF
)
633 wxLogError(_("Sorry, could not open this file."));
638 // ----------------------------------------------------------------------------
640 // ----------------------------------------------------------------------------
644 m_viewDocument
= NULL
;
651 GetDocumentManager()->ActivateView(this, false);
652 m_viewDocument
->RemoveView(this);
655 bool wxView::TryValidator(wxEvent
& event
)
657 wxDocument
* const doc
= GetDocument();
658 return doc
&& doc
->ProcessEventHere(event
);
661 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
665 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
670 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
674 void wxView::OnChangeFilename()
676 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
677 // generic MDI implementation so use SetLabel rather than SetTitle.
678 // It should cause SetTitle() for top level windows.
679 wxWindow
*win
= GetFrame();
682 wxDocument
*doc
= GetDocument();
685 win
->SetLabel(doc
->GetUserReadableName());
688 void wxView::SetDocument(wxDocument
*doc
)
690 m_viewDocument
= doc
;
695 bool wxView::Close(bool deleteWindow
)
697 return OnClose(deleteWindow
);
700 void wxView::Activate(bool activate
)
702 if (GetDocument() && GetDocumentManager())
704 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
705 GetDocumentManager()->ActivateView(this, activate
);
709 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
711 return GetDocument() ? GetDocument()->Close() : true;
714 #if wxUSE_PRINTING_ARCHITECTURE
715 wxPrintout
*wxView::OnCreatePrintout()
717 return new wxDocPrintout(this);
719 #endif // wxUSE_PRINTING_ARCHITECTURE
721 // ----------------------------------------------------------------------------
723 // ----------------------------------------------------------------------------
725 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
726 const wxString
& descr
,
727 const wxString
& filter
,
730 const wxString
& docTypeName
,
731 const wxString
& viewTypeName
,
732 wxClassInfo
*docClassInfo
,
733 wxClassInfo
*viewClassInfo
,
736 m_documentManager
= manager
;
737 m_description
= descr
;
740 m_fileFilter
= filter
;
742 m_docTypeName
= docTypeName
;
743 m_viewTypeName
= viewTypeName
;
744 m_documentManager
->AssociateTemplate(this);
746 m_docClassInfo
= docClassInfo
;
747 m_viewClassInfo
= viewClassInfo
;
750 wxDocTemplate::~wxDocTemplate()
752 m_documentManager
->DisassociateTemplate(this);
755 // Tries to dynamically construct an object of the right class.
756 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
758 wxDocument
* const doc
= DoCreateDocument();
760 // VZ: this code doesn't delete doc if InitDocument() (i.e. doc->OnCreate())
761 // fails, is this intentional?
763 return doc
&& InitDocument(doc
, path
, flags
) ? doc
: NULL
;
767 wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
769 doc
->SetFilename(path
);
770 doc
->SetDocumentTemplate(this);
771 GetDocumentManager()->AddDocument(doc
);
772 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
774 if (doc
->OnCreate(path
, flags
))
778 if (GetDocumentManager()->GetDocuments().Member(doc
))
779 doc
->DeleteAllViews();
784 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
786 wxView
*view
= DoCreateView();
790 view
->SetDocument(doc
);
791 if (view
->OnCreate(doc
, flags
))
802 // The default (very primitive) format detection: check is the extension is
803 // that of the template
804 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
806 wxStringTokenizer
parser (GetFileFilter(), wxT(";"));
807 wxString anything
= wxT ("*");
808 while (parser
.HasMoreTokens())
810 wxString filter
= parser
.GetNextToken();
811 wxString filterExt
= FindExtension (filter
);
812 if ( filter
.IsSameAs (anything
) ||
813 filterExt
.IsSameAs (anything
) ||
814 filterExt
.IsSameAs (FindExtension (path
)) )
817 return GetDefaultExtension().IsSameAs(FindExtension(path
));
820 wxDocument
*wxDocTemplate::DoCreateDocument()
825 return (wxDocument
*)m_docClassInfo
->CreateObject();
828 wxView
*wxDocTemplate::DoCreateView()
830 if (!m_viewClassInfo
)
833 return (wxView
*)m_viewClassInfo
->CreateObject();
836 // ----------------------------------------------------------------------------
838 // ----------------------------------------------------------------------------
840 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
841 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
842 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
843 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
844 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
845 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
846 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
847 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
848 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
849 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
851 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
852 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateDisableIfNoDoc
)
853 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateDisableIfNoDoc
)
854 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateDisableIfNoDoc
)
855 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
856 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
857 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateDisableIfNoDoc
)
858 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
859 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
861 #if wxUSE_PRINTING_ARCHITECTURE
862 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
863 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
865 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdateDisableIfNoDoc
)
866 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdateDisableIfNoDoc
)
870 wxDocManager
* wxDocManager::sm_docManager
= NULL
;
872 wxDocManager::wxDocManager(long WXUNUSED(flags
), bool initialize
)
874 wxASSERT_MSG( !sm_docManager
, "multiple wxDocManagers not allowed" );
876 sm_docManager
= this;
878 m_defaultDocumentNameCounter
= 1;
879 m_currentView
= NULL
;
880 m_maxDocsOpen
= INT_MAX
;
881 m_fileHistory
= NULL
;
886 wxDocManager::~wxDocManager()
889 delete m_fileHistory
;
890 sm_docManager
= NULL
;
893 // closes the specified document
894 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
896 if (doc
->Close() || force
)
898 // Implicitly deletes the document when
899 // the last view is deleted
900 doc
->DeleteAllViews();
902 // Check we're really deleted
903 if (m_docs
.Member(doc
))
911 bool wxDocManager::CloseDocuments(bool force
)
913 wxList::compatibility_iterator node
= m_docs
.GetFirst();
916 wxDocument
*doc
= (wxDocument
*)node
->GetData();
917 wxList::compatibility_iterator next
= node
->GetNext();
919 if (!CloseDocument(doc
, force
))
922 // This assumes that documents are not connected in
923 // any way, i.e. deleting one document does NOT
930 bool wxDocManager::Clear(bool force
)
932 if (!CloseDocuments(force
))
935 m_currentView
= NULL
;
937 wxList::compatibility_iterator node
= m_templates
.GetFirst();
940 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
941 wxList::compatibility_iterator next
= node
->GetNext();
948 bool wxDocManager::Initialize()
950 m_fileHistory
= OnCreateFileHistory();
954 wxFileHistory
*wxDocManager::OnCreateFileHistory()
956 return new wxFileHistory
;
959 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
961 wxDocument
*doc
= GetCurrentDocument();
966 doc
->DeleteAllViews();
967 if (m_docs
.Member(doc
))
972 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
974 CloseDocuments(false);
977 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
979 CreateDocument( wxEmptyString
, wxDOC_NEW
);
982 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
984 if ( !CreateDocument( wxEmptyString
, 0) )
990 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
992 wxDocument
*doc
= GetCurrentDocument();
998 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1000 wxDocument
*doc
= GetCurrentDocument();
1006 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
1008 wxDocument
*doc
= GetCurrentDocument();
1014 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
1016 #if wxUSE_PRINTING_ARCHITECTURE
1017 wxView
*view
= GetCurrentView();
1021 wxPrintout
*printout
= view
->OnCreatePrintout();
1025 printer
.Print(view
->GetFrame(), printout
, true);
1029 #endif // wxUSE_PRINTING_ARCHITECTURE
1032 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1034 #if wxUSE_PRINTING_ARCHITECTURE
1035 wxView
*view
= GetCurrentView();
1039 wxPrintout
*printout
= view
->OnCreatePrintout();
1042 // Pass two printout objects: for preview, and possible printing.
1043 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1044 if ( !preview
->Ok() )
1047 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1051 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1052 wxPoint(100, 100), wxSize(600, 650));
1053 frame
->Centre(wxBOTH
);
1054 frame
->Initialize();
1057 #endif // wxUSE_PRINTING_ARCHITECTURE
1060 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1062 wxDocument
*doc
= GetCurrentDocument();
1065 if (doc
->GetCommandProcessor())
1066 doc
->GetCommandProcessor()->Undo();
1071 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1073 wxDocument
*doc
= GetCurrentDocument();
1076 if (doc
->GetCommandProcessor())
1077 doc
->GetCommandProcessor()->Redo();
1082 // Handlers for UI update commands
1084 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1086 event
.Enable( true );
1089 void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent
& event
)
1091 event
.Enable( GetCurrentDocument() != NULL
);
1094 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1096 event
.Enable( true );
1099 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1101 wxDocument
*doc
= GetCurrentDocument();
1102 event
.Enable( doc
&& doc
->IsModified() );
1105 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1107 wxDocument
*doc
= GetCurrentDocument();
1109 event
.Enable(false);
1110 else if (!doc
->GetCommandProcessor())
1114 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1115 doc
->GetCommandProcessor()->SetMenuStrings();
1119 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1121 wxDocument
*doc
= GetCurrentDocument();
1123 event
.Enable(false);
1124 else if (!doc
->GetCommandProcessor())
1128 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1129 doc
->GetCommandProcessor()->SetMenuStrings();
1133 wxView
*wxDocManager::GetCurrentView() const
1136 return m_currentView
;
1137 if (m_docs
.GetCount() == 1)
1139 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1140 return doc
->GetFirstView();
1145 bool wxDocManager::TryValidator(wxEvent
& event
)
1147 wxView
* const view
= GetCurrentView();
1148 return view
&& view
->ProcessEventHere(event
);
1154 // helper function: return only the visible templates
1155 wxDocTemplates
GetVisibleTemplates(const wxList
& allTemplates
)
1157 // select only the visible templates
1158 const size_t totalNumTemplates
= allTemplates
.GetCount();
1159 wxDocTemplates templates
;
1160 if ( totalNumTemplates
)
1162 templates
.reserve(totalNumTemplates
);
1164 for ( wxList::const_iterator i
= allTemplates
.begin(),
1165 end
= allTemplates
.end();
1169 wxDocTemplate
* const temp
= (wxDocTemplate
*)*i
;
1170 if ( temp
->IsVisible() )
1171 templates
.push_back(temp
);
1178 } // anonymous namespace
1180 wxDocument
*wxDocManager::CreateDocument(const wxString
& pathOrig
, long flags
)
1182 // this ought to be const but SelectDocumentType/Path() are not
1183 // const-correct and can't be changed as, being virtual, this risks
1184 // breaking user code overriding them
1185 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1186 const size_t numTemplates
= templates
.size();
1187 if ( !numTemplates
)
1189 // no templates can be used, can't create document
1194 // normally user should select the template to use but wxDOC_SILENT flag we
1195 // choose one ourselves
1196 wxString path
= pathOrig
; // may be modified below
1197 wxDocTemplate
*temp
;
1198 if ( flags
& wxDOC_SILENT
)
1200 wxASSERT_MSG( !path
.empty(),
1201 "using empty path with wxDOC_SILENT doesn't make sense" );
1203 temp
= FindTemplateForPath(path
);
1206 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1210 else // not silent, ask the user
1212 // for the new file we need just the template, for an existing one we
1213 // need the template and the path, unless it's already specified
1214 if ( (flags
& wxDOC_NEW
) || !path
.empty() )
1215 temp
= SelectDocumentType(&templates
[0], numTemplates
);
1217 temp
= SelectDocumentPath(&templates
[0], numTemplates
, path
, flags
);
1223 // check whether the document with this path is already opened
1224 if ( !path
.empty() )
1226 const wxFileName
fn(path
);
1227 for ( wxList::const_iterator i
= m_docs
.begin(); i
!= m_docs
.end(); ++i
)
1229 wxDocument
* const doc
= (wxDocument
*)*i
;
1231 if ( fn
== doc
->GetFilename() )
1233 // file already open, just activate it and return
1234 if ( doc
->GetFirstView() )
1236 ActivateView(doc
->GetFirstView());
1237 if ( doc
->GetDocumentWindow() )
1238 doc
->GetDocumentWindow()->SetFocus();
1246 // no, we need to create a new document
1249 // if we've reached the max number of docs, close the first one.
1250 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1252 if ( !CloseDocument((wxDocument
*)GetDocuments().GetFirst()->GetData()) )
1254 // can't open the new document if closing the old one failed
1260 // do create and initialize the new document finally
1261 wxDocument
* const docNew
= temp
->CreateDocument(path
, flags
);
1265 docNew
->SetDocumentName(temp
->GetDocumentName());
1266 docNew
->SetDocumentTemplate(temp
);
1268 // call the appropriate function depending on whether we're creating a new
1269 // file or opening an existing one
1270 if ( !(flags
& wxDOC_NEW
? docNew
->OnNewDocument()
1271 : docNew
->OnOpenDocument(path
)) )
1273 // Document is implicitly deleted by DeleteAllViews
1274 docNew
->DeleteAllViews();
1278 // add the successfully opened file to MRU, but only if we're going to be
1279 // able to reopen it successfully later which requires the template for
1280 // this document to be retrievable from the file extension
1281 if ( !(flags
& wxDOC_NEW
) && temp
->FileMatchesTemplate(path
) )
1282 AddFileToHistory(path
);
1287 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1289 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1290 const size_t numTemplates
= templates
.size();
1292 if ( numTemplates
== 0 )
1295 wxDocTemplate
* const
1296 temp
= numTemplates
== 1 ? templates
[0]
1297 : SelectViewType(&templates
[0], numTemplates
);
1302 wxView
*view
= temp
->CreateView(doc
, flags
);
1304 view
->SetViewName(temp
->GetViewName());
1308 // Not yet implemented
1310 wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1314 // Not yet implemented
1315 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1320 wxDocument
*wxDocManager::GetCurrentDocument() const
1322 wxView
*view
= GetCurrentView();
1324 return view
->GetDocument();
1329 // Make a default name for a new document
1330 #if WXWIN_COMPATIBILITY_2_8
1331 bool wxDocManager::MakeDefaultName(wxString
& WXUNUSED(name
))
1333 // we consider that this function can only be overridden by the user code,
1334 // not called by it as it only makes sense to call it internally, so we
1335 // don't bother to return anything from here
1338 #endif // WXWIN_COMPATIBILITY_2_8
1340 wxString
wxDocManager::MakeNewDocumentName()
1344 #if WXWIN_COMPATIBILITY_2_8
1345 if ( !MakeDefaultName(name
) )
1346 #endif // WXWIN_COMPATIBILITY_2_8
1348 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1349 m_defaultDocumentNameCounter
++;
1355 // Make a frame title (override this to do something different)
1356 // If docName is empty, a document is not currently active.
1357 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1359 wxString appName
= wxTheApp
->GetAppDisplayName();
1365 wxString docName
= doc
->GetUserReadableName();
1366 title
= docName
+ wxString(_(" - ")) + appName
;
1372 // Not yet implemented
1373 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1378 // File history management
1379 void wxDocManager::AddFileToHistory(const wxString
& file
)
1382 m_fileHistory
->AddFileToHistory(file
);
1385 void wxDocManager::RemoveFileFromHistory(size_t i
)
1388 m_fileHistory
->RemoveFileFromHistory(i
);
1391 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1396 histFile
= m_fileHistory
->GetHistoryFile(i
);
1401 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1404 m_fileHistory
->UseMenu(menu
);
1407 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1410 m_fileHistory
->RemoveMenu(menu
);
1414 void wxDocManager::FileHistoryLoad(const wxConfigBase
& config
)
1417 m_fileHistory
->Load(config
);
1420 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1423 m_fileHistory
->Save(config
);
1427 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1430 m_fileHistory
->AddFilesToMenu(menu
);
1433 void wxDocManager::FileHistoryAddFilesToMenu()
1436 m_fileHistory
->AddFilesToMenu();
1439 size_t wxDocManager::GetHistoryFilesCount() const
1441 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1445 // Find out the document template via matching in the document file format
1446 // against that of the template
1447 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1449 wxDocTemplate
*theTemplate
= NULL
;
1451 // Find the template which this extension corresponds to
1452 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1454 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1455 if ( temp
->FileMatchesTemplate(path
) )
1464 // Prompts user to open a file, using file specs in templates.
1465 // Must extend the file selector dialog or implement own; OR
1466 // match the extension to the template extension.
1468 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1471 long WXUNUSED(flags
),
1472 bool WXUNUSED(save
))
1474 // We can only have multiple filters in Windows and GTK
1475 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1479 for (i
= 0; i
< noTemplates
; i
++)
1481 if (templates
[i
]->IsVisible())
1483 // add a '|' to separate this filter from the previous one
1484 if ( !descrBuf
.empty() )
1485 descrBuf
<< wxT('|');
1487 descrBuf
<< templates
[i
]->GetDescription()
1488 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1489 << templates
[i
]->GetFileFilter();
1493 wxString descrBuf
= wxT("*.*");
1494 wxUnusedVar(noTemplates
);
1497 int FilterIndex
= -1;
1499 wxWindow
* parent
= wxFindSuitableParent();
1501 wxString pathTmp
= wxFileSelectorEx(_("Open File"),
1509 wxDocTemplate
*theTemplate
= NULL
;
1510 if (!pathTmp
.empty())
1512 if (!wxFileExists(pathTmp
))
1515 if (!wxTheApp
->GetAppDisplayName().empty())
1516 msgTitle
= wxTheApp
->GetAppDisplayName();
1518 msgTitle
= wxString(_("File error"));
1520 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1523 path
= wxEmptyString
;
1526 m_lastDirectory
= wxPathOnly(pathTmp
);
1530 // first choose the template using the extension, if this fails (i.e.
1531 // wxFileSelectorEx() didn't fill it), then use the path
1532 if ( FilterIndex
!= -1 )
1533 theTemplate
= templates
[FilterIndex
];
1535 theTemplate
= FindTemplateForPath(path
);
1538 // Since we do not add files with non-default extensions to the FileHistory this
1539 // can only happen if the application changes the allowed templates in runtime.
1540 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1542 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1547 path
= wxEmptyString
;
1553 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1554 int noTemplates
, bool sort
)
1556 wxArrayString strings
;
1557 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1561 for (i
= 0; i
< noTemplates
; i
++)
1563 if (templates
[i
]->IsVisible())
1567 for (j
= 0; j
< n
; j
++)
1569 //filter out NOT unique documents + view combinations
1570 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1571 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1578 strings
.Add(templates
[i
]->m_description
);
1580 data
[n
] = templates
[i
];
1588 strings
.Sort(); // ascending sort
1589 // Yes, this will be slow, but template lists
1590 // are typically short.
1592 n
= strings
.Count();
1593 for (i
= 0; i
< n
; i
++)
1595 for (j
= 0; j
< noTemplates
; j
++)
1597 if (strings
[i
] == templates
[j
]->m_description
)
1598 data
[i
] = templates
[j
];
1603 wxDocTemplate
*theTemplate
;
1608 // no visible templates, hence nothing to choose from
1613 // don't propose the user to choose if he has no choice
1614 theTemplate
= data
[0];
1618 // propose the user to choose one of several
1619 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1621 _("Select a document template"),
1625 wxFindSuitableParent()
1634 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1635 int noTemplates
, bool sort
)
1637 wxArrayString strings
;
1638 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1642 for (i
= 0; i
< noTemplates
; i
++)
1644 wxDocTemplate
*templ
= templates
[i
];
1645 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1649 for (j
= 0; j
< n
; j
++)
1651 //filter out NOT unique views
1652 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1658 strings
.Add(templ
->m_viewTypeName
);
1667 strings
.Sort(); // ascending sort
1668 // Yes, this will be slow, but template lists
1669 // are typically short.
1671 n
= strings
.Count();
1672 for (i
= 0; i
< n
; i
++)
1674 for (j
= 0; j
< noTemplates
; j
++)
1676 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1677 data
[i
] = templates
[j
];
1682 wxDocTemplate
*theTemplate
;
1684 // the same logic as above
1692 theTemplate
= data
[0];
1696 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1698 _("Select a document view"),
1702 wxFindSuitableParent()
1711 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1713 if (!m_templates
.Member(temp
))
1714 m_templates
.Append(temp
);
1717 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1719 m_templates
.DeleteObject(temp
);
1722 // Add and remove a document from the manager's list
1723 void wxDocManager::AddDocument(wxDocument
*doc
)
1725 if (!m_docs
.Member(doc
))
1729 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1731 m_docs
.DeleteObject(doc
);
1734 // Views or windows should inform the document manager
1735 // when a view is going in or out of focus
1736 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1740 m_currentView
= view
;
1744 if ( m_currentView
== view
)
1746 // don't keep stale pointer
1747 m_currentView
= NULL
;
1752 // ----------------------------------------------------------------------------
1753 // Default document child frame
1754 // ----------------------------------------------------------------------------
1756 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1757 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1758 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1761 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1765 const wxString
& title
,
1769 const wxString
& name
)
1770 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1772 m_childDocument
= doc
;
1775 view
->SetFrame(this);
1778 bool wxDocChildFrame::TryValidator(wxEvent
& event
)
1783 // FIXME: why is this needed here?
1784 m_childView
->Activate(true);
1786 return m_childView
->ProcessEventHere(event
);
1789 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1791 wxFrame::OnActivate(event
);
1794 m_childView
->Activate(event
.GetActive());
1797 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1801 bool ans
= event
.CanVeto()
1802 ? m_childView
->Close(false) // false means don't delete associated window
1803 : true; // Must delete.
1807 m_childView
->Activate(false);
1810 m_childDocument
= NULL
;
1821 // ----------------------------------------------------------------------------
1822 // Default parent frame
1823 // ----------------------------------------------------------------------------
1825 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1826 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1827 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1828 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1831 wxDocParentFrame::wxDocParentFrame()
1833 m_docManager
= NULL
;
1836 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1839 const wxString
& title
,
1843 const wxString
& name
)
1844 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1846 m_docManager
= manager
;
1849 bool wxDocParentFrame::Create(wxDocManager
*manager
,
1852 const wxString
& title
,
1856 const wxString
& name
)
1858 m_docManager
= manager
;
1859 return base_type::Create(frame
, id
, title
, pos
, size
, style
, name
);
1862 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1867 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1869 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1870 wxString
filename(m_docManager
->GetHistoryFile(n
));
1871 if ( filename
.empty() )
1874 wxString errMsg
; // must contain exactly one "%s" if non-empty
1875 if ( wxFile::Exists(filename
) )
1878 if ( m_docManager
->CreateDocument(filename
, wxDOC_SILENT
) )
1881 errMsg
= _("The file '%s' couldn't be opened.");
1883 else // file doesn't exist
1885 errMsg
= _("The file '%s' doesn't exist and couldn't be opened.");
1889 wxASSERT_MSG( !errMsg
.empty(), "should have an error message" );
1891 // remove the file which we can't open from the MRU list
1892 m_docManager
->RemoveFileFromHistory(n
);
1894 // and tell the user about it
1895 wxLogError(errMsg
+ '\n' +
1896 _("It has been removed from the most recently used files list."),
1900 // Extend event processing to search the view's event table
1901 bool wxDocParentFrame::TryValidator(wxEvent
& event
)
1903 return m_docManager
&& m_docManager
->ProcessEventHere(event
);
1906 // Define the behaviour for the frame closing
1907 // - must delete all frames except for the main one.
1908 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1910 if (m_docManager
->Clear(!event
.CanVeto()))
1918 #if wxUSE_PRINTING_ARCHITECTURE
1920 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1923 m_printoutView
= view
;
1926 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1930 // Get the logical pixels per inch of screen and printer
1931 int ppiScreenX
, ppiScreenY
;
1932 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1933 wxUnusedVar(ppiScreenY
);
1934 int ppiPrinterX
, ppiPrinterY
;
1935 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1936 wxUnusedVar(ppiPrinterY
);
1938 // This scales the DC so that the printout roughly represents the
1939 // the screen scaling. The text point size _should_ be the right size
1940 // but in fact is too small for some reason. This is a detail that will
1941 // need to be addressed at some point but can be fudged for the
1943 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1945 // Now we have to check in case our real page size is reduced
1946 // (e.g. because we're drawing to a print preview memory DC)
1947 int pageWidth
, pageHeight
;
1949 dc
->GetSize(&w
, &h
);
1950 GetPageSizePixels(&pageWidth
, &pageHeight
);
1951 wxUnusedVar(pageHeight
);
1953 // If printer pageWidth == current DC width, then this doesn't
1954 // change. But w might be the preview bitmap width, so scale down.
1955 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1956 dc
->SetUserScale(overallScale
, overallScale
);
1960 m_printoutView
->OnDraw(dc
);
1965 bool wxDocPrintout::HasPage(int pageNum
)
1967 return (pageNum
== 1);
1970 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1972 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1978 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1986 #endif // wxUSE_PRINTING_ARCHITECTURE
1988 // ----------------------------------------------------------------------------
1989 // File history (a.k.a. MRU, most recently used, files list)
1990 // ----------------------------------------------------------------------------
1992 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
1994 m_fileMaxFiles
= maxFiles
;
1998 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2000 // check if we don't already have this file
2001 const wxFileName
fnNew(file
);
2003 numFiles
= m_fileHistory
.size();
2004 for ( i
= 0; i
< numFiles
; i
++ )
2006 if ( fnNew
== m_fileHistory
[i
] )
2008 // we do have it, move it to the top of the history
2009 RemoveFileFromHistory(i
);
2015 // if we already have a full history, delete the one at the end
2016 if ( numFiles
== m_fileMaxFiles
)
2018 RemoveFileFromHistory(--numFiles
);
2020 else // add a new menu item to all file menus (will be updated below)
2022 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2024 node
= node
->GetNext() )
2026 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2028 if ( !numFiles
&& menu
->GetMenuItemCount() )
2029 menu
->AppendSeparator();
2031 // label doesn't matter, it will be set below anyhow, but it can't
2032 // be empty (this is supposed to indicate a stock item)
2033 menu
->Append(m_idBase
+ numFiles
, " ");
2038 // insert the new file in the beginning of the file history
2039 m_fileHistory
.insert(m_fileHistory
.begin(), file
);
2042 // update the labels in all menus
2043 for ( i
= 0; i
< numFiles
; i
++ )
2045 // if in same directory just show the filename; otherwise the full path
2046 const wxFileName
fnOld(m_fileHistory
[i
]);
2048 wxString pathInMenu
;
2049 if ( fnOld
.GetPath() == fnNew
.GetPath() )
2051 pathInMenu
= fnOld
.GetFullName();
2053 else // file in different directory
2055 // absolute path; could also set relative path
2056 pathInMenu
= m_fileHistory
[i
];
2059 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2061 node
= node
->GetNext() )
2063 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2065 menu
->SetLabel(m_idBase
+ i
, GetMRUEntryLabel(i
, pathInMenu
));
2070 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2072 size_t numFiles
= m_fileHistory
.size();
2073 wxCHECK_RET( i
< numFiles
,
2074 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2076 // delete the element from the array
2077 m_fileHistory
.RemoveAt(i
);
2080 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2082 node
= node
->GetNext() )
2084 wxMenu
* const menu
= (wxMenu
*) node
->GetData();
2086 // shift filenames up
2087 for ( size_t j
= i
; j
< numFiles
; j
++ )
2089 menu
->SetLabel(m_idBase
+ j
, GetMRUEntryLabel(j
, m_fileHistory
[j
]));
2092 // delete the last menu item which is unused now
2093 const wxWindowID lastItemId
= m_idBase
+ numFiles
;
2094 if ( menu
->FindItem(lastItemId
) )
2095 menu
->Delete(lastItemId
);
2097 // delete the last separator too if no more files are left
2098 if ( m_fileHistory
.empty() )
2100 const wxMenuItemList::compatibility_iterator
2101 nodeLast
= menu
->GetMenuItems().GetLast();
2104 wxMenuItem
* const lastMenuItem
= nodeLast
->GetData();
2105 if ( lastMenuItem
->IsSeparator() )
2106 menu
->Delete(lastMenuItem
);
2108 //else: menu is empty somehow
2113 void wxFileHistory::UseMenu(wxMenu
*menu
)
2115 if ( !m_fileMenus
.Member(menu
) )
2116 m_fileMenus
.Append(menu
);
2119 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2121 m_fileMenus
.DeleteObject(menu
);
2125 void wxFileHistory::Load(const wxConfigBase
& config
)
2127 m_fileHistory
.Clear();
2130 buf
.Printf(wxT("file%d"), 1);
2132 wxString historyFile
;
2133 while ((m_fileHistory
.GetCount() < m_fileMaxFiles
) &&
2134 config
.Read(buf
, &historyFile
) && !historyFile
.empty())
2136 m_fileHistory
.Add(historyFile
);
2138 buf
.Printf(wxT("file%d"), (int)m_fileHistory
.GetCount()+1);
2139 historyFile
= wxEmptyString
;
2145 void wxFileHistory::Save(wxConfigBase
& config
)
2148 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2151 buf
.Printf(wxT("file%d"), (int)i
+1);
2152 if (i
< m_fileHistory
.GetCount())
2153 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2155 config
.Write(buf
, wxEmptyString
);
2158 #endif // wxUSE_CONFIG
2160 void wxFileHistory::AddFilesToMenu()
2162 if ( m_fileHistory
.empty() )
2165 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2167 node
= node
->GetNext() )
2169 AddFilesToMenu((wxMenu
*) node
->GetData());
2173 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2175 if ( m_fileHistory
.empty() )
2178 if ( menu
->GetMenuItemCount() )
2179 menu
->AppendSeparator();
2181 for ( size_t i
= 0; i
< m_fileHistory
.GetCount(); i
++ )
2183 menu
->Append(m_idBase
+ i
, GetMRUEntryLabel(i
, m_fileHistory
[i
]));
2187 // ----------------------------------------------------------------------------
2188 // Permits compatibility with existing file formats and functions that
2189 // manipulate files directly
2190 // ----------------------------------------------------------------------------
2192 #if wxUSE_STD_IOSTREAM
2194 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2196 wxFFile
file(filename
, _T("rb"));
2197 if ( !file
.IsOpened() )
2205 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2209 stream
.write(buf
, nRead
);
2213 while ( !file
.Eof() );
2218 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2220 wxFFile
file(filename
, _T("wb"));
2221 if ( !file
.IsOpened() )
2227 stream
.read(buf
, WXSIZEOF(buf
));
2228 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2230 if ( !file
.Write(buf
, stream
.gcount()) )
2234 while ( !stream
.eof() );
2239 #else // !wxUSE_STD_IOSTREAM
2241 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2243 wxFFile
file(filename
, _T("rb"));
2244 if ( !file
.IsOpened() )
2252 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2256 stream
.Write(buf
, nRead
);
2260 while ( !file
.Eof() );
2265 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2267 wxFFile
file(filename
, _T("wb"));
2268 if ( !file
.IsOpened() )
2274 stream
.Read(buf
, WXSIZEOF(buf
));
2276 const size_t nRead
= stream
.LastRead();
2285 if ( !file
.Write(buf
, nRead
) )
2292 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2294 #endif // wxUSE_DOC_VIEW_ARCHITECTURE